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 <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "base/threading/sequenced_worker_pool.h" | 12 #include "base/threading/sequenced_worker_pool.h" |
| 13 #include "components/prefs/default_pref_store.h" | 13 #include "components/prefs/default_pref_store.h" |
| 14 #include "components/prefs/pref_value_store.h" | 14 #include "components/prefs/pref_value_store.h" |
| 15 #include "mojo/public/cpp/bindings/interface_request.h" | 15 #include "mojo/public/cpp/bindings/interface_request.h" |
| 16 #include "services/preferences/persistent_pref_store_factory.h" | 16 #include "services/preferences/persistent_pref_store_factory.h" |
| 17 #include "services/preferences/persistent_pref_store_impl.h" | 17 #include "services/preferences/persistent_pref_store_impl.h" |
| 18 #include "services/preferences/public/cpp/pref_store_client.h" | |
| 18 #include "services/preferences/public/cpp/pref_store_impl.h" | 19 #include "services/preferences/public/cpp/pref_store_impl.h" |
| 19 #include "services/service_manager/public/cpp/bind_source_info.h" | 20 #include "services/service_manager/public/cpp/bind_source_info.h" |
| 20 | 21 |
| 21 namespace prefs { | 22 namespace prefs { |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 using ConnectCallback = mojom::PrefStoreConnector::ConnectCallback; | 25 using ConnectCallback = mojom::PrefStoreConnector::ConnectCallback; |
| 25 using PrefStorePtrs = | 26 using PrefStorePtrs = |
| 26 std::unordered_map<PrefValueStore::PrefStoreType, mojom::PrefStore*>; | 27 std::unordered_map<PrefValueStore::PrefStoreType, mojom::PrefStore*>; |
| 27 | 28 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 114 defaults_(new DefaultPrefStore), | 115 defaults_(new DefaultPrefStore), |
| 115 defaults_wrapper_(base::MakeUnique<PrefStoreImpl>( | 116 defaults_wrapper_(base::MakeUnique<PrefStoreImpl>( |
| 116 defaults_, | 117 defaults_, |
| 117 mojo::MakeRequest(&pref_store_ptrs_[PrefValueStore::DEFAULT_STORE]))), | 118 mojo::MakeRequest(&pref_store_ptrs_[PrefValueStore::DEFAULT_STORE]))), |
| 118 worker_pool_(std::move(worker_pool)) { | 119 worker_pool_(std::move(worker_pool)) { |
| 119 DCHECK( | 120 DCHECK( |
| 120 base::ContainsValue(expected_pref_stores_, PrefValueStore::USER_STORE) && | 121 base::ContainsValue(expected_pref_stores_, PrefValueStore::USER_STORE) && |
| 121 base::ContainsValue(expected_pref_stores_, PrefValueStore::DEFAULT_STORE)) | 122 base::ContainsValue(expected_pref_stores_, PrefValueStore::DEFAULT_STORE)) |
| 122 << "expected_pref_stores must always include PrefValueStore::USER_STORE " | 123 << "expected_pref_stores must always include PrefValueStore::USER_STORE " |
| 123 "and PrefValueStore::DEFAULT_STORE."; | 124 "and PrefValueStore::DEFAULT_STORE."; |
| 124 // The user store is not actually connected to in the implementation, but | 125 // The user store is not actually registered or connected to in the |
| 125 // accessed directly. | 126 // implementation, but accessed directly. |
| 126 expected_pref_stores_.erase(PrefValueStore::USER_STORE); | 127 expected_pref_stores_.erase(PrefValueStore::USER_STORE); |
| 128 DVLOG(1) << "Expecting " << expected_pref_stores_.size() | |
| 129 << " pref store(s) to register"; | |
| 130 // This store is done in-process so it's already "registered": | |
|
Sam McNally
2017/05/10 01:22:16
in-service
| |
| 131 DVLOG(1) << "Registering pref store: " << PrefValueStore::DEFAULT_STORE; | |
| 127 registry_.AddInterface<prefs::mojom::PrefStoreConnector>( | 132 registry_.AddInterface<prefs::mojom::PrefStoreConnector>( |
| 128 base::Bind(&PrefStoreManagerImpl::BindPrefStoreConnectorRequest, | 133 base::Bind(&PrefStoreManagerImpl::BindPrefStoreConnectorRequest, |
| 129 base::Unretained(this))); | 134 base::Unretained(this))); |
| 130 registry_.AddInterface<prefs::mojom::PrefStoreRegistry>( | 135 registry_.AddInterface<prefs::mojom::PrefStoreRegistry>( |
| 131 base::Bind(&PrefStoreManagerImpl::BindPrefStoreRegistryRequest, | 136 base::Bind(&PrefStoreManagerImpl::BindPrefStoreRegistryRequest, |
| 132 base::Unretained(this))); | 137 base::Unretained(this))); |
| 133 registry_.AddInterface<prefs::mojom::PrefServiceControl>( | 138 registry_.AddInterface<prefs::mojom::PrefServiceControl>( |
| 134 base::Bind(&PrefStoreManagerImpl::BindPrefServiceControlRequest, | 139 base::Bind(&PrefStoreManagerImpl::BindPrefServiceControlRequest, |
| 135 base::Unretained(this))); | 140 base::Unretained(this))); |
| 136 } | 141 } |
| 137 | 142 |
| 138 PrefStoreManagerImpl::~PrefStoreManagerImpl() = default; | 143 PrefStoreManagerImpl::~PrefStoreManagerImpl() { |
| 144 // For logging consistency: | |
| 145 DVLOG(1) << "Deregistering pref store: " << PrefValueStore::DEFAULT_STORE; | |
| 146 } | |
| 139 | 147 |
| 140 struct PrefStoreManagerImpl::PendingConnect { | 148 struct PrefStoreManagerImpl::PendingConnect { |
| 141 mojom::PrefRegistryPtr pref_registry; | 149 mojom::PrefRegistryPtr pref_registry; |
| 142 // Pref stores the caller already is connected to (and hence we won't | 150 // Pref stores the caller already is connected to (and hence we won't |
| 143 // connect to these). | 151 // connect to these). |
| 144 std::vector<PrefValueStore::PrefStoreType> already_connected_types; | 152 std::vector<PrefValueStore::PrefStoreType> already_connected_types; |
| 145 ConnectCallback callback; | 153 ConnectCallback callback; |
| 146 }; | 154 }; |
| 147 | 155 |
| 148 void PrefStoreManagerImpl::Register(PrefValueStore::PrefStoreType type, | 156 void PrefStoreManagerImpl::Register(PrefValueStore::PrefStoreType type, |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 162 if (AllConnected()) { | 170 if (AllConnected()) { |
| 163 DVLOG(1) << "All pref stores registered."; | 171 DVLOG(1) << "All pref stores registered."; |
| 164 ProcessPendingConnects(); | 172 ProcessPendingConnects(); |
| 165 } | 173 } |
| 166 } | 174 } |
| 167 | 175 |
| 168 void PrefStoreManagerImpl::Connect( | 176 void PrefStoreManagerImpl::Connect( |
| 169 mojom::PrefRegistryPtr pref_registry, | 177 mojom::PrefRegistryPtr pref_registry, |
| 170 const std::vector<PrefValueStore::PrefStoreType>& already_connected_types, | 178 const std::vector<PrefValueStore::PrefStoreType>& already_connected_types, |
| 171 const ConnectCallback& callback) { | 179 const ConnectCallback& callback) { |
| 180 DVLOG(1) << "Will connect to " | |
| 181 << expected_pref_stores_.size() - already_connected_types.size() | |
| 182 << " pref store(s)"; | |
| 183 if (incognito_connector_ && | |
| 184 !base::ContainsValue(already_connected_types, | |
| 185 PrefValueStore::INCOGNITO_STORE)) { | |
| 186 incognito_connector_->ConnectToUserPrefStore( | |
|
Sam McNally
2017/05/10 01:22:16
Move this to Init() and remove |incognito_connecto
| |
| 187 mojo::MakeRequest(&pref_store_ptrs_[PrefValueStore::INCOGNITO_STORE])); | |
| 188 } | |
| 172 if (AllConnected()) { | 189 if (AllConnected()) { |
| 173 ConnectImpl(std::move(pref_registry), already_connected_types, callback); | 190 ConnectImpl(std::move(pref_registry), already_connected_types, callback); |
| 174 } else { | 191 } else { |
| 175 pending_connects_.push_back( | 192 pending_connects_.push_back( |
| 176 {std::move(pref_registry), already_connected_types, callback}); | 193 {std::move(pref_registry), already_connected_types, callback}); |
| 177 } | 194 } |
| 178 } | 195 } |
| 179 | 196 |
| 197 void PrefStoreManagerImpl::ConnectToUserPrefStore( | |
| 198 mojom::PrefStoreRequest request) { | |
| 199 pending_ro_persistent_pref_store_requests_.push_back(std::move(request)); | |
| 200 if (AllConnected()) | |
| 201 ProcessPendingConnects(); | |
| 202 } | |
| 203 | |
| 180 void PrefStoreManagerImpl::BindPrefStoreConnectorRequest( | 204 void PrefStoreManagerImpl::BindPrefStoreConnectorRequest( |
| 181 const service_manager::BindSourceInfo& source_info, | 205 const service_manager::BindSourceInfo& source_info, |
| 182 prefs::mojom::PrefStoreConnectorRequest request) { | 206 prefs::mojom::PrefStoreConnectorRequest request) { |
| 183 connector_bindings_.AddBinding(this, std::move(request)); | 207 connector_bindings_.AddBinding(this, std::move(request)); |
| 184 } | 208 } |
| 185 | 209 |
| 186 void PrefStoreManagerImpl::BindPrefStoreRegistryRequest( | 210 void PrefStoreManagerImpl::BindPrefStoreRegistryRequest( |
| 187 const service_manager::BindSourceInfo& source_info, | 211 const service_manager::BindSourceInfo& source_info, |
| 188 prefs::mojom::PrefStoreRegistryRequest request) { | 212 prefs::mojom::PrefStoreRegistryRequest request) { |
| 189 registry_bindings_.AddBinding(this, std::move(request)); | 213 registry_bindings_.AddBinding(this, std::move(request)); |
| 190 } | 214 } |
| 191 | 215 |
| 192 void PrefStoreManagerImpl::BindPrefServiceControlRequest( | 216 void PrefStoreManagerImpl::BindPrefServiceControlRequest( |
| 193 const service_manager::BindSourceInfo& source_info, | 217 const service_manager::BindSourceInfo& source_info, |
| 194 prefs::mojom::PrefServiceControlRequest request) { | 218 prefs::mojom::PrefServiceControlRequest request) { |
| 195 if (init_binding_.is_bound()) { | 219 if (init_binding_.is_bound()) { |
| 196 LOG(ERROR) | 220 LOG(ERROR) |
| 197 << "Pref service received unexpected control interface connection from " | 221 << "Pref service received unexpected control interface connection from " |
| 198 << source_info.identity.name(); | 222 << source_info.identity.name(); |
| 199 return; | 223 return; |
| 200 } | 224 } |
| 201 | 225 |
| 202 init_binding_.Bind(std::move(request)); | 226 init_binding_.Bind(std::move(request)); |
| 203 } | 227 } |
| 204 | 228 |
| 205 void PrefStoreManagerImpl::Init( | 229 void PrefStoreManagerImpl::Init( |
| 206 mojom::PersistentPrefStoreConfigurationPtr configuration) { | 230 mojom::PersistentPrefStoreConfigurationPtr configuration) { |
| 207 DCHECK(!persistent_pref_store_); | 231 DCHECK(!persistent_pref_store_); |
| 208 | 232 |
| 233 if (configuration->is_incognito_configuration()) { | |
| 234 DCHECK(expected_pref_stores_.count(PrefValueStore::INCOGNITO_STORE)) | |
| 235 << "expected_pref_stores must include PrefValueStore::INCOGNITO_STORE " | |
| 236 << "when creating an incognito instance"; | |
| 237 incognito_connector_ = | |
| 238 std::move(configuration->get_incognito_configuration()->connector); | |
| 239 } | |
| 209 persistent_pref_store_ = CreatePersistentPrefStore( | 240 persistent_pref_store_ = CreatePersistentPrefStore( |
| 210 std::move(configuration), worker_pool_.get(), | 241 std::move(configuration), worker_pool_.get(), |
| 211 base::Bind(&PrefStoreManagerImpl::OnPersistentPrefStoreReady, | 242 base::Bind(&PrefStoreManagerImpl::OnPersistentPrefStoreReady, |
| 212 base::Unretained(this))); | 243 base::Unretained(this))); |
| 213 DCHECK(persistent_pref_store_); | 244 DCHECK(persistent_pref_store_); |
| 214 if (AllConnected()) | 245 if (AllConnected()) |
| 215 ProcessPendingConnects(); | 246 ProcessPendingConnects(); |
| 216 } | 247 } |
| 217 | 248 |
| 218 void PrefStoreManagerImpl::OnStart() {} | 249 void PrefStoreManagerImpl::OnStart() {} |
| 219 | 250 |
| 220 void PrefStoreManagerImpl::OnBindInterface( | 251 void PrefStoreManagerImpl::OnBindInterface( |
| 221 const service_manager::BindSourceInfo& source_info, | 252 const service_manager::BindSourceInfo& source_info, |
| 222 const std::string& interface_name, | 253 const std::string& interface_name, |
| 223 mojo::ScopedMessagePipeHandle interface_pipe) { | 254 mojo::ScopedMessagePipeHandle interface_pipe) { |
| 224 registry_.BindInterface(source_info, interface_name, | 255 registry_.BindInterface(source_info, interface_name, |
| 225 std::move(interface_pipe)); | 256 std::move(interface_pipe)); |
| 226 } | 257 } |
| 227 | 258 |
| 228 void PrefStoreManagerImpl::OnPrefStoreDisconnect( | 259 void PrefStoreManagerImpl::OnPrefStoreDisconnect( |
| 229 PrefValueStore::PrefStoreType type) { | 260 PrefValueStore::PrefStoreType type) { |
| 230 DVLOG(1) << "Deregistering pref store: " << type; | 261 DVLOG(1) << "Deregistering pref store: " << type; |
| 231 pref_store_ptrs_.erase(type); | 262 pref_store_ptrs_.erase(type); |
| 232 } | 263 } |
| 233 | 264 |
| 234 bool PrefStoreManagerImpl::AllConnected() const { | 265 bool PrefStoreManagerImpl::AllConnected() const { |
| 266 DCHECK_LE(pref_store_ptrs_.size(), expected_pref_stores_.size()); | |
| 235 return pref_store_ptrs_.size() == expected_pref_stores_.size() && | 267 return pref_store_ptrs_.size() == expected_pref_stores_.size() && |
| 236 persistent_pref_store_ && persistent_pref_store_->initialized(); | 268 persistent_pref_store_ && persistent_pref_store_->initialized(); |
| 237 } | 269 } |
| 238 | 270 |
| 239 void PrefStoreManagerImpl::ProcessPendingConnects() { | 271 void PrefStoreManagerImpl::ProcessPendingConnects() { |
| 272 DCHECK(persistent_pref_store_); | |
| 240 for (auto& connect : pending_connects_) | 273 for (auto& connect : pending_connects_) |
| 241 ConnectImpl(std::move(connect.pref_registry), | 274 ConnectImpl(std::move(connect.pref_registry), |
| 242 connect.already_connected_types, connect.callback); | 275 connect.already_connected_types, connect.callback); |
| 243 pending_connects_.clear(); | 276 pending_connects_.clear(); |
| 277 ro_persistent_pref_store_bindings_.reset( | |
| 278 new mojo::BindingSet<mojom::PrefStore>()); | |
| 279 for (auto& request : pending_ro_persistent_pref_store_requests_) { | |
| 280 ro_persistent_pref_store_bindings_->AddBinding(persistent_pref_store_.get(), | |
| 281 std::move(request)); | |
| 282 } | |
| 283 pending_ro_persistent_pref_store_requests_.clear(); | |
| 244 } | 284 } |
| 245 | 285 |
| 246 void PrefStoreManagerImpl::ConnectImpl( | 286 std::vector<std::string> PrefStoreManagerImpl::RegisterPrefs( |
| 247 mojom::PrefRegistryPtr pref_registry, | 287 mojom::PrefRegistryPtr pref_registry) { |
| 248 const std::vector<PrefValueStore::PrefStoreType>& already_connected_types, | |
| 249 const ConnectCallback& callback) { | |
| 250 std::vector<std::string> observed_prefs; | 288 std::vector<std::string> observed_prefs; |
| 251 for (auto& registration : pref_registry->registrations) { | 289 for (auto& registration : pref_registry->registrations) { |
| 252 observed_prefs.push_back(registration.first); | 290 observed_prefs.push_back(registration.first); |
| 253 const auto& key = registration.first; | 291 const auto& key = registration.first; |
| 254 auto& default_value = registration.second->default_value; | 292 auto& default_value = registration.second->default_value; |
| 255 const base::Value* old_default = nullptr; | 293 const base::Value* old_default = nullptr; |
| 256 // TODO(sammc): Once non-owning registrations are supported, disallow | 294 // TODO(sammc): Once non-owning registrations are supported, disallow |
| 257 // multiple owners instead of just checking for consistent defaults. | 295 // multiple owners instead of just checking for consistent defaults. |
| 258 if (defaults_->GetValue(key, &old_default)) | 296 if (defaults_->GetValue(key, &old_default)) |
| 259 DCHECK(old_default->Equals(default_value.get())); | 297 DCHECK(old_default->Equals(default_value.get())); |
| 260 else | 298 else |
| 261 defaults_->SetDefaultValue(key, std::move(default_value)); | 299 defaults_->SetDefaultValue(key, std::move(default_value)); |
| 262 } | 300 } |
| 301 return observed_prefs; | |
| 302 } | |
| 263 | 303 |
| 304 void PrefStoreManagerImpl::ConnectImpl( | |
| 305 mojom::PrefRegistryPtr pref_registry, | |
| 306 const std::vector<PrefValueStore::PrefStoreType>& already_connected_types, | |
| 307 const ConnectCallback& callback) { | |
| 308 std::vector<std::string> observed_prefs = | |
| 309 RegisterPrefs(std::move(pref_registry)); | |
| 264 // Only connect to pref stores the client isn't already connected to. | 310 // Only connect to pref stores the client isn't already connected to. |
| 265 PrefStorePtrs ptrs; | 311 PrefStorePtrs ptrs; |
| 266 for (const auto& entry : pref_store_ptrs_) { | 312 for (const auto& entry : pref_store_ptrs_) { |
| 267 if (!base::ContainsValue(already_connected_types, entry.first)) { | 313 if (!base::ContainsValue(already_connected_types, entry.first)) { |
| 268 ptrs.insert(std::make_pair(entry.first, entry.second.get())); | 314 ptrs.insert(std::make_pair(entry.first, entry.second.get())); |
| 269 } | 315 } |
| 270 } | 316 } |
| 271 ConnectionBarrier::Create( | 317 ConnectionBarrier::Create( |
| 272 ptrs, | 318 ptrs, |
| 273 persistent_pref_store_->CreateConnection( | 319 persistent_pref_store_->CreateConnection( |
| 274 PersistentPrefStoreImpl::ObservedPrefs(observed_prefs.begin(), | 320 PersistentPrefStoreImpl::ObservedPrefs(observed_prefs.begin(), |
| 275 observed_prefs.end())), | 321 observed_prefs.end())), |
| 276 observed_prefs, callback); | 322 observed_prefs, callback); |
| 277 } | 323 } |
| 278 | 324 |
| 279 void PrefStoreManagerImpl::OnPersistentPrefStoreReady() { | 325 void PrefStoreManagerImpl::OnPersistentPrefStoreReady() { |
| 280 DVLOG(1) << "PersistentPrefStore ready"; | 326 DVLOG(1) << "PersistentPrefStore ready"; |
| 281 if (AllConnected()) { | 327 if (AllConnected()) { |
| 282 ProcessPendingConnects(); | 328 ProcessPendingConnects(); |
| 283 } | 329 } |
| 284 } | 330 } |
| 285 | 331 |
| 286 } // namespace prefs | 332 } // namespace prefs |
| OLD | NEW |