Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / content / browser / dom_storage / dom_storage_context_impl.cc
blobf790ecd8001a750bec29f8337cd165f311fd8745
1 // Copyright (c) 2012 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_impl.h"
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/session_storage_namespace_impl.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "webkit/browser/dom_storage/dom_storage_area.h"
14 #include "webkit/browser/dom_storage/dom_storage_context.h"
15 #include "webkit/browser/dom_storage/dom_storage_task_runner.h"
17 using dom_storage::DomStorageArea;
18 using dom_storage::DomStorageContext;
19 using dom_storage::DomStorageTaskRunner;
20 using dom_storage::DomStorageWorkerPoolTaskRunner;
22 namespace content {
23 namespace {
25 const char kLocalStorageDirectory[] = "Local Storage";
26 const char kSessionStorageDirectory[] = "Session Storage";
28 void InvokeLocalStorageUsageCallbackHelper(
29 const DOMStorageContext::GetLocalStorageUsageCallback& callback,
30 const std::vector<dom_storage::LocalStorageUsageInfo>* infos) {
31 callback.Run(*infos);
34 void GetLocalStorageUsageHelper(
35 base::MessageLoopProxy* reply_loop,
36 DomStorageContext* context,
37 const DOMStorageContext::GetLocalStorageUsageCallback& callback) {
38 std::vector<dom_storage::LocalStorageUsageInfo>* infos =
39 new std::vector<dom_storage::LocalStorageUsageInfo>;
40 context->GetLocalStorageUsage(infos, true);
41 reply_loop->PostTask(
42 FROM_HERE,
43 base::Bind(&InvokeLocalStorageUsageCallbackHelper,
44 callback, base::Owned(infos)));
47 void InvokeSessionStorageUsageCallbackHelper(
48 const DOMStorageContext::GetSessionStorageUsageCallback& callback,
49 const std::vector<dom_storage::SessionStorageUsageInfo>* infos) {
50 callback.Run(*infos);
53 void GetSessionStorageUsageHelper(
54 base::MessageLoopProxy* reply_loop,
55 DomStorageContext* context,
56 const DOMStorageContext::GetSessionStorageUsageCallback& callback) {
57 std::vector<dom_storage::SessionStorageUsageInfo>* infos =
58 new std::vector<dom_storage::SessionStorageUsageInfo>;
59 context->GetSessionStorageUsage(infos);
60 reply_loop->PostTask(
61 FROM_HERE,
62 base::Bind(&InvokeSessionStorageUsageCallbackHelper,
63 callback, base::Owned(infos)));
66 } // namespace
68 DOMStorageContextImpl::DOMStorageContextImpl(
69 const base::FilePath& data_path,
70 quota::SpecialStoragePolicy* special_storage_policy) {
71 base::SequencedWorkerPool* worker_pool = BrowserThread::GetBlockingPool();
72 context_ = new dom_storage::DomStorageContext(
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 DOMStorageContextImpl::~DOMStorageContextImpl() {
89 void DOMStorageContextImpl::GetLocalStorageUsage(
90 const GetLocalStorageUsageCallback& callback) {
91 DCHECK(context_.get());
92 context_->task_runner()
93 ->PostShutdownBlockingTask(FROM_HERE,
94 DomStorageTaskRunner::PRIMARY_SEQUENCE,
95 base::Bind(&GetLocalStorageUsageHelper,
96 base::MessageLoopProxy::current(),
97 context_,
98 callback));
101 void DOMStorageContextImpl::GetSessionStorageUsage(
102 const GetSessionStorageUsageCallback& callback) {
103 DCHECK(context_.get());
104 context_->task_runner()
105 ->PostShutdownBlockingTask(FROM_HERE,
106 DomStorageTaskRunner::PRIMARY_SEQUENCE,
107 base::Bind(&GetSessionStorageUsageHelper,
108 base::MessageLoopProxy::current(),
109 context_,
110 callback));
113 void DOMStorageContextImpl::DeleteLocalStorage(const GURL& origin) {
114 DCHECK(context_.get());
115 context_->task_runner()->PostShutdownBlockingTask(
116 FROM_HERE,
117 DomStorageTaskRunner::PRIMARY_SEQUENCE,
118 base::Bind(&DomStorageContext::DeleteLocalStorage, context_, origin));
121 void DOMStorageContextImpl::DeleteSessionStorage(
122 const dom_storage::SessionStorageUsageInfo& usage_info) {
123 DCHECK(context_.get());
124 context_->task_runner()->PostShutdownBlockingTask(
125 FROM_HERE,
126 DomStorageTaskRunner::PRIMARY_SEQUENCE,
127 base::Bind(
128 &DomStorageContext::DeleteSessionStorage, context_, usage_info));
131 void DOMStorageContextImpl::SetSaveSessionStorageOnDisk() {
132 DCHECK(context_.get());
133 context_->SetSaveSessionStorageOnDisk();
136 scoped_refptr<SessionStorageNamespace>
137 DOMStorageContextImpl::RecreateSessionStorage(
138 const std::string& persistent_id) {
139 return scoped_refptr<SessionStorageNamespace>(
140 new SessionStorageNamespaceImpl(this, persistent_id));
143 void DOMStorageContextImpl::StartScavengingUnusedSessionStorage() {
144 DCHECK(context_.get());
145 context_->task_runner()->PostShutdownBlockingTask(
146 FROM_HERE,
147 DomStorageTaskRunner::PRIMARY_SEQUENCE,
148 base::Bind(&DomStorageContext::StartScavengingUnusedSessionStorage,
149 context_));
152 void DOMStorageContextImpl::PurgeMemory() {
153 DCHECK(context_.get());
154 context_->task_runner()->PostShutdownBlockingTask(
155 FROM_HERE,
156 DomStorageTaskRunner::PRIMARY_SEQUENCE,
157 base::Bind(&DomStorageContext::PurgeMemory, context_));
160 void DOMStorageContextImpl::SetForceKeepSessionState() {
161 DCHECK(context_.get());
162 context_->task_runner()->PostShutdownBlockingTask(
163 FROM_HERE,
164 DomStorageTaskRunner::PRIMARY_SEQUENCE,
165 base::Bind(&DomStorageContext::SetForceKeepSessionState, context_));
168 void DOMStorageContextImpl::Shutdown() {
169 DCHECK(context_.get());
170 context_->task_runner()->PostShutdownBlockingTask(
171 FROM_HERE,
172 DomStorageTaskRunner::PRIMARY_SEQUENCE,
173 base::Bind(&DomStorageContext::Shutdown, context_));
176 } // namespace content