| 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/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 18 #include "base/thread_task_runner_handle.h" | 18 #include "base/thread_task_runner_handle.h" |
| 19 #include "components/filesystem/public/interfaces/directory.mojom.h" | 19 #include "components/filesystem/public/interfaces/directory.mojom.h" |
| 20 #include "components/leveldb/public/interfaces/leveldb.mojom.h" | 20 #include "components/leveldb/public/interfaces/leveldb.mojom.h" |
| 21 #include "components/profile_service/public/cpp/constants.h" |
| 21 #include "components/profile_service/public/interfaces/profile.mojom.h" | 22 #include "components/profile_service/public/interfaces/profile.mojom.h" |
| 22 #include "content/browser/dom_storage/dom_storage_area.h" | 23 #include "content/browser/dom_storage/dom_storage_area.h" |
| 23 #include "content/browser/dom_storage/dom_storage_context_impl.h" | 24 #include "content/browser/dom_storage/dom_storage_context_impl.h" |
| 24 #include "content/browser/dom_storage/dom_storage_task_runner.h" | 25 #include "content/browser/dom_storage/dom_storage_task_runner.h" |
| 25 #include "content/browser/dom_storage/session_storage_namespace_impl.h" | 26 #include "content/browser/dom_storage/session_storage_namespace_impl.h" |
| 26 #include "content/browser/leveldb_wrapper_impl.h" | 27 #include "content/browser/leveldb_wrapper_impl.h" |
| 27 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
| 28 #include "content/public/browser/local_storage_usage_info.h" | 29 #include "content/public/browser/local_storage_usage_info.h" |
| 29 #include "content/public/browser/mojo_app_connection.h" | |
| 30 #include "content/public/browser/session_storage_usage_info.h" | 30 #include "content/public/browser/session_storage_usage_info.h" |
| 31 #include "mojo/common/common_type_converters.h" | 31 #include "mojo/common/common_type_converters.h" |
| 32 #include "services/shell/public/cpp/connection.h" |
| 33 #include "services/shell/public/cpp/connector.h" |
| 32 | 34 |
| 33 namespace content { | 35 namespace content { |
| 34 namespace { | 36 namespace { |
| 35 | 37 |
| 36 const char kLocalStorageDirectory[] = "Local Storage"; | 38 const char kLocalStorageDirectory[] = "Local Storage"; |
| 37 const char kSessionStorageDirectory[] = "Session Storage"; | 39 const char kSessionStorageDirectory[] = "Session Storage"; |
| 38 | 40 |
| 39 void InvokeLocalStorageUsageCallbackHelper( | 41 void InvokeLocalStorageUsageCallbackHelper( |
| 40 const DOMStorageContext::GetLocalStorageUsageCallback& callback, | 42 const DOMStorageContext::GetLocalStorageUsageCallback& callback, |
| 41 const std::vector<LocalStorageUsageInfo>* infos) { | 43 const std::vector<LocalStorageUsageInfo>* infos) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 71 FROM_HERE, base::Bind(&InvokeSessionStorageUsageCallbackHelper, callback, | 73 FROM_HERE, base::Bind(&InvokeSessionStorageUsageCallbackHelper, callback, |
| 72 base::Owned(infos))); | 74 base::Owned(infos))); |
| 73 } | 75 } |
| 74 | 76 |
| 75 } // namespace | 77 } // namespace |
| 76 | 78 |
| 77 // Used for mojo-based LocalStorage implementation (behind --mojo-local-storage | 79 // Used for mojo-based LocalStorage implementation (behind --mojo-local-storage |
| 78 // for now). | 80 // for now). |
| 79 class DOMStorageContextWrapper::MojoState { | 81 class DOMStorageContextWrapper::MojoState { |
| 80 public: | 82 public: |
| 81 MojoState(const std::string& mojo_user_id, const base::FilePath& subdirectory) | 83 MojoState(mojo::Connector* connector, const base::FilePath& subdirectory) |
| 82 : mojo_user_id_(mojo_user_id), | 84 : connector_(connector), |
| 83 subdirectory_(subdirectory), | 85 subdirectory_(subdirectory), |
| 84 connection_state_(NO_CONNECTION), | 86 connection_state_(NO_CONNECTION), |
| 85 weak_ptr_factory_(this) {} | 87 weak_ptr_factory_(this) {} |
| 86 | 88 |
| 87 void OpenLocalStorage(const url::Origin& origin, | 89 void OpenLocalStorage(const url::Origin& origin, |
| 88 mojom::LevelDBObserverPtr observer, | 90 mojom::LevelDBObserverPtr observer, |
| 89 mojom::LevelDBWrapperRequest request); | 91 mojom::LevelDBWrapperRequest request); |
| 90 | 92 |
| 91 private: | 93 private: |
| 92 void OnLevelDDWrapperHasNoBindings(const url::Origin& origin) { | 94 void OnLevelDDWrapperHasNoBindings(const url::Origin& origin) { |
| 93 DCHECK(level_db_wrappers_.find(origin) != level_db_wrappers_.end()); | 95 DCHECK(level_db_wrappers_.find(origin) != level_db_wrappers_.end()); |
| 94 level_db_wrappers_.erase(origin); | 96 level_db_wrappers_.erase(origin); |
| 95 } | 97 } |
| 96 | 98 |
| 97 // Part of our asynchronous directory opening called from OpenLocalStorage(). | 99 // Part of our asynchronous directory opening called from OpenLocalStorage(). |
| 98 void OnDirectoryOpened(filesystem::FileError err); | 100 void OnDirectoryOpened(filesystem::FileError err); |
| 99 void OnDatabaseOpened(leveldb::DatabaseError status); | 101 void OnDatabaseOpened(leveldb::DatabaseError status); |
| 100 | 102 |
| 101 // The (possibly delayed) implementation of OpenLocalStorage(). Can be called | 103 // The (possibly delayed) implementation of OpenLocalStorage(). Can be called |
| 102 // directly from that function, or through |on_database_open_callbacks_|. | 104 // directly from that function, or through |on_database_open_callbacks_|. |
| 103 void BindLocalStorage(const url::Origin& origin, | 105 void BindLocalStorage(const url::Origin& origin, |
| 104 mojom::LevelDBObserverPtr observer, | 106 mojom::LevelDBObserverPtr observer, |
| 105 mojom::LevelDBWrapperRequest request); | 107 mojom::LevelDBWrapperRequest request); |
| 106 | 108 |
| 107 // Maps between an origin and its prefixed LevelDB view. | 109 // Maps between an origin and its prefixed LevelDB view. |
| 108 std::map<url::Origin, std::unique_ptr<LevelDBWrapperImpl>> level_db_wrappers_; | 110 std::map<url::Origin, std::unique_ptr<LevelDBWrapperImpl>> level_db_wrappers_; |
| 109 | 111 |
| 110 std::string mojo_user_id_; | 112 mojo::Connector* const connector_; |
| 111 base::FilePath subdirectory_; | 113 const base::FilePath subdirectory_; |
| 112 | 114 |
| 113 enum ConnectionState { | 115 enum ConnectionState { |
| 114 NO_CONNECTION, | 116 NO_CONNECTION, |
| 115 CONNECTION_IN_PROGRESS, | 117 CONNECTION_IN_PROGRESS, |
| 116 CONNECTION_FINISHED | 118 CONNECTION_FINISHED |
| 117 } connection_state_; | 119 } connection_state_; |
| 118 | 120 |
| 119 std::unique_ptr<MojoAppConnection> profile_app_connection_; | 121 std::unique_ptr<mojo::Connection> profile_app_connection_; |
| 122 |
| 120 profile::ProfileServicePtr profile_service_; | 123 profile::ProfileServicePtr profile_service_; |
| 121 filesystem::DirectoryPtr directory_; | 124 filesystem::DirectoryPtr directory_; |
| 122 | 125 |
| 123 leveldb::LevelDBServicePtr leveldb_service_; | 126 leveldb::LevelDBServicePtr leveldb_service_; |
| 124 leveldb::LevelDBDatabasePtr database_; | 127 leveldb::LevelDBDatabasePtr database_; |
| 125 | 128 |
| 126 std::vector<base::Closure> on_database_opened_callbacks_; | 129 std::vector<base::Closure> on_database_opened_callbacks_; |
| 127 | 130 |
| 128 base::WeakPtrFactory<MojoState> weak_ptr_factory_; | 131 base::WeakPtrFactory<MojoState> weak_ptr_factory_; |
| 129 }; | 132 }; |
| 130 | 133 |
| 131 void DOMStorageContextWrapper::MojoState::OpenLocalStorage( | 134 void DOMStorageContextWrapper::MojoState::OpenLocalStorage( |
| 132 const url::Origin& origin, | 135 const url::Origin& origin, |
| 133 mojom::LevelDBObserverPtr observer, | 136 mojom::LevelDBObserverPtr observer, |
| 134 mojom::LevelDBWrapperRequest request) { | 137 mojom::LevelDBWrapperRequest request) { |
| 135 // If we don't have a filesystem_connection_, we'll need to establish one. | 138 // If we don't have a filesystem_connection_, we'll need to establish one. |
| 136 if (connection_state_ == NO_CONNECTION) { | 139 if (connection_state_ == NO_CONNECTION) { |
| 137 profile_app_connection_ = MojoAppConnection::Create( | 140 CHECK(connector_); |
| 138 mojo_user_id_, "mojo:profile", kBrowserMojoAppUrl); | 141 profile_app_connection_ = |
| 139 | 142 connector_->Connect(profile::kProfileMojoApplicationName); |
| 140 connection_state_ = CONNECTION_IN_PROGRESS; | 143 connection_state_ = CONNECTION_IN_PROGRESS; |
| 141 | 144 |
| 142 if (!subdirectory_.empty()) { | 145 if (!subdirectory_.empty()) { |
| 143 // We were given a subdirectory to write to. Get it and use a disk backed | 146 // We were given a subdirectory to write to. Get it and use a disk backed |
| 144 // database. | 147 // database. |
| 145 profile_app_connection_->GetInterface(&profile_service_); | 148 profile_app_connection_->GetInterface(&profile_service_); |
| 146 profile_service_->GetSubDirectory( | 149 profile_service_->GetSubDirectory( |
| 147 mojo::String::From(subdirectory_.AsUTF8Unsafe()), | 150 mojo::String::From(subdirectory_.AsUTF8Unsafe()), |
| 148 GetProxy(&directory_), | 151 GetProxy(&directory_), |
| 149 base::Bind(&MojoState::OnDirectoryOpened, | 152 base::Bind(&MojoState::OnDirectoryOpened, |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 base::Bind(&MojoState::OnLevelDDWrapperHasNoBindings, | 225 base::Bind(&MojoState::OnLevelDDWrapperHasNoBindings, |
| 223 base::Unretained(this), origin))); | 226 base::Unretained(this), origin))); |
| 224 found = level_db_wrappers_.find(origin); | 227 found = level_db_wrappers_.find(origin); |
| 225 } | 228 } |
| 226 | 229 |
| 227 found->second->Bind(std::move(request)); | 230 found->second->Bind(std::move(request)); |
| 228 found->second->AddObserver(std::move(observer)); | 231 found->second->AddObserver(std::move(observer)); |
| 229 } | 232 } |
| 230 | 233 |
| 231 DOMStorageContextWrapper::DOMStorageContextWrapper( | 234 DOMStorageContextWrapper::DOMStorageContextWrapper( |
| 232 const std::string& mojo_user_id, | 235 mojo::Connector* connector, |
| 233 const base::FilePath& profile_path, | 236 const base::FilePath& profile_path, |
| 234 const base::FilePath& local_partition_path, | 237 const base::FilePath& local_partition_path, |
| 235 storage::SpecialStoragePolicy* special_storage_policy) { | 238 storage::SpecialStoragePolicy* special_storage_policy) { |
| 236 base::FilePath storage_dir; | 239 base::FilePath storage_dir; |
| 237 if (!profile_path.empty()) | 240 if (!profile_path.empty()) |
| 238 storage_dir = local_partition_path.AppendASCII(kLocalStorageDirectory); | 241 storage_dir = local_partition_path.AppendASCII(kLocalStorageDirectory); |
| 239 mojo_state_.reset(new MojoState(mojo_user_id, storage_dir)); | 242 mojo_state_.reset(new MojoState(connector, storage_dir)); |
| 240 | 243 |
| 241 base::FilePath data_path; | 244 base::FilePath data_path; |
| 242 if (!profile_path.empty()) | 245 if (!profile_path.empty()) |
| 243 data_path = profile_path.Append(local_partition_path); | 246 data_path = profile_path.Append(local_partition_path); |
| 244 base::SequencedWorkerPool* worker_pool = BrowserThread::GetBlockingPool(); | 247 base::SequencedWorkerPool* worker_pool = BrowserThread::GetBlockingPool(); |
| 245 context_ = new DOMStorageContextImpl( | 248 context_ = new DOMStorageContextImpl( |
| 246 data_path.empty() ? data_path | 249 data_path.empty() ? data_path |
| 247 : data_path.AppendASCII(kLocalStorageDirectory), | 250 : data_path.AppendASCII(kLocalStorageDirectory), |
| 248 data_path.empty() ? data_path | 251 data_path.empty() ? data_path |
| 249 : data_path.AppendASCII(kSessionStorageDirectory), | 252 : data_path.AppendASCII(kSessionStorageDirectory), |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 | 346 |
| 344 void DOMStorageContextWrapper::OpenLocalStorage( | 347 void DOMStorageContextWrapper::OpenLocalStorage( |
| 345 const url::Origin& origin, | 348 const url::Origin& origin, |
| 346 mojom::LevelDBObserverPtr observer, | 349 mojom::LevelDBObserverPtr observer, |
| 347 mojom::LevelDBWrapperRequest request) { | 350 mojom::LevelDBWrapperRequest request) { |
| 348 mojo_state_->OpenLocalStorage( | 351 mojo_state_->OpenLocalStorage( |
| 349 origin, std::move(observer), std::move(request)); | 352 origin, std::move(observer), std::move(request)); |
| 350 } | 353 } |
| 351 | 354 |
| 352 } // namespace content | 355 } // namespace content |
| OLD | NEW |