Index: content/browser/dom_storage/dom_storage_context_wrapper.cc |
diff --git a/content/browser/dom_storage/dom_storage_context_wrapper.cc b/content/browser/dom_storage/dom_storage_context_wrapper.cc |
index d028d6faab5145e18d5ecc6bef40bfcd4c819801..7e8b9df8e5d1f04e129e9129c299c4224ba3718d 100644 |
--- a/content/browser/dom_storage/dom_storage_context_wrapper.cc |
+++ b/content/browser/dom_storage/dom_storage_context_wrapper.cc |
@@ -11,8 +11,13 @@ |
#include "base/bind_helpers.h" |
#include "base/files/file_path.h" |
#include "base/location.h" |
+#include "base/memory/weak_ptr.h" |
#include "base/single_thread_task_runner.h" |
+#include "base/strings/utf_string_conversions.h" |
#include "base/thread_task_runner_handle.h" |
+#include "components/filesystem/public/interfaces/directory.mojom.h" |
+#include "components/leveldb/public/interfaces/leveldb.mojom.h" |
+#include "components/profile_service/public/interfaces/profile.mojom.h" |
#include "content/browser/dom_storage/dom_storage_area.h" |
#include "content/browser/dom_storage/dom_storage_context_impl.h" |
#include "content/browser/dom_storage/dom_storage_task_runner.h" |
@@ -20,7 +25,9 @@ |
#include "content/browser/leveldb_wrapper_impl.h" |
#include "content/public/browser/browser_thread.h" |
#include "content/public/browser/local_storage_usage_info.h" |
+#include "content/public/browser/mojo_app_connection.h" |
#include "content/public/browser/session_storage_usage_info.h" |
+#include "mojo/common/common_type_converters.h" |
namespace content { |
namespace { |
@@ -66,9 +73,167 @@ void GetSessionStorageUsageHelper( |
} // namespace |
+// An internal class which encapsulates all the mojoy details. |
+class DOMStorageContextWrapper::MojoState { |
+ public: |
+ MojoState(const std::string& mojo_user_id, |
+ const std::string& subdirectory) |
+ : mojo_user_id_(mojo_user_id), |
+ subdirectory_(subdirectory), |
+ connection_state_(NO_CONNECTION), |
+ weak_ptr_factory_(this) {} |
+ |
+ void OpenLocalStorage( |
+ const mojo::String& origin, |
michaeln
2016/03/16 03:11:13
needs rebaselining to const url::Origin& origin
|
+ LevelDBWrapperRequest request); |
+ |
+ private: |
+ void LevelDBWrapperImplHasNoBindings(const std::string& origin) { |
+ DCHECK(level_db_wrappers_.find(origin) != level_db_wrappers_.end()); |
+ level_db_wrappers_.erase(origin); |
+ } |
+ |
+ // Part of our asynchronous directory opening called from OpenLocalStorage(). |
+ void OnDirectoryOpened(filesystem::FileError err); |
+ void OnDatabaseOpened(leveldb::DatabaseError status); |
+ |
+ // The (possibly delayed) implementation of OpenLocalStorage(). Can be called |
+ // directly from that function, or through |on_database_open_callbacks_|. |
+ void BindLocalStorage(const mojo::String& origin, |
+ LevelDBWrapperRequest request); |
+ |
+ // Used for mojo-based LocalStorage implementation (behind |
+ // --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
|
+ // LevelDB view. |
+ 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
|
+ |
+ // The underlying mojo user id. |
michaeln
2016/03/16 03:11:13
nit: comment not really needed
|
+ std::string mojo_user_id_; |
+ |
+ // The subdirectory of this dom storage context. |
michaeln
2016/03/16 03:11:13
This directory is specifically for local storage,
|
+ std::string subdirectory_; |
michaeln
2016/03/16 03:11:13
base::FilePath?
|
+ |
+ enum ConnectionState { |
+ NO_CONNECTION, |
+ CONNECTION_IN_PROGRESS, |
+ CONNECTION_FINISHED |
+ } connection_state_; |
+ |
+ scoped_ptr<MojoAppConnection> profile_app_connection_; |
+ profile::ProfileServicePtr profile_service_; |
+ filesystem::DirectoryPtr directory_; |
+ |
+ leveldb::LevelDBServicePtr leveldb_service_; |
+ leveldb::LevelDBDatabasePtr database_; |
+ |
+ std::vector<base::Closure> on_database_opened_callbacks_; |
+ |
+ base::WeakPtrFactory<MojoState> weak_ptr_factory_; |
+}; |
+ |
+void DOMStorageContextWrapper::MojoState::OpenLocalStorage( |
+ const mojo::String& origin, |
+ LevelDBWrapperRequest request) { |
+ // If we don't have a filesystem_connection_, we'll need to establish one. |
+ if (connection_state_ == NO_CONNECTION) { |
+ profile_app_connection_ = MojoAppConnection::Create( |
+ mojo_user_id_, "mojo:profile", kBrowserMojoAppUrl); |
+ profile_app_connection_->GetInterface(&profile_service_); |
+ |
+ profile_service_->GetSubDirectory( |
+ mojo::String::From(subdirectory_), |
michaeln
2016/03/16 03:11:13
when subdirectory_ is empty, as in the incognito c
|
+ GetProxy(&directory_), |
+ base::Bind(&MojoState::OnDirectoryOpened, |
+ weak_ptr_factory_.GetWeakPtr())); |
+ connection_state_ = CONNECTION_IN_PROGRESS; |
+ } |
+ |
+ if (connection_state_ == CONNECTION_IN_PROGRESS) { |
+ // Queue this OpenLocalStorage call for when we have a level db pointer. |
+ on_database_opened_callbacks_.push_back( |
+ base::Bind(&MojoState::BindLocalStorage, |
+ weak_ptr_factory_.GetWeakPtr(), |
+ origin, |
+ base::Passed(&request))); |
+ return; |
+ } |
+ |
+ BindLocalStorage(origin, std::move(request)); |
+} |
+ |
+void DOMStorageContextWrapper::MojoState::OnDirectoryOpened( |
+ filesystem::FileError err) { |
+ if (err != filesystem::FileError::OK) { |
+ // We failed to open the directory; continue with startup so that we create |
+ // the |level_db_wrappers_|. |
+ OnDatabaseOpened(leveldb::DatabaseError::IO_ERROR); |
+ return; |
+ } |
+ |
+ // Now that we have a directory, connect to the LevelDB service and get our |
+ // database. |
+ profile_app_connection_->GetInterface(&leveldb_service_); |
+ |
+ leveldb_service_->Open(std::move(directory_), "leveldb", GetProxy(&database_), |
+ base::Bind(&MojoState::OnDatabaseOpened, |
+ weak_ptr_factory_.GetWeakPtr())); |
+} |
+ |
+void DOMStorageContextWrapper::MojoState::OnDatabaseOpened( |
+ leveldb::DatabaseError status) { |
+ if (status != leveldb::DatabaseError::OK) { |
+ // If we failed to open the database, reset the service object so we pass |
+ // null pointers to our wrappers. |
+ database_.reset(); |
+ leveldb_service_.reset(); |
+ } |
+ |
+ // We no longer need the profile service; we've either transferred |
+ // |directory_| to the leveldb service, or we got a file error and no more is |
+ // possible. |
+ directory_.reset(); |
+ profile_service_.reset(); |
+ |
+ // |leveldb_| should be know to either be valid or invalid by now. Run our |
+ // delayed bindings. |
+ connection_state_ = CONNECTION_FINISHED; |
+ for (size_t i = 0; i < on_database_opened_callbacks_.size(); ++i) |
+ on_database_opened_callbacks_[i].Run(); |
+ on_database_opened_callbacks_.clear(); |
+} |
+ |
+void DOMStorageContextWrapper::MojoState::BindLocalStorage( |
+ const mojo::String& origin, |
+ LevelDBWrapperRequest request) { |
+ if (level_db_wrappers_.find(origin) == level_db_wrappers_.end()) { |
+ level_db_wrappers_[origin] = make_scoped_ptr(new LevelDBWrapperImpl( |
+ database_.get(), |
+ origin, |
+ base::Bind(&MojoState::LevelDBWrapperImplHasNoBindings, |
+ base::Unretained(this), |
+ origin.get()))); |
+ } |
+ |
+ level_db_wrappers_[origin]->Bind(std::move(request)); |
+} |
+ |
DOMStorageContextWrapper::DOMStorageContextWrapper( |
+ const std::string& mojo_user_id, |
const base::FilePath& data_path, |
+ const base::FilePath& local_partition_path, |
storage::SpecialStoragePolicy* special_storage_policy) { |
+ std::string subdirectory; |
+ if (!data_path.empty()) { |
+ subdirectory = |
+#if defined(OS_WIN) |
michaeln
2016/03/16 03:11:13
Resorting to platform specific path handling feels
|
+ base::WideToUTF8( |
+ local_partition_path.AppendASCII(kLocalStorageDirectory).value()); |
+#else |
+ local_partition_path.AppendASCII(kLocalStorageDirectory).value(); |
+#endif |
+ } |
+ mojo_state_.reset(new MojoState(mojo_user_id, subdirectory)); |
+ |
base::SequencedWorkerPool* worker_pool = BrowserThread::GetBlockingPool(); |
context_ = new DOMStorageContextImpl( |
data_path.empty() ? data_path |
@@ -84,8 +249,7 @@ DOMStorageContextWrapper::DOMStorageContextWrapper( |
.get())); |
} |
-DOMStorageContextWrapper::~DOMStorageContextWrapper() { |
-} |
+DOMStorageContextWrapper::~DOMStorageContextWrapper() {} |
void DOMStorageContextWrapper::GetLocalStorageUsage( |
const GetLocalStorageUsageCallback& callback) { |
@@ -154,6 +318,7 @@ void DOMStorageContextWrapper::SetForceKeepSessionState() { |
void DOMStorageContextWrapper::Shutdown() { |
DCHECK(context_.get()); |
+ mojo_state_.reset(); |
context_->task_runner()->PostShutdownBlockingTask( |
FROM_HERE, |
DOMStorageTaskRunner::PRIMARY_SEQUENCE, |
@@ -169,26 +334,8 @@ void DOMStorageContextWrapper::Flush() { |
void DOMStorageContextWrapper::OpenLocalStorage( |
const mojo::String& origin, |
- mojo::InterfaceRequest<LevelDBWrapper> request) { |
- if (level_db_wrappers_.find(origin) == level_db_wrappers_.end()) { |
- level_db_wrappers_[origin] = make_scoped_ptr(new LevelDBWrapperImpl( |
- origin, |
- base::Bind(&DOMStorageContextWrapper::LevelDBWrapperImplHasNoBindings, |
- base::Unretained(this), |
- origin.get()))); |
- } |
- // TODO(jam): call LevelDB service (once per this object) to open the database |
- // for LocalStorage and keep a pointer to it in this class. Then keep a map |
- // from origins to LevelDBWrapper object. Each call here for the same origin |
- // should use the same LevelDBWrapper object. |
- |
- level_db_wrappers_[origin]->Bind(std::move(request)); |
-} |
- |
-void DOMStorageContextWrapper::LevelDBWrapperImplHasNoBindings( |
- const std::string& origin) { |
- DCHECK(level_db_wrappers_.find(origin) != level_db_wrappers_.end()); |
- level_db_wrappers_.erase(origin); |
+ LevelDBWrapperRequest request) { |
+ mojo_state_->OpenLocalStorage(origin, std::move(request)); |
} |
} // namespace content |