blob: b8606f984f1b1fad69185642193953217d50deba [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"
[email protected]495cad92013-07-18 08:12:4012#include "base/message_loop/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"
[email protected]3ea1b182013-02-08 22:38:4119#include "base/strings/string_number_conversions.h"
[email protected]d529cb02013-06-10 19:06:5720#include "base/strings/string_util.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
24namespace {
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.
[email protected]9e7154122013-05-30 23:11:0482 base::MessageLoop::current()->PostTask(
[email protected]845b43a82011-05-11 10:14:4383 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
[email protected]3826fed2011-03-25 10:59:5690void PrefService::CommitPendingWrite() {
91 DCHECK(CalledOnValidThread());
92 user_pref_store_->CommitPendingWrite();
93}
94
[email protected]57ecc4b2010-08-11 03:02:5195bool PrefService::GetBoolean(const char* path) const {
initial.commit09911bf2008-07-26 23:55:2996 DCHECK(CalledOnValidThread());
97
98 bool result = false;
initial.commit09911bf2008-07-26 23:55:2999
[email protected]18038f82012-11-20 13:46:58100 const base::Value* value = GetPreferenceValue(path);
101 if (!value) {
[email protected]b154e6f2009-03-06 01:52:40102 NOTREACHED() << "Trying to read an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29103 return result;
104 }
[email protected]18038f82012-11-20 13:46:58105 bool rv = value->GetAsBoolean(&result);
initial.commit09911bf2008-07-26 23:55:29106 DCHECK(rv);
107 return result;
108}
109
[email protected]57ecc4b2010-08-11 03:02:51110int PrefService::GetInteger(const char* path) const {
initial.commit09911bf2008-07-26 23:55:29111 DCHECK(CalledOnValidThread());
112
113 int result = 0;
initial.commit09911bf2008-07-26 23:55:29114
[email protected]18038f82012-11-20 13:46:58115 const base::Value* value = GetPreferenceValue(path);
116 if (!value) {
[email protected]b154e6f2009-03-06 01:52:40117 NOTREACHED() << "Trying to read an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29118 return result;
119 }
[email protected]18038f82012-11-20 13:46:58120 bool rv = value->GetAsInteger(&result);
initial.commit09911bf2008-07-26 23:55:29121 DCHECK(rv);
122 return result;
123}
124
[email protected]fb534c92011-02-01 01:02:07125double PrefService::GetDouble(const char* path) const {
initial.commit09911bf2008-07-26 23:55:29126 DCHECK(CalledOnValidThread());
127
128 double result = 0.0;
initial.commit09911bf2008-07-26 23:55:29129
[email protected]18038f82012-11-20 13:46:58130 const base::Value* value = GetPreferenceValue(path);
131 if (!value) {
[email protected]b154e6f2009-03-06 01:52:40132 NOTREACHED() << "Trying to read an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29133 return result;
134 }
[email protected]18038f82012-11-20 13:46:58135 bool rv = value->GetAsDouble(&result);
initial.commit09911bf2008-07-26 23:55:29136 DCHECK(rv);
137 return result;
138}
139
[email protected]57ecc4b2010-08-11 03:02:51140std::string PrefService::GetString(const char* path) const {
initial.commit09911bf2008-07-26 23:55:29141 DCHECK(CalledOnValidThread());
142
[email protected]ddd231e2010-06-29 20:35:19143 std::string result;
[email protected]8e50b602009-03-03 22:59:43144
[email protected]18038f82012-11-20 13:46:58145 const base::Value* value = GetPreferenceValue(path);
146 if (!value) {
[email protected]b154e6f2009-03-06 01:52:40147 NOTREACHED() << "Trying to read an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29148 return result;
149 }
[email protected]18038f82012-11-20 13:46:58150 bool rv = value->GetAsString(&result);
initial.commit09911bf2008-07-26 23:55:29151 DCHECK(rv);
152 return result;
153}
154
[email protected]650b2d52013-02-10 03:41:45155base::FilePath PrefService::GetFilePath(const char* path) const {
[email protected]b9636002009-03-04 00:05:25156 DCHECK(CalledOnValidThread());
157
[email protected]650b2d52013-02-10 03:41:45158 base::FilePath result;
[email protected]b9636002009-03-04 00:05:25159
[email protected]18038f82012-11-20 13:46:58160 const base::Value* value = GetPreferenceValue(path);
161 if (!value) {
[email protected]b154e6f2009-03-06 01:52:40162 NOTREACHED() << "Trying to read an unregistered pref: " << path;
[email protected]650b2d52013-02-10 03:41:45163 return base::FilePath(result);
[email protected]b9636002009-03-04 00:05:25164 }
[email protected]18038f82012-11-20 13:46:58165 bool rv = base::GetValueAsFilePath(*value, &result);
[email protected]b9636002009-03-04 00:05:25166 DCHECK(rv);
[email protected]68d9d352011-02-21 16:35:04167 return result;
[email protected]b9636002009-03-04 00:05:25168}
169
[email protected]57ecc4b2010-08-11 03:02:51170bool PrefService::HasPrefPath(const char* path) const {
[email protected]9a8c4022011-01-25 14:25:33171 const Preference* pref = FindPreference(path);
172 return pref && !pref->IsDefaultValue();
initial.commit09911bf2008-07-26 23:55:29173}
174
[email protected]a43a667b2013-06-14 17:56:08175base::DictionaryValue* PrefService::GetPreferenceValues() const {
[email protected]ebd0b022011-01-27 13:24:14176 DCHECK(CalledOnValidThread());
[email protected]a43a667b2013-06-14 17:56:08177 base::DictionaryValue* out = new base::DictionaryValue;
[email protected]b1de2c72013-02-06 02:45:47178 PrefRegistry::const_iterator i = pref_registry_->begin();
179 for (; i != pref_registry_->end(); ++i) {
[email protected]a43a667b2013-06-14 17:56:08180 const base::Value* value = GetPreferenceValue(i->first);
[email protected]8874e02172011-03-11 19:44:08181 DCHECK(value);
[email protected]ebd0b022011-01-27 13:24:14182 out->Set(i->first, value->DeepCopy());
183 }
184 return out;
185}
186
[email protected]2a1965c2013-10-02 16:07:01187base::DictionaryValue* PrefService::GetPreferenceValuesWithoutPathExpansion()
188 const {
189 DCHECK(CalledOnValidThread());
190 base::DictionaryValue* out = new base::DictionaryValue;
191 PrefRegistry::const_iterator i = pref_registry_->begin();
192 for (; i != pref_registry_->end(); ++i) {
193 const base::Value* value = GetPreferenceValue(i->first);
194 DCHECK(value);
195 out->SetWithoutPathExpansion(i->first, value->DeepCopy());
196 }
197 return out;
198}
199
initial.commit09911bf2008-07-26 23:55:29200const PrefService::Preference* PrefService::FindPreference(
[email protected]57ecc4b2010-08-11 03:02:51201 const char* pref_name) const {
initial.commit09911bf2008-07-26 23:55:29202 DCHECK(CalledOnValidThread());
[email protected]d15d5682012-10-23 17:50:42203 PreferenceMap::iterator it = prefs_map_.find(pref_name);
204 if (it != prefs_map_.end())
205 return &(it->second);
[email protected]b1de2c72013-02-06 02:45:47206 const base::Value* default_value = NULL;
207 if (!pref_registry_->defaults()->GetValue(pref_name, &default_value))
[email protected]9a8c4022011-01-25 14:25:33208 return NULL;
[email protected]d15d5682012-10-23 17:50:42209 it = prefs_map_.insert(
[email protected]b1de2c72013-02-06 02:45:47210 std::make_pair(pref_name, Preference(
211 this, pref_name, default_value->GetType()))).first;
[email protected]d15d5682012-10-23 17:50:42212 return &(it->second);
initial.commit09911bf2008-07-26 23:55:29213}
214
[email protected]acd78969c2010-12-08 09:49:11215bool PrefService::ReadOnly() const {
[email protected]f2d1f612010-12-09 15:10:17216 return user_pref_store_->ReadOnly();
[email protected]acd78969c2010-12-08 09:49:11217}
218
[email protected]59c10712012-03-13 02:10:34219PrefService::PrefInitializationStatus PrefService::GetInitializationStatus()
220 const {
221 if (!user_pref_store_->IsInitializationComplete())
222 return INITIALIZATION_STATUS_WAITING;
223
224 switch (user_pref_store_->GetReadError()) {
225 case PersistentPrefStore::PREF_READ_ERROR_NONE:
226 return INITIALIZATION_STATUS_SUCCESS;
227 case PersistentPrefStore::PREF_READ_ERROR_NO_FILE:
[email protected]88c6fb52013-04-09 10:39:18228 return INITIALIZATION_STATUS_CREATED_NEW_PREF_STORE;
[email protected]59c10712012-03-13 02:10:34229 default:
230 return INITIALIZATION_STATUS_ERROR;
231 }
232}
233
[email protected]57ecc4b2010-08-11 03:02:51234bool PrefService::IsManagedPreference(const char* pref_name) const {
[email protected]d90de1c02010-07-19 19:50:48235 const Preference* pref = FindPreference(pref_name);
[email protected]9a8c4022011-01-25 14:25:33236 return pref && pref->IsManaged();
[email protected]d90de1c02010-07-19 19:50:48237}
238
[email protected]d6bbd2932012-03-19 17:10:07239bool PrefService::IsUserModifiablePreference(const char* pref_name) const {
240 const Preference* pref = FindPreference(pref_name);
241 return pref && pref->IsUserModifiable();
242}
243
[email protected]a43a667b2013-06-14 17:56:08244const base::DictionaryValue* PrefService::GetDictionary(
245 const char* path) const {
initial.commit09911bf2008-07-26 23:55:29246 DCHECK(CalledOnValidThread());
247
[email protected]a43a667b2013-06-14 17:56:08248 const base::Value* value = GetPreferenceValue(path);
[email protected]18038f82012-11-20 13:46:58249 if (!value) {
[email protected]b154e6f2009-03-06 01:52:40250 NOTREACHED() << "Trying to read an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29251 return NULL;
252 }
[email protected]a43a667b2013-06-14 17:56:08253 if (value->GetType() != base::Value::TYPE_DICTIONARY) {
[email protected]11b040b2011-02-02 12:42:25254 NOTREACHED();
initial.commit09911bf2008-07-26 23:55:29255 return NULL;
[email protected]11b040b2011-02-02 12:42:25256 }
[email protected]a43a667b2013-06-14 17:56:08257 return static_cast<const base::DictionaryValue*>(value);
initial.commit09911bf2008-07-26 23:55:29258}
259
[email protected]1a5a31f2012-04-26 20:21:34260const base::Value* PrefService::GetUserPrefValue(const char* path) const {
261 DCHECK(CalledOnValidThread());
262
263 const Preference* pref = FindPreference(path);
264 if (!pref) {
265 NOTREACHED() << "Trying to get an unregistered pref: " << path;
266 return NULL;
267 }
268
269 // Look for an existing preference in the user store. If it doesn't
270 // exist, return NULL.
271 base::Value* value = NULL;
[email protected]892f1d62012-11-08 18:24:34272 if (!user_pref_store_->GetMutableValue(path, &value))
[email protected]1a5a31f2012-04-26 20:21:34273 return NULL;
[email protected]1a5a31f2012-04-26 20:21:34274
275 if (!value->IsType(pref->GetType())) {
276 NOTREACHED() << "Pref value type doesn't match registered type.";
277 return NULL;
278 }
279
280 return value;
281}
282
[email protected]5879cef2013-03-02 17:02:25283void PrefService::SetDefaultPrefValue(const char* path,
284 base::Value* value) {
285 DCHECK(CalledOnValidThread());
286 pref_registry_->SetDefaultPrefValue(path, value);
287}
288
[email protected]35a6fd12012-05-28 18:08:08289const base::Value* PrefService::GetDefaultPrefValue(const char* path) const {
290 DCHECK(CalledOnValidThread());
291 // Lookup the preference in the default store.
292 const base::Value* value = NULL;
[email protected]b1de2c72013-02-06 02:45:47293 if (!pref_registry_->defaults()->GetValue(path, &value)) {
[email protected]35a6fd12012-05-28 18:08:08294 NOTREACHED() << "Default value missing for pref: " << path;
295 return NULL;
296 }
297 return value;
298}
299
[email protected]a43a667b2013-06-14 17:56:08300const base::ListValue* PrefService::GetList(const char* path) const {
initial.commit09911bf2008-07-26 23:55:29301 DCHECK(CalledOnValidThread());
302
[email protected]a43a667b2013-06-14 17:56:08303 const base::Value* value = GetPreferenceValue(path);
[email protected]18038f82012-11-20 13:46:58304 if (!value) {
[email protected]b154e6f2009-03-06 01:52:40305 NOTREACHED() << "Trying to read an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29306 return NULL;
307 }
[email protected]a43a667b2013-06-14 17:56:08308 if (value->GetType() != base::Value::TYPE_LIST) {
[email protected]11b040b2011-02-02 12:42:25309 NOTREACHED();
initial.commit09911bf2008-07-26 23:55:29310 return NULL;
[email protected]11b040b2011-02-02 12:42:25311 }
[email protected]a43a667b2013-06-14 17:56:08312 return static_cast<const base::ListValue*>(value);
initial.commit09911bf2008-07-26 23:55:29313}
314
[email protected]a6a7ced2012-11-01 17:24:18315void PrefService::AddPrefObserver(const char* path, PrefObserver* obs) {
[email protected]d81288a02010-08-18 07:25:50316 pref_notifier_->AddPrefObserver(path, obs);
initial.commit09911bf2008-07-26 23:55:29317}
318
[email protected]a6a7ced2012-11-01 17:24:18319void PrefService::RemovePrefObserver(const char* path, PrefObserver* obs) {
[email protected]d81288a02010-08-18 07:25:50320 pref_notifier_->RemovePrefObserver(path, obs);
initial.commit09911bf2008-07-26 23:55:29321}
322
[email protected]a6a7ced2012-11-01 17:24:18323void PrefService::AddPrefInitObserver(base::Callback<void(bool)> obs) {
324 pref_notifier_->AddInitObserver(obs);
325}
326
[email protected]b1de2c72013-02-06 02:45:47327PrefRegistry* PrefService::DeprecatedGetPrefRegistry() {
328 return pref_registry_.get();
329}
330
331void PrefService::AddInitialPreferences() {
332 for (PrefRegistry::const_iterator it = pref_registry_->begin();
333 it != pref_registry_->end();
334 ++it) {
335 AddRegisteredPreference(it->first.c_str(), it->second);
336 }
337}
338
339// TODO(joi): Once MarkNeedsEmptyValue is gone, we can probably
340// completely get rid of this method. There will be one difference in
341// semantics; currently all registered preferences are stored right
342// away in the prefs_map_, if we remove this they would be stored only
343// opportunistically.
344void PrefService::AddRegisteredPreference(const char* path,
[email protected]a43a667b2013-06-14 17:56:08345 base::Value* default_value) {
initial.commit09911bf2008-07-26 23:55:29346 DCHECK(CalledOnValidThread());
347
[email protected]ea3e4972012-04-12 03:41:37348 // For ListValue and DictionaryValue with non empty default, empty value
349 // for |path| needs to be persisted in |user_pref_store_|. So that
350 // non empty default is not used when user sets an empty ListValue or
351 // DictionaryValue.
352 bool needs_empty_value = false;
[email protected]b1de2c72013-02-06 02:45:47353 base::Value::Type orig_type = default_value->GetType();
[email protected]ea3e4972012-04-12 03:41:37354 if (orig_type == base::Value::TYPE_LIST) {
355 const base::ListValue* list = NULL;
356 if (default_value->GetAsList(&list) && !list->empty())
357 needs_empty_value = true;
358 } else if (orig_type == base::Value::TYPE_DICTIONARY) {
359 const base::DictionaryValue* dict = NULL;
360 if (default_value->GetAsDictionary(&dict) && !dict->empty())
361 needs_empty_value = true;
362 }
363 if (needs_empty_value)
364 user_pref_store_->MarkNeedsEmptyValue(path);
initial.commit09911bf2008-07-26 23:55:29365}
366
[email protected]57ecc4b2010-08-11 03:02:51367void PrefService::ClearPref(const char* path) {
initial.commit09911bf2008-07-26 23:55:29368 DCHECK(CalledOnValidThread());
369
370 const Preference* pref = FindPreference(path);
371 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40372 NOTREACHED() << "Trying to clear an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29373 return;
374 }
[email protected]f2d1f612010-12-09 15:10:17375 user_pref_store_->RemoveValue(path);
initial.commit09911bf2008-07-26 23:55:29376}
377
[email protected]a43a667b2013-06-14 17:56:08378void PrefService::Set(const char* path, const base::Value& value) {
[email protected]b99c41c2011-04-27 15:18:48379 SetUserPrefValue(path, value.DeepCopy());
[email protected]a048d7e42009-12-01 01:02:39380}
381
[email protected]57ecc4b2010-08-11 03:02:51382void PrefService::SetBoolean(const char* path, bool value) {
[email protected]a43a667b2013-06-14 17:56:08383 SetUserPrefValue(path, base::Value::CreateBooleanValue(value));
initial.commit09911bf2008-07-26 23:55:29384}
385
[email protected]57ecc4b2010-08-11 03:02:51386void PrefService::SetInteger(const char* path, int value) {
[email protected]a43a667b2013-06-14 17:56:08387 SetUserPrefValue(path, base::Value::CreateIntegerValue(value));
initial.commit09911bf2008-07-26 23:55:29388}
389
[email protected]fb534c92011-02-01 01:02:07390void PrefService::SetDouble(const char* path, double value) {
[email protected]a43a667b2013-06-14 17:56:08391 SetUserPrefValue(path, base::Value::CreateDoubleValue(value));
initial.commit09911bf2008-07-26 23:55:29392}
393
[email protected]57ecc4b2010-08-11 03:02:51394void PrefService::SetString(const char* path, const std::string& value) {
[email protected]a43a667b2013-06-14 17:56:08395 SetUserPrefValue(path, base::Value::CreateStringValue(value));
initial.commit09911bf2008-07-26 23:55:29396}
397
[email protected]650b2d52013-02-10 03:41:45398void PrefService::SetFilePath(const char* path, const base::FilePath& value) {
[email protected]8703b2b2011-03-15 09:51:50399 SetUserPrefValue(path, base::CreateFilePathValue(value));
[email protected]b9636002009-03-04 00:05:25400}
401
[email protected]57ecc4b2010-08-11 03:02:51402void PrefService::SetInt64(const char* path, int64 value) {
[email protected]a43a667b2013-06-14 17:56:08403 SetUserPrefValue(path,
404 base::Value::CreateStringValue(base::Int64ToString(value)));
[email protected]0bb1a622009-03-04 03:22:32405}
406
[email protected]57ecc4b2010-08-11 03:02:51407int64 PrefService::GetInt64(const char* path) const {
[email protected]0bb1a622009-03-04 03:22:32408 DCHECK(CalledOnValidThread());
409
[email protected]a43a667b2013-06-14 17:56:08410 const base::Value* value = GetPreferenceValue(path);
[email protected]18038f82012-11-20 13:46:58411 if (!value) {
[email protected]b154e6f2009-03-06 01:52:40412 NOTREACHED() << "Trying to read an unregistered pref: " << path;
[email protected]c3453302009-12-01 01:33:08413 return 0;
[email protected]0bb1a622009-03-04 03:22:32414 }
[email protected]dc9a6762010-08-16 07:13:53415 std::string result("0");
[email protected]18038f82012-11-20 13:46:58416 bool rv = value->GetAsString(&result);
[email protected]0bb1a622009-03-04 03:22:32417 DCHECK(rv);
[email protected]e83326f2010-07-31 17:29:25418
419 int64 val;
[email protected]dc9a6762010-08-16 07:13:53420 base::StringToInt64(result, &val);
[email protected]e83326f2010-07-31 17:29:25421 return val;
[email protected]0bb1a622009-03-04 03:22:32422}
423
[email protected]3cbe0812012-07-03 02:51:43424void PrefService::SetUint64(const char* path, uint64 value) {
[email protected]a43a667b2013-06-14 17:56:08425 SetUserPrefValue(path,
426 base::Value::CreateStringValue(base::Uint64ToString(value)));
[email protected]3cbe0812012-07-03 02:51:43427}
428
429uint64 PrefService::GetUint64(const char* path) const {
430 DCHECK(CalledOnValidThread());
431
[email protected]a43a667b2013-06-14 17:56:08432 const base::Value* value = GetPreferenceValue(path);
[email protected]18038f82012-11-20 13:46:58433 if (!value) {
[email protected]3cbe0812012-07-03 02:51:43434 NOTREACHED() << "Trying to read an unregistered pref: " << path;
435 return 0;
436 }
437 std::string result("0");
[email protected]18038f82012-11-20 13:46:58438 bool rv = value->GetAsString(&result);
[email protected]3cbe0812012-07-03 02:51:43439 DCHECK(rv);
440
441 uint64 val;
442 base::StringToUint64(result, &val);
443 return val;
444}
445
[email protected]a43a667b2013-06-14 17:56:08446base::Value* PrefService::GetMutableUserPref(const char* path,
447 base::Value::Type type) {
448 CHECK(type == base::Value::TYPE_DICTIONARY || type == base::Value::TYPE_LIST);
initial.commit09911bf2008-07-26 23:55:29449 DCHECK(CalledOnValidThread());
450
451 const Preference* pref = FindPreference(path);
452 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40453 NOTREACHED() << "Trying to get an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29454 return NULL;
455 }
[email protected]26418b72011-03-30 14:07:39456 if (pref->GetType() != type) {
457 NOTREACHED() << "Wrong type for GetMutableValue: " << path;
initial.commit09911bf2008-07-26 23:55:29458 return NULL;
459 }
460
[email protected]e0250892010-10-01 18:57:53461 // Look for an existing preference in the user store. If it doesn't
462 // exist or isn't the correct type, create a new user preference.
[email protected]a43a667b2013-06-14 17:56:08463 base::Value* value = NULL;
[email protected]892f1d62012-11-08 18:24:34464 if (!user_pref_store_->GetMutableValue(path, &value) ||
[email protected]26418b72011-03-30 14:07:39465 !value->IsType(type)) {
[email protected]a43a667b2013-06-14 17:56:08466 if (type == base::Value::TYPE_DICTIONARY) {
467 value = new base::DictionaryValue;
468 } else if (type == base::Value::TYPE_LIST) {
469 value = new base::ListValue;
[email protected]26418b72011-03-30 14:07:39470 } else {
471 NOTREACHED();
472 }
473 user_pref_store_->SetValueSilently(path, value);
initial.commit09911bf2008-07-26 23:55:29474 }
[email protected]26418b72011-03-30 14:07:39475 return value;
476}
477
[email protected]68bf41a2011-03-25 16:38:31478void PrefService::ReportUserPrefChanged(const std::string& key) {
[email protected]f89ee342011-03-07 09:28:27479 user_pref_store_->ReportValueChanged(key);
480}
481
[email protected]a43a667b2013-06-14 17:56:08482void PrefService::SetUserPrefValue(const char* path, base::Value* new_value) {
483 scoped_ptr<base::Value> owned_value(new_value);
[email protected]c3b54f372010-09-14 08:25:07484 DCHECK(CalledOnValidThread());
485
486 const Preference* pref = FindPreference(path);
487 if (!pref) {
488 NOTREACHED() << "Trying to write an unregistered pref: " << path;
489 return;
490 }
[email protected]99cc9a02010-09-17 07:53:28491 if (pref->GetType() != new_value->GetType()) {
492 NOTREACHED() << "Trying to set pref " << path
493 << " of type " << pref->GetType()
[email protected]c3b54f372010-09-14 08:25:07494 << " to value of type " << new_value->GetType();
495 return;
496 }
497
[email protected]b99c41c2011-04-27 15:18:48498 user_pref_store_->SetValue(path, owned_value.release());
[email protected]73c47932010-12-06 18:13:43499}
500
[email protected]5b199522012-12-22 17:24:44501void PrefService::UpdateCommandLinePrefStore(PrefStore* command_line_store) {
502 pref_value_store_->UpdateCommandLinePrefStore(command_line_store);
[email protected]d3b05ea2012-01-24 22:57:05503}
504
initial.commit09911bf2008-07-26 23:55:29505///////////////////////////////////////////////////////////////////////////////
506// PrefService::Preference
507
[email protected]c3b54f372010-09-14 08:25:07508PrefService::Preference::Preference(const PrefService* service,
[email protected]9a8c4022011-01-25 14:25:33509 const char* name,
[email protected]bab1c13f2011-08-12 20:59:02510 base::Value::Type type)
[email protected]99cc9a02010-09-17 07:53:28511 : name_(name),
[email protected]9a8c4022011-01-25 14:25:33512 type_(type),
[email protected]c3b54f372010-09-14 08:25:07513 pref_service_(service) {
initial.commit09911bf2008-07-26 23:55:29514 DCHECK(name);
[email protected]c3b54f372010-09-14 08:25:07515 DCHECK(service);
[email protected]99cc9a02010-09-17 07:53:28516}
517
[email protected]fb8fdf12012-08-21 16:28:20518const std::string PrefService::Preference::name() const {
519 return name_;
520}
521
[email protected]bab1c13f2011-08-12 20:59:02522base::Value::Type PrefService::Preference::GetType() const {
[email protected]9a8c4022011-01-25 14:25:33523 return type_;
initial.commit09911bf2008-07-26 23:55:29524}
525
[email protected]a43a667b2013-06-14 17:56:08526const base::Value* PrefService::Preference::GetValue() const {
527 const base::Value* result= pref_service_->GetPreferenceValue(name_);
[email protected]18038f82012-11-20 13:46:58528 DCHECK(result) << "Must register pref before getting its value";
529 return result;
[email protected]40a47c162010-09-09 11:14:01530}
531
[email protected]a43a667b2013-06-14 17:56:08532const base::Value* PrefService::Preference::GetRecommendedValue() const {
[email protected]7ca0f362012-07-30 10:14:03533 DCHECK(pref_service_->FindPreference(name_.c_str())) <<
534 "Must register pref before getting its value";
535
[email protected]a43a667b2013-06-14 17:56:08536 const base::Value* found_value = NULL;
[email protected]7ca0f362012-07-30 10:14:03537 if (pref_value_store()->GetRecommendedValue(name_, type_, &found_value)) {
538 DCHECK(found_value->IsType(type_));
539 return found_value;
540 }
541
542 // The pref has no recommended value.
543 return NULL;
544}
545
[email protected]40a47c162010-09-09 11:14:01546bool PrefService::Preference::IsManaged() const {
[email protected]887288f02011-02-04 22:52:46547 return pref_value_store()->PrefValueInManagedStore(name_.c_str());
[email protected]40a47c162010-09-09 11:14:01548}
549
[email protected]a5437282011-12-12 12:33:21550bool PrefService::Preference::IsRecommended() const {
551 return pref_value_store()->PrefValueFromRecommendedStore(name_.c_str());
552}
553
[email protected]40a47c162010-09-09 11:14:01554bool PrefService::Preference::HasExtensionSetting() const {
[email protected]887288f02011-02-04 22:52:46555 return pref_value_store()->PrefValueInExtensionStore(name_.c_str());
[email protected]40a47c162010-09-09 11:14:01556}
557
558bool PrefService::Preference::HasUserSetting() const {
[email protected]887288f02011-02-04 22:52:46559 return pref_value_store()->PrefValueInUserStore(name_.c_str());
[email protected]40a47c162010-09-09 11:14:01560}
561
562bool PrefService::Preference::IsExtensionControlled() const {
[email protected]887288f02011-02-04 22:52:46563 return pref_value_store()->PrefValueFromExtensionStore(name_.c_str());
[email protected]40a47c162010-09-09 11:14:01564}
565
566bool PrefService::Preference::IsUserControlled() const {
[email protected]887288f02011-02-04 22:52:46567 return pref_value_store()->PrefValueFromUserStore(name_.c_str());
[email protected]c3b54f372010-09-14 08:25:07568}
569
570bool PrefService::Preference::IsDefaultValue() const {
[email protected]887288f02011-02-04 22:52:46571 return pref_value_store()->PrefValueFromDefaultStore(name_.c_str());
[email protected]d7449e82010-07-14 11:42:35572}
[email protected]74379bc52010-07-21 13:54:08573
574bool PrefService::Preference::IsUserModifiable() const {
[email protected]887288f02011-02-04 22:52:46575 return pref_value_store()->PrefValueUserModifiable(name_.c_str());
[email protected]74379bc52010-07-21 13:54:08576}
[email protected]9a28f132011-02-24 21:15:16577
578bool PrefService::Preference::IsExtensionModifiable() const {
579 return pref_value_store()->PrefValueExtensionModifiable(name_.c_str());
580}
[email protected]18038f82012-11-20 13:46:58581
582const base::Value* PrefService::GetPreferenceValue(
583 const std::string& path) const {
584 DCHECK(CalledOnValidThread());
[email protected]a43a667b2013-06-14 17:56:08585 const base::Value* default_value = NULL;
[email protected]b1de2c72013-02-06 02:45:47586 if (pref_registry_->defaults()->GetValue(path, &default_value)) {
[email protected]a43a667b2013-06-14 17:56:08587 const base::Value* found_value = NULL;
[email protected]b1de2c72013-02-06 02:45:47588 base::Value::Type default_type = default_value->GetType();
589 if (pref_value_store_->GetValue(path, default_type, &found_value)) {
590 DCHECK(found_value->IsType(default_type));
591 return found_value;
592 } else {
593 // Every registered preference has at least a default value.
594 NOTREACHED() << "no valid value found for registered pref " << path;
595 }
[email protected]18038f82012-11-20 13:46:58596 }
597
[email protected]18038f82012-11-20 13:46:58598 return NULL;
599}