blob: 7706ec850641cba60d160632b7532ee23acec94f [file] [log] [blame]
Ben Murdochbb1529c2013-08-08 10:24:53 +01001// Copyright 2013 The Chromium Authors. All rights reserved.
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +01002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ben Murdochbb1529c2013-08-08 10:24:53 +01005#include "content/browser/dom_storage/dom_storage_session.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +01006
7#include "base/bind.h"
8#include "base/bind_helpers.h"
9#include "base/logging.h"
10#include "base/tracked_objects.h"
Ben Murdochbb1529c2013-08-08 10:24:53 +010011#include "content/browser/dom_storage/dom_storage_context_impl.h"
12#include "content/browser/dom_storage/dom_storage_task_runner.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010013
Ben Murdochbb1529c2013-08-08 10:24:53 +010014namespace content {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010015
Ben Murdochbb1529c2013-08-08 10:24:53 +010016DOMStorageSession::DOMStorageSession(DOMStorageContextImpl* context)
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010017 : context_(context),
18 namespace_id_(context->AllocateSessionId()),
19 persistent_namespace_id_(context->AllocatePersistentSessionId()),
20 should_persist_(false) {
21 context->task_runner()->PostTask(
22 FROM_HERE,
Ben Murdochbb1529c2013-08-08 10:24:53 +010023 base::Bind(&DOMStorageContextImpl::CreateSessionNamespace,
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010024 context_, namespace_id_, persistent_namespace_id_));
25}
26
Ben Murdochbb1529c2013-08-08 10:24:53 +010027DOMStorageSession::DOMStorageSession(DOMStorageContextImpl* context,
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010028 const std::string& persistent_namespace_id)
29 : context_(context),
30 namespace_id_(context->AllocateSessionId()),
31 persistent_namespace_id_(persistent_namespace_id),
32 should_persist_(false) {
33 context->task_runner()->PostTask(
34 FROM_HERE,
Ben Murdochbb1529c2013-08-08 10:24:53 +010035 base::Bind(&DOMStorageContextImpl::CreateSessionNamespace,
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010036 context_, namespace_id_, persistent_namespace_id_));
37}
38
Ben Murdochbb1529c2013-08-08 10:24:53 +010039void DOMStorageSession::SetShouldPersist(bool should_persist) {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010040 should_persist_ = should_persist;
41}
42
Ben Murdochbb1529c2013-08-08 10:24:53 +010043bool DOMStorageSession::should_persist() const {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010044 return should_persist_;
45}
46
Ben Murdochbb1529c2013-08-08 10:24:53 +010047bool DOMStorageSession::IsFromContext(DOMStorageContextImpl* context) {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010048 return context_.get() == context;
49}
50
Ben Murdochbb1529c2013-08-08 10:24:53 +010051DOMStorageSession* DOMStorageSession::Clone() {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010052 return CloneFrom(context_.get(), namespace_id_);
53}
54
55// static
Ben Murdochbb1529c2013-08-08 10:24:53 +010056DOMStorageSession* DOMStorageSession::CloneFrom(DOMStorageContextImpl* context,
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010057 int64 namepace_id_to_clone) {
58 int64 clone_id = context->AllocateSessionId();
59 std::string persistent_clone_id = context->AllocatePersistentSessionId();
60 context->task_runner()->PostTask(
61 FROM_HERE,
Ben Murdochbb1529c2013-08-08 10:24:53 +010062 base::Bind(&DOMStorageContextImpl::CloneSessionNamespace,
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010063 context, namepace_id_to_clone, clone_id, persistent_clone_id));
Ben Murdochbb1529c2013-08-08 10:24:53 +010064 return new DOMStorageSession(context, clone_id, persistent_clone_id);
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010065}
66
Ben Murdochbb1529c2013-08-08 10:24:53 +010067DOMStorageSession::DOMStorageSession(DOMStorageContextImpl* context,
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010068 int64 namespace_id,
69 const std::string& persistent_namespace_id)
70 : context_(context),
71 namespace_id_(namespace_id),
72 persistent_namespace_id_(persistent_namespace_id),
73 should_persist_(false) {
74 // This ctor is intended for use by the Clone() method.
75}
76
Ben Murdochbb1529c2013-08-08 10:24:53 +010077DOMStorageSession::~DOMStorageSession() {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010078 context_->task_runner()->PostTask(
79 FROM_HERE,
Ben Murdochbb1529c2013-08-08 10:24:53 +010080 base::Bind(&DOMStorageContextImpl::DeleteSessionNamespace,
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010081 context_, namespace_id_, should_persist_));
82}
83
Ben Murdochbb1529c2013-08-08 10:24:53 +010084} // namespace content