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_context_wrapper.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/files/file_path.h"
13 #include "base/location.h"
14 #include "base/single_thread_task_runner.h"
15 #include "base/thread_task_runner_handle.h"
16 #include "content/browser/dom_storage/dom_storage_area.h"
17 #include "content/browser/dom_storage/dom_storage_context_impl.h"
18 #include "content/browser/dom_storage/dom_storage_task_runner.h"
19 #include "content/browser/dom_storage/session_storage_namespace_impl.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "content/public/browser/local_storage_usage_info.h"
22 #include "content/public/browser/session_storage_usage_info.h"
27 const char kLocalStorageDirectory
[] = "Local Storage";
28 const char kSessionStorageDirectory
[] = "Session Storage";
30 void InvokeLocalStorageUsageCallbackHelper(
31 const DOMStorageContext::GetLocalStorageUsageCallback
& callback
,
32 const std::vector
<LocalStorageUsageInfo
>* infos
) {
36 void GetLocalStorageUsageHelper(
37 base::SingleThreadTaskRunner
* reply_task_runner
,
38 DOMStorageContextImpl
* context
,
39 const DOMStorageContext::GetLocalStorageUsageCallback
& callback
) {
40 std::vector
<LocalStorageUsageInfo
>* infos
=
41 new std::vector
<LocalStorageUsageInfo
>;
42 context
->GetLocalStorageUsage(infos
, true);
43 reply_task_runner
->PostTask(
44 FROM_HERE
, base::Bind(&InvokeLocalStorageUsageCallbackHelper
, callback
,
48 void InvokeSessionStorageUsageCallbackHelper(
49 const DOMStorageContext::GetSessionStorageUsageCallback
& callback
,
50 const std::vector
<SessionStorageUsageInfo
>* infos
) {
54 void GetSessionStorageUsageHelper(
55 base::SingleThreadTaskRunner
* reply_task_runner
,
56 DOMStorageContextImpl
* context
,
57 const DOMStorageContext::GetSessionStorageUsageCallback
& callback
) {
58 std::vector
<SessionStorageUsageInfo
>* infos
=
59 new std::vector
<SessionStorageUsageInfo
>;
60 context
->GetSessionStorageUsage(infos
);
61 reply_task_runner
->PostTask(
62 FROM_HERE
, base::Bind(&InvokeSessionStorageUsageCallbackHelper
, callback
,
68 DOMStorageContextWrapper::DOMStorageContextWrapper(
69 const base::FilePath
& data_path
,
70 storage::SpecialStoragePolicy
* special_storage_policy
) {
71 base::SequencedWorkerPool
* worker_pool
= BrowserThread::GetBlockingPool();
72 context_
= new DOMStorageContextImpl(
73 data_path
.empty() ? data_path
74 : data_path
.AppendASCII(kLocalStorageDirectory
),
75 data_path
.empty() ? data_path
76 : data_path
.AppendASCII(kSessionStorageDirectory
),
77 special_storage_policy
,
78 new DOMStorageWorkerPoolTaskRunner(
80 worker_pool
->GetNamedSequenceToken("dom_storage_primary"),
81 worker_pool
->GetNamedSequenceToken("dom_storage_commit"),
82 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
)
86 DOMStorageContextWrapper::~DOMStorageContextWrapper() {
89 void DOMStorageContextWrapper::GetLocalStorageUsage(
90 const GetLocalStorageUsageCallback
& callback
) {
91 DCHECK(context_
.get());
92 context_
->task_runner()->PostShutdownBlockingTask(
93 FROM_HERE
, DOMStorageTaskRunner::PRIMARY_SEQUENCE
,
94 base::Bind(&GetLocalStorageUsageHelper
,
95 base::ThreadTaskRunnerHandle::Get(), context_
, callback
));
98 void DOMStorageContextWrapper::GetSessionStorageUsage(
99 const GetSessionStorageUsageCallback
& callback
) {
100 DCHECK(context_
.get());
101 context_
->task_runner()->PostShutdownBlockingTask(
102 FROM_HERE
, DOMStorageTaskRunner::PRIMARY_SEQUENCE
,
103 base::Bind(&GetSessionStorageUsageHelper
,
104 base::ThreadTaskRunnerHandle::Get(), context_
, callback
));
107 void DOMStorageContextWrapper::DeleteLocalStorage(const GURL
& origin
) {
108 DCHECK(context_
.get());
109 context_
->task_runner()->PostShutdownBlockingTask(
111 DOMStorageTaskRunner::PRIMARY_SEQUENCE
,
112 base::Bind(&DOMStorageContextImpl::DeleteLocalStorage
, context_
, origin
));
115 void DOMStorageContextWrapper::DeleteSessionStorage(
116 const SessionStorageUsageInfo
& usage_info
) {
117 DCHECK(context_
.get());
118 context_
->task_runner()->PostShutdownBlockingTask(
120 DOMStorageTaskRunner::PRIMARY_SEQUENCE
,
121 base::Bind(&DOMStorageContextImpl::DeleteSessionStorage
,
122 context_
, usage_info
));
125 void DOMStorageContextWrapper::SetSaveSessionStorageOnDisk() {
126 DCHECK(context_
.get());
127 context_
->SetSaveSessionStorageOnDisk();
130 scoped_refptr
<SessionStorageNamespace
>
131 DOMStorageContextWrapper::RecreateSessionStorage(
132 const std::string
& persistent_id
) {
133 return scoped_refptr
<SessionStorageNamespace
>(
134 new SessionStorageNamespaceImpl(this, persistent_id
));
137 void DOMStorageContextWrapper::StartScavengingUnusedSessionStorage() {
138 DCHECK(context_
.get());
139 context_
->task_runner()->PostShutdownBlockingTask(
141 DOMStorageTaskRunner::PRIMARY_SEQUENCE
,
142 base::Bind(&DOMStorageContextImpl::StartScavengingUnusedSessionStorage
,
146 void DOMStorageContextWrapper::SetForceKeepSessionState() {
147 DCHECK(context_
.get());
148 context_
->task_runner()->PostShutdownBlockingTask(
150 DOMStorageTaskRunner::PRIMARY_SEQUENCE
,
151 base::Bind(&DOMStorageContextImpl::SetForceKeepSessionState
, context_
));
154 void DOMStorageContextWrapper::Shutdown() {
155 DCHECK(context_
.get());
156 context_
->task_runner()->PostShutdownBlockingTask(
158 DOMStorageTaskRunner::PRIMARY_SEQUENCE
,
159 base::Bind(&DOMStorageContextImpl::Shutdown
, context_
));
162 void DOMStorageContextWrapper::Flush() {
163 DCHECK(context_
.get());
164 context_
->task_runner()->PostShutdownBlockingTask(
165 FROM_HERE
, DOMStorageTaskRunner::PRIMARY_SEQUENCE
,
166 base::Bind(&DOMStorageContextImpl::Flush
, context_
));
169 } // namespace content