[email protected] | 32c3c75 | 2012-01-05 17:33:47 | [diff] [blame] | 1 | // Copyright (c) 2012 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> |
[email protected] | 1cb92b8 | 2010-03-08 23:12:15 | [diff] [blame] | 8 | |
[email protected] | bebe6943 | 2011-09-28 18:36:45 | [diff] [blame] | 9 | #include "base/bind.h" |
[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] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 16 | #include "base/stl_util.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] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 21 | #include "chrome/browser/browser_process.h" |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 22 | #include "chrome/browser/extensions/extension_pref_store.h" |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 23 | #include "chrome/browser/policy/configuration_policy_pref_store.h" |
| 24 | #include "chrome/browser/prefs/command_line_pref_store.h" |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 25 | #include "chrome/browser/prefs/default_pref_store.h" |
[email protected] | 32c3c75 | 2012-01-05 17:33:47 | [diff] [blame] | 26 | #include "chrome/browser/prefs/overlay_user_pref_store.h" |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 27 | #include "chrome/browser/prefs/pref_model_associator.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] | 32c3c75 | 2012-01-05 17:33:47 | [diff] [blame] | 30 | #include "chrome/browser/ui/prefs/prefs_tab_helper.h" |
[email protected] | c7fb2da3 | 2011-04-14 20:47:10 | [diff] [blame] | 31 | #include "chrome/browser/ui/profile_error_dialog.h" |
[email protected] | e4f8649 | 2011-04-13 12:23:53 | [diff] [blame] | 32 | #include "chrome/common/json_pref_store.h" |
[email protected] | 32c3c75 | 2012-01-05 17:33:47 | [diff] [blame] | 33 | #include "chrome/common/pref_names.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 34 | #include "content/public/browser/browser_thread.h" |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 35 | #include "grit/chromium_strings.h" |
[email protected] | 34ac8f3 | 2009-02-22 23:03:27 | [diff] [blame] | 36 | #include "grit/generated_resources.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 37 | #include "ui/base/l10n/l10n_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 38 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 39 | using content::BrowserThread; |
| 40 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 41 | namespace { |
| 42 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 43 | // A helper function for RegisterLocalized*Pref that creates a Value* based on |
| 44 | // the string value in the locale dll. Because we control the values in a |
| 45 | // locale dll, this should always return a Value of the appropriate type. |
[email protected] | bab1c13f | 2011-08-12 20:59:02 | [diff] [blame] | 46 | Value* CreateLocaleDefaultValue(base::Value::Type type, int message_id) { |
[email protected] | 16b52716 | 2010-08-15 18:37:10 | [diff] [blame] | 47 | std::string resource_string = l10n_util::GetStringUTF8(message_id); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 48 | DCHECK(!resource_string.empty()); |
| 49 | switch (type) { |
| 50 | case Value::TYPE_BOOLEAN: { |
[email protected] | 16b52716 | 2010-08-15 18:37:10 | [diff] [blame] | 51 | if ("true" == resource_string) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 52 | return Value::CreateBooleanValue(true); |
[email protected] | 16b52716 | 2010-08-15 18:37:10 | [diff] [blame] | 53 | if ("false" == resource_string) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 54 | return Value::CreateBooleanValue(false); |
| 55 | break; |
| 56 | } |
| 57 | |
| 58 | case Value::TYPE_INTEGER: { |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 59 | int val; |
[email protected] | 16b52716 | 2010-08-15 18:37:10 | [diff] [blame] | 60 | base::StringToInt(resource_string, &val); |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 61 | return Value::CreateIntegerValue(val); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 62 | } |
| 63 | |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 64 | case Value::TYPE_DOUBLE: { |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 65 | double val; |
[email protected] | 16b52716 | 2010-08-15 18:37:10 | [diff] [blame] | 66 | base::StringToDouble(resource_string, &val); |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 67 | return Value::CreateDoubleValue(val); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | case Value::TYPE_STRING: { |
| 71 | return Value::CreateStringValue(resource_string); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | default: { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 75 | NOTREACHED() << |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 76 | "list and dictionary types cannot have default locale values"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | NOTREACHED(); |
| 80 | return Value::CreateNullValue(); |
| 81 | } |
| 82 | |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 83 | // Forwards a notification after a PostMessage so that we can wait for the |
| 84 | // MessageLoop to run. |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 85 | void NotifyReadError(int message_id) { |
[email protected] | c7fb2da3 | 2011-04-14 20:47:10 | [diff] [blame] | 86 | ShowProfileErrorDialog(message_id); |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 87 | } |
| 88 | |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 89 | // Shows notifications which correspond to PersistentPrefStore's reading errors. |
| 90 | class ReadErrorHandler : public PersistentPrefStore::ReadErrorDelegate { |
| 91 | public: |
| 92 | virtual void OnError(PersistentPrefStore::PrefReadError error) { |
| 93 | if (error != PersistentPrefStore::PREF_READ_ERROR_NONE) { |
| 94 | // Failing to load prefs on startup is a bad thing(TM). See bug 38352 for |
| 95 | // an example problem that this can cause. |
| 96 | // Do some diagnosis and try to avoid losing data. |
| 97 | int message_id = 0; |
| 98 | if (error <= PersistentPrefStore::PREF_READ_ERROR_JSON_TYPE) { |
| 99 | message_id = IDS_PREFERENCES_CORRUPT_ERROR; |
| 100 | } else if (error != PersistentPrefStore::PREF_READ_ERROR_NO_FILE) { |
| 101 | message_id = IDS_PREFERENCES_UNREADABLE_ERROR; |
| 102 | } |
| 103 | |
| 104 | if (message_id) { |
| 105 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
[email protected] | bebe6943 | 2011-09-28 18:36:45 | [diff] [blame] | 106 | base::Bind(&NotifyReadError, message_id)); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 107 | } |
[email protected] | ff3c69a | 2011-09-15 00:36:48 | [diff] [blame] | 108 | UMA_HISTOGRAM_ENUMERATION("PrefService.ReadError", error, |
| 109 | PersistentPrefStore::PREF_READ_ERROR_MAX_ENUM); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | }; |
| 113 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 114 | } // namespace |
| 115 | |
[email protected] | db198b2 | 2010-07-12 16:48:49 | [diff] [blame] | 116 | // static |
[email protected] | 21d3a88 | 2012-05-31 14:41:55 | [diff] [blame^] | 117 | PrefService* PrefService::CreatePrefService( |
| 118 | const FilePath& pref_filename, |
| 119 | policy::PolicyService* policy_service, |
| 120 | PrefStore* extension_prefs, |
| 121 | bool async) { |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 122 | using policy::ConfigurationPolicyPrefStore; |
| 123 | |
[email protected] | 1d01d41 | 2010-08-20 00:36:01 | [diff] [blame] | 124 | #if defined(OS_LINUX) |
| 125 | // We'd like to see what fraction of our users have the preferences |
| 126 | // stored on a network file system, as we've had no end of troubles |
| 127 | // with NFS/AFS. |
| 128 | // TODO(evanm): remove this once we've collected state. |
| 129 | file_util::FileSystemType fstype; |
| 130 | if (file_util::GetFileSystemType(pref_filename.DirName(), &fstype)) { |
| 131 | UMA_HISTOGRAM_ENUMERATION("PrefService.FileSystemType", |
| 132 | static_cast<int>(fstype), |
| 133 | file_util::FILE_SYSTEM_TYPE_COUNT); |
| 134 | } |
| 135 | #endif |
| 136 | |
[email protected] | f31e2e5 | 2011-07-14 16:01:19 | [diff] [blame] | 137 | #if defined(ENABLE_CONFIGURATION_POLICY) |
[email protected] | 4eb4d6ce | 2012-04-02 14:06:57 | [diff] [blame] | 138 | ConfigurationPolicyPrefStore* managed = |
[email protected] | 21d3a88 | 2012-05-31 14:41:55 | [diff] [blame^] | 139 | ConfigurationPolicyPrefStore::CreateMandatoryPolicyPrefStore( |
| 140 | policy_service); |
[email protected] | 4eb4d6ce | 2012-04-02 14:06:57 | [diff] [blame] | 141 | ConfigurationPolicyPrefStore* recommended = |
[email protected] | 21d3a88 | 2012-05-31 14:41:55 | [diff] [blame^] | 142 | ConfigurationPolicyPrefStore::CreateRecommendedPolicyPrefStore( |
| 143 | policy_service); |
[email protected] | f31e2e5 | 2011-07-14 16:01:19 | [diff] [blame] | 144 | #else |
[email protected] | 4eb4d6ce | 2012-04-02 14:06:57 | [diff] [blame] | 145 | ConfigurationPolicyPrefStore* managed = NULL; |
| 146 | ConfigurationPolicyPrefStore* recommended = NULL; |
[email protected] | f31e2e5 | 2011-07-14 16:01:19 | [diff] [blame] | 147 | #endif // ENABLE_CONFIGURATION_POLICY |
| 148 | |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 149 | CommandLinePrefStore* command_line = |
| 150 | new CommandLinePrefStore(CommandLine::ForCurrentProcess()); |
[email protected] | 370bc73 | 2012-05-07 21:25:20 | [diff] [blame] | 151 | JsonPrefStore* user = new JsonPrefStore( |
| 152 | pref_filename, |
| 153 | BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 154 | DefaultPrefStore* default_pref_store = new DefaultPrefStore(); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 155 | |
[email protected] | 361d37f6 | 2011-11-22 10:37:02 | [diff] [blame] | 156 | PrefNotifierImpl* pref_notifier = new PrefNotifierImpl(); |
| 157 | PrefModelAssociator* pref_sync_associator = new PrefModelAssociator(); |
| 158 | |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 159 | return new PrefService( |
[email protected] | 361d37f6 | 2011-11-22 10:37:02 | [diff] [blame] | 160 | pref_notifier, |
| 161 | new PrefValueStore( |
[email protected] | 4eb4d6ce | 2012-04-02 14:06:57 | [diff] [blame] | 162 | managed, |
[email protected] | 361d37f6 | 2011-11-22 10:37:02 | [diff] [blame] | 163 | extension_prefs, |
| 164 | command_line, |
| 165 | user, |
[email protected] | 4eb4d6ce | 2012-04-02 14:06:57 | [diff] [blame] | 166 | recommended, |
[email protected] | 361d37f6 | 2011-11-22 10:37:02 | [diff] [blame] | 167 | default_pref_store, |
| 168 | pref_sync_associator, |
| 169 | pref_notifier), |
| 170 | user, |
| 171 | default_pref_store, |
| 172 | pref_sync_associator, |
| 173 | async); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | PrefService* PrefService::CreateIncognitoPrefService( |
| 177 | PrefStore* incognito_extension_prefs) { |
[email protected] | d3b05ea | 2012-01-24 22:57:05 | [diff] [blame] | 178 | pref_service_forked_ = true; |
[email protected] | 361d37f6 | 2011-11-22 10:37:02 | [diff] [blame] | 179 | PrefNotifierImpl* pref_notifier = new PrefNotifierImpl(); |
[email protected] | 32c3c75 | 2012-01-05 17:33:47 | [diff] [blame] | 180 | OverlayUserPrefStore* incognito_pref_store = |
| 181 | new OverlayUserPrefStore(user_pref_store_.get()); |
| 182 | PrefsTabHelper::InitIncognitoUserPrefStore(incognito_pref_store); |
[email protected] | 361d37f6 | 2011-11-22 10:37:02 | [diff] [blame] | 183 | return new PrefService( |
| 184 | pref_notifier, |
| 185 | pref_value_store_->CloneAndSpecialize( |
[email protected] | 4eb4d6ce | 2012-04-02 14:06:57 | [diff] [blame] | 186 | NULL, // managed |
[email protected] | 361d37f6 | 2011-11-22 10:37:02 | [diff] [blame] | 187 | incognito_extension_prefs, |
[email protected] | d3b05ea | 2012-01-24 22:57:05 | [diff] [blame] | 188 | NULL, // command_line_prefs |
[email protected] | 361d37f6 | 2011-11-22 10:37:02 | [diff] [blame] | 189 | incognito_pref_store, |
[email protected] | 4eb4d6ce | 2012-04-02 14:06:57 | [diff] [blame] | 190 | NULL, // recommended |
[email protected] | 361d37f6 | 2011-11-22 10:37:02 | [diff] [blame] | 191 | default_store_.get(), |
[email protected] | d3b05ea | 2012-01-24 22:57:05 | [diff] [blame] | 192 | NULL, // pref_sync_associator |
[email protected] | 361d37f6 | 2011-11-22 10:37:02 | [diff] [blame] | 193 | pref_notifier), |
| 194 | incognito_pref_store, |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 195 | default_store_.get(), |
[email protected] | 361d37f6 | 2011-11-22 10:37:02 | [diff] [blame] | 196 | NULL, |
| 197 | false); |
| 198 | } |
| 199 | |
[email protected] | 361d37f6 | 2011-11-22 10:37:02 | [diff] [blame] | 200 | PrefService::PrefService(PrefNotifierImpl* pref_notifier, |
| 201 | PrefValueStore* pref_value_store, |
| 202 | PersistentPrefStore* user_prefs, |
| 203 | DefaultPrefStore* default_store, |
| 204 | PrefModelAssociator* pref_sync_associator, |
| 205 | bool async) |
| 206 | : pref_notifier_(pref_notifier), |
| 207 | pref_value_store_(pref_value_store), |
| 208 | user_pref_store_(user_prefs), |
| 209 | default_store_(default_store), |
[email protected] | d3b05ea | 2012-01-24 22:57:05 | [diff] [blame] | 210 | pref_sync_associator_(pref_sync_associator), |
| 211 | pref_service_forked_(false) { |
[email protected] | 361d37f6 | 2011-11-22 10:37:02 | [diff] [blame] | 212 | pref_notifier_->SetPrefService(this); |
| 213 | if (pref_sync_associator_.get()) |
| 214 | pref_sync_associator_->SetPrefService(this); |
| 215 | InitFromStorage(async); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 216 | } |
| 217 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 218 | PrefService::~PrefService() { |
| 219 | DCHECK(CalledOnValidThread()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 220 | STLDeleteContainerPointers(prefs_.begin(), prefs_.end()); |
| 221 | prefs_.clear(); |
[email protected] | a98ce126 | 2011-01-28 13:20:23 | [diff] [blame] | 222 | |
| 223 | // Reset pointers so accesses after destruction reliably crash. |
| 224 | pref_value_store_.reset(); |
| 225 | user_pref_store_ = NULL; |
| 226 | default_store_ = NULL; |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 227 | pref_sync_associator_.reset(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 228 | } |
| 229 | |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 230 | void PrefService::InitFromStorage(bool async) { |
| 231 | if (!async) { |
| 232 | ReadErrorHandler error_handler; |
| 233 | error_handler.OnError(user_pref_store_->ReadPrefs()); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 234 | } else { |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 235 | // Guarantee that initialization happens after this function returned. |
| 236 | MessageLoop::current()->PostTask( |
| 237 | FROM_HERE, |
[email protected] | bebe6943 | 2011-09-28 18:36:45 | [diff] [blame] | 238 | base::Bind(&PersistentPrefStore::ReadPrefsAsync, |
| 239 | user_pref_store_.get(), |
| 240 | new ReadErrorHandler())); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 241 | } |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | bool PrefService::ReloadPersistentPrefs() { |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 245 | return user_pref_store_->ReadPrefs() == |
| 246 | PersistentPrefStore::PREF_READ_ERROR_NONE; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 247 | } |
| 248 | |
[email protected] | 3826fed | 2011-03-25 10:59:56 | [diff] [blame] | 249 | void PrefService::CommitPendingWrite() { |
| 250 | DCHECK(CalledOnValidThread()); |
| 251 | user_pref_store_->CommitPendingWrite(); |
| 252 | } |
| 253 | |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 254 | namespace { |
| 255 | |
| 256 | // If there's no g_browser_process or no local state, return true (for testing). |
[email protected] | d3b05ea | 2012-01-24 22:57:05 | [diff] [blame] | 257 | bool IsLocalStatePrefService(PrefService* prefs) { |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 258 | return (!g_browser_process || |
| 259 | !g_browser_process->local_state() || |
| 260 | g_browser_process->local_state() == prefs); |
| 261 | } |
| 262 | |
| 263 | // If there's no g_browser_process, return true (for testing). |
[email protected] | d3b05ea | 2012-01-24 22:57:05 | [diff] [blame] | 264 | bool IsProfilePrefService(PrefService* prefs) { |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 265 | // TODO(zea): uncomment this once all preferences are only ever registered |
| 266 | // with either the local_state's pref service or the profile's pref service. |
| 267 | // return (!g_browser_process || g_browser_process->local_state() != prefs); |
| 268 | return true; |
| 269 | } |
| 270 | |
| 271 | } // namespace |
| 272 | |
| 273 | |
| 274 | // Local State prefs. |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 275 | void PrefService::RegisterBooleanPref(const char* path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 276 | bool default_value) { |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 277 | // If this fails, the pref service in use is a profile pref service, so the |
| 278 | // sync status must be provided (see profile pref registration calls below). |
| 279 | DCHECK(IsLocalStatePrefService(this)); |
| 280 | RegisterPreference(path, |
| 281 | Value::CreateBooleanValue(default_value), |
| 282 | UNSYNCABLE_PREF); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 283 | } |
| 284 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 285 | void PrefService::RegisterIntegerPref(const char* path, int default_value) { |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 286 | // If this fails, the pref service in use is a profile pref service, so the |
| 287 | // sync status must be provided (see profile pref registration calls below). |
| 288 | DCHECK(IsLocalStatePrefService(this)); |
| 289 | RegisterPreference(path, |
| 290 | Value::CreateIntegerValue(default_value), |
| 291 | UNSYNCABLE_PREF); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 292 | } |
| 293 | |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 294 | void PrefService::RegisterDoublePref(const char* path, double default_value) { |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 295 | // If this fails, the pref service in use is a profile pref service, so the |
| 296 | // sync status must be provided (see profile pref registration calls below). |
| 297 | DCHECK(IsLocalStatePrefService(this)); |
| 298 | RegisterPreference(path, |
| 299 | Value::CreateDoubleValue(default_value), |
| 300 | UNSYNCABLE_PREF); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 301 | } |
| 302 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 303 | void PrefService::RegisterStringPref(const char* path, |
[email protected] | 20ce516d | 2010-06-18 02:20:04 | [diff] [blame] | 304 | const std::string& default_value) { |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 305 | // If this fails, the pref service in use is a profile pref service, so the |
| 306 | // sync status must be provided (see profile pref registration calls below). |
| 307 | DCHECK(IsLocalStatePrefService(this)); |
| 308 | RegisterPreference(path, |
| 309 | Value::CreateStringValue(default_value), |
| 310 | UNSYNCABLE_PREF); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 311 | } |
| 312 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 313 | void PrefService::RegisterFilePathPref(const char* path, |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 314 | const FilePath& default_value) { |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 315 | // If this fails, the pref service in use is a profile pref service, so the |
| 316 | // sync status must be provided (see profile pref registration calls below). |
| 317 | DCHECK(IsLocalStatePrefService(this)); |
| 318 | RegisterPreference(path, |
| 319 | Value::CreateStringValue(default_value.value()), |
| 320 | UNSYNCABLE_PREF); |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 321 | } |
| 322 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 323 | void PrefService::RegisterListPref(const char* path) { |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 324 | // If this fails, the pref service in use is a profile pref service, so the |
| 325 | // sync status must be provided (see profile pref registration calls below). |
| 326 | DCHECK(IsLocalStatePrefService(this)); |
| 327 | RegisterPreference(path, |
| 328 | new ListValue(), |
| 329 | UNSYNCABLE_PREF); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 330 | } |
| 331 | |
[email protected] | c2f23d01 | 2011-02-09 14:52:17 | [diff] [blame] | 332 | void PrefService::RegisterListPref(const char* path, ListValue* default_value) { |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 333 | // If this fails, the pref service in use is a profile pref service, so the |
| 334 | // sync status must be provided (see profile pref registration calls below). |
| 335 | DCHECK(IsLocalStatePrefService(this)); |
| 336 | RegisterPreference(path, |
| 337 | default_value, |
| 338 | UNSYNCABLE_PREF); |
[email protected] | c2f23d01 | 2011-02-09 14:52:17 | [diff] [blame] | 339 | } |
| 340 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 341 | void PrefService::RegisterDictionaryPref(const char* path) { |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 342 | // If this fails, the pref service in use is a profile pref service, so the |
| 343 | // sync status must be provided (see profile pref registration calls below). |
| 344 | DCHECK(IsLocalStatePrefService(this)); |
| 345 | RegisterPreference(path, |
| 346 | new DictionaryValue(), |
| 347 | UNSYNCABLE_PREF); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 348 | } |
| 349 | |
[email protected] | c2f23d01 | 2011-02-09 14:52:17 | [diff] [blame] | 350 | void PrefService::RegisterDictionaryPref(const char* path, |
| 351 | DictionaryValue* default_value) { |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 352 | // If this fails, the pref service in use is a profile pref service, so the |
| 353 | // sync status must be provided (see profile pref registration calls below). |
| 354 | DCHECK(IsLocalStatePrefService(this)); |
| 355 | RegisterPreference(path, |
| 356 | default_value, |
| 357 | UNSYNCABLE_PREF); |
[email protected] | c2f23d01 | 2011-02-09 14:52:17 | [diff] [blame] | 358 | } |
| 359 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 360 | void PrefService::RegisterLocalizedBooleanPref(const char* path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 361 | int locale_default_message_id) { |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 362 | // If this fails, the pref service in use is a profile pref service, so the |
| 363 | // sync status must be provided (see profile pref registration calls below). |
| 364 | DCHECK(IsLocalStatePrefService(this)); |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 365 | RegisterPreference( |
| 366 | path, |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 367 | CreateLocaleDefaultValue(Value::TYPE_BOOLEAN, locale_default_message_id), |
| 368 | UNSYNCABLE_PREF); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 369 | } |
| 370 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 371 | void PrefService::RegisterLocalizedIntegerPref(const char* path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 372 | int locale_default_message_id) { |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 373 | // If this fails, the pref service in use is a profile pref service, so the |
| 374 | // sync status must be provided (see profile pref registration calls below). |
| 375 | DCHECK(IsLocalStatePrefService(this)); |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 376 | RegisterPreference( |
| 377 | path, |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 378 | CreateLocaleDefaultValue(Value::TYPE_INTEGER, locale_default_message_id), |
| 379 | UNSYNCABLE_PREF); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 380 | } |
| 381 | |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 382 | void PrefService::RegisterLocalizedDoublePref(const char* path, |
| 383 | int locale_default_message_id) { |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 384 | // If this fails, the pref service in use is a profile pref service, so the |
| 385 | // sync status must be provided (see profile pref registration calls below). |
| 386 | DCHECK(IsLocalStatePrefService(this)); |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 387 | RegisterPreference( |
| 388 | path, |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 389 | CreateLocaleDefaultValue(Value::TYPE_DOUBLE, locale_default_message_id), |
| 390 | UNSYNCABLE_PREF); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 391 | } |
| 392 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 393 | void PrefService::RegisterLocalizedStringPref(const char* path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 394 | int locale_default_message_id) { |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 395 | // If this fails, the pref service in use is a profile pref service, so the |
| 396 | // sync status must be provided (see profile pref registration calls below). |
| 397 | DCHECK(IsLocalStatePrefService(this)); |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 398 | RegisterPreference( |
| 399 | path, |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 400 | CreateLocaleDefaultValue(Value::TYPE_STRING, locale_default_message_id), |
| 401 | UNSYNCABLE_PREF); |
| 402 | } |
| 403 | |
| 404 | void PrefService::RegisterInt64Pref(const char* path, int64 default_value) { |
| 405 | // If this fails, the pref service in use is a profile pref service, so the |
| 406 | // sync status must be provided (see profile pref registration calls below). |
| 407 | DCHECK(IsLocalStatePrefService(this)); |
| 408 | RegisterPreference( |
| 409 | path, |
| 410 | Value::CreateStringValue(base::Int64ToString(default_value)), |
| 411 | UNSYNCABLE_PREF); |
| 412 | } |
| 413 | |
| 414 | // Profile prefs (must use the sync_status variable). |
| 415 | void PrefService::RegisterBooleanPref(const char* path, |
| 416 | bool default_value, |
| 417 | PrefSyncStatus sync_status) { |
| 418 | DCHECK(IsProfilePrefService(this)); |
| 419 | RegisterPreference(path, |
| 420 | Value::CreateBooleanValue(default_value), |
| 421 | sync_status); |
| 422 | } |
| 423 | |
| 424 | void PrefService::RegisterIntegerPref(const char* path, |
| 425 | int default_value, |
| 426 | PrefSyncStatus sync_status) { |
| 427 | DCHECK(IsProfilePrefService(this)); |
| 428 | RegisterPreference(path, |
| 429 | Value::CreateIntegerValue(default_value), |
| 430 | sync_status); |
| 431 | } |
| 432 | |
| 433 | void PrefService::RegisterDoublePref(const char* path, |
| 434 | double default_value, |
| 435 | PrefSyncStatus sync_status) { |
| 436 | DCHECK(IsProfilePrefService(this)); |
| 437 | RegisterPreference(path, |
| 438 | Value::CreateDoubleValue(default_value), |
| 439 | sync_status); |
| 440 | } |
| 441 | |
| 442 | void PrefService::RegisterStringPref(const char* path, |
| 443 | const std::string& default_value, |
| 444 | PrefSyncStatus sync_status) { |
| 445 | DCHECK(IsProfilePrefService(this)); |
| 446 | RegisterPreference(path, |
| 447 | Value::CreateStringValue(default_value), |
| 448 | sync_status); |
| 449 | } |
| 450 | |
| 451 | void PrefService::RegisterFilePathPref(const char* path, |
| 452 | const FilePath& default_value, |
| 453 | PrefSyncStatus sync_status) { |
| 454 | DCHECK(IsProfilePrefService(this)); |
| 455 | RegisterPreference(path, |
| 456 | Value::CreateStringValue(default_value.value()), |
| 457 | sync_status); |
| 458 | } |
| 459 | |
| 460 | void PrefService::RegisterListPref(const char* path, |
| 461 | PrefSyncStatus sync_status) { |
| 462 | DCHECK(IsProfilePrefService(this)); |
| 463 | RegisterPreference(path, new ListValue(), sync_status); |
| 464 | } |
| 465 | |
| 466 | void PrefService::RegisterListPref(const char* path, |
| 467 | ListValue* default_value, |
| 468 | PrefSyncStatus sync_status) { |
| 469 | DCHECK(IsProfilePrefService(this)); |
| 470 | RegisterPreference(path, default_value, sync_status); |
| 471 | } |
| 472 | |
| 473 | void PrefService::RegisterDictionaryPref(const char* path, |
| 474 | PrefSyncStatus sync_status) { |
| 475 | DCHECK(IsProfilePrefService(this)); |
| 476 | RegisterPreference(path, new DictionaryValue(), sync_status); |
| 477 | } |
| 478 | |
| 479 | void PrefService::RegisterDictionaryPref(const char* path, |
| 480 | DictionaryValue* default_value, |
| 481 | PrefSyncStatus sync_status) { |
| 482 | DCHECK(IsProfilePrefService(this)); |
| 483 | RegisterPreference(path, default_value, sync_status); |
| 484 | } |
| 485 | |
| 486 | void PrefService::RegisterLocalizedBooleanPref(const char* path, |
| 487 | int locale_default_message_id, |
| 488 | PrefSyncStatus sync_status) { |
| 489 | DCHECK(IsProfilePrefService(this)); |
| 490 | RegisterPreference( |
| 491 | path, |
[email protected] | d3b05ea | 2012-01-24 22:57:05 | [diff] [blame] | 492 | CreateLocaleDefaultValue(Value::TYPE_BOOLEAN, locale_default_message_id), |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 493 | sync_status); |
| 494 | } |
| 495 | |
| 496 | void PrefService::RegisterLocalizedIntegerPref(const char* path, |
| 497 | int locale_default_message_id, |
| 498 | PrefSyncStatus sync_status) { |
| 499 | DCHECK(IsProfilePrefService(this)); |
| 500 | RegisterPreference( |
| 501 | path, |
| 502 | CreateLocaleDefaultValue(Value::TYPE_INTEGER, locale_default_message_id), |
| 503 | sync_status); |
| 504 | } |
| 505 | |
| 506 | void PrefService::RegisterLocalizedDoublePref(const char* path, |
| 507 | int locale_default_message_id, |
| 508 | PrefSyncStatus sync_status) { |
| 509 | DCHECK(IsProfilePrefService(this)); |
| 510 | RegisterPreference( |
| 511 | path, |
| 512 | CreateLocaleDefaultValue(Value::TYPE_DOUBLE, locale_default_message_id), |
| 513 | sync_status); |
| 514 | } |
| 515 | |
| 516 | void PrefService::RegisterLocalizedStringPref(const char* path, |
| 517 | int locale_default_message_id, |
| 518 | PrefSyncStatus sync_status) { |
| 519 | DCHECK(IsProfilePrefService(this)); |
| 520 | RegisterPreference( |
| 521 | path, |
| 522 | CreateLocaleDefaultValue(Value::TYPE_STRING, locale_default_message_id), |
| 523 | sync_status); |
| 524 | } |
| 525 | |
| 526 | void PrefService::RegisterInt64Pref(const char* path, |
| 527 | int64 default_value, |
| 528 | PrefSyncStatus sync_status) { |
| 529 | DCHECK(IsProfilePrefService(this)); |
| 530 | RegisterPreference( |
| 531 | path, |
| 532 | Value::CreateStringValue(base::Int64ToString(default_value)), |
| 533 | sync_status); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 534 | } |
| 535 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 536 | bool PrefService::GetBoolean(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 537 | DCHECK(CalledOnValidThread()); |
| 538 | |
| 539 | bool result = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 540 | |
| 541 | const Preference* pref = FindPreference(path); |
| 542 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 543 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 544 | return result; |
| 545 | } |
| 546 | bool rv = pref->GetValue()->GetAsBoolean(&result); |
| 547 | DCHECK(rv); |
| 548 | return result; |
| 549 | } |
| 550 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 551 | int PrefService::GetInteger(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 552 | DCHECK(CalledOnValidThread()); |
| 553 | |
| 554 | int result = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 555 | |
| 556 | const Preference* pref = FindPreference(path); |
| 557 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 558 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 559 | return result; |
| 560 | } |
| 561 | bool rv = pref->GetValue()->GetAsInteger(&result); |
| 562 | DCHECK(rv); |
| 563 | return result; |
| 564 | } |
| 565 | |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 566 | double PrefService::GetDouble(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 567 | DCHECK(CalledOnValidThread()); |
| 568 | |
| 569 | double result = 0.0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 570 | |
| 571 | const Preference* pref = FindPreference(path); |
| 572 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 573 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 574 | return result; |
| 575 | } |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 576 | bool rv = pref->GetValue()->GetAsDouble(&result); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 577 | DCHECK(rv); |
| 578 | return result; |
| 579 | } |
| 580 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 581 | std::string PrefService::GetString(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 582 | DCHECK(CalledOnValidThread()); |
| 583 | |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 584 | std::string result; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 585 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 586 | const Preference* pref = FindPreference(path); |
| 587 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 588 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 589 | return result; |
| 590 | } |
| 591 | bool rv = pref->GetValue()->GetAsString(&result); |
| 592 | DCHECK(rv); |
| 593 | return result; |
| 594 | } |
| 595 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 596 | FilePath PrefService::GetFilePath(const char* path) const { |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 597 | DCHECK(CalledOnValidThread()); |
| 598 | |
[email protected] | 68d9d35 | 2011-02-21 16:35:04 | [diff] [blame] | 599 | FilePath result; |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 600 | |
| 601 | const Preference* pref = FindPreference(path); |
| 602 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 603 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 604 | return FilePath(result); |
| 605 | } |
[email protected] | 8703b2b | 2011-03-15 09:51:50 | [diff] [blame] | 606 | bool rv = base::GetValueAsFilePath(*pref->GetValue(), &result); |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 607 | DCHECK(rv); |
[email protected] | 68d9d35 | 2011-02-21 16:35:04 | [diff] [blame] | 608 | return result; |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 609 | } |
| 610 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 611 | bool PrefService::HasPrefPath(const char* path) const { |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 612 | const Preference* pref = FindPreference(path); |
| 613 | return pref && !pref->IsDefaultValue(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 614 | } |
| 615 | |
[email protected] | ebd0b02 | 2011-01-27 13:24:14 | [diff] [blame] | 616 | DictionaryValue* PrefService::GetPreferenceValues() const { |
| 617 | DCHECK(CalledOnValidThread()); |
| 618 | DictionaryValue* out = new DictionaryValue; |
| 619 | DefaultPrefStore::const_iterator i = default_store_->begin(); |
| 620 | for (; i != default_store_->end(); ++i) { |
[email protected] | 8874e0217 | 2011-03-11 19:44:08 | [diff] [blame] | 621 | const Preference* pref = FindPreference(i->first.c_str()); |
| 622 | DCHECK(pref); |
| 623 | const Value* value = pref->GetValue(); |
| 624 | DCHECK(value); |
[email protected] | ebd0b02 | 2011-01-27 13:24:14 | [diff] [blame] | 625 | out->Set(i->first, value->DeepCopy()); |
| 626 | } |
| 627 | return out; |
| 628 | } |
| 629 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 630 | const PrefService::Preference* PrefService::FindPreference( |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 631 | const char* pref_name) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 632 | DCHECK(CalledOnValidThread()); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 633 | Preference p(this, pref_name, Value::TYPE_NULL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 634 | PreferenceSet::const_iterator it = prefs_.find(&p); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 635 | if (it != prefs_.end()) |
| 636 | return *it; |
[email protected] | bab1c13f | 2011-08-12 20:59:02 | [diff] [blame] | 637 | const base::Value::Type type = default_store_->GetType(pref_name); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 638 | if (type == Value::TYPE_NULL) |
| 639 | return NULL; |
| 640 | Preference* new_pref = new Preference(this, pref_name, type); |
| 641 | prefs_.insert(new_pref); |
| 642 | return new_pref; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 643 | } |
| 644 | |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 645 | bool PrefService::ReadOnly() const { |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 646 | return user_pref_store_->ReadOnly(); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 647 | } |
| 648 | |
[email protected] | 59c1071 | 2012-03-13 02:10:34 | [diff] [blame] | 649 | PrefService::PrefInitializationStatus PrefService::GetInitializationStatus() |
| 650 | const { |
| 651 | if (!user_pref_store_->IsInitializationComplete()) |
| 652 | return INITIALIZATION_STATUS_WAITING; |
| 653 | |
| 654 | switch (user_pref_store_->GetReadError()) { |
| 655 | case PersistentPrefStore::PREF_READ_ERROR_NONE: |
| 656 | return INITIALIZATION_STATUS_SUCCESS; |
| 657 | case PersistentPrefStore::PREF_READ_ERROR_NO_FILE: |
| 658 | return INITIALIZATION_STATUS_CREATED_NEW_PROFILE; |
| 659 | default: |
| 660 | return INITIALIZATION_STATUS_ERROR; |
| 661 | } |
| 662 | } |
| 663 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 664 | bool PrefService::IsManagedPreference(const char* pref_name) const { |
[email protected] | d90de1c0 | 2010-07-19 19:50:48 | [diff] [blame] | 665 | const Preference* pref = FindPreference(pref_name); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 666 | return pref && pref->IsManaged(); |
[email protected] | d90de1c0 | 2010-07-19 19:50:48 | [diff] [blame] | 667 | } |
| 668 | |
[email protected] | d6bbd293 | 2012-03-19 17:10:07 | [diff] [blame] | 669 | bool PrefService::IsUserModifiablePreference(const char* pref_name) const { |
| 670 | const Preference* pref = FindPreference(pref_name); |
| 671 | return pref && pref->IsUserModifiable(); |
| 672 | } |
| 673 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 674 | const DictionaryValue* PrefService::GetDictionary(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 675 | DCHECK(CalledOnValidThread()); |
| 676 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 677 | const Preference* pref = FindPreference(path); |
| 678 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 679 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 680 | return NULL; |
| 681 | } |
| 682 | const Value* value = pref->GetValue(); |
[email protected] | 11b040b | 2011-02-02 12:42:25 | [diff] [blame] | 683 | if (value->GetType() != Value::TYPE_DICTIONARY) { |
| 684 | NOTREACHED(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 685 | return NULL; |
[email protected] | 11b040b | 2011-02-02 12:42:25 | [diff] [blame] | 686 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 687 | return static_cast<const DictionaryValue*>(value); |
| 688 | } |
| 689 | |
[email protected] | 1a5a31f | 2012-04-26 20:21:34 | [diff] [blame] | 690 | const base::Value* PrefService::GetUserPrefValue(const char* path) const { |
| 691 | DCHECK(CalledOnValidThread()); |
| 692 | |
| 693 | const Preference* pref = FindPreference(path); |
| 694 | if (!pref) { |
| 695 | NOTREACHED() << "Trying to get an unregistered pref: " << path; |
| 696 | return NULL; |
| 697 | } |
| 698 | |
| 699 | // Look for an existing preference in the user store. If it doesn't |
| 700 | // exist, return NULL. |
| 701 | base::Value* value = NULL; |
[email protected] | 35a6fd1 | 2012-05-28 18:08:08 | [diff] [blame] | 702 | if (user_pref_store_->GetMutableValue(path, &value) != PrefStore::READ_OK) |
[email protected] | 1a5a31f | 2012-04-26 20:21:34 | [diff] [blame] | 703 | return NULL; |
[email protected] | 1a5a31f | 2012-04-26 20:21:34 | [diff] [blame] | 704 | |
| 705 | if (!value->IsType(pref->GetType())) { |
| 706 | NOTREACHED() << "Pref value type doesn't match registered type."; |
| 707 | return NULL; |
| 708 | } |
| 709 | |
| 710 | return value; |
| 711 | } |
| 712 | |
[email protected] | 35a6fd1 | 2012-05-28 18:08:08 | [diff] [blame] | 713 | const base::Value* PrefService::GetDefaultPrefValue(const char* path) const { |
| 714 | DCHECK(CalledOnValidThread()); |
| 715 | // Lookup the preference in the default store. |
| 716 | const base::Value* value = NULL; |
| 717 | if (default_store_->GetValue(path, &value) != PrefStore::READ_OK) { |
| 718 | NOTREACHED() << "Default value missing for pref: " << path; |
| 719 | return NULL; |
| 720 | } |
| 721 | return value; |
| 722 | } |
| 723 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 724 | const ListValue* PrefService::GetList(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 725 | DCHECK(CalledOnValidThread()); |
| 726 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 727 | const Preference* pref = FindPreference(path); |
| 728 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 729 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 730 | return NULL; |
| 731 | } |
| 732 | const Value* value = pref->GetValue(); |
[email protected] | 11b040b | 2011-02-02 12:42:25 | [diff] [blame] | 733 | if (value->GetType() != Value::TYPE_LIST) { |
| 734 | NOTREACHED(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 735 | return NULL; |
[email protected] | 11b040b | 2011-02-02 12:42:25 | [diff] [blame] | 736 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 737 | return static_cast<const ListValue*>(value); |
| 738 | } |
| 739 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 740 | void PrefService::AddPrefObserver(const char* path, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 741 | content::NotificationObserver* obs) { |
[email protected] | d81288a0 | 2010-08-18 07:25:50 | [diff] [blame] | 742 | pref_notifier_->AddPrefObserver(path, obs); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 743 | } |
| 744 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 745 | void PrefService::RemovePrefObserver(const char* path, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 746 | content::NotificationObserver* obs) { |
[email protected] | d81288a0 | 2010-08-18 07:25:50 | [diff] [blame] | 747 | pref_notifier_->RemovePrefObserver(path, obs); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 748 | } |
| 749 | |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 750 | void PrefService::RegisterPreference(const char* path, |
| 751 | Value* default_value, |
| 752 | PrefSyncStatus sync_status) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 753 | DCHECK(CalledOnValidThread()); |
| 754 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 755 | // The main code path takes ownership, but most don't. We'll be safe. |
| 756 | scoped_ptr<Value> scoped_value(default_value); |
| 757 | |
| 758 | if (FindPreference(path)) { |
| 759 | NOTREACHED() << "Tried to register duplicate pref " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 760 | return; |
| 761 | } |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 762 | |
[email protected] | bab1c13f | 2011-08-12 20:59:02 | [diff] [blame] | 763 | base::Value::Type orig_type = default_value->GetType(); |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 764 | DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) << |
| 765 | "invalid preference type: " << orig_type; |
| 766 | |
[email protected] | ea3e497 | 2012-04-12 03:41:37 | [diff] [blame] | 767 | // For ListValue and DictionaryValue with non empty default, empty value |
| 768 | // for |path| needs to be persisted in |user_pref_store_|. So that |
| 769 | // non empty default is not used when user sets an empty ListValue or |
| 770 | // DictionaryValue. |
| 771 | bool needs_empty_value = false; |
| 772 | if (orig_type == base::Value::TYPE_LIST) { |
| 773 | const base::ListValue* list = NULL; |
| 774 | if (default_value->GetAsList(&list) && !list->empty()) |
| 775 | needs_empty_value = true; |
| 776 | } else if (orig_type == base::Value::TYPE_DICTIONARY) { |
| 777 | const base::DictionaryValue* dict = NULL; |
| 778 | if (default_value->GetAsDictionary(&dict) && !dict->empty()) |
| 779 | needs_empty_value = true; |
| 780 | } |
| 781 | if (needs_empty_value) |
| 782 | user_pref_store_->MarkNeedsEmptyValue(path); |
| 783 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 784 | // Hand off ownership. |
| 785 | default_store_->SetDefaultValue(path, scoped_value.release()); |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 786 | |
| 787 | // Register with sync if necessary. |
| 788 | if (sync_status == SYNCABLE_PREF && pref_sync_associator_.get()) |
| 789 | pref_sync_associator_->RegisterPref(path); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 790 | } |
| 791 | |
[email protected] | 7a5f593 | 2011-12-29 10:35:49 | [diff] [blame] | 792 | void PrefService::UnregisterPreference(const char* path) { |
| 793 | DCHECK(CalledOnValidThread()); |
| 794 | |
| 795 | Preference p(this, path, Value::TYPE_NULL); |
[email protected] | 398007ce | 2012-01-05 19:25:50 | [diff] [blame] | 796 | PreferenceSet::iterator it = prefs_.find(&p); |
[email protected] | 7a5f593 | 2011-12-29 10:35:49 | [diff] [blame] | 797 | if (it == prefs_.end()) { |
| 798 | NOTREACHED() << "Trying to unregister an unregistered pref: " << path; |
| 799 | return; |
| 800 | } |
| 801 | |
[email protected] | 5a56d37 | 2011-12-29 11:34:40 | [diff] [blame] | 802 | delete *it; |
[email protected] | 7a5f593 | 2011-12-29 10:35:49 | [diff] [blame] | 803 | prefs_.erase(it); |
| 804 | default_store_->RemoveDefaultValue(path); |
| 805 | if (pref_sync_associator_.get() && |
| 806 | pref_sync_associator_->IsPrefRegistered(path)) { |
| 807 | pref_sync_associator_->UnregisterPref(path); |
| 808 | } |
| 809 | } |
| 810 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 811 | void PrefService::ClearPref(const char* path) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 812 | DCHECK(CalledOnValidThread()); |
| 813 | |
| 814 | const Preference* pref = FindPreference(path); |
| 815 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 816 | NOTREACHED() << "Trying to clear an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 817 | return; |
| 818 | } |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 819 | user_pref_store_->RemoveValue(path); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 820 | } |
| 821 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 822 | void PrefService::Set(const char* path, const Value& value) { |
[email protected] | b99c41c | 2011-04-27 15:18:48 | [diff] [blame] | 823 | SetUserPrefValue(path, value.DeepCopy()); |
[email protected] | a048d7e4 | 2009-12-01 01:02:39 | [diff] [blame] | 824 | } |
| 825 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 826 | void PrefService::SetBoolean(const char* path, bool value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 827 | SetUserPrefValue(path, Value::CreateBooleanValue(value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 828 | } |
| 829 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 830 | void PrefService::SetInteger(const char* path, int value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 831 | SetUserPrefValue(path, Value::CreateIntegerValue(value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 832 | } |
| 833 | |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 834 | void PrefService::SetDouble(const char* path, double value) { |
| 835 | SetUserPrefValue(path, Value::CreateDoubleValue(value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 836 | } |
| 837 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 838 | void PrefService::SetString(const char* path, const std::string& value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 839 | SetUserPrefValue(path, Value::CreateStringValue(value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 840 | } |
| 841 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 842 | void PrefService::SetFilePath(const char* path, const FilePath& value) { |
[email protected] | 8703b2b | 2011-03-15 09:51:50 | [diff] [blame] | 843 | SetUserPrefValue(path, base::CreateFilePathValue(value)); |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 844 | } |
| 845 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 846 | void PrefService::SetInt64(const char* path, int64 value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 847 | SetUserPrefValue(path, Value::CreateStringValue(base::Int64ToString(value))); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 848 | } |
| 849 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 850 | int64 PrefService::GetInt64(const char* path) const { |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 851 | DCHECK(CalledOnValidThread()); |
| 852 | |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 853 | const Preference* pref = FindPreference(path); |
| 854 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 855 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
[email protected] | c345330 | 2009-12-01 01:33:08 | [diff] [blame] | 856 | return 0; |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 857 | } |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 858 | std::string result("0"); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 859 | bool rv = pref->GetValue()->GetAsString(&result); |
| 860 | DCHECK(rv); |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 861 | |
| 862 | int64 val; |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 863 | base::StringToInt64(result, &val); |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 864 | return val; |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 865 | } |
| 866 | |
[email protected] | 26418b7 | 2011-03-30 14:07:39 | [diff] [blame] | 867 | Value* PrefService::GetMutableUserPref(const char* path, |
[email protected] | bab1c13f | 2011-08-12 20:59:02 | [diff] [blame] | 868 | base::Value::Type type) { |
[email protected] | 26418b7 | 2011-03-30 14:07:39 | [diff] [blame] | 869 | CHECK(type == Value::TYPE_DICTIONARY || type == Value::TYPE_LIST); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 870 | DCHECK(CalledOnValidThread()); |
| 871 | |
| 872 | const Preference* pref = FindPreference(path); |
| 873 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 874 | NOTREACHED() << "Trying to get an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 875 | return NULL; |
| 876 | } |
[email protected] | 26418b7 | 2011-03-30 14:07:39 | [diff] [blame] | 877 | if (pref->GetType() != type) { |
| 878 | NOTREACHED() << "Wrong type for GetMutableValue: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 879 | return NULL; |
| 880 | } |
| 881 | |
[email protected] | e025089 | 2010-10-01 18:57:53 | [diff] [blame] | 882 | // Look for an existing preference in the user store. If it doesn't |
| 883 | // exist or isn't the correct type, create a new user preference. |
[email protected] | 26418b7 | 2011-03-30 14:07:39 | [diff] [blame] | 884 | Value* value = NULL; |
[email protected] | 35a6fd1 | 2012-05-28 18:08:08 | [diff] [blame] | 885 | if (user_pref_store_->GetMutableValue(path, &value) != PrefStore::READ_OK || |
[email protected] | 26418b7 | 2011-03-30 14:07:39 | [diff] [blame] | 886 | !value->IsType(type)) { |
| 887 | if (type == Value::TYPE_DICTIONARY) { |
| 888 | value = new DictionaryValue; |
| 889 | } else if (type == Value::TYPE_LIST) { |
| 890 | value = new ListValue; |
| 891 | } else { |
| 892 | NOTREACHED(); |
| 893 | } |
| 894 | user_pref_store_->SetValueSilently(path, value); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 895 | } |
[email protected] | 26418b7 | 2011-03-30 14:07:39 | [diff] [blame] | 896 | return value; |
| 897 | } |
| 898 | |
[email protected] | 68bf41a | 2011-03-25 16:38:31 | [diff] [blame] | 899 | void PrefService::ReportUserPrefChanged(const std::string& key) { |
[email protected] | f89ee34 | 2011-03-07 09:28:27 | [diff] [blame] | 900 | user_pref_store_->ReportValueChanged(key); |
| 901 | } |
| 902 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 903 | void PrefService::SetUserPrefValue(const char* path, Value* new_value) { |
[email protected] | b99c41c | 2011-04-27 15:18:48 | [diff] [blame] | 904 | scoped_ptr<Value> owned_value(new_value); |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 905 | DCHECK(CalledOnValidThread()); |
| 906 | |
| 907 | const Preference* pref = FindPreference(path); |
| 908 | if (!pref) { |
| 909 | NOTREACHED() << "Trying to write an unregistered pref: " << path; |
| 910 | return; |
| 911 | } |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 912 | if (pref->GetType() != new_value->GetType()) { |
| 913 | NOTREACHED() << "Trying to set pref " << path |
| 914 | << " of type " << pref->GetType() |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 915 | << " to value of type " << new_value->GetType(); |
| 916 | return; |
| 917 | } |
| 918 | |
[email protected] | b99c41c | 2011-04-27 15:18:48 | [diff] [blame] | 919 | user_pref_store_->SetValue(path, owned_value.release()); |
[email protected] | 73c4793 | 2010-12-06 18:13:43 | [diff] [blame] | 920 | } |
| 921 | |
[email protected] | d36f941b | 2011-05-09 06:19:16 | [diff] [blame] | 922 | SyncableService* PrefService::GetSyncableService() { |
| 923 | return pref_sync_associator_.get(); |
| 924 | } |
| 925 | |
[email protected] | d3b05ea | 2012-01-24 22:57:05 | [diff] [blame] | 926 | void PrefService::UpdateCommandLinePrefStore(CommandLine* command_line) { |
| 927 | // If |pref_service_forked_| is true, then this PrefService and the forked |
| 928 | // copies will be out of sync. |
| 929 | DCHECK(!pref_service_forked_); |
| 930 | pref_value_store_->UpdateCommandLinePrefStore( |
| 931 | new CommandLinePrefStore(command_line)); |
| 932 | } |
| 933 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 934 | /////////////////////////////////////////////////////////////////////////////// |
| 935 | // PrefService::Preference |
| 936 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 937 | PrefService::Preference::Preference(const PrefService* service, |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 938 | const char* name, |
[email protected] | bab1c13f | 2011-08-12 20:59:02 | [diff] [blame] | 939 | base::Value::Type type) |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 940 | : name_(name), |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 941 | type_(type), |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 942 | pref_service_(service) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 943 | DCHECK(name); |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 944 | DCHECK(service); |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 945 | } |
| 946 | |
[email protected] | bab1c13f | 2011-08-12 20:59:02 | [diff] [blame] | 947 | base::Value::Type PrefService::Preference::GetType() const { |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 948 | return type_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | const Value* PrefService::Preference::GetValue() const { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 952 | DCHECK(pref_service_->FindPreference(name_.c_str())) << |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 953 | "Must register pref before getting its value"; |
| 954 | |
[email protected] | 68bf41a | 2011-03-25 16:38:31 | [diff] [blame] | 955 | const Value* found_value = NULL; |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 956 | if (pref_value_store()->GetValue(name_, type_, &found_value)) { |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 957 | DCHECK(found_value->IsType(type_)); |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 958 | return found_value; |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 959 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 960 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 961 | // Every registered preference has at least a default value. |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 962 | NOTREACHED() << "no valid value found for registered pref " << name_; |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 963 | return NULL; |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | bool PrefService::Preference::IsManaged() const { |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 967 | return pref_value_store()->PrefValueInManagedStore(name_.c_str()); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 968 | } |
| 969 | |
[email protected] | a543728 | 2011-12-12 12:33:21 | [diff] [blame] | 970 | bool PrefService::Preference::IsRecommended() const { |
| 971 | return pref_value_store()->PrefValueFromRecommendedStore(name_.c_str()); |
| 972 | } |
| 973 | |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 974 | bool PrefService::Preference::HasExtensionSetting() const { |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 975 | return pref_value_store()->PrefValueInExtensionStore(name_.c_str()); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | bool PrefService::Preference::HasUserSetting() const { |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 979 | return pref_value_store()->PrefValueInUserStore(name_.c_str()); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | bool PrefService::Preference::IsExtensionControlled() const { |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 983 | return pref_value_store()->PrefValueFromExtensionStore(name_.c_str()); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | bool PrefService::Preference::IsUserControlled() const { |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 987 | return pref_value_store()->PrefValueFromUserStore(name_.c_str()); |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | bool PrefService::Preference::IsDefaultValue() const { |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 991 | return pref_value_store()->PrefValueFromDefaultStore(name_.c_str()); |
[email protected] | d7449e8 | 2010-07-14 11:42:35 | [diff] [blame] | 992 | } |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 993 | |
| 994 | bool PrefService::Preference::IsUserModifiable() const { |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 995 | return pref_value_store()->PrefValueUserModifiable(name_.c_str()); |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 996 | } |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 997 | |
| 998 | bool PrefService::Preference::IsExtensionModifiable() const { |
| 999 | return pref_value_store()->PrefValueExtensionModifiable(name_.c_str()); |
| 1000 | } |