Add testonly=true to components/pairing:unit_tests
[chromium-blink-merge.git] / base / prefs / pref_service.cc
blob65605c5f8e6d346f1d10f82b3a1dc76b08e989a9
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"
7 #include <algorithm>
9 #include "base/bind.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"
24 namespace {
26 class ReadErrorHandler : public PersistentPrefStore::ReadErrorDelegate {
27 public:
28 ReadErrorHandler(base::Callback<void(PersistentPrefStore::PrefReadError)> cb)
29 : callback_(cb) {}
31 virtual void OnError(PersistentPrefStore::PrefReadError error) OVERRIDE {
32 callback_.Run(error);
35 private:
36 base::Callback<void(PersistentPrefStore::PrefReadError)> callback_;
39 } // namespace
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)>
47 read_error_callback,
48 bool async)
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());
72 } else if (!async) {
73 read_error_callback_.Run(user_pref_store_->ReadPrefs());
74 } else {
75 // Guarantee that initialization happens after this function returned.
76 base::MessageLoop::current()->PostTask(
77 FROM_HERE,
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());
92 bool result = false;
94 const base::Value* value = GetPreferenceValue(path);
95 if (!value) {
96 NOTREACHED() << "Trying to read an unregistered pref: " << path;
97 return result;
99 bool rv = value->GetAsBoolean(&result);
100 DCHECK(rv);
101 return result;
104 int PrefService::GetInteger(const char* path) const {
105 DCHECK(CalledOnValidThread());
107 int result = 0;
109 const base::Value* value = GetPreferenceValue(path);
110 if (!value) {
111 NOTREACHED() << "Trying to read an unregistered pref: " << path;
112 return result;
114 bool rv = value->GetAsInteger(&result);
115 DCHECK(rv);
116 return result;
119 double PrefService::GetDouble(const char* path) const {
120 DCHECK(CalledOnValidThread());
122 double result = 0.0;
124 const base::Value* value = GetPreferenceValue(path);
125 if (!value) {
126 NOTREACHED() << "Trying to read an unregistered pref: " << path;
127 return result;
129 bool rv = value->GetAsDouble(&result);
130 DCHECK(rv);
131 return result;
134 std::string PrefService::GetString(const char* path) const {
135 DCHECK(CalledOnValidThread());
137 std::string result;
139 const base::Value* value = GetPreferenceValue(path);
140 if (!value) {
141 NOTREACHED() << "Trying to read an unregistered pref: " << path;
142 return result;
144 bool rv = value->GetAsString(&result);
145 DCHECK(rv);
146 return result;
149 base::FilePath PrefService::GetFilePath(const char* path) const {
150 DCHECK(CalledOnValidThread());
152 base::FilePath result;
154 const base::Value* value = GetPreferenceValue(path);
155 if (!value) {
156 NOTREACHED() << "Trying to read an unregistered pref: " << path;
157 return base::FilePath(result);
159 bool rv = base::GetValueAsFilePath(*value, &result);
160 DCHECK(rv);
161 return 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);
175 DCHECK(value);
176 out->Set(i->first, value->DeepCopy());
178 return out.Pass();
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);
188 DCHECK(value);
189 out->SetWithoutPathExpansion(i->first, value->DeepCopy());
191 return out.Pass();
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))
202 return NULL;
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()
214 const {
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;
223 default:
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);
243 if (!value) {
244 NOTREACHED() << "Trying to read an unregistered pref: " << path;
245 return NULL;
247 if (value->GetType() != base::Value::TYPE_DICTIONARY) {
248 NOTREACHED();
249 return NULL;
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);
258 if (!pref) {
259 NOTREACHED() << "Trying to get an unregistered pref: " << path;
260 return NULL;
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))
267 return NULL;
269 if (!value->IsType(pref->GetType())) {
270 NOTREACHED() << "Pref value type doesn't match registered type.";
271 return NULL;
274 return value;
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;
289 return NULL;
291 return value;
294 const base::ListValue* PrefService::GetList(const char* path) const {
295 DCHECK(CalledOnValidThread());
297 const base::Value* value = GetPreferenceValue(path);
298 if (!value) {
299 NOTREACHED() << "Trying to read an unregistered pref: " << path;
300 return NULL;
302 if (value->GetType() != base::Value::TYPE_LIST) {
303 NOTREACHED();
304 return NULL;
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);
329 if (!pref) {
330 NOTREACHED() << "Trying to clear an unregistered pref: " << path;
331 return;
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);
368 if (!value) {
369 NOTREACHED() << "Trying to read an unregistered pref: " << path;
370 return 0;
372 std::string result("0");
373 bool rv = value->GetAsString(&result);
374 DCHECK(rv);
376 int64 val;
377 base::StringToInt64(result, &val);
378 return 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);
389 if (!value) {
390 NOTREACHED() << "Trying to read an unregistered pref: " << path;
391 return 0;
393 std::string result("0");
394 bool rv = value->GetAsString(&result);
395 DCHECK(rv);
397 uint64 val;
398 base::StringToUint64(result, &val);
399 return 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);
408 if (!pref) {
409 NOTREACHED() << "Trying to get an unregistered pref: " << path;
410 return NULL;
412 if (pref->GetType() != type) {
413 NOTREACHED() << "Wrong type for GetMutableValue: " << path;
414 return NULL;
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;
426 } else {
427 NOTREACHED();
429 user_pref_store_->SetValueSilently(path, value);
431 return 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);
444 if (!pref) {
445 NOTREACHED() << "Trying to write an unregistered pref: " << path;
446 return;
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();
452 return;
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,
466 const char* name,
467 base::Value::Type type)
468 : name_(name),
469 type_(type),
470 pref_service_(service) {
471 DCHECK(name);
472 DCHECK(service);
475 const std::string PrefService::Preference::name() const {
476 return name_;
479 base::Value::Type PrefService::Preference::GetType() const {
480 return type_;
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";
486 return result;
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_));
496 return found_value;
499 // The pref has no recommended value.
500 return NULL;
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));
548 return found_value;
549 } else {
550 // Every registered preference has at least a default value.
551 NOTREACHED() << "no valid value found for registered pref " << path;
555 return NULL;