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 std::string
& 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 std::string
& 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 std::string
& 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 std::string
& 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 std::string
& 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 std::string
& 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 for (const auto& it
: *pref_registry_
) {
173 const base::Value
* value
= GetPreferenceValue(it
.first
);
174 out
->Set(it
.first
, value
->DeepCopy());
179 scoped_ptr
<base::DictionaryValue
> PrefService::GetPreferenceValuesOmitDefaults()
181 DCHECK(CalledOnValidThread());
182 scoped_ptr
<base::DictionaryValue
> out(new base::DictionaryValue
);
183 for (const auto& it
: *pref_registry_
) {
184 const Preference
* pref
= FindPreference(it
.first
);
185 if (pref
->IsDefaultValue())
187 out
->Set(it
.first
, pref
->GetValue()->DeepCopy());
192 scoped_ptr
<base::DictionaryValue
>
193 PrefService::GetPreferenceValuesWithoutPathExpansion() const {
194 DCHECK(CalledOnValidThread());
195 scoped_ptr
<base::DictionaryValue
> out(new base::DictionaryValue
);
196 for (const auto& it
: *pref_registry_
) {
197 const base::Value
* value
= GetPreferenceValue(it
.first
);
199 out
->SetWithoutPathExpansion(it
.first
, value
->DeepCopy());
204 const PrefService::Preference
* PrefService::FindPreference(
205 const std::string
& pref_name
) const {
206 DCHECK(CalledOnValidThread());
207 PreferenceMap::iterator it
= prefs_map_
.find(pref_name
);
208 if (it
!= prefs_map_
.end())
209 return &(it
->second
);
210 const base::Value
* default_value
= NULL
;
211 if (!pref_registry_
->defaults()->GetValue(pref_name
, &default_value
))
213 it
= prefs_map_
.insert(
214 std::make_pair(pref_name
, Preference(
215 this, pref_name
, default_value
->GetType()))).first
;
216 return &(it
->second
);
219 bool PrefService::ReadOnly() const {
220 return user_pref_store_
->ReadOnly();
223 PrefService::PrefInitializationStatus
PrefService::GetInitializationStatus()
225 if (!user_pref_store_
->IsInitializationComplete())
226 return INITIALIZATION_STATUS_WAITING
;
228 switch (user_pref_store_
->GetReadError()) {
229 case PersistentPrefStore::PREF_READ_ERROR_NONE
:
230 return INITIALIZATION_STATUS_SUCCESS
;
231 case PersistentPrefStore::PREF_READ_ERROR_NO_FILE
:
232 return INITIALIZATION_STATUS_CREATED_NEW_PREF_STORE
;
234 return INITIALIZATION_STATUS_ERROR
;
238 bool PrefService::IsManagedPreference(const std::string
& pref_name
) const {
239 const Preference
* pref
= FindPreference(pref_name
);
240 return pref
&& pref
->IsManaged();
243 bool PrefService::IsUserModifiablePreference(
244 const std::string
& pref_name
) const {
245 const Preference
* pref
= FindPreference(pref_name
);
246 return pref
&& pref
->IsUserModifiable();
249 const base::DictionaryValue
* PrefService::GetDictionary(
250 const std::string
& path
) const {
251 DCHECK(CalledOnValidThread());
253 const base::Value
* value
= GetPreferenceValue(path
);
255 NOTREACHED() << "Trying to read an unregistered pref: " << path
;
258 if (value
->GetType() != base::Value::TYPE_DICTIONARY
) {
262 return static_cast<const base::DictionaryValue
*>(value
);
265 const base::Value
* PrefService::GetUserPrefValue(
266 const std::string
& path
) const {
267 DCHECK(CalledOnValidThread());
269 const Preference
* pref
= FindPreference(path
);
271 NOTREACHED() << "Trying to get an unregistered pref: " << path
;
275 // Look for an existing preference in the user store. If it doesn't
276 // exist, return NULL.
277 base::Value
* value
= NULL
;
278 if (!user_pref_store_
->GetMutableValue(path
, &value
))
281 if (!value
->IsType(pref
->GetType())) {
282 NOTREACHED() << "Pref value type doesn't match registered type.";
289 void PrefService::SetDefaultPrefValue(const std::string
& path
,
290 base::Value
* value
) {
291 DCHECK(CalledOnValidThread());
292 pref_registry_
->SetDefaultPrefValue(path
, value
);
295 const base::Value
* PrefService::GetDefaultPrefValue(
296 const std::string
& path
) const {
297 DCHECK(CalledOnValidThread());
298 // Lookup the preference in the default store.
299 const base::Value
* value
= NULL
;
300 if (!pref_registry_
->defaults()->GetValue(path
, &value
)) {
301 NOTREACHED() << "Default value missing for pref: " << path
;
307 const base::ListValue
* PrefService::GetList(const std::string
& path
) const {
308 DCHECK(CalledOnValidThread());
310 const base::Value
* value
= GetPreferenceValue(path
);
312 NOTREACHED() << "Trying to read an unregistered pref: " << path
;
315 if (value
->GetType() != base::Value::TYPE_LIST
) {
319 return static_cast<const base::ListValue
*>(value
);
322 void PrefService::AddPrefObserver(const std::string
& path
, PrefObserver
* obs
) {
323 pref_notifier_
->AddPrefObserver(path
, obs
);
326 void PrefService::RemovePrefObserver(const std::string
& path
,
328 pref_notifier_
->RemovePrefObserver(path
, obs
);
331 void PrefService::AddPrefInitObserver(base::Callback
<void(bool)> obs
) {
332 pref_notifier_
->AddInitObserver(obs
);
335 PrefRegistry
* PrefService::DeprecatedGetPrefRegistry() {
336 return pref_registry_
.get();
339 void PrefService::ClearPref(const std::string
& path
) {
340 DCHECK(CalledOnValidThread());
342 const Preference
* pref
= FindPreference(path
);
344 NOTREACHED() << "Trying to clear an unregistered pref: " << path
;
347 user_pref_store_
->RemoveValue(path
);
350 void PrefService::Set(const std::string
& path
, const base::Value
& value
) {
351 SetUserPrefValue(path
, value
.DeepCopy());
354 void PrefService::SetBoolean(const std::string
& path
, bool value
) {
355 SetUserPrefValue(path
, new base::FundamentalValue(value
));
358 void PrefService::SetInteger(const std::string
& path
, int value
) {
359 SetUserPrefValue(path
, new base::FundamentalValue(value
));
362 void PrefService::SetDouble(const std::string
& path
, double value
) {
363 SetUserPrefValue(path
, new base::FundamentalValue(value
));
366 void PrefService::SetString(const std::string
& path
, const std::string
& value
) {
367 SetUserPrefValue(path
, new base::StringValue(value
));
370 void PrefService::SetFilePath(const std::string
& path
,
371 const base::FilePath
& value
) {
372 SetUserPrefValue(path
, base::CreateFilePathValue(value
));
375 void PrefService::SetInt64(const std::string
& path
, int64 value
) {
376 SetUserPrefValue(path
, new base::StringValue(base::Int64ToString(value
)));
379 int64
PrefService::GetInt64(const std::string
& path
) const {
380 DCHECK(CalledOnValidThread());
382 const base::Value
* value
= GetPreferenceValue(path
);
384 NOTREACHED() << "Trying to read an unregistered pref: " << path
;
387 std::string
result("0");
388 bool rv
= value
->GetAsString(&result
);
392 base::StringToInt64(result
, &val
);
396 void PrefService::SetUint64(const std::string
& path
, uint64 value
) {
397 SetUserPrefValue(path
, new base::StringValue(base::Uint64ToString(value
)));
400 uint64
PrefService::GetUint64(const std::string
& path
) const {
401 DCHECK(CalledOnValidThread());
403 const base::Value
* value
= GetPreferenceValue(path
);
405 NOTREACHED() << "Trying to read an unregistered pref: " << path
;
408 std::string
result("0");
409 bool rv
= value
->GetAsString(&result
);
413 base::StringToUint64(result
, &val
);
417 base::Value
* PrefService::GetMutableUserPref(const std::string
& path
,
418 base::Value::Type type
) {
419 CHECK(type
== base::Value::TYPE_DICTIONARY
|| type
== base::Value::TYPE_LIST
);
420 DCHECK(CalledOnValidThread());
422 const Preference
* pref
= FindPreference(path
);
424 NOTREACHED() << "Trying to get an unregistered pref: " << path
;
427 if (pref
->GetType() != type
) {
428 NOTREACHED() << "Wrong type for GetMutableValue: " << path
;
432 // Look for an existing preference in the user store. If it doesn't
433 // exist or isn't the correct type, create a new user preference.
434 base::Value
* value
= NULL
;
435 if (!user_pref_store_
->GetMutableValue(path
, &value
) ||
436 !value
->IsType(type
)) {
437 if (type
== base::Value::TYPE_DICTIONARY
) {
438 value
= new base::DictionaryValue
;
439 } else if (type
== base::Value::TYPE_LIST
) {
440 value
= new base::ListValue
;
444 user_pref_store_
->SetValueSilently(path
, value
);
449 void PrefService::ReportUserPrefChanged(const std::string
& key
) {
450 DCHECK(CalledOnValidThread());
451 user_pref_store_
->ReportValueChanged(key
);
454 void PrefService::SetUserPrefValue(const std::string
& path
,
455 base::Value
* new_value
) {
456 scoped_ptr
<base::Value
> owned_value(new_value
);
457 DCHECK(CalledOnValidThread());
459 const Preference
* pref
= FindPreference(path
);
461 NOTREACHED() << "Trying to write an unregistered pref: " << path
;
464 if (pref
->GetType() != new_value
->GetType()) {
465 NOTREACHED() << "Trying to set pref " << path
466 << " of type " << pref
->GetType()
467 << " to value of type " << new_value
->GetType();
471 user_pref_store_
->SetValue(path
, owned_value
.release());
474 void PrefService::UpdateCommandLinePrefStore(PrefStore
* command_line_store
) {
475 pref_value_store_
->UpdateCommandLinePrefStore(command_line_store
);
478 ///////////////////////////////////////////////////////////////////////////////
479 // PrefService::Preference
481 PrefService::Preference::Preference(const PrefService
* service
,
482 const std::string
& name
,
483 base::Value::Type type
)
484 : name_(name
), type_(type
), pref_service_(service
) {
488 const std::string
PrefService::Preference::name() const {
492 base::Value::Type
PrefService::Preference::GetType() const {
496 const base::Value
* PrefService::Preference::GetValue() const {
497 const base::Value
* result
= pref_service_
->GetPreferenceValue(name_
);
498 DCHECK(result
) << "Must register pref before getting its value";
502 const base::Value
* PrefService::Preference::GetRecommendedValue() const {
503 DCHECK(pref_service_
->FindPreference(name_
))
504 << "Must register pref before getting its value";
506 const base::Value
* found_value
= NULL
;
507 if (pref_value_store()->GetRecommendedValue(name_
, type_
, &found_value
)) {
508 DCHECK(found_value
->IsType(type_
));
512 // The pref has no recommended value.
516 bool PrefService::Preference::IsManaged() const {
517 return pref_value_store()->PrefValueInManagedStore(name_
);
520 bool PrefService::Preference::IsRecommended() const {
521 return pref_value_store()->PrefValueFromRecommendedStore(name_
);
524 bool PrefService::Preference::HasExtensionSetting() const {
525 return pref_value_store()->PrefValueInExtensionStore(name_
);
528 bool PrefService::Preference::HasUserSetting() const {
529 return pref_value_store()->PrefValueInUserStore(name_
);
532 bool PrefService::Preference::IsExtensionControlled() const {
533 return pref_value_store()->PrefValueFromExtensionStore(name_
);
536 bool PrefService::Preference::IsUserControlled() const {
537 return pref_value_store()->PrefValueFromUserStore(name_
);
540 bool PrefService::Preference::IsDefaultValue() const {
541 return pref_value_store()->PrefValueFromDefaultStore(name_
);
544 bool PrefService::Preference::IsUserModifiable() const {
545 return pref_value_store()->PrefValueUserModifiable(name_
);
548 bool PrefService::Preference::IsExtensionModifiable() const {
549 return pref_value_store()->PrefValueExtensionModifiable(name_
);
552 const base::Value
* PrefService::GetPreferenceValue(
553 const std::string
& path
) const {
554 DCHECK(CalledOnValidThread());
555 const base::Value
* default_value
= NULL
;
556 if (pref_registry_
->defaults()->GetValue(path
, &default_value
)) {
557 const base::Value
* found_value
= NULL
;
558 base::Value::Type default_type
= default_value
->GetType();
559 if (pref_value_store_
->GetValue(path
, default_type
, &found_value
)) {
560 DCHECK(found_value
->IsType(default_type
));
563 // Every registered preference has at least a default value.
564 NOTREACHED() << "no valid value found for registered pref " << path
;