Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(230)

Side by Side Diff: content/browser/dom_storage/dom_storage_context_wrapper.cc

Issue 1737933002: mojo leveldb: Get profile and leveldb connected to DOMStorageContext. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add OWNERS file by request. Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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,
88 mojo::InterfaceRequest<LevelDBWrapper> 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(
103 const mojo::String& origin,
104 mojo::InterfaceRequest<LevelDBWrapper> request);
105
106 // Used for mojo-based LocalStorage implementation (behind
107 // --mojo-local-storage for now). Maps between an origin and its prefixed
108 // LevelDB view.
109 std::map<std::string, scoped_ptr<LevelDBWrapperImpl>> level_db_wrappers_;
110
111 // The underlying mojo user id.
112 std::string mojo_user_id_;
113
114 // The subdirectory of this dom storage context.
115 std::string subdirectory_;
116
117 enum ConnectionState {
118 NO_CONNECTION,
119 CONNECTION_IN_PROGRESS,
120 CONNECTION_FINISHED
121 } connection_state_;
122
123 scoped_ptr<MojoAppConnection> profile_app_connection_;
124 profile::ProfileServicePtr profile_service_;
125 filesystem::DirectoryPtr directory_;
126
127 leveldb::LevelDBServicePtr leveldb_service_;
128 leveldb::LevelDBDatabasePtr database_;
129
130 std::vector<base::Closure> on_database_opened_callbacks_;
131
132 base::WeakPtrFactory<MojoState> weak_ptr_factory_;
133 };
134
135 void DOMStorageContextWrapper::MojoState::OpenLocalStorage(
136 const mojo::String& origin,
137 mojo::InterfaceRequest<LevelDBWrapper> request) {
138 // If we don't have a filesystem_connection_, we'll need to establish one.
139 if (connection_state_ == NO_CONNECTION) {
140 profile_app_connection_ = MojoAppConnection::Create(
141 mojo_user_id_, "mojo:profile", kBrowserMojoAppUrl);
142 profile_app_connection_->GetInterface(&profile_service_);
143
144 profile_service_->GetSubDirectory(
145 mojo::String::From(subdirectory_),
146 GetProxy(&directory_),
147 base::Bind(&MojoState::OnDirectoryOpened,
148 weak_ptr_factory_.GetWeakPtr()));
149 connection_state_ = CONNECTION_IN_PROGRESS;
150 }
151
152 if (connection_state_ == CONNECTION_IN_PROGRESS) {
153 // Queue this OpenLocalStorage call for when we have a level db pointer.
154 on_database_opened_callbacks_.push_back(
155 base::Bind(&MojoState::BindLocalStorage,
156 weak_ptr_factory_.GetWeakPtr(),
157 origin,
158 base::Passed(&request)));
159 return;
160 }
161
162 BindLocalStorage(origin, std::move(request));
163 }
164
165 void DOMStorageContextWrapper::MojoState::OnDirectoryOpened(
166 filesystem::FileError err) {
167 if (err != filesystem::FileError::OK) {
168 // We failed to open the directory; continue with startup so that we create
169 // the |level_db_wrappers_|.
170 OnDatabaseOpened(leveldb::DatabaseError::IO_ERROR);
171 return;
172 }
173
174 // Now that we have a directory, connect to the LevelDB service and get our
175 // database.
176 profile_app_connection_->GetInterface(&leveldb_service_);
177
178 leveldb_service_->Open(std::move(directory_), "leveldb", GetProxy(&database_),
179 base::Bind(&MojoState::OnDatabaseOpened,
180 weak_ptr_factory_.GetWeakPtr()));
181 }
182
183 void DOMStorageContextWrapper::MojoState::OnDatabaseOpened(
184 leveldb::DatabaseError status) {
185 if (status != leveldb::DatabaseError::OK) {
186 // If we failed to open the database, reset the service object so we pass
187 // null pointers to our wrappers.
188 database_.reset();
189 leveldb_service_.reset();
190 }
191
192 // We no longer need the profile service; we've either transferred
193 // |directory_| to the leveldb service, or we got a file error and no more is
194 // possible.
195 directory_.reset();
196 profile_service_.reset();
197
198 // |leveldb_| should be know to either be valid or invalid by now. Run our
199 // delayed bindings.
200 connection_state_ = CONNECTION_FINISHED;
201 for (size_t i = 0; i < on_database_opened_callbacks_.size(); ++i)
202 on_database_opened_callbacks_[i].Run();
203 on_database_opened_callbacks_.clear();
204 }
205
206 void DOMStorageContextWrapper::MojoState::BindLocalStorage(
207 const mojo::String& origin,
208 mojo::InterfaceRequest<LevelDBWrapper> request) {
209 if (level_db_wrappers_.find(origin) == level_db_wrappers_.end()) {
210 level_db_wrappers_[origin] = make_scoped_ptr(new LevelDBWrapperImpl(
211 database_.get(),
212 origin,
213 base::Bind(&MojoState::LevelDBWrapperImplHasNoBindings,
214 base::Unretained(this),
215 origin.get())));
216 }
217
218 level_db_wrappers_[origin]->Bind(std::move(request));
219 }
220
69 DOMStorageContextWrapper::DOMStorageContextWrapper( 221 DOMStorageContextWrapper::DOMStorageContextWrapper(
222 const std::string& mojo_user_id,
70 const base::FilePath& data_path, 223 const base::FilePath& data_path,
224 const base::FilePath& local_partition_path,
71 storage::SpecialStoragePolicy* special_storage_policy) { 225 storage::SpecialStoragePolicy* special_storage_policy) {
226 std::string subdirectory;
227 if (!data_path.empty()) {
228 subdirectory =
229 #if defined(OS_WIN)
230 base::WideToUTF8(
231 local_partition_path.AppendASCII(kLocalStorageDirectory).value());
232 #else
233 local_partition_path.AppendASCII(kLocalStorageDirectory).value();
234 #endif
235 }
236 mojo_state_.reset(new MojoState(mojo_user_id, subdirectory));
237
72 base::SequencedWorkerPool* worker_pool = BrowserThread::GetBlockingPool(); 238 base::SequencedWorkerPool* worker_pool = BrowserThread::GetBlockingPool();
73 context_ = new DOMStorageContextImpl( 239 context_ = new DOMStorageContextImpl(
74 data_path.empty() ? data_path 240 data_path.empty() ? data_path
75 : data_path.AppendASCII(kLocalStorageDirectory), 241 : data_path.AppendASCII(kLocalStorageDirectory),
76 data_path.empty() ? data_path 242 data_path.empty() ? data_path
77 : data_path.AppendASCII(kSessionStorageDirectory), 243 : data_path.AppendASCII(kSessionStorageDirectory),
78 special_storage_policy, 244 special_storage_policy,
79 new DOMStorageWorkerPoolTaskRunner( 245 new DOMStorageWorkerPoolTaskRunner(
80 worker_pool, 246 worker_pool,
81 worker_pool->GetNamedSequenceToken("dom_storage_primary"), 247 worker_pool->GetNamedSequenceToken("dom_storage_primary"),
82 worker_pool->GetNamedSequenceToken("dom_storage_commit"), 248 worker_pool->GetNamedSequenceToken("dom_storage_commit"),
83 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO) 249 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)
84 .get())); 250 .get()));
85 } 251 }
86 252
87 DOMStorageContextWrapper::~DOMStorageContextWrapper() { 253 DOMStorageContextWrapper::~DOMStorageContextWrapper() {}
88 }
89 254
90 void DOMStorageContextWrapper::GetLocalStorageUsage( 255 void DOMStorageContextWrapper::GetLocalStorageUsage(
91 const GetLocalStorageUsageCallback& callback) { 256 const GetLocalStorageUsageCallback& callback) {
92 DCHECK(context_.get()); 257 DCHECK(context_.get());
93 context_->task_runner()->PostShutdownBlockingTask( 258 context_->task_runner()->PostShutdownBlockingTask(
94 FROM_HERE, DOMStorageTaskRunner::PRIMARY_SEQUENCE, 259 FROM_HERE, DOMStorageTaskRunner::PRIMARY_SEQUENCE,
95 base::Bind(&GetLocalStorageUsageHelper, 260 base::Bind(&GetLocalStorageUsageHelper,
96 base::ThreadTaskRunnerHandle::Get(), context_, callback)); 261 base::ThreadTaskRunnerHandle::Get(), context_, callback));
97 } 262 }
98 263
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 void DOMStorageContextWrapper::SetForceKeepSessionState() { 312 void DOMStorageContextWrapper::SetForceKeepSessionState() {
148 DCHECK(context_.get()); 313 DCHECK(context_.get());
149 context_->task_runner()->PostShutdownBlockingTask( 314 context_->task_runner()->PostShutdownBlockingTask(
150 FROM_HERE, 315 FROM_HERE,
151 DOMStorageTaskRunner::PRIMARY_SEQUENCE, 316 DOMStorageTaskRunner::PRIMARY_SEQUENCE,
152 base::Bind(&DOMStorageContextImpl::SetForceKeepSessionState, context_)); 317 base::Bind(&DOMStorageContextImpl::SetForceKeepSessionState, context_));
153 } 318 }
154 319
155 void DOMStorageContextWrapper::Shutdown() { 320 void DOMStorageContextWrapper::Shutdown() {
156 DCHECK(context_.get()); 321 DCHECK(context_.get());
322 mojo_state_.reset();
157 context_->task_runner()->PostShutdownBlockingTask( 323 context_->task_runner()->PostShutdownBlockingTask(
158 FROM_HERE, 324 FROM_HERE,
159 DOMStorageTaskRunner::PRIMARY_SEQUENCE, 325 DOMStorageTaskRunner::PRIMARY_SEQUENCE,
160 base::Bind(&DOMStorageContextImpl::Shutdown, context_)); 326 base::Bind(&DOMStorageContextImpl::Shutdown, context_));
161 } 327 }
162 328
163 void DOMStorageContextWrapper::Flush() { 329 void DOMStorageContextWrapper::Flush() {
164 DCHECK(context_.get()); 330 DCHECK(context_.get());
165 context_->task_runner()->PostShutdownBlockingTask( 331 context_->task_runner()->PostShutdownBlockingTask(
166 FROM_HERE, DOMStorageTaskRunner::PRIMARY_SEQUENCE, 332 FROM_HERE, DOMStorageTaskRunner::PRIMARY_SEQUENCE,
167 base::Bind(&DOMStorageContextImpl::Flush, context_)); 333 base::Bind(&DOMStorageContextImpl::Flush, context_));
168 } 334 }
169 335
170 void DOMStorageContextWrapper::OpenLocalStorage( 336 void DOMStorageContextWrapper::OpenLocalStorage(
171 const mojo::String& origin, 337 const mojo::String& origin,
172 mojo::InterfaceRequest<LevelDBWrapper> request) { 338 mojo::InterfaceRequest<LevelDBWrapper> request) {
173 if (level_db_wrappers_.find(origin) == level_db_wrappers_.end()) { 339 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 } 340 }
193 341
194 } // namespace content 342 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698