OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/dom_storage/dom_storage_context_wrapper.h" | 5 #include "content/browser/dom_storage/dom_storage_context_wrapper.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/location.h" | 13 #include "base/location.h" |
14 #include "base/memory/weak_ptr.h" | |
14 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
16 #include "base/strings/utf_string_conversions.h" | |
15 #include "base/thread_task_runner_handle.h" | 17 #include "base/thread_task_runner_handle.h" |
18 #include "components/filesystem/public/interfaces/directory.mojom.h" | |
19 #include "components/leveldb/public/interfaces/leveldb.mojom.h" | |
20 #include "components/profile_service/public/interfaces/profile.mojom.h" | |
16 #include "content/browser/dom_storage/dom_storage_area.h" | 21 #include "content/browser/dom_storage/dom_storage_area.h" |
17 #include "content/browser/dom_storage/dom_storage_context_impl.h" | 22 #include "content/browser/dom_storage/dom_storage_context_impl.h" |
18 #include "content/browser/dom_storage/dom_storage_task_runner.h" | 23 #include "content/browser/dom_storage/dom_storage_task_runner.h" |
19 #include "content/browser/dom_storage/session_storage_namespace_impl.h" | 24 #include "content/browser/dom_storage/session_storage_namespace_impl.h" |
20 #include "content/browser/leveldb_wrapper_impl.h" | 25 #include "content/browser/leveldb_wrapper_impl.h" |
21 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
22 #include "content/public/browser/local_storage_usage_info.h" | 27 #include "content/public/browser/local_storage_usage_info.h" |
28 #include "content/public/browser/mojo_app_connection.h" | |
23 #include "content/public/browser/session_storage_usage_info.h" | 29 #include "content/public/browser/session_storage_usage_info.h" |
30 #include "mojo/common/common_type_converters.h" | |
24 | 31 |
25 namespace content { | 32 namespace content { |
26 namespace { | 33 namespace { |
27 | 34 |
28 const char kLocalStorageDirectory[] = "Local Storage"; | 35 const char kLocalStorageDirectory[] = "Local Storage"; |
29 const char kSessionStorageDirectory[] = "Session Storage"; | 36 const char kSessionStorageDirectory[] = "Session Storage"; |
30 | 37 |
31 void InvokeLocalStorageUsageCallbackHelper( | 38 void InvokeLocalStorageUsageCallbackHelper( |
32 const DOMStorageContext::GetLocalStorageUsageCallback& callback, | 39 const DOMStorageContext::GetLocalStorageUsageCallback& callback, |
33 const std::vector<LocalStorageUsageInfo>* infos) { | 40 const std::vector<LocalStorageUsageInfo>* infos) { |
(...skipping 25 matching lines...) Expand all Loading... | |
59 std::vector<SessionStorageUsageInfo>* infos = | 66 std::vector<SessionStorageUsageInfo>* infos = |
60 new std::vector<SessionStorageUsageInfo>; | 67 new std::vector<SessionStorageUsageInfo>; |
61 context->GetSessionStorageUsage(infos); | 68 context->GetSessionStorageUsage(infos); |
62 reply_task_runner->PostTask( | 69 reply_task_runner->PostTask( |
63 FROM_HERE, base::Bind(&InvokeSessionStorageUsageCallbackHelper, callback, | 70 FROM_HERE, base::Bind(&InvokeSessionStorageUsageCallbackHelper, callback, |
64 base::Owned(infos))); | 71 base::Owned(infos))); |
65 } | 72 } |
66 | 73 |
67 } // namespace | 74 } // namespace |
68 | 75 |
76 // An internal class which encapsulates all the mojoy details. | |
77 class DOMStorageContextWrapper::MojoState { | |
78 public: | |
79 MojoState(const std::string& mojo_user_id, | |
80 const std::string& subdirectory) | |
81 : mojo_user_id_(mojo_user_id), | |
82 subdirectory_(subdirectory), | |
83 connection_state_(NO_CONNECTION), | |
84 weak_ptr_factory_(this) {} | |
85 | |
86 void OpenLocalStorage( | |
87 const mojo::String& origin, | |
michaeln
2016/03/16 03:11:13
needs rebaselining to const url::Origin& origin
| |
88 LevelDBWrapperRequest request); | |
89 | |
90 private: | |
91 void LevelDBWrapperImplHasNoBindings(const std::string& origin) { | |
92 DCHECK(level_db_wrappers_.find(origin) != level_db_wrappers_.end()); | |
93 level_db_wrappers_.erase(origin); | |
94 } | |
95 | |
96 // Part of our asynchronous directory opening called from OpenLocalStorage(). | |
97 void OnDirectoryOpened(filesystem::FileError err); | |
98 void OnDatabaseOpened(leveldb::DatabaseError status); | |
99 | |
100 // The (possibly delayed) implementation of OpenLocalStorage(). Can be called | |
101 // directly from that function, or through |on_database_open_callbacks_|. | |
102 void BindLocalStorage(const mojo::String& origin, | |
103 LevelDBWrapperRequest request); | |
104 | |
105 // Used for mojo-based LocalStorage implementation (behind | |
106 // --mojo-local-storage for now). Maps between an origin and its prefixed | |
michaeln
2016/03/16 03:11:13
nit: hoist the --mojo-locals-storage comment to th
| |
107 // LevelDB view. | |
108 std::map<std::string, scoped_ptr<LevelDBWrapperImpl>> level_db_wrappers_; | |
michaeln
2016/03/16 03:11:13
ditto url::Origin rebaseline, and then the doc com
| |
109 | |
110 // The underlying mojo user id. | |
michaeln
2016/03/16 03:11:13
nit: comment not really needed
| |
111 std::string mojo_user_id_; | |
112 | |
113 // The subdirectory of this dom storage context. | |
michaeln
2016/03/16 03:11:13
This directory is specifically for local storage,
| |
114 std::string subdirectory_; | |
michaeln
2016/03/16 03:11:13
base::FilePath?
| |
115 | |
116 enum ConnectionState { | |
117 NO_CONNECTION, | |
118 CONNECTION_IN_PROGRESS, | |
119 CONNECTION_FINISHED | |
120 } connection_state_; | |
121 | |
122 scoped_ptr<MojoAppConnection> profile_app_connection_; | |
123 profile::ProfileServicePtr profile_service_; | |
124 filesystem::DirectoryPtr directory_; | |
125 | |
126 leveldb::LevelDBServicePtr leveldb_service_; | |
127 leveldb::LevelDBDatabasePtr database_; | |
128 | |
129 std::vector<base::Closure> on_database_opened_callbacks_; | |
130 | |
131 base::WeakPtrFactory<MojoState> weak_ptr_factory_; | |
132 }; | |
133 | |
134 void DOMStorageContextWrapper::MojoState::OpenLocalStorage( | |
135 const mojo::String& origin, | |
136 LevelDBWrapperRequest request) { | |
137 // If we don't have a filesystem_connection_, we'll need to establish one. | |
138 if (connection_state_ == NO_CONNECTION) { | |
139 profile_app_connection_ = MojoAppConnection::Create( | |
140 mojo_user_id_, "mojo:profile", kBrowserMojoAppUrl); | |
141 profile_app_connection_->GetInterface(&profile_service_); | |
142 | |
143 profile_service_->GetSubDirectory( | |
144 mojo::String::From(subdirectory_), | |
michaeln
2016/03/16 03:11:13
when subdirectory_ is empty, as in the incognito c
| |
145 GetProxy(&directory_), | |
146 base::Bind(&MojoState::OnDirectoryOpened, | |
147 weak_ptr_factory_.GetWeakPtr())); | |
148 connection_state_ = CONNECTION_IN_PROGRESS; | |
149 } | |
150 | |
151 if (connection_state_ == CONNECTION_IN_PROGRESS) { | |
152 // Queue this OpenLocalStorage call for when we have a level db pointer. | |
153 on_database_opened_callbacks_.push_back( | |
154 base::Bind(&MojoState::BindLocalStorage, | |
155 weak_ptr_factory_.GetWeakPtr(), | |
156 origin, | |
157 base::Passed(&request))); | |
158 return; | |
159 } | |
160 | |
161 BindLocalStorage(origin, std::move(request)); | |
162 } | |
163 | |
164 void DOMStorageContextWrapper::MojoState::OnDirectoryOpened( | |
165 filesystem::FileError err) { | |
166 if (err != filesystem::FileError::OK) { | |
167 // We failed to open the directory; continue with startup so that we create | |
168 // the |level_db_wrappers_|. | |
169 OnDatabaseOpened(leveldb::DatabaseError::IO_ERROR); | |
170 return; | |
171 } | |
172 | |
173 // Now that we have a directory, connect to the LevelDB service and get our | |
174 // database. | |
175 profile_app_connection_->GetInterface(&leveldb_service_); | |
176 | |
177 leveldb_service_->Open(std::move(directory_), "leveldb", GetProxy(&database_), | |
178 base::Bind(&MojoState::OnDatabaseOpened, | |
179 weak_ptr_factory_.GetWeakPtr())); | |
180 } | |
181 | |
182 void DOMStorageContextWrapper::MojoState::OnDatabaseOpened( | |
183 leveldb::DatabaseError status) { | |
184 if (status != leveldb::DatabaseError::OK) { | |
185 // If we failed to open the database, reset the service object so we pass | |
186 // null pointers to our wrappers. | |
187 database_.reset(); | |
188 leveldb_service_.reset(); | |
189 } | |
190 | |
191 // We no longer need the profile service; we've either transferred | |
192 // |directory_| to the leveldb service, or we got a file error and no more is | |
193 // possible. | |
194 directory_.reset(); | |
195 profile_service_.reset(); | |
196 | |
197 // |leveldb_| should be know to either be valid or invalid by now. Run our | |
198 // delayed bindings. | |
199 connection_state_ = CONNECTION_FINISHED; | |
200 for (size_t i = 0; i < on_database_opened_callbacks_.size(); ++i) | |
201 on_database_opened_callbacks_[i].Run(); | |
202 on_database_opened_callbacks_.clear(); | |
203 } | |
204 | |
205 void DOMStorageContextWrapper::MojoState::BindLocalStorage( | |
206 const mojo::String& origin, | |
207 LevelDBWrapperRequest request) { | |
208 if (level_db_wrappers_.find(origin) == level_db_wrappers_.end()) { | |
209 level_db_wrappers_[origin] = make_scoped_ptr(new LevelDBWrapperImpl( | |
210 database_.get(), | |
211 origin, | |
212 base::Bind(&MojoState::LevelDBWrapperImplHasNoBindings, | |
213 base::Unretained(this), | |
214 origin.get()))); | |
215 } | |
216 | |
217 level_db_wrappers_[origin]->Bind(std::move(request)); | |
218 } | |
219 | |
69 DOMStorageContextWrapper::DOMStorageContextWrapper( | 220 DOMStorageContextWrapper::DOMStorageContextWrapper( |
221 const std::string& mojo_user_id, | |
70 const base::FilePath& data_path, | 222 const base::FilePath& data_path, |
223 const base::FilePath& local_partition_path, | |
71 storage::SpecialStoragePolicy* special_storage_policy) { | 224 storage::SpecialStoragePolicy* special_storage_policy) { |
225 std::string subdirectory; | |
226 if (!data_path.empty()) { | |
227 subdirectory = | |
228 #if defined(OS_WIN) | |
michaeln
2016/03/16 03:11:13
Resorting to platform specific path handling feels
| |
229 base::WideToUTF8( | |
230 local_partition_path.AppendASCII(kLocalStorageDirectory).value()); | |
231 #else | |
232 local_partition_path.AppendASCII(kLocalStorageDirectory).value(); | |
233 #endif | |
234 } | |
235 mojo_state_.reset(new MojoState(mojo_user_id, subdirectory)); | |
236 | |
72 base::SequencedWorkerPool* worker_pool = BrowserThread::GetBlockingPool(); | 237 base::SequencedWorkerPool* worker_pool = BrowserThread::GetBlockingPool(); |
73 context_ = new DOMStorageContextImpl( | 238 context_ = new DOMStorageContextImpl( |
74 data_path.empty() ? data_path | 239 data_path.empty() ? data_path |
75 : data_path.AppendASCII(kLocalStorageDirectory), | 240 : data_path.AppendASCII(kLocalStorageDirectory), |
76 data_path.empty() ? data_path | 241 data_path.empty() ? data_path |
77 : data_path.AppendASCII(kSessionStorageDirectory), | 242 : data_path.AppendASCII(kSessionStorageDirectory), |
78 special_storage_policy, | 243 special_storage_policy, |
79 new DOMStorageWorkerPoolTaskRunner( | 244 new DOMStorageWorkerPoolTaskRunner( |
80 worker_pool, | 245 worker_pool, |
81 worker_pool->GetNamedSequenceToken("dom_storage_primary"), | 246 worker_pool->GetNamedSequenceToken("dom_storage_primary"), |
82 worker_pool->GetNamedSequenceToken("dom_storage_commit"), | 247 worker_pool->GetNamedSequenceToken("dom_storage_commit"), |
83 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO) | 248 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO) |
84 .get())); | 249 .get())); |
85 } | 250 } |
86 | 251 |
87 DOMStorageContextWrapper::~DOMStorageContextWrapper() { | 252 DOMStorageContextWrapper::~DOMStorageContextWrapper() {} |
88 } | |
89 | 253 |
90 void DOMStorageContextWrapper::GetLocalStorageUsage( | 254 void DOMStorageContextWrapper::GetLocalStorageUsage( |
91 const GetLocalStorageUsageCallback& callback) { | 255 const GetLocalStorageUsageCallback& callback) { |
92 DCHECK(context_.get()); | 256 DCHECK(context_.get()); |
93 context_->task_runner()->PostShutdownBlockingTask( | 257 context_->task_runner()->PostShutdownBlockingTask( |
94 FROM_HERE, DOMStorageTaskRunner::PRIMARY_SEQUENCE, | 258 FROM_HERE, DOMStorageTaskRunner::PRIMARY_SEQUENCE, |
95 base::Bind(&GetLocalStorageUsageHelper, | 259 base::Bind(&GetLocalStorageUsageHelper, |
96 base::ThreadTaskRunnerHandle::Get(), context_, callback)); | 260 base::ThreadTaskRunnerHandle::Get(), context_, callback)); |
97 } | 261 } |
98 | 262 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
147 void DOMStorageContextWrapper::SetForceKeepSessionState() { | 311 void DOMStorageContextWrapper::SetForceKeepSessionState() { |
148 DCHECK(context_.get()); | 312 DCHECK(context_.get()); |
149 context_->task_runner()->PostShutdownBlockingTask( | 313 context_->task_runner()->PostShutdownBlockingTask( |
150 FROM_HERE, | 314 FROM_HERE, |
151 DOMStorageTaskRunner::PRIMARY_SEQUENCE, | 315 DOMStorageTaskRunner::PRIMARY_SEQUENCE, |
152 base::Bind(&DOMStorageContextImpl::SetForceKeepSessionState, context_)); | 316 base::Bind(&DOMStorageContextImpl::SetForceKeepSessionState, context_)); |
153 } | 317 } |
154 | 318 |
155 void DOMStorageContextWrapper::Shutdown() { | 319 void DOMStorageContextWrapper::Shutdown() { |
156 DCHECK(context_.get()); | 320 DCHECK(context_.get()); |
321 mojo_state_.reset(); | |
157 context_->task_runner()->PostShutdownBlockingTask( | 322 context_->task_runner()->PostShutdownBlockingTask( |
158 FROM_HERE, | 323 FROM_HERE, |
159 DOMStorageTaskRunner::PRIMARY_SEQUENCE, | 324 DOMStorageTaskRunner::PRIMARY_SEQUENCE, |
160 base::Bind(&DOMStorageContextImpl::Shutdown, context_)); | 325 base::Bind(&DOMStorageContextImpl::Shutdown, context_)); |
161 } | 326 } |
162 | 327 |
163 void DOMStorageContextWrapper::Flush() { | 328 void DOMStorageContextWrapper::Flush() { |
164 DCHECK(context_.get()); | 329 DCHECK(context_.get()); |
165 context_->task_runner()->PostShutdownBlockingTask( | 330 context_->task_runner()->PostShutdownBlockingTask( |
166 FROM_HERE, DOMStorageTaskRunner::PRIMARY_SEQUENCE, | 331 FROM_HERE, DOMStorageTaskRunner::PRIMARY_SEQUENCE, |
167 base::Bind(&DOMStorageContextImpl::Flush, context_)); | 332 base::Bind(&DOMStorageContextImpl::Flush, context_)); |
168 } | 333 } |
169 | 334 |
170 void DOMStorageContextWrapper::OpenLocalStorage( | 335 void DOMStorageContextWrapper::OpenLocalStorage( |
171 const mojo::String& origin, | 336 const mojo::String& origin, |
172 mojo::InterfaceRequest<LevelDBWrapper> request) { | 337 LevelDBWrapperRequest request) { |
173 if (level_db_wrappers_.find(origin) == level_db_wrappers_.end()) { | 338 mojo_state_->OpenLocalStorage(origin, std::move(request)); |
174 level_db_wrappers_[origin] = make_scoped_ptr(new LevelDBWrapperImpl( | |
175 origin, | |
176 base::Bind(&DOMStorageContextWrapper::LevelDBWrapperImplHasNoBindings, | |
177 base::Unretained(this), | |
178 origin.get()))); | |
179 } | |
180 // TODO(jam): call LevelDB service (once per this object) to open the database | |
181 // for LocalStorage and keep a pointer to it in this class. Then keep a map | |
182 // from origins to LevelDBWrapper object. Each call here for the same origin | |
183 // should use the same LevelDBWrapper object. | |
184 | |
185 level_db_wrappers_[origin]->Bind(std::move(request)); | |
186 } | |
187 | |
188 void DOMStorageContextWrapper::LevelDBWrapperImplHasNoBindings( | |
189 const std::string& origin) { | |
190 DCHECK(level_db_wrappers_.find(origin) != level_db_wrappers_.end()); | |
191 level_db_wrappers_.erase(origin); | |
192 } | 339 } |
193 | 340 |
194 } // namespace content | 341 } // namespace content |
OLD | NEW |