blob: 6cb98364ed38ee4d05345c2c50f46859adb20509 [file] [log] [blame]
[email protected]9a8c4022011-01-25 14:25:331// Copyright (c) 2011 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]aa4dc5e22010-06-16 11:32:5410#include "base/command_line.h"
[email protected]c02c853d72010-08-07 06:23:2411#include "base/file_path.h"
[email protected]1d01d412010-08-20 00:36:0112#include "base/file_util.h"
initial.commit09911bf2008-07-26 23:55:2913#include "base/logging.h"
14#include "base/message_loop.h"
[email protected]835d7c82010-10-14 04:38:3815#include "base/metrics/histogram.h"
[email protected]7286e3fc2011-07-19 22:13:2416#include "base/stl_util.h"
[email protected]e83326f2010-07-31 17:29:2517#include "base/string_number_conversions.h"
initial.commit09911bf2008-07-26 23:55:2918#include "base/string_util.h"
[email protected]8703b2b2011-03-15 09:51:5019#include "base/value_conversions.h"
[email protected]66de4f092009-09-04 23:59:4020#include "build/build_config.h"
[email protected]d36f941b2011-05-09 06:19:1621#include "chrome/browser/browser_process.h"
[email protected]9a8c4022011-01-25 14:25:3322#include "chrome/browser/extensions/extension_pref_store.h"
[email protected]acd78969c2010-12-08 09:49:1123#include "chrome/browser/policy/configuration_policy_pref_store.h"
24#include "chrome/browser/prefs/command_line_pref_store.h"
[email protected]f2d1f612010-12-09 15:10:1725#include "chrome/browser/prefs/default_pref_store.h"
[email protected]a78e9482011-07-14 19:22:2926#include "chrome/browser/prefs/incognito_user_pref_store.h"
[email protected]d36f941b2011-05-09 06:19:1627#include "chrome/browser/prefs/pref_model_associator.h"
[email protected]acd78969c2010-12-08 09:49:1128#include "chrome/browser/prefs/pref_notifier_impl.h"
[email protected]39d9f62c2010-12-03 10:48:5029#include "chrome/browser/prefs/pref_value_store.h"
[email protected]c7fb2da32011-04-14 20:47:1030#include "chrome/browser/ui/profile_error_dialog.h"
[email protected]e4f86492011-04-13 12:23:5331#include "chrome/common/json_pref_store.h"
[email protected]5f945a0e2011-03-01 17:47:5332#include "content/browser/browser_thread.h"
[email protected]ebbbb9f2011-03-09 13:16:1433#include "content/common/notification_service.h"
[email protected]ba399672010-04-06 15:42:3934#include "grit/chromium_strings.h"
[email protected]34ac8f32009-02-22 23:03:2735#include "grit/generated_resources.h"
[email protected]c051a1b2011-01-21 23:30:1736#include "ui/base/l10n/l10n_util.h"
initial.commit09911bf2008-07-26 23:55:2937
38namespace {
39
initial.commit09911bf2008-07-26 23:55:2940// A helper function for RegisterLocalized*Pref that creates a Value* based on
41// the string value in the locale dll. Because we control the values in a
42// locale dll, this should always return a Value of the appropriate type.
[email protected]bab1c13f2011-08-12 20:59:0243Value* CreateLocaleDefaultValue(base::Value::Type type, int message_id) {
[email protected]16b527162010-08-15 18:37:1044 std::string resource_string = l10n_util::GetStringUTF8(message_id);
initial.commit09911bf2008-07-26 23:55:2945 DCHECK(!resource_string.empty());
46 switch (type) {
47 case Value::TYPE_BOOLEAN: {
[email protected]16b527162010-08-15 18:37:1048 if ("true" == resource_string)
initial.commit09911bf2008-07-26 23:55:2949 return Value::CreateBooleanValue(true);
[email protected]16b527162010-08-15 18:37:1050 if ("false" == resource_string)
initial.commit09911bf2008-07-26 23:55:2951 return Value::CreateBooleanValue(false);
52 break;
53 }
54
55 case Value::TYPE_INTEGER: {
[email protected]e83326f2010-07-31 17:29:2556 int val;
[email protected]16b527162010-08-15 18:37:1057 base::StringToInt(resource_string, &val);
[email protected]e83326f2010-07-31 17:29:2558 return Value::CreateIntegerValue(val);
initial.commit09911bf2008-07-26 23:55:2959 }
60
[email protected]fb534c92011-02-01 01:02:0761 case Value::TYPE_DOUBLE: {
[email protected]e83326f2010-07-31 17:29:2562 double val;
[email protected]16b527162010-08-15 18:37:1063 base::StringToDouble(resource_string, &val);
[email protected]fb534c92011-02-01 01:02:0764 return Value::CreateDoubleValue(val);
initial.commit09911bf2008-07-26 23:55:2965 }
66
67 case Value::TYPE_STRING: {
68 return Value::CreateStringValue(resource_string);
initial.commit09911bf2008-07-26 23:55:2969 }
70
71 default: {
[email protected]b154e6f2009-03-06 01:52:4072 NOTREACHED() <<
[email protected]c3b54f372010-09-14 08:25:0773 "list and dictionary types cannot have default locale values";
initial.commit09911bf2008-07-26 23:55:2974 }
75 }
76 NOTREACHED();
77 return Value::CreateNullValue();
78}
79
[email protected]ba399672010-04-06 15:42:3980// Forwards a notification after a PostMessage so that we can wait for the
81// MessageLoop to run.
[email protected]845b43a82011-05-11 10:14:4382void NotifyReadError(int message_id) {
[email protected]c7fb2da32011-04-14 20:47:1083 ShowProfileErrorDialog(message_id);
[email protected]ba399672010-04-06 15:42:3984}
85
[email protected]845b43a82011-05-11 10:14:4386// Shows notifications which correspond to PersistentPrefStore's reading errors.
87class ReadErrorHandler : public PersistentPrefStore::ReadErrorDelegate {
88 public:
89 virtual void OnError(PersistentPrefStore::PrefReadError error) {
90 if (error != PersistentPrefStore::PREF_READ_ERROR_NONE) {
91 // Failing to load prefs on startup is a bad thing(TM). See bug 38352 for
92 // an example problem that this can cause.
93 // Do some diagnosis and try to avoid losing data.
94 int message_id = 0;
95 if (error <= PersistentPrefStore::PREF_READ_ERROR_JSON_TYPE) {
96 message_id = IDS_PREFERENCES_CORRUPT_ERROR;
97 } else if (error != PersistentPrefStore::PREF_READ_ERROR_NO_FILE) {
98 message_id = IDS_PREFERENCES_UNREADABLE_ERROR;
99 }
100
101 if (message_id) {
102 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
103 NewRunnableFunction(&NotifyReadError, message_id));
104 }
105 UMA_HISTOGRAM_ENUMERATION("PrefService.ReadError", error, 20);
106 }
107 }
108};
109
initial.commit09911bf2008-07-26 23:55:29110} // namespace
111
[email protected]db198b22010-07-12 16:48:49112// static
[email protected]a9c23a52010-08-04 09:13:44113PrefService* PrefService::CreatePrefService(const FilePath& pref_filename,
[email protected]f2d1f612010-12-09 15:10:17114 PrefStore* extension_prefs,
[email protected]845b43a82011-05-11 10:14:43115 bool async) {
[email protected]acd78969c2010-12-08 09:49:11116 using policy::ConfigurationPolicyPrefStore;
117
[email protected]1d01d412010-08-20 00:36:01118#if defined(OS_LINUX)
119 // We'd like to see what fraction of our users have the preferences
120 // stored on a network file system, as we've had no end of troubles
121 // with NFS/AFS.
122 // TODO(evanm): remove this once we've collected state.
123 file_util::FileSystemType fstype;
124 if (file_util::GetFileSystemType(pref_filename.DirName(), &fstype)) {
125 UMA_HISTOGRAM_ENUMERATION("PrefService.FileSystemType",
126 static_cast<int>(fstype),
127 file_util::FILE_SYSTEM_TYPE_COUNT);
128 }
129#endif
130
[email protected]f31e2e52011-07-14 16:01:19131#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]887288f02011-02-04 22:52:46132 ConfigurationPolicyPrefStore* managed_platform =
[email protected]acd78969c2010-12-08 09:49:11133 ConfigurationPolicyPrefStore::CreateManagedPlatformPolicyPrefStore();
[email protected]887288f02011-02-04 22:52:46134 ConfigurationPolicyPrefStore* managed_cloud =
[email protected]fcf53572011-06-29 15:44:37135 ConfigurationPolicyPrefStore::CreateManagedCloudPolicyPrefStore();
[email protected]f31e2e52011-07-14 16:01:19136 ConfigurationPolicyPrefStore* recommended_platform =
137 ConfigurationPolicyPrefStore::CreateRecommendedPlatformPolicyPrefStore();
138 ConfigurationPolicyPrefStore* recommended_cloud =
139 ConfigurationPolicyPrefStore::CreateRecommendedCloudPolicyPrefStore();
140#else
141 ConfigurationPolicyPrefStore* managed_platform = NULL;
142 ConfigurationPolicyPrefStore* managed_cloud = NULL;
143 ConfigurationPolicyPrefStore* recommended_platform = NULL;
144 ConfigurationPolicyPrefStore* recommended_cloud = NULL;
145#endif // ENABLE_CONFIGURATION_POLICY
146
[email protected]acd78969c2010-12-08 09:49:11147 CommandLinePrefStore* command_line =
148 new CommandLinePrefStore(CommandLine::ForCurrentProcess());
149 JsonPrefStore* user = new JsonPrefStore(
150 pref_filename,
151 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE));
[email protected]9a8c4022011-01-25 14:25:33152 DefaultPrefStore* default_pref_store = new DefaultPrefStore();
[email protected]acd78969c2010-12-08 09:49:11153
[email protected]845b43a82011-05-11 10:14:43154 return new PrefService(
155 managed_platform, managed_cloud, extension_prefs,
156 command_line, user, recommended_platform,
157 recommended_cloud, default_pref_store, async);
[email protected]9a8c4022011-01-25 14:25:33158}
159
160PrefService* PrefService::CreateIncognitoPrefService(
161 PrefStore* incognito_extension_prefs) {
162 return new PrefService(*this, incognito_extension_prefs);
[email protected]d8b08c92010-06-07 13:13:28163}
164
[email protected]acd78969c2010-12-08 09:49:11165PrefService::PrefService(PrefStore* managed_platform_prefs,
[email protected]887288f02011-02-04 22:52:46166 PrefStore* managed_cloud_prefs,
[email protected]acd78969c2010-12-08 09:49:11167 PrefStore* extension_prefs,
168 PrefStore* command_line_prefs,
[email protected]f2d1f612010-12-09 15:10:17169 PersistentPrefStore* user_prefs,
[email protected]887288f02011-02-04 22:52:46170 PrefStore* recommended_platform_prefs,
171 PrefStore* recommended_cloud_prefs,
[email protected]844a1002011-04-19 11:37:21172 DefaultPrefStore* default_store,
[email protected]845b43a82011-05-11 10:14:43173 bool async)
[email protected]9a8c4022011-01-25 14:25:33174 : user_pref_store_(user_prefs),
[email protected]845b43a82011-05-11 10:14:43175 default_store_(default_store) {
[email protected]d36f941b2011-05-09 06:19:16176 pref_sync_associator_.reset(new PrefModelAssociator(this));
[email protected]acd78969c2010-12-08 09:49:11177 pref_notifier_.reset(new PrefNotifierImpl(this));
[email protected]a98ce1262011-01-28 13:20:23178 pref_value_store_.reset(
[email protected]acd78969c2010-12-08 09:49:11179 new PrefValueStore(managed_platform_prefs,
[email protected]887288f02011-02-04 22:52:46180 managed_cloud_prefs,
[email protected]f2d1f612010-12-09 15:10:17181 extension_prefs,
[email protected]acd78969c2010-12-08 09:49:11182 command_line_prefs,
[email protected]f2d1f612010-12-09 15:10:17183 user_pref_store_,
[email protected]887288f02011-02-04 22:52:46184 recommended_platform_prefs,
185 recommended_cloud_prefs,
[email protected]9a8c4022011-01-25 14:25:33186 default_store,
[email protected]d36f941b2011-05-09 06:19:16187 pref_sync_associator_.get(),
[email protected]a98ce1262011-01-28 13:20:23188 pref_notifier_.get()));
[email protected]845b43a82011-05-11 10:14:43189 InitFromStorage(async);
initial.commit09911bf2008-07-26 23:55:29190}
191
[email protected]9a8c4022011-01-25 14:25:33192PrefService::PrefService(const PrefService& original,
193 PrefStore* incognito_extension_prefs)
194 : user_pref_store_(
[email protected]a78e9482011-07-14 19:22:29195 new IncognitoUserPrefStore(original.user_pref_store_.get())),
[email protected]845b43a82011-05-11 10:14:43196 default_store_(original.default_store_.get()) {
[email protected]d36f941b2011-05-09 06:19:16197 // Incognito mode doesn't sync, so no need to create PrefModelAssociator.
[email protected]9a8c4022011-01-25 14:25:33198 pref_notifier_.reset(new PrefNotifierImpl(this));
[email protected]a98ce1262011-01-28 13:20:23199 pref_value_store_.reset(original.pref_value_store_->CloneAndSpecialize(
[email protected]9a8c4022011-01-25 14:25:33200 NULL, // managed_platform_prefs
[email protected]887288f02011-02-04 22:52:46201 NULL, // managed_cloud_prefs
[email protected]9a8c4022011-01-25 14:25:33202 incognito_extension_prefs,
203 NULL, // command_line_prefs
204 user_pref_store_.get(),
[email protected]887288f02011-02-04 22:52:46205 NULL, // recommended_platform_prefs
206 NULL, // recommended_cloud_prefs
[email protected]9a8c4022011-01-25 14:25:33207 default_store_.get(),
[email protected]d36f941b2011-05-09 06:19:16208 NULL, // pref_sync_associator_
[email protected]a98ce1262011-01-28 13:20:23209 pref_notifier_.get()));
[email protected]9a8c4022011-01-25 14:25:33210}
211
initial.commit09911bf2008-07-26 23:55:29212PrefService::~PrefService() {
213 DCHECK(CalledOnValidThread());
initial.commit09911bf2008-07-26 23:55:29214 STLDeleteContainerPointers(prefs_.begin(), prefs_.end());
215 prefs_.clear();
[email protected]a98ce1262011-01-28 13:20:23216
217 // Reset pointers so accesses after destruction reliably crash.
218 pref_value_store_.reset();
219 user_pref_store_ = NULL;
220 default_store_ = NULL;
[email protected]d36f941b2011-05-09 06:19:16221 pref_sync_associator_.reset();
initial.commit09911bf2008-07-26 23:55:29222}
223
[email protected]845b43a82011-05-11 10:14:43224void PrefService::InitFromStorage(bool async) {
225 if (!async) {
226 ReadErrorHandler error_handler;
227 error_handler.OnError(user_pref_store_->ReadPrefs());
[email protected]844a1002011-04-19 11:37:21228 } else {
[email protected]845b43a82011-05-11 10:14:43229 // Guarantee that initialization happens after this function returned.
230 MessageLoop::current()->PostTask(
231 FROM_HERE,
232 NewRunnableMethod(user_pref_store_.get(),
233 &PersistentPrefStore::ReadPrefsAsync,
234 new ReadErrorHandler()));
[email protected]844a1002011-04-19 11:37:21235 }
[email protected]ba399672010-04-06 15:42:39236}
237
238bool PrefService::ReloadPersistentPrefs() {
[email protected]f2d1f612010-12-09 15:10:17239 return user_pref_store_->ReadPrefs() ==
240 PersistentPrefStore::PREF_READ_ERROR_NONE;
initial.commit09911bf2008-07-26 23:55:29241}
242
[email protected]6faa0e0d2009-04-28 06:50:36243bool PrefService::SavePersistentPrefs() {
244 DCHECK(CalledOnValidThread());
[email protected]f2d1f612010-12-09 15:10:17245 return user_pref_store_->WritePrefs();
[email protected]6faa0e0d2009-04-28 06:50:36246}
247
[email protected]6c1164042009-05-08 14:41:08248void PrefService::ScheduleSavePersistentPrefs() {
[email protected]6faa0e0d2009-04-28 06:50:36249 DCHECK(CalledOnValidThread());
[email protected]f2d1f612010-12-09 15:10:17250 user_pref_store_->ScheduleWritePrefs();
initial.commit09911bf2008-07-26 23:55:29251}
252
[email protected]3826fed2011-03-25 10:59:56253void PrefService::CommitPendingWrite() {
254 DCHECK(CalledOnValidThread());
255 user_pref_store_->CommitPendingWrite();
256}
257
[email protected]d36f941b2011-05-09 06:19:16258namespace {
259
260// If there's no g_browser_process or no local state, return true (for testing).
261bool IsLocalStatePrefService(PrefService* prefs){
262 return (!g_browser_process ||
263 !g_browser_process->local_state() ||
264 g_browser_process->local_state() == prefs);
265}
266
267// If there's no g_browser_process, return true (for testing).
268bool IsProfilePrefService(PrefService* prefs){
269 // TODO(zea): uncomment this once all preferences are only ever registered
270 // with either the local_state's pref service or the profile's pref service.
271 // return (!g_browser_process || g_browser_process->local_state() != prefs);
272 return true;
273}
274
275} // namespace
276
277
278// Local State prefs.
[email protected]57ecc4b2010-08-11 03:02:51279void PrefService::RegisterBooleanPref(const char* path,
initial.commit09911bf2008-07-26 23:55:29280 bool default_value) {
[email protected]d36f941b2011-05-09 06:19:16281 // If this fails, the pref service in use is a profile pref service, so the
282 // sync status must be provided (see profile pref registration calls below).
283 DCHECK(IsLocalStatePrefService(this));
284 RegisterPreference(path,
285 Value::CreateBooleanValue(default_value),
286 UNSYNCABLE_PREF);
initial.commit09911bf2008-07-26 23:55:29287}
288
[email protected]57ecc4b2010-08-11 03:02:51289void PrefService::RegisterIntegerPref(const char* path, int default_value) {
[email protected]d36f941b2011-05-09 06:19:16290 // If this fails, the pref service in use is a profile pref service, so the
291 // sync status must be provided (see profile pref registration calls below).
292 DCHECK(IsLocalStatePrefService(this));
293 RegisterPreference(path,
294 Value::CreateIntegerValue(default_value),
295 UNSYNCABLE_PREF);
initial.commit09911bf2008-07-26 23:55:29296}
297
[email protected]fb534c92011-02-01 01:02:07298void PrefService::RegisterDoublePref(const char* path, double default_value) {
[email protected]d36f941b2011-05-09 06:19:16299 // If this fails, the pref service in use is a profile pref service, so the
300 // sync status must be provided (see profile pref registration calls below).
301 DCHECK(IsLocalStatePrefService(this));
302 RegisterPreference(path,
303 Value::CreateDoubleValue(default_value),
304 UNSYNCABLE_PREF);
initial.commit09911bf2008-07-26 23:55:29305}
306
[email protected]57ecc4b2010-08-11 03:02:51307void PrefService::RegisterStringPref(const char* path,
[email protected]20ce516d2010-06-18 02:20:04308 const std::string& default_value) {
[email protected]d36f941b2011-05-09 06:19:16309 // If this fails, the pref service in use is a profile pref service, so the
310 // sync status must be provided (see profile pref registration calls below).
311 DCHECK(IsLocalStatePrefService(this));
312 RegisterPreference(path,
313 Value::CreateStringValue(default_value),
314 UNSYNCABLE_PREF);
initial.commit09911bf2008-07-26 23:55:29315}
316
[email protected]57ecc4b2010-08-11 03:02:51317void PrefService::RegisterFilePathPref(const char* path,
[email protected]b9636002009-03-04 00:05:25318 const FilePath& default_value) {
[email protected]d36f941b2011-05-09 06:19:16319 // If this fails, the pref service in use is a profile pref service, so the
320 // sync status must be provided (see profile pref registration calls below).
321 DCHECK(IsLocalStatePrefService(this));
322 RegisterPreference(path,
323 Value::CreateStringValue(default_value.value()),
324 UNSYNCABLE_PREF);
[email protected]b9636002009-03-04 00:05:25325}
326
[email protected]57ecc4b2010-08-11 03:02:51327void PrefService::RegisterListPref(const char* path) {
[email protected]d36f941b2011-05-09 06:19:16328 // If this fails, the pref service in use is a profile pref service, so the
329 // sync status must be provided (see profile pref registration calls below).
330 DCHECK(IsLocalStatePrefService(this));
331 RegisterPreference(path,
332 new ListValue(),
333 UNSYNCABLE_PREF);
initial.commit09911bf2008-07-26 23:55:29334}
335
[email protected]c2f23d012011-02-09 14:52:17336void PrefService::RegisterListPref(const char* path, ListValue* default_value) {
[email protected]d36f941b2011-05-09 06:19:16337 // If this fails, the pref service in use is a profile pref service, so the
338 // sync status must be provided (see profile pref registration calls below).
339 DCHECK(IsLocalStatePrefService(this));
340 RegisterPreference(path,
341 default_value,
342 UNSYNCABLE_PREF);
[email protected]c2f23d012011-02-09 14:52:17343}
344
[email protected]57ecc4b2010-08-11 03:02:51345void PrefService::RegisterDictionaryPref(const char* path) {
[email protected]d36f941b2011-05-09 06:19:16346 // If this fails, the pref service in use is a profile pref service, so the
347 // sync status must be provided (see profile pref registration calls below).
348 DCHECK(IsLocalStatePrefService(this));
349 RegisterPreference(path,
350 new DictionaryValue(),
351 UNSYNCABLE_PREF);
initial.commit09911bf2008-07-26 23:55:29352}
353
[email protected]c2f23d012011-02-09 14:52:17354void PrefService::RegisterDictionaryPref(const char* path,
355 DictionaryValue* default_value) {
[email protected]d36f941b2011-05-09 06:19:16356 // If this fails, the pref service in use is a profile pref service, so the
357 // sync status must be provided (see profile pref registration calls below).
358 DCHECK(IsLocalStatePrefService(this));
359 RegisterPreference(path,
360 default_value,
361 UNSYNCABLE_PREF);
[email protected]c2f23d012011-02-09 14:52:17362}
363
[email protected]57ecc4b2010-08-11 03:02:51364void PrefService::RegisterLocalizedBooleanPref(const char* path,
initial.commit09911bf2008-07-26 23:55:29365 int locale_default_message_id) {
[email protected]d36f941b2011-05-09 06:19:16366 // If this fails, the pref service in use is a profile pref service, so the
367 // sync status must be provided (see profile pref registration calls below).
368 DCHECK(IsLocalStatePrefService(this));
[email protected]c3b54f372010-09-14 08:25:07369 RegisterPreference(
370 path,
[email protected]d36f941b2011-05-09 06:19:16371 CreateLocaleDefaultValue(Value::TYPE_BOOLEAN, locale_default_message_id),
372 UNSYNCABLE_PREF);
initial.commit09911bf2008-07-26 23:55:29373}
374
[email protected]57ecc4b2010-08-11 03:02:51375void PrefService::RegisterLocalizedIntegerPref(const char* path,
initial.commit09911bf2008-07-26 23:55:29376 int locale_default_message_id) {
[email protected]d36f941b2011-05-09 06:19:16377 // If this fails, the pref service in use is a profile pref service, so the
378 // sync status must be provided (see profile pref registration calls below).
379 DCHECK(IsLocalStatePrefService(this));
[email protected]c3b54f372010-09-14 08:25:07380 RegisterPreference(
381 path,
[email protected]d36f941b2011-05-09 06:19:16382 CreateLocaleDefaultValue(Value::TYPE_INTEGER, locale_default_message_id),
383 UNSYNCABLE_PREF);
initial.commit09911bf2008-07-26 23:55:29384}
385
[email protected]fb534c92011-02-01 01:02:07386void PrefService::RegisterLocalizedDoublePref(const char* path,
387 int locale_default_message_id) {
[email protected]d36f941b2011-05-09 06:19:16388 // If this fails, the pref service in use is a profile pref service, so the
389 // sync status must be provided (see profile pref registration calls below).
390 DCHECK(IsLocalStatePrefService(this));
[email protected]c3b54f372010-09-14 08:25:07391 RegisterPreference(
392 path,
[email protected]d36f941b2011-05-09 06:19:16393 CreateLocaleDefaultValue(Value::TYPE_DOUBLE, locale_default_message_id),
394 UNSYNCABLE_PREF);
initial.commit09911bf2008-07-26 23:55:29395}
396
[email protected]57ecc4b2010-08-11 03:02:51397void PrefService::RegisterLocalizedStringPref(const char* path,
initial.commit09911bf2008-07-26 23:55:29398 int locale_default_message_id) {
[email protected]d36f941b2011-05-09 06:19:16399 // If this fails, the pref service in use is a profile pref service, so the
400 // sync status must be provided (see profile pref registration calls below).
401 DCHECK(IsLocalStatePrefService(this));
[email protected]c3b54f372010-09-14 08:25:07402 RegisterPreference(
403 path,
[email protected]d36f941b2011-05-09 06:19:16404 CreateLocaleDefaultValue(Value::TYPE_STRING, locale_default_message_id),
405 UNSYNCABLE_PREF);
406}
407
408void PrefService::RegisterInt64Pref(const char* path, int64 default_value) {
409 // If this fails, the pref service in use is a profile pref service, so the
410 // sync status must be provided (see profile pref registration calls below).
411 DCHECK(IsLocalStatePrefService(this));
412 RegisterPreference(
413 path,
414 Value::CreateStringValue(base::Int64ToString(default_value)),
415 UNSYNCABLE_PREF);
416}
417
418// Profile prefs (must use the sync_status variable).
419void PrefService::RegisterBooleanPref(const char* path,
420 bool default_value,
421 PrefSyncStatus sync_status) {
422 DCHECK(IsProfilePrefService(this));
423 RegisterPreference(path,
424 Value::CreateBooleanValue(default_value),
425 sync_status);
426}
427
428void PrefService::RegisterIntegerPref(const char* path,
429 int default_value,
430 PrefSyncStatus sync_status) {
431 DCHECK(IsProfilePrefService(this));
432 RegisterPreference(path,
433 Value::CreateIntegerValue(default_value),
434 sync_status);
435}
436
437void PrefService::RegisterDoublePref(const char* path,
438 double default_value,
439 PrefSyncStatus sync_status) {
440 DCHECK(IsProfilePrefService(this));
441 RegisterPreference(path,
442 Value::CreateDoubleValue(default_value),
443 sync_status);
444}
445
446void PrefService::RegisterStringPref(const char* path,
447 const std::string& default_value,
448 PrefSyncStatus sync_status) {
449 DCHECK(IsProfilePrefService(this));
450 RegisterPreference(path,
451 Value::CreateStringValue(default_value),
452 sync_status);
453}
454
455void PrefService::RegisterFilePathPref(const char* path,
456 const FilePath& default_value,
457 PrefSyncStatus sync_status) {
458 DCHECK(IsProfilePrefService(this));
459 RegisterPreference(path,
460 Value::CreateStringValue(default_value.value()),
461 sync_status);
462}
463
464void PrefService::RegisterListPref(const char* path,
465 PrefSyncStatus sync_status) {
466 DCHECK(IsProfilePrefService(this));
467 RegisterPreference(path, new ListValue(), sync_status);
468}
469
470void PrefService::RegisterListPref(const char* path,
471 ListValue* default_value,
472 PrefSyncStatus sync_status) {
473 DCHECK(IsProfilePrefService(this));
474 RegisterPreference(path, default_value, sync_status);
475}
476
477void PrefService::RegisterDictionaryPref(const char* path,
478 PrefSyncStatus sync_status) {
479 DCHECK(IsProfilePrefService(this));
480 RegisterPreference(path, new DictionaryValue(), sync_status);
481}
482
483void PrefService::RegisterDictionaryPref(const char* path,
484 DictionaryValue* default_value,
485 PrefSyncStatus sync_status) {
486 DCHECK(IsProfilePrefService(this));
487 RegisterPreference(path, default_value, sync_status);
488}
489
490void PrefService::RegisterLocalizedBooleanPref(const char* path,
491 int locale_default_message_id,
492 PrefSyncStatus sync_status) {
493 DCHECK(IsProfilePrefService(this));
494 RegisterPreference(
495 path,
496 CreateLocaleDefaultValue(Value::TYPE_BOOLEAN,locale_default_message_id),
497 sync_status);
498}
499
500void PrefService::RegisterLocalizedIntegerPref(const char* path,
501 int locale_default_message_id,
502 PrefSyncStatus sync_status) {
503 DCHECK(IsProfilePrefService(this));
504 RegisterPreference(
505 path,
506 CreateLocaleDefaultValue(Value::TYPE_INTEGER, locale_default_message_id),
507 sync_status);
508}
509
510void PrefService::RegisterLocalizedDoublePref(const char* path,
511 int locale_default_message_id,
512 PrefSyncStatus sync_status) {
513 DCHECK(IsProfilePrefService(this));
514 RegisterPreference(
515 path,
516 CreateLocaleDefaultValue(Value::TYPE_DOUBLE, locale_default_message_id),
517 sync_status);
518}
519
520void PrefService::RegisterLocalizedStringPref(const char* path,
521 int locale_default_message_id,
522 PrefSyncStatus sync_status) {
523 DCHECK(IsProfilePrefService(this));
524 RegisterPreference(
525 path,
526 CreateLocaleDefaultValue(Value::TYPE_STRING, locale_default_message_id),
527 sync_status);
528}
529
530void PrefService::RegisterInt64Pref(const char* path,
531 int64 default_value,
532 PrefSyncStatus sync_status) {
533 DCHECK(IsProfilePrefService(this));
534 RegisterPreference(
535 path,
536 Value::CreateStringValue(base::Int64ToString(default_value)),
537 sync_status);
initial.commit09911bf2008-07-26 23:55:29538}
539
[email protected]57ecc4b2010-08-11 03:02:51540bool PrefService::GetBoolean(const char* path) const {
initial.commit09911bf2008-07-26 23:55:29541 DCHECK(CalledOnValidThread());
542
543 bool result = false;
initial.commit09911bf2008-07-26 23:55:29544
545 const Preference* pref = FindPreference(path);
546 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40547 NOTREACHED() << "Trying to read an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29548 return result;
549 }
550 bool rv = pref->GetValue()->GetAsBoolean(&result);
551 DCHECK(rv);
552 return result;
553}
554
[email protected]57ecc4b2010-08-11 03:02:51555int PrefService::GetInteger(const char* path) const {
initial.commit09911bf2008-07-26 23:55:29556 DCHECK(CalledOnValidThread());
557
558 int result = 0;
initial.commit09911bf2008-07-26 23:55:29559
560 const Preference* pref = FindPreference(path);
561 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40562 NOTREACHED() << "Trying to read an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29563 return result;
564 }
565 bool rv = pref->GetValue()->GetAsInteger(&result);
566 DCHECK(rv);
567 return result;
568}
569
[email protected]fb534c92011-02-01 01:02:07570double PrefService::GetDouble(const char* path) const {
initial.commit09911bf2008-07-26 23:55:29571 DCHECK(CalledOnValidThread());
572
573 double result = 0.0;
initial.commit09911bf2008-07-26 23:55:29574
575 const Preference* pref = FindPreference(path);
576 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40577 NOTREACHED() << "Trying to read an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29578 return result;
579 }
[email protected]fb534c92011-02-01 01:02:07580 bool rv = pref->GetValue()->GetAsDouble(&result);
initial.commit09911bf2008-07-26 23:55:29581 DCHECK(rv);
582 return result;
583}
584
[email protected]57ecc4b2010-08-11 03:02:51585std::string PrefService::GetString(const char* path) const {
initial.commit09911bf2008-07-26 23:55:29586 DCHECK(CalledOnValidThread());
587
[email protected]ddd231e2010-06-29 20:35:19588 std::string result;
[email protected]8e50b602009-03-03 22:59:43589
initial.commit09911bf2008-07-26 23:55:29590 const Preference* pref = FindPreference(path);
591 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40592 NOTREACHED() << "Trying to read an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29593 return result;
594 }
595 bool rv = pref->GetValue()->GetAsString(&result);
596 DCHECK(rv);
597 return result;
598}
599
[email protected]57ecc4b2010-08-11 03:02:51600FilePath PrefService::GetFilePath(const char* path) const {
[email protected]b9636002009-03-04 00:05:25601 DCHECK(CalledOnValidThread());
602
[email protected]68d9d352011-02-21 16:35:04603 FilePath result;
[email protected]b9636002009-03-04 00:05:25604
605 const Preference* pref = FindPreference(path);
606 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40607 NOTREACHED() << "Trying to read an unregistered pref: " << path;
[email protected]b9636002009-03-04 00:05:25608 return FilePath(result);
609 }
[email protected]8703b2b2011-03-15 09:51:50610 bool rv = base::GetValueAsFilePath(*pref->GetValue(), &result);
[email protected]b9636002009-03-04 00:05:25611 DCHECK(rv);
[email protected]68d9d352011-02-21 16:35:04612 return result;
[email protected]b9636002009-03-04 00:05:25613}
614
[email protected]57ecc4b2010-08-11 03:02:51615bool PrefService::HasPrefPath(const char* path) const {
[email protected]9a8c4022011-01-25 14:25:33616 const Preference* pref = FindPreference(path);
617 return pref && !pref->IsDefaultValue();
initial.commit09911bf2008-07-26 23:55:29618}
619
[email protected]ebd0b022011-01-27 13:24:14620DictionaryValue* PrefService::GetPreferenceValues() const {
621 DCHECK(CalledOnValidThread());
622 DictionaryValue* out = new DictionaryValue;
623 DefaultPrefStore::const_iterator i = default_store_->begin();
624 for (; i != default_store_->end(); ++i) {
[email protected]8874e02172011-03-11 19:44:08625 const Preference* pref = FindPreference(i->first.c_str());
626 DCHECK(pref);
627 const Value* value = pref->GetValue();
628 DCHECK(value);
[email protected]ebd0b022011-01-27 13:24:14629 out->Set(i->first, value->DeepCopy());
630 }
631 return out;
632}
633
initial.commit09911bf2008-07-26 23:55:29634const PrefService::Preference* PrefService::FindPreference(
[email protected]57ecc4b2010-08-11 03:02:51635 const char* pref_name) const {
initial.commit09911bf2008-07-26 23:55:29636 DCHECK(CalledOnValidThread());
[email protected]9a8c4022011-01-25 14:25:33637 Preference p(this, pref_name, Value::TYPE_NULL);
initial.commit09911bf2008-07-26 23:55:29638 PreferenceSet::const_iterator it = prefs_.find(&p);
[email protected]9a8c4022011-01-25 14:25:33639 if (it != prefs_.end())
640 return *it;
[email protected]bab1c13f2011-08-12 20:59:02641 const base::Value::Type type = default_store_->GetType(pref_name);
[email protected]9a8c4022011-01-25 14:25:33642 if (type == Value::TYPE_NULL)
643 return NULL;
644 Preference* new_pref = new Preference(this, pref_name, type);
645 prefs_.insert(new_pref);
646 return new_pref;
initial.commit09911bf2008-07-26 23:55:29647}
648
[email protected]acd78969c2010-12-08 09:49:11649bool PrefService::ReadOnly() const {
[email protected]f2d1f612010-12-09 15:10:17650 return user_pref_store_->ReadOnly();
[email protected]acd78969c2010-12-08 09:49:11651}
652
[email protected]57ecc4b2010-08-11 03:02:51653bool PrefService::IsManagedPreference(const char* pref_name) const {
[email protected]d90de1c02010-07-19 19:50:48654 const Preference* pref = FindPreference(pref_name);
[email protected]9a8c4022011-01-25 14:25:33655 return pref && pref->IsManaged();
[email protected]d90de1c02010-07-19 19:50:48656}
657
[email protected]57ecc4b2010-08-11 03:02:51658const DictionaryValue* PrefService::GetDictionary(const char* path) const {
initial.commit09911bf2008-07-26 23:55:29659 DCHECK(CalledOnValidThread());
660
initial.commit09911bf2008-07-26 23:55:29661 const Preference* pref = FindPreference(path);
662 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40663 NOTREACHED() << "Trying to read an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29664 return NULL;
665 }
666 const Value* value = pref->GetValue();
[email protected]11b040b2011-02-02 12:42:25667 if (value->GetType() != Value::TYPE_DICTIONARY) {
668 NOTREACHED();
initial.commit09911bf2008-07-26 23:55:29669 return NULL;
[email protected]11b040b2011-02-02 12:42:25670 }
initial.commit09911bf2008-07-26 23:55:29671 return static_cast<const DictionaryValue*>(value);
672}
673
[email protected]57ecc4b2010-08-11 03:02:51674const ListValue* PrefService::GetList(const char* path) const {
initial.commit09911bf2008-07-26 23:55:29675 DCHECK(CalledOnValidThread());
676
initial.commit09911bf2008-07-26 23:55:29677 const Preference* pref = FindPreference(path);
678 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40679 NOTREACHED() << "Trying to read an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29680 return NULL;
681 }
682 const Value* value = pref->GetValue();
[email protected]11b040b2011-02-02 12:42:25683 if (value->GetType() != Value::TYPE_LIST) {
684 NOTREACHED();
initial.commit09911bf2008-07-26 23:55:29685 return NULL;
[email protected]11b040b2011-02-02 12:42:25686 }
initial.commit09911bf2008-07-26 23:55:29687 return static_cast<const ListValue*>(value);
688}
689
[email protected]57ecc4b2010-08-11 03:02:51690void PrefService::AddPrefObserver(const char* path,
initial.commit09911bf2008-07-26 23:55:29691 NotificationObserver* obs) {
[email protected]d81288a02010-08-18 07:25:50692 pref_notifier_->AddPrefObserver(path, obs);
initial.commit09911bf2008-07-26 23:55:29693}
694
[email protected]57ecc4b2010-08-11 03:02:51695void PrefService::RemovePrefObserver(const char* path,
initial.commit09911bf2008-07-26 23:55:29696 NotificationObserver* obs) {
[email protected]d81288a02010-08-18 07:25:50697 pref_notifier_->RemovePrefObserver(path, obs);
initial.commit09911bf2008-07-26 23:55:29698}
699
[email protected]d36f941b2011-05-09 06:19:16700void PrefService::RegisterPreference(const char* path,
701 Value* default_value,
702 PrefSyncStatus sync_status) {
initial.commit09911bf2008-07-26 23:55:29703 DCHECK(CalledOnValidThread());
704
[email protected]c3b54f372010-09-14 08:25:07705 // The main code path takes ownership, but most don't. We'll be safe.
706 scoped_ptr<Value> scoped_value(default_value);
707
708 if (FindPreference(path)) {
709 NOTREACHED() << "Tried to register duplicate pref " << path;
initial.commit09911bf2008-07-26 23:55:29710 return;
711 }
[email protected]c3b54f372010-09-14 08:25:07712
[email protected]bab1c13f2011-08-12 20:59:02713 base::Value::Type orig_type = default_value->GetType();
[email protected]99cc9a02010-09-17 07:53:28714 DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) <<
715 "invalid preference type: " << orig_type;
716
[email protected]9a8c4022011-01-25 14:25:33717 // Hand off ownership.
718 default_store_->SetDefaultValue(path, scoped_value.release());
[email protected]d36f941b2011-05-09 06:19:16719
720 // Register with sync if necessary.
721 if (sync_status == SYNCABLE_PREF && pref_sync_associator_.get())
722 pref_sync_associator_->RegisterPref(path);
initial.commit09911bf2008-07-26 23:55:29723}
724
[email protected]57ecc4b2010-08-11 03:02:51725void PrefService::ClearPref(const char* path) {
initial.commit09911bf2008-07-26 23:55:29726 DCHECK(CalledOnValidThread());
727
728 const Preference* pref = FindPreference(path);
729 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40730 NOTREACHED() << "Trying to clear an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29731 return;
732 }
[email protected]f2d1f612010-12-09 15:10:17733 user_pref_store_->RemoveValue(path);
initial.commit09911bf2008-07-26 23:55:29734}
735
[email protected]57ecc4b2010-08-11 03:02:51736void PrefService::Set(const char* path, const Value& value) {
[email protected]b99c41c2011-04-27 15:18:48737 SetUserPrefValue(path, value.DeepCopy());
[email protected]a048d7e42009-12-01 01:02:39738}
739
[email protected]57ecc4b2010-08-11 03:02:51740void PrefService::SetBoolean(const char* path, bool value) {
[email protected]c3b54f372010-09-14 08:25:07741 SetUserPrefValue(path, Value::CreateBooleanValue(value));
initial.commit09911bf2008-07-26 23:55:29742}
743
[email protected]57ecc4b2010-08-11 03:02:51744void PrefService::SetInteger(const char* path, int value) {
[email protected]c3b54f372010-09-14 08:25:07745 SetUserPrefValue(path, Value::CreateIntegerValue(value));
initial.commit09911bf2008-07-26 23:55:29746}
747
[email protected]fb534c92011-02-01 01:02:07748void PrefService::SetDouble(const char* path, double value) {
749 SetUserPrefValue(path, Value::CreateDoubleValue(value));
initial.commit09911bf2008-07-26 23:55:29750}
751
[email protected]57ecc4b2010-08-11 03:02:51752void PrefService::SetString(const char* path, const std::string& value) {
[email protected]c3b54f372010-09-14 08:25:07753 SetUserPrefValue(path, Value::CreateStringValue(value));
initial.commit09911bf2008-07-26 23:55:29754}
755
[email protected]57ecc4b2010-08-11 03:02:51756void PrefService::SetFilePath(const char* path, const FilePath& value) {
[email protected]8703b2b2011-03-15 09:51:50757 SetUserPrefValue(path, base::CreateFilePathValue(value));
[email protected]b9636002009-03-04 00:05:25758}
759
[email protected]57ecc4b2010-08-11 03:02:51760void PrefService::SetInt64(const char* path, int64 value) {
[email protected]c3b54f372010-09-14 08:25:07761 SetUserPrefValue(path, Value::CreateStringValue(base::Int64ToString(value)));
[email protected]0bb1a622009-03-04 03:22:32762}
763
[email protected]57ecc4b2010-08-11 03:02:51764int64 PrefService::GetInt64(const char* path) const {
[email protected]0bb1a622009-03-04 03:22:32765 DCHECK(CalledOnValidThread());
766
[email protected]0bb1a622009-03-04 03:22:32767 const Preference* pref = FindPreference(path);
768 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40769 NOTREACHED() << "Trying to read an unregistered pref: " << path;
[email protected]c3453302009-12-01 01:33:08770 return 0;
[email protected]0bb1a622009-03-04 03:22:32771 }
[email protected]dc9a6762010-08-16 07:13:53772 std::string result("0");
[email protected]0bb1a622009-03-04 03:22:32773 bool rv = pref->GetValue()->GetAsString(&result);
774 DCHECK(rv);
[email protected]e83326f2010-07-31 17:29:25775
776 int64 val;
[email protected]dc9a6762010-08-16 07:13:53777 base::StringToInt64(result, &val);
[email protected]e83326f2010-07-31 17:29:25778 return val;
[email protected]0bb1a622009-03-04 03:22:32779}
780
[email protected]26418b72011-03-30 14:07:39781Value* PrefService::GetMutableUserPref(const char* path,
[email protected]bab1c13f2011-08-12 20:59:02782 base::Value::Type type) {
[email protected]26418b72011-03-30 14:07:39783 CHECK(type == Value::TYPE_DICTIONARY || type == Value::TYPE_LIST);
initial.commit09911bf2008-07-26 23:55:29784 DCHECK(CalledOnValidThread());
785
786 const Preference* pref = FindPreference(path);
787 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40788 NOTREACHED() << "Trying to get an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29789 return NULL;
790 }
[email protected]26418b72011-03-30 14:07:39791 if (pref->GetType() != type) {
792 NOTREACHED() << "Wrong type for GetMutableValue: " << path;
initial.commit09911bf2008-07-26 23:55:29793 return NULL;
794 }
795
[email protected]e0250892010-10-01 18:57:53796 // Look for an existing preference in the user store. If it doesn't
797 // exist or isn't the correct type, create a new user preference.
[email protected]26418b72011-03-30 14:07:39798 Value* value = NULL;
799 if (user_pref_store_->GetMutableValue(path, &value)
[email protected]f2d1f612010-12-09 15:10:17800 != PersistentPrefStore::READ_OK ||
[email protected]26418b72011-03-30 14:07:39801 !value->IsType(type)) {
802 if (type == Value::TYPE_DICTIONARY) {
803 value = new DictionaryValue;
804 } else if (type == Value::TYPE_LIST) {
805 value = new ListValue;
806 } else {
807 NOTREACHED();
808 }
809 user_pref_store_->SetValueSilently(path, value);
initial.commit09911bf2008-07-26 23:55:29810 }
[email protected]26418b72011-03-30 14:07:39811 return value;
812}
813
[email protected]68bf41a2011-03-25 16:38:31814void PrefService::ReportUserPrefChanged(const std::string& key) {
[email protected]f89ee342011-03-07 09:28:27815 user_pref_store_->ReportValueChanged(key);
816}
817
[email protected]c3b54f372010-09-14 08:25:07818void PrefService::SetUserPrefValue(const char* path, Value* new_value) {
[email protected]b99c41c2011-04-27 15:18:48819 scoped_ptr<Value> owned_value(new_value);
[email protected]c3b54f372010-09-14 08:25:07820 DCHECK(CalledOnValidThread());
821
822 const Preference* pref = FindPreference(path);
823 if (!pref) {
824 NOTREACHED() << "Trying to write an unregistered pref: " << path;
825 return;
826 }
[email protected]99cc9a02010-09-17 07:53:28827 if (pref->GetType() != new_value->GetType()) {
828 NOTREACHED() << "Trying to set pref " << path
829 << " of type " << pref->GetType()
[email protected]c3b54f372010-09-14 08:25:07830 << " to value of type " << new_value->GetType();
831 return;
832 }
833
[email protected]b99c41c2011-04-27 15:18:48834 user_pref_store_->SetValue(path, owned_value.release());
[email protected]73c47932010-12-06 18:13:43835}
836
[email protected]d36f941b2011-05-09 06:19:16837SyncableService* PrefService::GetSyncableService() {
838 return pref_sync_associator_.get();
839}
840
initial.commit09911bf2008-07-26 23:55:29841///////////////////////////////////////////////////////////////////////////////
842// PrefService::Preference
843
[email protected]c3b54f372010-09-14 08:25:07844PrefService::Preference::Preference(const PrefService* service,
[email protected]9a8c4022011-01-25 14:25:33845 const char* name,
[email protected]bab1c13f2011-08-12 20:59:02846 base::Value::Type type)
[email protected]99cc9a02010-09-17 07:53:28847 : name_(name),
[email protected]9a8c4022011-01-25 14:25:33848 type_(type),
[email protected]c3b54f372010-09-14 08:25:07849 pref_service_(service) {
initial.commit09911bf2008-07-26 23:55:29850 DCHECK(name);
[email protected]c3b54f372010-09-14 08:25:07851 DCHECK(service);
[email protected]99cc9a02010-09-17 07:53:28852}
853
[email protected]bab1c13f2011-08-12 20:59:02854base::Value::Type PrefService::Preference::GetType() const {
[email protected]9a8c4022011-01-25 14:25:33855 return type_;
initial.commit09911bf2008-07-26 23:55:29856}
857
858const Value* PrefService::Preference::GetValue() const {
[email protected]c3b54f372010-09-14 08:25:07859 DCHECK(pref_service_->FindPreference(name_.c_str())) <<
initial.commit09911bf2008-07-26 23:55:29860 "Must register pref before getting its value";
861
[email protected]68bf41a2011-03-25 16:38:31862 const Value* found_value = NULL;
[email protected]887288f02011-02-04 22:52:46863 if (pref_value_store()->GetValue(name_, type_, &found_value)) {
[email protected]9a8c4022011-01-25 14:25:33864 DCHECK(found_value->IsType(type_));
[email protected]99cc9a02010-09-17 07:53:28865 return found_value;
[email protected]9a8c4022011-01-25 14:25:33866 }
initial.commit09911bf2008-07-26 23:55:29867
[email protected]c3b54f372010-09-14 08:25:07868 // Every registered preference has at least a default value.
[email protected]99cc9a02010-09-17 07:53:28869 NOTREACHED() << "no valid value found for registered pref " << name_;
[email protected]c3b54f372010-09-14 08:25:07870 return NULL;
[email protected]40a47c162010-09-09 11:14:01871}
872
873bool PrefService::Preference::IsManaged() const {
[email protected]887288f02011-02-04 22:52:46874 return pref_value_store()->PrefValueInManagedStore(name_.c_str());
[email protected]40a47c162010-09-09 11:14:01875}
876
877bool PrefService::Preference::HasExtensionSetting() const {
[email protected]887288f02011-02-04 22:52:46878 return pref_value_store()->PrefValueInExtensionStore(name_.c_str());
[email protected]40a47c162010-09-09 11:14:01879}
880
881bool PrefService::Preference::HasUserSetting() const {
[email protected]887288f02011-02-04 22:52:46882 return pref_value_store()->PrefValueInUserStore(name_.c_str());
[email protected]40a47c162010-09-09 11:14:01883}
884
885bool PrefService::Preference::IsExtensionControlled() const {
[email protected]887288f02011-02-04 22:52:46886 return pref_value_store()->PrefValueFromExtensionStore(name_.c_str());
[email protected]40a47c162010-09-09 11:14:01887}
888
889bool PrefService::Preference::IsUserControlled() const {
[email protected]887288f02011-02-04 22:52:46890 return pref_value_store()->PrefValueFromUserStore(name_.c_str());
[email protected]c3b54f372010-09-14 08:25:07891}
892
893bool PrefService::Preference::IsDefaultValue() const {
[email protected]887288f02011-02-04 22:52:46894 return pref_value_store()->PrefValueFromDefaultStore(name_.c_str());
[email protected]d7449e82010-07-14 11:42:35895}
[email protected]74379bc52010-07-21 13:54:08896
897bool PrefService::Preference::IsUserModifiable() const {
[email protected]887288f02011-02-04 22:52:46898 return pref_value_store()->PrefValueUserModifiable(name_.c_str());
[email protected]74379bc52010-07-21 13:54:08899}
[email protected]9a28f132011-02-24 21:15:16900
901bool PrefService::Preference::IsExtensionModifiable() const {
902 return pref_value_store()->PrefValueExtensionModifiable(name_.c_str());
903}