blob: babe40afb4f942b6aef777db10fdb0b2afe26db5 [file] [log] [blame]
[email protected]32c3c752012-01-05 17:33:471// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]3853a4c2013-02-11 17:15:575#include "base/prefs/pref_service.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]1cb92b82010-03-08 23:12:157#include <algorithm>
[email protected]1cb92b82010-03-08 23:12:158
[email protected]bebe69432011-09-28 18:36:459#include "base/bind.h"
[email protected]57999812013-02-24 05:40:5210#include "base/files/file_path.h"
initial.commit09911bf2008-07-26 23:55:2911#include "base/logging.h"
12#include "base/message_loop.h"
[email protected]835d7c82010-10-14 04:38:3813#include "base/metrics/histogram.h"
[email protected]03b9b4e2012-10-22 20:01:5214#include "base/prefs/default_pref_store.h"
[email protected]3853a4c2013-02-11 17:15:5715#include "base/prefs/pref_notifier_impl.h"
16#include "base/prefs/pref_registry.h"
17#include "base/prefs/pref_value_store.h"
[email protected]7286e3fc2011-07-19 22:13:2418#include "base/stl_util.h"
initial.commit09911bf2008-07-26 23:55:2919#include "base/string_util.h"
[email protected]3ea1b182013-02-08 22:38:4120#include "base/strings/string_number_conversions.h"
[email protected]8703b2b2011-03-15 09:51:5021#include "base/value_conversions.h"
[email protected]66de4f092009-09-04 23:59:4022#include "build/build_config.h"
initial.commit09911bf2008-07-26 23:55:2923
initial.commit09911bf2008-07-26 23:55:2924namespace {
25
[email protected]845b43a82011-05-11 10:14:4326class ReadErrorHandler : public PersistentPrefStore::ReadErrorDelegate {
27 public:
[email protected]70a317a2012-12-19 20:59:3328 ReadErrorHandler(base::Callback<void(PersistentPrefStore::PrefReadError)> cb)
29 : callback_(cb) {}
[email protected]845b43a82011-05-11 10:14:4330
[email protected]b94584a2013-02-07 03:02:0831 virtual void OnError(PersistentPrefStore::PrefReadError error) OVERRIDE {
[email protected]70a317a2012-12-19 20:59:3332 callback_.Run(error);
[email protected]845b43a82011-05-11 10:14:4333 }
[email protected]70a317a2012-12-19 20:59:3334
35 private:
36 base::Callback<void(PersistentPrefStore::PrefReadError)> callback_;
[email protected]845b43a82011-05-11 10:14:4337};
38
initial.commit09911bf2008-07-26 23:55:2939} // namespace
40
[email protected]70a317a2012-12-19 20:59:3341PrefService::PrefService(
42 PrefNotifierImpl* pref_notifier,
43 PrefValueStore* pref_value_store,
44 PersistentPrefStore* user_prefs,
[email protected]b1de2c72013-02-06 02:45:4745 PrefRegistry* pref_registry,
[email protected]70a317a2012-12-19 20:59:3346 base::Callback<void(PersistentPrefStore::PrefReadError)>
47 read_error_callback,
48 bool async)
[email protected]361d37f62011-11-22 10:37:0249 : pref_notifier_(pref_notifier),
50 pref_value_store_(pref_value_store),
[email protected]b1de2c72013-02-06 02:45:4751 pref_registry_(pref_registry),
[email protected]361d37f62011-11-22 10:37:0252 user_pref_store_(user_prefs),
[email protected]5b199522012-12-22 17:24:4453 read_error_callback_(read_error_callback) {
[email protected]361d37f62011-11-22 10:37:0254 pref_notifier_->SetPrefService(this);
[email protected]b1de2c72013-02-06 02:45:4755
56 pref_registry_->SetRegistrationCallback(
57 base::Bind(&PrefService::AddRegisteredPreference,
58 base::Unretained(this)));
[email protected]b1de2c72013-02-06 02:45:4759 AddInitialPreferences();
60
[email protected]361d37f62011-11-22 10:37:0261 InitFromStorage(async);
[email protected]9a8c4022011-01-25 14:25:3362}
63
initial.commit09911bf2008-07-26 23:55:2964PrefService::~PrefService() {
65 DCHECK(CalledOnValidThread());
[email protected]a98ce1262011-01-28 13:20:2366
[email protected]eeec6ca2013-02-21 15:10:4367 // Remove our callback, setting a NULL one.
[email protected]b1de2c72013-02-06 02:45:4768 pref_registry_->SetRegistrationCallback(PrefRegistry::RegistrationCallback());
[email protected]b1de2c72013-02-06 02:45:4769
[email protected]a98ce1262011-01-28 13:20:2370 // Reset pointers so accesses after destruction reliably crash.
71 pref_value_store_.reset();
[email protected]b1de2c72013-02-06 02:45:4772 pref_registry_ = NULL;
[email protected]a98ce1262011-01-28 13:20:2373 user_pref_store_ = NULL;
[email protected]42f23782012-06-08 19:11:5374 pref_notifier_.reset();
initial.commit09911bf2008-07-26 23:55:2975}
76
[email protected]845b43a82011-05-11 10:14:4377void PrefService::InitFromStorage(bool async) {
78 if (!async) {
[email protected]70a317a2012-12-19 20:59:3379 read_error_callback_.Run(user_pref_store_->ReadPrefs());
[email protected]844a1002011-04-19 11:37:2180 } else {
[email protected]845b43a82011-05-11 10:14:4381 // Guarantee that initialization happens after this function returned.
82 MessageLoop::current()->PostTask(
83 FROM_HERE,
[email protected]bebe69432011-09-28 18:36:4584 base::Bind(&PersistentPrefStore::ReadPrefsAsync,
85 user_pref_store_.get(),
[email protected]70a317a2012-12-19 20:59:3386 new ReadErrorHandler(read_error_callback_)));
[email protected]844a1002011-04-19 11:37:2187 }
[email protected]ba399672010-04-06 15:42:3988}
89
90bool PrefService::ReloadPersistentPrefs() {
[email protected]f2d1f612010-12-09 15:10:1791 return user_pref_store_->ReadPrefs() ==
92 PersistentPrefStore::PREF_READ_ERROR_NONE;
initial.commit09911bf2008-07-26 23:55:2993}
94
[email protected]3826fed2011-03-25 10:59:5695void PrefService::CommitPendingWrite() {
96 DCHECK(CalledOnValidThread());
97 user_pref_store_->CommitPendingWrite();
98}
99
[email protected]57ecc4b2010-08-11 03:02:51100bool PrefService::GetBoolean(const char* path) const {
initial.commit09911bf2008-07-26 23:55:29101 DCHECK(CalledOnValidThread());
102
103 bool result = false;
initial.commit09911bf2008-07-26 23:55:29104
[email protected]18038f82012-11-20 13:46:58105 const base::Value* value = GetPreferenceValue(path);
106 if (!value) {
[email protected]b154e6f2009-03-06 01:52:40107 NOTREACHED() << "Trying to read an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29108 return result;
109 }
[email protected]18038f82012-11-20 13:46:58110 bool rv = value->GetAsBoolean(&result);
initial.commit09911bf2008-07-26 23:55:29111 DCHECK(rv);
112 return result;
113}
114
[email protected]57ecc4b2010-08-11 03:02:51115int PrefService::GetInteger(const char* path) const {
initial.commit09911bf2008-07-26 23:55:29116 DCHECK(CalledOnValidThread());
117
118 int result = 0;
initial.commit09911bf2008-07-26 23:55:29119
[email protected]18038f82012-11-20 13:46:58120 const base::Value* value = GetPreferenceValue(path);
121 if (!value) {
[email protected]b154e6f2009-03-06 01:52:40122 NOTREACHED() << "Trying to read an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29123 return result;
124 }
[email protected]18038f82012-11-20 13:46:58125 bool rv = value->GetAsInteger(&result);
initial.commit09911bf2008-07-26 23:55:29126 DCHECK(rv);
127 return result;
128}
129
[email protected]fb534c92011-02-01 01:02:07130double PrefService::GetDouble(const char* path) const {
initial.commit09911bf2008-07-26 23:55:29131 DCHECK(CalledOnValidThread());
132
133 double result = 0.0;
initial.commit09911bf2008-07-26 23:55:29134
[email protected]18038f82012-11-20 13:46:58135 const base::Value* value = GetPreferenceValue(path);
136 if (!value) {
[email protected]b154e6f2009-03-06 01:52:40137 NOTREACHED() << "Trying to read an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29138 return result;
139 }
[email protected]18038f82012-11-20 13:46:58140 bool rv = value->GetAsDouble(&result);
initial.commit09911bf2008-07-26 23:55:29141 DCHECK(rv);
142 return result;
143}
144
[email protected]57ecc4b2010-08-11 03:02:51145std::string PrefService::GetString(const char* path) const {
initial.commit09911bf2008-07-26 23:55:29146 DCHECK(CalledOnValidThread());
147
[email protected]ddd231e2010-06-29 20:35:19148 std::string result;
[email protected]8e50b602009-03-03 22:59:43149
[email protected]18038f82012-11-20 13:46:58150 const base::Value* value = GetPreferenceValue(path);
151 if (!value) {
[email protected]b154e6f2009-03-06 01:52:40152 NOTREACHED() << "Trying to read an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29153 return result;
154 }
[email protected]18038f82012-11-20 13:46:58155 bool rv = value->GetAsString(&result);
initial.commit09911bf2008-07-26 23:55:29156 DCHECK(rv);
157 return result;
158}
159
[email protected]650b2d52013-02-10 03:41:45160base::FilePath PrefService::GetFilePath(const char* path) const {
[email protected]b9636002009-03-04 00:05:25161 DCHECK(CalledOnValidThread());
162
[email protected]650b2d52013-02-10 03:41:45163 base::FilePath result;
[email protected]b9636002009-03-04 00:05:25164
[email protected]18038f82012-11-20 13:46:58165 const base::Value* value = GetPreferenceValue(path);
166 if (!value) {
[email protected]b154e6f2009-03-06 01:52:40167 NOTREACHED() << "Trying to read an unregistered pref: " << path;
[email protected]650b2d52013-02-10 03:41:45168 return base::FilePath(result);
[email protected]b9636002009-03-04 00:05:25169 }
[email protected]18038f82012-11-20 13:46:58170 bool rv = base::GetValueAsFilePath(*value, &result);
[email protected]b9636002009-03-04 00:05:25171 DCHECK(rv);
[email protected]68d9d352011-02-21 16:35:04172 return result;
[email protected]b9636002009-03-04 00:05:25173}
174
[email protected]57ecc4b2010-08-11 03:02:51175bool PrefService::HasPrefPath(const char* path) const {
[email protected]9a8c4022011-01-25 14:25:33176 const Preference* pref = FindPreference(path);
177 return pref && !pref->IsDefaultValue();
initial.commit09911bf2008-07-26 23:55:29178}
179
[email protected]ebd0b022011-01-27 13:24:14180DictionaryValue* PrefService::GetPreferenceValues() const {
181 DCHECK(CalledOnValidThread());
182 DictionaryValue* out = new DictionaryValue;
[email protected]b1de2c72013-02-06 02:45:47183 PrefRegistry::const_iterator i = pref_registry_->begin();
184 for (; i != pref_registry_->end(); ++i) {
[email protected]18038f82012-11-20 13:46:58185 const Value* value = GetPreferenceValue(i->first);
[email protected]8874e02172011-03-11 19:44:08186 DCHECK(value);
[email protected]ebd0b022011-01-27 13:24:14187 out->Set(i->first, value->DeepCopy());
188 }
189 return out;
190}
191
initial.commit09911bf2008-07-26 23:55:29192const PrefService::Preference* PrefService::FindPreference(
[email protected]57ecc4b2010-08-11 03:02:51193 const char* pref_name) const {
initial.commit09911bf2008-07-26 23:55:29194 DCHECK(CalledOnValidThread());
[email protected]d15d5682012-10-23 17:50:42195 PreferenceMap::iterator it = prefs_map_.find(pref_name);
196 if (it != prefs_map_.end())
197 return &(it->second);
[email protected]b1de2c72013-02-06 02:45:47198 const base::Value* default_value = NULL;
199 if (!pref_registry_->defaults()->GetValue(pref_name, &default_value))
[email protected]9a8c4022011-01-25 14:25:33200 return NULL;
[email protected]d15d5682012-10-23 17:50:42201 it = prefs_map_.insert(
[email protected]b1de2c72013-02-06 02:45:47202 std::make_pair(pref_name, Preference(
203 this, pref_name, default_value->GetType()))).first;
[email protected]d15d5682012-10-23 17:50:42204 return &(it->second);
initial.commit09911bf2008-07-26 23:55:29205}
206
[email protected]acd78969c2010-12-08 09:49:11207bool PrefService::ReadOnly() const {
[email protected]f2d1f612010-12-09 15:10:17208 return user_pref_store_->ReadOnly();
[email protected]acd78969c2010-12-08 09:49:11209}
210
[email protected]59c10712012-03-13 02:10:34211PrefService::PrefInitializationStatus PrefService::GetInitializationStatus()
212 const {
213 if (!user_pref_store_->IsInitializationComplete())
214 return INITIALIZATION_STATUS_WAITING;
215
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:
[email protected]88c6fb52013-04-09 10:39:18220 return INITIALIZATION_STATUS_CREATED_NEW_PREF_STORE;
[email protected]59c10712012-03-13 02:10:34221 default:
222 return INITIALIZATION_STATUS_ERROR;
223 }
224}
225
[email protected]57ecc4b2010-08-11 03:02:51226bool PrefService::IsManagedPreference(const char* pref_name) const {
[email protected]d90de1c02010-07-19 19:50:48227 const Preference* pref = FindPreference(pref_name);
[email protected]9a8c4022011-01-25 14:25:33228 return pref && pref->IsManaged();
[email protected]d90de1c02010-07-19 19:50:48229}
230
[email protected]d6bbd2932012-03-19 17:10:07231bool PrefService::IsUserModifiablePreference(const char* pref_name) const {
232 const Preference* pref = FindPreference(pref_name);
233 return pref && pref->IsUserModifiable();
234}
235
[email protected]57ecc4b2010-08-11 03:02:51236const DictionaryValue* PrefService::GetDictionary(const char* path) const {
initial.commit09911bf2008-07-26 23:55:29237 DCHECK(CalledOnValidThread());
238
[email protected]18038f82012-11-20 13:46:58239 const Value* value = GetPreferenceValue(path);
240 if (!value) {
[email protected]b154e6f2009-03-06 01:52:40241 NOTREACHED() << "Trying to read an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29242 return NULL;
243 }
[email protected]11b040b2011-02-02 12:42:25244 if (value->GetType() != Value::TYPE_DICTIONARY) {
245 NOTREACHED();
initial.commit09911bf2008-07-26 23:55:29246 return NULL;
[email protected]11b040b2011-02-02 12:42:25247 }
initial.commit09911bf2008-07-26 23:55:29248 return static_cast<const DictionaryValue*>(value);
249}
250
[email protected]1a5a31f2012-04-26 20:21:34251const base::Value* PrefService::GetUserPrefValue(const char* path) const {
252 DCHECK(CalledOnValidThread());
253
254 const Preference* pref = FindPreference(path);
255 if (!pref) {
256 NOTREACHED() << "Trying to get an unregistered pref: " << path;
257 return NULL;
258 }
259
260 // Look for an existing preference in the user store. If it doesn't
261 // exist, return NULL.
262 base::Value* value = NULL;
[email protected]892f1d62012-11-08 18:24:34263 if (!user_pref_store_->GetMutableValue(path, &value))
[email protected]1a5a31f2012-04-26 20:21:34264 return NULL;
[email protected]1a5a31f2012-04-26 20:21:34265
266 if (!value->IsType(pref->GetType())) {
267 NOTREACHED() << "Pref value type doesn't match registered type.";
268 return NULL;
269 }
270
271 return value;
272}
273
[email protected]5879cef2013-03-02 17:02:25274void PrefService::SetDefaultPrefValue(const char* path,
275 base::Value* value) {
276 DCHECK(CalledOnValidThread());
277 pref_registry_->SetDefaultPrefValue(path, value);
278}
279
[email protected]35a6fd12012-05-28 18:08:08280const 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;
[email protected]b1de2c72013-02-06 02:45:47284 if (!pref_registry_->defaults()->GetValue(path, &value)) {
[email protected]35a6fd12012-05-28 18:08:08285 NOTREACHED() << "Default value missing for pref: " << path;
286 return NULL;
287 }
288 return value;
289}
290
[email protected]57ecc4b2010-08-11 03:02:51291const ListValue* PrefService::GetList(const char* path) const {
initial.commit09911bf2008-07-26 23:55:29292 DCHECK(CalledOnValidThread());
293
[email protected]18038f82012-11-20 13:46:58294 const Value* value = GetPreferenceValue(path);
295 if (!value) {
[email protected]b154e6f2009-03-06 01:52:40296 NOTREACHED() << "Trying to read an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29297 return NULL;
298 }
[email protected]11b040b2011-02-02 12:42:25299 if (value->GetType() != Value::TYPE_LIST) {
300 NOTREACHED();
initial.commit09911bf2008-07-26 23:55:29301 return NULL;
[email protected]11b040b2011-02-02 12:42:25302 }
initial.commit09911bf2008-07-26 23:55:29303 return static_cast<const ListValue*>(value);
304}
305
[email protected]a6a7ced2012-11-01 17:24:18306void PrefService::AddPrefObserver(const char* path, PrefObserver* obs) {
[email protected]d81288a02010-08-18 07:25:50307 pref_notifier_->AddPrefObserver(path, obs);
initial.commit09911bf2008-07-26 23:55:29308}
309
[email protected]a6a7ced2012-11-01 17:24:18310void PrefService::RemovePrefObserver(const char* path, PrefObserver* obs) {
[email protected]d81288a02010-08-18 07:25:50311 pref_notifier_->RemovePrefObserver(path, obs);
initial.commit09911bf2008-07-26 23:55:29312}
313
[email protected]a6a7ced2012-11-01 17:24:18314void PrefService::AddPrefInitObserver(base::Callback<void(bool)> obs) {
315 pref_notifier_->AddInitObserver(obs);
316}
317
[email protected]b1de2c72013-02-06 02:45:47318PrefRegistry* PrefService::DeprecatedGetPrefRegistry() {
319 return pref_registry_.get();
320}
321
322void 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);
327 }
328}
329
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.
335void PrefService::AddRegisteredPreference(const char* path,
336 Value* default_value) {
initial.commit09911bf2008-07-26 23:55:29337 DCHECK(CalledOnValidThread());
338
[email protected]ea3e4972012-04-12 03:41:37339 // 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;
[email protected]b1de2c72013-02-06 02:45:47344 base::Value::Type orig_type = default_value->GetType();
[email protected]ea3e4972012-04-12 03:41:37345 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;
353 }
354 if (needs_empty_value)
355 user_pref_store_->MarkNeedsEmptyValue(path);
initial.commit09911bf2008-07-26 23:55:29356}
357
[email protected]57ecc4b2010-08-11 03:02:51358void PrefService::ClearPref(const char* path) {
initial.commit09911bf2008-07-26 23:55:29359 DCHECK(CalledOnValidThread());
360
361 const Preference* pref = FindPreference(path);
362 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40363 NOTREACHED() << "Trying to clear an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29364 return;
365 }
[email protected]f2d1f612010-12-09 15:10:17366 user_pref_store_->RemoveValue(path);
initial.commit09911bf2008-07-26 23:55:29367}
368
[email protected]57ecc4b2010-08-11 03:02:51369void PrefService::Set(const char* path, const Value& value) {
[email protected]b99c41c2011-04-27 15:18:48370 SetUserPrefValue(path, value.DeepCopy());
[email protected]a048d7e42009-12-01 01:02:39371}
372
[email protected]57ecc4b2010-08-11 03:02:51373void PrefService::SetBoolean(const char* path, bool value) {
[email protected]c3b54f372010-09-14 08:25:07374 SetUserPrefValue(path, Value::CreateBooleanValue(value));
initial.commit09911bf2008-07-26 23:55:29375}
376
[email protected]57ecc4b2010-08-11 03:02:51377void PrefService::SetInteger(const char* path, int value) {
[email protected]c3b54f372010-09-14 08:25:07378 SetUserPrefValue(path, Value::CreateIntegerValue(value));
initial.commit09911bf2008-07-26 23:55:29379}
380
[email protected]fb534c92011-02-01 01:02:07381void PrefService::SetDouble(const char* path, double value) {
382 SetUserPrefValue(path, Value::CreateDoubleValue(value));
initial.commit09911bf2008-07-26 23:55:29383}
384
[email protected]57ecc4b2010-08-11 03:02:51385void PrefService::SetString(const char* path, const std::string& value) {
[email protected]c3b54f372010-09-14 08:25:07386 SetUserPrefValue(path, Value::CreateStringValue(value));
initial.commit09911bf2008-07-26 23:55:29387}
388
[email protected]650b2d52013-02-10 03:41:45389void PrefService::SetFilePath(const char* path, const base::FilePath& value) {
[email protected]8703b2b2011-03-15 09:51:50390 SetUserPrefValue(path, base::CreateFilePathValue(value));
[email protected]b9636002009-03-04 00:05:25391}
392
[email protected]57ecc4b2010-08-11 03:02:51393void PrefService::SetInt64(const char* path, int64 value) {
[email protected]c3b54f372010-09-14 08:25:07394 SetUserPrefValue(path, Value::CreateStringValue(base::Int64ToString(value)));
[email protected]0bb1a622009-03-04 03:22:32395}
396
[email protected]57ecc4b2010-08-11 03:02:51397int64 PrefService::GetInt64(const char* path) const {
[email protected]0bb1a622009-03-04 03:22:32398 DCHECK(CalledOnValidThread());
399
[email protected]18038f82012-11-20 13:46:58400 const Value* value = GetPreferenceValue(path);
401 if (!value) {
[email protected]b154e6f2009-03-06 01:52:40402 NOTREACHED() << "Trying to read an unregistered pref: " << path;
[email protected]c3453302009-12-01 01:33:08403 return 0;
[email protected]0bb1a622009-03-04 03:22:32404 }
[email protected]dc9a6762010-08-16 07:13:53405 std::string result("0");
[email protected]18038f82012-11-20 13:46:58406 bool rv = value->GetAsString(&result);
[email protected]0bb1a622009-03-04 03:22:32407 DCHECK(rv);
[email protected]e83326f2010-07-31 17:29:25408
409 int64 val;
[email protected]dc9a6762010-08-16 07:13:53410 base::StringToInt64(result, &val);
[email protected]e83326f2010-07-31 17:29:25411 return val;
[email protected]0bb1a622009-03-04 03:22:32412}
413
[email protected]3cbe0812012-07-03 02:51:43414void PrefService::SetUint64(const char* path, uint64 value) {
415 SetUserPrefValue(path, Value::CreateStringValue(base::Uint64ToString(value)));
416}
417
418uint64 PrefService::GetUint64(const char* path) const {
419 DCHECK(CalledOnValidThread());
420
[email protected]18038f82012-11-20 13:46:58421 const Value* value = GetPreferenceValue(path);
422 if (!value) {
[email protected]3cbe0812012-07-03 02:51:43423 NOTREACHED() << "Trying to read an unregistered pref: " << path;
424 return 0;
425 }
426 std::string result("0");
[email protected]18038f82012-11-20 13:46:58427 bool rv = value->GetAsString(&result);
[email protected]3cbe0812012-07-03 02:51:43428 DCHECK(rv);
429
430 uint64 val;
431 base::StringToUint64(result, &val);
432 return val;
433}
434
[email protected]26418b72011-03-30 14:07:39435Value* PrefService::GetMutableUserPref(const char* path,
[email protected]bab1c13f2011-08-12 20:59:02436 base::Value::Type type) {
[email protected]26418b72011-03-30 14:07:39437 CHECK(type == Value::TYPE_DICTIONARY || type == Value::TYPE_LIST);
initial.commit09911bf2008-07-26 23:55:29438 DCHECK(CalledOnValidThread());
439
440 const Preference* pref = FindPreference(path);
441 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40442 NOTREACHED() << "Trying to get an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29443 return NULL;
444 }
[email protected]26418b72011-03-30 14:07:39445 if (pref->GetType() != type) {
446 NOTREACHED() << "Wrong type for GetMutableValue: " << path;
initial.commit09911bf2008-07-26 23:55:29447 return NULL;
448 }
449
[email protected]e0250892010-10-01 18:57:53450 // 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.
[email protected]26418b72011-03-30 14:07:39452 Value* value = NULL;
[email protected]892f1d62012-11-08 18:24:34453 if (!user_pref_store_->GetMutableValue(path, &value) ||
[email protected]26418b72011-03-30 14:07:39454 !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();
461 }
462 user_pref_store_->SetValueSilently(path, value);
initial.commit09911bf2008-07-26 23:55:29463 }
[email protected]26418b72011-03-30 14:07:39464 return value;
465}
466
[email protected]68bf41a2011-03-25 16:38:31467void PrefService::ReportUserPrefChanged(const std::string& key) {
[email protected]f89ee342011-03-07 09:28:27468 user_pref_store_->ReportValueChanged(key);
469}
470
[email protected]c3b54f372010-09-14 08:25:07471void PrefService::SetUserPrefValue(const char* path, Value* new_value) {
[email protected]b99c41c2011-04-27 15:18:48472 scoped_ptr<Value> owned_value(new_value);
[email protected]c3b54f372010-09-14 08:25:07473 DCHECK(CalledOnValidThread());
474
475 const Preference* pref = FindPreference(path);
476 if (!pref) {
477 NOTREACHED() << "Trying to write an unregistered pref: " << path;
478 return;
479 }
[email protected]99cc9a02010-09-17 07:53:28480 if (pref->GetType() != new_value->GetType()) {
481 NOTREACHED() << "Trying to set pref " << path
482 << " of type " << pref->GetType()
[email protected]c3b54f372010-09-14 08:25:07483 << " to value of type " << new_value->GetType();
484 return;
485 }
486
[email protected]b99c41c2011-04-27 15:18:48487 user_pref_store_->SetValue(path, owned_value.release());
[email protected]73c47932010-12-06 18:13:43488}
489
[email protected]5b199522012-12-22 17:24:44490void PrefService::UpdateCommandLinePrefStore(PrefStore* command_line_store) {
491 pref_value_store_->UpdateCommandLinePrefStore(command_line_store);
[email protected]d3b05ea2012-01-24 22:57:05492}
493
initial.commit09911bf2008-07-26 23:55:29494///////////////////////////////////////////////////////////////////////////////
495// PrefService::Preference
496
[email protected]c3b54f372010-09-14 08:25:07497PrefService::Preference::Preference(const PrefService* service,
[email protected]9a8c4022011-01-25 14:25:33498 const char* name,
[email protected]bab1c13f2011-08-12 20:59:02499 base::Value::Type type)
[email protected]99cc9a02010-09-17 07:53:28500 : name_(name),
[email protected]9a8c4022011-01-25 14:25:33501 type_(type),
[email protected]c3b54f372010-09-14 08:25:07502 pref_service_(service) {
initial.commit09911bf2008-07-26 23:55:29503 DCHECK(name);
[email protected]c3b54f372010-09-14 08:25:07504 DCHECK(service);
[email protected]99cc9a02010-09-17 07:53:28505}
506
[email protected]fb8fdf12012-08-21 16:28:20507const std::string PrefService::Preference::name() const {
508 return name_;
509}
510
[email protected]bab1c13f2011-08-12 20:59:02511base::Value::Type PrefService::Preference::GetType() const {
[email protected]9a8c4022011-01-25 14:25:33512 return type_;
initial.commit09911bf2008-07-26 23:55:29513}
514
515const Value* PrefService::Preference::GetValue() const {
[email protected]18038f82012-11-20 13:46:58516 const Value* result= pref_service_->GetPreferenceValue(name_);
517 DCHECK(result) << "Must register pref before getting its value";
518 return result;
[email protected]40a47c162010-09-09 11:14:01519}
520
[email protected]7ca0f362012-07-30 10:14:03521const Value* PrefService::Preference::GetRecommendedValue() const {
522 DCHECK(pref_service_->FindPreference(name_.c_str())) <<
523 "Must register pref before getting its value";
524
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;
529 }
530
531 // The pref has no recommended value.
532 return NULL;
533}
534
[email protected]40a47c162010-09-09 11:14:01535bool PrefService::Preference::IsManaged() const {
[email protected]887288f02011-02-04 22:52:46536 return pref_value_store()->PrefValueInManagedStore(name_.c_str());
[email protected]40a47c162010-09-09 11:14:01537}
538
[email protected]a5437282011-12-12 12:33:21539bool PrefService::Preference::IsRecommended() const {
540 return pref_value_store()->PrefValueFromRecommendedStore(name_.c_str());
541}
542
[email protected]40a47c162010-09-09 11:14:01543bool PrefService::Preference::HasExtensionSetting() const {
[email protected]887288f02011-02-04 22:52:46544 return pref_value_store()->PrefValueInExtensionStore(name_.c_str());
[email protected]40a47c162010-09-09 11:14:01545}
546
547bool PrefService::Preference::HasUserSetting() const {
[email protected]887288f02011-02-04 22:52:46548 return pref_value_store()->PrefValueInUserStore(name_.c_str());
[email protected]40a47c162010-09-09 11:14:01549}
550
551bool PrefService::Preference::IsExtensionControlled() const {
[email protected]887288f02011-02-04 22:52:46552 return pref_value_store()->PrefValueFromExtensionStore(name_.c_str());
[email protected]40a47c162010-09-09 11:14:01553}
554
555bool PrefService::Preference::IsUserControlled() const {
[email protected]887288f02011-02-04 22:52:46556 return pref_value_store()->PrefValueFromUserStore(name_.c_str());
[email protected]c3b54f372010-09-14 08:25:07557}
558
559bool PrefService::Preference::IsDefaultValue() const {
[email protected]887288f02011-02-04 22:52:46560 return pref_value_store()->PrefValueFromDefaultStore(name_.c_str());
[email protected]d7449e82010-07-14 11:42:35561}
[email protected]74379bc52010-07-21 13:54:08562
563bool PrefService::Preference::IsUserModifiable() const {
[email protected]887288f02011-02-04 22:52:46564 return pref_value_store()->PrefValueUserModifiable(name_.c_str());
[email protected]74379bc52010-07-21 13:54:08565}
[email protected]9a28f132011-02-24 21:15:16566
567bool PrefService::Preference::IsExtensionModifiable() const {
568 return pref_value_store()->PrefValueExtensionModifiable(name_.c_str());
569}
[email protected]18038f82012-11-20 13:46:58570
571const base::Value* PrefService::GetPreferenceValue(
572 const std::string& path) const {
573 DCHECK(CalledOnValidThread());
[email protected]b1de2c72013-02-06 02:45:47574 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;
584 }
[email protected]18038f82012-11-20 13:46:58585 }
586
[email protected]18038f82012-11-20 13:46:58587 return NULL;
588}