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.
4
5 #include "content/browser/dom_storage/dom_storage_context_wrapper.h"
6
7 #include "base/bind.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"
18
19 namespace content {
20 namespace {
21
22 const char kLocalStorageDirectory[] = "Local Storage";
23 const char kSessionStorageDirectory[] = "Session Storage";
24
InvokeLocalStorageUsageCallbackHelper(const DOMStorageContext::GetLocalStorageUsageCallback & callback,const std::vector<LocalStorageUsageInfo> * infos)25 void InvokeLocalStorageUsageCallbackHelper(
26 const DOMStorageContext::GetLocalStorageUsageCallback& callback,
27 const std::vector<LocalStorageUsageInfo>* infos) {
28 callback.Run(*infos);
29 }
30
GetLocalStorageUsageHelper(base::MessageLoopProxy * reply_loop,DOMStorageContextImpl * context,const DOMStorageContext::GetLocalStorageUsageCallback & callback)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);
38 reply_loop->PostTask(
39 FROM_HERE,
40 base::Bind(&InvokeLocalStorageUsageCallbackHelper,
41 callback, base::Owned(infos)));
42 }
43
InvokeSessionStorageUsageCallbackHelper(const DOMStorageContext::GetSessionStorageUsageCallback & callback,const std::vector<SessionStorageUsageInfo> * infos)44 void InvokeSessionStorageUsageCallbackHelper(
45 const DOMStorageContext::GetSessionStorageUsageCallback& callback,
46 const std::vector<SessionStorageUsageInfo>* infos) {
47 callback.Run(*infos);
48 }
49
GetSessionStorageUsageHelper(base::MessageLoopProxy * reply_loop,DOMStorageContextImpl * context,const DOMStorageContext::GetSessionStorageUsageCallback & callback)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);
57 reply_loop->PostTask(
58 FROM_HERE,
59 base::Bind(&InvokeSessionStorageUsageCallbackHelper,
60 callback, base::Owned(infos)));
61 }
62
63 } // namespace
64
DOMStorageContextWrapper(const base::FilePath & data_path,storage::SpecialStoragePolicy * special_storage_policy)65 DOMStorageContextWrapper::DOMStorageContextWrapper(
66 const base::FilePath& data_path,
67 storage::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(
76 worker_pool,
77 worker_pool->GetNamedSequenceToken("dom_storage_primary"),
78 worker_pool->GetNamedSequenceToken("dom_storage_commit"),
79 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)
80 .get()));
81 }
82
~DOMStorageContextWrapper()83 DOMStorageContextWrapper::~DOMStorageContextWrapper() {
84 }
85
GetLocalStorageUsage(const GetLocalStorageUsageCallback & callback)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(),
94 context_,
95 callback));
96 }
97
GetSessionStorageUsage(const GetSessionStorageUsageCallback & callback)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(),
106 context_,
107 callback));
108 }
109
DeleteLocalStorage(const GURL & origin)110 void DOMStorageContextWrapper::DeleteLocalStorage(const GURL& origin) {
111 DCHECK(context_.get());
112 context_->task_runner()->PostShutdownBlockingTask(
113 FROM_HERE,
114 DOMStorageTaskRunner::PRIMARY_SEQUENCE,
115 base::Bind(&DOMStorageContextImpl::DeleteLocalStorage, context_, origin));
116 }
117
DeleteSessionStorage(const SessionStorageUsageInfo & usage_info)118 void DOMStorageContextWrapper::DeleteSessionStorage(
119 const SessionStorageUsageInfo& usage_info) {
120 DCHECK(context_.get());
121 context_->task_runner()->PostShutdownBlockingTask(
122 FROM_HERE,
123 DOMStorageTaskRunner::PRIMARY_SEQUENCE,
124 base::Bind(&DOMStorageContextImpl::DeleteSessionStorage,
125 context_, usage_info));
126 }
127
SetSaveSessionStorageOnDisk()128 void DOMStorageContextWrapper::SetSaveSessionStorageOnDisk() {
129 DCHECK(context_.get());
130 context_->SetSaveSessionStorageOnDisk();
131 }
132
133 scoped_refptr<SessionStorageNamespace>
RecreateSessionStorage(const std::string & persistent_id)134 DOMStorageContextWrapper::RecreateSessionStorage(
135 const std::string& persistent_id) {
136 return scoped_refptr<SessionStorageNamespace>(
137 new SessionStorageNamespaceImpl(this, persistent_id));
138 }
139
StartScavengingUnusedSessionStorage()140 void DOMStorageContextWrapper::StartScavengingUnusedSessionStorage() {
141 DCHECK(context_.get());
142 context_->task_runner()->PostShutdownBlockingTask(
143 FROM_HERE,
144 DOMStorageTaskRunner::PRIMARY_SEQUENCE,
145 base::Bind(&DOMStorageContextImpl::StartScavengingUnusedSessionStorage,
146 context_));
147 }
148
SetForceKeepSessionState()149 void DOMStorageContextWrapper::SetForceKeepSessionState() {
150 DCHECK(context_.get());
151 context_->task_runner()->PostShutdownBlockingTask(
152 FROM_HERE,
153 DOMStorageTaskRunner::PRIMARY_SEQUENCE,
154 base::Bind(&DOMStorageContextImpl::SetForceKeepSessionState, context_));
155 }
156
Shutdown()157 void DOMStorageContextWrapper::Shutdown() {
158 DCHECK(context_.get());
159 context_->task_runner()->PostShutdownBlockingTask(
160 FROM_HERE,
161 DOMStorageTaskRunner::PRIMARY_SEQUENCE,
162 base::Bind(&DOMStorageContextImpl::Shutdown, context_));
163 }
164
165 } // namespace content
166