| 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 |
| 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); |
| 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 scoped_refptr<base::SingleThreadTaskRunner> profile_service_runner, |
| 73 scoped_refptr<base::SingleThreadTaskRunner> leveldb_service_runner) { |
| 74 return make_scoped_ptr(new ProfileApp( |
| 75 std::move(profile_service_runner), |
| 76 std::move(leveldb_service_runner))); |
| 23 } | 77 } |
| 24 | 78 |
| 25 ProfileApp::ProfileApp() | 79 ProfileApp::ProfileApp( |
| 26 : lock_table_(new filesystem::LockTable) { | 80 scoped_refptr<base::SingleThreadTaskRunner> profile_service_runner, |
| 27 } | 81 scoped_refptr<base::SingleThreadTaskRunner> leveldb_service_runner) |
| 82 : profile_service_runner_(std::move(profile_service_runner)), |
| 83 leveldb_service_runner_(std::move(leveldb_service_runner)) {} |
| 28 | 84 |
| 29 ProfileApp::~ProfileApp() {} | 85 ProfileApp::~ProfileApp() { |
| 30 | 86 profile_service_runner_->DeleteSoon(FROM_HERE, profile_objects_.release()); |
| 31 // static | 87 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 } | 88 } |
| 37 | 89 |
| 38 void ProfileApp::Initialize(mojo::Connector* connector, | 90 void ProfileApp::Initialize(mojo::Connector* connector, |
| 39 const mojo::Identity& identity, | 91 const mojo::Identity& identity, |
| 40 uint32_t id) { | 92 uint32_t id) { |
| 41 tracing_.Initialize(connector, identity.name()); | 93 tracing_.Initialize(connector, identity.name()); |
| 42 leveldb_service_.reset(new leveldb::LevelDBServiceImpl); | 94 profile_objects_.reset(new ProfileApp::ProfileServiceObjects( |
| 43 | 95 GetProfileDirForUserID(identity.user_id()))); |
| 44 auto it = g_user_id_to_data_dir.Get().find(identity.user_id()); | 96 leveldb_objects_.reset(new ProfileApp::LevelDBServiceObjects); |
| 45 DCHECK(it != g_user_id_to_data_dir.Get().end()); | |
| 46 profile_data_dir_ = it->second; | |
| 47 } | 97 } |
| 48 | 98 |
| 49 bool ProfileApp::AcceptConnection(mojo::Connection* connection) { | 99 bool ProfileApp::AcceptConnection(mojo::Connection* connection) { |
| 50 connection->AddInterface<leveldb::LevelDBService>(this); | 100 connection->AddInterface<leveldb::LevelDBService>(this); |
| 51 connection->AddInterface<ProfileService>(this); | 101 connection->AddInterface<ProfileService>(this); |
| 52 return true; | 102 return true; |
| 53 } | 103 } |
| 54 | 104 |
| 55 void ProfileApp::Create(mojo::Connection* connection, | 105 void ProfileApp::Create(mojo::Connection* connection, |
| 56 ProfileServiceRequest request) { | 106 ProfileServiceRequest request) { |
| 57 // No, we need one of these per connection. | 107 profile_service_runner_->PostTask( |
| 58 new ProfileServiceImpl(connection, | 108 FROM_HERE, |
| 59 std::move(request), | 109 base::Bind(&ProfileApp::ProfileServiceObjects::OnProfileServiceRequest, |
| 60 profile_data_dir_, | 110 profile_objects_->AsWeakPtr(), connection, |
| 61 lock_table_.get()); | 111 base::Passed(&request))); |
| 62 } | 112 } |
| 63 | 113 |
| 64 void ProfileApp::Create(mojo::Connection* connection, | 114 void ProfileApp::Create(mojo::Connection* connection, |
| 65 leveldb::LevelDBServiceRequest request) { | 115 leveldb::LevelDBServiceRequest request) { |
| 66 leveldb_bindings_.AddBinding(leveldb_service_.get(), std::move(request)); | 116 leveldb_service_runner_->PostTask( |
| 117 FROM_HERE, |
| 118 base::Bind(&ProfileApp::LevelDBServiceObjects::OnLevelDBServiceRequest, |
| 119 leveldb_objects_->AsWeakPtr(), connection, |
| 120 base::Passed(&request))); |
| 67 } | 121 } |
| 68 | 122 |
| 69 } // namespace profile | 123 } // namespace profile |
| OLD | NEW |