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 3 // found in the LICENSE file. 4 5 #include "base/prefs/pref_service_builder.h" 6 7 #include "base/bind.h" 8 #include "base/prefs/default_pref_store.h" 9 #include "base/prefs/json_pref_store.h" 10 #include "base/prefs/pref_notifier_impl.h" 11 #include "base/prefs/pref_service.h" 12 13 #include "base/prefs/pref_value_store.h" 14 15 namespace { 16 17 // Do-nothing default implementation. 18 void DoNothingHandleReadError(PersistentPrefStore::PrefReadError error) { 19 } 20 21 } // namespace 22 23 PrefServiceBuilder::PrefServiceBuilder() { 24 ResetDefaultState(); 25 } 26 27 PrefServiceBuilder::~PrefServiceBuilder() { 28 } 29 30 PrefServiceBuilder& PrefServiceBuilder::WithManagedPrefs(PrefStore* store) { 31 managed_prefs_ = store; 32 return *this; 33 } 34 35 PrefServiceBuilder& PrefServiceBuilder::WithExtensionPrefs(PrefStore* store) { 36 extension_prefs_ = store; 37 return *this; 38 } 39 40 PrefServiceBuilder& PrefServiceBuilder::WithCommandLinePrefs(PrefStore* store) { 41 command_line_prefs_ = store; 42 return *this; 43 } 44 45 PrefServiceBuilder& PrefServiceBuilder::WithUserPrefs( 46 PersistentPrefStore* store) { 47 user_prefs_ = store; 48 return *this; 49 } 50 51 PrefServiceBuilder& PrefServiceBuilder::WithRecommendedPrefs(PrefStore* store) { 52 recommended_prefs_ = store; 53 return *this; 54 } 55 56 PrefServiceBuilder& PrefServiceBuilder::WithReadErrorCallback( 57 const base::Callback<void(PersistentPrefStore::PrefReadError)>& 58 read_error_callback) { 59 read_error_callback_ = read_error_callback; 60 return *this; 61 } 62 63 PrefServiceBuilder& PrefServiceBuilder::WithUserFilePrefs( 64 const base::FilePath& prefs_file, 65 base::SequencedTaskRunner* task_runner) { 66 user_prefs_ = new JsonPrefStore(prefs_file, task_runner); 67 return *this; 68 } 69 70 PrefServiceBuilder& PrefServiceBuilder::WithAsync(bool async) { 71 async_ = async; 72 return *this; 73 } 74 75 PrefService* PrefServiceBuilder::Create(PrefRegistry* pref_registry) { 76 PrefNotifierImpl* pref_notifier = new PrefNotifierImpl(); 77 PrefService* pref_service = 78 new PrefService(pref_notifier, 79 new PrefValueStore(managed_prefs_.get(), 80 extension_prefs_.get(), 81 command_line_prefs_.get(), 82 user_prefs_.get(), 83 recommended_prefs_.get(), 84 pref_registry->defaults().get(), 85 pref_notifier), 86 user_prefs_.get(), 87 pref_registry, 88 read_error_callback_, 89 async_); 90 ResetDefaultState(); 91 return pref_service; 92 } 93 94 void PrefServiceBuilder::ResetDefaultState() { 95 managed_prefs_ = NULL; 96 extension_prefs_ = NULL; 97 command_line_prefs_ = NULL; 98 user_prefs_ = NULL; 99 recommended_prefs_ = NULL; 100 read_error_callback_ = base::Bind(&DoNothingHandleReadError); 101 async_ = false; 102 } 103