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

Unified Diff: components/profile_service/profile_app.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: merge tot 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 side-by-side diff with in-line comments
Download patch
Index: components/profile_service/profile_app.cc
diff --git a/components/profile_service/profile_app.cc b/components/profile_service/profile_app.cc
index 27491db5e910d27946721e9031860a493591c71b..a58071163cb47e5c64029ae5f00dda1f43712d29 100644
--- a/components/profile_service/profile_app.cc
+++ b/components/profile_service/profile_app.cc
@@ -4,46 +4,96 @@
#include "components/profile_service/profile_app.h"
-#include "base/lazy_instance.h"
+#include "base/bind.h"
+#include "base/memory/weak_ptr.h"
+#include "components/filesystem/lock_table.h"
#include "components/leveldb/leveldb_service_impl.h"
#include "components/profile_service/profile_service_impl.h"
+#include "components/profile_service/user_id_map.h"
+#include "mojo/public/cpp/bindings/callback.h"
#include "mojo/shell/public/cpp/connection.h"
namespace profile {
-namespace {
-
-base::LazyInstance<std::map<std::string, base::FilePath>>
- g_user_id_to_data_dir = LAZY_INSTANCE_INITIALIZER;
-
-} // namespace
-
-scoped_ptr<mojo::ShellClient> CreateProfileApp() {
- return make_scoped_ptr(new ProfileApp);
+class ProfileApp::ProfileServiceObjects
+ : public base::SupportsWeakPtr<ProfileServiceObjects> {
+ public:
+ // Created on the main thread.
+ ProfileServiceObjects(base::FilePath profile_data_dir)
+ : profile_data_dir_(profile_data_dir) {}
+
+ // Destroyed on the |profile_service_runner_|.
+ ~ProfileServiceObjects() {}
+
+ // Called on the |profile_service_runner_|.
+ void OnProfileServiceRequest(mojo::Connection* connection,
+ ProfileServiceRequest request) {
+ if (!lock_table_)
+ lock_table_ = new filesystem::LockTable;
+ profile_service_bindings_.AddBinding(
+ new ProfileServiceImpl(profile_data_dir_, lock_table_),
+ std::move(request));
+ }
+
+ private:
+ mojo::BindingSet<ProfileService> profile_service_bindings_;
+ scoped_refptr<filesystem::LockTable> lock_table_;
+ base::FilePath profile_data_dir_;
+
+ DISALLOW_COPY_AND_ASSIGN(ProfileServiceObjects);
+};
+
+class ProfileApp::LevelDBServiceObjects
+ : public base::SupportsWeakPtr<LevelDBServiceObjects> {
+ public:
+ // Created on the main thread.
+ LevelDBServiceObjects() {}
+
+ // Destroyed on the |leveldb_service_runner_|.
+ ~LevelDBServiceObjects() {}
+
+ // Called on the |leveldb_service_runner_|.
+ void OnLevelDBServiceRequest(mojo::Connection* connection,
+ leveldb::LevelDBServiceRequest request) {
+ if (!leveldb_service_)
+ leveldb_service_.reset(new leveldb::LevelDBServiceImpl);
+ leveldb_bindings_.AddBinding(leveldb_service_.get(), std::move(request));
+ }
+
+ private:
+ // Variables that are only accessible on the |leveldb_service_runner_| thread.
+ scoped_ptr<leveldb::LevelDBService> leveldb_service_;
+ mojo::BindingSet<leveldb::LevelDBService> leveldb_bindings_;
+
+ DISALLOW_COPY_AND_ASSIGN(LevelDBServiceObjects);
+};
+
+scoped_ptr<mojo::ShellClient> CreateProfileApp(
+ scoped_refptr<base::SingleThreadTaskRunner> profile_service_runner,
+ scoped_refptr<base::SingleThreadTaskRunner> leveldb_service_runner) {
+ return make_scoped_ptr(new ProfileApp(
+ std::move(profile_service_runner),
+ std::move(leveldb_service_runner)));
}
-ProfileApp::ProfileApp()
- : lock_table_(new filesystem::LockTable) {
-}
-
-ProfileApp::~ProfileApp() {}
+ProfileApp::ProfileApp(
+ scoped_refptr<base::SingleThreadTaskRunner> profile_service_runner,
+ scoped_refptr<base::SingleThreadTaskRunner> leveldb_service_runner)
+ : profile_service_runner_(std::move(profile_service_runner)),
+ leveldb_service_runner_(std::move(leveldb_service_runner)) {}
-// static
-void ProfileApp::AssociateMojoUserIDWithProfileDir(
- const std::string& user_id,
- const base::FilePath& profile_data_dir) {
- g_user_id_to_data_dir.Get()[user_id] = profile_data_dir;
+ProfileApp::~ProfileApp() {
+ profile_service_runner_->DeleteSoon(FROM_HERE, profile_objects_.release());
+ leveldb_service_runner_->DeleteSoon(FROM_HERE, leveldb_objects_.release());
}
void ProfileApp::Initialize(mojo::Connector* connector,
const mojo::Identity& identity,
uint32_t id) {
tracing_.Initialize(connector, identity.name());
- leveldb_service_.reset(new leveldb::LevelDBServiceImpl);
-
- auto it = g_user_id_to_data_dir.Get().find(identity.user_id());
- DCHECK(it != g_user_id_to_data_dir.Get().end());
- profile_data_dir_ = it->second;
+ profile_objects_.reset(new ProfileApp::ProfileServiceObjects(
+ GetProfileDirForUserID(identity.user_id())));
+ leveldb_objects_.reset(new ProfileApp::LevelDBServiceObjects);
}
bool ProfileApp::AcceptConnection(mojo::Connection* connection) {
@@ -54,16 +104,20 @@ bool ProfileApp::AcceptConnection(mojo::Connection* connection) {
void ProfileApp::Create(mojo::Connection* connection,
ProfileServiceRequest request) {
- // No, we need one of these per connection.
- new ProfileServiceImpl(connection,
- std::move(request),
- profile_data_dir_,
- lock_table_.get());
+ profile_service_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(&ProfileApp::ProfileServiceObjects::OnProfileServiceRequest,
+ profile_objects_->AsWeakPtr(), connection,
+ base::Passed(&request)));
}
void ProfileApp::Create(mojo::Connection* connection,
leveldb::LevelDBServiceRequest request) {
- leveldb_bindings_.AddBinding(leveldb_service_.get(), std::move(request));
+ leveldb_service_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(&ProfileApp::LevelDBServiceObjects::OnLevelDBServiceRequest,
+ leveldb_objects_->AsWeakPtr(), connection,
+ base::Passed(&request)));
}
} // namespace profile

Powered by Google App Engine
This is Rietveld 408576698