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