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