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