1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/dom_storage/dom_storage_session.h"
8 #include "base/bind_helpers.h"
9 #include "base/logging.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "base/tracked_objects.h"
13 #include "content/browser/dom_storage/dom_storage_context_impl.h"
14 #include "content/browser/dom_storage/dom_storage_task_runner.h"
20 void PostMergeTaskResult(
21 const SessionStorageNamespace::MergeResultCallback
& callback
,
22 SessionStorageNamespace::MergeResult result
) {
26 void RunMergeTaskAndPostResult(
27 const base::Callback
<SessionStorageNamespace::MergeResult(void)>& task
,
28 scoped_refptr
<base::SingleThreadTaskRunner
> result_loop
,
29 const SessionStorageNamespace::MergeResultCallback
& callback
) {
30 SessionStorageNamespace::MergeResult result
= task
.Run();
31 result_loop
->PostTask(
32 FROM_HERE
, base::Bind(&PostMergeTaskResult
, callback
, result
));
37 DOMStorageSession::DOMStorageSession(DOMStorageContextImpl
* context
)
39 namespace_id_(context
->AllocateSessionId()),
40 persistent_namespace_id_(context
->AllocatePersistentSessionId()),
41 should_persist_(false) {
42 context
->task_runner()->PostTask(
44 base::Bind(&DOMStorageContextImpl::CreateSessionNamespace
,
45 context_
, namespace_id_
, persistent_namespace_id_
));
48 DOMStorageSession::DOMStorageSession(DOMStorageContextImpl
* context
,
49 const std::string
& persistent_namespace_id
)
51 namespace_id_(context
->AllocateSessionId()),
52 persistent_namespace_id_(persistent_namespace_id
),
53 should_persist_(false) {
54 context
->task_runner()->PostTask(
56 base::Bind(&DOMStorageContextImpl::CreateSessionNamespace
,
57 context_
, namespace_id_
, persistent_namespace_id_
));
60 DOMStorageSession::DOMStorageSession(
61 DOMStorageSession
* master_dom_storage_session
)
62 : context_(master_dom_storage_session
->context_
),
63 namespace_id_(master_dom_storage_session
->context_
->AllocateSessionId()),
64 persistent_namespace_id_(
65 master_dom_storage_session
->persistent_namespace_id()),
66 should_persist_(false) {
67 context_
->task_runner()->PostTask(
69 base::Bind(&DOMStorageContextImpl::CreateAliasSessionNamespace
,
71 master_dom_storage_session
->namespace_id(),
73 persistent_namespace_id_
));
76 void DOMStorageSession::SetShouldPersist(bool should_persist
) {
77 should_persist_
= should_persist
;
80 bool DOMStorageSession::should_persist() const {
81 return should_persist_
;
84 bool DOMStorageSession::IsFromContext(DOMStorageContextImpl
* context
) {
85 return context_
.get() == context
;
88 DOMStorageSession
* DOMStorageSession::Clone() {
89 return CloneFrom(context_
.get(), namespace_id_
);
93 DOMStorageSession
* DOMStorageSession::CloneFrom(DOMStorageContextImpl
* context
,
94 int64 namepace_id_to_clone
) {
95 int64 clone_id
= context
->AllocateSessionId();
96 std::string persistent_clone_id
= context
->AllocatePersistentSessionId();
97 context
->task_runner()->PostTask(
99 base::Bind(&DOMStorageContextImpl::CloneSessionNamespace
,
100 context
, namepace_id_to_clone
, clone_id
, persistent_clone_id
));
101 return new DOMStorageSession(context
, clone_id
, persistent_clone_id
);
104 DOMStorageSession::DOMStorageSession(DOMStorageContextImpl
* context
,
106 const std::string
& persistent_namespace_id
)
108 namespace_id_(namespace_id
),
109 persistent_namespace_id_(persistent_namespace_id
),
110 should_persist_(false) {
111 // This ctor is intended for use by the Clone() method.
114 DOMStorageSession::~DOMStorageSession() {
115 context_
->task_runner()->PostTask(
117 base::Bind(&DOMStorageContextImpl::DeleteSessionNamespace
,
118 context_
, namespace_id_
, should_persist_
));
121 void DOMStorageSession::AddTransactionLogProcessId(int process_id
) {
122 context_
->task_runner()->PostTask(
124 base::Bind(&DOMStorageContextImpl::AddTransactionLogProcessId
,
125 context_
, namespace_id_
, process_id
));
128 void DOMStorageSession::RemoveTransactionLogProcessId(int process_id
) {
129 context_
->task_runner()->PostTask(
131 base::Bind(&DOMStorageContextImpl::RemoveTransactionLogProcessId
,
132 context_
, namespace_id_
, process_id
));
135 void DOMStorageSession::Merge(
138 DOMStorageSession
* other
,
139 const SessionStorageNamespace::MergeResultCallback
& callback
) {
140 scoped_refptr
<base::SingleThreadTaskRunner
> current_loop(
141 base::ThreadTaskRunnerHandle::Get());
142 SessionStorageNamespace::MergeResultCallback cb
=
143 base::Bind(&DOMStorageSession::ProcessMergeResult
,
147 other
->persistent_namespace_id());
148 context_
->task_runner()->PostTask(
150 base::Bind(&RunMergeTaskAndPostResult
,
151 base::Bind(&DOMStorageContextImpl::MergeSessionStorage
,
152 context_
, namespace_id_
, actually_merge
, process_id
,
153 other
->namespace_id_
),
158 void DOMStorageSession::ProcessMergeResult(
160 const SessionStorageNamespace::MergeResultCallback
& callback
,
161 const std::string
& new_persistent_namespace_id
,
162 SessionStorageNamespace::MergeResult result
) {
163 if (actually_merge
&&
164 (result
== SessionStorageNamespace::MERGE_RESULT_MERGEABLE
||
165 result
== SessionStorageNamespace::MERGE_RESULT_NO_TRANSACTIONS
)) {
166 persistent_namespace_id_
= new_persistent_namespace_id
;
168 callback
.Run(result
);
171 } // namespace content