Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(328)

Side by Side Diff: base/prefs/pref_value_store.cc

Issue 1647803004: Move base to DEPS (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/prefs/pref_value_store.h ('k') | base/prefs/pref_value_store_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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_value_store.h"
6
7 #include "base/logging.h"
8 #include "base/prefs/pref_notifier.h"
9 #include "base/prefs/pref_observer.h"
10
11 PrefValueStore::PrefStoreKeeper::PrefStoreKeeper()
12 : pref_value_store_(NULL),
13 type_(PrefValueStore::INVALID_STORE) {
14 }
15
16 PrefValueStore::PrefStoreKeeper::~PrefStoreKeeper() {
17 if (pref_store_.get()) {
18 pref_store_->RemoveObserver(this);
19 pref_store_ = NULL;
20 }
21 pref_value_store_ = NULL;
22 }
23
24 void PrefValueStore::PrefStoreKeeper::Initialize(
25 PrefValueStore* store,
26 PrefStore* pref_store,
27 PrefValueStore::PrefStoreType type) {
28 if (pref_store_.get()) {
29 pref_store_->RemoveObserver(this);
30 DCHECK(!pref_store_->HasObservers());
31 }
32 type_ = type;
33 pref_value_store_ = store;
34 pref_store_ = pref_store;
35 if (pref_store_.get())
36 pref_store_->AddObserver(this);
37 }
38
39 void PrefValueStore::PrefStoreKeeper::OnPrefValueChanged(
40 const std::string& key) {
41 pref_value_store_->OnPrefValueChanged(type_, key);
42 }
43
44 void PrefValueStore::PrefStoreKeeper::OnInitializationCompleted(
45 bool succeeded) {
46 pref_value_store_->OnInitializationCompleted(type_, succeeded);
47 }
48
49 PrefValueStore::PrefValueStore(PrefStore* managed_prefs,
50 PrefStore* supervised_user_prefs,
51 PrefStore* extension_prefs,
52 PrefStore* command_line_prefs,
53 PrefStore* user_prefs,
54 PrefStore* recommended_prefs,
55 PrefStore* default_prefs,
56 PrefNotifier* pref_notifier)
57 : pref_notifier_(pref_notifier),
58 initialization_failed_(false) {
59 InitPrefStore(MANAGED_STORE, managed_prefs);
60 InitPrefStore(SUPERVISED_USER_STORE, supervised_user_prefs);
61 InitPrefStore(EXTENSION_STORE, extension_prefs);
62 InitPrefStore(COMMAND_LINE_STORE, command_line_prefs);
63 InitPrefStore(USER_STORE, user_prefs);
64 InitPrefStore(RECOMMENDED_STORE, recommended_prefs);
65 InitPrefStore(DEFAULT_STORE, default_prefs);
66
67 CheckInitializationCompleted();
68 }
69
70 PrefValueStore::~PrefValueStore() {}
71
72 PrefValueStore* PrefValueStore::CloneAndSpecialize(
73 PrefStore* managed_prefs,
74 PrefStore* supervised_user_prefs,
75 PrefStore* extension_prefs,
76 PrefStore* command_line_prefs,
77 PrefStore* user_prefs,
78 PrefStore* recommended_prefs,
79 PrefStore* default_prefs,
80 PrefNotifier* pref_notifier) {
81 DCHECK(pref_notifier);
82 if (!managed_prefs)
83 managed_prefs = GetPrefStore(MANAGED_STORE);
84 if (!supervised_user_prefs)
85 supervised_user_prefs = GetPrefStore(SUPERVISED_USER_STORE);
86 if (!extension_prefs)
87 extension_prefs = GetPrefStore(EXTENSION_STORE);
88 if (!command_line_prefs)
89 command_line_prefs = GetPrefStore(COMMAND_LINE_STORE);
90 if (!user_prefs)
91 user_prefs = GetPrefStore(USER_STORE);
92 if (!recommended_prefs)
93 recommended_prefs = GetPrefStore(RECOMMENDED_STORE);
94 if (!default_prefs)
95 default_prefs = GetPrefStore(DEFAULT_STORE);
96
97 return new PrefValueStore(
98 managed_prefs, supervised_user_prefs, extension_prefs, command_line_prefs,
99 user_prefs, recommended_prefs, default_prefs, pref_notifier);
100 }
101
102 void PrefValueStore::set_callback(const PrefChangedCallback& callback) {
103 pref_changed_callback_ = callback;
104 }
105
106 bool PrefValueStore::GetValue(const std::string& name,
107 base::Value::Type type,
108 const base::Value** out_value) const {
109 // Check the |PrefStore|s in order of their priority from highest to lowest,
110 // looking for the first preference value with the given |name| and |type|.
111 for (size_t i = 0; i <= PREF_STORE_TYPE_MAX; ++i) {
112 if (GetValueFromStoreWithType(name, type, static_cast<PrefStoreType>(i),
113 out_value))
114 return true;
115 }
116 return false;
117 }
118
119 bool PrefValueStore::GetRecommendedValue(const std::string& name,
120 base::Value::Type type,
121 const base::Value** out_value) const {
122 return GetValueFromStoreWithType(name, type, RECOMMENDED_STORE, out_value);
123 }
124
125 void PrefValueStore::NotifyPrefChanged(
126 const std::string& path,
127 PrefValueStore::PrefStoreType new_store) {
128 DCHECK(new_store != INVALID_STORE);
129 // A notification is sent when the pref value in any store changes. If this
130 // store is currently being overridden by a higher-priority store, the
131 // effective value of the pref will not have changed.
132 pref_notifier_->OnPreferenceChanged(path);
133 if (!pref_changed_callback_.is_null())
134 pref_changed_callback_.Run(path);
135 }
136
137 bool PrefValueStore::PrefValueInManagedStore(const std::string& name) const {
138 return PrefValueInStore(name, MANAGED_STORE);
139 }
140
141 bool PrefValueStore::PrefValueInSupervisedStore(const std::string& name) const {
142 return PrefValueInStore(name, SUPERVISED_USER_STORE);
143 }
144
145 bool PrefValueStore::PrefValueInExtensionStore(const std::string& name) const {
146 return PrefValueInStore(name, EXTENSION_STORE);
147 }
148
149 bool PrefValueStore::PrefValueInUserStore(const std::string& name) const {
150 return PrefValueInStore(name, USER_STORE);
151 }
152
153 bool PrefValueStore::PrefValueFromExtensionStore(
154 const std::string& name) const {
155 return ControllingPrefStoreForPref(name) == EXTENSION_STORE;
156 }
157
158 bool PrefValueStore::PrefValueFromUserStore(const std::string& name) const {
159 return ControllingPrefStoreForPref(name) == USER_STORE;
160 }
161
162 bool PrefValueStore::PrefValueFromRecommendedStore(
163 const std::string& name) const {
164 return ControllingPrefStoreForPref(name) == RECOMMENDED_STORE;
165 }
166
167 bool PrefValueStore::PrefValueFromDefaultStore(const std::string& name) const {
168 return ControllingPrefStoreForPref(name) == DEFAULT_STORE;
169 }
170
171 bool PrefValueStore::PrefValueUserModifiable(const std::string& name) const {
172 PrefStoreType effective_store = ControllingPrefStoreForPref(name);
173 return effective_store >= USER_STORE ||
174 effective_store == INVALID_STORE;
175 }
176
177 bool PrefValueStore::PrefValueExtensionModifiable(
178 const std::string& name) const {
179 PrefStoreType effective_store = ControllingPrefStoreForPref(name);
180 return effective_store >= EXTENSION_STORE ||
181 effective_store == INVALID_STORE;
182 }
183
184 void PrefValueStore::UpdateCommandLinePrefStore(PrefStore* command_line_prefs) {
185 InitPrefStore(COMMAND_LINE_STORE, command_line_prefs);
186 }
187
188 bool PrefValueStore::PrefValueInStore(
189 const std::string& name,
190 PrefValueStore::PrefStoreType store) const {
191 // Declare a temp Value* and call GetValueFromStore,
192 // ignoring the output value.
193 const base::Value* tmp_value = NULL;
194 return GetValueFromStore(name, store, &tmp_value);
195 }
196
197 bool PrefValueStore::PrefValueInStoreRange(
198 const std::string& name,
199 PrefValueStore::PrefStoreType first_checked_store,
200 PrefValueStore::PrefStoreType last_checked_store) const {
201 if (first_checked_store > last_checked_store) {
202 NOTREACHED();
203 return false;
204 }
205
206 for (size_t i = first_checked_store;
207 i <= static_cast<size_t>(last_checked_store); ++i) {
208 if (PrefValueInStore(name, static_cast<PrefStoreType>(i)))
209 return true;
210 }
211 return false;
212 }
213
214 PrefValueStore::PrefStoreType PrefValueStore::ControllingPrefStoreForPref(
215 const std::string& name) const {
216 for (size_t i = 0; i <= PREF_STORE_TYPE_MAX; ++i) {
217 if (PrefValueInStore(name, static_cast<PrefStoreType>(i)))
218 return static_cast<PrefStoreType>(i);
219 }
220 return INVALID_STORE;
221 }
222
223 bool PrefValueStore::GetValueFromStore(const std::string& name,
224 PrefValueStore::PrefStoreType store_type,
225 const base::Value** out_value) const {
226 // Only return true if we find a value and it is the correct type, so stale
227 // values with the incorrect type will be ignored.
228 const PrefStore* store = GetPrefStore(static_cast<PrefStoreType>(store_type));
229 if (store && store->GetValue(name, out_value))
230 return true;
231
232 // No valid value found for the given preference name: set the return value
233 // to false.
234 *out_value = NULL;
235 return false;
236 }
237
238 bool PrefValueStore::GetValueFromStoreWithType(
239 const std::string& name,
240 base::Value::Type type,
241 PrefStoreType store,
242 const base::Value** out_value) const {
243 if (GetValueFromStore(name, store, out_value)) {
244 if ((*out_value)->IsType(type))
245 return true;
246
247 LOG(WARNING) << "Expected type for " << name << " is " << type
248 << " but got " << (*out_value)->GetType()
249 << " in store " << store;
250 }
251
252 *out_value = NULL;
253 return false;
254 }
255
256 void PrefValueStore::OnPrefValueChanged(PrefValueStore::PrefStoreType type,
257 const std::string& key) {
258 NotifyPrefChanged(key, type);
259 }
260
261 void PrefValueStore::OnInitializationCompleted(
262 PrefValueStore::PrefStoreType type, bool succeeded) {
263 if (initialization_failed_)
264 return;
265 if (!succeeded) {
266 initialization_failed_ = true;
267 pref_notifier_->OnInitializationCompleted(false);
268 return;
269 }
270 CheckInitializationCompleted();
271 }
272
273 void PrefValueStore::InitPrefStore(PrefValueStore::PrefStoreType type,
274 PrefStore* pref_store) {
275 pref_stores_[type].Initialize(this, pref_store, type);
276 }
277
278 void PrefValueStore::CheckInitializationCompleted() {
279 if (initialization_failed_)
280 return;
281 for (size_t i = 0; i <= PREF_STORE_TYPE_MAX; ++i) {
282 scoped_refptr<PrefStore> store =
283 GetPrefStore(static_cast<PrefStoreType>(i));
284 if (store.get() && !store->IsInitializationComplete())
285 return;
286 }
287 pref_notifier_->OnInitializationCompleted(true);
288 }
OLDNEW
« no previous file with comments | « base/prefs/pref_value_store.h ('k') | base/prefs/pref_value_store_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698