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 "chrome/browser/prefs/pref_service.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/file_path.h" | |
11 #include "base/logging.h" | |
12 #include "base/message_loop.h" | |
13 #include "base/metrics/histogram.h" | |
14 #include "base/prefs/default_pref_store.h" | |
15 #include "base/stl_util.h" | |
16 #include "base/string_util.h" | |
17 #include "base/strings/string_number_conversions.h" | |
18 #include "base/value_conversions.h" | |
19 #include "build/build_config.h" | |
20 #include "chrome/browser/prefs/pref_notifier_impl.h" | |
21 #include "chrome/browser/prefs/pref_registry.h" | |
22 #include "chrome/browser/prefs/pref_value_store.h" | |
23 | |
24 using content::BrowserContext; | |
25 | |
26 namespace { | |
27 | |
28 class ReadErrorHandler : public PersistentPrefStore::ReadErrorDelegate { | |
29 public: | |
30 ReadErrorHandler(base::Callback<void(PersistentPrefStore::PrefReadError)> cb) | |
31 : callback_(cb) {} | |
32 | |
33 virtual void OnError(PersistentPrefStore::PrefReadError error) OVERRIDE { | |
34 callback_.Run(error); | |
35 } | |
36 | |
37 private: | |
38 base::Callback<void(PersistentPrefStore::PrefReadError)> callback_; | |
39 }; | |
40 | |
41 } // namespace | |
42 | |
43 PrefService::PrefService( | |
44 PrefNotifierImpl* pref_notifier, | |
45 PrefValueStore* pref_value_store, | |
46 PersistentPrefStore* user_prefs, | |
47 PrefRegistry* pref_registry, | |
48 base::Callback<void(PersistentPrefStore::PrefReadError)> | |
49 read_error_callback, | |
50 bool async) | |
51 : pref_notifier_(pref_notifier), | |
52 pref_value_store_(pref_value_store), | |
53 pref_registry_(pref_registry), | |
54 user_pref_store_(user_prefs), | |
55 read_error_callback_(read_error_callback) { | |
56 pref_notifier_->SetPrefService(this); | |
57 | |
58 pref_registry_->SetRegistrationCallback( | |
59 base::Bind(&PrefService::AddRegisteredPreference, | |
60 base::Unretained(this))); | |
61 pref_registry_->SetUnregistrationCallback( | |
62 base::Bind(&PrefService::RemoveRegisteredPreference, | |
63 base::Unretained(this))); | |
64 AddInitialPreferences(); | |
65 | |
66 InitFromStorage(async); | |
67 } | |
68 | |
69 PrefService::~PrefService() { | |
70 DCHECK(CalledOnValidThread()); | |
71 | |
72 // Remove our callbacks, setting NULL ones. | |
73 pref_registry_->SetRegistrationCallback(PrefRegistry::RegistrationCallback()); | |
74 pref_registry_->SetUnregistrationCallback( | |
75 PrefRegistry::UnregistrationCallback()); | |
76 | |
77 // Reset pointers so accesses after destruction reliably crash. | |
78 pref_value_store_.reset(); | |
79 pref_registry_ = NULL; | |
80 user_pref_store_ = NULL; | |
81 pref_notifier_.reset(); | |
82 } | |
83 | |
84 void PrefService::InitFromStorage(bool async) { | |
85 if (!async) { | |
86 read_error_callback_.Run(user_pref_store_->ReadPrefs()); | |
87 } else { | |
88 // Guarantee that initialization happens after this function returned. | |
89 MessageLoop::current()->PostTask( | |
90 FROM_HERE, | |
91 base::Bind(&PersistentPrefStore::ReadPrefsAsync, | |
92 user_pref_store_.get(), | |
93 new ReadErrorHandler(read_error_callback_))); | |
94 } | |
95 } | |
96 | |
97 bool PrefService::ReloadPersistentPrefs() { | |
98 return user_pref_store_->ReadPrefs() == | |
99 PersistentPrefStore::PREF_READ_ERROR_NONE; | |
100 } | |
101 | |
102 void PrefService::CommitPendingWrite() { | |
103 DCHECK(CalledOnValidThread()); | |
104 user_pref_store_->CommitPendingWrite(); | |
105 } | |
106 | |
107 bool PrefService::GetBoolean(const char* path) const { | |
108 DCHECK(CalledOnValidThread()); | |
109 | |
110 bool result = false; | |
111 | |
112 const base::Value* value = GetPreferenceValue(path); | |
113 if (!value) { | |
114 NOTREACHED() << "Trying to read an unregistered pref: " << path; | |
115 return result; | |
116 } | |
117 bool rv = value->GetAsBoolean(&result); | |
118 DCHECK(rv); | |
119 return result; | |
120 } | |
121 | |
122 int PrefService::GetInteger(const char* path) const { | |
123 DCHECK(CalledOnValidThread()); | |
124 | |
125 int result = 0; | |
126 | |
127 const base::Value* value = GetPreferenceValue(path); | |
128 if (!value) { | |
129 NOTREACHED() << "Trying to read an unregistered pref: " << path; | |
130 return result; | |
131 } | |
132 bool rv = value->GetAsInteger(&result); | |
133 DCHECK(rv); | |
134 return result; | |
135 } | |
136 | |
137 double PrefService::GetDouble(const char* path) const { | |
138 DCHECK(CalledOnValidThread()); | |
139 | |
140 double result = 0.0; | |
141 | |
142 const base::Value* value = GetPreferenceValue(path); | |
143 if (!value) { | |
144 NOTREACHED() << "Trying to read an unregistered pref: " << path; | |
145 return result; | |
146 } | |
147 bool rv = value->GetAsDouble(&result); | |
148 DCHECK(rv); | |
149 return result; | |
150 } | |
151 | |
152 std::string PrefService::GetString(const char* path) const { | |
153 DCHECK(CalledOnValidThread()); | |
154 | |
155 std::string result; | |
156 | |
157 const base::Value* value = GetPreferenceValue(path); | |
158 if (!value) { | |
159 NOTREACHED() << "Trying to read an unregistered pref: " << path; | |
160 return result; | |
161 } | |
162 bool rv = value->GetAsString(&result); | |
163 DCHECK(rv); | |
164 return result; | |
165 } | |
166 | |
167 base::FilePath PrefService::GetFilePath(const char* path) const { | |
168 DCHECK(CalledOnValidThread()); | |
169 | |
170 base::FilePath result; | |
171 | |
172 const base::Value* value = GetPreferenceValue(path); | |
173 if (!value) { | |
174 NOTREACHED() << "Trying to read an unregistered pref: " << path; | |
175 return base::FilePath(result); | |
176 } | |
177 bool rv = base::GetValueAsFilePath(*value, &result); | |
178 DCHECK(rv); | |
179 return result; | |
180 } | |
181 | |
182 bool PrefService::HasPrefPath(const char* path) const { | |
183 const Preference* pref = FindPreference(path); | |
184 return pref && !pref->IsDefaultValue(); | |
185 } | |
186 | |
187 DictionaryValue* PrefService::GetPreferenceValues() const { | |
188 DCHECK(CalledOnValidThread()); | |
189 DictionaryValue* out = new DictionaryValue; | |
190 PrefRegistry::const_iterator i = pref_registry_->begin(); | |
191 for (; i != pref_registry_->end(); ++i) { | |
192 const Value* value = GetPreferenceValue(i->first); | |
193 DCHECK(value); | |
194 out->Set(i->first, value->DeepCopy()); | |
195 } | |
196 return out; | |
197 } | |
198 | |
199 const PrefService::Preference* PrefService::FindPreference( | |
200 const char* pref_name) const { | |
201 DCHECK(CalledOnValidThread()); | |
202 PreferenceMap::iterator it = prefs_map_.find(pref_name); | |
203 if (it != prefs_map_.end()) | |
204 return &(it->second); | |
205 const base::Value* default_value = NULL; | |
206 if (!pref_registry_->defaults()->GetValue(pref_name, &default_value)) | |
207 return NULL; | |
208 it = prefs_map_.insert( | |
209 std::make_pair(pref_name, Preference( | |
210 this, pref_name, default_value->GetType()))).first; | |
211 return &(it->second); | |
212 } | |
213 | |
214 bool PrefService::ReadOnly() const { | |
215 return user_pref_store_->ReadOnly(); | |
216 } | |
217 | |
218 PrefService::PrefInitializationStatus PrefService::GetInitializationStatus() | |
219 const { | |
220 if (!user_pref_store_->IsInitializationComplete()) | |
221 return INITIALIZATION_STATUS_WAITING; | |
222 | |
223 switch (user_pref_store_->GetReadError()) { | |
224 case PersistentPrefStore::PREF_READ_ERROR_NONE: | |
225 return INITIALIZATION_STATUS_SUCCESS; | |
226 case PersistentPrefStore::PREF_READ_ERROR_NO_FILE: | |
227 return INITIALIZATION_STATUS_CREATED_NEW_PROFILE; | |
228 default: | |
229 return INITIALIZATION_STATUS_ERROR; | |
230 } | |
231 } | |
232 | |
233 bool PrefService::IsManagedPreference(const char* pref_name) const { | |
234 const Preference* pref = FindPreference(pref_name); | |
235 return pref && pref->IsManaged(); | |
236 } | |
237 | |
238 bool PrefService::IsUserModifiablePreference(const char* pref_name) const { | |
239 const Preference* pref = FindPreference(pref_name); | |
240 return pref && pref->IsUserModifiable(); | |
241 } | |
242 | |
243 const DictionaryValue* PrefService::GetDictionary(const char* path) const { | |
244 DCHECK(CalledOnValidThread()); | |
245 | |
246 const Value* value = GetPreferenceValue(path); | |
247 if (!value) { | |
248 NOTREACHED() << "Trying to read an unregistered pref: " << path; | |
249 return NULL; | |
250 } | |
251 if (value->GetType() != Value::TYPE_DICTIONARY) { | |
252 NOTREACHED(); | |
253 return NULL; | |
254 } | |
255 return static_cast<const DictionaryValue*>(value); | |
256 } | |
257 | |
258 const base::Value* PrefService::GetUserPrefValue(const char* path) const { | |
259 DCHECK(CalledOnValidThread()); | |
260 | |
261 const Preference* pref = FindPreference(path); | |
262 if (!pref) { | |
263 NOTREACHED() << "Trying to get an unregistered pref: " << path; | |
264 return NULL; | |
265 } | |
266 | |
267 // Look for an existing preference in the user store. If it doesn't | |
268 // exist, return NULL. | |
269 base::Value* value = NULL; | |
270 if (!user_pref_store_->GetMutableValue(path, &value)) | |
271 return NULL; | |
272 | |
273 if (!value->IsType(pref->GetType())) { | |
274 NOTREACHED() << "Pref value type doesn't match registered type."; | |
275 return NULL; | |
276 } | |
277 | |
278 return value; | |
279 } | |
280 | |
281 const base::Value* PrefService::GetDefaultPrefValue(const char* path) const { | |
282 DCHECK(CalledOnValidThread()); | |
283 // Lookup the preference in the default store. | |
284 const base::Value* value = NULL; | |
285 if (!pref_registry_->defaults()->GetValue(path, &value)) { | |
286 NOTREACHED() << "Default value missing for pref: " << path; | |
287 return NULL; | |
288 } | |
289 return value; | |
290 } | |
291 | |
292 const ListValue* PrefService::GetList(const char* path) const { | |
293 DCHECK(CalledOnValidThread()); | |
294 | |
295 const Value* value = GetPreferenceValue(path); | |
296 if (!value) { | |
297 NOTREACHED() << "Trying to read an unregistered pref: " << path; | |
298 return NULL; | |
299 } | |
300 if (value->GetType() != Value::TYPE_LIST) { | |
301 NOTREACHED(); | |
302 return NULL; | |
303 } | |
304 return static_cast<const ListValue*>(value); | |
305 } | |
306 | |
307 void PrefService::AddPrefObserver(const char* path, PrefObserver* obs) { | |
308 pref_notifier_->AddPrefObserver(path, obs); | |
309 } | |
310 | |
311 void PrefService::RemovePrefObserver(const char* path, PrefObserver* obs) { | |
312 pref_notifier_->RemovePrefObserver(path, obs); | |
313 } | |
314 | |
315 void PrefService::AddPrefInitObserver(base::Callback<void(bool)> obs) { | |
316 pref_notifier_->AddInitObserver(obs); | |
317 } | |
318 | |
319 PrefRegistry* PrefService::DeprecatedGetPrefRegistry() { | |
320 return pref_registry_.get(); | |
321 } | |
322 | |
323 void PrefService::AddInitialPreferences() { | |
324 for (PrefRegistry::const_iterator it = pref_registry_->begin(); | |
325 it != pref_registry_->end(); | |
326 ++it) { | |
327 AddRegisteredPreference(it->first.c_str(), it->second); | |
328 } | |
329 } | |
330 | |
331 // TODO(joi): Once MarkNeedsEmptyValue is gone, we can probably | |
332 // completely get rid of this method. There will be one difference in | |
333 // semantics; currently all registered preferences are stored right | |
334 // away in the prefs_map_, if we remove this they would be stored only | |
335 // opportunistically. | |
336 void PrefService::AddRegisteredPreference(const char* path, | |
337 Value* default_value) { | |
338 DCHECK(CalledOnValidThread()); | |
339 | |
340 // For ListValue and DictionaryValue with non empty default, empty value | |
341 // for |path| needs to be persisted in |user_pref_store_|. So that | |
342 // non empty default is not used when user sets an empty ListValue or | |
343 // DictionaryValue. | |
344 bool needs_empty_value = false; | |
345 base::Value::Type orig_type = default_value->GetType(); | |
346 if (orig_type == base::Value::TYPE_LIST) { | |
347 const base::ListValue* list = NULL; | |
348 if (default_value->GetAsList(&list) && !list->empty()) | |
349 needs_empty_value = true; | |
350 } else if (orig_type == base::Value::TYPE_DICTIONARY) { | |
351 const base::DictionaryValue* dict = NULL; | |
352 if (default_value->GetAsDictionary(&dict) && !dict->empty()) | |
353 needs_empty_value = true; | |
354 } | |
355 if (needs_empty_value) | |
356 user_pref_store_->MarkNeedsEmptyValue(path); | |
357 } | |
358 | |
359 // TODO(joi): We can get rid of this once the ability to unregister | |
360 // prefs has been removed. | |
361 void PrefService::RemoveRegisteredPreference(const char* path) { | |
362 DCHECK(CalledOnValidThread()); | |
363 | |
364 prefs_map_.erase(path); | |
365 } | |
366 | |
367 void PrefService::ClearPref(const char* path) { | |
368 DCHECK(CalledOnValidThread()); | |
369 | |
370 const Preference* pref = FindPreference(path); | |
371 if (!pref) { | |
372 NOTREACHED() << "Trying to clear an unregistered pref: " << path; | |
373 return; | |
374 } | |
375 user_pref_store_->RemoveValue(path); | |
376 } | |
377 | |
378 void PrefService::Set(const char* path, const Value& value) { | |
379 SetUserPrefValue(path, value.DeepCopy()); | |
380 } | |
381 | |
382 void PrefService::SetBoolean(const char* path, bool value) { | |
383 SetUserPrefValue(path, Value::CreateBooleanValue(value)); | |
384 } | |
385 | |
386 void PrefService::SetInteger(const char* path, int value) { | |
387 SetUserPrefValue(path, Value::CreateIntegerValue(value)); | |
388 } | |
389 | |
390 void PrefService::SetDouble(const char* path, double value) { | |
391 SetUserPrefValue(path, Value::CreateDoubleValue(value)); | |
392 } | |
393 | |
394 void PrefService::SetString(const char* path, const std::string& value) { | |
395 SetUserPrefValue(path, Value::CreateStringValue(value)); | |
396 } | |
397 | |
398 void PrefService::SetFilePath(const char* path, const base::FilePath& value) { | |
399 SetUserPrefValue(path, base::CreateFilePathValue(value)); | |
400 } | |
401 | |
402 void PrefService::SetInt64(const char* path, int64 value) { | |
403 SetUserPrefValue(path, Value::CreateStringValue(base::Int64ToString(value))); | |
404 } | |
405 | |
406 int64 PrefService::GetInt64(const char* path) const { | |
407 DCHECK(CalledOnValidThread()); | |
408 | |
409 const Value* value = GetPreferenceValue(path); | |
410 if (!value) { | |
411 NOTREACHED() << "Trying to read an unregistered pref: " << path; | |
412 return 0; | |
413 } | |
414 std::string result("0"); | |
415 bool rv = value->GetAsString(&result); | |
416 DCHECK(rv); | |
417 | |
418 int64 val; | |
419 base::StringToInt64(result, &val); | |
420 return val; | |
421 } | |
422 | |
423 void PrefService::SetUint64(const char* path, uint64 value) { | |
424 SetUserPrefValue(path, Value::CreateStringValue(base::Uint64ToString(value))); | |
425 } | |
426 | |
427 uint64 PrefService::GetUint64(const char* path) const { | |
428 DCHECK(CalledOnValidThread()); | |
429 | |
430 const Value* value = GetPreferenceValue(path); | |
431 if (!value) { | |
432 NOTREACHED() << "Trying to read an unregistered pref: " << path; | |
433 return 0; | |
434 } | |
435 std::string result("0"); | |
436 bool rv = value->GetAsString(&result); | |
437 DCHECK(rv); | |
438 | |
439 uint64 val; | |
440 base::StringToUint64(result, &val); | |
441 return val; | |
442 } | |
443 | |
444 Value* PrefService::GetMutableUserPref(const char* path, | |
445 base::Value::Type type) { | |
446 CHECK(type == Value::TYPE_DICTIONARY || type == Value::TYPE_LIST); | |
447 DCHECK(CalledOnValidThread()); | |
448 | |
449 const Preference* pref = FindPreference(path); | |
450 if (!pref) { | |
451 NOTREACHED() << "Trying to get an unregistered pref: " << path; | |
452 return NULL; | |
453 } | |
454 if (pref->GetType() != type) { | |
455 NOTREACHED() << "Wrong type for GetMutableValue: " << path; | |
456 return NULL; | |
457 } | |
458 | |
459 // Look for an existing preference in the user store. If it doesn't | |
460 // exist or isn't the correct type, create a new user preference. | |
461 Value* value = NULL; | |
462 if (!user_pref_store_->GetMutableValue(path, &value) || | |
463 !value->IsType(type)) { | |
464 if (type == Value::TYPE_DICTIONARY) { | |
465 value = new DictionaryValue; | |
466 } else if (type == Value::TYPE_LIST) { | |
467 value = new ListValue; | |
468 } else { | |
469 NOTREACHED(); | |
470 } | |
471 user_pref_store_->SetValueSilently(path, value); | |
472 } | |
473 return value; | |
474 } | |
475 | |
476 void PrefService::ReportUserPrefChanged(const std::string& key) { | |
477 user_pref_store_->ReportValueChanged(key); | |
478 } | |
479 | |
480 void PrefService::SetUserPrefValue(const char* path, Value* new_value) { | |
481 scoped_ptr<Value> owned_value(new_value); | |
482 DCHECK(CalledOnValidThread()); | |
483 | |
484 const Preference* pref = FindPreference(path); | |
485 if (!pref) { | |
486 NOTREACHED() << "Trying to write an unregistered pref: " << path; | |
487 return; | |
488 } | |
489 if (pref->GetType() != new_value->GetType()) { | |
490 NOTREACHED() << "Trying to set pref " << path | |
491 << " of type " << pref->GetType() | |
492 << " to value of type " << new_value->GetType(); | |
493 return; | |
494 } | |
495 | |
496 user_pref_store_->SetValue(path, owned_value.release()); | |
497 } | |
498 | |
499 void PrefService::UpdateCommandLinePrefStore(PrefStore* command_line_store) { | |
500 pref_value_store_->UpdateCommandLinePrefStore(command_line_store); | |
501 } | |
502 | |
503 /////////////////////////////////////////////////////////////////////////////// | |
504 // PrefService::Preference | |
505 | |
506 PrefService::Preference::Preference(const PrefService* service, | |
507 const char* name, | |
508 base::Value::Type type) | |
509 : name_(name), | |
510 type_(type), | |
511 pref_service_(service) { | |
512 DCHECK(name); | |
513 DCHECK(service); | |
514 } | |
515 | |
516 const std::string PrefService::Preference::name() const { | |
517 return name_; | |
518 } | |
519 | |
520 base::Value::Type PrefService::Preference::GetType() const { | |
521 return type_; | |
522 } | |
523 | |
524 const Value* PrefService::Preference::GetValue() const { | |
525 const Value* result= pref_service_->GetPreferenceValue(name_); | |
526 DCHECK(result) << "Must register pref before getting its value"; | |
527 return result; | |
528 } | |
529 | |
530 const Value* PrefService::Preference::GetRecommendedValue() const { | |
531 DCHECK(pref_service_->FindPreference(name_.c_str())) << | |
532 "Must register pref before getting its value"; | |
533 | |
534 const Value* found_value = NULL; | |
535 if (pref_value_store()->GetRecommendedValue(name_, type_, &found_value)) { | |
536 DCHECK(found_value->IsType(type_)); | |
537 return found_value; | |
538 } | |
539 | |
540 // The pref has no recommended value. | |
541 return NULL; | |
542 } | |
543 | |
544 bool PrefService::Preference::IsManaged() const { | |
545 return pref_value_store()->PrefValueInManagedStore(name_.c_str()); | |
546 } | |
547 | |
548 bool PrefService::Preference::IsRecommended() const { | |
549 return pref_value_store()->PrefValueFromRecommendedStore(name_.c_str()); | |
550 } | |
551 | |
552 bool PrefService::Preference::HasExtensionSetting() const { | |
553 return pref_value_store()->PrefValueInExtensionStore(name_.c_str()); | |
554 } | |
555 | |
556 bool PrefService::Preference::HasUserSetting() const { | |
557 return pref_value_store()->PrefValueInUserStore(name_.c_str()); | |
558 } | |
559 | |
560 bool PrefService::Preference::IsExtensionControlled() const { | |
561 return pref_value_store()->PrefValueFromExtensionStore(name_.c_str()); | |
562 } | |
563 | |
564 bool PrefService::Preference::IsUserControlled() const { | |
565 return pref_value_store()->PrefValueFromUserStore(name_.c_str()); | |
566 } | |
567 | |
568 bool PrefService::Preference::IsDefaultValue() const { | |
569 return pref_value_store()->PrefValueFromDefaultStore(name_.c_str()); | |
570 } | |
571 | |
572 bool PrefService::Preference::IsUserModifiable() const { | |
573 return pref_value_store()->PrefValueUserModifiable(name_.c_str()); | |
574 } | |
575 | |
576 bool PrefService::Preference::IsExtensionModifiable() const { | |
577 return pref_value_store()->PrefValueExtensionModifiable(name_.c_str()); | |
578 } | |
579 | |
580 const base::Value* PrefService::GetPreferenceValue( | |
581 const std::string& path) const { | |
582 DCHECK(CalledOnValidThread()); | |
583 const Value* default_value = NULL; | |
584 if (pref_registry_->defaults()->GetValue(path, &default_value)) { | |
585 const Value* found_value = NULL; | |
586 base::Value::Type default_type = default_value->GetType(); | |
587 if (pref_value_store_->GetValue(path, default_type, &found_value)) { | |
588 DCHECK(found_value->IsType(default_type)); | |
589 return found_value; | |
590 } else { | |
591 // Every registered preference has at least a default value. | |
592 NOTREACHED() << "no valid value found for registered pref " << path; | |
593 } | |
594 } | |
595 | |
596 return NULL; | |
597 } | |
OLD | NEW |