| 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 private: |
| 287 virtual ~DictionaryPrefStore() {} |
| 288 |
| 289 const base::DictionaryValue* dictionary_; |
| 290 |
| 291 DISALLOW_COPY_AND_ASSIGN(DictionaryPrefStore); |
| 292 }; |
| 293 |
| 268 // Waits for a PrefStore to be initialized and then initializes the | 294 // Waits for a PrefStore to be initialized and then initializes the |
| 269 // corresponding PrefHashStore. | 295 // corresponding PrefHashStore. |
| 270 // The observer deletes itself when its work is completed. | 296 // The observer deletes itself when its work is completed. |
| 271 class InitializeHashStoreObserver : public PrefStore::Observer { | 297 class InitializeHashStoreObserver : public PrefStore::Observer { |
| 272 public: | 298 public: |
| 273 // Creates an observer that will initialize |pref_hash_store| with the | 299 // Creates an observer that will initialize |pref_hash_store| with the |
| 274 // contents of |pref_store| when the latter is fully loaded. | 300 // contents of |pref_store| when the latter is fully loaded. |
| 275 InitializeHashStoreObserver(const scoped_refptr<PrefStore>& pref_store, | 301 InitializeHashStoreObserver(const scoped_refptr<PrefStore>& pref_store, |
| 276 scoped_ptr<PrefHashStore> pref_hash_store) | 302 scoped_ptr<PrefHashStore> pref_hash_store) |
| 277 : pref_store_(pref_store), pref_hash_store_(pref_hash_store.Pass()) {} | 303 : pref_store_(pref_store), pref_hash_store_(pref_hash_store.Pass()) {} |
| (...skipping 11 matching lines...) Expand all Loading... |
| 289 DISALLOW_COPY_AND_ASSIGN(InitializeHashStoreObserver); | 315 DISALLOW_COPY_AND_ASSIGN(InitializeHashStoreObserver); |
| 290 }; | 316 }; |
| 291 | 317 |
| 292 InitializeHashStoreObserver::~InitializeHashStoreObserver() {} | 318 InitializeHashStoreObserver::~InitializeHashStoreObserver() {} |
| 293 | 319 |
| 294 void InitializeHashStoreObserver::OnPrefValueChanged(const std::string& key) {} | 320 void InitializeHashStoreObserver::OnPrefValueChanged(const std::string& key) {} |
| 295 | 321 |
| 296 void InitializeHashStoreObserver::OnInitializationCompleted(bool succeeded) { | 322 void InitializeHashStoreObserver::OnInitializationCompleted(bool succeeded) { |
| 297 // If we successfully loaded the preferences _and_ the PrefHashStore hasn't | 323 // If we successfully loaded the preferences _and_ the PrefHashStore hasn't |
| 298 // been initialized by someone else in the meantime initialize it now. | 324 // been initialized by someone else in the meantime initialize it now. |
| 299 if (succeeded & !pref_hash_store_->IsInitialized()) | 325 if (succeeded & !pref_hash_store_->IsInitialized()) { |
| 300 CreatePrefHashFilter(pref_hash_store_.Pass())->Initialize(pref_store_); | 326 CreatePrefHashFilter( |
| 327 pref_hash_store_.Pass())->Initialize(*pref_store_); |
| 328 UMA_HISTOGRAM_BOOLEAN( |
| 329 "Settings.TrackedPreferencesInitializedForUnloadedProfile", true); |
| 330 } |
| 301 pref_store_->RemoveObserver(this); | 331 pref_store_->RemoveObserver(this); |
| 302 delete this; | 332 delete this; |
| 303 } | 333 } |
| 304 | 334 |
| 305 } // namespace | 335 } // namespace |
| 306 | 336 |
| 307 namespace chrome_prefs { | 337 namespace chrome_prefs { |
| 308 | 338 |
| 309 scoped_ptr<PrefService> CreateLocalState( | 339 scoped_ptr<PrefService> CreateLocalState( |
| 310 const base::FilePath& pref_filename, | 340 const base::FilePath& pref_filename, |
| (...skipping 28 matching lines...) Expand all Loading... |
| 339 pref_filename, | 369 pref_filename, |
| 340 pref_io_task_runner, | 370 pref_io_task_runner, |
| 341 policy_service, | 371 policy_service, |
| 342 managed_user_settings, | 372 managed_user_settings, |
| 343 pref_hash_store.Pass(), | 373 pref_hash_store.Pass(), |
| 344 extension_prefs, | 374 extension_prefs, |
| 345 async); | 375 async); |
| 346 return factory.CreateSyncable(pref_registry.get()); | 376 return factory.CreateSyncable(pref_registry.get()); |
| 347 } | 377 } |
| 348 | 378 |
| 349 void InitializeHashStoreForPrefFile( | 379 void InitializeHashStoreFromPrefFile( |
| 350 const base::FilePath& pref_filename, | 380 const base::FilePath& pref_filename, |
| 351 base::SequencedTaskRunner* pref_io_task_runner, | 381 base::SequencedTaskRunner* pref_io_task_runner, |
| 352 scoped_ptr<PrefHashStore> pref_hash_store) { | 382 scoped_ptr<PrefHashStore> pref_hash_store) { |
| 353 scoped_refptr<JsonPrefStore> pref_store( | 383 scoped_refptr<JsonPrefStore> pref_store( |
| 354 new JsonPrefStore(pref_filename, | 384 new JsonPrefStore(pref_filename, |
| 355 pref_io_task_runner, | 385 pref_io_task_runner, |
| 356 scoped_ptr<PrefFilter>())); | 386 scoped_ptr<PrefFilter>())); |
| 357 pref_store->AddObserver( | 387 pref_store->AddObserver( |
| 358 new InitializeHashStoreObserver(pref_store, pref_hash_store.Pass())); | 388 new InitializeHashStoreObserver(pref_store, pref_hash_store.Pass())); |
| 359 pref_store->ReadPrefsAsync(NULL); | 389 pref_store->ReadPrefsAsync(NULL); |
| 360 } | 390 } |
| 361 | 391 |
| 392 void InitializeHashStoreFromMasterPrefs( |
| 393 const base::DictionaryValue& master_prefs, |
| 394 scoped_ptr<PrefHashStore> pref_hash_store) { |
| 395 scoped_refptr<const PrefStore> pref_store( |
| 396 new DictionaryPrefStore(&master_prefs)); |
| 397 CreatePrefHashFilter(pref_hash_store.Pass())->Initialize(*pref_store); |
| 398 } |
| 399 |
| 362 } // namespace chrome_prefs | 400 } // namespace chrome_prefs |
| OLD | NEW |