Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/prefs/chrome_pref_service_factory.h" | 5 #include "chrome/browser/prefs/chrome_pref_service_factory.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/metrics/field_trial.h" | 12 #include "base/metrics/field_trial.h" |
| 13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 14 #include "base/prefs/default_pref_store.h" | 14 #include "base/prefs/default_pref_store.h" |
| 15 #include "base/prefs/json_pref_store.h" | 15 #include "base/prefs/json_pref_store.h" |
| 16 #include "base/prefs/pref_filter.h" | 16 #include "base/prefs/pref_filter.h" |
| 17 #include "base/prefs/pref_notifier_impl.h" | 17 #include "base/prefs/pref_notifier_impl.h" |
| 18 #include "base/prefs/pref_registry.h" | 18 #include "base/prefs/pref_registry.h" |
| 19 #include "base/prefs/pref_service.h" | 19 #include "base/prefs/pref_service.h" |
| 20 #include "base/prefs/pref_store.h" | 20 #include "base/prefs/pref_store.h" |
| 21 #include "base/prefs/pref_value_store.h" | 21 #include "base/prefs/pref_value_store.h" |
| 22 #include "base/values.h" | |
| 22 #include "chrome/browser/browser_process.h" | 23 #include "chrome/browser/browser_process.h" |
| 23 #include "chrome/browser/prefs/command_line_pref_store.h" | 24 #include "chrome/browser/prefs/command_line_pref_store.h" |
| 24 #include "chrome/browser/prefs/pref_hash_filter.h" | 25 #include "chrome/browser/prefs/pref_hash_filter.h" |
| 25 #include "chrome/browser/prefs/pref_hash_store.h" | 26 #include "chrome/browser/prefs/pref_hash_store.h" |
| 26 #include "chrome/browser/prefs/pref_model_associator.h" | 27 #include "chrome/browser/prefs/pref_model_associator.h" |
| 27 #include "chrome/browser/prefs/pref_service_syncable.h" | 28 #include "chrome/browser/prefs/pref_service_syncable.h" |
| 28 #include "chrome/browser/prefs/pref_service_syncable_factory.h" | 29 #include "chrome/browser/prefs/pref_service_syncable_factory.h" |
| 29 #include "chrome/browser/ui/profile_error_dialog.h" | 30 #include "chrome/browser/ui/profile_error_dialog.h" |
| 30 #include "chrome/common/pref_names.h" | 31 #include "chrome/common/pref_names.h" |
| 31 #include "components/user_prefs/pref_registry_syncable.h" | 32 #include "components/user_prefs/pref_registry_syncable.h" |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 258 scoped_ptr<PrefFilter> pref_filter; | 259 scoped_ptr<PrefFilter> pref_filter; |
| 259 if (pref_hash_store) | 260 if (pref_hash_store) |
| 260 pref_filter = CreatePrefHashFilter(pref_hash_store.Pass()); | 261 pref_filter = CreatePrefHashFilter(pref_hash_store.Pass()); |
| 261 factory->set_user_prefs( | 262 factory->set_user_prefs( |
| 262 new JsonPrefStore( | 263 new JsonPrefStore( |
| 263 pref_filename, | 264 pref_filename, |
| 264 pref_io_task_runner, | 265 pref_io_task_runner, |
| 265 pref_filter.Pass())); | 266 pref_filter.Pass())); |
| 266 } | 267 } |
| 267 | 268 |
| 269 // An in-memory PrefStore backed by an immutable DictionaryValue. | |
| 270 class DictionaryPrefStore : public PrefStore { | |
| 271 public: | |
| 272 explicit DictionaryPrefStore(const base::DictionaryValue* dictionary) | |
| 273 : dictionary_(dictionary) {} | |
| 274 | |
| 275 virtual bool GetValue(const std::string& key, | |
| 276 const base::Value** result) const OVERRIDE { | |
| 277 const base::Value* tmp = NULL; | |
| 278 if (!dictionary_->Get(key, &tmp)) | |
| 279 return false; | |
| 280 | |
| 281 if (result) | |
| 282 *result = tmp; | |
| 283 return true; | |
| 284 } | |
| 285 | |
| 286 protected: | |
| 287 virtual ~DictionaryPrefStore() {} | |
|
Bernhard Bauer
2014/02/04 09:08:02
Nit: Make the destructor private? Protected implie
gab
2014/02/04 14:01:04
Done.
| |
| 288 | |
| 289 private: | |
| 290 const base::DictionaryValue* dictionary_; | |
| 291 | |
| 292 DISALLOW_COPY_AND_ASSIGN(DictionaryPrefStore); | |
| 293 }; | |
| 294 | |
| 268 // Waits for a PrefStore to be initialized and then initializes the | 295 // Waits for a PrefStore to be initialized and then initializes the |
| 269 // corresponding PrefHashStore. | 296 // corresponding PrefHashStore. |
| 270 // The observer deletes itself when its work is completed. | 297 // The observer deletes itself when its work is completed. |
| 271 class InitializeHashStoreObserver : public PrefStore::Observer { | 298 class InitializeHashStoreObserver : public PrefStore::Observer { |
| 272 public: | 299 public: |
| 273 // Creates an observer that will initialize |pref_hash_store| with the | 300 // Creates an observer that will initialize |pref_hash_store| with the |
| 274 // contents of |pref_store| when the latter is fully loaded. | 301 // contents of |pref_store| when the latter is fully loaded. |
| 275 InitializeHashStoreObserver(const scoped_refptr<PrefStore>& pref_store, | 302 InitializeHashStoreObserver(const scoped_refptr<PrefStore>& pref_store, |
| 276 scoped_ptr<PrefHashStore> pref_hash_store) | 303 scoped_ptr<PrefHashStore> pref_hash_store) |
| 277 : pref_store_(pref_store), pref_hash_store_(pref_hash_store.Pass()) {} | 304 : pref_store_(pref_store), pref_hash_store_(pref_hash_store.Pass()) {} |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 289 DISALLOW_COPY_AND_ASSIGN(InitializeHashStoreObserver); | 316 DISALLOW_COPY_AND_ASSIGN(InitializeHashStoreObserver); |
| 290 }; | 317 }; |
| 291 | 318 |
| 292 InitializeHashStoreObserver::~InitializeHashStoreObserver() {} | 319 InitializeHashStoreObserver::~InitializeHashStoreObserver() {} |
| 293 | 320 |
| 294 void InitializeHashStoreObserver::OnPrefValueChanged(const std::string& key) {} | 321 void InitializeHashStoreObserver::OnPrefValueChanged(const std::string& key) {} |
| 295 | 322 |
| 296 void InitializeHashStoreObserver::OnInitializationCompleted(bool succeeded) { | 323 void InitializeHashStoreObserver::OnInitializationCompleted(bool succeeded) { |
| 297 // If we successfully loaded the preferences _and_ the PrefHashStore hasn't | 324 // If we successfully loaded the preferences _and_ the PrefHashStore hasn't |
| 298 // been initialized by someone else in the meantime initialize it now. | 325 // been initialized by someone else in the meantime initialize it now. |
| 299 if (succeeded & !pref_hash_store_->IsInitialized()) | 326 if (succeeded & !pref_hash_store_->IsInitialized()) { |
| 300 CreatePrefHashFilter(pref_hash_store_.Pass())->Initialize(pref_store_); | 327 CreatePrefHashFilter( |
| 328 pref_hash_store_.Pass())->Initialize(*pref_store_); | |
| 329 UMA_HISTOGRAM_BOOLEAN( | |
| 330 "Settings.TrackedPreferencesInitializedForUnloadedProfile", true); | |
| 331 } | |
| 301 pref_store_->RemoveObserver(this); | 332 pref_store_->RemoveObserver(this); |
| 302 delete this; | 333 delete this; |
| 303 } | 334 } |
| 304 | 335 |
| 305 } // namespace | 336 } // namespace |
| 306 | 337 |
| 307 namespace chrome_prefs { | 338 namespace chrome_prefs { |
| 308 | 339 |
| 309 scoped_ptr<PrefService> CreateLocalState( | 340 scoped_ptr<PrefService> CreateLocalState( |
| 310 const base::FilePath& pref_filename, | 341 const base::FilePath& pref_filename, |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 339 pref_filename, | 370 pref_filename, |
| 340 pref_io_task_runner, | 371 pref_io_task_runner, |
| 341 policy_service, | 372 policy_service, |
| 342 managed_user_settings, | 373 managed_user_settings, |
| 343 pref_hash_store.Pass(), | 374 pref_hash_store.Pass(), |
| 344 extension_prefs, | 375 extension_prefs, |
| 345 async); | 376 async); |
| 346 return factory.CreateSyncable(pref_registry.get()); | 377 return factory.CreateSyncable(pref_registry.get()); |
| 347 } | 378 } |
| 348 | 379 |
| 349 void InitializeHashStoreForPrefFile( | 380 void InitializeHashStoreFromPrefFile( |
| 350 const base::FilePath& pref_filename, | 381 const base::FilePath& pref_filename, |
| 351 base::SequencedTaskRunner* pref_io_task_runner, | 382 base::SequencedTaskRunner* pref_io_task_runner, |
| 352 scoped_ptr<PrefHashStore> pref_hash_store) { | 383 scoped_ptr<PrefHashStore> pref_hash_store) { |
| 353 scoped_refptr<JsonPrefStore> pref_store( | 384 scoped_refptr<JsonPrefStore> pref_store( |
| 354 new JsonPrefStore(pref_filename, | 385 new JsonPrefStore(pref_filename, |
| 355 pref_io_task_runner, | 386 pref_io_task_runner, |
| 356 scoped_ptr<PrefFilter>())); | 387 scoped_ptr<PrefFilter>())); |
| 357 pref_store->AddObserver( | 388 pref_store->AddObserver( |
| 358 new InitializeHashStoreObserver(pref_store, pref_hash_store.Pass())); | 389 new InitializeHashStoreObserver(pref_store, pref_hash_store.Pass())); |
| 359 pref_store->ReadPrefsAsync(NULL); | 390 pref_store->ReadPrefsAsync(NULL); |
| 360 } | 391 } |
| 361 | 392 |
| 393 void InitializeHashStoreFromMasterPrefs( | |
| 394 const base::DictionaryValue& master_prefs, | |
| 395 scoped_ptr<PrefHashStore> pref_hash_store) { | |
| 396 scoped_refptr<const PrefStore> pref_store( | |
| 397 new DictionaryPrefStore(&master_prefs)); | |
| 398 CreatePrefHashFilter(pref_hash_store.Pass())->Initialize(*pref_store); | |
| 399 } | |
| 400 | |
| 362 } // namespace chrome_prefs | 401 } // namespace chrome_prefs |
| OLD | NEW |