Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: services/preferences/pref_store_manager_impl.cc

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

Powered by Google App Engine
This is Rietveld 408576698