OLD | NEW |
| (Empty) |
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::WithSupervisedUserPrefs( | |
36 PrefStore* store) { | |
37 supervised_user_prefs_ = store; | |
38 return *this; | |
39 } | |
40 | |
41 PrefServiceBuilder& PrefServiceBuilder::WithExtensionPrefs(PrefStore* store) { | |
42 extension_prefs_ = store; | |
43 return *this; | |
44 } | |
45 | |
46 PrefServiceBuilder& PrefServiceBuilder::WithCommandLinePrefs(PrefStore* store) { | |
47 command_line_prefs_ = store; | |
48 return *this; | |
49 } | |
50 | |
51 PrefServiceBuilder& PrefServiceBuilder::WithUserPrefs( | |
52 PersistentPrefStore* store) { | |
53 user_prefs_ = store; | |
54 return *this; | |
55 } | |
56 | |
57 PrefServiceBuilder& PrefServiceBuilder::WithRecommendedPrefs(PrefStore* store) { | |
58 recommended_prefs_ = store; | |
59 return *this; | |
60 } | |
61 | |
62 PrefServiceBuilder& PrefServiceBuilder::WithReadErrorCallback( | |
63 const base::Callback<void(PersistentPrefStore::PrefReadError)>& | |
64 read_error_callback) { | |
65 read_error_callback_ = read_error_callback; | |
66 return *this; | |
67 } | |
68 | |
69 PrefServiceBuilder& PrefServiceBuilder::WithUserFilePrefs( | |
70 const base::FilePath& prefs_file, | |
71 base::SequencedTaskRunner* task_runner) { | |
72 user_prefs_ = new JsonPrefStore(prefs_file, task_runner); | |
73 return *this; | |
74 } | |
75 | |
76 PrefServiceBuilder& PrefServiceBuilder::WithAsync(bool async) { | |
77 async_ = async; | |
78 return *this; | |
79 } | |
80 | |
81 PrefService* PrefServiceBuilder::Create(PrefRegistry* pref_registry) { | |
82 PrefNotifierImpl* pref_notifier = new PrefNotifierImpl(); | |
83 PrefService* pref_service = | |
84 new PrefService(pref_notifier, | |
85 new PrefValueStore(managed_prefs_.get(), | |
86 supervised_user_prefs_.get(), | |
87 extension_prefs_.get(), | |
88 command_line_prefs_.get(), | |
89 user_prefs_.get(), | |
90 recommended_prefs_.get(), | |
91 pref_registry->defaults().get(), | |
92 pref_notifier), | |
93 user_prefs_.get(), | |
94 pref_registry, | |
95 read_error_callback_, | |
96 async_); | |
97 ResetDefaultState(); | |
98 return pref_service; | |
99 } | |
100 | |
101 void PrefServiceBuilder::ResetDefaultState() { | |
102 managed_prefs_ = NULL; | |
103 supervised_user_prefs_ = NULL; | |
104 extension_prefs_ = NULL; | |
105 command_line_prefs_ = NULL; | |
106 user_prefs_ = NULL; | |
107 recommended_prefs_ = NULL; | |
108 read_error_callback_ = base::Bind(&DoNothingHandleReadError); | |
109 async_ = false; | |
110 } | |
OLD | NEW |