Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / content / browser / dom_storage / dom_storage_context_wrapper.cc
blobdb486f35e36a6ea9a581bf58c14c8816da0972c2
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"
7 #include <string>
8 #include <vector>
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"
24 namespace content {
25 namespace {
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) {
33 callback.Run(*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,
45 base::Owned(infos)));
48 void InvokeSessionStorageUsageCallbackHelper(
49 const DOMStorageContext::GetSessionStorageUsageCallback& callback,
50 const std::vector<SessionStorageUsageInfo>* infos) {
51 callback.Run(*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,
63 base::Owned(infos)));
66 } // namespace
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(
79 worker_pool,
80 worker_pool->GetNamedSequenceToken("dom_storage_primary"),
81 worker_pool->GetNamedSequenceToken("dom_storage_commit"),
82 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)
83 .get()));
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(
110 FROM_HERE,
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(
119 FROM_HERE,
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(
140 FROM_HERE,
141 DOMStorageTaskRunner::PRIMARY_SEQUENCE,
142 base::Bind(&DOMStorageContextImpl::StartScavengingUnusedSessionStorage,
143 context_));
146 void DOMStorageContextWrapper::SetForceKeepSessionState() {
147 DCHECK(context_.get());
148 context_->task_runner()->PostShutdownBlockingTask(
149 FROM_HERE,
150 DOMStorageTaskRunner::PRIMARY_SEQUENCE,
151 base::Bind(&DOMStorageContextImpl::SetForceKeepSessionState, context_));
154 void DOMStorageContextWrapper::Shutdown() {
155 DCHECK(context_.get());
156 context_->task_runner()->PostShutdownBlockingTask(
157 FROM_HERE,
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