Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/profile_service/profile_app.h" | 5 #include "components/profile_service/profile_app.h" |
| 6 | 6 |
| 7 #include "base/lazy_instance.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/weak_ptr.h" | |
| 9 #include "components/filesystem/lock_table.h" | |
| 8 #include "components/leveldb/leveldb_service_impl.h" | 10 #include "components/leveldb/leveldb_service_impl.h" |
| 9 #include "components/profile_service/profile_service_impl.h" | 11 #include "components/profile_service/profile_service_impl.h" |
| 12 #include "components/profile_service/user_id_map.h" | |
| 13 #include "mojo/public/cpp/bindings/callback.h" | |
| 10 #include "mojo/shell/public/cpp/connection.h" | 14 #include "mojo/shell/public/cpp/connection.h" |
| 11 | 15 |
| 12 namespace profile { | 16 namespace profile { |
| 13 | 17 |
| 14 namespace { | 18 class ProfileApp::ProfileServiceObjects |
| 19 : public base::SupportsWeakPtr<ProfileServiceObjects> { | |
| 20 public: | |
| 21 // Created on the main thread. | |
| 22 ProfileServiceObjects(base::FilePath profile_data_dir) | |
| 23 : profile_data_dir_(profile_data_dir) {} | |
| 15 | 24 |
| 16 base::LazyInstance<std::map<std::string, base::FilePath>> | 25 // Destroyed on the |profile_service_runner_|. |
| 17 g_user_id_to_data_dir = LAZY_INSTANCE_INITIALIZER; | 26 ~ProfileServiceObjects() {} |
| 18 | 27 |
| 19 } // namespace | 28 // Called on the |profile_service_runner_|. |
| 29 void OnProfileServiceRequest(mojo::Connection* connection, | |
| 30 ProfileServiceRequest request) { | |
| 31 if (!lock_table_) | |
| 32 lock_table_ = new filesystem::LockTable; | |
| 33 profile_service_bindings_.AddBinding( | |
| 34 new ProfileServiceImpl(profile_data_dir_, lock_table_), | |
| 35 std::move(request)); | |
| 36 } | |
| 20 | 37 |
| 21 scoped_ptr<mojo::ShellClient> CreateProfileApp() { | 38 private: |
| 22 return make_scoped_ptr(new ProfileApp); | 39 mojo::BindingSet<ProfileService> profile_service_bindings_; |
| 40 scoped_refptr<filesystem::LockTable> lock_table_; | |
| 41 base::FilePath profile_data_dir_; | |
| 42 | |
| 43 DISALLOW_COPY_AND_ASSIGN(ProfileServiceObjects); | |
| 44 }; | |
| 45 | |
| 46 class ProfileApp::LevelDBServiceObjects | |
|
michaeln
2016/03/17 22:35:41
nice arrangement for thread safety!
| |
| 47 : public base::SupportsWeakPtr<LevelDBServiceObjects> { | |
| 48 public: | |
| 49 // Created on the main thread. | |
| 50 LevelDBServiceObjects() {} | |
| 51 | |
| 52 // Destroyed on the |leveldb_service_runner_|. | |
| 53 ~LevelDBServiceObjects() {} | |
| 54 | |
| 55 // Called on the |leveldb_service_runner_|. | |
| 56 void OnLevelDBServiceRequest(mojo::Connection* connection, | |
| 57 leveldb::LevelDBServiceRequest request) { | |
| 58 if (!leveldb_service_) | |
| 59 leveldb_service_.reset(new leveldb::LevelDBServiceImpl); | |
|
michaeln
2016/03/17 22:35:41
does this direct instantiation mean we're not usin
Elliot Glaysher
2016/03/18 19:41:12
Correct. Apps don't contain other apps. Apps conta
| |
| 60 leveldb_bindings_.AddBinding(leveldb_service_.get(), std::move(request)); | |
| 61 } | |
| 62 | |
| 63 private: | |
| 64 // Variables that are only accessible on the |leveldb_service_runner_| thread. | |
| 65 scoped_ptr<leveldb::LevelDBService> leveldb_service_; | |
| 66 mojo::BindingSet<leveldb::LevelDBService> leveldb_bindings_; | |
| 67 | |
| 68 DISALLOW_COPY_AND_ASSIGN(LevelDBServiceObjects); | |
| 69 }; | |
| 70 | |
| 71 scoped_ptr<mojo::ShellClient> CreateProfileApp( | |
| 72 const scoped_refptr<base::SingleThreadTaskRunner>& profile_service_runner, | |
| 73 const scoped_refptr<base::SingleThreadTaskRunner>& leveldb_service_runner) { | |
| 74 return make_scoped_ptr( | |
| 75 new ProfileApp(profile_service_runner, leveldb_service_runner)); | |
| 23 } | 76 } |
| 24 | 77 |
| 25 ProfileApp::ProfileApp() | 78 ProfileApp::ProfileApp( |
| 26 : lock_table_(new filesystem::LockTable) { | 79 const scoped_refptr<base::SingleThreadTaskRunner>& profile_service_runner, |
| 27 } | 80 const scoped_refptr<base::SingleThreadTaskRunner>& leveldb_service_runner) |
| 81 : profile_service_runner_(profile_service_runner), | |
| 82 leveldb_service_runner_(leveldb_service_runner) {} | |
| 28 | 83 |
| 29 ProfileApp::~ProfileApp() {} | 84 ProfileApp::~ProfileApp() { |
| 30 | 85 profile_service_runner_->DeleteSoon(FROM_HERE, profile_objects_.release()); |
| 31 // static | 86 leveldb_service_runner_->DeleteSoon(FROM_HERE, leveldb_objects_.release()); |
| 32 void ProfileApp::AssociateMojoUserIDWithProfileDir( | |
| 33 const std::string& user_id, | |
| 34 const base::FilePath& profile_data_dir) { | |
| 35 g_user_id_to_data_dir.Get()[user_id] = profile_data_dir; | |
| 36 } | 87 } |
| 37 | 88 |
| 38 void ProfileApp::Initialize(mojo::Connector* connector, | 89 void ProfileApp::Initialize(mojo::Connector* connector, |
| 39 const mojo::Identity& identity, | 90 const mojo::Identity& identity, |
| 40 uint32_t id) { | 91 uint32_t id) { |
| 41 tracing_.Initialize(connector, identity.name()); | 92 tracing_.Initialize(connector, identity.name()); |
| 42 leveldb_service_.reset(new leveldb::LevelDBServiceImpl); | 93 profile_objects_.reset(new ProfileApp::ProfileServiceObjects( |
| 43 | 94 GetProfileDirForUserID(identity.user_id()))); |
| 44 auto it = g_user_id_to_data_dir.Get().find(identity.user_id()); | 95 leveldb_objects_.reset(new ProfileApp::LevelDBServiceObjects); |
| 45 DCHECK(it != g_user_id_to_data_dir.Get().end()); | |
| 46 profile_data_dir_ = it->second; | |
| 47 } | 96 } |
| 48 | 97 |
| 49 bool ProfileApp::AcceptConnection(mojo::Connection* connection) { | 98 bool ProfileApp::AcceptConnection(mojo::Connection* connection) { |
| 50 connection->AddInterface<leveldb::LevelDBService>(this); | 99 connection->AddInterface<leveldb::LevelDBService>(this); |
| 51 connection->AddInterface<ProfileService>(this); | 100 connection->AddInterface<ProfileService>(this); |
| 52 return true; | 101 return true; |
| 53 } | 102 } |
| 54 | 103 |
| 55 void ProfileApp::Create(mojo::Connection* connection, | 104 void ProfileApp::Create(mojo::Connection* connection, |
| 56 ProfileServiceRequest request) { | 105 ProfileServiceRequest request) { |
| 57 // No, we need one of these per connection. | 106 profile_service_runner_->PostTask( |
| 58 new ProfileServiceImpl(connection, | 107 FROM_HERE, |
| 59 std::move(request), | 108 base::Bind(&ProfileApp::ProfileServiceObjects::OnProfileServiceRequest, |
| 60 profile_data_dir_, | 109 profile_objects_->AsWeakPtr(), connection, |
| 61 lock_table_.get()); | 110 base::Passed(&request))); |
| 62 } | 111 } |
| 63 | 112 |
| 64 void ProfileApp::Create(mojo::Connection* connection, | 113 void ProfileApp::Create(mojo::Connection* connection, |
| 65 leveldb::LevelDBServiceRequest request) { | 114 leveldb::LevelDBServiceRequest request) { |
| 66 leveldb_bindings_.AddBinding(leveldb_service_.get(), std::move(request)); | 115 leveldb_service_runner_->PostTask( |
| 116 FROM_HERE, | |
| 117 base::Bind(&ProfileApp::LevelDBServiceObjects::OnLevelDBServiceRequest, | |
| 118 leveldb_objects_->AsWeakPtr(), connection, | |
| 119 base::Passed(&request))); | |
| 67 } | 120 } |
| 68 | 121 |
| 69 } // namespace profile | 122 } // namespace profile |
| OLD | NEW |