blob: ea1d705eac19c365f2940c65f9b3eead5ea9b2f9 [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]37858e52010-08-26 00:22:025#include "chrome/browser/prefs/pref_service.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]1cb92b82010-03-08 23:12:157#include <algorithm>
8#include <string>
9
[email protected]bebe69432011-09-28 18:36:4510#include "base/bind.h"
[email protected]aa4dc5e22010-06-16 11:32:5411#include "base/command_line.h"
[email protected]c02c853d72010-08-07 06:23:2412#include "base/file_path.h"
[email protected]1d01d412010-08-20 00:36:0113#include "base/file_util.h"
initial.commit09911bf2008-07-26 23:55:2914#include "base/logging.h"
15#include "base/message_loop.h"
[email protected]835d7c82010-10-14 04:38:3816#include "base/metrics/histogram.h"
[email protected]7286e3fc2011-07-19 22:13:2417#include "base/stl_util.h"
[email protected]e83326f2010-07-31 17:29:2518#include "base/string_number_conversions.h"
initial.commit09911bf2008-07-26 23:55:2919#include "base/string_util.h"
[email protected]8703b2b2011-03-15 09:51:5020#include "base/value_conversions.h"
[email protected]66de4f092009-09-04 23:59:4021#include "build/build_config.h"
[email protected]d36f941b2011-05-09 06:19:1622#include "chrome/browser/browser_process.h"
[email protected]9a8c4022011-01-25 14:25:3323#include "chrome/browser/extensions/extension_pref_store.h"
[email protected]acd78969c2010-12-08 09:49:1124#include "chrome/browser/policy/configuration_policy_pref_store.h"
25#include "chrome/browser/prefs/command_line_pref_store.h"
[email protected]f2d1f612010-12-09 15:10:1726#include "chrome/browser/prefs/default_pref_store.h"
[email protected]32c3c752012-01-05 17:33:4727#include "chrome/browser/prefs/overlay_user_pref_store.h"
[email protected]d36f941b2011-05-09 06:19:1628#include "chrome/browser/prefs/pref_model_associator.h"
[email protected]acd78969c2010-12-08 09:49:1129#include "chrome/browser/prefs/pref_notifier_impl.h"
[email protected]39d9f62c2010-12-03 10:48:5030#include "chrome/browser/prefs/pref_value_store.h"
[email protected]32c3c752012-01-05 17:33:4731#include "chrome/browser/ui/prefs/prefs_tab_helper.h"
[email protected]c7fb2da32011-04-14 20:47:1032#include "chrome/browser/ui/profile_error_dialog.h"
[email protected]e4f86492011-04-13 12:23:5333#include "chrome/common/json_pref_store.h"
[email protected]32c3c752012-01-05 17:33:4734#include "chrome/common/pref_names.h"
[email protected]c38831a12011-10-28 12:44:4935#include "content/public/browser/browser_thread.h"
[email protected]ba399672010-04-06 15:42:3936#include "grit/chromium_strings.h"
[email protected]34ac8f32009-02-22 23:03:2737#include "grit/generated_resources.h"
[email protected]c051a1b2011-01-21 23:30:1738#include "ui/base/l10n/l10n_util.h"
initial.commit09911bf2008-07-26 23:55:2939
[email protected]631bb742011-11-02 11:29:3940using content::BrowserThread;
41
initial.commit09911bf2008-07-26 23:55:2942namespace {
43
initial.commit09911bf2008-07-26 23:55:2944// A helper function for RegisterLocalized*Pref that creates a Value* based on
45// the string value in the locale dll. Because we control the values in a
46// locale dll, this should always return a Value of the appropriate type.
[email protected]bab1c13f2011-08-12 20:59:0247Value* CreateLocaleDefaultValue(base::Value::Type type, int message_id) {
[email protected]16b527162010-08-15 18:37:1048 std::string resource_string = l10n_util::GetStringUTF8(message_id);
initial.commit09911bf2008-07-26 23:55:2949 DCHECK(!resource_string.empty());
50 switch (type) {
51 case Value::TYPE_BOOLEAN: {
[email protected]16b527162010-08-15 18:37:1052 if ("true" == resource_string)
initial.commit09911bf2008-07-26 23:55:2953 return Value::CreateBooleanValue(true);
[email protected]16b527162010-08-15 18:37:1054 if ("false" == resource_string)
initial.commit09911bf2008-07-26 23:55:2955 return Value::CreateBooleanValue(false);
56 break;
57 }
58
59 case Value::TYPE_INTEGER: {
[email protected]e83326f2010-07-31 17:29:2560 int val;
[email protected]16b527162010-08-15 18:37:1061 base::StringToInt(resource_string, &val);
[email protected]e83326f2010-07-31 17:29:2562 return Value::CreateIntegerValue(val);
initial.commit09911bf2008-07-26 23:55:2963 }
64
[email protected]fb534c92011-02-01 01:02:0765 case Value::TYPE_DOUBLE: {
[email protected]e83326f2010-07-31 17:29:2566 double val;
[email protected]16b527162010-08-15 18:37:1067 base::StringToDouble(resource_string, &val);
[email protected]fb534c92011-02-01 01:02:0768 return Value::CreateDoubleValue(val);
initial.commit09911bf2008-07-26 23:55:2969 }
70
71 case Value::TYPE_STRING: {
72 return Value::CreateStringValue(resource_string);
initial.commit09911bf2008-07-26 23:55:2973 }
74
75 default: {
[email protected]b154e6f2009-03-06 01:52:4076 NOTREACHED() <<
[email protected]c3b54f372010-09-14 08:25:0777 "list and dictionary types cannot have default locale values";
initial.commit09911bf2008-07-26 23:55:2978 }
79 }
80 NOTREACHED();
81 return Value::CreateNullValue();
82}
83
[email protected]ba399672010-04-06 15:42:3984// Forwards a notification after a PostMessage so that we can wait for the
85// MessageLoop to run.
[email protected]845b43a82011-05-11 10:14:4386void NotifyReadError(int message_id) {
[email protected]c7fb2da32011-04-14 20:47:1087 ShowProfileErrorDialog(message_id);
[email protected]ba399672010-04-06 15:42:3988}
89
[email protected]845b43a82011-05-11 10:14:4390// Shows notifications which correspond to PersistentPrefStore's reading errors.
91class ReadErrorHandler : public PersistentPrefStore::ReadErrorDelegate {
92 public:
93 virtual void OnError(PersistentPrefStore::PrefReadError error) {
94 if (error != PersistentPrefStore::PREF_READ_ERROR_NONE) {
95 // Failing to load prefs on startup is a bad thing(TM). See bug 38352 for
96 // an example problem that this can cause.
97 // Do some diagnosis and try to avoid losing data.
98 int message_id = 0;
99 if (error <= PersistentPrefStore::PREF_READ_ERROR_JSON_TYPE) {
100 message_id = IDS_PREFERENCES_CORRUPT_ERROR;
101 } else if (error != PersistentPrefStore::PREF_READ_ERROR_NO_FILE) {
102 message_id = IDS_PREFERENCES_UNREADABLE_ERROR;
103 }
104
105 if (message_id) {
106 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
[email protected]bebe69432011-09-28 18:36:45107 base::Bind(&NotifyReadError, message_id));
[email protected]845b43a82011-05-11 10:14:43108 }
[email protected]ff3c69a2011-09-15 00:36:48109 UMA_HISTOGRAM_ENUMERATION("PrefService.ReadError", error,
110 PersistentPrefStore::PREF_READ_ERROR_MAX_ENUM);
[email protected]845b43a82011-05-11 10:14:43111 }
112 }
113};
114
initial.commit09911bf2008-07-26 23:55:29115} // namespace
116
[email protected]db198b22010-07-12 16:48:49117// static
[email protected]a9c23a52010-08-04 09:13:44118PrefService* PrefService::CreatePrefService(const FilePath& pref_filename,
[email protected]f2d1f612010-12-09 15:10:17119 PrefStore* extension_prefs,
[email protected]845b43a82011-05-11 10:14:43120 bool async) {
[email protected]acd78969c2010-12-08 09:49:11121 using policy::ConfigurationPolicyPrefStore;
122
[email protected]1d01d412010-08-20 00:36:01123#if defined(OS_LINUX)
124 // We'd like to see what fraction of our users have the preferences
125 // stored on a network file system, as we've had no end of troubles
126 // with NFS/AFS.
127 // TODO(evanm): remove this once we've collected state.
128 file_util::FileSystemType fstype;
129 if (file_util::GetFileSystemType(pref_filename.DirName(), &fstype)) {
130 UMA_HISTOGRAM_ENUMERATION("PrefService.FileSystemType",
131 static_cast<int>(fstype),
132 file_util::FILE_SYSTEM_TYPE_COUNT);
133 }
134#endif
135
[email protected]f31e2e52011-07-14 16:01:19136#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]4eb4d6ce2012-04-02 14:06:57137 ConfigurationPolicyPrefStore* managed =
138 ConfigurationPolicyPrefStore::CreateMandatoryPolicyPrefStore();
139 ConfigurationPolicyPrefStore* recommended =
140 ConfigurationPolicyPrefStore::CreateRecommendedPolicyPrefStore();
[email protected]f31e2e52011-07-14 16:01:19141#else
[email protected]4eb4d6ce2012-04-02 14:06:57142 ConfigurationPolicyPrefStore* managed = NULL;
143 ConfigurationPolicyPrefStore* recommended = NULL;
[email protected]f31e2e52011-07-14 16:01:19144#endif // ENABLE_CONFIGURATION_POLICY
145
[email protected]acd78969c2010-12-08 09:49:11146 CommandLinePrefStore* command_line =
147 new CommandLinePrefStore(CommandLine::ForCurrentProcess());
148 JsonPrefStore* user = new JsonPrefStore(
149 pref_filename,
150 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE));
[email protected]9a8c4022011-01-25 14:25:33151 DefaultPrefStore* default_pref_store = new DefaultPrefStore();
[email protected]acd78969c2010-12-08 09:49:11152
[email protected]361d37f62011-11-22 10:37:02153 PrefNotifierImpl* pref_notifier = new PrefNotifierImpl();
154 PrefModelAssociator* pref_sync_associator = new PrefModelAssociator();
155
[email protected]845b43a82011-05-11 10:14:43156 return new PrefService(
[email protected]361d37f62011-11-22 10:37:02157 pref_notifier,
158 new PrefValueStore(
[email protected]4eb4d6ce2012-04-02 14:06:57159 managed,
[email protected]361d37f62011-11-22 10:37:02160 extension_prefs,
161 command_line,
162 user,
[email protected]4eb4d6ce2012-04-02 14:06:57163 recommended,
[email protected]361d37f62011-11-22 10:37:02164 default_pref_store,
165 pref_sync_associator,
166 pref_notifier),
167 user,
168 default_pref_store,
169 pref_sync_associator,
170 async);
[email protected]9a8c4022011-01-25 14:25:33171}
172
173PrefService* PrefService::CreateIncognitoPrefService(
174 PrefStore* incognito_extension_prefs) {
[email protected]d3b05ea2012-01-24 22:57:05175 pref_service_forked_ = true;
[email protected]361d37f62011-11-22 10:37:02176 PrefNotifierImpl* pref_notifier = new PrefNotifierImpl();
[email protected]32c3c752012-01-05 17:33:47177 OverlayUserPrefStore* incognito_pref_store =
178 new OverlayUserPrefStore(user_pref_store_.get());
179 PrefsTabHelper::InitIncognitoUserPrefStore(incognito_pref_store);
[email protected]361d37f62011-11-22 10:37:02180 return new PrefService(
181 pref_notifier,
182 pref_value_store_->CloneAndSpecialize(
[email protected]4eb4d6ce2012-04-02 14:06:57183 NULL, // managed
[email protected]361d37f62011-11-22 10:37:02184 incognito_extension_prefs,
[email protected]d3b05ea2012-01-24 22:57:05185 NULL, // command_line_prefs
[email protected]361d37f62011-11-22 10:37:02186 incognito_pref_store,
[email protected]4eb4d6ce2012-04-02 14:06:57187 NULL, // recommended
[email protected]361d37f62011-11-22 10:37:02188 default_store_.get(),
[email protected]d3b05ea2012-01-24 22:57:05189 NULL, // pref_sync_associator
[email protected]361d37f62011-11-22 10:37:02190 pref_notifier),
191 incognito_pref_store,
[email protected]9a8c4022011-01-25 14:25:33192 default_store_.get(),
[email protected]361d37f62011-11-22 10:37:02193 NULL,
194 false);
195}
196
[email protected]361d37f62011-11-22 10:37:02197PrefService::PrefService(PrefNotifierImpl* pref_notifier,
198 PrefValueStore* pref_value_store,
199 PersistentPrefStore* user_prefs,
200 DefaultPrefStore* default_store,
201 PrefModelAssociator* pref_sync_associator,
202 bool async)
203 : pref_notifier_(pref_notifier),
204 pref_value_store_(pref_value_store),
205 user_pref_store_(user_prefs),
206 default_store_(default_store),
[email protected]d3b05ea2012-01-24 22:57:05207 pref_sync_associator_(pref_sync_associator),
208 pref_service_forked_(false) {
[email protected]361d37f62011-11-22 10:37:02209 pref_notifier_->SetPrefService(this);
210 if (pref_sync_associator_.get())
211 pref_sync_associator_->SetPrefService(this);
212 InitFromStorage(async);
[email protected]9a8c4022011-01-25 14:25:33213}
214
initial.commit09911bf2008-07-26 23:55:29215PrefService::~PrefService() {
216 DCHECK(CalledOnValidThread());
initial.commit09911bf2008-07-26 23:55:29217 STLDeleteContainerPointers(prefs_.begin(), prefs_.end());
218 prefs_.clear();
[email protected]a98ce1262011-01-28 13:20:23219
220 // Reset pointers so accesses after destruction reliably crash.
221 pref_value_store_.reset();
222 user_pref_store_ = NULL;
223 default_store_ = NULL;
[email protected]d36f941b2011-05-09 06:19:16224 pref_sync_associator_.reset();
initial.commit09911bf2008-07-26 23:55:29225}
226
[email protected]845b43a82011-05-11 10:14:43227void PrefService::InitFromStorage(bool async) {
228 if (!async) {
229 ReadErrorHandler error_handler;
230 error_handler.OnError(user_pref_store_->ReadPrefs());
[email protected]844a1002011-04-19 11:37:21231 } else {
[email protected]845b43a82011-05-11 10:14:43232 // Guarantee that initialization happens after this function returned.
233 MessageLoop::current()->PostTask(
234 FROM_HERE,
[email protected]bebe69432011-09-28 18:36:45235 base::Bind(&PersistentPrefStore::ReadPrefsAsync,
236 user_pref_store_.get(),
237 new ReadErrorHandler()));
[email protected]844a1002011-04-19 11:37:21238 }
[email protected]ba399672010-04-06 15:42:39239}
240
241bool PrefService::ReloadPersistentPrefs() {
[email protected]f2d1f612010-12-09 15:10:17242 return user_pref_store_->ReadPrefs() ==
243 PersistentPrefStore::PREF_READ_ERROR_NONE;
initial.commit09911bf2008-07-26 23:55:29244}
245
[email protected]3826fed2011-03-25 10:59:56246void PrefService::CommitPendingWrite() {
247 DCHECK(CalledOnValidThread());
248 user_pref_store_->CommitPendingWrite();
249}
250
[email protected]d36f941b2011-05-09 06:19:16251namespace {
252
253// If there's no g_browser_process or no local state, return true (for testing).
[email protected]d3b05ea2012-01-24 22:57:05254bool IsLocalStatePrefService(PrefService* prefs) {
[email protected]d36f941b2011-05-09 06:19:16255 return (!g_browser_process ||
256 !g_browser_process->local_state() ||
257 g_browser_process->local_state() == prefs);
258}
259
260// If there's no g_browser_process, return true (for testing).
[email protected]d3b05ea2012-01-24 22:57:05261bool IsProfilePrefService(PrefService* prefs) {
[email protected]d36f941b2011-05-09 06:19:16262 // TODO(zea): uncomment this once all preferences are only ever registered
263 // with either the local_state's pref service or the profile's pref service.
264 // return (!g_browser_process || g_browser_process->local_state() != prefs);
265 return true;
266}
267
268} // namespace
269
270
271// Local State prefs.
[email protected]57ecc4b2010-08-11 03:02:51272void PrefService::RegisterBooleanPref(const char* path,
initial.commit09911bf2008-07-26 23:55:29273 bool default_value) {
[email protected]d36f941b2011-05-09 06:19:16274 // If this fails, the pref service in use is a profile pref service, so the
275 // sync status must be provided (see profile pref registration calls below).
276 DCHECK(IsLocalStatePrefService(this));
277 RegisterPreference(path,
278 Value::CreateBooleanValue(default_value),
279 UNSYNCABLE_PREF);
initial.commit09911bf2008-07-26 23:55:29280}
281
[email protected]57ecc4b2010-08-11 03:02:51282void PrefService::RegisterIntegerPref(const char* path, int default_value) {
[email protected]d36f941b2011-05-09 06:19:16283 // If this fails, the pref service in use is a profile pref service, so the
284 // sync status must be provided (see profile pref registration calls below).
285 DCHECK(IsLocalStatePrefService(this));
286 RegisterPreference(path,
287 Value::CreateIntegerValue(default_value),
288 UNSYNCABLE_PREF);
initial.commit09911bf2008-07-26 23:55:29289}
290
[email protected]fb534c92011-02-01 01:02:07291void PrefService::RegisterDoublePref(const char* path, double default_value) {
[email protected]d36f941b2011-05-09 06:19:16292 // If this fails, the pref service in use is a profile pref service, so the
293 // sync status must be provided (see profile pref registration calls below).
294 DCHECK(IsLocalStatePrefService(this));
295 RegisterPreference(path,
296 Value::CreateDoubleValue(default_value),
297 UNSYNCABLE_PREF);
initial.commit09911bf2008-07-26 23:55:29298}
299
[email protected]57ecc4b2010-08-11 03:02:51300void PrefService::RegisterStringPref(const char* path,
[email protected]20ce516d2010-06-18 02:20:04301 const std::string& default_value) {
[email protected]d36f941b2011-05-09 06:19:16302 // If this fails, the pref service in use is a profile pref service, so the
303 // sync status must be provided (see profile pref registration calls below).
304 DCHECK(IsLocalStatePrefService(this));
305 RegisterPreference(path,
306 Value::CreateStringValue(default_value),
307 UNSYNCABLE_PREF);
initial.commit09911bf2008-07-26 23:55:29308}
309
[email protected]57ecc4b2010-08-11 03:02:51310void PrefService::RegisterFilePathPref(const char* path,
[email protected]b9636002009-03-04 00:05:25311 const FilePath& default_value) {
[email protected]d36f941b2011-05-09 06:19:16312 // If this fails, the pref service in use is a profile pref service, so the
313 // sync status must be provided (see profile pref registration calls below).
314 DCHECK(IsLocalStatePrefService(this));
315 RegisterPreference(path,
316 Value::CreateStringValue(default_value.value()),
317 UNSYNCABLE_PREF);
[email protected]b9636002009-03-04 00:05:25318}
319
[email protected]57ecc4b2010-08-11 03:02:51320void PrefService::RegisterListPref(const char* path) {
[email protected]d36f941b2011-05-09 06:19:16321 // If this fails, the pref service in use is a profile pref service, so the
322 // sync status must be provided (see profile pref registration calls below).
323 DCHECK(IsLocalStatePrefService(this));
324 RegisterPreference(path,
325 new ListValue(),
326 UNSYNCABLE_PREF);
initial.commit09911bf2008-07-26 23:55:29327}
328
[email protected]c2f23d012011-02-09 14:52:17329void PrefService::RegisterListPref(const char* path, ListValue* default_value) {
[email protected]d36f941b2011-05-09 06:19:16330 // If this fails, the pref service in use is a profile pref service, so the
331 // sync status must be provided (see profile pref registration calls below).
332 DCHECK(IsLocalStatePrefService(this));
333 RegisterPreference(path,
334 default_value,
335 UNSYNCABLE_PREF);
[email protected]c2f23d012011-02-09 14:52:17336}
337
[email protected]57ecc4b2010-08-11 03:02:51338void PrefService::RegisterDictionaryPref(const char* path) {
[email protected]d36f941b2011-05-09 06:19:16339 // If this fails, the pref service in use is a profile pref service, so the
340 // sync status must be provided (see profile pref registration calls below).
341 DCHECK(IsLocalStatePrefService(this));
342 RegisterPreference(path,
343 new DictionaryValue(),
344 UNSYNCABLE_PREF);
initial.commit09911bf2008-07-26 23:55:29345}
346
[email protected]c2f23d012011-02-09 14:52:17347void PrefService::RegisterDictionaryPref(const char* path,
348 DictionaryValue* default_value) {
[email protected]d36f941b2011-05-09 06:19:16349 // If this fails, the pref service in use is a profile pref service, so the
350 // sync status must be provided (see profile pref registration calls below).
351 DCHECK(IsLocalStatePrefService(this));
352 RegisterPreference(path,
353 default_value,
354 UNSYNCABLE_PREF);
[email protected]c2f23d012011-02-09 14:52:17355}
356
[email protected]57ecc4b2010-08-11 03:02:51357void PrefService::RegisterLocalizedBooleanPref(const char* path,
initial.commit09911bf2008-07-26 23:55:29358 int locale_default_message_id) {
[email protected]d36f941b2011-05-09 06:19:16359 // If this fails, the pref service in use is a profile pref service, so the
360 // sync status must be provided (see profile pref registration calls below).
361 DCHECK(IsLocalStatePrefService(this));
[email protected]c3b54f372010-09-14 08:25:07362 RegisterPreference(
363 path,
[email protected]d36f941b2011-05-09 06:19:16364 CreateLocaleDefaultValue(Value::TYPE_BOOLEAN, locale_default_message_id),
365 UNSYNCABLE_PREF);
initial.commit09911bf2008-07-26 23:55:29366}
367
[email protected]57ecc4b2010-08-11 03:02:51368void PrefService::RegisterLocalizedIntegerPref(const char* path,
initial.commit09911bf2008-07-26 23:55:29369 int locale_default_message_id) {
[email protected]d36f941b2011-05-09 06:19:16370 // If this fails, the pref service in use is a profile pref service, so the
371 // sync status must be provided (see profile pref registration calls below).
372 DCHECK(IsLocalStatePrefService(this));
[email protected]c3b54f372010-09-14 08:25:07373 RegisterPreference(
374 path,
[email protected]d36f941b2011-05-09 06:19:16375 CreateLocaleDefaultValue(Value::TYPE_INTEGER, locale_default_message_id),
376 UNSYNCABLE_PREF);
initial.commit09911bf2008-07-26 23:55:29377}
378
[email protected]fb534c92011-02-01 01:02:07379void PrefService::RegisterLocalizedDoublePref(const char* path,
380 int locale_default_message_id) {
[email protected]d36f941b2011-05-09 06:19:16381 // If this fails, the pref service in use is a profile pref service, so the
382 // sync status must be provided (see profile pref registration calls below).
383 DCHECK(IsLocalStatePrefService(this));
[email protected]c3b54f372010-09-14 08:25:07384 RegisterPreference(
385 path,
[email protected]d36f941b2011-05-09 06:19:16386 CreateLocaleDefaultValue(Value::TYPE_DOUBLE, locale_default_message_id),
387 UNSYNCABLE_PREF);
initial.commit09911bf2008-07-26 23:55:29388}
389
[email protected]57ecc4b2010-08-11 03:02:51390void PrefService::RegisterLocalizedStringPref(const char* path,
initial.commit09911bf2008-07-26 23:55:29391 int locale_default_message_id) {
[email protected]d36f941b2011-05-09 06:19:16392 // If this fails, the pref service in use is a profile pref service, so the
393 // sync status must be provided (see profile pref registration calls below).
394 DCHECK(IsLocalStatePrefService(this));
[email protected]c3b54f372010-09-14 08:25:07395 RegisterPreference(
396 path,
[email protected]d36f941b2011-05-09 06:19:16397 CreateLocaleDefaultValue(Value::TYPE_STRING, locale_default_message_id),
398 UNSYNCABLE_PREF);
399}
400
401void PrefService::RegisterInt64Pref(const char* path, int64 default_value) {
402 // If this fails, the pref service in use is a profile pref service, so the
403 // sync status must be provided (see profile pref registration calls below).
404 DCHECK(IsLocalStatePrefService(this));
405 RegisterPreference(
406 path,
407 Value::CreateStringValue(base::Int64ToString(default_value)),
408 UNSYNCABLE_PREF);
409}
410
411// Profile prefs (must use the sync_status variable).
412void PrefService::RegisterBooleanPref(const char* path,
413 bool default_value,
414 PrefSyncStatus sync_status) {
415 DCHECK(IsProfilePrefService(this));
416 RegisterPreference(path,
417 Value::CreateBooleanValue(default_value),
418 sync_status);
419}
420
421void PrefService::RegisterIntegerPref(const char* path,
422 int default_value,
423 PrefSyncStatus sync_status) {
424 DCHECK(IsProfilePrefService(this));
425 RegisterPreference(path,
426 Value::CreateIntegerValue(default_value),
427 sync_status);
428}
429
430void PrefService::RegisterDoublePref(const char* path,
431 double default_value,
432 PrefSyncStatus sync_status) {
433 DCHECK(IsProfilePrefService(this));
434 RegisterPreference(path,
435 Value::CreateDoubleValue(default_value),
436 sync_status);
437}
438
439void PrefService::RegisterStringPref(const char* path,
440 const std::string& default_value,
441 PrefSyncStatus sync_status) {
442 DCHECK(IsProfilePrefService(this));
443 RegisterPreference(path,
444 Value::CreateStringValue(default_value),
445 sync_status);
446}
447
448void PrefService::RegisterFilePathPref(const char* path,
449 const FilePath& default_value,
450 PrefSyncStatus sync_status) {
451 DCHECK(IsProfilePrefService(this));
452 RegisterPreference(path,
453 Value::CreateStringValue(default_value.value()),
454 sync_status);
455}
456
457void PrefService::RegisterListPref(const char* path,
458 PrefSyncStatus sync_status) {
459 DCHECK(IsProfilePrefService(this));
460 RegisterPreference(path, new ListValue(), sync_status);
461}
462
463void PrefService::RegisterListPref(const char* path,
464 ListValue* default_value,
465 PrefSyncStatus sync_status) {
466 DCHECK(IsProfilePrefService(this));
467 RegisterPreference(path, default_value, sync_status);
468}
469
470void PrefService::RegisterDictionaryPref(const char* path,
471 PrefSyncStatus sync_status) {
472 DCHECK(IsProfilePrefService(this));
473 RegisterPreference(path, new DictionaryValue(), sync_status);
474}
475
476void PrefService::RegisterDictionaryPref(const char* path,
477 DictionaryValue* default_value,
478 PrefSyncStatus sync_status) {
479 DCHECK(IsProfilePrefService(this));
480 RegisterPreference(path, default_value, sync_status);
481}
482
483void PrefService::RegisterLocalizedBooleanPref(const char* path,
484 int locale_default_message_id,
485 PrefSyncStatus sync_status) {
486 DCHECK(IsProfilePrefService(this));
487 RegisterPreference(
488 path,
[email protected]d3b05ea2012-01-24 22:57:05489 CreateLocaleDefaultValue(Value::TYPE_BOOLEAN, locale_default_message_id),
[email protected]d36f941b2011-05-09 06:19:16490 sync_status);
491}
492
493void PrefService::RegisterLocalizedIntegerPref(const char* path,
494 int locale_default_message_id,
495 PrefSyncStatus sync_status) {
496 DCHECK(IsProfilePrefService(this));
497 RegisterPreference(
498 path,
499 CreateLocaleDefaultValue(Value::TYPE_INTEGER, locale_default_message_id),
500 sync_status);
501}
502
503void PrefService::RegisterLocalizedDoublePref(const char* path,
504 int locale_default_message_id,
505 PrefSyncStatus sync_status) {
506 DCHECK(IsProfilePrefService(this));
507 RegisterPreference(
508 path,
509 CreateLocaleDefaultValue(Value::TYPE_DOUBLE, locale_default_message_id),
510 sync_status);
511}
512
513void PrefService::RegisterLocalizedStringPref(const char* path,
514 int locale_default_message_id,
515 PrefSyncStatus sync_status) {
516 DCHECK(IsProfilePrefService(this));
517 RegisterPreference(
518 path,
519 CreateLocaleDefaultValue(Value::TYPE_STRING, locale_default_message_id),
520 sync_status);
521}
522
523void PrefService::RegisterInt64Pref(const char* path,
524 int64 default_value,
525 PrefSyncStatus sync_status) {
526 DCHECK(IsProfilePrefService(this));
527 RegisterPreference(
528 path,
529 Value::CreateStringValue(base::Int64ToString(default_value)),
530 sync_status);
initial.commit09911bf2008-07-26 23:55:29531}
532
[email protected]57ecc4b2010-08-11 03:02:51533bool PrefService::GetBoolean(const char* path) const {
initial.commit09911bf2008-07-26 23:55:29534 DCHECK(CalledOnValidThread());
535
536 bool result = false;
initial.commit09911bf2008-07-26 23:55:29537
538 const Preference* pref = FindPreference(path);
539 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40540 NOTREACHED() << "Trying to read an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29541 return result;
542 }
543 bool rv = pref->GetValue()->GetAsBoolean(&result);
544 DCHECK(rv);
545 return result;
546}
547
[email protected]57ecc4b2010-08-11 03:02:51548int PrefService::GetInteger(const char* path) const {
initial.commit09911bf2008-07-26 23:55:29549 DCHECK(CalledOnValidThread());
550
551 int result = 0;
initial.commit09911bf2008-07-26 23:55:29552
553 const Preference* pref = FindPreference(path);
554 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40555 NOTREACHED() << "Trying to read an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29556 return result;
557 }
558 bool rv = pref->GetValue()->GetAsInteger(&result);
559 DCHECK(rv);
560 return result;
561}
562
[email protected]fb534c92011-02-01 01:02:07563double PrefService::GetDouble(const char* path) const {
initial.commit09911bf2008-07-26 23:55:29564 DCHECK(CalledOnValidThread());
565
566 double result = 0.0;
initial.commit09911bf2008-07-26 23:55:29567
568 const Preference* pref = FindPreference(path);
569 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40570 NOTREACHED() << "Trying to read an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29571 return result;
572 }
[email protected]fb534c92011-02-01 01:02:07573 bool rv = pref->GetValue()->GetAsDouble(&result);
initial.commit09911bf2008-07-26 23:55:29574 DCHECK(rv);
575 return result;
576}
577
[email protected]57ecc4b2010-08-11 03:02:51578std::string PrefService::GetString(const char* path) const {
initial.commit09911bf2008-07-26 23:55:29579 DCHECK(CalledOnValidThread());
580
[email protected]ddd231e2010-06-29 20:35:19581 std::string result;
[email protected]8e50b602009-03-03 22:59:43582
initial.commit09911bf2008-07-26 23:55:29583 const Preference* pref = FindPreference(path);
584 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40585 NOTREACHED() << "Trying to read an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29586 return result;
587 }
588 bool rv = pref->GetValue()->GetAsString(&result);
589 DCHECK(rv);
590 return result;
591}
592
[email protected]57ecc4b2010-08-11 03:02:51593FilePath PrefService::GetFilePath(const char* path) const {
[email protected]b9636002009-03-04 00:05:25594 DCHECK(CalledOnValidThread());
595
[email protected]68d9d352011-02-21 16:35:04596 FilePath result;
[email protected]b9636002009-03-04 00:05:25597
598 const Preference* pref = FindPreference(path);
599 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40600 NOTREACHED() << "Trying to read an unregistered pref: " << path;
[email protected]b9636002009-03-04 00:05:25601 return FilePath(result);
602 }
[email protected]8703b2b2011-03-15 09:51:50603 bool rv = base::GetValueAsFilePath(*pref->GetValue(), &result);
[email protected]b9636002009-03-04 00:05:25604 DCHECK(rv);
[email protected]68d9d352011-02-21 16:35:04605 return result;
[email protected]b9636002009-03-04 00:05:25606}
607
[email protected]57ecc4b2010-08-11 03:02:51608bool PrefService::HasPrefPath(const char* path) const {
[email protected]9a8c4022011-01-25 14:25:33609 const Preference* pref = FindPreference(path);
610 return pref && !pref->IsDefaultValue();
initial.commit09911bf2008-07-26 23:55:29611}
612
[email protected]ebd0b022011-01-27 13:24:14613DictionaryValue* PrefService::GetPreferenceValues() const {
614 DCHECK(CalledOnValidThread());
615 DictionaryValue* out = new DictionaryValue;
616 DefaultPrefStore::const_iterator i = default_store_->begin();
617 for (; i != default_store_->end(); ++i) {
[email protected]8874e02172011-03-11 19:44:08618 const Preference* pref = FindPreference(i->first.c_str());
619 DCHECK(pref);
620 const Value* value = pref->GetValue();
621 DCHECK(value);
[email protected]ebd0b022011-01-27 13:24:14622 out->Set(i->first, value->DeepCopy());
623 }
624 return out;
625}
626
initial.commit09911bf2008-07-26 23:55:29627const PrefService::Preference* PrefService::FindPreference(
[email protected]57ecc4b2010-08-11 03:02:51628 const char* pref_name) const {
initial.commit09911bf2008-07-26 23:55:29629 DCHECK(CalledOnValidThread());
[email protected]9a8c4022011-01-25 14:25:33630 Preference p(this, pref_name, Value::TYPE_NULL);
initial.commit09911bf2008-07-26 23:55:29631 PreferenceSet::const_iterator it = prefs_.find(&p);
[email protected]9a8c4022011-01-25 14:25:33632 if (it != prefs_.end())
633 return *it;
[email protected]bab1c13f2011-08-12 20:59:02634 const base::Value::Type type = default_store_->GetType(pref_name);
[email protected]9a8c4022011-01-25 14:25:33635 if (type == Value::TYPE_NULL)
636 return NULL;
637 Preference* new_pref = new Preference(this, pref_name, type);
638 prefs_.insert(new_pref);
639 return new_pref;
initial.commit09911bf2008-07-26 23:55:29640}
641
[email protected]acd78969c2010-12-08 09:49:11642bool PrefService::ReadOnly() const {
[email protected]f2d1f612010-12-09 15:10:17643 return user_pref_store_->ReadOnly();
[email protected]acd78969c2010-12-08 09:49:11644}
645
[email protected]59c10712012-03-13 02:10:34646PrefService::PrefInitializationStatus PrefService::GetInitializationStatus()
647 const {
648 if (!user_pref_store_->IsInitializationComplete())
649 return INITIALIZATION_STATUS_WAITING;
650
651 switch (user_pref_store_->GetReadError()) {
652 case PersistentPrefStore::PREF_READ_ERROR_NONE:
653 return INITIALIZATION_STATUS_SUCCESS;
654 case PersistentPrefStore::PREF_READ_ERROR_NO_FILE:
655 return INITIALIZATION_STATUS_CREATED_NEW_PROFILE;
656 default:
657 return INITIALIZATION_STATUS_ERROR;
658 }
659}
660
[email protected]57ecc4b2010-08-11 03:02:51661bool PrefService::IsManagedPreference(const char* pref_name) const {
[email protected]d90de1c02010-07-19 19:50:48662 const Preference* pref = FindPreference(pref_name);
[email protected]9a8c4022011-01-25 14:25:33663 return pref && pref->IsManaged();
[email protected]d90de1c02010-07-19 19:50:48664}
665
[email protected]d6bbd2932012-03-19 17:10:07666bool PrefService::IsUserModifiablePreference(const char* pref_name) const {
667 const Preference* pref = FindPreference(pref_name);
668 return pref && pref->IsUserModifiable();
669}
670
[email protected]57ecc4b2010-08-11 03:02:51671const DictionaryValue* PrefService::GetDictionary(const char* path) const {
initial.commit09911bf2008-07-26 23:55:29672 DCHECK(CalledOnValidThread());
673
initial.commit09911bf2008-07-26 23:55:29674 const Preference* pref = FindPreference(path);
675 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40676 NOTREACHED() << "Trying to read an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29677 return NULL;
678 }
679 const Value* value = pref->GetValue();
[email protected]11b040b2011-02-02 12:42:25680 if (value->GetType() != Value::TYPE_DICTIONARY) {
681 NOTREACHED();
initial.commit09911bf2008-07-26 23:55:29682 return NULL;
[email protected]11b040b2011-02-02 12:42:25683 }
initial.commit09911bf2008-07-26 23:55:29684 return static_cast<const DictionaryValue*>(value);
685}
686
[email protected]1a5a31f2012-04-26 20:21:34687const base::Value* PrefService::GetUserPrefValue(const char* path) const {
688 DCHECK(CalledOnValidThread());
689
690 const Preference* pref = FindPreference(path);
691 if (!pref) {
692 NOTREACHED() << "Trying to get an unregistered pref: " << path;
693 return NULL;
694 }
695
696 // Look for an existing preference in the user store. If it doesn't
697 // exist, return NULL.
698 base::Value* value = NULL;
699 if (user_pref_store_->GetMutableValue(path, &value) !=
700 PersistentPrefStore::READ_OK) {
701 return NULL;
702 }
703
704 if (!value->IsType(pref->GetType())) {
705 NOTREACHED() << "Pref value type doesn't match registered type.";
706 return NULL;
707 }
708
709 return value;
710}
711
[email protected]57ecc4b2010-08-11 03:02:51712const ListValue* PrefService::GetList(const char* path) const {
initial.commit09911bf2008-07-26 23:55:29713 DCHECK(CalledOnValidThread());
714
initial.commit09911bf2008-07-26 23:55:29715 const Preference* pref = FindPreference(path);
716 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40717 NOTREACHED() << "Trying to read an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29718 return NULL;
719 }
720 const Value* value = pref->GetValue();
[email protected]11b040b2011-02-02 12:42:25721 if (value->GetType() != Value::TYPE_LIST) {
722 NOTREACHED();
initial.commit09911bf2008-07-26 23:55:29723 return NULL;
[email protected]11b040b2011-02-02 12:42:25724 }
initial.commit09911bf2008-07-26 23:55:29725 return static_cast<const ListValue*>(value);
726}
727
[email protected]57ecc4b2010-08-11 03:02:51728void PrefService::AddPrefObserver(const char* path,
[email protected]6c2381d2011-10-19 02:52:53729 content::NotificationObserver* obs) {
[email protected]d81288a02010-08-18 07:25:50730 pref_notifier_->AddPrefObserver(path, obs);
initial.commit09911bf2008-07-26 23:55:29731}
732
[email protected]57ecc4b2010-08-11 03:02:51733void PrefService::RemovePrefObserver(const char* path,
[email protected]6c2381d2011-10-19 02:52:53734 content::NotificationObserver* obs) {
[email protected]d81288a02010-08-18 07:25:50735 pref_notifier_->RemovePrefObserver(path, obs);
initial.commit09911bf2008-07-26 23:55:29736}
737
[email protected]d36f941b2011-05-09 06:19:16738void PrefService::RegisterPreference(const char* path,
739 Value* default_value,
740 PrefSyncStatus sync_status) {
initial.commit09911bf2008-07-26 23:55:29741 DCHECK(CalledOnValidThread());
742
[email protected]c3b54f372010-09-14 08:25:07743 // The main code path takes ownership, but most don't. We'll be safe.
744 scoped_ptr<Value> scoped_value(default_value);
745
746 if (FindPreference(path)) {
747 NOTREACHED() << "Tried to register duplicate pref " << path;
initial.commit09911bf2008-07-26 23:55:29748 return;
749 }
[email protected]c3b54f372010-09-14 08:25:07750
[email protected]bab1c13f2011-08-12 20:59:02751 base::Value::Type orig_type = default_value->GetType();
[email protected]99cc9a02010-09-17 07:53:28752 DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) <<
753 "invalid preference type: " << orig_type;
754
[email protected]ea3e4972012-04-12 03:41:37755 // For ListValue and DictionaryValue with non empty default, empty value
756 // for |path| needs to be persisted in |user_pref_store_|. So that
757 // non empty default is not used when user sets an empty ListValue or
758 // DictionaryValue.
759 bool needs_empty_value = false;
760 if (orig_type == base::Value::TYPE_LIST) {
761 const base::ListValue* list = NULL;
762 if (default_value->GetAsList(&list) && !list->empty())
763 needs_empty_value = true;
764 } else if (orig_type == base::Value::TYPE_DICTIONARY) {
765 const base::DictionaryValue* dict = NULL;
766 if (default_value->GetAsDictionary(&dict) && !dict->empty())
767 needs_empty_value = true;
768 }
769 if (needs_empty_value)
770 user_pref_store_->MarkNeedsEmptyValue(path);
771
[email protected]9a8c4022011-01-25 14:25:33772 // Hand off ownership.
773 default_store_->SetDefaultValue(path, scoped_value.release());
[email protected]d36f941b2011-05-09 06:19:16774
775 // Register with sync if necessary.
776 if (sync_status == SYNCABLE_PREF && pref_sync_associator_.get())
777 pref_sync_associator_->RegisterPref(path);
initial.commit09911bf2008-07-26 23:55:29778}
779
[email protected]7a5f5932011-12-29 10:35:49780void PrefService::UnregisterPreference(const char* path) {
781 DCHECK(CalledOnValidThread());
782
783 Preference p(this, path, Value::TYPE_NULL);
[email protected]398007ce2012-01-05 19:25:50784 PreferenceSet::iterator it = prefs_.find(&p);
[email protected]7a5f5932011-12-29 10:35:49785 if (it == prefs_.end()) {
786 NOTREACHED() << "Trying to unregister an unregistered pref: " << path;
787 return;
788 }
789
[email protected]5a56d372011-12-29 11:34:40790 delete *it;
[email protected]7a5f5932011-12-29 10:35:49791 prefs_.erase(it);
792 default_store_->RemoveDefaultValue(path);
793 if (pref_sync_associator_.get() &&
794 pref_sync_associator_->IsPrefRegistered(path)) {
795 pref_sync_associator_->UnregisterPref(path);
796 }
797}
798
[email protected]57ecc4b2010-08-11 03:02:51799void PrefService::ClearPref(const char* path) {
initial.commit09911bf2008-07-26 23:55:29800 DCHECK(CalledOnValidThread());
801
802 const Preference* pref = FindPreference(path);
803 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40804 NOTREACHED() << "Trying to clear an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29805 return;
806 }
[email protected]f2d1f612010-12-09 15:10:17807 user_pref_store_->RemoveValue(path);
initial.commit09911bf2008-07-26 23:55:29808}
809
[email protected]57ecc4b2010-08-11 03:02:51810void PrefService::Set(const char* path, const Value& value) {
[email protected]b99c41c2011-04-27 15:18:48811 SetUserPrefValue(path, value.DeepCopy());
[email protected]a048d7e42009-12-01 01:02:39812}
813
[email protected]57ecc4b2010-08-11 03:02:51814void PrefService::SetBoolean(const char* path, bool value) {
[email protected]c3b54f372010-09-14 08:25:07815 SetUserPrefValue(path, Value::CreateBooleanValue(value));
initial.commit09911bf2008-07-26 23:55:29816}
817
[email protected]57ecc4b2010-08-11 03:02:51818void PrefService::SetInteger(const char* path, int value) {
[email protected]c3b54f372010-09-14 08:25:07819 SetUserPrefValue(path, Value::CreateIntegerValue(value));
initial.commit09911bf2008-07-26 23:55:29820}
821
[email protected]fb534c92011-02-01 01:02:07822void PrefService::SetDouble(const char* path, double value) {
823 SetUserPrefValue(path, Value::CreateDoubleValue(value));
initial.commit09911bf2008-07-26 23:55:29824}
825
[email protected]57ecc4b2010-08-11 03:02:51826void PrefService::SetString(const char* path, const std::string& value) {
[email protected]c3b54f372010-09-14 08:25:07827 SetUserPrefValue(path, Value::CreateStringValue(value));
initial.commit09911bf2008-07-26 23:55:29828}
829
[email protected]57ecc4b2010-08-11 03:02:51830void PrefService::SetFilePath(const char* path, const FilePath& value) {
[email protected]8703b2b2011-03-15 09:51:50831 SetUserPrefValue(path, base::CreateFilePathValue(value));
[email protected]b9636002009-03-04 00:05:25832}
833
[email protected]57ecc4b2010-08-11 03:02:51834void PrefService::SetInt64(const char* path, int64 value) {
[email protected]c3b54f372010-09-14 08:25:07835 SetUserPrefValue(path, Value::CreateStringValue(base::Int64ToString(value)));
[email protected]0bb1a622009-03-04 03:22:32836}
837
[email protected]57ecc4b2010-08-11 03:02:51838int64 PrefService::GetInt64(const char* path) const {
[email protected]0bb1a622009-03-04 03:22:32839 DCHECK(CalledOnValidThread());
840
[email protected]0bb1a622009-03-04 03:22:32841 const Preference* pref = FindPreference(path);
842 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40843 NOTREACHED() << "Trying to read an unregistered pref: " << path;
[email protected]c3453302009-12-01 01:33:08844 return 0;
[email protected]0bb1a622009-03-04 03:22:32845 }
[email protected]dc9a6762010-08-16 07:13:53846 std::string result("0");
[email protected]0bb1a622009-03-04 03:22:32847 bool rv = pref->GetValue()->GetAsString(&result);
848 DCHECK(rv);
[email protected]e83326f2010-07-31 17:29:25849
850 int64 val;
[email protected]dc9a6762010-08-16 07:13:53851 base::StringToInt64(result, &val);
[email protected]e83326f2010-07-31 17:29:25852 return val;
[email protected]0bb1a622009-03-04 03:22:32853}
854
[email protected]26418b72011-03-30 14:07:39855Value* PrefService::GetMutableUserPref(const char* path,
[email protected]bab1c13f2011-08-12 20:59:02856 base::Value::Type type) {
[email protected]26418b72011-03-30 14:07:39857 CHECK(type == Value::TYPE_DICTIONARY || type == Value::TYPE_LIST);
initial.commit09911bf2008-07-26 23:55:29858 DCHECK(CalledOnValidThread());
859
860 const Preference* pref = FindPreference(path);
861 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40862 NOTREACHED() << "Trying to get an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29863 return NULL;
864 }
[email protected]26418b72011-03-30 14:07:39865 if (pref->GetType() != type) {
866 NOTREACHED() << "Wrong type for GetMutableValue: " << path;
initial.commit09911bf2008-07-26 23:55:29867 return NULL;
868 }
869
[email protected]e0250892010-10-01 18:57:53870 // Look for an existing preference in the user store. If it doesn't
871 // exist or isn't the correct type, create a new user preference.
[email protected]26418b72011-03-30 14:07:39872 Value* value = NULL;
873 if (user_pref_store_->GetMutableValue(path, &value)
[email protected]f2d1f612010-12-09 15:10:17874 != PersistentPrefStore::READ_OK ||
[email protected]26418b72011-03-30 14:07:39875 !value->IsType(type)) {
876 if (type == Value::TYPE_DICTIONARY) {
877 value = new DictionaryValue;
878 } else if (type == Value::TYPE_LIST) {
879 value = new ListValue;
880 } else {
881 NOTREACHED();
882 }
883 user_pref_store_->SetValueSilently(path, value);
initial.commit09911bf2008-07-26 23:55:29884 }
[email protected]26418b72011-03-30 14:07:39885 return value;
886}
887
[email protected]68bf41a2011-03-25 16:38:31888void PrefService::ReportUserPrefChanged(const std::string& key) {
[email protected]f89ee342011-03-07 09:28:27889 user_pref_store_->ReportValueChanged(key);
890}
891
[email protected]c3b54f372010-09-14 08:25:07892void PrefService::SetUserPrefValue(const char* path, Value* new_value) {
[email protected]b99c41c2011-04-27 15:18:48893 scoped_ptr<Value> owned_value(new_value);
[email protected]c3b54f372010-09-14 08:25:07894 DCHECK(CalledOnValidThread());
895
896 const Preference* pref = FindPreference(path);
897 if (!pref) {
898 NOTREACHED() << "Trying to write an unregistered pref: " << path;
899 return;
900 }
[email protected]99cc9a02010-09-17 07:53:28901 if (pref->GetType() != new_value->GetType()) {
902 NOTREACHED() << "Trying to set pref " << path
903 << " of type " << pref->GetType()
[email protected]c3b54f372010-09-14 08:25:07904 << " to value of type " << new_value->GetType();
905 return;
906 }
907
[email protected]b99c41c2011-04-27 15:18:48908 user_pref_store_->SetValue(path, owned_value.release());
[email protected]73c47932010-12-06 18:13:43909}
910
[email protected]d36f941b2011-05-09 06:19:16911SyncableService* PrefService::GetSyncableService() {
912 return pref_sync_associator_.get();
913}
914
[email protected]d3b05ea2012-01-24 22:57:05915void PrefService::UpdateCommandLinePrefStore(CommandLine* command_line) {
916 // If |pref_service_forked_| is true, then this PrefService and the forked
917 // copies will be out of sync.
918 DCHECK(!pref_service_forked_);
919 pref_value_store_->UpdateCommandLinePrefStore(
920 new CommandLinePrefStore(command_line));
921}
922
initial.commit09911bf2008-07-26 23:55:29923///////////////////////////////////////////////////////////////////////////////
924// PrefService::Preference
925
[email protected]c3b54f372010-09-14 08:25:07926PrefService::Preference::Preference(const PrefService* service,
[email protected]9a8c4022011-01-25 14:25:33927 const char* name,
[email protected]bab1c13f2011-08-12 20:59:02928 base::Value::Type type)
[email protected]99cc9a02010-09-17 07:53:28929 : name_(name),
[email protected]9a8c4022011-01-25 14:25:33930 type_(type),
[email protected]c3b54f372010-09-14 08:25:07931 pref_service_(service) {
initial.commit09911bf2008-07-26 23:55:29932 DCHECK(name);
[email protected]c3b54f372010-09-14 08:25:07933 DCHECK(service);
[email protected]99cc9a02010-09-17 07:53:28934}
935
[email protected]bab1c13f2011-08-12 20:59:02936base::Value::Type PrefService::Preference::GetType() const {
[email protected]9a8c4022011-01-25 14:25:33937 return type_;
initial.commit09911bf2008-07-26 23:55:29938}
939
940const Value* PrefService::Preference::GetValue() const {
[email protected]c3b54f372010-09-14 08:25:07941 DCHECK(pref_service_->FindPreference(name_.c_str())) <<
initial.commit09911bf2008-07-26 23:55:29942 "Must register pref before getting its value";
943
[email protected]68bf41a2011-03-25 16:38:31944 const Value* found_value = NULL;
[email protected]887288f02011-02-04 22:52:46945 if (pref_value_store()->GetValue(name_, type_, &found_value)) {
[email protected]9a8c4022011-01-25 14:25:33946 DCHECK(found_value->IsType(type_));
[email protected]99cc9a02010-09-17 07:53:28947 return found_value;
[email protected]9a8c4022011-01-25 14:25:33948 }
initial.commit09911bf2008-07-26 23:55:29949
[email protected]c3b54f372010-09-14 08:25:07950 // Every registered preference has at least a default value.
[email protected]99cc9a02010-09-17 07:53:28951 NOTREACHED() << "no valid value found for registered pref " << name_;
[email protected]c3b54f372010-09-14 08:25:07952 return NULL;
[email protected]40a47c162010-09-09 11:14:01953}
954
955bool PrefService::Preference::IsManaged() const {
[email protected]887288f02011-02-04 22:52:46956 return pref_value_store()->PrefValueInManagedStore(name_.c_str());
[email protected]40a47c162010-09-09 11:14:01957}
958
[email protected]a5437282011-12-12 12:33:21959bool PrefService::Preference::IsRecommended() const {
960 return pref_value_store()->PrefValueFromRecommendedStore(name_.c_str());
961}
962
[email protected]40a47c162010-09-09 11:14:01963bool PrefService::Preference::HasExtensionSetting() const {
[email protected]887288f02011-02-04 22:52:46964 return pref_value_store()->PrefValueInExtensionStore(name_.c_str());
[email protected]40a47c162010-09-09 11:14:01965}
966
967bool PrefService::Preference::HasUserSetting() const {
[email protected]887288f02011-02-04 22:52:46968 return pref_value_store()->PrefValueInUserStore(name_.c_str());
[email protected]40a47c162010-09-09 11:14:01969}
970
971bool PrefService::Preference::IsExtensionControlled() const {
[email protected]887288f02011-02-04 22:52:46972 return pref_value_store()->PrefValueFromExtensionStore(name_.c_str());
[email protected]40a47c162010-09-09 11:14:01973}
974
975bool PrefService::Preference::IsUserControlled() const {
[email protected]887288f02011-02-04 22:52:46976 return pref_value_store()->PrefValueFromUserStore(name_.c_str());
[email protected]c3b54f372010-09-14 08:25:07977}
978
979bool PrefService::Preference::IsDefaultValue() const {
[email protected]887288f02011-02-04 22:52:46980 return pref_value_store()->PrefValueFromDefaultStore(name_.c_str());
[email protected]d7449e82010-07-14 11:42:35981}
[email protected]74379bc52010-07-21 13:54:08982
983bool PrefService::Preference::IsUserModifiable() const {
[email protected]887288f02011-02-04 22:52:46984 return pref_value_store()->PrefValueUserModifiable(name_.c_str());
[email protected]74379bc52010-07-21 13:54:08985}
[email protected]9a28f132011-02-24 21:15:16986
987bool PrefService::Preference::IsExtensionModifiable() const {
988 return pref_value_store()->PrefValueExtensionModifiable(name_.c_str());
989}