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