[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 37858e5 | 2010-08-26 00:22:02 | [diff] [blame] | 5 | #include "chrome/browser/prefs/pref_service.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
[email protected] | 1cb92b8 | 2010-03-08 23:12:15 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | #include <string> |
| 9 | |
[email protected] | aa4dc5e2 | 2010-06-16 11:32:54 | [diff] [blame] | 10 | #include "base/command_line.h" |
[email protected] | c02c853d7 | 2010-08-07 06:23:24 | [diff] [blame] | 11 | #include "base/file_path.h" |
[email protected] | 1d01d41 | 2010-08-20 00:36:01 | [diff] [blame] | 12 | #include "base/file_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 13 | #include "base/logging.h" |
| 14 | #include "base/message_loop.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 15 | #include "base/metrics/histogram.h" |
[email protected] | 80720414 | 2009-05-05 03:31:44 | [diff] [blame] | 16 | #include "base/stl_util-inl.h" |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 17 | #include "base/string_number_conversions.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 18 | #include "base/string_util.h" |
[email protected] | 66de4f09 | 2009-09-04 23:59:40 | [diff] [blame] | 19 | #include "base/sys_string_conversions.h" |
[email protected] | 1cb92b8 | 2010-03-08 23:12:15 | [diff] [blame] | 20 | #include "base/utf_string_conversions.h" |
[email protected] | 66de4f09 | 2009-09-04 23:59:40 | [diff] [blame] | 21 | #include "build/build_config.h" |
[email protected] | 017a7a11 | 2010-10-12 16:38:27 | [diff] [blame] | 22 | #include "chrome/browser/browser_thread.h" |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 23 | #include "chrome/browser/extensions/extension_pref_store.h" |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 24 | #include "chrome/browser/policy/configuration_policy_pref_store.h" |
| 25 | #include "chrome/browser/prefs/command_line_pref_store.h" |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 26 | #include "chrome/browser/prefs/default_pref_store.h" |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 27 | #include "chrome/browser/prefs/overlay_persistent_pref_store.h" |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 28 | #include "chrome/browser/prefs/pref_notifier_impl.h" |
[email protected] | 39d9f62c | 2010-12-03 10:48:50 | [diff] [blame] | 29 | #include "chrome/browser/prefs/pref_value_store.h" |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 30 | #include "chrome/common/json_pref_store.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 31 | #include "chrome/common/notification_service.h" |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 32 | #include "grit/chromium_strings.h" |
[email protected] | 34ac8f3 | 2009-02-22 23:03:27 | [diff] [blame] | 33 | #include "grit/generated_resources.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 34 | #include "ui/base/l10n/l10n_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 35 | |
| 36 | namespace { |
| 37 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 38 | // A helper function for RegisterLocalized*Pref that creates a Value* based on |
| 39 | // the string value in the locale dll. Because we control the values in a |
| 40 | // locale dll, this should always return a Value of the appropriate type. |
| 41 | Value* CreateLocaleDefaultValue(Value::ValueType type, int message_id) { |
[email protected] | 16b52716 | 2010-08-15 18:37:10 | [diff] [blame] | 42 | std::string resource_string = l10n_util::GetStringUTF8(message_id); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 43 | DCHECK(!resource_string.empty()); |
| 44 | switch (type) { |
| 45 | case Value::TYPE_BOOLEAN: { |
[email protected] | 16b52716 | 2010-08-15 18:37:10 | [diff] [blame] | 46 | if ("true" == resource_string) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 47 | return Value::CreateBooleanValue(true); |
[email protected] | 16b52716 | 2010-08-15 18:37:10 | [diff] [blame] | 48 | if ("false" == resource_string) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 49 | return Value::CreateBooleanValue(false); |
| 50 | break; |
| 51 | } |
| 52 | |
| 53 | case Value::TYPE_INTEGER: { |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 54 | int val; |
[email protected] | 16b52716 | 2010-08-15 18:37:10 | [diff] [blame] | 55 | base::StringToInt(resource_string, &val); |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 56 | return Value::CreateIntegerValue(val); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 57 | } |
| 58 | |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 59 | case Value::TYPE_DOUBLE: { |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 60 | double val; |
[email protected] | 16b52716 | 2010-08-15 18:37:10 | [diff] [blame] | 61 | base::StringToDouble(resource_string, &val); |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 62 | return Value::CreateDoubleValue(val); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | case Value::TYPE_STRING: { |
| 66 | return Value::CreateStringValue(resource_string); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | default: { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 70 | NOTREACHED() << |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 71 | "list and dictionary types cannot have default locale values"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | NOTREACHED(); |
| 75 | return Value::CreateNullValue(); |
| 76 | } |
| 77 | |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 78 | // Forwards a notification after a PostMessage so that we can wait for the |
| 79 | // MessageLoop to run. |
| 80 | void NotifyReadError(PrefService* pref, int message_id) { |
| 81 | Source<PrefService> source(pref); |
| 82 | NotificationService::current()->Notify(NotificationType::PROFILE_ERROR, |
| 83 | source, Details<int>(&message_id)); |
| 84 | } |
| 85 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 86 | } // namespace |
| 87 | |
[email protected] | db198b2 | 2010-07-12 16:48:49 | [diff] [blame] | 88 | // static |
[email protected] | a9c23a5 | 2010-08-04 09:13:44 | [diff] [blame] | 89 | PrefService* PrefService::CreatePrefService(const FilePath& pref_filename, |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 90 | PrefStore* extension_prefs, |
[email protected] | a9c23a5 | 2010-08-04 09:13:44 | [diff] [blame] | 91 | Profile* profile) { |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 92 | using policy::ConfigurationPolicyPrefStore; |
| 93 | |
[email protected] | 1d01d41 | 2010-08-20 00:36:01 | [diff] [blame] | 94 | #if defined(OS_LINUX) |
| 95 | // We'd like to see what fraction of our users have the preferences |
| 96 | // stored on a network file system, as we've had no end of troubles |
| 97 | // with NFS/AFS. |
| 98 | // TODO(evanm): remove this once we've collected state. |
| 99 | file_util::FileSystemType fstype; |
| 100 | if (file_util::GetFileSystemType(pref_filename.DirName(), &fstype)) { |
| 101 | UMA_HISTOGRAM_ENUMERATION("PrefService.FileSystemType", |
| 102 | static_cast<int>(fstype), |
| 103 | file_util::FILE_SYSTEM_TYPE_COUNT); |
| 104 | } |
| 105 | #endif |
| 106 | |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 107 | ConfigurationPolicyPrefStore* managed_platform = |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 108 | ConfigurationPolicyPrefStore::CreateManagedPlatformPolicyPrefStore(); |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 109 | ConfigurationPolicyPrefStore* managed_cloud = |
| 110 | ConfigurationPolicyPrefStore::CreateManagedCloudPolicyPrefStore(profile); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 111 | CommandLinePrefStore* command_line = |
| 112 | new CommandLinePrefStore(CommandLine::ForCurrentProcess()); |
| 113 | JsonPrefStore* user = new JsonPrefStore( |
| 114 | pref_filename, |
| 115 | BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 116 | ConfigurationPolicyPrefStore* recommended_platform = |
| 117 | ConfigurationPolicyPrefStore::CreateRecommendedPlatformPolicyPrefStore(); |
| 118 | ConfigurationPolicyPrefStore* recommended_cloud = |
| 119 | ConfigurationPolicyPrefStore::CreateRecommendedCloudPolicyPrefStore( |
| 120 | profile); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 121 | DefaultPrefStore* default_pref_store = new DefaultPrefStore(); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 122 | |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 123 | return new PrefService(managed_platform, managed_cloud, extension_prefs, |
| 124 | command_line, user, recommended_platform, |
| 125 | recommended_cloud, default_pref_store); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | PrefService* PrefService::CreateIncognitoPrefService( |
| 129 | PrefStore* incognito_extension_prefs) { |
| 130 | return new PrefService(*this, incognito_extension_prefs); |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 131 | } |
| 132 | |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 133 | PrefService::PrefService(PrefStore* managed_platform_prefs, |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 134 | PrefStore* managed_cloud_prefs, |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 135 | PrefStore* extension_prefs, |
| 136 | PrefStore* command_line_prefs, |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 137 | PersistentPrefStore* user_prefs, |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 138 | PrefStore* recommended_platform_prefs, |
| 139 | PrefStore* recommended_cloud_prefs, |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 140 | DefaultPrefStore* default_store) |
| 141 | : user_pref_store_(user_prefs), |
| 142 | default_store_(default_store) { |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 143 | pref_notifier_.reset(new PrefNotifierImpl(this)); |
[email protected] | a98ce126 | 2011-01-28 13:20:23 | [diff] [blame] | 144 | pref_value_store_.reset( |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 145 | new PrefValueStore(managed_platform_prefs, |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 146 | managed_cloud_prefs, |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 147 | extension_prefs, |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 148 | command_line_prefs, |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 149 | user_pref_store_, |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 150 | recommended_platform_prefs, |
| 151 | recommended_cloud_prefs, |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 152 | default_store, |
[email protected] | a98ce126 | 2011-01-28 13:20:23 | [diff] [blame] | 153 | pref_notifier_.get())); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 154 | InitFromStorage(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 155 | } |
| 156 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 157 | PrefService::PrefService(const PrefService& original, |
| 158 | PrefStore* incognito_extension_prefs) |
| 159 | : user_pref_store_( |
| 160 | new OverlayPersistentPrefStore(original.user_pref_store_.get())), |
| 161 | default_store_(original.default_store_.get()){ |
| 162 | pref_notifier_.reset(new PrefNotifierImpl(this)); |
[email protected] | a98ce126 | 2011-01-28 13:20:23 | [diff] [blame] | 163 | pref_value_store_.reset(original.pref_value_store_->CloneAndSpecialize( |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 164 | NULL, // managed_platform_prefs |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 165 | NULL, // managed_cloud_prefs |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 166 | incognito_extension_prefs, |
| 167 | NULL, // command_line_prefs |
| 168 | user_pref_store_.get(), |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 169 | NULL, // recommended_platform_prefs |
| 170 | NULL, // recommended_cloud_prefs |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 171 | default_store_.get(), |
[email protected] | a98ce126 | 2011-01-28 13:20:23 | [diff] [blame] | 172 | pref_notifier_.get())); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 173 | InitFromStorage(); |
| 174 | } |
| 175 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 176 | PrefService::~PrefService() { |
| 177 | DCHECK(CalledOnValidThread()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 178 | STLDeleteContainerPointers(prefs_.begin(), prefs_.end()); |
| 179 | prefs_.clear(); |
[email protected] | a98ce126 | 2011-01-28 13:20:23 | [diff] [blame] | 180 | |
| 181 | // Reset pointers so accesses after destruction reliably crash. |
| 182 | pref_value_store_.reset(); |
| 183 | user_pref_store_ = NULL; |
| 184 | default_store_ = NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 185 | } |
| 186 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 187 | void PrefService::InitFromStorage() { |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 188 | const PersistentPrefStore::PrefReadError error = |
| 189 | user_pref_store_->ReadPrefs(); |
| 190 | if (error == PersistentPrefStore::PREF_READ_ERROR_NONE) |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 191 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 192 | |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 193 | // Failing to load prefs on startup is a bad thing(TM). See bug 38352 for |
| 194 | // an example problem that this can cause. |
| 195 | // Do some diagnosis and try to avoid losing data. |
| 196 | int message_id = 0; |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 197 | if (error <= PersistentPrefStore::PREF_READ_ERROR_JSON_TYPE) { |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 198 | message_id = IDS_PREFERENCES_CORRUPT_ERROR; |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 199 | } else if (error != PersistentPrefStore::PREF_READ_ERROR_NO_FILE) { |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 200 | message_id = IDS_PREFERENCES_UNREADABLE_ERROR; |
| 201 | } |
| 202 | |
| 203 | if (message_id) { |
[email protected] | 8bac235 | 2010-10-10 18:53:21 | [diff] [blame] | 204 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 205 | NewRunnableFunction(&NotifyReadError, this, message_id)); |
| 206 | } |
| 207 | UMA_HISTOGRAM_ENUMERATION("PrefService.ReadError", error, 20); |
| 208 | } |
| 209 | |
| 210 | bool PrefService::ReloadPersistentPrefs() { |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 211 | return user_pref_store_->ReadPrefs() == |
| 212 | PersistentPrefStore::PREF_READ_ERROR_NONE; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 213 | } |
| 214 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 215 | bool PrefService::SavePersistentPrefs() { |
| 216 | DCHECK(CalledOnValidThread()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 217 | |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 218 | return user_pref_store_->WritePrefs(); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 219 | } |
| 220 | |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 221 | void PrefService::ScheduleSavePersistentPrefs() { |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 222 | DCHECK(CalledOnValidThread()); |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 223 | |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 224 | user_pref_store_->ScheduleWritePrefs(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 225 | } |
| 226 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 227 | void PrefService::RegisterBooleanPref(const char* path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 228 | bool default_value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 229 | RegisterPreference(path, Value::CreateBooleanValue(default_value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 230 | } |
| 231 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 232 | void PrefService::RegisterIntegerPref(const char* path, int default_value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 233 | RegisterPreference(path, Value::CreateIntegerValue(default_value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 234 | } |
| 235 | |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 236 | void PrefService::RegisterDoublePref(const char* path, double default_value) { |
| 237 | RegisterPreference(path, Value::CreateDoubleValue(default_value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 238 | } |
| 239 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 240 | void PrefService::RegisterStringPref(const char* path, |
[email protected] | 20ce516d | 2010-06-18 02:20:04 | [diff] [blame] | 241 | const std::string& default_value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 242 | RegisterPreference(path, Value::CreateStringValue(default_value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 243 | } |
| 244 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 245 | void PrefService::RegisterFilePathPref(const char* path, |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 246 | const FilePath& default_value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 247 | RegisterPreference(path, Value::CreateStringValue(default_value.value())); |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 248 | } |
| 249 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 250 | void PrefService::RegisterListPref(const char* path) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 251 | RegisterPreference(path, new ListValue()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 252 | } |
| 253 | |
[email protected] | c2f23d01 | 2011-02-09 14:52:17 | [diff] [blame] | 254 | void PrefService::RegisterListPref(const char* path, ListValue* default_value) { |
| 255 | RegisterPreference(path, default_value); |
| 256 | } |
| 257 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 258 | void PrefService::RegisterDictionaryPref(const char* path) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 259 | RegisterPreference(path, new DictionaryValue()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 260 | } |
| 261 | |
[email protected] | c2f23d01 | 2011-02-09 14:52:17 | [diff] [blame] | 262 | void PrefService::RegisterDictionaryPref(const char* path, |
| 263 | DictionaryValue* default_value) { |
| 264 | RegisterPreference(path, default_value); |
| 265 | } |
| 266 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 267 | void PrefService::RegisterLocalizedBooleanPref(const char* path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 268 | int locale_default_message_id) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 269 | RegisterPreference( |
| 270 | path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 271 | CreateLocaleDefaultValue(Value::TYPE_BOOLEAN, locale_default_message_id)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 272 | } |
| 273 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 274 | void PrefService::RegisterLocalizedIntegerPref(const char* path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 275 | int locale_default_message_id) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 276 | RegisterPreference( |
| 277 | path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 278 | CreateLocaleDefaultValue(Value::TYPE_INTEGER, locale_default_message_id)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 279 | } |
| 280 | |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 281 | void PrefService::RegisterLocalizedDoublePref(const char* path, |
| 282 | int locale_default_message_id) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 283 | RegisterPreference( |
| 284 | path, |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 285 | CreateLocaleDefaultValue(Value::TYPE_DOUBLE, locale_default_message_id)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 286 | } |
| 287 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 288 | void PrefService::RegisterLocalizedStringPref(const char* path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 289 | int locale_default_message_id) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 290 | RegisterPreference( |
| 291 | path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 292 | CreateLocaleDefaultValue(Value::TYPE_STRING, locale_default_message_id)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 293 | } |
| 294 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 295 | bool PrefService::GetBoolean(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 296 | DCHECK(CalledOnValidThread()); |
| 297 | |
| 298 | bool result = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 299 | |
| 300 | const Preference* pref = FindPreference(path); |
| 301 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 302 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 303 | return result; |
| 304 | } |
| 305 | bool rv = pref->GetValue()->GetAsBoolean(&result); |
| 306 | DCHECK(rv); |
| 307 | return result; |
| 308 | } |
| 309 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 310 | int PrefService::GetInteger(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 311 | DCHECK(CalledOnValidThread()); |
| 312 | |
| 313 | int result = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 314 | |
| 315 | const Preference* pref = FindPreference(path); |
| 316 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 317 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 318 | return result; |
| 319 | } |
| 320 | bool rv = pref->GetValue()->GetAsInteger(&result); |
| 321 | DCHECK(rv); |
| 322 | return result; |
| 323 | } |
| 324 | |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 325 | double PrefService::GetDouble(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 326 | DCHECK(CalledOnValidThread()); |
| 327 | |
| 328 | double result = 0.0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 329 | |
| 330 | const Preference* pref = FindPreference(path); |
| 331 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 332 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 333 | return result; |
| 334 | } |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 335 | bool rv = pref->GetValue()->GetAsDouble(&result); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 336 | DCHECK(rv); |
| 337 | return result; |
| 338 | } |
| 339 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 340 | std::string PrefService::GetString(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 341 | DCHECK(CalledOnValidThread()); |
| 342 | |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 343 | std::string result; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 344 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 345 | const Preference* pref = FindPreference(path); |
| 346 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 347 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 348 | return result; |
| 349 | } |
| 350 | bool rv = pref->GetValue()->GetAsString(&result); |
| 351 | DCHECK(rv); |
| 352 | return result; |
| 353 | } |
| 354 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 355 | FilePath PrefService::GetFilePath(const char* path) const { |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 356 | DCHECK(CalledOnValidThread()); |
| 357 | |
| 358 | FilePath::StringType result; |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 359 | |
| 360 | const Preference* pref = FindPreference(path); |
| 361 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 362 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 363 | return FilePath(result); |
| 364 | } |
| 365 | bool rv = pref->GetValue()->GetAsString(&result); |
| 366 | DCHECK(rv); |
[email protected] | 66de4f09 | 2009-09-04 23:59:40 | [diff] [blame] | 367 | #if defined(OS_POSIX) |
| 368 | // We store filepaths as UTF8, so convert it back to the system type. |
| 369 | result = base::SysWideToNativeMB(UTF8ToWide(result)); |
| 370 | #endif |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 371 | return FilePath(result); |
| 372 | } |
| 373 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 374 | bool PrefService::HasPrefPath(const char* path) const { |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 375 | const Preference* pref = FindPreference(path); |
| 376 | return pref && !pref->IsDefaultValue(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 377 | } |
| 378 | |
[email protected] | ebd0b02 | 2011-01-27 13:24:14 | [diff] [blame] | 379 | DictionaryValue* PrefService::GetPreferenceValues() const { |
| 380 | DCHECK(CalledOnValidThread()); |
| 381 | DictionaryValue* out = new DictionaryValue; |
| 382 | DefaultPrefStore::const_iterator i = default_store_->begin(); |
| 383 | for (; i != default_store_->end(); ++i) { |
| 384 | const Value* value = FindPreference(i->first.c_str())->GetValue(); |
| 385 | out->Set(i->first, value->DeepCopy()); |
| 386 | } |
| 387 | return out; |
| 388 | } |
| 389 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 390 | const PrefService::Preference* PrefService::FindPreference( |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 391 | const char* pref_name) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 392 | DCHECK(CalledOnValidThread()); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 393 | Preference p(this, pref_name, Value::TYPE_NULL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 394 | PreferenceSet::const_iterator it = prefs_.find(&p); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 395 | if (it != prefs_.end()) |
| 396 | return *it; |
| 397 | const Value::ValueType type = default_store_->GetType(pref_name); |
| 398 | if (type == Value::TYPE_NULL) |
| 399 | return NULL; |
| 400 | Preference* new_pref = new Preference(this, pref_name, type); |
| 401 | prefs_.insert(new_pref); |
| 402 | return new_pref; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 403 | } |
| 404 | |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 405 | bool PrefService::ReadOnly() const { |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 406 | return user_pref_store_->ReadOnly(); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | PrefNotifier* PrefService::pref_notifier() const { |
| 410 | return pref_notifier_.get(); |
| 411 | } |
| 412 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 413 | bool PrefService::IsManagedPreference(const char* pref_name) const { |
[email protected] | d90de1c0 | 2010-07-19 19:50:48 | [diff] [blame] | 414 | const Preference* pref = FindPreference(pref_name); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 415 | return pref && pref->IsManaged(); |
[email protected] | d90de1c0 | 2010-07-19 19:50:48 | [diff] [blame] | 416 | } |
| 417 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 418 | const DictionaryValue* PrefService::GetDictionary(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 419 | DCHECK(CalledOnValidThread()); |
| 420 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 421 | const Preference* pref = FindPreference(path); |
| 422 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 423 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 424 | return NULL; |
| 425 | } |
| 426 | const Value* value = pref->GetValue(); |
[email protected] | 11b040b | 2011-02-02 12:42:25 | [diff] [blame] | 427 | if (value->GetType() != Value::TYPE_DICTIONARY) { |
| 428 | NOTREACHED(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 429 | return NULL; |
[email protected] | 11b040b | 2011-02-02 12:42:25 | [diff] [blame] | 430 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 431 | return static_cast<const DictionaryValue*>(value); |
| 432 | } |
| 433 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 434 | const ListValue* PrefService::GetList(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 435 | DCHECK(CalledOnValidThread()); |
| 436 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 437 | const Preference* pref = FindPreference(path); |
| 438 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 439 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 440 | return NULL; |
| 441 | } |
| 442 | const Value* value = pref->GetValue(); |
[email protected] | 11b040b | 2011-02-02 12:42:25 | [diff] [blame] | 443 | if (value->GetType() != Value::TYPE_LIST) { |
| 444 | NOTREACHED(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 445 | return NULL; |
[email protected] | 11b040b | 2011-02-02 12:42:25 | [diff] [blame] | 446 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 447 | return static_cast<const ListValue*>(value); |
| 448 | } |
| 449 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 450 | void PrefService::AddPrefObserver(const char* path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 451 | NotificationObserver* obs) { |
[email protected] | d81288a0 | 2010-08-18 07:25:50 | [diff] [blame] | 452 | pref_notifier_->AddPrefObserver(path, obs); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 453 | } |
| 454 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 455 | void PrefService::RemovePrefObserver(const char* path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 456 | NotificationObserver* obs) { |
[email protected] | d81288a0 | 2010-08-18 07:25:50 | [diff] [blame] | 457 | pref_notifier_->RemovePrefObserver(path, obs); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 458 | } |
| 459 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 460 | void PrefService::RegisterPreference(const char* path, Value* default_value) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 461 | DCHECK(CalledOnValidThread()); |
| 462 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 463 | // The main code path takes ownership, but most don't. We'll be safe. |
| 464 | scoped_ptr<Value> scoped_value(default_value); |
| 465 | |
| 466 | if (FindPreference(path)) { |
| 467 | NOTREACHED() << "Tried to register duplicate pref " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 468 | return; |
| 469 | } |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 470 | |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 471 | Value::ValueType orig_type = default_value->GetType(); |
| 472 | DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) << |
| 473 | "invalid preference type: " << orig_type; |
| 474 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 475 | // Hand off ownership. |
| 476 | default_store_->SetDefaultValue(path, scoped_value.release()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 477 | } |
| 478 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 479 | void PrefService::ClearPref(const char* path) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 480 | DCHECK(CalledOnValidThread()); |
| 481 | |
| 482 | const Preference* pref = FindPreference(path); |
| 483 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 484 | NOTREACHED() << "Trying to clear an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 485 | return; |
| 486 | } |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 487 | user_pref_store_->RemoveValue(path); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 488 | } |
| 489 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 490 | void PrefService::Set(const char* path, const Value& value) { |
[email protected] | a048d7e4 | 2009-12-01 01:02:39 | [diff] [blame] | 491 | DCHECK(CalledOnValidThread()); |
| 492 | |
| 493 | const Preference* pref = FindPreference(path); |
| 494 | if (!pref) { |
| 495 | NOTREACHED() << "Trying to write an unregistered pref: " << path; |
| 496 | return; |
| 497 | } |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 498 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 499 | if (pref->GetType() != value.GetType()) { |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 500 | NOTREACHED() << "Trying to set pref " << path |
| 501 | << " of type " << pref->GetType() |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 502 | << " to value of type " << value.GetType(); |
| 503 | } else { |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 504 | user_pref_store_->SetValue(path, value.DeepCopy()); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 505 | } |
[email protected] | a048d7e4 | 2009-12-01 01:02:39 | [diff] [blame] | 506 | } |
| 507 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 508 | void PrefService::SetBoolean(const char* path, bool value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 509 | SetUserPrefValue(path, Value::CreateBooleanValue(value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 510 | } |
| 511 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 512 | void PrefService::SetInteger(const char* path, int value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 513 | SetUserPrefValue(path, Value::CreateIntegerValue(value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 514 | } |
| 515 | |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 516 | void PrefService::SetDouble(const char* path, double value) { |
| 517 | SetUserPrefValue(path, Value::CreateDoubleValue(value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 518 | } |
| 519 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 520 | void PrefService::SetString(const char* path, const std::string& value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 521 | SetUserPrefValue(path, Value::CreateStringValue(value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 522 | } |
| 523 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 524 | void PrefService::SetFilePath(const char* path, const FilePath& value) { |
[email protected] | 66de4f09 | 2009-09-04 23:59:40 | [diff] [blame] | 525 | #if defined(OS_POSIX) |
| 526 | // Value::SetString only knows about UTF8 strings, so convert the path from |
| 527 | // the system native value to UTF8. |
| 528 | std::string path_utf8 = WideToUTF8(base::SysNativeMBToWide(value.value())); |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 529 | Value* new_value = Value::CreateStringValue(path_utf8); |
[email protected] | 66de4f09 | 2009-09-04 23:59:40 | [diff] [blame] | 530 | #else |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 531 | Value* new_value = Value::CreateStringValue(value.value()); |
[email protected] | 66de4f09 | 2009-09-04 23:59:40 | [diff] [blame] | 532 | #endif |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 533 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 534 | SetUserPrefValue(path, new_value); |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 535 | } |
| 536 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 537 | void PrefService::SetInt64(const char* path, int64 value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 538 | SetUserPrefValue(path, Value::CreateStringValue(base::Int64ToString(value))); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 539 | } |
| 540 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 541 | int64 PrefService::GetInt64(const char* path) const { |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 542 | DCHECK(CalledOnValidThread()); |
| 543 | |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 544 | const Preference* pref = FindPreference(path); |
| 545 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 546 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
[email protected] | c345330 | 2009-12-01 01:33:08 | [diff] [blame] | 547 | return 0; |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 548 | } |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 549 | std::string result("0"); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 550 | bool rv = pref->GetValue()->GetAsString(&result); |
| 551 | DCHECK(rv); |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 552 | |
| 553 | int64 val; |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 554 | base::StringToInt64(result, &val); |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 555 | return val; |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 556 | } |
| 557 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 558 | void PrefService::RegisterInt64Pref(const char* path, int64 default_value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 559 | RegisterPreference( |
| 560 | path, Value::CreateStringValue(base::Int64ToString(default_value))); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 561 | } |
| 562 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 563 | DictionaryValue* PrefService::GetMutableDictionary(const char* path) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 564 | DCHECK(CalledOnValidThread()); |
[email protected] | d166fcb | 2011-02-09 18:04:11 | [diff] [blame] | 565 | DLOG_IF(WARNING, IsManagedPreference(path)) << |
| 566 | "Attempt to change managed preference " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 567 | |
| 568 | const Preference* pref = FindPreference(path); |
| 569 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 570 | NOTREACHED() << "Trying to get an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 571 | return NULL; |
| 572 | } |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 573 | if (pref->GetType() != Value::TYPE_DICTIONARY) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 574 | NOTREACHED() << "Wrong type for GetMutableDictionary: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 575 | return NULL; |
| 576 | } |
| 577 | |
| 578 | DictionaryValue* dict = NULL; |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 579 | Value* tmp_value = NULL; |
[email protected] | e025089 | 2010-10-01 18:57:53 | [diff] [blame] | 580 | // Look for an existing preference in the user store. If it doesn't |
| 581 | // exist or isn't the correct type, create a new user preference. |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 582 | if (user_pref_store_->GetValue(path, &tmp_value) |
| 583 | != PersistentPrefStore::READ_OK || |
[email protected] | 9176124 | 2010-06-10 15:35:37 | [diff] [blame] | 584 | !tmp_value->IsType(Value::TYPE_DICTIONARY)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 585 | dict = new DictionaryValue; |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 586 | user_pref_store_->SetValueSilently(path, dict); |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 587 | } else { |
| 588 | dict = static_cast<DictionaryValue*>(tmp_value); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 589 | } |
| 590 | return dict; |
| 591 | } |
| 592 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 593 | ListValue* PrefService::GetMutableList(const char* path) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 594 | DCHECK(CalledOnValidThread()); |
[email protected] | d166fcb | 2011-02-09 18:04:11 | [diff] [blame] | 595 | DLOG_IF(WARNING, IsManagedPreference(path)) << |
| 596 | "Attempt to change managed preference " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 597 | |
| 598 | const Preference* pref = FindPreference(path); |
| 599 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 600 | NOTREACHED() << "Trying to get an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 601 | return NULL; |
| 602 | } |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 603 | if (pref->GetType() != Value::TYPE_LIST) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 604 | NOTREACHED() << "Wrong type for GetMutableList: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 605 | return NULL; |
| 606 | } |
| 607 | |
| 608 | ListValue* list = NULL; |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 609 | Value* tmp_value = NULL; |
[email protected] | e025089 | 2010-10-01 18:57:53 | [diff] [blame] | 610 | // Look for an existing preference in the user store. If it doesn't |
| 611 | // exist or isn't the correct type, create a new user preference. |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 612 | if (user_pref_store_->GetValue(path, &tmp_value) |
| 613 | != PersistentPrefStore::READ_OK || |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 614 | !tmp_value->IsType(Value::TYPE_LIST)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 615 | list = new ListValue; |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 616 | user_pref_store_->SetValueSilently(path, list); |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 617 | } else { |
| 618 | list = static_cast<ListValue*>(tmp_value); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 619 | } |
| 620 | return list; |
| 621 | } |
| 622 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 623 | void PrefService::SetUserPrefValue(const char* path, Value* new_value) { |
| 624 | DCHECK(CalledOnValidThread()); |
[email protected] | d166fcb | 2011-02-09 18:04:11 | [diff] [blame] | 625 | DLOG_IF(WARNING, IsManagedPreference(path)) << |
| 626 | "Attempt to change managed preference " << path; |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 627 | |
| 628 | const Preference* pref = FindPreference(path); |
| 629 | if (!pref) { |
| 630 | NOTREACHED() << "Trying to write an unregistered pref: " << path; |
| 631 | return; |
| 632 | } |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 633 | if (pref->GetType() != new_value->GetType()) { |
| 634 | NOTREACHED() << "Trying to set pref " << path |
| 635 | << " of type " << pref->GetType() |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 636 | << " to value of type " << new_value->GetType(); |
| 637 | return; |
| 638 | } |
| 639 | |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 640 | user_pref_store_->SetValue(path, new_value); |
[email protected] | 73c4793 | 2010-12-06 18:13:43 | [diff] [blame] | 641 | } |
| 642 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 643 | /////////////////////////////////////////////////////////////////////////////// |
| 644 | // PrefService::Preference |
| 645 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 646 | PrefService::Preference::Preference(const PrefService* service, |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 647 | const char* name, |
| 648 | Value::ValueType type) |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 649 | : name_(name), |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 650 | type_(type), |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 651 | pref_service_(service) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 652 | DCHECK(name); |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 653 | DCHECK(service); |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | Value::ValueType PrefService::Preference::GetType() const { |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 657 | return type_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | const Value* PrefService::Preference::GetValue() const { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 661 | DCHECK(pref_service_->FindPreference(name_.c_str())) << |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 662 | "Must register pref before getting its value"; |
| 663 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 664 | Value* found_value = NULL; |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 665 | if (pref_value_store()->GetValue(name_, type_, &found_value)) { |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 666 | DCHECK(found_value->IsType(type_)); |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 667 | return found_value; |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 668 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 669 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 670 | // Every registered preference has at least a default value. |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 671 | NOTREACHED() << "no valid value found for registered pref " << name_; |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 672 | return NULL; |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | bool PrefService::Preference::IsManaged() const { |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 676 | return pref_value_store()->PrefValueInManagedStore(name_.c_str()); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | bool PrefService::Preference::HasExtensionSetting() const { |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 680 | return pref_value_store()->PrefValueInExtensionStore(name_.c_str()); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | bool PrefService::Preference::HasUserSetting() const { |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 684 | return pref_value_store()->PrefValueInUserStore(name_.c_str()); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | bool PrefService::Preference::IsExtensionControlled() const { |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 688 | return pref_value_store()->PrefValueFromExtensionStore(name_.c_str()); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | bool PrefService::Preference::IsUserControlled() const { |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 692 | return pref_value_store()->PrefValueFromUserStore(name_.c_str()); |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | bool PrefService::Preference::IsDefaultValue() const { |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 696 | return pref_value_store()->PrefValueFromDefaultStore(name_.c_str()); |
[email protected] | d7449e8 | 2010-07-14 11:42:35 | [diff] [blame] | 697 | } |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 698 | |
| 699 | bool PrefService::Preference::IsUserModifiable() const { |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 700 | return pref_value_store()->PrefValueUserModifiable(name_.c_str()); |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 701 | } |