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 "base/prefs/pref_value_store.h" | 5 #include "base/prefs/pref_value_store.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/prefs/pref_notifier.h" | 8 #include "base/prefs/pref_notifier.h" |
9 #include "base/prefs/pref_observer.h" | 9 #include "base/prefs/pref_observer.h" |
10 | 10 |
(...skipping 91 matching lines...) Loading... |
102 void PrefValueStore::set_callback(const PrefChangedCallback& callback) { | 102 void PrefValueStore::set_callback(const PrefChangedCallback& callback) { |
103 pref_changed_callback_ = callback; | 103 pref_changed_callback_ = callback; |
104 } | 104 } |
105 | 105 |
106 bool PrefValueStore::GetValue(const std::string& name, | 106 bool PrefValueStore::GetValue(const std::string& name, |
107 base::Value::Type type, | 107 base::Value::Type type, |
108 const base::Value** out_value) const { | 108 const base::Value** out_value) const { |
109 // Check the |PrefStore|s in order of their priority from highest to lowest, | 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|. | 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) { | 111 for (size_t i = 0; i <= PREF_STORE_TYPE_MAX; ++i) { |
112 if (GetValueFromStoreWithType(name.c_str(), type, | 112 if (GetValueFromStoreWithType(name, type, static_cast<PrefStoreType>(i), |
113 static_cast<PrefStoreType>(i), out_value)) | 113 out_value)) |
114 return true; | 114 return true; |
115 } | 115 } |
116 return false; | 116 return false; |
117 } | 117 } |
118 | 118 |
119 bool PrefValueStore::GetRecommendedValue(const std::string& name, | 119 bool PrefValueStore::GetRecommendedValue(const std::string& name, |
120 base::Value::Type type, | 120 base::Value::Type type, |
121 const base::Value** out_value) const { | 121 const base::Value** out_value) const { |
122 return GetValueFromStoreWithType(name.c_str(), type, RECOMMENDED_STORE, | 122 return GetValueFromStoreWithType(name, type, RECOMMENDED_STORE, out_value); |
123 out_value); | |
124 } | 123 } |
125 | 124 |
126 void PrefValueStore::NotifyPrefChanged( | 125 void PrefValueStore::NotifyPrefChanged( |
127 const char* path, | 126 const std::string& path, |
128 PrefValueStore::PrefStoreType new_store) { | 127 PrefValueStore::PrefStoreType new_store) { |
129 DCHECK(new_store != INVALID_STORE); | 128 DCHECK(new_store != INVALID_STORE); |
130 // A notification is sent when the pref value in any store changes. If this | 129 // A notification is sent when the pref value in any store changes. If this |
131 // store is currently being overridden by a higher-priority store, the | 130 // store is currently being overridden by a higher-priority store, the |
132 // effective value of the pref will not have changed. | 131 // effective value of the pref will not have changed. |
133 pref_notifier_->OnPreferenceChanged(path); | 132 pref_notifier_->OnPreferenceChanged(path); |
134 if (!pref_changed_callback_.is_null()) | 133 if (!pref_changed_callback_.is_null()) |
135 pref_changed_callback_.Run(path); | 134 pref_changed_callback_.Run(path); |
136 } | 135 } |
137 | 136 |
138 bool PrefValueStore::PrefValueInManagedStore(const char* name) const { | 137 bool PrefValueStore::PrefValueInManagedStore(const std::string& name) const { |
139 return PrefValueInStore(name, MANAGED_STORE); | 138 return PrefValueInStore(name, MANAGED_STORE); |
140 } | 139 } |
141 | 140 |
142 bool PrefValueStore::PrefValueInExtensionStore(const char* name) const { | 141 bool PrefValueStore::PrefValueInExtensionStore(const std::string& name) const { |
143 return PrefValueInStore(name, EXTENSION_STORE); | 142 return PrefValueInStore(name, EXTENSION_STORE); |
144 } | 143 } |
145 | 144 |
146 bool PrefValueStore::PrefValueInUserStore(const char* name) const { | 145 bool PrefValueStore::PrefValueInUserStore(const std::string& name) const { |
147 return PrefValueInStore(name, USER_STORE); | 146 return PrefValueInStore(name, USER_STORE); |
148 } | 147 } |
149 | 148 |
150 bool PrefValueStore::PrefValueFromExtensionStore(const char* name) const { | 149 bool PrefValueStore::PrefValueFromExtensionStore( |
| 150 const std::string& name) const { |
151 return ControllingPrefStoreForPref(name) == EXTENSION_STORE; | 151 return ControllingPrefStoreForPref(name) == EXTENSION_STORE; |
152 } | 152 } |
153 | 153 |
154 bool PrefValueStore::PrefValueFromUserStore(const char* name) const { | 154 bool PrefValueStore::PrefValueFromUserStore(const std::string& name) const { |
155 return ControllingPrefStoreForPref(name) == USER_STORE; | 155 return ControllingPrefStoreForPref(name) == USER_STORE; |
156 } | 156 } |
157 | 157 |
158 bool PrefValueStore::PrefValueFromRecommendedStore(const char* name) const { | 158 bool PrefValueStore::PrefValueFromRecommendedStore( |
| 159 const std::string& name) const { |
159 return ControllingPrefStoreForPref(name) == RECOMMENDED_STORE; | 160 return ControllingPrefStoreForPref(name) == RECOMMENDED_STORE; |
160 } | 161 } |
161 | 162 |
162 bool PrefValueStore::PrefValueFromDefaultStore(const char* name) const { | 163 bool PrefValueStore::PrefValueFromDefaultStore(const std::string& name) const { |
163 return ControllingPrefStoreForPref(name) == DEFAULT_STORE; | 164 return ControllingPrefStoreForPref(name) == DEFAULT_STORE; |
164 } | 165 } |
165 | 166 |
166 bool PrefValueStore::PrefValueUserModifiable(const char* name) const { | 167 bool PrefValueStore::PrefValueUserModifiable(const std::string& name) const { |
167 PrefStoreType effective_store = ControllingPrefStoreForPref(name); | 168 PrefStoreType effective_store = ControllingPrefStoreForPref(name); |
168 return effective_store >= USER_STORE || | 169 return effective_store >= USER_STORE || |
169 effective_store == INVALID_STORE; | 170 effective_store == INVALID_STORE; |
170 } | 171 } |
171 | 172 |
172 bool PrefValueStore::PrefValueExtensionModifiable(const char* name) const { | 173 bool PrefValueStore::PrefValueExtensionModifiable( |
| 174 const std::string& name) const { |
173 PrefStoreType effective_store = ControllingPrefStoreForPref(name); | 175 PrefStoreType effective_store = ControllingPrefStoreForPref(name); |
174 return effective_store >= EXTENSION_STORE || | 176 return effective_store >= EXTENSION_STORE || |
175 effective_store == INVALID_STORE; | 177 effective_store == INVALID_STORE; |
176 } | 178 } |
177 | 179 |
178 void PrefValueStore::UpdateCommandLinePrefStore(PrefStore* command_line_prefs) { | 180 void PrefValueStore::UpdateCommandLinePrefStore(PrefStore* command_line_prefs) { |
179 InitPrefStore(COMMAND_LINE_STORE, command_line_prefs); | 181 InitPrefStore(COMMAND_LINE_STORE, command_line_prefs); |
180 } | 182 } |
181 | 183 |
182 bool PrefValueStore::PrefValueInStore( | 184 bool PrefValueStore::PrefValueInStore( |
183 const char* name, | 185 const std::string& name, |
184 PrefValueStore::PrefStoreType store) const { | 186 PrefValueStore::PrefStoreType store) const { |
185 // Declare a temp Value* and call GetValueFromStore, | 187 // Declare a temp Value* and call GetValueFromStore, |
186 // ignoring the output value. | 188 // ignoring the output value. |
187 const base::Value* tmp_value = NULL; | 189 const base::Value* tmp_value = NULL; |
188 return GetValueFromStore(name, store, &tmp_value); | 190 return GetValueFromStore(name, store, &tmp_value); |
189 } | 191 } |
190 | 192 |
191 bool PrefValueStore::PrefValueInStoreRange( | 193 bool PrefValueStore::PrefValueInStoreRange( |
192 const char* name, | 194 const std::string& name, |
193 PrefValueStore::PrefStoreType first_checked_store, | 195 PrefValueStore::PrefStoreType first_checked_store, |
194 PrefValueStore::PrefStoreType last_checked_store) const { | 196 PrefValueStore::PrefStoreType last_checked_store) const { |
195 if (first_checked_store > last_checked_store) { | 197 if (first_checked_store > last_checked_store) { |
196 NOTREACHED(); | 198 NOTREACHED(); |
197 return false; | 199 return false; |
198 } | 200 } |
199 | 201 |
200 for (size_t i = first_checked_store; | 202 for (size_t i = first_checked_store; |
201 i <= static_cast<size_t>(last_checked_store); ++i) { | 203 i <= static_cast<size_t>(last_checked_store); ++i) { |
202 if (PrefValueInStore(name, static_cast<PrefStoreType>(i))) | 204 if (PrefValueInStore(name, static_cast<PrefStoreType>(i))) |
203 return true; | 205 return true; |
204 } | 206 } |
205 return false; | 207 return false; |
206 } | 208 } |
207 | 209 |
208 PrefValueStore::PrefStoreType PrefValueStore::ControllingPrefStoreForPref( | 210 PrefValueStore::PrefStoreType PrefValueStore::ControllingPrefStoreForPref( |
209 const char* name) const { | 211 const std::string& name) const { |
210 for (size_t i = 0; i <= PREF_STORE_TYPE_MAX; ++i) { | 212 for (size_t i = 0; i <= PREF_STORE_TYPE_MAX; ++i) { |
211 if (PrefValueInStore(name, static_cast<PrefStoreType>(i))) | 213 if (PrefValueInStore(name, static_cast<PrefStoreType>(i))) |
212 return static_cast<PrefStoreType>(i); | 214 return static_cast<PrefStoreType>(i); |
213 } | 215 } |
214 return INVALID_STORE; | 216 return INVALID_STORE; |
215 } | 217 } |
216 | 218 |
217 bool PrefValueStore::GetValueFromStore(const char* name, | 219 bool PrefValueStore::GetValueFromStore(const std::string& name, |
218 PrefValueStore::PrefStoreType store_type, | 220 PrefValueStore::PrefStoreType store_type, |
219 const base::Value** out_value) const { | 221 const base::Value** out_value) const { |
220 // Only return true if we find a value and it is the correct type, so stale | 222 // Only return true if we find a value and it is the correct type, so stale |
221 // values with the incorrect type will be ignored. | 223 // values with the incorrect type will be ignored. |
222 const PrefStore* store = GetPrefStore(static_cast<PrefStoreType>(store_type)); | 224 const PrefStore* store = GetPrefStore(static_cast<PrefStoreType>(store_type)); |
223 if (store && store->GetValue(name, out_value)) | 225 if (store && store->GetValue(name, out_value)) |
224 return true; | 226 return true; |
225 | 227 |
226 // No valid value found for the given preference name: set the return value | 228 // No valid value found for the given preference name: set the return value |
227 // to false. | 229 // to false. |
228 *out_value = NULL; | 230 *out_value = NULL; |
229 return false; | 231 return false; |
230 } | 232 } |
231 | 233 |
232 bool PrefValueStore::GetValueFromStoreWithType( | 234 bool PrefValueStore::GetValueFromStoreWithType( |
233 const char* name, | 235 const std::string& name, |
234 base::Value::Type type, | 236 base::Value::Type type, |
235 PrefStoreType store, | 237 PrefStoreType store, |
236 const base::Value** out_value) const { | 238 const base::Value** out_value) const { |
237 if (GetValueFromStore(name, store, out_value)) { | 239 if (GetValueFromStore(name, store, out_value)) { |
238 if ((*out_value)->IsType(type)) | 240 if ((*out_value)->IsType(type)) |
239 return true; | 241 return true; |
240 | 242 |
241 LOG(WARNING) << "Expected type for " << name << " is " << type | 243 LOG(WARNING) << "Expected type for " << name << " is " << type |
242 << " but got " << (*out_value)->GetType() | 244 << " but got " << (*out_value)->GetType() |
243 << " in store " << store; | 245 << " in store " << store; |
244 } | 246 } |
245 | 247 |
246 *out_value = NULL; | 248 *out_value = NULL; |
247 return false; | 249 return false; |
248 } | 250 } |
249 | 251 |
250 void PrefValueStore::OnPrefValueChanged(PrefValueStore::PrefStoreType type, | 252 void PrefValueStore::OnPrefValueChanged(PrefValueStore::PrefStoreType type, |
251 const std::string& key) { | 253 const std::string& key) { |
252 NotifyPrefChanged(key.c_str(), type); | 254 NotifyPrefChanged(key, type); |
253 } | 255 } |
254 | 256 |
255 void PrefValueStore::OnInitializationCompleted( | 257 void PrefValueStore::OnInitializationCompleted( |
256 PrefValueStore::PrefStoreType type, bool succeeded) { | 258 PrefValueStore::PrefStoreType type, bool succeeded) { |
257 if (initialization_failed_) | 259 if (initialization_failed_) |
258 return; | 260 return; |
259 if (!succeeded) { | 261 if (!succeeded) { |
260 initialization_failed_ = true; | 262 initialization_failed_ = true; |
261 pref_notifier_->OnInitializationCompleted(false); | 263 pref_notifier_->OnInitializationCompleted(false); |
262 return; | 264 return; |
(...skipping 10 matching lines...) Loading... |
273 if (initialization_failed_) | 275 if (initialization_failed_) |
274 return; | 276 return; |
275 for (size_t i = 0; i <= PREF_STORE_TYPE_MAX; ++i) { | 277 for (size_t i = 0; i <= PREF_STORE_TYPE_MAX; ++i) { |
276 scoped_refptr<PrefStore> store = | 278 scoped_refptr<PrefStore> store = |
277 GetPrefStore(static_cast<PrefStoreType>(i)); | 279 GetPrefStore(static_cast<PrefStoreType>(i)); |
278 if (store.get() && !store->IsInitializationComplete()) | 280 if (store.get() && !store->IsInitializationComplete()) |
279 return; | 281 return; |
280 } | 282 } |
281 pref_notifier_->OnInitializationCompleted(true); | 283 pref_notifier_->OnInitializationCompleted(true); |
282 } | 284 } |
OLD | NEW |