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/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/strings/string_number_conversions.h"
20 #include "base/strings/string_util.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 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 InitFromStorage(async
);
59 PrefService::~PrefService() {
60 DCHECK(CalledOnValidThread());
62 // Reset pointers so accesses after destruction reliably crash.
63 pref_value_store_
.reset();
64 pref_registry_
= NULL
;
65 user_pref_store_
= NULL
;
66 pref_notifier_
.reset();
69 void PrefService::InitFromStorage(bool async
) {
70 if (user_pref_store_
->IsInitializationComplete()) {
71 read_error_callback_
.Run(user_pref_store_
->GetReadError());
73 read_error_callback_
.Run(user_pref_store_
->ReadPrefs());
75 // Guarantee that initialization happens after this function returned.
76 base::MessageLoop::current()->PostTask(
78 base::Bind(&PersistentPrefStore::ReadPrefsAsync
,
79 user_pref_store_
.get(),
80 new ReadErrorHandler(read_error_callback_
)));
84 void PrefService::CommitPendingWrite() {
85 DCHECK(CalledOnValidThread());
86 user_pref_store_
->CommitPendingWrite();
89 bool PrefService::GetBoolean(const char* path
) const {
90 DCHECK(CalledOnValidThread());
94 const base::Value
* value
= GetPreferenceValue(path
);
96 NOTREACHED() << "Trying to read an unregistered pref: " << path
;
99 bool rv
= value
->GetAsBoolean(&result
);
104 int PrefService::GetInteger(const char* path
) const {
105 DCHECK(CalledOnValidThread());
109 const base::Value
* value
= GetPreferenceValue(path
);
111 NOTREACHED() << "Trying to read an unregistered pref: " << path
;
114 bool rv
= value
->GetAsInteger(&result
);
119 double PrefService::GetDouble(const char* path
) const {
120 DCHECK(CalledOnValidThread());
124 const base::Value
* value
= GetPreferenceValue(path
);
126 NOTREACHED() << "Trying to read an unregistered pref: " << path
;
129 bool rv
= value
->GetAsDouble(&result
);
134 std::string
PrefService::GetString(const char* path
) const {
135 DCHECK(CalledOnValidThread());
139 const base::Value
* value
= GetPreferenceValue(path
);
141 NOTREACHED() << "Trying to read an unregistered pref: " << path
;
144 bool rv
= value
->GetAsString(&result
);
149 base::FilePath
PrefService::GetFilePath(const char* path
) const {
150 DCHECK(CalledOnValidThread());
152 base::FilePath result
;
154 const base::Value
* value
= GetPreferenceValue(path
);
156 NOTREACHED() << "Trying to read an unregistered pref: " << path
;
157 return base::FilePath(result
);
159 bool rv
= base::GetValueAsFilePath(*value
, &result
);
164 bool PrefService::HasPrefPath(const char* path
) const {
165 const Preference
* pref
= FindPreference(path
);
166 return pref
&& !pref
->IsDefaultValue();
169 scoped_ptr
<base::DictionaryValue
> PrefService::GetPreferenceValues() const {
170 DCHECK(CalledOnValidThread());
171 scoped_ptr
<base::DictionaryValue
> out(new base::DictionaryValue
);
172 PrefRegistry::const_iterator i
= pref_registry_
->begin();
173 for (; i
!= pref_registry_
->end(); ++i
) {
174 const base::Value
* value
= GetPreferenceValue(i
->first
);
176 out
->Set(i
->first
, value
->DeepCopy());
181 scoped_ptr
<base::DictionaryValue
>
182 PrefService::GetPreferenceValuesWithoutPathExpansion() const {
183 DCHECK(CalledOnValidThread());
184 scoped_ptr
<base::DictionaryValue
> out(new base::DictionaryValue
);
185 PrefRegistry::const_iterator i
= pref_registry_
->begin();
186 for (; i
!= pref_registry_
->end(); ++i
) {
187 const base::Value
* value
= GetPreferenceValue(i
->first
);
189 out
->SetWithoutPathExpansion(i
->first
, value
->DeepCopy());
194 const PrefService::Preference
* PrefService::FindPreference(
195 const char* pref_name
) const {
196 DCHECK(CalledOnValidThread());
197 PreferenceMap::iterator it
= prefs_map_
.find(pref_name
);
198 if (it
!= prefs_map_
.end())
199 return &(it
->second
);
200 const base::Value
* default_value
= NULL
;
201 if (!pref_registry_
->defaults()->GetValue(pref_name
, &default_value
))
203 it
= prefs_map_
.insert(
204 std::make_pair(pref_name
, Preference(
205 this, pref_name
, default_value
->GetType()))).first
;
206 return &(it
->second
);
209 bool PrefService::ReadOnly() const {
210 return user_pref_store_
->ReadOnly();
213 PrefService::PrefInitializationStatus
PrefService::GetInitializationStatus()
215 if (!user_pref_store_
->IsInitializationComplete())
216 return INITIALIZATION_STATUS_WAITING
;
218 switch (user_pref_store_
->GetReadError()) {
219 case PersistentPrefStore::PREF_READ_ERROR_NONE
:
220 return INITIALIZATION_STATUS_SUCCESS
;
221 case PersistentPrefStore::PREF_READ_ERROR_NO_FILE
:
222 return INITIALIZATION_STATUS_CREATED_NEW_PREF_STORE
;
224 return INITIALIZATION_STATUS_ERROR
;
228 bool PrefService::IsManagedPreference(const char* pref_name
) const {
229 const Preference
* pref
= FindPreference(pref_name
);
230 return pref
&& pref
->IsManaged();
233 bool PrefService::IsUserModifiablePreference(const char* pref_name
) const {
234 const Preference
* pref
= FindPreference(pref_name
);
235 return pref
&& pref
->IsUserModifiable();
238 const base::DictionaryValue
* PrefService::GetDictionary(
239 const char* path
) const {
240 DCHECK(CalledOnValidThread());
242 const base::Value
* value
= GetPreferenceValue(path
);
244 NOTREACHED() << "Trying to read an unregistered pref: " << path
;
247 if (value
->GetType() != base::Value::TYPE_DICTIONARY
) {
251 return static_cast<const base::DictionaryValue
*>(value
);
254 const base::Value
* PrefService::GetUserPrefValue(const char* path
) const {
255 DCHECK(CalledOnValidThread());
257 const Preference
* pref
= FindPreference(path
);
259 NOTREACHED() << "Trying to get an unregistered pref: " << path
;
263 // Look for an existing preference in the user store. If it doesn't
264 // exist, return NULL.
265 base::Value
* value
= NULL
;
266 if (!user_pref_store_
->GetMutableValue(path
, &value
))
269 if (!value
->IsType(pref
->GetType())) {
270 NOTREACHED() << "Pref value type doesn't match registered type.";
277 void PrefService::SetDefaultPrefValue(const char* path
,
278 base::Value
* value
) {
279 DCHECK(CalledOnValidThread());
280 pref_registry_
->SetDefaultPrefValue(path
, value
);
283 const base::Value
* PrefService::GetDefaultPrefValue(const char* path
) const {
284 DCHECK(CalledOnValidThread());
285 // Lookup the preference in the default store.
286 const base::Value
* value
= NULL
;
287 if (!pref_registry_
->defaults()->GetValue(path
, &value
)) {
288 NOTREACHED() << "Default value missing for pref: " << path
;
294 const base::ListValue
* PrefService::GetList(const char* path
) const {
295 DCHECK(CalledOnValidThread());
297 const base::Value
* value
= GetPreferenceValue(path
);
299 NOTREACHED() << "Trying to read an unregistered pref: " << path
;
302 if (value
->GetType() != base::Value::TYPE_LIST
) {
306 return static_cast<const base::ListValue
*>(value
);
309 void PrefService::AddPrefObserver(const char* path
, PrefObserver
* obs
) {
310 pref_notifier_
->AddPrefObserver(path
, obs
);
313 void PrefService::RemovePrefObserver(const char* path
, PrefObserver
* obs
) {
314 pref_notifier_
->RemovePrefObserver(path
, obs
);
317 void PrefService::AddPrefInitObserver(base::Callback
<void(bool)> obs
) {
318 pref_notifier_
->AddInitObserver(obs
);
321 PrefRegistry
* PrefService::DeprecatedGetPrefRegistry() {
322 return pref_registry_
.get();
325 void PrefService::ClearPref(const char* path
) {
326 DCHECK(CalledOnValidThread());
328 const Preference
* pref
= FindPreference(path
);
330 NOTREACHED() << "Trying to clear an unregistered pref: " << path
;
333 user_pref_store_
->RemoveValue(path
);
336 void PrefService::Set(const char* path
, const base::Value
& value
) {
337 SetUserPrefValue(path
, value
.DeepCopy());
340 void PrefService::SetBoolean(const char* path
, bool value
) {
341 SetUserPrefValue(path
, new base::FundamentalValue(value
));
344 void PrefService::SetInteger(const char* path
, int value
) {
345 SetUserPrefValue(path
, new base::FundamentalValue(value
));
348 void PrefService::SetDouble(const char* path
, double value
) {
349 SetUserPrefValue(path
, new base::FundamentalValue(value
));
352 void PrefService::SetString(const char* path
, const std::string
& value
) {
353 SetUserPrefValue(path
, new base::StringValue(value
));
356 void PrefService::SetFilePath(const char* path
, const base::FilePath
& value
) {
357 SetUserPrefValue(path
, base::CreateFilePathValue(value
));
360 void PrefService::SetInt64(const char* path
, int64 value
) {
361 SetUserPrefValue(path
, new base::StringValue(base::Int64ToString(value
)));
364 int64
PrefService::GetInt64(const char* path
) const {
365 DCHECK(CalledOnValidThread());
367 const base::Value
* value
= GetPreferenceValue(path
);
369 NOTREACHED() << "Trying to read an unregistered pref: " << path
;
372 std::string
result("0");
373 bool rv
= value
->GetAsString(&result
);
377 base::StringToInt64(result
, &val
);
381 void PrefService::SetUint64(const char* path
, uint64 value
) {
382 SetUserPrefValue(path
, new base::StringValue(base::Uint64ToString(value
)));
385 uint64
PrefService::GetUint64(const char* path
) const {
386 DCHECK(CalledOnValidThread());
388 const base::Value
* value
= GetPreferenceValue(path
);
390 NOTREACHED() << "Trying to read an unregistered pref: " << path
;
393 std::string
result("0");
394 bool rv
= value
->GetAsString(&result
);
398 base::StringToUint64(result
, &val
);
402 base::Value
* PrefService::GetMutableUserPref(const char* path
,
403 base::Value::Type type
) {
404 CHECK(type
== base::Value::TYPE_DICTIONARY
|| type
== base::Value::TYPE_LIST
);
405 DCHECK(CalledOnValidThread());
407 const Preference
* pref
= FindPreference(path
);
409 NOTREACHED() << "Trying to get an unregistered pref: " << path
;
412 if (pref
->GetType() != type
) {
413 NOTREACHED() << "Wrong type for GetMutableValue: " << path
;
417 // Look for an existing preference in the user store. If it doesn't
418 // exist or isn't the correct type, create a new user preference.
419 base::Value
* value
= NULL
;
420 if (!user_pref_store_
->GetMutableValue(path
, &value
) ||
421 !value
->IsType(type
)) {
422 if (type
== base::Value::TYPE_DICTIONARY
) {
423 value
= new base::DictionaryValue
;
424 } else if (type
== base::Value::TYPE_LIST
) {
425 value
= new base::ListValue
;
429 user_pref_store_
->SetValueSilently(path
, value
);
434 void PrefService::ReportUserPrefChanged(const std::string
& key
) {
435 DCHECK(CalledOnValidThread());
436 user_pref_store_
->ReportValueChanged(key
);
439 void PrefService::SetUserPrefValue(const char* path
, base::Value
* new_value
) {
440 scoped_ptr
<base::Value
> owned_value(new_value
);
441 DCHECK(CalledOnValidThread());
443 const Preference
* pref
= FindPreference(path
);
445 NOTREACHED() << "Trying to write an unregistered pref: " << path
;
448 if (pref
->GetType() != new_value
->GetType()) {
449 NOTREACHED() << "Trying to set pref " << path
450 << " of type " << pref
->GetType()
451 << " to value of type " << new_value
->GetType();
455 user_pref_store_
->SetValue(path
, owned_value
.release());
458 void PrefService::UpdateCommandLinePrefStore(PrefStore
* command_line_store
) {
459 pref_value_store_
->UpdateCommandLinePrefStore(command_line_store
);
462 ///////////////////////////////////////////////////////////////////////////////
463 // PrefService::Preference
465 PrefService::Preference::Preference(const PrefService
* service
,
467 base::Value::Type type
)
470 pref_service_(service
) {
475 const std::string
PrefService::Preference::name() const {
479 base::Value::Type
PrefService::Preference::GetType() const {
483 const base::Value
* PrefService::Preference::GetValue() const {
484 const base::Value
* result
= pref_service_
->GetPreferenceValue(name_
);
485 DCHECK(result
) << "Must register pref before getting its value";
489 const base::Value
* PrefService::Preference::GetRecommendedValue() const {
490 DCHECK(pref_service_
->FindPreference(name_
.c_str())) <<
491 "Must register pref before getting its value";
493 const base::Value
* found_value
= NULL
;
494 if (pref_value_store()->GetRecommendedValue(name_
, type_
, &found_value
)) {
495 DCHECK(found_value
->IsType(type_
));
499 // The pref has no recommended value.
503 bool PrefService::Preference::IsManaged() const {
504 return pref_value_store()->PrefValueInManagedStore(name_
.c_str());
507 bool PrefService::Preference::IsRecommended() const {
508 return pref_value_store()->PrefValueFromRecommendedStore(name_
.c_str());
511 bool PrefService::Preference::HasExtensionSetting() const {
512 return pref_value_store()->PrefValueInExtensionStore(name_
.c_str());
515 bool PrefService::Preference::HasUserSetting() const {
516 return pref_value_store()->PrefValueInUserStore(name_
.c_str());
519 bool PrefService::Preference::IsExtensionControlled() const {
520 return pref_value_store()->PrefValueFromExtensionStore(name_
.c_str());
523 bool PrefService::Preference::IsUserControlled() const {
524 return pref_value_store()->PrefValueFromUserStore(name_
.c_str());
527 bool PrefService::Preference::IsDefaultValue() const {
528 return pref_value_store()->PrefValueFromDefaultStore(name_
.c_str());
531 bool PrefService::Preference::IsUserModifiable() const {
532 return pref_value_store()->PrefValueUserModifiable(name_
.c_str());
535 bool PrefService::Preference::IsExtensionModifiable() const {
536 return pref_value_store()->PrefValueExtensionModifiable(name_
.c_str());
539 const base::Value
* PrefService::GetPreferenceValue(
540 const std::string
& path
) const {
541 DCHECK(CalledOnValidThread());
542 const base::Value
* default_value
= NULL
;
543 if (pref_registry_
->defaults()->GetValue(path
, &default_value
)) {
544 const base::Value
* found_value
= NULL
;
545 base::Value::Type default_type
= default_value
->GetType();
546 if (pref_value_store_
->GetValue(path
, default_type
, &found_value
)) {
547 DCHECK(found_value
->IsType(default_type
));
550 // Every registered preference has at least a default value.
551 NOTREACHED() << "no valid value found for registered pref " << path
;