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/files/file_path.h" | 11 #include "base/files/file_path.h" |
11 #include "base/json/json_file_value_serializer.h" | 12 #include "base/json/json_file_value_serializer.h" |
12 #include "base/metrics/field_trial.h" | 13 #include "base/metrics/field_trial.h" |
13 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
14 #include "base/prefs/default_pref_store.h" | 15 #include "base/prefs/default_pref_store.h" |
15 #include "base/prefs/json_pref_store.h" | 16 #include "base/prefs/json_pref_store.h" |
16 #include "base/prefs/pref_filter.h" | 17 #include "base/prefs/pref_filter.h" |
17 #include "base/prefs/pref_notifier_impl.h" | 18 #include "base/prefs/pref_notifier_impl.h" |
18 #include "base/prefs/pref_registry.h" | 19 #include "base/prefs/pref_registry.h" |
19 #include "base/prefs/pref_service.h" | 20 #include "base/prefs/pref_service.h" |
20 #include "base/prefs/pref_store.h" | 21 #include "base/prefs/pref_store.h" |
21 #include "base/prefs/pref_value_store.h" | 22 #include "base/prefs/pref_value_store.h" |
22 #include "base/threading/sequenced_worker_pool.h" | 23 #include "base/threading/sequenced_worker_pool.h" |
23 #include "base/time/time.h" | 24 #include "base/time/time.h" |
| 25 #include "base/values.h" |
24 #include "chrome/browser/browser_process.h" | 26 #include "chrome/browser/browser_process.h" |
25 #include "chrome/browser/prefs/command_line_pref_store.h" | 27 #include "chrome/browser/prefs/command_line_pref_store.h" |
26 #include "chrome/browser/prefs/pref_hash_filter.h" | 28 #include "chrome/browser/prefs/pref_hash_filter.h" |
27 #include "chrome/browser/prefs/pref_hash_store_impl.h" | 29 #include "chrome/browser/prefs/pref_hash_store_impl.h" |
28 #include "chrome/browser/prefs/pref_model_associator.h" | 30 #include "chrome/browser/prefs/pref_model_associator.h" |
29 #include "chrome/browser/prefs/pref_service_syncable.h" | 31 #include "chrome/browser/prefs/pref_service_syncable.h" |
30 #include "chrome/browser/prefs/pref_service_syncable_factory.h" | 32 #include "chrome/browser/prefs/pref_service_syncable_factory.h" |
31 #include "chrome/browser/profiles/file_path_verifier_win.h" | 33 #include "chrome/browser/profiles/file_path_verifier_win.h" |
32 #include "chrome/browser/ui/profile_error_dialog.h" | 34 #include "chrome/browser/ui/profile_error_dialog.h" |
33 #include "chrome/common/chrome_constants.h" | 35 #include "chrome/common/chrome_constants.h" |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 scoped_ptr<PrefFilter> pref_filter; | 292 scoped_ptr<PrefFilter> pref_filter; |
291 if (pref_hash_store) | 293 if (pref_hash_store) |
292 pref_filter = CreatePrefHashFilter(pref_hash_store.Pass()); | 294 pref_filter = CreatePrefHashFilter(pref_hash_store.Pass()); |
293 factory->set_user_prefs( | 295 factory->set_user_prefs( |
294 new JsonPrefStore( | 296 new JsonPrefStore( |
295 pref_filename, | 297 pref_filename, |
296 pref_io_task_runner, | 298 pref_io_task_runner, |
297 pref_filter.Pass())); | 299 pref_filter.Pass())); |
298 } | 300 } |
299 | 301 |
| 302 // An in-memory PrefStore backed by an immutable DictionaryValue. |
| 303 class DictionaryPrefStore : public PrefStore { |
| 304 public: |
| 305 explicit DictionaryPrefStore(const base::DictionaryValue* dictionary) |
| 306 : dictionary_(dictionary) {} |
| 307 |
| 308 virtual bool GetValue(const std::string& key, |
| 309 const base::Value** result) const OVERRIDE { |
| 310 const base::Value* tmp = NULL; |
| 311 if (!dictionary_->Get(key, &tmp)) |
| 312 return false; |
| 313 |
| 314 if (result) |
| 315 *result = tmp; |
| 316 return true; |
| 317 } |
| 318 |
| 319 private: |
| 320 virtual ~DictionaryPrefStore() {} |
| 321 |
| 322 const base::DictionaryValue* dictionary_; |
| 323 |
| 324 DISALLOW_COPY_AND_ASSIGN(DictionaryPrefStore); |
| 325 }; |
| 326 |
300 // Waits for a PrefStore to be initialized and then initializes the | 327 // Waits for a PrefStore to be initialized and then initializes the |
301 // corresponding PrefHashStore. | 328 // corresponding PrefHashStore. |
302 // The observer deletes itself when its work is completed. | 329 // The observer deletes itself when its work is completed. |
303 class InitializeHashStoreObserver : public PrefStore::Observer { | 330 class InitializeHashStoreObserver : public PrefStore::Observer { |
304 public: | 331 public: |
305 // Creates an observer that will initialize |pref_hash_store| with the | 332 // Creates an observer that will initialize |pref_hash_store| with the |
306 // contents of |pref_store| when the latter is fully loaded. | 333 // contents of |pref_store| when the latter is fully loaded. |
307 InitializeHashStoreObserver(const scoped_refptr<PrefStore>& pref_store, | 334 InitializeHashStoreObserver(const scoped_refptr<PrefStore>& pref_store, |
308 scoped_ptr<PrefHashStore> pref_hash_store) | 335 scoped_ptr<PrefHashStore> pref_hash_store) |
309 : pref_store_(pref_store), pref_hash_store_(pref_hash_store.Pass()) {} | 336 : pref_store_(pref_store), pref_hash_store_(pref_hash_store.Pass()) {} |
(...skipping 11 matching lines...) Expand all Loading... |
321 DISALLOW_COPY_AND_ASSIGN(InitializeHashStoreObserver); | 348 DISALLOW_COPY_AND_ASSIGN(InitializeHashStoreObserver); |
322 }; | 349 }; |
323 | 350 |
324 InitializeHashStoreObserver::~InitializeHashStoreObserver() {} | 351 InitializeHashStoreObserver::~InitializeHashStoreObserver() {} |
325 | 352 |
326 void InitializeHashStoreObserver::OnPrefValueChanged(const std::string& key) {} | 353 void InitializeHashStoreObserver::OnPrefValueChanged(const std::string& key) {} |
327 | 354 |
328 void InitializeHashStoreObserver::OnInitializationCompleted(bool succeeded) { | 355 void InitializeHashStoreObserver::OnInitializationCompleted(bool succeeded) { |
329 // If we successfully loaded the preferences _and_ the PrefHashStore hasn't | 356 // If we successfully loaded the preferences _and_ the PrefHashStore hasn't |
330 // been initialized by someone else in the meantime initialize it now. | 357 // been initialized by someone else in the meantime initialize it now. |
331 if (succeeded & !pref_hash_store_->IsInitialized()) | 358 if (succeeded & !pref_hash_store_->IsInitialized()) { |
332 CreatePrefHashFilter(pref_hash_store_.Pass())->Initialize(pref_store_); | 359 CreatePrefHashFilter( |
| 360 pref_hash_store_.Pass())->Initialize(*pref_store_); |
| 361 UMA_HISTOGRAM_BOOLEAN( |
| 362 "Settings.TrackedPreferencesInitializedForUnloadedProfile", true); |
| 363 } |
333 pref_store_->RemoveObserver(this); | 364 pref_store_->RemoveObserver(this); |
334 delete this; | 365 delete this; |
335 } | 366 } |
336 | 367 |
337 } // namespace | 368 } // namespace |
338 | 369 |
339 namespace chrome_prefs { | 370 namespace chrome_prefs { |
340 | 371 |
341 scoped_ptr<PrefService> CreateLocalState( | 372 scoped_ptr<PrefService> CreateLocalState( |
342 const base::FilePath& pref_filename, | 373 const base::FilePath& pref_filename, |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 new InitializeHashStoreObserver( | 436 new InitializeHashStoreObserver( |
406 pref_store, pref_hash_store.PassAs<PrefHashStore>())); | 437 pref_store, pref_hash_store.PassAs<PrefHashStore>())); |
407 pref_store->ReadPrefsAsync(NULL); | 438 pref_store->ReadPrefsAsync(NULL); |
408 } | 439 } |
409 } | 440 } |
410 | 441 |
411 void ResetPrefHashStore(const base::FilePath& profile_path) { | 442 void ResetPrefHashStore(const base::FilePath& profile_path) { |
412 GetPrefHashStoreImpl(profile_path)->Reset(); | 443 GetPrefHashStoreImpl(profile_path)->Reset(); |
413 } | 444 } |
414 | 445 |
| 446 bool InitializePrefsFromMasterPrefs( |
| 447 const base::FilePath& profile_path, |
| 448 const base::DictionaryValue& master_prefs) { |
| 449 // Create the profile directory if it doesn't exist yet (very possible on |
| 450 // first run). |
| 451 if (!base::CreateDirectory(profile_path)) |
| 452 return false; |
| 453 |
| 454 JSONFileValueSerializer serializer( |
| 455 GetPrefFilePathFromProfilePath(profile_path)); |
| 456 |
| 457 // Call Serialize (which does IO) on the main thread, which would _normally_ |
| 458 // be verboten. In this case however, we require this IO to synchronously |
| 459 // complete before Chrome can start (as master preferences seed the Local |
| 460 // State and Preferences files). This won't trip ThreadIORestrictions as they |
| 461 // won't have kicked in yet on the main thread. |
| 462 bool success = serializer.Serialize(master_prefs); |
| 463 |
| 464 if (success) { |
| 465 scoped_refptr<const PrefStore> pref_store( |
| 466 new DictionaryPrefStore(&master_prefs)); |
| 467 CreatePrefHashFilter( |
| 468 GetPrefHashStoreImpl(profile_path).PassAs<PrefHashStore>())-> |
| 469 Initialize(*pref_store); |
| 470 } |
| 471 |
| 472 UMA_HISTOGRAM_BOOLEAN("Settings.InitializedFromMasterPrefs", success); |
| 473 return success; |
| 474 } |
| 475 |
415 } // namespace chrome_prefs | 476 } // namespace chrome_prefs |
OLD | NEW |