blob: 89bd511b7ec2e512a16ec1f2a2dbca48b3820fc7 [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]887288f02011-02-04 22:52:46137 ConfigurationPolicyPrefStore* managed_platform =
[email protected]acd78969c2010-12-08 09:49:11138 ConfigurationPolicyPrefStore::CreateManagedPlatformPolicyPrefStore();
[email protected]887288f02011-02-04 22:52:46139 ConfigurationPolicyPrefStore* managed_cloud =
[email protected]fcf53572011-06-29 15:44:37140 ConfigurationPolicyPrefStore::CreateManagedCloudPolicyPrefStore();
[email protected]f31e2e52011-07-14 16:01:19141 ConfigurationPolicyPrefStore* recommended_platform =
142 ConfigurationPolicyPrefStore::CreateRecommendedPlatformPolicyPrefStore();
143 ConfigurationPolicyPrefStore* recommended_cloud =
144 ConfigurationPolicyPrefStore::CreateRecommendedCloudPolicyPrefStore();
145#else
146 ConfigurationPolicyPrefStore* managed_platform = NULL;
147 ConfigurationPolicyPrefStore* managed_cloud = NULL;
148 ConfigurationPolicyPrefStore* recommended_platform = NULL;
149 ConfigurationPolicyPrefStore* recommended_cloud = NULL;
150#endif // ENABLE_CONFIGURATION_POLICY
151
[email protected]acd78969c2010-12-08 09:49:11152 CommandLinePrefStore* command_line =
153 new CommandLinePrefStore(CommandLine::ForCurrentProcess());
154 JsonPrefStore* user = new JsonPrefStore(
155 pref_filename,
156 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE));
[email protected]9a8c4022011-01-25 14:25:33157 DefaultPrefStore* default_pref_store = new DefaultPrefStore();
[email protected]acd78969c2010-12-08 09:49:11158
[email protected]361d37f62011-11-22 10:37:02159 PrefNotifierImpl* pref_notifier = new PrefNotifierImpl();
160 PrefModelAssociator* pref_sync_associator = new PrefModelAssociator();
161
[email protected]845b43a82011-05-11 10:14:43162 return new PrefService(
[email protected]361d37f62011-11-22 10:37:02163 pref_notifier,
164 new PrefValueStore(
165 managed_platform,
166 managed_cloud,
167 extension_prefs,
168 command_line,
169 user,
170 recommended_platform,
171 recommended_cloud,
172 default_pref_store,
173 pref_sync_associator,
174 pref_notifier),
175 user,
176 default_pref_store,
177 pref_sync_associator,
178 async);
[email protected]9a8c4022011-01-25 14:25:33179}
180
181PrefService* PrefService::CreateIncognitoPrefService(
182 PrefStore* incognito_extension_prefs) {
[email protected]361d37f62011-11-22 10:37:02183 PrefNotifierImpl* pref_notifier = new PrefNotifierImpl();
[email protected]32c3c752012-01-05 17:33:47184 OverlayUserPrefStore* incognito_pref_store =
185 new OverlayUserPrefStore(user_pref_store_.get());
186 PrefsTabHelper::InitIncognitoUserPrefStore(incognito_pref_store);
[email protected]361d37f62011-11-22 10:37:02187 return new PrefService(
188 pref_notifier,
189 pref_value_store_->CloneAndSpecialize(
190 NULL, // managed_platform_prefs
191 NULL, // managed_cloud_prefs
192 incognito_extension_prefs,
193 NULL, // command_line_prefs
194 incognito_pref_store,
195 NULL, // recommended_platform_prefs
196 NULL, // recommended_cloud_prefs
197 default_store_.get(),
198 NULL, // pref_sync_associator
199 pref_notifier),
200 incognito_pref_store,
[email protected]9a8c4022011-01-25 14:25:33201 default_store_.get(),
[email protected]361d37f62011-11-22 10:37:02202 NULL,
203 false);
204}
205
206PrefService* PrefService::CreatePrefServiceWithPerTabPrefStore() {
207 PrefNotifierImpl* pref_notifier = new PrefNotifierImpl();
[email protected]32c3c752012-01-05 17:33:47208 OverlayUserPrefStore* per_tab_pref_store =
209 new OverlayUserPrefStore(user_pref_store_.get());
210 PrefsTabHelper::InitPerTabUserPrefStore(per_tab_pref_store);
[email protected]361d37f62011-11-22 10:37:02211 DefaultPrefStore* default_store = new DefaultPrefStore();
212 return new PrefService(
213 pref_notifier,
214 pref_value_store_->CloneAndSpecialize(
215 NULL, // managed_platform_prefs
216 NULL, // managed_cloud_prefs
217 NULL, // extension_prefs
218 NULL, // command_line_prefs
219 per_tab_pref_store,
220 NULL, // recommended_platform_prefs
221 NULL, // recommended_cloud_prefs
222 default_store,
223 NULL,
224 pref_notifier),
225 per_tab_pref_store,
226 default_store,
227 NULL,
228 false);
229}
230
231PrefService::PrefService(PrefNotifierImpl* pref_notifier,
232 PrefValueStore* pref_value_store,
233 PersistentPrefStore* user_prefs,
234 DefaultPrefStore* default_store,
235 PrefModelAssociator* pref_sync_associator,
236 bool async)
237 : pref_notifier_(pref_notifier),
238 pref_value_store_(pref_value_store),
239 user_pref_store_(user_prefs),
240 default_store_(default_store),
241 pref_sync_associator_(pref_sync_associator) {
242 pref_notifier_->SetPrefService(this);
243 if (pref_sync_associator_.get())
244 pref_sync_associator_->SetPrefService(this);
245 InitFromStorage(async);
[email protected]9a8c4022011-01-25 14:25:33246}
247
initial.commit09911bf2008-07-26 23:55:29248PrefService::~PrefService() {
249 DCHECK(CalledOnValidThread());
initial.commit09911bf2008-07-26 23:55:29250 STLDeleteContainerPointers(prefs_.begin(), prefs_.end());
251 prefs_.clear();
[email protected]a98ce1262011-01-28 13:20:23252
253 // Reset pointers so accesses after destruction reliably crash.
254 pref_value_store_.reset();
255 user_pref_store_ = NULL;
256 default_store_ = NULL;
[email protected]d36f941b2011-05-09 06:19:16257 pref_sync_associator_.reset();
initial.commit09911bf2008-07-26 23:55:29258}
259
[email protected]845b43a82011-05-11 10:14:43260void PrefService::InitFromStorage(bool async) {
261 if (!async) {
262 ReadErrorHandler error_handler;
263 error_handler.OnError(user_pref_store_->ReadPrefs());
[email protected]844a1002011-04-19 11:37:21264 } else {
[email protected]845b43a82011-05-11 10:14:43265 // Guarantee that initialization happens after this function returned.
266 MessageLoop::current()->PostTask(
267 FROM_HERE,
[email protected]bebe69432011-09-28 18:36:45268 base::Bind(&PersistentPrefStore::ReadPrefsAsync,
269 user_pref_store_.get(),
270 new ReadErrorHandler()));
[email protected]844a1002011-04-19 11:37:21271 }
[email protected]ba399672010-04-06 15:42:39272}
273
274bool PrefService::ReloadPersistentPrefs() {
[email protected]f2d1f612010-12-09 15:10:17275 return user_pref_store_->ReadPrefs() ==
276 PersistentPrefStore::PREF_READ_ERROR_NONE;
initial.commit09911bf2008-07-26 23:55:29277}
278
[email protected]3826fed2011-03-25 10:59:56279void PrefService::CommitPendingWrite() {
280 DCHECK(CalledOnValidThread());
281 user_pref_store_->CommitPendingWrite();
282}
283
[email protected]d36f941b2011-05-09 06:19:16284namespace {
285
286// If there's no g_browser_process or no local state, return true (for testing).
287bool IsLocalStatePrefService(PrefService* prefs){
288 return (!g_browser_process ||
289 !g_browser_process->local_state() ||
290 g_browser_process->local_state() == prefs);
291}
292
293// If there's no g_browser_process, return true (for testing).
294bool IsProfilePrefService(PrefService* prefs){
295 // TODO(zea): uncomment this once all preferences are only ever registered
296 // with either the local_state's pref service or the profile's pref service.
297 // return (!g_browser_process || g_browser_process->local_state() != prefs);
298 return true;
299}
300
301} // namespace
302
303
304// Local State prefs.
[email protected]57ecc4b2010-08-11 03:02:51305void PrefService::RegisterBooleanPref(const char* path,
initial.commit09911bf2008-07-26 23:55:29306 bool default_value) {
[email protected]d36f941b2011-05-09 06:19:16307 // If this fails, the pref service in use is a profile pref service, so the
308 // sync status must be provided (see profile pref registration calls below).
309 DCHECK(IsLocalStatePrefService(this));
310 RegisterPreference(path,
311 Value::CreateBooleanValue(default_value),
312 UNSYNCABLE_PREF);
initial.commit09911bf2008-07-26 23:55:29313}
314
[email protected]57ecc4b2010-08-11 03:02:51315void PrefService::RegisterIntegerPref(const char* path, int default_value) {
[email protected]d36f941b2011-05-09 06:19:16316 // If this fails, the pref service in use is a profile pref service, so the
317 // sync status must be provided (see profile pref registration calls below).
318 DCHECK(IsLocalStatePrefService(this));
319 RegisterPreference(path,
320 Value::CreateIntegerValue(default_value),
321 UNSYNCABLE_PREF);
initial.commit09911bf2008-07-26 23:55:29322}
323
[email protected]fb534c92011-02-01 01:02:07324void PrefService::RegisterDoublePref(const char* path, double default_value) {
[email protected]d36f941b2011-05-09 06:19:16325 // If this fails, the pref service in use is a profile pref service, so the
326 // sync status must be provided (see profile pref registration calls below).
327 DCHECK(IsLocalStatePrefService(this));
328 RegisterPreference(path,
329 Value::CreateDoubleValue(default_value),
330 UNSYNCABLE_PREF);
initial.commit09911bf2008-07-26 23:55:29331}
332
[email protected]57ecc4b2010-08-11 03:02:51333void PrefService::RegisterStringPref(const char* path,
[email protected]20ce516d2010-06-18 02:20:04334 const std::string& default_value) {
[email protected]d36f941b2011-05-09 06:19:16335 // If this fails, the pref service in use is a profile pref service, so the
336 // sync status must be provided (see profile pref registration calls below).
337 DCHECK(IsLocalStatePrefService(this));
338 RegisterPreference(path,
339 Value::CreateStringValue(default_value),
340 UNSYNCABLE_PREF);
initial.commit09911bf2008-07-26 23:55:29341}
342
[email protected]57ecc4b2010-08-11 03:02:51343void PrefService::RegisterFilePathPref(const char* path,
[email protected]b9636002009-03-04 00:05:25344 const FilePath& default_value) {
[email protected]d36f941b2011-05-09 06:19:16345 // If this fails, the pref service in use is a profile pref service, so the
346 // sync status must be provided (see profile pref registration calls below).
347 DCHECK(IsLocalStatePrefService(this));
348 RegisterPreference(path,
349 Value::CreateStringValue(default_value.value()),
350 UNSYNCABLE_PREF);
[email protected]b9636002009-03-04 00:05:25351}
352
[email protected]57ecc4b2010-08-11 03:02:51353void PrefService::RegisterListPref(const char* path) {
[email protected]d36f941b2011-05-09 06:19:16354 // If this fails, the pref service in use is a profile pref service, so the
355 // sync status must be provided (see profile pref registration calls below).
356 DCHECK(IsLocalStatePrefService(this));
357 RegisterPreference(path,
358 new ListValue(),
359 UNSYNCABLE_PREF);
initial.commit09911bf2008-07-26 23:55:29360}
361
[email protected]c2f23d012011-02-09 14:52:17362void PrefService::RegisterListPref(const char* path, ListValue* default_value) {
[email protected]d36f941b2011-05-09 06:19:16363 // If this fails, the pref service in use is a profile pref service, so the
364 // sync status must be provided (see profile pref registration calls below).
365 DCHECK(IsLocalStatePrefService(this));
366 RegisterPreference(path,
367 default_value,
368 UNSYNCABLE_PREF);
[email protected]c2f23d012011-02-09 14:52:17369}
370
[email protected]57ecc4b2010-08-11 03:02:51371void PrefService::RegisterDictionaryPref(const char* path) {
[email protected]d36f941b2011-05-09 06:19:16372 // If this fails, the pref service in use is a profile pref service, so the
373 // sync status must be provided (see profile pref registration calls below).
374 DCHECK(IsLocalStatePrefService(this));
375 RegisterPreference(path,
376 new DictionaryValue(),
377 UNSYNCABLE_PREF);
initial.commit09911bf2008-07-26 23:55:29378}
379
[email protected]c2f23d012011-02-09 14:52:17380void PrefService::RegisterDictionaryPref(const char* path,
381 DictionaryValue* default_value) {
[email protected]d36f941b2011-05-09 06:19:16382 // If this fails, the pref service in use is a profile pref service, so the
383 // sync status must be provided (see profile pref registration calls below).
384 DCHECK(IsLocalStatePrefService(this));
385 RegisterPreference(path,
386 default_value,
387 UNSYNCABLE_PREF);
[email protected]c2f23d012011-02-09 14:52:17388}
389
[email protected]57ecc4b2010-08-11 03:02:51390void PrefService::RegisterLocalizedBooleanPref(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_BOOLEAN, locale_default_message_id),
398 UNSYNCABLE_PREF);
initial.commit09911bf2008-07-26 23:55:29399}
400
[email protected]57ecc4b2010-08-11 03:02:51401void PrefService::RegisterLocalizedIntegerPref(const char* path,
initial.commit09911bf2008-07-26 23:55:29402 int locale_default_message_id) {
[email protected]d36f941b2011-05-09 06:19:16403 // If this fails, the pref service in use is a profile pref service, so the
404 // sync status must be provided (see profile pref registration calls below).
405 DCHECK(IsLocalStatePrefService(this));
[email protected]c3b54f372010-09-14 08:25:07406 RegisterPreference(
407 path,
[email protected]d36f941b2011-05-09 06:19:16408 CreateLocaleDefaultValue(Value::TYPE_INTEGER, locale_default_message_id),
409 UNSYNCABLE_PREF);
initial.commit09911bf2008-07-26 23:55:29410}
411
[email protected]fb534c92011-02-01 01:02:07412void PrefService::RegisterLocalizedDoublePref(const char* path,
413 int locale_default_message_id) {
[email protected]d36f941b2011-05-09 06:19:16414 // If this fails, the pref service in use is a profile pref service, so the
415 // sync status must be provided (see profile pref registration calls below).
416 DCHECK(IsLocalStatePrefService(this));
[email protected]c3b54f372010-09-14 08:25:07417 RegisterPreference(
418 path,
[email protected]d36f941b2011-05-09 06:19:16419 CreateLocaleDefaultValue(Value::TYPE_DOUBLE, locale_default_message_id),
420 UNSYNCABLE_PREF);
initial.commit09911bf2008-07-26 23:55:29421}
422
[email protected]57ecc4b2010-08-11 03:02:51423void PrefService::RegisterLocalizedStringPref(const char* path,
initial.commit09911bf2008-07-26 23:55:29424 int locale_default_message_id) {
[email protected]d36f941b2011-05-09 06:19:16425 // If this fails, the pref service in use is a profile pref service, so the
426 // sync status must be provided (see profile pref registration calls below).
427 DCHECK(IsLocalStatePrefService(this));
[email protected]c3b54f372010-09-14 08:25:07428 RegisterPreference(
429 path,
[email protected]d36f941b2011-05-09 06:19:16430 CreateLocaleDefaultValue(Value::TYPE_STRING, locale_default_message_id),
431 UNSYNCABLE_PREF);
432}
433
434void PrefService::RegisterInt64Pref(const char* path, int64 default_value) {
435 // If this fails, the pref service in use is a profile pref service, so the
436 // sync status must be provided (see profile pref registration calls below).
437 DCHECK(IsLocalStatePrefService(this));
438 RegisterPreference(
439 path,
440 Value::CreateStringValue(base::Int64ToString(default_value)),
441 UNSYNCABLE_PREF);
442}
443
444// Profile prefs (must use the sync_status variable).
445void PrefService::RegisterBooleanPref(const char* path,
446 bool default_value,
447 PrefSyncStatus sync_status) {
448 DCHECK(IsProfilePrefService(this));
449 RegisterPreference(path,
450 Value::CreateBooleanValue(default_value),
451 sync_status);
452}
453
454void PrefService::RegisterIntegerPref(const char* path,
455 int default_value,
456 PrefSyncStatus sync_status) {
457 DCHECK(IsProfilePrefService(this));
458 RegisterPreference(path,
459 Value::CreateIntegerValue(default_value),
460 sync_status);
461}
462
463void PrefService::RegisterDoublePref(const char* path,
464 double default_value,
465 PrefSyncStatus sync_status) {
466 DCHECK(IsProfilePrefService(this));
467 RegisterPreference(path,
468 Value::CreateDoubleValue(default_value),
469 sync_status);
470}
471
472void PrefService::RegisterStringPref(const char* path,
473 const std::string& default_value,
474 PrefSyncStatus sync_status) {
475 DCHECK(IsProfilePrefService(this));
476 RegisterPreference(path,
477 Value::CreateStringValue(default_value),
478 sync_status);
479}
480
481void PrefService::RegisterFilePathPref(const char* path,
482 const FilePath& default_value,
483 PrefSyncStatus sync_status) {
484 DCHECK(IsProfilePrefService(this));
485 RegisterPreference(path,
486 Value::CreateStringValue(default_value.value()),
487 sync_status);
488}
489
490void PrefService::RegisterListPref(const char* path,
491 PrefSyncStatus sync_status) {
492 DCHECK(IsProfilePrefService(this));
493 RegisterPreference(path, new ListValue(), sync_status);
494}
495
496void PrefService::RegisterListPref(const char* path,
497 ListValue* default_value,
498 PrefSyncStatus sync_status) {
499 DCHECK(IsProfilePrefService(this));
500 RegisterPreference(path, default_value, sync_status);
501}
502
503void PrefService::RegisterDictionaryPref(const char* path,
504 PrefSyncStatus sync_status) {
505 DCHECK(IsProfilePrefService(this));
506 RegisterPreference(path, new DictionaryValue(), sync_status);
507}
508
509void PrefService::RegisterDictionaryPref(const char* path,
510 DictionaryValue* default_value,
511 PrefSyncStatus sync_status) {
512 DCHECK(IsProfilePrefService(this));
513 RegisterPreference(path, default_value, sync_status);
514}
515
516void PrefService::RegisterLocalizedBooleanPref(const char* path,
517 int locale_default_message_id,
518 PrefSyncStatus sync_status) {
519 DCHECK(IsProfilePrefService(this));
520 RegisterPreference(
521 path,
522 CreateLocaleDefaultValue(Value::TYPE_BOOLEAN,locale_default_message_id),
523 sync_status);
524}
525
526void PrefService::RegisterLocalizedIntegerPref(const char* path,
527 int locale_default_message_id,
528 PrefSyncStatus sync_status) {
529 DCHECK(IsProfilePrefService(this));
530 RegisterPreference(
531 path,
532 CreateLocaleDefaultValue(Value::TYPE_INTEGER, locale_default_message_id),
533 sync_status);
534}
535
536void PrefService::RegisterLocalizedDoublePref(const char* path,
537 int locale_default_message_id,
538 PrefSyncStatus sync_status) {
539 DCHECK(IsProfilePrefService(this));
540 RegisterPreference(
541 path,
542 CreateLocaleDefaultValue(Value::TYPE_DOUBLE, locale_default_message_id),
543 sync_status);
544}
545
546void PrefService::RegisterLocalizedStringPref(const char* path,
547 int locale_default_message_id,
548 PrefSyncStatus sync_status) {
549 DCHECK(IsProfilePrefService(this));
550 RegisterPreference(
551 path,
552 CreateLocaleDefaultValue(Value::TYPE_STRING, locale_default_message_id),
553 sync_status);
554}
555
556void PrefService::RegisterInt64Pref(const char* path,
557 int64 default_value,
558 PrefSyncStatus sync_status) {
559 DCHECK(IsProfilePrefService(this));
560 RegisterPreference(
561 path,
562 Value::CreateStringValue(base::Int64ToString(default_value)),
563 sync_status);
initial.commit09911bf2008-07-26 23:55:29564}
565
[email protected]57ecc4b2010-08-11 03:02:51566bool PrefService::GetBoolean(const char* path) const {
initial.commit09911bf2008-07-26 23:55:29567 DCHECK(CalledOnValidThread());
568
569 bool result = false;
initial.commit09911bf2008-07-26 23:55:29570
571 const Preference* pref = FindPreference(path);
572 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40573 NOTREACHED() << "Trying to read an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29574 return result;
575 }
576 bool rv = pref->GetValue()->GetAsBoolean(&result);
577 DCHECK(rv);
578 return result;
579}
580
[email protected]57ecc4b2010-08-11 03:02:51581int PrefService::GetInteger(const char* path) const {
initial.commit09911bf2008-07-26 23:55:29582 DCHECK(CalledOnValidThread());
583
584 int result = 0;
initial.commit09911bf2008-07-26 23:55:29585
586 const Preference* pref = FindPreference(path);
587 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40588 NOTREACHED() << "Trying to read an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29589 return result;
590 }
591 bool rv = pref->GetValue()->GetAsInteger(&result);
592 DCHECK(rv);
593 return result;
594}
595
[email protected]fb534c92011-02-01 01:02:07596double PrefService::GetDouble(const char* path) const {
initial.commit09911bf2008-07-26 23:55:29597 DCHECK(CalledOnValidThread());
598
599 double result = 0.0;
initial.commit09911bf2008-07-26 23:55:29600
601 const Preference* pref = FindPreference(path);
602 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40603 NOTREACHED() << "Trying to read an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29604 return result;
605 }
[email protected]fb534c92011-02-01 01:02:07606 bool rv = pref->GetValue()->GetAsDouble(&result);
initial.commit09911bf2008-07-26 23:55:29607 DCHECK(rv);
608 return result;
609}
610
[email protected]57ecc4b2010-08-11 03:02:51611std::string PrefService::GetString(const char* path) const {
initial.commit09911bf2008-07-26 23:55:29612 DCHECK(CalledOnValidThread());
613
[email protected]ddd231e2010-06-29 20:35:19614 std::string result;
[email protected]8e50b602009-03-03 22:59:43615
initial.commit09911bf2008-07-26 23:55:29616 const Preference* pref = FindPreference(path);
617 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40618 NOTREACHED() << "Trying to read an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29619 return result;
620 }
621 bool rv = pref->GetValue()->GetAsString(&result);
622 DCHECK(rv);
623 return result;
624}
625
[email protected]57ecc4b2010-08-11 03:02:51626FilePath PrefService::GetFilePath(const char* path) const {
[email protected]b9636002009-03-04 00:05:25627 DCHECK(CalledOnValidThread());
628
[email protected]68d9d352011-02-21 16:35:04629 FilePath result;
[email protected]b9636002009-03-04 00:05:25630
631 const Preference* pref = FindPreference(path);
632 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40633 NOTREACHED() << "Trying to read an unregistered pref: " << path;
[email protected]b9636002009-03-04 00:05:25634 return FilePath(result);
635 }
[email protected]8703b2b2011-03-15 09:51:50636 bool rv = base::GetValueAsFilePath(*pref->GetValue(), &result);
[email protected]b9636002009-03-04 00:05:25637 DCHECK(rv);
[email protected]68d9d352011-02-21 16:35:04638 return result;
[email protected]b9636002009-03-04 00:05:25639}
640
[email protected]57ecc4b2010-08-11 03:02:51641bool PrefService::HasPrefPath(const char* path) const {
[email protected]9a8c4022011-01-25 14:25:33642 const Preference* pref = FindPreference(path);
643 return pref && !pref->IsDefaultValue();
initial.commit09911bf2008-07-26 23:55:29644}
645
[email protected]ebd0b022011-01-27 13:24:14646DictionaryValue* PrefService::GetPreferenceValues() const {
647 DCHECK(CalledOnValidThread());
648 DictionaryValue* out = new DictionaryValue;
649 DefaultPrefStore::const_iterator i = default_store_->begin();
650 for (; i != default_store_->end(); ++i) {
[email protected]8874e02172011-03-11 19:44:08651 const Preference* pref = FindPreference(i->first.c_str());
652 DCHECK(pref);
653 const Value* value = pref->GetValue();
654 DCHECK(value);
[email protected]ebd0b022011-01-27 13:24:14655 out->Set(i->first, value->DeepCopy());
656 }
657 return out;
658}
659
initial.commit09911bf2008-07-26 23:55:29660const PrefService::Preference* PrefService::FindPreference(
[email protected]57ecc4b2010-08-11 03:02:51661 const char* pref_name) const {
initial.commit09911bf2008-07-26 23:55:29662 DCHECK(CalledOnValidThread());
[email protected]9a8c4022011-01-25 14:25:33663 Preference p(this, pref_name, Value::TYPE_NULL);
initial.commit09911bf2008-07-26 23:55:29664 PreferenceSet::const_iterator it = prefs_.find(&p);
[email protected]9a8c4022011-01-25 14:25:33665 if (it != prefs_.end())
666 return *it;
[email protected]bab1c13f2011-08-12 20:59:02667 const base::Value::Type type = default_store_->GetType(pref_name);
[email protected]9a8c4022011-01-25 14:25:33668 if (type == Value::TYPE_NULL)
669 return NULL;
670 Preference* new_pref = new Preference(this, pref_name, type);
671 prefs_.insert(new_pref);
672 return new_pref;
initial.commit09911bf2008-07-26 23:55:29673}
674
[email protected]acd78969c2010-12-08 09:49:11675bool PrefService::ReadOnly() const {
[email protected]f2d1f612010-12-09 15:10:17676 return user_pref_store_->ReadOnly();
[email protected]acd78969c2010-12-08 09:49:11677}
678
[email protected]57ecc4b2010-08-11 03:02:51679bool PrefService::IsManagedPreference(const char* pref_name) const {
[email protected]d90de1c02010-07-19 19:50:48680 const Preference* pref = FindPreference(pref_name);
[email protected]9a8c4022011-01-25 14:25:33681 return pref && pref->IsManaged();
[email protected]d90de1c02010-07-19 19:50:48682}
683
[email protected]57ecc4b2010-08-11 03:02:51684const DictionaryValue* PrefService::GetDictionary(const char* path) const {
initial.commit09911bf2008-07-26 23:55:29685 DCHECK(CalledOnValidThread());
686
initial.commit09911bf2008-07-26 23:55:29687 const Preference* pref = FindPreference(path);
688 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40689 NOTREACHED() << "Trying to read an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29690 return NULL;
691 }
692 const Value* value = pref->GetValue();
[email protected]11b040b2011-02-02 12:42:25693 if (value->GetType() != Value::TYPE_DICTIONARY) {
694 NOTREACHED();
initial.commit09911bf2008-07-26 23:55:29695 return NULL;
[email protected]11b040b2011-02-02 12:42:25696 }
initial.commit09911bf2008-07-26 23:55:29697 return static_cast<const DictionaryValue*>(value);
698}
699
[email protected]57ecc4b2010-08-11 03:02:51700const ListValue* PrefService::GetList(const char* path) const {
initial.commit09911bf2008-07-26 23:55:29701 DCHECK(CalledOnValidThread());
702
initial.commit09911bf2008-07-26 23:55:29703 const Preference* pref = FindPreference(path);
704 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40705 NOTREACHED() << "Trying to read an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29706 return NULL;
707 }
708 const Value* value = pref->GetValue();
[email protected]11b040b2011-02-02 12:42:25709 if (value->GetType() != Value::TYPE_LIST) {
710 NOTREACHED();
initial.commit09911bf2008-07-26 23:55:29711 return NULL;
[email protected]11b040b2011-02-02 12:42:25712 }
initial.commit09911bf2008-07-26 23:55:29713 return static_cast<const ListValue*>(value);
714}
715
[email protected]57ecc4b2010-08-11 03:02:51716void PrefService::AddPrefObserver(const char* path,
[email protected]6c2381d2011-10-19 02:52:53717 content::NotificationObserver* obs) {
[email protected]d81288a02010-08-18 07:25:50718 pref_notifier_->AddPrefObserver(path, obs);
initial.commit09911bf2008-07-26 23:55:29719}
720
[email protected]57ecc4b2010-08-11 03:02:51721void PrefService::RemovePrefObserver(const char* path,
[email protected]6c2381d2011-10-19 02:52:53722 content::NotificationObserver* obs) {
[email protected]d81288a02010-08-18 07:25:50723 pref_notifier_->RemovePrefObserver(path, obs);
initial.commit09911bf2008-07-26 23:55:29724}
725
[email protected]d36f941b2011-05-09 06:19:16726void PrefService::RegisterPreference(const char* path,
727 Value* default_value,
728 PrefSyncStatus sync_status) {
initial.commit09911bf2008-07-26 23:55:29729 DCHECK(CalledOnValidThread());
730
[email protected]c3b54f372010-09-14 08:25:07731 // The main code path takes ownership, but most don't. We'll be safe.
732 scoped_ptr<Value> scoped_value(default_value);
733
734 if (FindPreference(path)) {
735 NOTREACHED() << "Tried to register duplicate pref " << path;
initial.commit09911bf2008-07-26 23:55:29736 return;
737 }
[email protected]c3b54f372010-09-14 08:25:07738
[email protected]bab1c13f2011-08-12 20:59:02739 base::Value::Type orig_type = default_value->GetType();
[email protected]99cc9a02010-09-17 07:53:28740 DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) <<
741 "invalid preference type: " << orig_type;
742
[email protected]9a8c4022011-01-25 14:25:33743 // Hand off ownership.
744 default_store_->SetDefaultValue(path, scoped_value.release());
[email protected]d36f941b2011-05-09 06:19:16745
746 // Register with sync if necessary.
747 if (sync_status == SYNCABLE_PREF && pref_sync_associator_.get())
748 pref_sync_associator_->RegisterPref(path);
initial.commit09911bf2008-07-26 23:55:29749}
750
[email protected]7a5f5932011-12-29 10:35:49751void PrefService::UnregisterPreference(const char* path) {
752 DCHECK(CalledOnValidThread());
753
754 Preference p(this, path, Value::TYPE_NULL);
755 PreferenceSet::const_iterator it = prefs_.find(&p);
756 if (it == prefs_.end()) {
757 NOTREACHED() << "Trying to unregister an unregistered pref: " << path;
758 return;
759 }
760
[email protected]5a56d372011-12-29 11:34:40761 delete *it;
[email protected]7a5f5932011-12-29 10:35:49762 prefs_.erase(it);
763 default_store_->RemoveDefaultValue(path);
764 if (pref_sync_associator_.get() &&
765 pref_sync_associator_->IsPrefRegistered(path)) {
766 pref_sync_associator_->UnregisterPref(path);
767 }
768}
769
[email protected]57ecc4b2010-08-11 03:02:51770void PrefService::ClearPref(const char* path) {
initial.commit09911bf2008-07-26 23:55:29771 DCHECK(CalledOnValidThread());
772
773 const Preference* pref = FindPreference(path);
774 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40775 NOTREACHED() << "Trying to clear an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29776 return;
777 }
[email protected]f2d1f612010-12-09 15:10:17778 user_pref_store_->RemoveValue(path);
initial.commit09911bf2008-07-26 23:55:29779}
780
[email protected]57ecc4b2010-08-11 03:02:51781void PrefService::Set(const char* path, const Value& value) {
[email protected]b99c41c2011-04-27 15:18:48782 SetUserPrefValue(path, value.DeepCopy());
[email protected]a048d7e42009-12-01 01:02:39783}
784
[email protected]57ecc4b2010-08-11 03:02:51785void PrefService::SetBoolean(const char* path, bool value) {
[email protected]c3b54f372010-09-14 08:25:07786 SetUserPrefValue(path, Value::CreateBooleanValue(value));
initial.commit09911bf2008-07-26 23:55:29787}
788
[email protected]57ecc4b2010-08-11 03:02:51789void PrefService::SetInteger(const char* path, int value) {
[email protected]c3b54f372010-09-14 08:25:07790 SetUserPrefValue(path, Value::CreateIntegerValue(value));
initial.commit09911bf2008-07-26 23:55:29791}
792
[email protected]fb534c92011-02-01 01:02:07793void PrefService::SetDouble(const char* path, double value) {
794 SetUserPrefValue(path, Value::CreateDoubleValue(value));
initial.commit09911bf2008-07-26 23:55:29795}
796
[email protected]57ecc4b2010-08-11 03:02:51797void PrefService::SetString(const char* path, const std::string& value) {
[email protected]c3b54f372010-09-14 08:25:07798 SetUserPrefValue(path, Value::CreateStringValue(value));
initial.commit09911bf2008-07-26 23:55:29799}
800
[email protected]57ecc4b2010-08-11 03:02:51801void PrefService::SetFilePath(const char* path, const FilePath& value) {
[email protected]8703b2b2011-03-15 09:51:50802 SetUserPrefValue(path, base::CreateFilePathValue(value));
[email protected]b9636002009-03-04 00:05:25803}
804
[email protected]57ecc4b2010-08-11 03:02:51805void PrefService::SetInt64(const char* path, int64 value) {
[email protected]c3b54f372010-09-14 08:25:07806 SetUserPrefValue(path, Value::CreateStringValue(base::Int64ToString(value)));
[email protected]0bb1a622009-03-04 03:22:32807}
808
[email protected]57ecc4b2010-08-11 03:02:51809int64 PrefService::GetInt64(const char* path) const {
[email protected]0bb1a622009-03-04 03:22:32810 DCHECK(CalledOnValidThread());
811
[email protected]0bb1a622009-03-04 03:22:32812 const Preference* pref = FindPreference(path);
813 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40814 NOTREACHED() << "Trying to read an unregistered pref: " << path;
[email protected]c3453302009-12-01 01:33:08815 return 0;
[email protected]0bb1a622009-03-04 03:22:32816 }
[email protected]dc9a6762010-08-16 07:13:53817 std::string result("0");
[email protected]0bb1a622009-03-04 03:22:32818 bool rv = pref->GetValue()->GetAsString(&result);
819 DCHECK(rv);
[email protected]e83326f2010-07-31 17:29:25820
821 int64 val;
[email protected]dc9a6762010-08-16 07:13:53822 base::StringToInt64(result, &val);
[email protected]e83326f2010-07-31 17:29:25823 return val;
[email protected]0bb1a622009-03-04 03:22:32824}
825
[email protected]26418b72011-03-30 14:07:39826Value* PrefService::GetMutableUserPref(const char* path,
[email protected]bab1c13f2011-08-12 20:59:02827 base::Value::Type type) {
[email protected]26418b72011-03-30 14:07:39828 CHECK(type == Value::TYPE_DICTIONARY || type == Value::TYPE_LIST);
initial.commit09911bf2008-07-26 23:55:29829 DCHECK(CalledOnValidThread());
830
831 const Preference* pref = FindPreference(path);
832 if (!pref) {
[email protected]b154e6f2009-03-06 01:52:40833 NOTREACHED() << "Trying to get an unregistered pref: " << path;
initial.commit09911bf2008-07-26 23:55:29834 return NULL;
835 }
[email protected]26418b72011-03-30 14:07:39836 if (pref->GetType() != type) {
837 NOTREACHED() << "Wrong type for GetMutableValue: " << path;
initial.commit09911bf2008-07-26 23:55:29838 return NULL;
839 }
840
[email protected]e0250892010-10-01 18:57:53841 // Look for an existing preference in the user store. If it doesn't
842 // exist or isn't the correct type, create a new user preference.
[email protected]26418b72011-03-30 14:07:39843 Value* value = NULL;
844 if (user_pref_store_->GetMutableValue(path, &value)
[email protected]f2d1f612010-12-09 15:10:17845 != PersistentPrefStore::READ_OK ||
[email protected]26418b72011-03-30 14:07:39846 !value->IsType(type)) {
847 if (type == Value::TYPE_DICTIONARY) {
848 value = new DictionaryValue;
849 } else if (type == Value::TYPE_LIST) {
850 value = new ListValue;
851 } else {
852 NOTREACHED();
853 }
854 user_pref_store_->SetValueSilently(path, value);
initial.commit09911bf2008-07-26 23:55:29855 }
[email protected]26418b72011-03-30 14:07:39856 return value;
857}
858
[email protected]68bf41a2011-03-25 16:38:31859void PrefService::ReportUserPrefChanged(const std::string& key) {
[email protected]f89ee342011-03-07 09:28:27860 user_pref_store_->ReportValueChanged(key);
861}
862
[email protected]c3b54f372010-09-14 08:25:07863void PrefService::SetUserPrefValue(const char* path, Value* new_value) {
[email protected]b99c41c2011-04-27 15:18:48864 scoped_ptr<Value> owned_value(new_value);
[email protected]c3b54f372010-09-14 08:25:07865 DCHECK(CalledOnValidThread());
866
867 const Preference* pref = FindPreference(path);
868 if (!pref) {
869 NOTREACHED() << "Trying to write an unregistered pref: " << path;
870 return;
871 }
[email protected]99cc9a02010-09-17 07:53:28872 if (pref->GetType() != new_value->GetType()) {
873 NOTREACHED() << "Trying to set pref " << path
874 << " of type " << pref->GetType()
[email protected]c3b54f372010-09-14 08:25:07875 << " to value of type " << new_value->GetType();
876 return;
877 }
878
[email protected]b99c41c2011-04-27 15:18:48879 user_pref_store_->SetValue(path, owned_value.release());
[email protected]73c47932010-12-06 18:13:43880}
881
[email protected]d36f941b2011-05-09 06:19:16882SyncableService* PrefService::GetSyncableService() {
883 return pref_sync_associator_.get();
884}
885
initial.commit09911bf2008-07-26 23:55:29886///////////////////////////////////////////////////////////////////////////////
887// PrefService::Preference
888
[email protected]c3b54f372010-09-14 08:25:07889PrefService::Preference::Preference(const PrefService* service,
[email protected]9a8c4022011-01-25 14:25:33890 const char* name,
[email protected]bab1c13f2011-08-12 20:59:02891 base::Value::Type type)
[email protected]99cc9a02010-09-17 07:53:28892 : name_(name),
[email protected]9a8c4022011-01-25 14:25:33893 type_(type),
[email protected]c3b54f372010-09-14 08:25:07894 pref_service_(service) {
initial.commit09911bf2008-07-26 23:55:29895 DCHECK(name);
[email protected]c3b54f372010-09-14 08:25:07896 DCHECK(service);
[email protected]99cc9a02010-09-17 07:53:28897}
898
[email protected]bab1c13f2011-08-12 20:59:02899base::Value::Type PrefService::Preference::GetType() const {
[email protected]9a8c4022011-01-25 14:25:33900 return type_;
initial.commit09911bf2008-07-26 23:55:29901}
902
903const Value* PrefService::Preference::GetValue() const {
[email protected]c3b54f372010-09-14 08:25:07904 DCHECK(pref_service_->FindPreference(name_.c_str())) <<
initial.commit09911bf2008-07-26 23:55:29905 "Must register pref before getting its value";
906
[email protected]68bf41a2011-03-25 16:38:31907 const Value* found_value = NULL;
[email protected]887288f02011-02-04 22:52:46908 if (pref_value_store()->GetValue(name_, type_, &found_value)) {
[email protected]9a8c4022011-01-25 14:25:33909 DCHECK(found_value->IsType(type_));
[email protected]99cc9a02010-09-17 07:53:28910 return found_value;
[email protected]9a8c4022011-01-25 14:25:33911 }
initial.commit09911bf2008-07-26 23:55:29912
[email protected]c3b54f372010-09-14 08:25:07913 // Every registered preference has at least a default value.
[email protected]99cc9a02010-09-17 07:53:28914 NOTREACHED() << "no valid value found for registered pref " << name_;
[email protected]c3b54f372010-09-14 08:25:07915 return NULL;
[email protected]40a47c162010-09-09 11:14:01916}
917
918bool PrefService::Preference::IsManaged() const {
[email protected]887288f02011-02-04 22:52:46919 return pref_value_store()->PrefValueInManagedStore(name_.c_str());
[email protected]40a47c162010-09-09 11:14:01920}
921
[email protected]a5437282011-12-12 12:33:21922bool PrefService::Preference::IsRecommended() const {
923 return pref_value_store()->PrefValueFromRecommendedStore(name_.c_str());
924}
925
[email protected]40a47c162010-09-09 11:14:01926bool PrefService::Preference::HasExtensionSetting() const {
[email protected]887288f02011-02-04 22:52:46927 return pref_value_store()->PrefValueInExtensionStore(name_.c_str());
[email protected]40a47c162010-09-09 11:14:01928}
929
930bool PrefService::Preference::HasUserSetting() const {
[email protected]887288f02011-02-04 22:52:46931 return pref_value_store()->PrefValueInUserStore(name_.c_str());
[email protected]40a47c162010-09-09 11:14:01932}
933
934bool PrefService::Preference::IsExtensionControlled() const {
[email protected]887288f02011-02-04 22:52:46935 return pref_value_store()->PrefValueFromExtensionStore(name_.c_str());
[email protected]40a47c162010-09-09 11:14:01936}
937
938bool PrefService::Preference::IsUserControlled() const {
[email protected]887288f02011-02-04 22:52:46939 return pref_value_store()->PrefValueFromUserStore(name_.c_str());
[email protected]c3b54f372010-09-14 08:25:07940}
941
942bool PrefService::Preference::IsDefaultValue() const {
[email protected]887288f02011-02-04 22:52:46943 return pref_value_store()->PrefValueFromDefaultStore(name_.c_str());
[email protected]d7449e82010-07-14 11:42:35944}
[email protected]74379bc52010-07-21 13:54:08945
946bool PrefService::Preference::IsUserModifiable() const {
[email protected]887288f02011-02-04 22:52:46947 return pref_value_store()->PrefValueUserModifiable(name_.c_str());
[email protected]74379bc52010-07-21 13:54:08948}
[email protected]9a28f132011-02-24 21:15:16949
950bool PrefService::Preference::IsExtensionModifiable() const {
951 return pref_value_store()->PrefValueExtensionModifiable(name_.c_str());
952}