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 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 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
) {
71 read_error_callback_
.Run(user_pref_store_
->ReadPrefs());
73 // Guarantee that initialization happens after this function returned.
74 base::MessageLoop::current()->PostTask(
76 base::Bind(&PersistentPrefStore::ReadPrefsAsync
,
77 user_pref_store_
.get(),
78 new ReadErrorHandler(read_error_callback_
)));
82 void PrefService::CommitPendingWrite() {
83 DCHECK(CalledOnValidThread());
84 user_pref_store_
->CommitPendingWrite();
87 bool PrefService::GetBoolean(const char* path
) const {
88 DCHECK(CalledOnValidThread());
92 const base::Value
* value
= GetPreferenceValue(path
);
94 NOTREACHED() << "Trying to read an unregistered pref: " << path
;
97 bool rv
= value
->GetAsBoolean(&result
);
102 int PrefService::GetInteger(const char* path
) const {
103 DCHECK(CalledOnValidThread());
107 const base::Value
* value
= GetPreferenceValue(path
);
109 NOTREACHED() << "Trying to read an unregistered pref: " << path
;
112 bool rv
= value
->GetAsInteger(&result
);
117 double PrefService::GetDouble(const char* path
) const {
118 DCHECK(CalledOnValidThread());
122 const base::Value
* value
= GetPreferenceValue(path
);
124 NOTREACHED() << "Trying to read an unregistered pref: " << path
;
127 bool rv
= value
->GetAsDouble(&result
);
132 std::string
PrefService::GetString(const char* path
) const {
133 DCHECK(CalledOnValidThread());
137 const base::Value
* value
= GetPreferenceValue(path
);
139 NOTREACHED() << "Trying to read an unregistered pref: " << path
;
142 bool rv
= value
->GetAsString(&result
);
147 base::FilePath
PrefService::GetFilePath(const char* path
) const {
148 DCHECK(CalledOnValidThread());
150 base::FilePath result
;
152 const base::Value
* value
= GetPreferenceValue(path
);
154 NOTREACHED() << "Trying to read an unregistered pref: " << path
;
155 return base::FilePath(result
);
157 bool rv
= base::GetValueAsFilePath(*value
, &result
);
162 bool PrefService::HasPrefPath(const char* path
) const {
163 const Preference
* pref
= FindPreference(path
);
164 return pref
&& !pref
->IsDefaultValue();
167 scoped_ptr
<base::DictionaryValue
> PrefService::GetPreferenceValues() const {
168 DCHECK(CalledOnValidThread());
169 scoped_ptr
<base::DictionaryValue
> out(new base::DictionaryValue
);
170 PrefRegistry::const_iterator i
= pref_registry_
->begin();
171 for (; i
!= pref_registry_
->end(); ++i
) {
172 const base::Value
* value
= GetPreferenceValue(i
->first
);
174 out
->Set(i
->first
, value
->DeepCopy());
179 scoped_ptr
<base::DictionaryValue
>
180 PrefService::GetPreferenceValuesWithoutPathExpansion() const {
181 DCHECK(CalledOnValidThread());
182 scoped_ptr
<base::DictionaryValue
> out(new base::DictionaryValue
);
183 PrefRegistry::const_iterator i
= pref_registry_
->begin();
184 for (; i
!= pref_registry_
->end(); ++i
) {
185 const base::Value
* value
= GetPreferenceValue(i
->first
);
187 out
->SetWithoutPathExpansion(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 base::DictionaryValue
* PrefService::GetDictionary(
237 const char* path
) const {
238 DCHECK(CalledOnValidThread());
240 const base::Value
* value
= GetPreferenceValue(path
);
242 NOTREACHED() << "Trying to read an unregistered pref: " << path
;
245 if (value
->GetType() != base::Value::TYPE_DICTIONARY
) {
249 return static_cast<const base::DictionaryValue
*>(value
);
252 const base::Value
* PrefService::GetUserPrefValue(const char* path
) const {
253 DCHECK(CalledOnValidThread());
255 const Preference
* pref
= FindPreference(path
);
257 NOTREACHED() << "Trying to get an unregistered pref: " << path
;
261 // Look for an existing preference in the user store. If it doesn't
262 // exist, return NULL.
263 base::Value
* value
= NULL
;
264 if (!user_pref_store_
->GetMutableValue(path
, &value
))
267 if (!value
->IsType(pref
->GetType())) {
268 NOTREACHED() << "Pref value type doesn't match registered type.";
275 void PrefService::SetDefaultPrefValue(const char* path
,
276 base::Value
* value
) {
277 DCHECK(CalledOnValidThread());
278 pref_registry_
->SetDefaultPrefValue(path
, value
);
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
;
292 const base::ListValue
* PrefService::GetList(const char* path
) const {
293 DCHECK(CalledOnValidThread());
295 const base::Value
* value
= GetPreferenceValue(path
);
297 NOTREACHED() << "Trying to read an unregistered pref: " << path
;
300 if (value
->GetType() != base::Value::TYPE_LIST
) {
304 return static_cast<const base::ListValue
*>(value
);
307 void PrefService::AddPrefObserver(const char* path
, PrefObserver
* obs
) {
308 pref_notifier_
->AddPrefObserver(path
, obs
);
311 void PrefService::RemovePrefObserver(const char* path
, PrefObserver
* obs
) {
312 pref_notifier_
->RemovePrefObserver(path
, obs
);
315 void PrefService::AddPrefInitObserver(base::Callback
<void(bool)> obs
) {
316 pref_notifier_
->AddInitObserver(obs
);
319 PrefRegistry
* PrefService::DeprecatedGetPrefRegistry() {
320 return pref_registry_
.get();
323 void PrefService::ClearPref(const char* path
) {
324 DCHECK(CalledOnValidThread());
326 const Preference
* pref
= FindPreference(path
);
328 NOTREACHED() << "Trying to clear an unregistered pref: " << path
;
331 user_pref_store_
->RemoveValue(path
);
334 void PrefService::Set(const char* path
, const base::Value
& value
) {
335 SetUserPrefValue(path
, value
.DeepCopy());
338 void PrefService::SetBoolean(const char* path
, bool value
) {
339 SetUserPrefValue(path
, base::Value::CreateBooleanValue(value
));
342 void PrefService::SetInteger(const char* path
, int value
) {
343 SetUserPrefValue(path
, base::Value::CreateIntegerValue(value
));
346 void PrefService::SetDouble(const char* path
, double value
) {
347 SetUserPrefValue(path
, base::Value::CreateDoubleValue(value
));
350 void PrefService::SetString(const char* path
, const std::string
& value
) {
351 SetUserPrefValue(path
, base::Value::CreateStringValue(value
));
354 void PrefService::SetFilePath(const char* path
, const base::FilePath
& value
) {
355 SetUserPrefValue(path
, base::CreateFilePathValue(value
));
358 void PrefService::SetInt64(const char* path
, int64 value
) {
359 SetUserPrefValue(path
,
360 base::Value::CreateStringValue(base::Int64ToString(value
)));
363 int64
PrefService::GetInt64(const char* path
) const {
364 DCHECK(CalledOnValidThread());
366 const base::Value
* value
= GetPreferenceValue(path
);
368 NOTREACHED() << "Trying to read an unregistered pref: " << path
;
371 std::string
result("0");
372 bool rv
= value
->GetAsString(&result
);
376 base::StringToInt64(result
, &val
);
380 void PrefService::SetUint64(const char* path
, uint64 value
) {
381 SetUserPrefValue(path
,
382 base::Value::CreateStringValue(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 user_pref_store_
->ReportValueChanged(key
);
438 void PrefService::SetUserPrefValue(const char* path
, base::Value
* new_value
) {
439 scoped_ptr
<base::Value
> owned_value(new_value
);
440 DCHECK(CalledOnValidThread());
442 const Preference
* pref
= FindPreference(path
);
444 NOTREACHED() << "Trying to write an unregistered pref: " << path
;
447 if (pref
->GetType() != new_value
->GetType()) {
448 NOTREACHED() << "Trying to set pref " << path
449 << " of type " << pref
->GetType()
450 << " to value of type " << new_value
->GetType();
454 user_pref_store_
->SetValue(path
, owned_value
.release());
457 void PrefService::UpdateCommandLinePrefStore(PrefStore
* command_line_store
) {
458 pref_value_store_
->UpdateCommandLinePrefStore(command_line_store
);
461 ///////////////////////////////////////////////////////////////////////////////
462 // PrefService::Preference
464 PrefService::Preference::Preference(const PrefService
* service
,
466 base::Value::Type type
)
469 pref_service_(service
) {
474 const std::string
PrefService::Preference::name() const {
478 base::Value::Type
PrefService::Preference::GetType() const {
482 const base::Value
* PrefService::Preference::GetValue() const {
483 const base::Value
* result
= pref_service_
->GetPreferenceValue(name_
);
484 DCHECK(result
) << "Must register pref before getting its value";
488 const base::Value
* PrefService::Preference::GetRecommendedValue() const {
489 DCHECK(pref_service_
->FindPreference(name_
.c_str())) <<
490 "Must register pref before getting its value";
492 const base::Value
* found_value
= NULL
;
493 if (pref_value_store()->GetRecommendedValue(name_
, type_
, &found_value
)) {
494 DCHECK(found_value
->IsType(type_
));
498 // The pref has no recommended value.
502 bool PrefService::Preference::IsManaged() const {
503 return pref_value_store()->PrefValueInManagedStore(name_
.c_str());
506 bool PrefService::Preference::IsRecommended() const {
507 return pref_value_store()->PrefValueFromRecommendedStore(name_
.c_str());
510 bool PrefService::Preference::HasExtensionSetting() const {
511 return pref_value_store()->PrefValueInExtensionStore(name_
.c_str());
514 bool PrefService::Preference::HasUserSetting() const {
515 return pref_value_store()->PrefValueInUserStore(name_
.c_str());
518 bool PrefService::Preference::IsExtensionControlled() const {
519 return pref_value_store()->PrefValueFromExtensionStore(name_
.c_str());
522 bool PrefService::Preference::IsUserControlled() const {
523 return pref_value_store()->PrefValueFromUserStore(name_
.c_str());
526 bool PrefService::Preference::IsDefaultValue() const {
527 return pref_value_store()->PrefValueFromDefaultStore(name_
.c_str());
530 bool PrefService::Preference::IsUserModifiable() const {
531 return pref_value_store()->PrefValueUserModifiable(name_
.c_str());
534 bool PrefService::Preference::IsExtensionModifiable() const {
535 return pref_value_store()->PrefValueExtensionModifiable(name_
.c_str());
538 const base::Value
* PrefService::GetPreferenceValue(
539 const std::string
& path
) const {
540 DCHECK(CalledOnValidThread());
541 const base::Value
* default_value
= NULL
;
542 if (pref_registry_
->defaults()->GetValue(path
, &default_value
)) {
543 const base::Value
* found_value
= NULL
;
544 base::Value::Type default_type
= default_value
->GetType();
545 if (pref_value_store_
->GetValue(path
, default_type
, &found_value
)) {
546 DCHECK(found_value
->IsType(default_type
));
549 // Every registered preference has at least a default value.
550 NOTREACHED() << "no valid value found for registered pref " << path
;