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"
8 #include "base/bind_helpers.h"
9 #include "base/files/file_path.h"
10 #include "base/message_loop/message_loop_proxy.h"
11 #include "content/browser/dom_storage/dom_storage_area.h"
12 #include "content/browser/dom_storage/dom_storage_context_impl.h"
13 #include "content/browser/dom_storage/dom_storage_task_runner.h"
14 #include "content/browser/dom_storage/session_storage_namespace_impl.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/local_storage_usage_info.h"
17 #include "content/public/browser/session_storage_usage_info.h"
22 const char kLocalStorageDirectory
[] = "Local Storage";
23 const char kSessionStorageDirectory
[] = "Session Storage";
25 void InvokeLocalStorageUsageCallbackHelper(
26 const DOMStorageContext::GetLocalStorageUsageCallback
& callback
,
27 const std::vector
<LocalStorageUsageInfo
>* infos
) {
31 void GetLocalStorageUsageHelper(
32 base::MessageLoopProxy
* reply_loop
,
33 DOMStorageContextImpl
* context
,
34 const DOMStorageContext::GetLocalStorageUsageCallback
& callback
) {
35 std::vector
<LocalStorageUsageInfo
>* infos
=
36 new std::vector
<LocalStorageUsageInfo
>;
37 context
->GetLocalStorageUsage(infos
, true);
40 base::Bind(&InvokeLocalStorageUsageCallbackHelper
,
41 callback
, base::Owned(infos
)));
44 void InvokeSessionStorageUsageCallbackHelper(
45 const DOMStorageContext::GetSessionStorageUsageCallback
& callback
,
46 const std::vector
<SessionStorageUsageInfo
>* infos
) {
50 void GetSessionStorageUsageHelper(
51 base::MessageLoopProxy
* reply_loop
,
52 DOMStorageContextImpl
* context
,
53 const DOMStorageContext::GetSessionStorageUsageCallback
& callback
) {
54 std::vector
<SessionStorageUsageInfo
>* infos
=
55 new std::vector
<SessionStorageUsageInfo
>;
56 context
->GetSessionStorageUsage(infos
);
59 base::Bind(&InvokeSessionStorageUsageCallbackHelper
,
60 callback
, base::Owned(infos
)));
65 DOMStorageContextWrapper::DOMStorageContextWrapper(
66 const base::FilePath
& data_path
,
67 quota::SpecialStoragePolicy
* special_storage_policy
) {
68 base::SequencedWorkerPool
* worker_pool
= BrowserThread::GetBlockingPool();
69 context_
= new DOMStorageContextImpl(
70 data_path
.empty() ? data_path
71 : data_path
.AppendASCII(kLocalStorageDirectory
),
72 data_path
.empty() ? data_path
73 : data_path
.AppendASCII(kSessionStorageDirectory
),
74 special_storage_policy
,
75 new DOMStorageWorkerPoolTaskRunner(
77 worker_pool
->GetNamedSequenceToken("dom_storage_primary"),
78 worker_pool
->GetNamedSequenceToken("dom_storage_commit"),
79 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
)
83 DOMStorageContextWrapper::~DOMStorageContextWrapper() {
86 void DOMStorageContextWrapper::GetLocalStorageUsage(
87 const GetLocalStorageUsageCallback
& callback
) {
88 DCHECK(context_
.get());
89 context_
->task_runner()
90 ->PostShutdownBlockingTask(FROM_HERE
,
91 DOMStorageTaskRunner::PRIMARY_SEQUENCE
,
92 base::Bind(&GetLocalStorageUsageHelper
,
93 base::MessageLoopProxy::current(),
98 void DOMStorageContextWrapper::GetSessionStorageUsage(
99 const GetSessionStorageUsageCallback
& callback
) {
100 DCHECK(context_
.get());
101 context_
->task_runner()
102 ->PostShutdownBlockingTask(FROM_HERE
,
103 DOMStorageTaskRunner::PRIMARY_SEQUENCE
,
104 base::Bind(&GetSessionStorageUsageHelper
,
105 base::MessageLoopProxy::current(),
110 void DOMStorageContextWrapper::DeleteLocalStorage(const GURL
& origin
) {
111 DCHECK(context_
.get());
112 context_
->task_runner()->PostShutdownBlockingTask(
114 DOMStorageTaskRunner::PRIMARY_SEQUENCE
,
115 base::Bind(&DOMStorageContextImpl::DeleteLocalStorage
, context_
, origin
));
118 void DOMStorageContextWrapper::DeleteSessionStorage(
119 const SessionStorageUsageInfo
& usage_info
) {
120 DCHECK(context_
.get());
121 context_
->task_runner()->PostShutdownBlockingTask(
123 DOMStorageTaskRunner::PRIMARY_SEQUENCE
,
124 base::Bind(&DOMStorageContextImpl::DeleteSessionStorage
,
125 context_
, usage_info
));
128 void DOMStorageContextWrapper::SetSaveSessionStorageOnDisk() {
129 DCHECK(context_
.get());
130 context_
->SetSaveSessionStorageOnDisk();
133 scoped_refptr
<SessionStorageNamespace
>
134 DOMStorageContextWrapper::RecreateSessionStorage(
135 const std::string
& persistent_id
) {
136 return scoped_refptr
<SessionStorageNamespace
>(
137 new SessionStorageNamespaceImpl(this, persistent_id
));
140 void DOMStorageContextWrapper::StartScavengingUnusedSessionStorage() {
141 DCHECK(context_
.get());
142 context_
->task_runner()->PostShutdownBlockingTask(
144 DOMStorageTaskRunner::PRIMARY_SEQUENCE
,
145 base::Bind(&DOMStorageContextImpl::StartScavengingUnusedSessionStorage
,
149 void DOMStorageContextWrapper::SetForceKeepSessionState() {
150 DCHECK(context_
.get());
151 context_
->task_runner()->PostShutdownBlockingTask(
153 DOMStorageTaskRunner::PRIMARY_SEQUENCE
,
154 base::Bind(&DOMStorageContextImpl::SetForceKeepSessionState
, context_
));
157 void DOMStorageContextWrapper::Shutdown() {
158 DCHECK(context_
.get());
159 context_
->task_runner()->PostShutdownBlockingTask(
161 DOMStorageTaskRunner::PRIMARY_SEQUENCE
,
162 base::Bind(&DOMStorageContextImpl::Shutdown
, context_
));
165 } // namespace content