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" |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 // for now). | 77 // for now). |
78 class DOMStorageContextWrapper::MojoState { | 78 class DOMStorageContextWrapper::MojoState { |
79 public: | 79 public: |
80 MojoState(const std::string& mojo_user_id, const base::FilePath& subdirectory) | 80 MojoState(const std::string& mojo_user_id, const base::FilePath& subdirectory) |
81 : mojo_user_id_(mojo_user_id), | 81 : mojo_user_id_(mojo_user_id), |
82 subdirectory_(subdirectory), | 82 subdirectory_(subdirectory), |
83 connection_state_(NO_CONNECTION), | 83 connection_state_(NO_CONNECTION), |
84 weak_ptr_factory_(this) {} | 84 weak_ptr_factory_(this) {} |
85 | 85 |
86 void OpenLocalStorage(const url::Origin& origin, | 86 void OpenLocalStorage(const url::Origin& origin, |
| 87 mojom::LevelDBObserverPtr observer, |
87 mojom::LevelDBWrapperRequest request); | 88 mojom::LevelDBWrapperRequest request); |
88 | 89 |
89 private: | 90 private: |
90 void LevelDBWrapperImplHasNoBindings(const url::Origin& origin) { | 91 void OnLevelDDWrapperHasNoBindings(const url::Origin& origin) { |
91 DCHECK(level_db_wrappers_.find(origin) != level_db_wrappers_.end()); | 92 DCHECK(level_db_wrappers_.find(origin) != level_db_wrappers_.end()); |
92 level_db_wrappers_.erase(origin); | 93 level_db_wrappers_.erase(origin); |
93 } | 94 } |
94 | 95 |
95 // Part of our asynchronous directory opening called from OpenLocalStorage(). | 96 // Part of our asynchronous directory opening called from OpenLocalStorage(). |
96 void OnDirectoryOpened(filesystem::FileError err); | 97 void OnDirectoryOpened(filesystem::FileError err); |
97 void OnDatabaseOpened(leveldb::DatabaseError status); | 98 void OnDatabaseOpened(leveldb::DatabaseError status); |
98 | 99 |
99 // The (possibly delayed) implementation of OpenLocalStorage(). Can be called | 100 // The (possibly delayed) implementation of OpenLocalStorage(). Can be called |
100 // directly from that function, or through |on_database_open_callbacks_|. | 101 // directly from that function, or through |on_database_open_callbacks_|. |
101 void BindLocalStorage(const url::Origin& origin, | 102 void BindLocalStorage(const url::Origin& origin, |
| 103 mojom::LevelDBObserverPtr observer, |
102 mojom::LevelDBWrapperRequest request); | 104 mojom::LevelDBWrapperRequest request); |
103 | 105 |
104 // Maps between an origin and its prefixed LevelDB view. | 106 // Maps between an origin and its prefixed LevelDB view. |
105 std::map<url::Origin, scoped_ptr<LevelDBWrapperImpl>> level_db_wrappers_; | 107 std::map<url::Origin, scoped_ptr<LevelDBWrapperImpl>> level_db_wrappers_; |
106 | 108 |
107 std::string mojo_user_id_; | 109 std::string mojo_user_id_; |
108 base::FilePath subdirectory_; | 110 base::FilePath subdirectory_; |
109 | 111 |
110 enum ConnectionState { | 112 enum ConnectionState { |
111 NO_CONNECTION, | 113 NO_CONNECTION, |
112 CONNECTION_IN_PROGRESS, | 114 CONNECTION_IN_PROGRESS, |
113 CONNECTION_FINISHED | 115 CONNECTION_FINISHED |
114 } connection_state_; | 116 } connection_state_; |
115 | 117 |
116 scoped_ptr<MojoAppConnection> profile_app_connection_; | 118 scoped_ptr<MojoAppConnection> profile_app_connection_; |
117 profile::ProfileServicePtr profile_service_; | 119 profile::ProfileServicePtr profile_service_; |
118 filesystem::DirectoryPtr directory_; | 120 filesystem::DirectoryPtr directory_; |
119 | 121 |
120 leveldb::LevelDBServicePtr leveldb_service_; | 122 leveldb::LevelDBServicePtr leveldb_service_; |
121 leveldb::LevelDBDatabasePtr database_; | 123 leveldb::LevelDBDatabasePtr database_; |
122 | 124 |
123 std::vector<base::Closure> on_database_opened_callbacks_; | 125 std::vector<base::Closure> on_database_opened_callbacks_; |
124 | 126 |
125 base::WeakPtrFactory<MojoState> weak_ptr_factory_; | 127 base::WeakPtrFactory<MojoState> weak_ptr_factory_; |
126 }; | 128 }; |
127 | 129 |
128 void DOMStorageContextWrapper::MojoState::OpenLocalStorage( | 130 void DOMStorageContextWrapper::MojoState::OpenLocalStorage( |
129 const url::Origin& origin, | 131 const url::Origin& origin, |
| 132 mojom::LevelDBObserverPtr observer, |
130 mojom::LevelDBWrapperRequest request) { | 133 mojom::LevelDBWrapperRequest request) { |
131 // If we don't have a filesystem_connection_, we'll need to establish one. | 134 // If we don't have a filesystem_connection_, we'll need to establish one. |
132 if (connection_state_ == NO_CONNECTION) { | 135 if (connection_state_ == NO_CONNECTION) { |
133 profile_app_connection_ = MojoAppConnection::Create( | 136 profile_app_connection_ = MojoAppConnection::Create( |
134 mojo_user_id_, "mojo:profile", kBrowserMojoAppUrl); | 137 mojo_user_id_, "mojo:profile", kBrowserMojoAppUrl); |
135 | 138 |
136 connection_state_ = CONNECTION_IN_PROGRESS; | 139 connection_state_ = CONNECTION_IN_PROGRESS; |
137 | 140 |
138 if (!subdirectory_.empty()) { | 141 if (!subdirectory_.empty()) { |
139 // We were given a subdirectory to write to. Get it and use a disk backed | 142 // We were given a subdirectory to write to. Get it and use a disk backed |
(...skipping 11 matching lines...) Expand all Loading... |
151 GetProxy(&database_), | 154 GetProxy(&database_), |
152 base::Bind(&MojoState::OnDatabaseOpened, | 155 base::Bind(&MojoState::OnDatabaseOpened, |
153 weak_ptr_factory_.GetWeakPtr())); | 156 weak_ptr_factory_.GetWeakPtr())); |
154 } | 157 } |
155 } | 158 } |
156 | 159 |
157 if (connection_state_ == CONNECTION_IN_PROGRESS) { | 160 if (connection_state_ == CONNECTION_IN_PROGRESS) { |
158 // Queue this OpenLocalStorage call for when we have a level db pointer. | 161 // Queue this OpenLocalStorage call for when we have a level db pointer. |
159 on_database_opened_callbacks_.push_back( | 162 on_database_opened_callbacks_.push_back( |
160 base::Bind(&MojoState::BindLocalStorage, weak_ptr_factory_.GetWeakPtr(), | 163 base::Bind(&MojoState::BindLocalStorage, weak_ptr_factory_.GetWeakPtr(), |
161 origin, base::Passed(&request))); | 164 origin, base::Passed(&observer), base::Passed(&request))); |
162 return; | 165 return; |
163 } | 166 } |
164 | 167 |
165 BindLocalStorage(origin, std::move(request)); | 168 BindLocalStorage(origin, std::move(observer), std::move(request)); |
166 } | 169 } |
167 | 170 |
168 void DOMStorageContextWrapper::MojoState::OnDirectoryOpened( | 171 void DOMStorageContextWrapper::MojoState::OnDirectoryOpened( |
169 filesystem::FileError err) { | 172 filesystem::FileError err) { |
170 if (err != filesystem::FileError::OK) { | 173 if (err != filesystem::FileError::OK) { |
171 // We failed to open the directory; continue with startup so that we create | 174 // We failed to open the directory; continue with startup so that we create |
172 // the |level_db_wrappers_|. | 175 // the |level_db_wrappers_|. |
173 OnDatabaseOpened(leveldb::DatabaseError::IO_ERROR); | 176 OnDatabaseOpened(leveldb::DatabaseError::IO_ERROR); |
174 return; | 177 return; |
175 } | 178 } |
(...skipping 25 matching lines...) Expand all Loading... |
201 // |leveldb_| should be known to either be valid or invalid by now. Run our | 204 // |leveldb_| should be known to either be valid or invalid by now. Run our |
202 // delayed bindings. | 205 // delayed bindings. |
203 connection_state_ = CONNECTION_FINISHED; | 206 connection_state_ = CONNECTION_FINISHED; |
204 for (size_t i = 0; i < on_database_opened_callbacks_.size(); ++i) | 207 for (size_t i = 0; i < on_database_opened_callbacks_.size(); ++i) |
205 on_database_opened_callbacks_[i].Run(); | 208 on_database_opened_callbacks_[i].Run(); |
206 on_database_opened_callbacks_.clear(); | 209 on_database_opened_callbacks_.clear(); |
207 } | 210 } |
208 | 211 |
209 void DOMStorageContextWrapper::MojoState::BindLocalStorage( | 212 void DOMStorageContextWrapper::MojoState::BindLocalStorage( |
210 const url::Origin& origin, | 213 const url::Origin& origin, |
| 214 mojom::LevelDBObserverPtr observer, |
211 mojom::LevelDBWrapperRequest request) { | 215 mojom::LevelDBWrapperRequest request) { |
212 if (level_db_wrappers_.find(origin) == level_db_wrappers_.end()) { | 216 auto found = level_db_wrappers_.find(origin); |
| 217 if (found == level_db_wrappers_.end()) { |
213 level_db_wrappers_[origin] = make_scoped_ptr(new LevelDBWrapperImpl( | 218 level_db_wrappers_[origin] = make_scoped_ptr(new LevelDBWrapperImpl( |
214 database_.get(), | 219 database_.get(), |
215 origin.Serialize(), | 220 origin.Serialize(), |
216 base::Bind(&MojoState::LevelDBWrapperImplHasNoBindings, | 221 kPerStorageAreaQuota + kPerStorageAreaOverQuotaAllowance, |
| 222 base::Bind(&MojoState::OnLevelDDWrapperHasNoBindings, |
217 base::Unretained(this), | 223 base::Unretained(this), |
218 origin))); | 224 origin))); |
| 225 found = level_db_wrappers_.find(origin); |
219 } | 226 } |
220 | 227 |
221 level_db_wrappers_[origin]->Bind(std::move(request)); | 228 found->second->Bind(std::move(request)); |
| 229 found->second->AddObserver(std::move(observer)); |
222 } | 230 } |
223 | 231 |
224 DOMStorageContextWrapper::DOMStorageContextWrapper( | 232 DOMStorageContextWrapper::DOMStorageContextWrapper( |
225 const std::string& mojo_user_id, | 233 const std::string& mojo_user_id, |
226 const base::FilePath& profile_path, | 234 const base::FilePath& profile_path, |
227 const base::FilePath& local_partition_path, | 235 const base::FilePath& local_partition_path, |
228 storage::SpecialStoragePolicy* special_storage_policy) { | 236 storage::SpecialStoragePolicy* special_storage_policy) { |
229 base::FilePath storage_dir; | 237 base::FilePath storage_dir; |
230 if (!profile_path.empty()) | 238 if (!profile_path.empty()) |
231 storage_dir = local_partition_path.AppendASCII(kLocalStorageDirectory); | 239 storage_dir = local_partition_path.AppendASCII(kLocalStorageDirectory); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 | 337 |
330 void DOMStorageContextWrapper::Flush() { | 338 void DOMStorageContextWrapper::Flush() { |
331 DCHECK(context_.get()); | 339 DCHECK(context_.get()); |
332 context_->task_runner()->PostShutdownBlockingTask( | 340 context_->task_runner()->PostShutdownBlockingTask( |
333 FROM_HERE, DOMStorageTaskRunner::PRIMARY_SEQUENCE, | 341 FROM_HERE, DOMStorageTaskRunner::PRIMARY_SEQUENCE, |
334 base::Bind(&DOMStorageContextImpl::Flush, context_)); | 342 base::Bind(&DOMStorageContextImpl::Flush, context_)); |
335 } | 343 } |
336 | 344 |
337 void DOMStorageContextWrapper::OpenLocalStorage( | 345 void DOMStorageContextWrapper::OpenLocalStorage( |
338 const url::Origin& origin, | 346 const url::Origin& origin, |
| 347 mojom::LevelDBObserverPtr observer, |
339 mojom::LevelDBWrapperRequest request) { | 348 mojom::LevelDBWrapperRequest request) { |
340 mojo_state_->OpenLocalStorage(origin, std::move(request)); | 349 mojo_state_->OpenLocalStorage( |
| 350 origin, std::move(observer), std::move(request)); |
341 } | 351 } |
342 | 352 |
343 } // namespace content | 353 } // namespace content |
OLD | NEW |