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.
5 #include "base/prefs/pref_service.h"
10 #include "base/files/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/prefs/pref_notifier_impl.h"
16 #include "base/prefs/pref_registry.h"
17 #include "base/prefs/pref_value_store.h"
18 #include "base/stl_util.h"
19 #include "base/string_util.h"
20 #include "base/strings/string_number_conversions.h"
21 #include "base/value_conversions.h"
22 #include "build/build_config.h"
26 class ReadErrorHandler
: public PersistentPrefStore::ReadErrorDelegate
{
28 ReadErrorHandler(base::Callback
<void(PersistentPrefStore::PrefReadError
)> cb
)
31 virtual void OnError(PersistentPrefStore::PrefReadError error
) OVERRIDE
{
36 base::Callback
<void(PersistentPrefStore::PrefReadError
)> callback_
;
41 PrefService::PrefService(
42 PrefNotifierImpl
* pref_notifier
,
43 PrefValueStore
* pref_value_store
,
44 PersistentPrefStore
* user_prefs
,
45 PrefRegistry
* pref_registry
,
46 base::Callback
<void(PersistentPrefStore::PrefReadError
)>
49 : pref_notifier_(pref_notifier
),
50 pref_value_store_(pref_value_store
),
51 pref_registry_(pref_registry
),
52 user_pref_store_(user_prefs
),
53 read_error_callback_(read_error_callback
) {
54 pref_notifier_
->SetPrefService(this);
56 pref_registry_
->SetRegistrationCallback(
57 base::Bind(&PrefService::AddRegisteredPreference
,
58 base::Unretained(this)));
59 AddInitialPreferences();
61 InitFromStorage(async
);
64 PrefService::~PrefService() {
65 DCHECK(CalledOnValidThread());
67 // Remove our callback, setting a NULL one.
68 pref_registry_
->SetRegistrationCallback(PrefRegistry::RegistrationCallback());
70 // Reset pointers so accesses after destruction reliably crash.
71 pref_value_store_
.reset();
72 pref_registry_
= NULL
;
73 user_pref_store_
= NULL
;
74 pref_notifier_
.reset();
77 void PrefService::InitFromStorage(bool async
) {
79 read_error_callback_
.Run(user_pref_store_
->ReadPrefs());
81 // Guarantee that initialization happens after this function returned.
82 MessageLoop::current()->PostTask(
84 base::Bind(&PersistentPrefStore::ReadPrefsAsync
,
85 user_pref_store_
.get(),
86 new ReadErrorHandler(read_error_callback_
)));
90 bool PrefService::ReloadPersistentPrefs() {
91 return user_pref_store_
->ReadPrefs() ==
92 PersistentPrefStore::PREF_READ_ERROR_NONE
;
95 void PrefService::CommitPendingWrite() {
96 DCHECK(CalledOnValidThread());
97 user_pref_store_
->CommitPendingWrite();
100 bool PrefService::GetBoolean(const char* path
) const {
101 DCHECK(CalledOnValidThread());
105 const base::Value
* value
= GetPreferenceValue(path
);
107 NOTREACHED() << "Trying to read an unregistered pref: " << path
;
110 bool rv
= value
->GetAsBoolean(&result
);
115 int PrefService::GetInteger(const char* path
) const {
116 DCHECK(CalledOnValidThread());
120 const base::Value
* value
= GetPreferenceValue(path
);
122 NOTREACHED() << "Trying to read an unregistered pref: " << path
;
125 bool rv
= value
->GetAsInteger(&result
);
130 double PrefService::GetDouble(const char* path
) const {
131 DCHECK(CalledOnValidThread());
135 const base::Value
* value
= GetPreferenceValue(path
);
137 NOTREACHED() << "Trying to read an unregistered pref: " << path
;
140 bool rv
= value
->GetAsDouble(&result
);
145 std::string
PrefService::GetString(const char* path
) const {
146 DCHECK(CalledOnValidThread());
150 const base::Value
* value
= GetPreferenceValue(path
);
152 NOTREACHED() << "Trying to read an unregistered pref: " << path
;
155 bool rv
= value
->GetAsString(&result
);
160 base::FilePath
PrefService::GetFilePath(const char* path
) const {
161 DCHECK(CalledOnValidThread());
163 base::FilePath result
;
165 const base::Value
* value
= GetPreferenceValue(path
);
167 NOTREACHED() << "Trying to read an unregistered pref: " << path
;
168 return base::FilePath(result
);
170 bool rv
= base::GetValueAsFilePath(*value
, &result
);
175 bool PrefService::HasPrefPath(const char* path
) const {
176 const Preference
* pref
= FindPreference(path
);
177 return pref
&& !pref
->IsDefaultValue();
180 DictionaryValue
* PrefService::GetPreferenceValues() const {
181 DCHECK(CalledOnValidThread());
182 DictionaryValue
* out
= new DictionaryValue
;
183 PrefRegistry::const_iterator i
= pref_registry_
->begin();
184 for (; i
!= pref_registry_
->end(); ++i
) {
185 const Value
* value
= GetPreferenceValue(i
->first
);
187 out
->Set(i
->first
, value
->DeepCopy());
192 const PrefService::Preference
* PrefService::FindPreference(
193 const char* pref_name
) const {
194 DCHECK(CalledOnValidThread());
195 PreferenceMap::iterator it
= prefs_map_
.find(pref_name
);
196 if (it
!= prefs_map_
.end())
197 return &(it
->second
);
198 const base::Value
* default_value
= NULL
;
199 if (!pref_registry_
->defaults()->GetValue(pref_name
, &default_value
))
201 it
= prefs_map_
.insert(
202 std::make_pair(pref_name
, Preference(
203 this, pref_name
, default_value
->GetType()))).first
;
204 return &(it
->second
);
207 bool PrefService::ReadOnly() const {
208 return user_pref_store_
->ReadOnly();
211 PrefService::PrefInitializationStatus
PrefService::GetInitializationStatus()
213 if (!user_pref_store_
->IsInitializationComplete())
214 return INITIALIZATION_STATUS_WAITING
;
216 switch (user_pref_store_
->GetReadError()) {
217 case PersistentPrefStore::PREF_READ_ERROR_NONE
:
218 return INITIALIZATION_STATUS_SUCCESS
;
219 case PersistentPrefStore::PREF_READ_ERROR_NO_FILE
:
220 return INITIALIZATION_STATUS_CREATED_NEW_PREF_STORE
;
222 return INITIALIZATION_STATUS_ERROR
;
226 bool PrefService::IsManagedPreference(const char* pref_name
) const {
227 const Preference
* pref
= FindPreference(pref_name
);
228 return pref
&& pref
->IsManaged();
231 bool PrefService::IsUserModifiablePreference(const char* pref_name
) const {
232 const Preference
* pref
= FindPreference(pref_name
);
233 return pref
&& pref
->IsUserModifiable();
236 const DictionaryValue
* PrefService::GetDictionary(const char* path
) const {
237 DCHECK(CalledOnValidThread());
239 const Value
* value
= GetPreferenceValue(path
);
241 NOTREACHED() << "Trying to read an unregistered pref: " << path
;
244 if (value
->GetType() != Value::TYPE_DICTIONARY
) {
248 return static_cast<const DictionaryValue
*>(value
);
251 const base::Value
* PrefService::GetUserPrefValue(const char* path
) const {
252 DCHECK(CalledOnValidThread());
254 const Preference
* pref
= FindPreference(path
);
256 NOTREACHED() << "Trying to get an unregistered pref: " << path
;
260 // Look for an existing preference in the user store. If it doesn't
261 // exist, return NULL.
262 base::Value
* value
= NULL
;
263 if (!user_pref_store_
->GetMutableValue(path
, &value
))
266 if (!value
->IsType(pref
->GetType())) {
267 NOTREACHED() << "Pref value type doesn't match registered type.";
274 void PrefService::SetDefaultPrefValue(const char* path
,
275 base::Value
* value
) {
276 DCHECK(CalledOnValidThread());
277 pref_registry_
->SetDefaultPrefValue(path
, value
);
280 const base::Value
* PrefService::GetDefaultPrefValue(const char* path
) const {
281 DCHECK(CalledOnValidThread());
282 // Lookup the preference in the default store.
283 const base::Value
* value
= NULL
;
284 if (!pref_registry_
->defaults()->GetValue(path
, &value
)) {
285 NOTREACHED() << "Default value missing for pref: " << path
;
291 const ListValue
* PrefService::GetList(const char* path
) const {
292 DCHECK(CalledOnValidThread());
294 const Value
* value
= GetPreferenceValue(path
);
296 NOTREACHED() << "Trying to read an unregistered pref: " << path
;
299 if (value
->GetType() != Value::TYPE_LIST
) {
303 return static_cast<const ListValue
*>(value
);
306 void PrefService::AddPrefObserver(const char* path
, PrefObserver
* obs
) {
307 pref_notifier_
->AddPrefObserver(path
, obs
);
310 void PrefService::RemovePrefObserver(const char* path
, PrefObserver
* obs
) {
311 pref_notifier_
->RemovePrefObserver(path
, obs
);
314 void PrefService::AddPrefInitObserver(base::Callback
<void(bool)> obs
) {
315 pref_notifier_
->AddInitObserver(obs
);
318 PrefRegistry
* PrefService::DeprecatedGetPrefRegistry() {
319 return pref_registry_
.get();
322 void PrefService::AddInitialPreferences() {
323 for (PrefRegistry::const_iterator it
= pref_registry_
->begin();
324 it
!= pref_registry_
->end();
326 AddRegisteredPreference(it
->first
.c_str(), it
->second
);
330 // TODO(joi): Once MarkNeedsEmptyValue is gone, we can probably
331 // completely get rid of this method. There will be one difference in
332 // semantics; currently all registered preferences are stored right
333 // away in the prefs_map_, if we remove this they would be stored only
334 // opportunistically.
335 void PrefService::AddRegisteredPreference(const char* path
,
336 Value
* default_value
) {
337 DCHECK(CalledOnValidThread());
339 // For ListValue and DictionaryValue with non empty default, empty value
340 // for |path| needs to be persisted in |user_pref_store_|. So that
341 // non empty default is not used when user sets an empty ListValue or
343 bool needs_empty_value
= false;
344 base::Value::Type orig_type
= default_value
->GetType();
345 if (orig_type
== base::Value::TYPE_LIST
) {
346 const base::ListValue
* list
= NULL
;
347 if (default_value
->GetAsList(&list
) && !list
->empty())
348 needs_empty_value
= true;
349 } else if (orig_type
== base::Value::TYPE_DICTIONARY
) {
350 const base::DictionaryValue
* dict
= NULL
;
351 if (default_value
->GetAsDictionary(&dict
) && !dict
->empty())
352 needs_empty_value
= true;
354 if (needs_empty_value
)
355 user_pref_store_
->MarkNeedsEmptyValue(path
);
358 void PrefService::ClearPref(const char* path
) {
359 DCHECK(CalledOnValidThread());
361 const Preference
* pref
= FindPreference(path
);
363 NOTREACHED() << "Trying to clear an unregistered pref: " << path
;
366 user_pref_store_
->RemoveValue(path
);
369 void PrefService::Set(const char* path
, const Value
& value
) {
370 SetUserPrefValue(path
, value
.DeepCopy());
373 void PrefService::SetBoolean(const char* path
, bool value
) {
374 SetUserPrefValue(path
, Value::CreateBooleanValue(value
));
377 void PrefService::SetInteger(const char* path
, int value
) {
378 SetUserPrefValue(path
, Value::CreateIntegerValue(value
));
381 void PrefService::SetDouble(const char* path
, double value
) {
382 SetUserPrefValue(path
, Value::CreateDoubleValue(value
));
385 void PrefService::SetString(const char* path
, const std::string
& value
) {
386 SetUserPrefValue(path
, Value::CreateStringValue(value
));
389 void PrefService::SetFilePath(const char* path
, const base::FilePath
& value
) {
390 SetUserPrefValue(path
, base::CreateFilePathValue(value
));
393 void PrefService::SetInt64(const char* path
, int64 value
) {
394 SetUserPrefValue(path
, Value::CreateStringValue(base::Int64ToString(value
)));
397 int64
PrefService::GetInt64(const char* path
) const {
398 DCHECK(CalledOnValidThread());
400 const Value
* value
= GetPreferenceValue(path
);
402 NOTREACHED() << "Trying to read an unregistered pref: " << path
;
405 std::string
result("0");
406 bool rv
= value
->GetAsString(&result
);
410 base::StringToInt64(result
, &val
);
414 void PrefService::SetUint64(const char* path
, uint64 value
) {
415 SetUserPrefValue(path
, Value::CreateStringValue(base::Uint64ToString(value
)));
418 uint64
PrefService::GetUint64(const char* path
) const {
419 DCHECK(CalledOnValidThread());
421 const Value
* value
= GetPreferenceValue(path
);
423 NOTREACHED() << "Trying to read an unregistered pref: " << path
;
426 std::string
result("0");
427 bool rv
= value
->GetAsString(&result
);
431 base::StringToUint64(result
, &val
);
435 Value
* PrefService::GetMutableUserPref(const char* path
,
436 base::Value::Type type
) {
437 CHECK(type
== Value::TYPE_DICTIONARY
|| type
== Value::TYPE_LIST
);
438 DCHECK(CalledOnValidThread());
440 const Preference
* pref
= FindPreference(path
);
442 NOTREACHED() << "Trying to get an unregistered pref: " << path
;
445 if (pref
->GetType() != type
) {
446 NOTREACHED() << "Wrong type for GetMutableValue: " << path
;
450 // Look for an existing preference in the user store. If it doesn't
451 // exist or isn't the correct type, create a new user preference.
453 if (!user_pref_store_
->GetMutableValue(path
, &value
) ||
454 !value
->IsType(type
)) {
455 if (type
== Value::TYPE_DICTIONARY
) {
456 value
= new DictionaryValue
;
457 } else if (type
== Value::TYPE_LIST
) {
458 value
= new ListValue
;
462 user_pref_store_
->SetValueSilently(path
, value
);
467 void PrefService::ReportUserPrefChanged(const std::string
& key
) {
468 user_pref_store_
->ReportValueChanged(key
);
471 void PrefService::SetUserPrefValue(const char* path
, Value
* new_value
) {
472 scoped_ptr
<Value
> owned_value(new_value
);
473 DCHECK(CalledOnValidThread());
475 const Preference
* pref
= FindPreference(path
);
477 NOTREACHED() << "Trying to write an unregistered pref: " << path
;
480 if (pref
->GetType() != new_value
->GetType()) {
481 NOTREACHED() << "Trying to set pref " << path
482 << " of type " << pref
->GetType()
483 << " to value of type " << new_value
->GetType();
487 user_pref_store_
->SetValue(path
, owned_value
.release());
490 void PrefService::UpdateCommandLinePrefStore(PrefStore
* command_line_store
) {
491 pref_value_store_
->UpdateCommandLinePrefStore(command_line_store
);
494 ///////////////////////////////////////////////////////////////////////////////
495 // PrefService::Preference
497 PrefService::Preference::Preference(const PrefService
* service
,
499 base::Value::Type type
)
502 pref_service_(service
) {
507 const std::string
PrefService::Preference::name() const {
511 base::Value::Type
PrefService::Preference::GetType() const {
515 const Value
* PrefService::Preference::GetValue() const {
516 const Value
* result
= pref_service_
->GetPreferenceValue(name_
);
517 DCHECK(result
) << "Must register pref before getting its value";
521 const Value
* PrefService::Preference::GetRecommendedValue() const {
522 DCHECK(pref_service_
->FindPreference(name_
.c_str())) <<
523 "Must register pref before getting its value";
525 const Value
* found_value
= NULL
;
526 if (pref_value_store()->GetRecommendedValue(name_
, type_
, &found_value
)) {
527 DCHECK(found_value
->IsType(type_
));
531 // The pref has no recommended value.
535 bool PrefService::Preference::IsManaged() const {
536 return pref_value_store()->PrefValueInManagedStore(name_
.c_str());
539 bool PrefService::Preference::IsRecommended() const {
540 return pref_value_store()->PrefValueFromRecommendedStore(name_
.c_str());
543 bool PrefService::Preference::HasExtensionSetting() const {
544 return pref_value_store()->PrefValueInExtensionStore(name_
.c_str());
547 bool PrefService::Preference::HasUserSetting() const {
548 return pref_value_store()->PrefValueInUserStore(name_
.c_str());
551 bool PrefService::Preference::IsExtensionControlled() const {
552 return pref_value_store()->PrefValueFromExtensionStore(name_
.c_str());
555 bool PrefService::Preference::IsUserControlled() const {
556 return pref_value_store()->PrefValueFromUserStore(name_
.c_str());
559 bool PrefService::Preference::IsDefaultValue() const {
560 return pref_value_store()->PrefValueFromDefaultStore(name_
.c_str());
563 bool PrefService::Preference::IsUserModifiable() const {
564 return pref_value_store()->PrefValueUserModifiable(name_
.c_str());
567 bool PrefService::Preference::IsExtensionModifiable() const {
568 return pref_value_store()->PrefValueExtensionModifiable(name_
.c_str());
571 const base::Value
* PrefService::GetPreferenceValue(
572 const std::string
& path
) const {
573 DCHECK(CalledOnValidThread());
574 const Value
* default_value
= NULL
;
575 if (pref_registry_
->defaults()->GetValue(path
, &default_value
)) {
576 const Value
* found_value
= NULL
;
577 base::Value::Type default_type
= default_value
->GetType();
578 if (pref_value_store_
->GetValue(path
, default_type
, &found_value
)) {
579 DCHECK(found_value
->IsType(default_type
));
582 // Every registered preference has at least a default value.
583 NOTREACHED() << "no valid value found for registered pref " << path
;