Chromium Code Reviews| 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 <algorithm> | |
| 7 #include <utility> | 8 #include <utility> |
| 8 | 9 |
| 9 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 10 #include "base/threading/sequenced_worker_pool.h" | 11 #include "base/threading/sequenced_worker_pool.h" |
| 11 #include "components/prefs/default_pref_store.h" | 12 #include "components/prefs/default_pref_store.h" |
| 12 #include "components/prefs/pref_value_store.h" | 13 #include "components/prefs/pref_value_store.h" |
| 13 #include "mojo/public/cpp/bindings/interface_request.h" | 14 #include "mojo/public/cpp/bindings/interface_request.h" |
| 14 #include "services/preferences/persistent_pref_store_factory.h" | 15 #include "services/preferences/persistent_pref_store_factory.h" |
| 15 #include "services/preferences/persistent_pref_store_impl.h" | 16 #include "services/preferences/persistent_pref_store_impl.h" |
| 16 #include "services/preferences/public/cpp/pref_store_impl.h" | 17 #include "services/preferences/public/cpp/pref_store_impl.h" |
| 17 #include "services/service_manager/public/cpp/interface_registry.h" | 18 #include "services/service_manager/public/cpp/interface_registry.h" |
| 18 | 19 |
| 19 namespace prefs { | 20 namespace prefs { |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 using ConnectCallback = mojom::PrefStoreConnector::ConnectCallback; | 23 using ConnectCallback = mojom::PrefStoreConnector::ConnectCallback; |
| 23 using PrefStorePtrs = | 24 using PrefStorePtrs = |
| 24 std::unordered_map<PrefValueStore::PrefStoreType, mojom::PrefStorePtr>; | 25 std::unordered_map<PrefValueStore::PrefStoreType, mojom::PrefStore*>; |
| 25 | 26 |
| 26 // Used to make sure all pref stores have been registered before replying to any | 27 // Used to make sure all pref stores have been registered before replying to any |
| 27 // Connect calls. | 28 // Connect calls. |
| 28 class ConnectionBarrier : public base::RefCounted<ConnectionBarrier> { | 29 class ConnectionBarrier : public base::RefCounted<ConnectionBarrier> { |
| 29 public: | 30 public: |
| 30 static void Create( | 31 static void Create( |
| 31 const PrefStorePtrs& pref_store_ptrs, | 32 const PrefStorePtrs& pref_store_ptrs, |
| 32 mojom::PersistentPrefStoreConnectionPtr persistent_pref_store_connection, | 33 mojom::PersistentPrefStoreConnectionPtr persistent_pref_store_connection, |
| 33 const std::vector<std::string>& observed_prefs, | 34 const std::vector<std::string>& observed_prefs, |
| 34 const ConnectCallback& callback); | 35 const ConnectCallback& callback); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 107 PrefStoreManagerImpl::PrefStoreManagerImpl( | 108 PrefStoreManagerImpl::PrefStoreManagerImpl( |
| 108 std::set<PrefValueStore::PrefStoreType> expected_pref_stores, | 109 std::set<PrefValueStore::PrefStoreType> expected_pref_stores, |
| 109 scoped_refptr<base::SequencedWorkerPool> worker_pool) | 110 scoped_refptr<base::SequencedWorkerPool> worker_pool) |
| 110 : expected_pref_stores_(std::move(expected_pref_stores)), | 111 : expected_pref_stores_(std::move(expected_pref_stores)), |
| 111 init_binding_(this), | 112 init_binding_(this), |
| 112 defaults_(new DefaultPrefStore), | 113 defaults_(new DefaultPrefStore), |
| 113 defaults_wrapper_(base::MakeUnique<PrefStoreImpl>( | 114 defaults_wrapper_(base::MakeUnique<PrefStoreImpl>( |
| 114 defaults_, | 115 defaults_, |
| 115 mojo::MakeRequest(&pref_store_ptrs_[PrefValueStore::DEFAULT_STORE]))), | 116 mojo::MakeRequest(&pref_store_ptrs_[PrefValueStore::DEFAULT_STORE]))), |
| 116 worker_pool_(std::move(worker_pool)) { | 117 worker_pool_(std::move(worker_pool)) { |
| 118 expected_pref_stores_.erase(PrefValueStore::USER_STORE); | |
| 117 expected_pref_stores_.insert(PrefValueStore::DEFAULT_STORE); | 119 expected_pref_stores_.insert(PrefValueStore::DEFAULT_STORE); |
| 118 } | 120 } |
| 119 | 121 |
| 120 PrefStoreManagerImpl::~PrefStoreManagerImpl() = default; | 122 PrefStoreManagerImpl::~PrefStoreManagerImpl() = default; |
| 121 | 123 |
| 124 struct PrefStoreManagerImpl::PendingConnect { | |
| 125 mojom::PrefRegistryPtr pref_registry; | |
| 126 // Pref stores the caller already is connected to (and hence we won't | |
| 127 // connect to these). | |
| 128 std::vector<PrefValueStore::PrefStoreType> already_connected_types; | |
| 129 ConnectCallback callback; | |
| 130 }; | |
| 131 | |
| 122 void PrefStoreManagerImpl::Register(PrefValueStore::PrefStoreType type, | 132 void PrefStoreManagerImpl::Register(PrefValueStore::PrefStoreType type, |
| 123 mojom::PrefStorePtr pref_store_ptr) { | 133 mojom::PrefStorePtr pref_store_ptr) { |
| 124 if (expected_pref_stores_.count(type) == 0) { | 134 if (expected_pref_stores_.count(type) == 0) { |
| 125 LOG(WARNING) << "Not registering unexpected pref store: " << type; | 135 LOG(WARNING) << "Not registering unexpected pref store: " << type; |
| 126 return; | 136 return; |
| 127 } | 137 } |
| 128 DVLOG(1) << "Registering pref store: " << type; | 138 DVLOG(1) << "Registering pref store: " << type; |
| 129 pref_store_ptr.set_connection_error_handler( | 139 pref_store_ptr.set_connection_error_handler( |
| 130 base::Bind(&PrefStoreManagerImpl::OnPrefStoreDisconnect, | 140 base::Bind(&PrefStoreManagerImpl::OnPrefStoreDisconnect, |
| 131 base::Unretained(this), type)); | 141 base::Unretained(this), type)); |
| 132 const bool success = | 142 const bool success = |
| 133 pref_store_ptrs_.insert(std::make_pair(type, std::move(pref_store_ptr))) | 143 pref_store_ptrs_.insert(std::make_pair(type, std::move(pref_store_ptr))) |
| 134 .second; | 144 .second; |
| 135 DCHECK(success) << "The same pref store registered twice: " << type; | 145 DCHECK(success) << "The same pref store registered twice: " << type; |
| 136 if (AllConnected()) { | 146 if (AllConnected()) { |
| 137 DVLOG(1) << "All pref stores registered."; | 147 DVLOG(1) << "All pref stores registered."; |
| 138 ProcessPendingConnects(); | 148 ProcessPendingConnects(); |
| 139 } | 149 } |
| 140 } | 150 } |
| 141 | 151 |
| 142 void PrefStoreManagerImpl::Connect(mojom::PrefRegistryPtr pref_registry, | 152 void PrefStoreManagerImpl::Connect( |
| 143 const ConnectCallback& callback) { | 153 mojom::PrefRegistryPtr pref_registry, |
| 154 const std::vector<PrefValueStore::PrefStoreType>& already_connected_types, | |
| 155 const ConnectCallback& callback) { | |
| 144 if (AllConnected()) { | 156 if (AllConnected()) { |
| 145 ConnectImpl(std::move(pref_registry), callback); | 157 ConnectImpl(std::move(pref_registry), already_connected_types, callback); |
| 146 } else { | 158 } else { |
| 147 pending_connects_.push_back( | 159 pending_connects_.push_back( |
| 148 std::make_pair(callback, std::move(pref_registry))); | 160 {std::move(pref_registry), already_connected_types, callback}); |
| 149 } | 161 } |
| 150 } | 162 } |
| 151 | 163 |
| 152 void PrefStoreManagerImpl::Create( | 164 void PrefStoreManagerImpl::Create( |
| 153 const service_manager::Identity& remote_identity, | 165 const service_manager::Identity& remote_identity, |
| 154 prefs::mojom::PrefStoreConnectorRequest request) { | 166 prefs::mojom::PrefStoreConnectorRequest request) { |
| 155 connector_bindings_.AddBinding(this, std::move(request)); | 167 connector_bindings_.AddBinding(this, std::move(request)); |
| 156 } | 168 } |
| 157 | 169 |
| 158 void PrefStoreManagerImpl::Create( | 170 void PrefStoreManagerImpl::Create( |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 204 pref_store_ptrs_.erase(type); | 216 pref_store_ptrs_.erase(type); |
| 205 } | 217 } |
| 206 | 218 |
| 207 bool PrefStoreManagerImpl::AllConnected() const { | 219 bool PrefStoreManagerImpl::AllConnected() const { |
| 208 return pref_store_ptrs_.size() == expected_pref_stores_.size() && | 220 return pref_store_ptrs_.size() == expected_pref_stores_.size() && |
| 209 persistent_pref_store_ && persistent_pref_store_->initialized(); | 221 persistent_pref_store_ && persistent_pref_store_->initialized(); |
| 210 } | 222 } |
| 211 | 223 |
| 212 void PrefStoreManagerImpl::ProcessPendingConnects() { | 224 void PrefStoreManagerImpl::ProcessPendingConnects() { |
| 213 for (auto& connect : pending_connects_) | 225 for (auto& connect : pending_connects_) |
| 214 ConnectImpl(std::move(connect.second), connect.first); | 226 ConnectImpl(std::move(connect.pref_registry), |
| 227 connect.already_connected_types, connect.callback); | |
| 215 pending_connects_.clear(); | 228 pending_connects_.clear(); |
| 216 } | 229 } |
| 217 | 230 |
| 218 void PrefStoreManagerImpl::ConnectImpl(mojom::PrefRegistryPtr pref_registry, | 231 void PrefStoreManagerImpl::ConnectImpl( |
| 219 const ConnectCallback& callback) { | 232 mojom::PrefRegistryPtr pref_registry, |
| 233 const std::vector<PrefValueStore::PrefStoreType>& already_connected_types, | |
| 234 const ConnectCallback& callback) { | |
| 220 std::vector<std::string> observed_prefs; | 235 std::vector<std::string> observed_prefs; |
| 221 for (auto& registration : pref_registry->registrations) { | 236 for (auto& registration : pref_registry->registrations) { |
| 222 observed_prefs.push_back(registration.first); | 237 observed_prefs.push_back(registration.first); |
| 223 const auto& key = registration.first; | 238 const auto& key = registration.first; |
| 224 auto& default_value = registration.second->default_value; | 239 auto& default_value = registration.second->default_value; |
| 225 const base::Value* old_default = nullptr; | 240 const base::Value* old_default = nullptr; |
| 226 // TODO(sammc): Once non-owning registrations are supported, disallow | 241 // TODO(sammc): Once non-owning registrations are supported, disallow |
| 227 // multiple owners instead of just checking for consistent defaults. | 242 // multiple owners instead of just checking for consistent defaults. |
| 228 if (defaults_->GetValue(key, &old_default)) | 243 if (defaults_->GetValue(key, &old_default)) |
| 229 DCHECK(old_default->Equals(default_value.get())); | 244 DCHECK(old_default->Equals(default_value.get())); |
| 230 else | 245 else |
| 231 defaults_->SetDefaultValue(key, std::move(default_value)); | 246 defaults_->SetDefaultValue(key, std::move(default_value)); |
| 232 } | 247 } |
| 248 | |
| 249 // Only connect to pref stores the client isn't already connected to. | |
| 250 PrefStorePtrs ptrs; | |
| 251 for (const auto& entry : pref_store_ptrs_) { | |
| 252 const bool already_connected = | |
| 253 std::find(already_connected_types.begin(), | |
|
Bernhard Bauer
2017/04/05 10:04:32
Nit: base::ContainsValue() (from base/stl_util.h)
tibell
2017/04/06 01:05:52
Done.
| |
| 254 already_connected_types.end(), | |
| 255 entry.first) != already_connected_types.end(); | |
| 256 if (!already_connected) { | |
| 257 ptrs.insert(std::make_pair(entry.first, entry.second.get())); | |
| 258 } | |
| 259 } | |
| 233 ConnectionBarrier::Create( | 260 ConnectionBarrier::Create( |
| 234 pref_store_ptrs_, | 261 ptrs, |
| 235 persistent_pref_store_->CreateConnection( | 262 persistent_pref_store_->CreateConnection( |
| 236 PersistentPrefStoreImpl::ObservedPrefs(observed_prefs.begin(), | 263 PersistentPrefStoreImpl::ObservedPrefs(observed_prefs.begin(), |
| 237 observed_prefs.end())), | 264 observed_prefs.end())), |
| 238 observed_prefs, callback); | 265 observed_prefs, callback); |
| 239 } | 266 } |
| 240 | 267 |
| 241 void PrefStoreManagerImpl::OnPersistentPrefStoreReady() { | 268 void PrefStoreManagerImpl::OnPersistentPrefStoreReady() { |
| 242 DVLOG(1) << "PersistentPrefStore ready"; | 269 DVLOG(1) << "PersistentPrefStore ready"; |
| 243 if (AllConnected()) { | 270 if (AllConnected()) { |
| 244 ProcessPendingConnects(); | 271 ProcessPendingConnects(); |
| 245 } | 272 } |
| 246 } | 273 } |
| 247 | 274 |
| 248 } // namespace prefs | 275 } // namespace prefs |
| OLD | NEW |