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::DomStorageWorkerPoolTaskRunner; | |
25 using webkit_database::DatabaseUtil; | |
26 | |
27 namespace { | |
28 | |
29 const char kLocalStorageDirectory[] = "Local Storage"; | |
30 | |
31 // TODO(michaeln): Fix the content layer api, FilePaths and | |
32 // string16 origin_ids are just wrong. Then get rid of | |
33 // this conversion non-sense. Most of the includes are just | |
34 // to support that non-sense. | |
35 | |
36 GURL OriginIdToGURL(const string16& origin_id) { | |
37 return DatabaseUtil::GetOriginFromIdentifier(origin_id); | |
38 } | |
39 | |
40 FilePath OriginToFullFilePath(const FilePath& directory, | |
41 const GURL& origin) { | |
42 return directory.Append(DomStorageArea::DatabaseFileNameFromOrigin(origin)); | |
43 } | |
44 | |
45 GURL FilePathToOrigin(const FilePath& path) { | |
46 DCHECK(path.MatchesExtension(DomStorageArea::kDatabaseFileExtension)); | |
47 return OriginIdToGURL( | |
48 webkit_glue::FilePathToWebString(path.BaseName().RemoveExtension())); | |
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 context->GetUsageInfo(&infos); | |
63 | |
64 std::vector<FilePath> paths; | |
65 for (size_t i = 0; i < infos.size(); ++i) { | |
66 paths.push_back( | |
67 OriginToFullFilePath(context->directory(), infos[i].origin)); | |
68 } | |
69 | |
70 reply_loop->PostTask( | |
71 FROM_HERE, | |
72 base::Bind(&InvokeAllStorageFilesCallbackHelper, | |
73 callback, paths)); | |
74 } | |
75 | |
76 } | |
77 | |
78 DOMStorageContextImpl::DOMStorageContextImpl( | |
79 const FilePath& data_path, | |
80 quota::SpecialStoragePolicy* special_storage_policy) { | |
81 base::SequencedWorkerPool* worker_pool = BrowserThread::GetBlockingPool(); | |
82 context_ = new dom_storage::DomStorageContext( | |
83 data_path.empty() ? | |
84 data_path : data_path.AppendASCII(kLocalStorageDirectory), | |
85 special_storage_policy, | |
86 new DomStorageWorkerPoolTaskRunner( | |
87 worker_pool, | |
88 worker_pool->GetNamedSequenceToken("dom_storage"), | |
89 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))); | |
90 } | |
91 | |
92 DOMStorageContextImpl::~DOMStorageContextImpl() { | |
93 } | |
94 | |
95 void DOMStorageContextImpl::GetAllStorageFiles( | |
96 const GetAllStorageFilesCallback& callback) { | |
97 DCHECK(context_); | |
98 context_->task_runner()->PostTask( | |
99 FROM_HERE, | |
100 base::Bind(&GetAllStorageFilesHelper, | |
101 base::MessageLoopProxy::current(), | |
102 context_, callback)); | |
103 } | |
104 | |
105 FilePath DOMStorageContextImpl::GetFilePath(const string16& origin_id) const { | |
106 DCHECK(context_); | |
107 return OriginToFullFilePath(context_->directory(), OriginIdToGURL(origin_id)); | |
108 } | |
109 | |
110 void DOMStorageContextImpl::DeleteForOrigin(const string16& origin_id) { | |
111 DCHECK(context_); | |
112 context_->task_runner()->PostTask( | |
113 FROM_HERE, | |
114 base::Bind(&DomStorageContext::DeleteOrigin, context_, | |
115 OriginIdToGURL(origin_id))); | |
116 } | |
117 | |
118 void DOMStorageContextImpl::DeleteLocalStorageFile(const FilePath& file_path) { | |
119 DCHECK(context_); | |
120 context_->task_runner()->PostTask( | |
121 FROM_HERE, | |
122 base::Bind(&DomStorageContext::DeleteOrigin, context_, | |
123 FilePathToOrigin(file_path))); | |
124 } | |
125 | |
126 void DOMStorageContextImpl::DeleteDataModifiedSince(const base::Time& cutoff) { | |
127 DCHECK(context_); | |
128 context_->task_runner()->PostTask( | |
129 FROM_HERE, | |
130 base::Bind(&DomStorageContext::DeleteDataModifiedSince, context_, | |
131 cutoff)); | |
132 } | |
133 | |
134 void DOMStorageContextImpl::PurgeMemory() { | |
135 DCHECK(context_); | |
136 context_->task_runner()->PostTask( | |
137 FROM_HERE, | |
138 base::Bind(&DomStorageContext::PurgeMemory, context_)); | |
139 } | |
140 | |
141 void DOMStorageContextImpl::SetClearLocalState(bool clear_local_state) { | |
142 DCHECK(context_); | |
143 context_->task_runner()->PostTask( | |
144 FROM_HERE, | |
145 base::Bind(&DomStorageContext::SetClearLocalState, context_, | |
146 clear_local_state)); | |
147 } | |
148 | |
149 void DOMStorageContextImpl::SaveSessionState() { | |
150 DCHECK(context_); | |
151 context_->task_runner()->PostTask( | |
152 FROM_HERE, | |
153 base::Bind(&DomStorageContext::SaveSessionState, context_)); | |
154 } | |
155 | |
156 void DOMStorageContextImpl::Shutdown() { | |
157 DCHECK(context_); | |
158 context_->task_runner()->PostTask( | |
159 FROM_HERE, | |
160 base::Bind(&DomStorageContext::Shutdown, context_)); | |
161 } | |
162 | |
163 int64 DOMStorageContextImpl::LeakyCloneSessionStorage( | |
164 int64 existing_namespace_id) { | |
165 DCHECK(context_); | |
166 int64 clone_id = context_->AllocateSessionId(); | |
167 context_->task_runner()->PostTask( | |
168 FROM_HERE, | |
169 base::Bind(&DomStorageContext::CloneSessionNamespace, context_, | |
170 existing_namespace_id, clone_id)); | |
171 return clone_id; | |
172 } | |
173 | |
174 #endif // ENABLE_NEW_DOM_STORAGE_BACKEND | |
OLD | NEW |