| 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" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 using PrefStorePtrs = | 23 using PrefStorePtrs = |
| 24 std::unordered_map<PrefValueStore::PrefStoreType, mojom::PrefStorePtr>; | 24 std::unordered_map<PrefValueStore::PrefStoreType, mojom::PrefStorePtr>; |
| 25 | 25 |
| 26 // 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 |
| 27 // Connect calls. | 27 // Connect calls. |
| 28 class ConnectionBarrier : public base::RefCounted<ConnectionBarrier> { | 28 class ConnectionBarrier : public base::RefCounted<ConnectionBarrier> { |
| 29 public: | 29 public: |
| 30 static void Create( | 30 static void Create( |
| 31 const PrefStorePtrs& pref_store_ptrs, | 31 const PrefStorePtrs& pref_store_ptrs, |
| 32 mojom::PersistentPrefStoreConnectionPtr persistent_pref_store_connection, | 32 mojom::PersistentPrefStoreConnectionPtr persistent_pref_store_connection, |
| 33 const std::vector<std::string>& observed_prefs, |
| 33 const ConnectCallback& callback); | 34 const ConnectCallback& callback); |
| 34 | 35 |
| 35 void Init(const PrefStorePtrs& pref_store_ptrs); | 36 void Init(const PrefStorePtrs& pref_store_ptrs, |
| 37 const std::vector<std::string>& observed_prefs); |
| 36 | 38 |
| 37 private: | 39 private: |
| 38 friend class base::RefCounted<ConnectionBarrier>; | 40 friend class base::RefCounted<ConnectionBarrier>; |
| 39 ConnectionBarrier( | 41 ConnectionBarrier( |
| 40 const PrefStorePtrs& pref_store_ptrs, | 42 const PrefStorePtrs& pref_store_ptrs, |
| 41 mojom::PersistentPrefStoreConnectionPtr persistent_pref_store_connection, | 43 mojom::PersistentPrefStoreConnectionPtr persistent_pref_store_connection, |
| 42 const ConnectCallback& callback); | 44 const ConnectCallback& callback); |
| 43 ~ConnectionBarrier() = default; | 45 ~ConnectionBarrier() = default; |
| 44 | 46 |
| 45 void OnConnect(PrefValueStore::PrefStoreType type, | 47 void OnConnect(PrefValueStore::PrefStoreType type, |
| 46 mojom::PrefStoreConnectionPtr connection_ptr); | 48 mojom::PrefStoreConnectionPtr connection_ptr); |
| 47 | 49 |
| 48 ConnectCallback callback_; | 50 ConnectCallback callback_; |
| 49 | 51 |
| 50 std::unordered_map<PrefValueStore::PrefStoreType, | 52 std::unordered_map<PrefValueStore::PrefStoreType, |
| 51 mojom::PrefStoreConnectionPtr> | 53 mojom::PrefStoreConnectionPtr> |
| 52 connections_; | 54 connections_; |
| 53 | 55 |
| 54 const size_t expected_connections_; | 56 const size_t expected_connections_; |
| 55 | 57 |
| 56 mojom::PersistentPrefStoreConnectionPtr persistent_pref_store_connection_; | 58 mojom::PersistentPrefStoreConnectionPtr persistent_pref_store_connection_; |
| 57 | 59 |
| 58 DISALLOW_COPY_AND_ASSIGN(ConnectionBarrier); | 60 DISALLOW_COPY_AND_ASSIGN(ConnectionBarrier); |
| 59 }; | 61 }; |
| 60 | 62 |
| 61 // static | 63 // static |
| 62 void ConnectionBarrier::Create( | 64 void ConnectionBarrier::Create( |
| 63 const PrefStorePtrs& pref_store_ptrs, | 65 const PrefStorePtrs& pref_store_ptrs, |
| 64 mojom::PersistentPrefStoreConnectionPtr persistent_pref_store_connection, | 66 mojom::PersistentPrefStoreConnectionPtr persistent_pref_store_connection, |
| 67 const std::vector<std::string>& observed_prefs, |
| 65 const ConnectCallback& callback) { | 68 const ConnectCallback& callback) { |
| 66 make_scoped_refptr(new ConnectionBarrier( | 69 make_scoped_refptr(new ConnectionBarrier( |
| 67 pref_store_ptrs, | 70 pref_store_ptrs, |
| 68 std::move(persistent_pref_store_connection), callback)) | 71 std::move(persistent_pref_store_connection), callback)) |
| 69 ->Init(pref_store_ptrs); | 72 ->Init(pref_store_ptrs, observed_prefs); |
| 70 } | 73 } |
| 71 | 74 |
| 72 void ConnectionBarrier::Init(const PrefStorePtrs& pref_store_ptrs) { | 75 void ConnectionBarrier::Init(const PrefStorePtrs& pref_store_ptrs, |
| 76 const std::vector<std::string>& observed_prefs) { |
| 73 for (const auto& ptr : pref_store_ptrs) { | 77 for (const auto& ptr : pref_store_ptrs) { |
| 74 ptr.second->AddObserver( | 78 ptr.second->AddObserver( |
| 79 observed_prefs, |
| 75 base::Bind(&ConnectionBarrier::OnConnect, this, ptr.first)); | 80 base::Bind(&ConnectionBarrier::OnConnect, this, ptr.first)); |
| 76 } | 81 } |
| 77 } | 82 } |
| 78 | 83 |
| 79 ConnectionBarrier::ConnectionBarrier( | 84 ConnectionBarrier::ConnectionBarrier( |
| 80 const PrefStorePtrs& pref_store_ptrs, | 85 const PrefStorePtrs& pref_store_ptrs, |
| 81 mojom::PersistentPrefStoreConnectionPtr persistent_pref_store_connection, | 86 mojom::PersistentPrefStoreConnectionPtr persistent_pref_store_connection, |
| 82 const ConnectCallback& callback) | 87 const ConnectCallback& callback) |
| 83 : callback_(callback), | 88 : callback_(callback), |
| 84 expected_connections_(pref_store_ptrs.size()), | 89 expected_connections_(pref_store_ptrs.size()), |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 } | 210 } |
| 206 | 211 |
| 207 void PrefStoreManagerImpl::ProcessPendingConnects() { | 212 void PrefStoreManagerImpl::ProcessPendingConnects() { |
| 208 for (auto& connect : pending_connects_) | 213 for (auto& connect : pending_connects_) |
| 209 ConnectImpl(std::move(connect.second), connect.first); | 214 ConnectImpl(std::move(connect.second), connect.first); |
| 210 pending_connects_.clear(); | 215 pending_connects_.clear(); |
| 211 } | 216 } |
| 212 | 217 |
| 213 void PrefStoreManagerImpl::ConnectImpl(mojom::PrefRegistryPtr pref_registry, | 218 void PrefStoreManagerImpl::ConnectImpl(mojom::PrefRegistryPtr pref_registry, |
| 214 const ConnectCallback& callback) { | 219 const ConnectCallback& callback) { |
| 215 PersistentPrefStoreImpl::ObservedPrefs observed_prefs; | 220 std::vector<std::string> observed_prefs; |
| 216 for (auto& registration : pref_registry->registrations) { | 221 for (auto& registration : pref_registry->registrations) { |
| 217 observed_prefs.insert(registration.first); | 222 observed_prefs.push_back(registration.first); |
| 218 const auto& key = registration.first; | 223 const auto& key = registration.first; |
| 219 auto& default_value = registration.second->default_value; | 224 auto& default_value = registration.second->default_value; |
| 220 const base::Value* old_default = nullptr; | 225 const base::Value* old_default = nullptr; |
| 221 // TODO(sammc): Once non-owning registrations are supported, disallow | 226 // TODO(sammc): Once non-owning registrations are supported, disallow |
| 222 // multiple owners instead of just checking for consistent defaults. | 227 // multiple owners instead of just checking for consistent defaults. |
| 223 if (defaults_->GetValue(key, &old_default)) | 228 if (defaults_->GetValue(key, &old_default)) |
| 224 DCHECK(old_default->Equals(default_value.get())); | 229 DCHECK(old_default->Equals(default_value.get())); |
| 225 else | 230 else |
| 226 defaults_->SetDefaultValue(key, std::move(default_value)); | 231 defaults_->SetDefaultValue(key, std::move(default_value)); |
| 227 } | 232 } |
| 228 ConnectionBarrier::Create( | 233 ConnectionBarrier::Create( |
| 229 pref_store_ptrs_, | 234 pref_store_ptrs_, |
| 230 persistent_pref_store_->CreateConnection(std::move(observed_prefs)), | 235 persistent_pref_store_->CreateConnection( |
| 231 callback); | 236 PersistentPrefStoreImpl::ObservedPrefs(observed_prefs.begin(), |
| 237 observed_prefs.end())), |
| 238 observed_prefs, callback); |
| 232 } | 239 } |
| 233 | 240 |
| 234 void PrefStoreManagerImpl::OnPersistentPrefStoreReady() { | 241 void PrefStoreManagerImpl::OnPersistentPrefStoreReady() { |
| 235 DVLOG(1) << "PersistentPrefStore ready"; | 242 DVLOG(1) << "PersistentPrefStore ready"; |
| 236 if (AllConnected()) { | 243 if (AllConnected()) { |
| 237 ProcessPendingConnects(); | 244 ProcessPendingConnects(); |
| 238 } | 245 } |
| 239 } | 246 } |
| 240 | 247 |
| 241 } // namespace prefs | 248 } // namespace prefs |
| OLD | NEW |