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