OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "content/browser/dom_storage/dom_storage_context_impl_new.h" | |
6 | |
7 #ifdef ENABLE_NEW_DOM_STORAGE_BACKEND | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/bind_helpers.h" | |
11 #include "base/message_loop_proxy.h" | |
12 #include "content/public/browser/browser_thread.h" | |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | |
14 #include "webkit/database/database_util.h" | |
15 #include "webkit/dom_storage/dom_storage_area.h" | |
16 #include "webkit/dom_storage/dom_storage_context.h" | |
17 #include "webkit/dom_storage/dom_storage_task_runner.h" | |
18 #include "webkit/glue/webkit_glue.h" | |
19 | |
20 using content::BrowserThread; | |
21 using content::DOMStorageContext; | |
22 using dom_storage::DomStorageArea; | |
23 using dom_storage::DomStorageContext; | |
24 using dom_storage::DomStorageTaskRunner; | |
25 using dom_storage::DomStorageWorkerPoolTaskRunner; | |
26 using webkit_database::DatabaseUtil; | |
27 | |
28 namespace { | |
29 | |
30 const char kLocalStorageDirectory[] = "Local Storage"; | |
31 | |
32 // TODO(michaeln): Fix the content layer api, FilePaths and | |
33 // string16 origin_ids are just wrong. Then get rid of | |
34 // this conversion non-sense. Most of the includes are just | |
35 // to support that non-sense. | |
36 | |
37 GURL OriginIdToGURL(const string16& origin_id) { | |
38 return DatabaseUtil::GetOriginFromIdentifier(origin_id); | |
39 } | |
40 | |
41 FilePath OriginToFullFilePath(const FilePath& directory, | |
42 const GURL& origin) { | |
43 return directory.Append(DomStorageArea::DatabaseFileNameFromOrigin(origin)); | |
44 } | |
45 | |
46 GURL FilePathToOrigin(const FilePath& path) { | |
47 DCHECK(path.MatchesExtension(DomStorageArea::kDatabaseFileExtension)); | |
48 return DomStorageArea::OriginFromDatabaseFileName(path); | |
49 } | |
50 | |
51 void InvokeAllStorageFilesCallbackHelper( | |
52 const DOMStorageContext::GetAllStorageFilesCallback& callback, | |
53 const std::vector<FilePath>& file_paths) { | |
54 callback.Run(file_paths); | |
55 } | |
56 | |
57 void GetAllStorageFilesHelper( | |
58 base::MessageLoopProxy* reply_loop, | |
59 DomStorageContext* context, | |
60 const DOMStorageContext::GetAllStorageFilesCallback& callback) { | |
61 std::vector<DomStorageContext::UsageInfo> infos; | |
62 // TODO(michaeln): Actually include the file info too when the | |
63 // content layer api is fixed. | |
64 const bool kDontIncludeFileInfo = false; | |
65 context->GetUsageInfo(&infos, kDontIncludeFileInfo); | |
66 | |
67 std::vector<FilePath> paths; | |
68 for (size_t i = 0; i < infos.size(); ++i) { | |
69 paths.push_back( | |
70 OriginToFullFilePath(context->directory(), infos[i].origin)); | |
71 } | |
72 | |
73 reply_loop->PostTask( | |
74 FROM_HERE, | |
75 base::Bind(&InvokeAllStorageFilesCallbackHelper, | |
76 callback, paths)); | |
77 } | |
78 | |
79 } // namespace | |
80 | |
81 DOMStorageContextImpl::DOMStorageContextImpl( | |
82 const FilePath& data_path, | |
83 quota::SpecialStoragePolicy* special_storage_policy) { | |
84 base::SequencedWorkerPool* worker_pool = BrowserThread::GetBlockingPool(); | |
85 context_ = new dom_storage::DomStorageContext( | |
86 data_path.empty() ? | |
87 data_path : data_path.AppendASCII(kLocalStorageDirectory), | |
88 special_storage_policy, | |
89 new DomStorageWorkerPoolTaskRunner( | |
90 worker_pool, | |
91 worker_pool->GetNamedSequenceToken("dom_storage_primary"), | |
92 worker_pool->GetNamedSequenceToken("dom_storage_commit"), | |
93 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))); | |
94 } | |
95 | |
96 DOMStorageContextImpl::~DOMStorageContextImpl() { | |
97 } | |
98 | |
99 void DOMStorageContextImpl::GetAllStorageFiles( | |
100 const GetAllStorageFilesCallback& callback) { | |
101 DCHECK(context_); | |
102 context_->task_runner()->PostShutdownBlockingTask( | |
103 FROM_HERE, | |
104 DomStorageTaskRunner::PRIMARY_SEQUENCE, | |
105 base::Bind(&GetAllStorageFilesHelper, | |
106 base::MessageLoopProxy::current(), | |
107 context_, callback)); | |
108 } | |
109 | |
110 FilePath DOMStorageContextImpl::GetFilePath(const string16& origin_id) const { | |
111 DCHECK(context_); | |
112 return OriginToFullFilePath(context_->directory(), OriginIdToGURL(origin_id)); | |
113 } | |
114 | |
115 void DOMStorageContextImpl::DeleteForOrigin(const string16& origin_id) { | |
116 DCHECK(context_); | |
117 context_->task_runner()->PostShutdownBlockingTask( | |
118 FROM_HERE, | |
119 DomStorageTaskRunner::PRIMARY_SEQUENCE, | |
120 base::Bind(&DomStorageContext::DeleteOrigin, context_, | |
121 OriginIdToGURL(origin_id))); | |
122 } | |
123 | |
124 void DOMStorageContextImpl::DeleteLocalStorageFile(const FilePath& file_path) { | |
125 DCHECK(context_); | |
126 context_->task_runner()->PostShutdownBlockingTask( | |
127 FROM_HERE, | |
128 DomStorageTaskRunner::PRIMARY_SEQUENCE, | |
129 base::Bind(&DomStorageContext::DeleteOrigin, context_, | |
130 FilePathToOrigin(file_path))); | |
131 } | |
132 | |
133 void DOMStorageContextImpl::DeleteDataModifiedSince(const base::Time& cutoff) { | |
134 DCHECK(context_); | |
135 context_->task_runner()->PostShutdownBlockingTask( | |
136 FROM_HERE, | |
137 DomStorageTaskRunner::PRIMARY_SEQUENCE, | |
138 base::Bind(&DomStorageContext::DeleteDataModifiedSince, context_, | |
139 cutoff)); | |
140 } | |
141 | |
142 void DOMStorageContextImpl::PurgeMemory() { | |
143 DCHECK(context_); | |
144 context_->task_runner()->PostShutdownBlockingTask( | |
145 FROM_HERE, | |
146 DomStorageTaskRunner::PRIMARY_SEQUENCE, | |
147 base::Bind(&DomStorageContext::PurgeMemory, context_)); | |
148 } | |
149 | |
150 void DOMStorageContextImpl::SetClearLocalState(bool clear_local_state) { | |
151 DCHECK(context_); | |
152 context_->task_runner()->PostShutdownBlockingTask( | |
153 FROM_HERE, | |
154 DomStorageTaskRunner::PRIMARY_SEQUENCE, | |
155 base::Bind(&DomStorageContext::SetClearLocalState, context_, | |
156 clear_local_state)); | |
157 } | |
158 | |
159 void DOMStorageContextImpl::SaveSessionState() { | |
160 DCHECK(context_); | |
161 context_->task_runner()->PostShutdownBlockingTask( | |
162 FROM_HERE, | |
163 DomStorageTaskRunner::PRIMARY_SEQUENCE, | |
164 base::Bind(&DomStorageContext::SaveSessionState, context_)); | |
165 } | |
166 | |
167 void DOMStorageContextImpl::Shutdown() { | |
168 DCHECK(context_); | |
169 context_->task_runner()->PostShutdownBlockingTask( | |
170 FROM_HERE, | |
171 DomStorageTaskRunner::PRIMARY_SEQUENCE, | |
172 base::Bind(&DomStorageContext::Shutdown, context_)); | |
173 } | |
174 | |
175 int64 DOMStorageContextImpl::LeakyCloneSessionStorage( | |
176 int64 existing_namespace_id) { | |
177 DCHECK(context_); | |
178 int64 clone_id = context_->AllocateSessionId(); | |
179 context_->task_runner()->PostTask( | |
180 FROM_HERE, | |
181 base::Bind(&DomStorageContext::CloneSessionNamespace, context_, | |
182 existing_namespace_id, clone_id)); | |
183 return clone_id; | |
184 } | |
185 | |
186 #endif // ENABLE_NEW_DOM_STORAGE_BACKEND | |
OLD | NEW |