[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 | |
| 59 | case Value::TYPE_REAL: { |
[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] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 62 | return Value::CreateRealValue(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] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 107 | ConfigurationPolicyPrefStore* managed = |
| 108 | ConfigurationPolicyPrefStore::CreateManagedPlatformPolicyPrefStore(); |
| 109 | ConfigurationPolicyPrefStore* device_management = |
| 110 | ConfigurationPolicyPrefStore::CreateDeviceManagementPolicyPrefStore( |
| 111 | profile); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 112 | CommandLinePrefStore* command_line = |
| 113 | new CommandLinePrefStore(CommandLine::ForCurrentProcess()); |
| 114 | JsonPrefStore* user = new JsonPrefStore( |
| 115 | pref_filename, |
| 116 | BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); |
| 117 | ConfigurationPolicyPrefStore* recommended = |
| 118 | ConfigurationPolicyPrefStore::CreateRecommendedPolicyPrefStore(); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 119 | DefaultPrefStore* default_pref_store = new DefaultPrefStore(); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 120 | |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 121 | return new PrefService(managed, device_management, extension_prefs, |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 122 | command_line, user, recommended, default_pref_store); |
| 123 | } |
| 124 | |
| 125 | PrefService* PrefService::CreateIncognitoPrefService( |
| 126 | PrefStore* incognito_extension_prefs) { |
| 127 | return new PrefService(*this, incognito_extension_prefs); |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 128 | } |
| 129 | |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 130 | PrefService::PrefService(PrefStore* managed_platform_prefs, |
| 131 | PrefStore* device_management_prefs, |
| 132 | PrefStore* extension_prefs, |
| 133 | PrefStore* command_line_prefs, |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 134 | PersistentPrefStore* user_prefs, |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 135 | PrefStore* recommended_prefs, |
| 136 | DefaultPrefStore* default_store) |
| 137 | : user_pref_store_(user_prefs), |
| 138 | default_store_(default_store) { |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 139 | pref_notifier_.reset(new PrefNotifierImpl(this)); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 140 | pref_value_store_ = |
| 141 | new PrefValueStore(managed_platform_prefs, |
| 142 | device_management_prefs, |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 143 | extension_prefs, |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 144 | command_line_prefs, |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 145 | user_pref_store_, |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 146 | recommended_prefs, |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 147 | default_store, |
[email protected] | f00768e | 2010-12-23 12:39:01 | [diff] [blame] | 148 | pref_notifier_.get()); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 149 | InitFromStorage(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 150 | } |
| 151 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 152 | PrefService::PrefService(const PrefService& original, |
| 153 | PrefStore* incognito_extension_prefs) |
| 154 | : user_pref_store_( |
| 155 | new OverlayPersistentPrefStore(original.user_pref_store_.get())), |
| 156 | default_store_(original.default_store_.get()){ |
| 157 | pref_notifier_.reset(new PrefNotifierImpl(this)); |
| 158 | pref_value_store_ = original.pref_value_store_->CloneAndSpecialize( |
| 159 | NULL, // managed_platform_prefs |
| 160 | NULL, // device_management_prefs |
| 161 | incognito_extension_prefs, |
| 162 | NULL, // command_line_prefs |
| 163 | user_pref_store_.get(), |
| 164 | NULL, // recommended_prefs |
| 165 | default_store_.get(), |
| 166 | pref_notifier_.get() ); |
| 167 | InitFromStorage(); |
| 168 | } |
| 169 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 170 | PrefService::~PrefService() { |
| 171 | DCHECK(CalledOnValidThread()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 172 | STLDeleteContainerPointers(prefs_.begin(), prefs_.end()); |
| 173 | prefs_.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 174 | } |
| 175 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 176 | void PrefService::InitFromStorage() { |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 177 | const PersistentPrefStore::PrefReadError error = |
| 178 | user_pref_store_->ReadPrefs(); |
| 179 | if (error == PersistentPrefStore::PREF_READ_ERROR_NONE) |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 180 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 181 | |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 182 | // Failing to load prefs on startup is a bad thing(TM). See bug 38352 for |
| 183 | // an example problem that this can cause. |
| 184 | // Do some diagnosis and try to avoid losing data. |
| 185 | int message_id = 0; |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 186 | if (error <= PersistentPrefStore::PREF_READ_ERROR_JSON_TYPE) { |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 187 | message_id = IDS_PREFERENCES_CORRUPT_ERROR; |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 188 | } else if (error != PersistentPrefStore::PREF_READ_ERROR_NO_FILE) { |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 189 | message_id = IDS_PREFERENCES_UNREADABLE_ERROR; |
| 190 | } |
| 191 | |
| 192 | if (message_id) { |
[email protected] | 8bac235 | 2010-10-10 18:53:21 | [diff] [blame] | 193 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 194 | NewRunnableFunction(&NotifyReadError, this, message_id)); |
| 195 | } |
| 196 | UMA_HISTOGRAM_ENUMERATION("PrefService.ReadError", error, 20); |
| 197 | } |
| 198 | |
| 199 | bool PrefService::ReloadPersistentPrefs() { |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 200 | return user_pref_store_->ReadPrefs() == |
| 201 | PersistentPrefStore::PREF_READ_ERROR_NONE; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 202 | } |
| 203 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 204 | bool PrefService::SavePersistentPrefs() { |
| 205 | DCHECK(CalledOnValidThread()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 206 | |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 207 | return user_pref_store_->WritePrefs(); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 208 | } |
| 209 | |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 210 | void PrefService::ScheduleSavePersistentPrefs() { |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 211 | DCHECK(CalledOnValidThread()); |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 212 | |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 213 | user_pref_store_->ScheduleWritePrefs(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 214 | } |
| 215 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 216 | void PrefService::RegisterBooleanPref(const char* path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 217 | bool default_value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 218 | RegisterPreference(path, Value::CreateBooleanValue(default_value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 219 | } |
| 220 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 221 | void PrefService::RegisterIntegerPref(const char* path, int default_value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 222 | RegisterPreference(path, Value::CreateIntegerValue(default_value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 223 | } |
| 224 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 225 | void PrefService::RegisterRealPref(const char* path, double default_value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 226 | RegisterPreference(path, Value::CreateRealValue(default_value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 227 | } |
| 228 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 229 | void PrefService::RegisterStringPref(const char* path, |
[email protected] | 20ce516d | 2010-06-18 02:20:04 | [diff] [blame] | 230 | const std::string& default_value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 231 | RegisterPreference(path, Value::CreateStringValue(default_value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 232 | } |
| 233 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 234 | void PrefService::RegisterFilePathPref(const char* path, |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 235 | const FilePath& default_value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 236 | RegisterPreference(path, Value::CreateStringValue(default_value.value())); |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 237 | } |
| 238 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 239 | void PrefService::RegisterListPref(const char* path) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 240 | RegisterPreference(path, new ListValue()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 241 | } |
| 242 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 243 | void PrefService::RegisterDictionaryPref(const char* path) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 244 | RegisterPreference(path, new DictionaryValue()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 245 | } |
| 246 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 247 | void PrefService::RegisterLocalizedBooleanPref(const char* path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 248 | int locale_default_message_id) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 249 | RegisterPreference( |
| 250 | path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 251 | CreateLocaleDefaultValue(Value::TYPE_BOOLEAN, locale_default_message_id)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 252 | } |
| 253 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 254 | void PrefService::RegisterLocalizedIntegerPref(const char* path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 255 | int locale_default_message_id) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 256 | RegisterPreference( |
| 257 | path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 258 | CreateLocaleDefaultValue(Value::TYPE_INTEGER, locale_default_message_id)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 259 | } |
| 260 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 261 | void PrefService::RegisterLocalizedRealPref(const char* path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 262 | int locale_default_message_id) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 263 | RegisterPreference( |
| 264 | path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 265 | CreateLocaleDefaultValue(Value::TYPE_REAL, locale_default_message_id)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 266 | } |
| 267 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 268 | void PrefService::RegisterLocalizedStringPref(const char* path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 269 | int locale_default_message_id) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 270 | RegisterPreference( |
| 271 | path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 272 | CreateLocaleDefaultValue(Value::TYPE_STRING, locale_default_message_id)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 273 | } |
| 274 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 275 | bool PrefService::GetBoolean(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 276 | DCHECK(CalledOnValidThread()); |
| 277 | |
| 278 | bool result = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 279 | |
| 280 | const Preference* pref = FindPreference(path); |
| 281 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 282 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 283 | return result; |
| 284 | } |
| 285 | bool rv = pref->GetValue()->GetAsBoolean(&result); |
| 286 | DCHECK(rv); |
| 287 | return result; |
| 288 | } |
| 289 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 290 | int PrefService::GetInteger(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 291 | DCHECK(CalledOnValidThread()); |
| 292 | |
| 293 | int result = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 294 | |
| 295 | const Preference* pref = FindPreference(path); |
| 296 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 297 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 298 | return result; |
| 299 | } |
| 300 | bool rv = pref->GetValue()->GetAsInteger(&result); |
| 301 | DCHECK(rv); |
| 302 | return result; |
| 303 | } |
| 304 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 305 | double PrefService::GetReal(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 306 | DCHECK(CalledOnValidThread()); |
| 307 | |
| 308 | double result = 0.0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 309 | |
| 310 | const Preference* pref = FindPreference(path); |
| 311 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 312 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 313 | return result; |
| 314 | } |
| 315 | bool rv = pref->GetValue()->GetAsReal(&result); |
| 316 | DCHECK(rv); |
| 317 | return result; |
| 318 | } |
| 319 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 320 | std::string PrefService::GetString(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 321 | DCHECK(CalledOnValidThread()); |
| 322 | |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 323 | std::string result; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 324 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 325 | const Preference* pref = FindPreference(path); |
| 326 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 327 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 328 | return result; |
| 329 | } |
| 330 | bool rv = pref->GetValue()->GetAsString(&result); |
| 331 | DCHECK(rv); |
| 332 | return result; |
| 333 | } |
| 334 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 335 | FilePath PrefService::GetFilePath(const char* path) const { |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 336 | DCHECK(CalledOnValidThread()); |
| 337 | |
| 338 | FilePath::StringType result; |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 339 | |
| 340 | const Preference* pref = FindPreference(path); |
| 341 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 342 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 343 | return FilePath(result); |
| 344 | } |
| 345 | bool rv = pref->GetValue()->GetAsString(&result); |
| 346 | DCHECK(rv); |
[email protected] | 66de4f09 | 2009-09-04 23:59:40 | [diff] [blame] | 347 | #if defined(OS_POSIX) |
| 348 | // We store filepaths as UTF8, so convert it back to the system type. |
| 349 | result = base::SysWideToNativeMB(UTF8ToWide(result)); |
| 350 | #endif |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 351 | return FilePath(result); |
| 352 | } |
| 353 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 354 | bool PrefService::HasPrefPath(const char* path) const { |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 355 | const Preference* pref = FindPreference(path); |
| 356 | return pref && !pref->IsDefaultValue(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | const PrefService::Preference* PrefService::FindPreference( |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 360 | const char* pref_name) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 361 | DCHECK(CalledOnValidThread()); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 362 | Preference p(this, pref_name, Value::TYPE_NULL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 363 | PreferenceSet::const_iterator it = prefs_.find(&p); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 364 | if (it != prefs_.end()) |
| 365 | return *it; |
| 366 | const Value::ValueType type = default_store_->GetType(pref_name); |
| 367 | if (type == Value::TYPE_NULL) |
| 368 | return NULL; |
| 369 | Preference* new_pref = new Preference(this, pref_name, type); |
| 370 | prefs_.insert(new_pref); |
| 371 | return new_pref; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 372 | } |
| 373 | |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 374 | bool PrefService::ReadOnly() const { |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 375 | return user_pref_store_->ReadOnly(); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | PrefNotifier* PrefService::pref_notifier() const { |
| 379 | return pref_notifier_.get(); |
| 380 | } |
| 381 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 382 | bool PrefService::IsManagedPreference(const char* pref_name) const { |
[email protected] | d90de1c0 | 2010-07-19 19:50:48 | [diff] [blame] | 383 | const Preference* pref = FindPreference(pref_name); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 384 | return pref && pref->IsManaged(); |
[email protected] | d90de1c0 | 2010-07-19 19:50:48 | [diff] [blame] | 385 | } |
| 386 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 387 | const DictionaryValue* PrefService::GetDictionary(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 388 | DCHECK(CalledOnValidThread()); |
| 389 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 390 | const Preference* pref = FindPreference(path); |
| 391 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 392 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 393 | return NULL; |
| 394 | } |
| 395 | const Value* value = pref->GetValue(); |
| 396 | if (value->GetType() == Value::TYPE_NULL) |
| 397 | return NULL; |
| 398 | return static_cast<const DictionaryValue*>(value); |
| 399 | } |
| 400 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 401 | const ListValue* PrefService::GetList(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 402 | DCHECK(CalledOnValidThread()); |
| 403 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 404 | const Preference* pref = FindPreference(path); |
| 405 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 406 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 407 | return NULL; |
| 408 | } |
| 409 | const Value* value = pref->GetValue(); |
| 410 | if (value->GetType() == Value::TYPE_NULL) |
| 411 | return NULL; |
| 412 | return static_cast<const ListValue*>(value); |
| 413 | } |
| 414 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 415 | void PrefService::AddPrefObserver(const char* path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 416 | NotificationObserver* obs) { |
[email protected] | d81288a0 | 2010-08-18 07:25:50 | [diff] [blame] | 417 | pref_notifier_->AddPrefObserver(path, obs); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 418 | } |
| 419 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 420 | void PrefService::RemovePrefObserver(const char* path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 421 | NotificationObserver* obs) { |
[email protected] | d81288a0 | 2010-08-18 07:25:50 | [diff] [blame] | 422 | pref_notifier_->RemovePrefObserver(path, obs); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 423 | } |
| 424 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 425 | void PrefService::RegisterPreference(const char* path, Value* default_value) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 426 | DCHECK(CalledOnValidThread()); |
| 427 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 428 | // The main code path takes ownership, but most don't. We'll be safe. |
| 429 | scoped_ptr<Value> scoped_value(default_value); |
| 430 | |
| 431 | if (FindPreference(path)) { |
| 432 | NOTREACHED() << "Tried to register duplicate pref " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 433 | return; |
| 434 | } |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 435 | |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 436 | Value::ValueType orig_type = default_value->GetType(); |
| 437 | DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) << |
| 438 | "invalid preference type: " << orig_type; |
| 439 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 440 | // Hand off ownership. |
| 441 | default_store_->SetDefaultValue(path, scoped_value.release()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 442 | } |
| 443 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 444 | void PrefService::ClearPref(const char* path) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 445 | DCHECK(CalledOnValidThread()); |
| 446 | |
| 447 | const Preference* pref = FindPreference(path); |
| 448 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 449 | NOTREACHED() << "Trying to clear an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 450 | return; |
| 451 | } |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 452 | user_pref_store_->RemoveValue(path); |
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::Set(const char* path, const Value& value) { |
[email protected] | a048d7e4 | 2009-12-01 01:02:39 | [diff] [blame] | 456 | DCHECK(CalledOnValidThread()); |
| 457 | |
| 458 | const Preference* pref = FindPreference(path); |
| 459 | if (!pref) { |
| 460 | NOTREACHED() << "Trying to write an unregistered pref: " << path; |
| 461 | return; |
| 462 | } |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 463 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 464 | if (pref->GetType() != value.GetType()) { |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 465 | NOTREACHED() << "Trying to set pref " << path |
| 466 | << " of type " << pref->GetType() |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 467 | << " to value of type " << value.GetType(); |
| 468 | } else { |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 469 | user_pref_store_->SetValue(path, value.DeepCopy()); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 470 | } |
[email protected] | a048d7e4 | 2009-12-01 01:02:39 | [diff] [blame] | 471 | } |
| 472 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 473 | void PrefService::SetBoolean(const char* path, bool value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 474 | SetUserPrefValue(path, Value::CreateBooleanValue(value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 475 | } |
| 476 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 477 | void PrefService::SetInteger(const char* path, int value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 478 | SetUserPrefValue(path, Value::CreateIntegerValue(value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 479 | } |
| 480 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 481 | void PrefService::SetReal(const char* path, double value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 482 | SetUserPrefValue(path, Value::CreateRealValue(value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 483 | } |
| 484 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 485 | void PrefService::SetString(const char* path, const std::string& value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 486 | SetUserPrefValue(path, Value::CreateStringValue(value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 487 | } |
| 488 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 489 | void PrefService::SetFilePath(const char* path, const FilePath& value) { |
[email protected] | 66de4f09 | 2009-09-04 23:59:40 | [diff] [blame] | 490 | #if defined(OS_POSIX) |
| 491 | // Value::SetString only knows about UTF8 strings, so convert the path from |
| 492 | // the system native value to UTF8. |
| 493 | std::string path_utf8 = WideToUTF8(base::SysNativeMBToWide(value.value())); |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 494 | Value* new_value = Value::CreateStringValue(path_utf8); |
[email protected] | 66de4f09 | 2009-09-04 23:59:40 | [diff] [blame] | 495 | #else |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 496 | Value* new_value = Value::CreateStringValue(value.value()); |
[email protected] | 66de4f09 | 2009-09-04 23:59:40 | [diff] [blame] | 497 | #endif |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 498 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 499 | SetUserPrefValue(path, new_value); |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 500 | } |
| 501 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 502 | void PrefService::SetInt64(const char* path, int64 value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 503 | SetUserPrefValue(path, Value::CreateStringValue(base::Int64ToString(value))); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 504 | } |
| 505 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 506 | int64 PrefService::GetInt64(const char* path) const { |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 507 | DCHECK(CalledOnValidThread()); |
| 508 | |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 509 | const Preference* pref = FindPreference(path); |
| 510 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 511 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
[email protected] | c345330 | 2009-12-01 01:33:08 | [diff] [blame] | 512 | return 0; |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 513 | } |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 514 | std::string result("0"); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 515 | bool rv = pref->GetValue()->GetAsString(&result); |
| 516 | DCHECK(rv); |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 517 | |
| 518 | int64 val; |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 519 | base::StringToInt64(result, &val); |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 520 | return val; |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 521 | } |
| 522 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 523 | void PrefService::RegisterInt64Pref(const char* path, int64 default_value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 524 | RegisterPreference( |
| 525 | path, Value::CreateStringValue(base::Int64ToString(default_value))); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 526 | } |
| 527 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 528 | DictionaryValue* PrefService::GetMutableDictionary(const char* path) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 529 | DCHECK(CalledOnValidThread()); |
| 530 | |
| 531 | const Preference* pref = FindPreference(path); |
| 532 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 533 | NOTREACHED() << "Trying to get an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 534 | return NULL; |
| 535 | } |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 536 | if (pref->GetType() != Value::TYPE_DICTIONARY) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 537 | NOTREACHED() << "Wrong type for GetMutableDictionary: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 538 | return NULL; |
| 539 | } |
| 540 | |
| 541 | DictionaryValue* dict = NULL; |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 542 | Value* tmp_value = NULL; |
[email protected] | e025089 | 2010-10-01 18:57:53 | [diff] [blame] | 543 | // Look for an existing preference in the user store. If it doesn't |
| 544 | // exist or isn't the correct type, create a new user preference. |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 545 | if (user_pref_store_->GetValue(path, &tmp_value) |
| 546 | != PersistentPrefStore::READ_OK || |
[email protected] | 9176124 | 2010-06-10 15:35:37 | [diff] [blame] | 547 | !tmp_value->IsType(Value::TYPE_DICTIONARY)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 548 | dict = new DictionaryValue; |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 549 | user_pref_store_->SetValueSilently(path, dict); |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 550 | } else { |
| 551 | dict = static_cast<DictionaryValue*>(tmp_value); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 552 | } |
| 553 | return dict; |
| 554 | } |
| 555 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 556 | ListValue* PrefService::GetMutableList(const char* path) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 557 | DCHECK(CalledOnValidThread()); |
| 558 | |
| 559 | const Preference* pref = FindPreference(path); |
| 560 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 561 | NOTREACHED() << "Trying to get an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 562 | return NULL; |
| 563 | } |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 564 | if (pref->GetType() != Value::TYPE_LIST) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 565 | NOTREACHED() << "Wrong type for GetMutableList: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 566 | return NULL; |
| 567 | } |
| 568 | |
| 569 | ListValue* list = NULL; |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 570 | Value* tmp_value = NULL; |
[email protected] | e025089 | 2010-10-01 18:57:53 | [diff] [blame] | 571 | // Look for an existing preference in the user store. If it doesn't |
| 572 | // exist or isn't the correct type, create a new user preference. |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 573 | if (user_pref_store_->GetValue(path, &tmp_value) |
| 574 | != PersistentPrefStore::READ_OK || |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 575 | !tmp_value->IsType(Value::TYPE_LIST)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 576 | list = new ListValue; |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 577 | user_pref_store_->SetValueSilently(path, list); |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 578 | } else { |
| 579 | list = static_cast<ListValue*>(tmp_value); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 580 | } |
| 581 | return list; |
| 582 | } |
| 583 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 584 | void PrefService::SetUserPrefValue(const char* path, Value* new_value) { |
| 585 | DCHECK(CalledOnValidThread()); |
| 586 | |
| 587 | const Preference* pref = FindPreference(path); |
| 588 | if (!pref) { |
| 589 | NOTREACHED() << "Trying to write an unregistered pref: " << path; |
| 590 | return; |
| 591 | } |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 592 | if (pref->GetType() != new_value->GetType()) { |
| 593 | NOTREACHED() << "Trying to set pref " << path |
| 594 | << " of type " << pref->GetType() |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 595 | << " to value of type " << new_value->GetType(); |
| 596 | return; |
| 597 | } |
| 598 | |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 599 | user_pref_store_->SetValue(path, new_value); |
[email protected] | 73c4793 | 2010-12-06 18:13:43 | [diff] [blame] | 600 | } |
| 601 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 602 | /////////////////////////////////////////////////////////////////////////////// |
| 603 | // PrefService::Preference |
| 604 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 605 | PrefService::Preference::Preference(const PrefService* service, |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 606 | const char* name, |
| 607 | Value::ValueType type) |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 608 | : name_(name), |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 609 | type_(type), |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 610 | pref_service_(service) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 611 | DCHECK(name); |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 612 | DCHECK(service); |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | Value::ValueType PrefService::Preference::GetType() const { |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 616 | return type_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | const Value* PrefService::Preference::GetValue() const { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 620 | DCHECK(pref_service_->FindPreference(name_.c_str())) << |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 621 | "Must register pref before getting its value"; |
| 622 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 623 | Value* found_value = NULL; |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 624 | if (pref_service_->pref_value_store_->GetValue(name_, type_, &found_value)) { |
| 625 | DCHECK(found_value->IsType(type_)); |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 626 | return found_value; |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 627 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 628 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 629 | // Every registered preference has at least a default value. |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 630 | NOTREACHED() << "no valid value found for registered pref " << name_; |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 631 | return NULL; |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | bool PrefService::Preference::IsManaged() const { |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 635 | PrefValueStore* pref_value_store = pref_service_->pref_value_store_; |
[email protected] | be1c6e92 | 2010-11-17 12:49:17 | [diff] [blame] | 636 | return pref_value_store->PrefValueInManagedPlatformStore(name_.c_str()) || |
| 637 | pref_value_store->PrefValueInDeviceManagementStore(name_.c_str()); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | bool PrefService::Preference::HasExtensionSetting() const { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 641 | return pref_service_->pref_value_store_-> |
| 642 | PrefValueInExtensionStore(name_.c_str()); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | bool PrefService::Preference::HasUserSetting() const { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 646 | return pref_service_->pref_value_store_-> |
| 647 | PrefValueInUserStore(name_.c_str()); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | bool PrefService::Preference::IsExtensionControlled() const { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 651 | return pref_service_->pref_value_store_-> |
| 652 | PrefValueFromExtensionStore(name_.c_str()); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | bool PrefService::Preference::IsUserControlled() const { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 656 | return pref_service_->pref_value_store_-> |
| 657 | PrefValueFromUserStore(name_.c_str()); |
| 658 | } |
| 659 | |
| 660 | bool PrefService::Preference::IsDefaultValue() const { |
| 661 | return pref_service_->pref_value_store_-> |
| 662 | PrefValueFromDefaultStore(name_.c_str()); |
[email protected] | d7449e8 | 2010-07-14 11:42:35 | [diff] [blame] | 663 | } |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 664 | |
| 665 | bool PrefService::Preference::IsUserModifiable() const { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 666 | return pref_service_->pref_value_store_-> |
| 667 | PrefValueUserModifiable(name_.c_str()); |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 668 | } |