| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "services/preferences/pref_store_manager_impl.h" | 5 #include "services/preferences/pref_store_manager_impl.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/threading/sequenced_worker_pool.h" | 10 #include "base/threading/sequenced_worker_pool.h" |
| 11 #include "components/prefs/default_pref_store.h" |
| 11 #include "components/prefs/pref_value_store.h" | 12 #include "components/prefs/pref_value_store.h" |
| 12 #include "mojo/public/cpp/bindings/interface_request.h" | 13 #include "mojo/public/cpp/bindings/interface_request.h" |
| 13 #include "services/preferences/persistent_pref_store_factory.h" | 14 #include "services/preferences/persistent_pref_store_factory.h" |
| 14 #include "services/preferences/persistent_pref_store_impl.h" | 15 #include "services/preferences/persistent_pref_store_impl.h" |
| 16 #include "services/preferences/public/cpp/pref_store_impl.h" |
| 15 #include "services/service_manager/public/cpp/interface_registry.h" | 17 #include "services/service_manager/public/cpp/interface_registry.h" |
| 16 | 18 |
| 17 namespace prefs { | 19 namespace prefs { |
| 18 namespace { | 20 namespace { |
| 19 | 21 |
| 20 using ConnectCallback = mojom::PrefStoreConnector::ConnectCallback; | 22 using ConnectCallback = mojom::PrefStoreConnector::ConnectCallback; |
| 21 using PrefStorePtrs = | 23 using PrefStorePtrs = |
| 22 std::unordered_map<PrefValueStore::PrefStoreType, mojom::PrefStorePtr>; | 24 std::unordered_map<PrefValueStore::PrefStoreType, mojom::PrefStorePtr>; |
| 23 | 25 |
| 24 // Used to make sure all pref stores have been registered before replying to any | 26 // Used to make sure all pref stores have been registered before replying to any |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 const PrefStorePtrs& pref_store_ptrs, | 63 const PrefStorePtrs& pref_store_ptrs, |
| 62 mojom::PersistentPrefStoreConnectionPtr persistent_pref_store_connection, | 64 mojom::PersistentPrefStoreConnectionPtr persistent_pref_store_connection, |
| 63 const ConnectCallback& callback) { | 65 const ConnectCallback& callback) { |
| 64 make_scoped_refptr(new ConnectionBarrier( | 66 make_scoped_refptr(new ConnectionBarrier( |
| 65 pref_store_ptrs, | 67 pref_store_ptrs, |
| 66 std::move(persistent_pref_store_connection), callback)) | 68 std::move(persistent_pref_store_connection), callback)) |
| 67 ->Init(pref_store_ptrs); | 69 ->Init(pref_store_ptrs); |
| 68 } | 70 } |
| 69 | 71 |
| 70 void ConnectionBarrier::Init(const PrefStorePtrs& pref_store_ptrs) { | 72 void ConnectionBarrier::Init(const PrefStorePtrs& pref_store_ptrs) { |
| 71 if (expected_connections_ == 0) { | |
| 72 // Degenerate case. We don't expect this, but it could happen in | |
| 73 // e.g. testing. | |
| 74 callback_.Run(std::move(persistent_pref_store_connection_), | |
| 75 std::move(connections_)); | |
| 76 return; | |
| 77 } | |
| 78 for (const auto& ptr : pref_store_ptrs) { | 73 for (const auto& ptr : pref_store_ptrs) { |
| 79 ptr.second->AddObserver( | 74 ptr.second->AddObserver( |
| 80 base::Bind(&ConnectionBarrier::OnConnect, this, ptr.first)); | 75 base::Bind(&ConnectionBarrier::OnConnect, this, ptr.first)); |
| 81 } | 76 } |
| 82 } | 77 } |
| 83 | 78 |
| 84 ConnectionBarrier::ConnectionBarrier( | 79 ConnectionBarrier::ConnectionBarrier( |
| 85 const PrefStorePtrs& pref_store_ptrs, | 80 const PrefStorePtrs& pref_store_ptrs, |
| 86 mojom::PersistentPrefStoreConnectionPtr persistent_pref_store_connection, | 81 mojom::PersistentPrefStoreConnectionPtr persistent_pref_store_connection, |
| 87 const ConnectCallback& callback) | 82 const ConnectCallback& callback) |
| (...skipping 14 matching lines...) Expand all Loading... |
| 102 } | 97 } |
| 103 } | 98 } |
| 104 | 99 |
| 105 } // namespace | 100 } // namespace |
| 106 | 101 |
| 107 PrefStoreManagerImpl::PrefStoreManagerImpl( | 102 PrefStoreManagerImpl::PrefStoreManagerImpl( |
| 108 std::set<PrefValueStore::PrefStoreType> expected_pref_stores, | 103 std::set<PrefValueStore::PrefStoreType> expected_pref_stores, |
| 109 scoped_refptr<base::SequencedWorkerPool> worker_pool) | 104 scoped_refptr<base::SequencedWorkerPool> worker_pool) |
| 110 : expected_pref_stores_(std::move(expected_pref_stores)), | 105 : expected_pref_stores_(std::move(expected_pref_stores)), |
| 111 init_binding_(this), | 106 init_binding_(this), |
| 112 worker_pool_(std::move(worker_pool)) {} | 107 defaults_(new DefaultPrefStore), |
| 108 defaults_wrapper_(base::MakeUnique<PrefStoreImpl>( |
| 109 defaults_, |
| 110 mojo::MakeRequest(&pref_store_ptrs_[PrefValueStore::DEFAULT_STORE]))), |
| 111 worker_pool_(std::move(worker_pool)) { |
| 112 expected_pref_stores_.insert(PrefValueStore::DEFAULT_STORE); |
| 113 } |
| 113 | 114 |
| 114 PrefStoreManagerImpl::~PrefStoreManagerImpl() = default; | 115 PrefStoreManagerImpl::~PrefStoreManagerImpl() = default; |
| 115 | 116 |
| 116 void PrefStoreManagerImpl::Register(PrefValueStore::PrefStoreType type, | 117 void PrefStoreManagerImpl::Register(PrefValueStore::PrefStoreType type, |
| 117 mojom::PrefStorePtr pref_store_ptr) { | 118 mojom::PrefStorePtr pref_store_ptr) { |
| 118 if (expected_pref_stores_.count(type) == 0) { | 119 if (expected_pref_stores_.count(type) == 0) { |
| 119 LOG(WARNING) << "Not registering unexpected pref store: " << type; | 120 LOG(WARNING) << "Not registering unexpected pref store: " << type; |
| 120 return; | 121 return; |
| 121 } | 122 } |
| 122 DVLOG(1) << "Registering pref store: " << type; | 123 DVLOG(1) << "Registering pref store: " << type; |
| 123 pref_store_ptr.set_connection_error_handler( | 124 pref_store_ptr.set_connection_error_handler( |
| 124 base::Bind(&PrefStoreManagerImpl::OnPrefStoreDisconnect, | 125 base::Bind(&PrefStoreManagerImpl::OnPrefStoreDisconnect, |
| 125 base::Unretained(this), type)); | 126 base::Unretained(this), type)); |
| 126 const bool success = | 127 const bool success = |
| 127 pref_store_ptrs_.insert(std::make_pair(type, std::move(pref_store_ptr))) | 128 pref_store_ptrs_.insert(std::make_pair(type, std::move(pref_store_ptr))) |
| 128 .second; | 129 .second; |
| 129 DCHECK(success) << "The same pref store registered twice: " << type; | 130 DCHECK(success) << "The same pref store registered twice: " << type; |
| 130 if (AllConnected()) { | 131 if (AllConnected()) { |
| 131 DVLOG(1) << "All pref stores registered."; | 132 DVLOG(1) << "All pref stores registered."; |
| 132 ProcessPendingConnects(); | 133 ProcessPendingConnects(); |
| 133 } | 134 } |
| 134 } | 135 } |
| 135 | 136 |
| 136 void PrefStoreManagerImpl::Connect(const ConnectCallback& callback) { | 137 void PrefStoreManagerImpl::Connect(mojom::PrefRegistryPtr pref_registry, |
| 138 const ConnectCallback& callback) { |
| 137 if (AllConnected()) { | 139 if (AllConnected()) { |
| 138 ConnectImpl(callback); | 140 ConnectImpl(std::move(pref_registry), callback); |
| 139 } else { | 141 } else { |
| 140 pending_callbacks_.push_back(callback); | 142 pending_connects_.push_back( |
| 143 std::make_pair(callback, std::move(pref_registry))); |
| 141 } | 144 } |
| 142 } | 145 } |
| 143 | 146 |
| 144 void PrefStoreManagerImpl::Create( | 147 void PrefStoreManagerImpl::Create( |
| 145 const service_manager::Identity& remote_identity, | 148 const service_manager::Identity& remote_identity, |
| 146 prefs::mojom::PrefStoreConnectorRequest request) { | 149 prefs::mojom::PrefStoreConnectorRequest request) { |
| 147 connector_bindings_.AddBinding(this, std::move(request)); | 150 connector_bindings_.AddBinding(this, std::move(request)); |
| 148 } | 151 } |
| 149 | 152 |
| 150 void PrefStoreManagerImpl::Create( | 153 void PrefStoreManagerImpl::Create( |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 DVLOG(1) << "Deregistering pref store: " << type; | 198 DVLOG(1) << "Deregistering pref store: " << type; |
| 196 pref_store_ptrs_.erase(type); | 199 pref_store_ptrs_.erase(type); |
| 197 } | 200 } |
| 198 | 201 |
| 199 bool PrefStoreManagerImpl::AllConnected() const { | 202 bool PrefStoreManagerImpl::AllConnected() const { |
| 200 return pref_store_ptrs_.size() == expected_pref_stores_.size() && | 203 return pref_store_ptrs_.size() == expected_pref_stores_.size() && |
| 201 persistent_pref_store_ && persistent_pref_store_->initialized(); | 204 persistent_pref_store_ && persistent_pref_store_->initialized(); |
| 202 } | 205 } |
| 203 | 206 |
| 204 void PrefStoreManagerImpl::ProcessPendingConnects() { | 207 void PrefStoreManagerImpl::ProcessPendingConnects() { |
| 205 for (auto& connect : pending_callbacks_) | 208 for (auto& connect : pending_connects_) |
| 206 ConnectImpl(connect); | 209 ConnectImpl(std::move(connect.second), connect.first); |
| 207 pending_callbacks_.clear(); | 210 pending_connects_.clear(); |
| 208 } | 211 } |
| 209 | 212 |
| 210 void PrefStoreManagerImpl::ConnectImpl(const ConnectCallback& callback) { | 213 void PrefStoreManagerImpl::ConnectImpl(mojom::PrefRegistryPtr pref_registry, |
| 214 const ConnectCallback& callback) { |
| 215 PersistentPrefStoreImpl::ObservedPrefs observed_prefs; |
| 216 for (auto& registration : pref_registry->registrations) { |
| 217 observed_prefs.insert(registration.first); |
| 218 const auto& key = registration.first; |
| 219 auto& default_value = registration.second->default_value; |
| 220 const base::Value* old_default = nullptr; |
| 221 // TODO(sammc): Once non-owning registrations are supported, disallow |
| 222 // multiple owners instead of just checking for consistent defaults. |
| 223 if (defaults_->GetValue(key, &old_default)) |
| 224 DCHECK(old_default->Equals(default_value.get())); |
| 225 else |
| 226 defaults_->SetDefaultValue(key, std::move(default_value)); |
| 227 } |
| 211 ConnectionBarrier::Create( | 228 ConnectionBarrier::Create( |
| 212 pref_store_ptrs_, persistent_pref_store_->CreateConnection(), callback); | 229 pref_store_ptrs_, |
| 230 persistent_pref_store_->CreateConnection(std::move(observed_prefs)), |
| 231 callback); |
| 213 } | 232 } |
| 214 | 233 |
| 215 void PrefStoreManagerImpl::OnPersistentPrefStoreReady() { | 234 void PrefStoreManagerImpl::OnPersistentPrefStoreReady() { |
| 216 DVLOG(1) << "PersistentPrefStore ready"; | 235 DVLOG(1) << "PersistentPrefStore ready"; |
| 217 if (AllConnected()) { | 236 if (AllConnected()) { |
| 218 ProcessPendingConnects(); | 237 ProcessPendingConnects(); |
| 219 } | 238 } |
| 220 } | 239 } |
| 221 | 240 |
| 222 } // namespace prefs | 241 } // namespace prefs |
| OLD | NEW |