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

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: 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"
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 = std::unordered_map<PrefValueStore::PrefStoreType,
24 std::unordered_map<PrefValueStore::PrefStoreType, mojom::PrefStorePtr>; 25 const mojom::PrefStorePtr&>;
Sam McNally 2017/04/03 10:09:31 mojom::PrefStore*
tibell 2017/04/04 04:05:59 Done.
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 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
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)) {
117 expected_pref_stores_.insert(PrefValueStore::DEFAULT_STORE); 118 expected_pref_stores_.insert(PrefValueStore::DEFAULT_STORE);
118 } 119 }
119 120
120 PrefStoreManagerImpl::~PrefStoreManagerImpl() = default; 121 PrefStoreManagerImpl::~PrefStoreManagerImpl() = default;
121 122
123 PrefStoreManagerImpl::PendingConnect::PendingConnect(
124 mojom::PrefRegistryPtr pref_registry_in,
125 std::vector<PrefValueStore::PrefStoreType> already_connected_types_in,
126 const ConnectCallback& callback_in)
127 : pref_registry(std::move(pref_registry_in)),
128 already_connected_types(std::move(already_connected_types_in)),
129 callback(callback_in) {}
130
131 PrefStoreManagerImpl::PendingConnect::PendingConnect(PendingConnect&& other) =
132 default;
133
134 PrefStoreManagerImpl::PendingConnect::~PendingConnect() = default;
135
122 void PrefStoreManagerImpl::Register(PrefValueStore::PrefStoreType type, 136 void PrefStoreManagerImpl::Register(PrefValueStore::PrefStoreType type,
123 mojom::PrefStorePtr pref_store_ptr) { 137 mojom::PrefStorePtr pref_store_ptr) {
124 if (expected_pref_stores_.count(type) == 0) { 138 if (expected_pref_stores_.count(type) == 0) {
125 LOG(WARNING) << "Not registering unexpected pref store: " << type; 139 LOG(WARNING) << "Not registering unexpected pref store: " << type;
126 return; 140 return;
127 } 141 }
128 DVLOG(1) << "Registering pref store: " << type; 142 DVLOG(1) << "Registering pref store: " << type;
129 pref_store_ptr.set_connection_error_handler( 143 pref_store_ptr.set_connection_error_handler(
130 base::Bind(&PrefStoreManagerImpl::OnPrefStoreDisconnect, 144 base::Bind(&PrefStoreManagerImpl::OnPrefStoreDisconnect,
131 base::Unretained(this), type)); 145 base::Unretained(this), type));
132 const bool success = 146 const bool success =
133 pref_store_ptrs_.insert(std::make_pair(type, std::move(pref_store_ptr))) 147 pref_store_ptrs_.insert(std::make_pair(type, std::move(pref_store_ptr)))
134 .second; 148 .second;
135 DCHECK(success) << "The same pref store registered twice: " << type; 149 DCHECK(success) << "The same pref store registered twice: " << type;
136 if (AllConnected()) { 150 if (AllConnected()) {
137 DVLOG(1) << "All pref stores registered."; 151 DVLOG(1) << "All pref stores registered.";
138 ProcessPendingConnects(); 152 ProcessPendingConnects();
139 } 153 }
140 } 154 }
141 155
142 void PrefStoreManagerImpl::Connect(mojom::PrefRegistryPtr pref_registry, 156 void PrefStoreManagerImpl::Connect(
143 const ConnectCallback& callback) { 157 mojom::PrefRegistryPtr pref_registry,
158 const std::vector<PrefValueStore::PrefStoreType>& already_connected_types,
159 const ConnectCallback& callback) {
144 if (AllConnected()) { 160 if (AllConnected()) {
145 ConnectImpl(std::move(pref_registry), callback); 161 ConnectImpl(std::move(pref_registry), already_connected_types, callback);
146 } else { 162 } else {
147 pending_connects_.push_back( 163 pending_connects_.emplace_back(std::move(pref_registry),
148 std::make_pair(callback, std::move(pref_registry))); 164 already_connected_types, callback);
149 } 165 }
150 } 166 }
151 167
152 void PrefStoreManagerImpl::Create( 168 void PrefStoreManagerImpl::Create(
153 const service_manager::Identity& remote_identity, 169 const service_manager::Identity& remote_identity,
154 prefs::mojom::PrefStoreConnectorRequest request) { 170 prefs::mojom::PrefStoreConnectorRequest request) {
155 connector_bindings_.AddBinding(this, std::move(request)); 171 connector_bindings_.AddBinding(this, std::move(request));
156 } 172 }
157 173
158 void PrefStoreManagerImpl::Create( 174 void PrefStoreManagerImpl::Create(
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 pref_store_ptrs_.erase(type); 220 pref_store_ptrs_.erase(type);
205 } 221 }
206 222
207 bool PrefStoreManagerImpl::AllConnected() const { 223 bool PrefStoreManagerImpl::AllConnected() const {
208 return pref_store_ptrs_.size() == expected_pref_stores_.size() && 224 return pref_store_ptrs_.size() == expected_pref_stores_.size() &&
209 persistent_pref_store_ && persistent_pref_store_->initialized(); 225 persistent_pref_store_ && persistent_pref_store_->initialized();
210 } 226 }
211 227
212 void PrefStoreManagerImpl::ProcessPendingConnects() { 228 void PrefStoreManagerImpl::ProcessPendingConnects() {
213 for (auto& connect : pending_connects_) 229 for (auto& connect : pending_connects_)
214 ConnectImpl(std::move(connect.second), connect.first); 230 ConnectImpl(std::move(connect.pref_registry),
231 connect.already_connected_types, connect.callback);
215 pending_connects_.clear(); 232 pending_connects_.clear();
216 } 233 }
217 234
218 void PrefStoreManagerImpl::ConnectImpl(mojom::PrefRegistryPtr pref_registry, 235 void PrefStoreManagerImpl::ConnectImpl(
219 const ConnectCallback& callback) { 236 mojom::PrefRegistryPtr pref_registry,
237 const std::vector<PrefValueStore::PrefStoreType>& already_connected_types,
238 const ConnectCallback& callback) {
220 std::vector<std::string> observed_prefs; 239 std::vector<std::string> observed_prefs;
221 for (auto& registration : pref_registry->registrations) { 240 for (auto& registration : pref_registry->registrations) {
222 observed_prefs.push_back(registration.first); 241 observed_prefs.push_back(registration.first);
223 const auto& key = registration.first; 242 const auto& key = registration.first;
224 auto& default_value = registration.second->default_value; 243 auto& default_value = registration.second->default_value;
225 const base::Value* old_default = nullptr; 244 const base::Value* old_default = nullptr;
226 // TODO(sammc): Once non-owning registrations are supported, disallow 245 // TODO(sammc): Once non-owning registrations are supported, disallow
227 // multiple owners instead of just checking for consistent defaults. 246 // multiple owners instead of just checking for consistent defaults.
228 if (defaults_->GetValue(key, &old_default)) 247 if (defaults_->GetValue(key, &old_default))
229 DCHECK(old_default->Equals(default_value.get())); 248 DCHECK(old_default->Equals(default_value.get()));
230 else 249 else
231 defaults_->SetDefaultValue(key, std::move(default_value)); 250 defaults_->SetDefaultValue(key, std::move(default_value));
232 } 251 }
252
253 // Only connect to pref stores the client isn't already connected to.
254 PrefStorePtrs ptrs;
255 for (const auto& entry : pref_store_ptrs_) {
256 if (std::find(already_connected_types.begin(),
257 already_connected_types.end(),
258 entry.first) == already_connected_types.end()) {
259 ptrs.insert(entry);
260 }
261 }
233 ConnectionBarrier::Create( 262 ConnectionBarrier::Create(
234 pref_store_ptrs_, 263 ptrs,
235 persistent_pref_store_->CreateConnection( 264 persistent_pref_store_->CreateConnection(
236 PersistentPrefStoreImpl::ObservedPrefs(observed_prefs.begin(), 265 PersistentPrefStoreImpl::ObservedPrefs(observed_prefs.begin(),
237 observed_prefs.end())), 266 observed_prefs.end())),
238 observed_prefs, callback); 267 observed_prefs, callback);
239 } 268 }
240 269
241 void PrefStoreManagerImpl::OnPersistentPrefStoreReady() { 270 void PrefStoreManagerImpl::OnPersistentPrefStoreReady() {
242 DVLOG(1) << "PersistentPrefStore ready"; 271 DVLOG(1) << "PersistentPrefStore ready";
243 if (AllConnected()) { 272 if (AllConnected()) {
244 ProcessPendingConnects(); 273 ProcessPendingConnects();
245 } 274 }
246 } 275 }
247 276
248 } // namespace prefs 277 } // namespace prefs
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698