Roll src/third_party/WebKit 8b42d1d:744641d (svn 186770:186771)
[chromium-blink-merge.git] / base / prefs / pref_service.cc
blob66323a1e152c25b7df7f66deac6dd8d3994de4dc
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 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 std::string& 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 std::string& 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 std::string& 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 std::string& 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 std::string& 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 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());
176 return out.Pass();
179 scoped_ptr<base::DictionaryValue> PrefService::GetPreferenceValuesOmitDefaults()
180 const {
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())
186 continue;
187 out->Set(it.first, pref->GetValue()->DeepCopy());
189 return out.Pass();
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);
198 DCHECK(value);
199 out->SetWithoutPathExpansion(it.first, value->DeepCopy());
201 return out.Pass();
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))
212 return NULL;
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()
224 const {
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;
233 default:
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);
254 if (!value) {
255 NOTREACHED() << "Trying to read an unregistered pref: " << path;
256 return NULL;
258 if (value->GetType() != base::Value::TYPE_DICTIONARY) {
259 NOTREACHED();
260 return NULL;
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);
270 if (!pref) {
271 NOTREACHED() << "Trying to get an unregistered pref: " << path;
272 return NULL;
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))
279 return NULL;
281 if (!value->IsType(pref->GetType())) {
282 NOTREACHED() << "Pref value type doesn't match registered type.";
283 return NULL;
286 return value;
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;
302 return NULL;
304 return value;
307 const base::ListValue* PrefService::GetList(const std::string& path) const {
308 DCHECK(CalledOnValidThread());
310 const base::Value* value = GetPreferenceValue(path);
311 if (!value) {
312 NOTREACHED() << "Trying to read an unregistered pref: " << path;
313 return NULL;
315 if (value->GetType() != base::Value::TYPE_LIST) {
316 NOTREACHED();
317 return NULL;
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,
327 PrefObserver* obs) {
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);
343 if (!pref) {
344 NOTREACHED() << "Trying to clear an unregistered pref: " << path;
345 return;
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);
383 if (!value) {
384 NOTREACHED() << "Trying to read an unregistered pref: " << path;
385 return 0;
387 std::string result("0");
388 bool rv = value->GetAsString(&result);
389 DCHECK(rv);
391 int64 val;
392 base::StringToInt64(result, &val);
393 return 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);
404 if (!value) {
405 NOTREACHED() << "Trying to read an unregistered pref: " << path;
406 return 0;
408 std::string result("0");
409 bool rv = value->GetAsString(&result);
410 DCHECK(rv);
412 uint64 val;
413 base::StringToUint64(result, &val);
414 return 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);
423 if (!pref) {
424 NOTREACHED() << "Trying to get an unregistered pref: " << path;
425 return NULL;
427 if (pref->GetType() != type) {
428 NOTREACHED() << "Wrong type for GetMutableValue: " << path;
429 return NULL;
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;
441 } else {
442 NOTREACHED();
444 user_pref_store_->SetValueSilently(path, value);
446 return 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);
460 if (!pref) {
461 NOTREACHED() << "Trying to write an unregistered pref: " << path;
462 return;
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();
468 return;
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) {
485 DCHECK(service);
488 const std::string PrefService::Preference::name() const {
489 return name_;
492 base::Value::Type PrefService::Preference::GetType() const {
493 return type_;
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";
499 return result;
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_));
509 return found_value;
512 // The pref has no recommended value.
513 return NULL;
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));
561 return found_value;
562 } else {
563 // Every registered preference has at least a default value.
564 NOTREACHED() << "no valid value found for registered pref " << path;
568 return NULL;