Stop linking tcmalloc into shared library components.
[chromium-blink-merge.git] / base / prefs / pref_service.cc
blobbabe40afb4f942b6aef777db10fdb0b2afe26db5
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.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/string_util.h"
20 #include "base/strings/string_number_conversions.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 pref_registry_->SetRegistrationCallback(
57 base::Bind(&PrefService::AddRegisteredPreference,
58 base::Unretained(this)));
59 AddInitialPreferences();
61 InitFromStorage(async);
64 PrefService::~PrefService() {
65 DCHECK(CalledOnValidThread());
67 // Remove our callback, setting a NULL one.
68 pref_registry_->SetRegistrationCallback(PrefRegistry::RegistrationCallback());
70 // Reset pointers so accesses after destruction reliably crash.
71 pref_value_store_.reset();
72 pref_registry_ = NULL;
73 user_pref_store_ = NULL;
74 pref_notifier_.reset();
77 void PrefService::InitFromStorage(bool async) {
78 if (!async) {
79 read_error_callback_.Run(user_pref_store_->ReadPrefs());
80 } else {
81 // Guarantee that initialization happens after this function returned.
82 MessageLoop::current()->PostTask(
83 FROM_HERE,
84 base::Bind(&PersistentPrefStore::ReadPrefsAsync,
85 user_pref_store_.get(),
86 new ReadErrorHandler(read_error_callback_)));
90 bool PrefService::ReloadPersistentPrefs() {
91 return user_pref_store_->ReadPrefs() ==
92 PersistentPrefStore::PREF_READ_ERROR_NONE;
95 void PrefService::CommitPendingWrite() {
96 DCHECK(CalledOnValidThread());
97 user_pref_store_->CommitPendingWrite();
100 bool PrefService::GetBoolean(const char* path) const {
101 DCHECK(CalledOnValidThread());
103 bool result = false;
105 const base::Value* value = GetPreferenceValue(path);
106 if (!value) {
107 NOTREACHED() << "Trying to read an unregistered pref: " << path;
108 return result;
110 bool rv = value->GetAsBoolean(&result);
111 DCHECK(rv);
112 return result;
115 int PrefService::GetInteger(const char* path) const {
116 DCHECK(CalledOnValidThread());
118 int result = 0;
120 const base::Value* value = GetPreferenceValue(path);
121 if (!value) {
122 NOTREACHED() << "Trying to read an unregistered pref: " << path;
123 return result;
125 bool rv = value->GetAsInteger(&result);
126 DCHECK(rv);
127 return result;
130 double PrefService::GetDouble(const char* path) const {
131 DCHECK(CalledOnValidThread());
133 double result = 0.0;
135 const base::Value* value = GetPreferenceValue(path);
136 if (!value) {
137 NOTREACHED() << "Trying to read an unregistered pref: " << path;
138 return result;
140 bool rv = value->GetAsDouble(&result);
141 DCHECK(rv);
142 return result;
145 std::string PrefService::GetString(const char* path) const {
146 DCHECK(CalledOnValidThread());
148 std::string result;
150 const base::Value* value = GetPreferenceValue(path);
151 if (!value) {
152 NOTREACHED() << "Trying to read an unregistered pref: " << path;
153 return result;
155 bool rv = value->GetAsString(&result);
156 DCHECK(rv);
157 return result;
160 base::FilePath PrefService::GetFilePath(const char* path) const {
161 DCHECK(CalledOnValidThread());
163 base::FilePath result;
165 const base::Value* value = GetPreferenceValue(path);
166 if (!value) {
167 NOTREACHED() << "Trying to read an unregistered pref: " << path;
168 return base::FilePath(result);
170 bool rv = base::GetValueAsFilePath(*value, &result);
171 DCHECK(rv);
172 return result;
175 bool PrefService::HasPrefPath(const char* path) const {
176 const Preference* pref = FindPreference(path);
177 return pref && !pref->IsDefaultValue();
180 DictionaryValue* PrefService::GetPreferenceValues() const {
181 DCHECK(CalledOnValidThread());
182 DictionaryValue* out = new DictionaryValue;
183 PrefRegistry::const_iterator i = pref_registry_->begin();
184 for (; i != pref_registry_->end(); ++i) {
185 const Value* value = GetPreferenceValue(i->first);
186 DCHECK(value);
187 out->Set(i->first, value->DeepCopy());
189 return out;
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))
200 return NULL;
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()
212 const {
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;
221 default:
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 DictionaryValue* PrefService::GetDictionary(const char* path) const {
237 DCHECK(CalledOnValidThread());
239 const Value* value = GetPreferenceValue(path);
240 if (!value) {
241 NOTREACHED() << "Trying to read an unregistered pref: " << path;
242 return NULL;
244 if (value->GetType() != Value::TYPE_DICTIONARY) {
245 NOTREACHED();
246 return NULL;
248 return static_cast<const DictionaryValue*>(value);
251 const base::Value* PrefService::GetUserPrefValue(const char* path) const {
252 DCHECK(CalledOnValidThread());
254 const Preference* pref = FindPreference(path);
255 if (!pref) {
256 NOTREACHED() << "Trying to get an unregistered pref: " << path;
257 return NULL;
260 // Look for an existing preference in the user store. If it doesn't
261 // exist, return NULL.
262 base::Value* value = NULL;
263 if (!user_pref_store_->GetMutableValue(path, &value))
264 return NULL;
266 if (!value->IsType(pref->GetType())) {
267 NOTREACHED() << "Pref value type doesn't match registered type.";
268 return NULL;
271 return value;
274 void PrefService::SetDefaultPrefValue(const char* path,
275 base::Value* value) {
276 DCHECK(CalledOnValidThread());
277 pref_registry_->SetDefaultPrefValue(path, value);
280 const base::Value* PrefService::GetDefaultPrefValue(const char* path) const {
281 DCHECK(CalledOnValidThread());
282 // Lookup the preference in the default store.
283 const base::Value* value = NULL;
284 if (!pref_registry_->defaults()->GetValue(path, &value)) {
285 NOTREACHED() << "Default value missing for pref: " << path;
286 return NULL;
288 return value;
291 const ListValue* PrefService::GetList(const char* path) const {
292 DCHECK(CalledOnValidThread());
294 const Value* value = GetPreferenceValue(path);
295 if (!value) {
296 NOTREACHED() << "Trying to read an unregistered pref: " << path;
297 return NULL;
299 if (value->GetType() != Value::TYPE_LIST) {
300 NOTREACHED();
301 return NULL;
303 return static_cast<const ListValue*>(value);
306 void PrefService::AddPrefObserver(const char* path, PrefObserver* obs) {
307 pref_notifier_->AddPrefObserver(path, obs);
310 void PrefService::RemovePrefObserver(const char* path, PrefObserver* obs) {
311 pref_notifier_->RemovePrefObserver(path, obs);
314 void PrefService::AddPrefInitObserver(base::Callback<void(bool)> obs) {
315 pref_notifier_->AddInitObserver(obs);
318 PrefRegistry* PrefService::DeprecatedGetPrefRegistry() {
319 return pref_registry_.get();
322 void PrefService::AddInitialPreferences() {
323 for (PrefRegistry::const_iterator it = pref_registry_->begin();
324 it != pref_registry_->end();
325 ++it) {
326 AddRegisteredPreference(it->first.c_str(), it->second);
330 // TODO(joi): Once MarkNeedsEmptyValue is gone, we can probably
331 // completely get rid of this method. There will be one difference in
332 // semantics; currently all registered preferences are stored right
333 // away in the prefs_map_, if we remove this they would be stored only
334 // opportunistically.
335 void PrefService::AddRegisteredPreference(const char* path,
336 Value* default_value) {
337 DCHECK(CalledOnValidThread());
339 // For ListValue and DictionaryValue with non empty default, empty value
340 // for |path| needs to be persisted in |user_pref_store_|. So that
341 // non empty default is not used when user sets an empty ListValue or
342 // DictionaryValue.
343 bool needs_empty_value = false;
344 base::Value::Type orig_type = default_value->GetType();
345 if (orig_type == base::Value::TYPE_LIST) {
346 const base::ListValue* list = NULL;
347 if (default_value->GetAsList(&list) && !list->empty())
348 needs_empty_value = true;
349 } else if (orig_type == base::Value::TYPE_DICTIONARY) {
350 const base::DictionaryValue* dict = NULL;
351 if (default_value->GetAsDictionary(&dict) && !dict->empty())
352 needs_empty_value = true;
354 if (needs_empty_value)
355 user_pref_store_->MarkNeedsEmptyValue(path);
358 void PrefService::ClearPref(const char* path) {
359 DCHECK(CalledOnValidThread());
361 const Preference* pref = FindPreference(path);
362 if (!pref) {
363 NOTREACHED() << "Trying to clear an unregistered pref: " << path;
364 return;
366 user_pref_store_->RemoveValue(path);
369 void PrefService::Set(const char* path, const Value& value) {
370 SetUserPrefValue(path, value.DeepCopy());
373 void PrefService::SetBoolean(const char* path, bool value) {
374 SetUserPrefValue(path, Value::CreateBooleanValue(value));
377 void PrefService::SetInteger(const char* path, int value) {
378 SetUserPrefValue(path, Value::CreateIntegerValue(value));
381 void PrefService::SetDouble(const char* path, double value) {
382 SetUserPrefValue(path, Value::CreateDoubleValue(value));
385 void PrefService::SetString(const char* path, const std::string& value) {
386 SetUserPrefValue(path, Value::CreateStringValue(value));
389 void PrefService::SetFilePath(const char* path, const base::FilePath& value) {
390 SetUserPrefValue(path, base::CreateFilePathValue(value));
393 void PrefService::SetInt64(const char* path, int64 value) {
394 SetUserPrefValue(path, Value::CreateStringValue(base::Int64ToString(value)));
397 int64 PrefService::GetInt64(const char* path) const {
398 DCHECK(CalledOnValidThread());
400 const Value* value = GetPreferenceValue(path);
401 if (!value) {
402 NOTREACHED() << "Trying to read an unregistered pref: " << path;
403 return 0;
405 std::string result("0");
406 bool rv = value->GetAsString(&result);
407 DCHECK(rv);
409 int64 val;
410 base::StringToInt64(result, &val);
411 return val;
414 void PrefService::SetUint64(const char* path, uint64 value) {
415 SetUserPrefValue(path, Value::CreateStringValue(base::Uint64ToString(value)));
418 uint64 PrefService::GetUint64(const char* path) const {
419 DCHECK(CalledOnValidThread());
421 const Value* value = GetPreferenceValue(path);
422 if (!value) {
423 NOTREACHED() << "Trying to read an unregistered pref: " << path;
424 return 0;
426 std::string result("0");
427 bool rv = value->GetAsString(&result);
428 DCHECK(rv);
430 uint64 val;
431 base::StringToUint64(result, &val);
432 return val;
435 Value* PrefService::GetMutableUserPref(const char* path,
436 base::Value::Type type) {
437 CHECK(type == Value::TYPE_DICTIONARY || type == Value::TYPE_LIST);
438 DCHECK(CalledOnValidThread());
440 const Preference* pref = FindPreference(path);
441 if (!pref) {
442 NOTREACHED() << "Trying to get an unregistered pref: " << path;
443 return NULL;
445 if (pref->GetType() != type) {
446 NOTREACHED() << "Wrong type for GetMutableValue: " << path;
447 return NULL;
450 // Look for an existing preference in the user store. If it doesn't
451 // exist or isn't the correct type, create a new user preference.
452 Value* value = NULL;
453 if (!user_pref_store_->GetMutableValue(path, &value) ||
454 !value->IsType(type)) {
455 if (type == Value::TYPE_DICTIONARY) {
456 value = new DictionaryValue;
457 } else if (type == Value::TYPE_LIST) {
458 value = new ListValue;
459 } else {
460 NOTREACHED();
462 user_pref_store_->SetValueSilently(path, value);
464 return value;
467 void PrefService::ReportUserPrefChanged(const std::string& key) {
468 user_pref_store_->ReportValueChanged(key);
471 void PrefService::SetUserPrefValue(const char* path, Value* new_value) {
472 scoped_ptr<Value> owned_value(new_value);
473 DCHECK(CalledOnValidThread());
475 const Preference* pref = FindPreference(path);
476 if (!pref) {
477 NOTREACHED() << "Trying to write an unregistered pref: " << path;
478 return;
480 if (pref->GetType() != new_value->GetType()) {
481 NOTREACHED() << "Trying to set pref " << path
482 << " of type " << pref->GetType()
483 << " to value of type " << new_value->GetType();
484 return;
487 user_pref_store_->SetValue(path, owned_value.release());
490 void PrefService::UpdateCommandLinePrefStore(PrefStore* command_line_store) {
491 pref_value_store_->UpdateCommandLinePrefStore(command_line_store);
494 ///////////////////////////////////////////////////////////////////////////////
495 // PrefService::Preference
497 PrefService::Preference::Preference(const PrefService* service,
498 const char* name,
499 base::Value::Type type)
500 : name_(name),
501 type_(type),
502 pref_service_(service) {
503 DCHECK(name);
504 DCHECK(service);
507 const std::string PrefService::Preference::name() const {
508 return name_;
511 base::Value::Type PrefService::Preference::GetType() const {
512 return type_;
515 const Value* PrefService::Preference::GetValue() const {
516 const Value* result= pref_service_->GetPreferenceValue(name_);
517 DCHECK(result) << "Must register pref before getting its value";
518 return result;
521 const Value* PrefService::Preference::GetRecommendedValue() const {
522 DCHECK(pref_service_->FindPreference(name_.c_str())) <<
523 "Must register pref before getting its value";
525 const Value* found_value = NULL;
526 if (pref_value_store()->GetRecommendedValue(name_, type_, &found_value)) {
527 DCHECK(found_value->IsType(type_));
528 return found_value;
531 // The pref has no recommended value.
532 return NULL;
535 bool PrefService::Preference::IsManaged() const {
536 return pref_value_store()->PrefValueInManagedStore(name_.c_str());
539 bool PrefService::Preference::IsRecommended() const {
540 return pref_value_store()->PrefValueFromRecommendedStore(name_.c_str());
543 bool PrefService::Preference::HasExtensionSetting() const {
544 return pref_value_store()->PrefValueInExtensionStore(name_.c_str());
547 bool PrefService::Preference::HasUserSetting() const {
548 return pref_value_store()->PrefValueInUserStore(name_.c_str());
551 bool PrefService::Preference::IsExtensionControlled() const {
552 return pref_value_store()->PrefValueFromExtensionStore(name_.c_str());
555 bool PrefService::Preference::IsUserControlled() const {
556 return pref_value_store()->PrefValueFromUserStore(name_.c_str());
559 bool PrefService::Preference::IsDefaultValue() const {
560 return pref_value_store()->PrefValueFromDefaultStore(name_.c_str());
563 bool PrefService::Preference::IsUserModifiable() const {
564 return pref_value_store()->PrefValueUserModifiable(name_.c_str());
567 bool PrefService::Preference::IsExtensionModifiable() const {
568 return pref_value_store()->PrefValueExtensionModifiable(name_.c_str());
571 const base::Value* PrefService::GetPreferenceValue(
572 const std::string& path) const {
573 DCHECK(CalledOnValidThread());
574 const Value* default_value = NULL;
575 if (pref_registry_->defaults()->GetValue(path, &default_value)) {
576 const Value* found_value = NULL;
577 base::Value::Type default_type = default_value->GetType();
578 if (pref_value_store_->GetValue(path, default_type, &found_value)) {
579 DCHECK(found_value->IsType(default_type));
580 return found_value;
581 } else {
582 // Every registered preference has at least a default value.
583 NOTREACHED() << "no valid value found for registered pref " << path;
587 return NULL;