[email protected] | c0858b0 | 2010-05-12 15:03:32 | [diff] [blame] | 1 | // Copyright (c) 2010 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] | a92b864 | 2009-05-05 23:38:56 | [diff] [blame] | 10 | #include "app/l10n_util.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" |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 14 | #include "base/histogram.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | #include "base/logging.h" |
| 16 | #include "base/message_loop.h" |
[email protected] | 80720414 | 2009-05-05 03:31:44 | [diff] [blame] | 17 | #include "base/stl_util-inl.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] | 66de4f09 | 2009-09-04 23:59:40 | [diff] [blame] | 20 | #include "base/sys_string_conversions.h" |
[email protected] | 1cb92b8 | 2010-03-08 23:12:15 | [diff] [blame] | 21 | #include "base/utf_string_conversions.h" |
[email protected] | 66de4f09 | 2009-09-04 23:59:40 | [diff] [blame] | 22 | #include "build/build_config.h" |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 23 | #include "chrome/browser/chrome_thread.h" |
[email protected] | fa26b3d5 | 2010-08-06 08:51:50 | [diff] [blame] | 24 | #include "chrome/browser/profile.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 25 | #include "chrome/common/notification_service.h" |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 26 | #include "grit/chromium_strings.h" |
[email protected] | 34ac8f3 | 2009-02-22 23:03:27 | [diff] [blame] | 27 | #include "grit/generated_resources.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 28 | |
| 29 | namespace { |
| 30 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 31 | // A helper function for RegisterLocalized*Pref that creates a Value* based on |
| 32 | // the string value in the locale dll. Because we control the values in a |
| 33 | // locale dll, this should always return a Value of the appropriate type. |
| 34 | Value* CreateLocaleDefaultValue(Value::ValueType type, int message_id) { |
[email protected] | 16b52716 | 2010-08-15 18:37:10 | [diff] [blame] | 35 | std::string resource_string = l10n_util::GetStringUTF8(message_id); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 36 | DCHECK(!resource_string.empty()); |
| 37 | switch (type) { |
| 38 | case Value::TYPE_BOOLEAN: { |
[email protected] | 16b52716 | 2010-08-15 18:37:10 | [diff] [blame] | 39 | if ("true" == resource_string) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 40 | return Value::CreateBooleanValue(true); |
[email protected] | 16b52716 | 2010-08-15 18:37:10 | [diff] [blame] | 41 | if ("false" == resource_string) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 42 | return Value::CreateBooleanValue(false); |
| 43 | break; |
| 44 | } |
| 45 | |
| 46 | case Value::TYPE_INTEGER: { |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 47 | int val; |
[email protected] | 16b52716 | 2010-08-15 18:37:10 | [diff] [blame] | 48 | base::StringToInt(resource_string, &val); |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 49 | return Value::CreateIntegerValue(val); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | case Value::TYPE_REAL: { |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 53 | double val; |
[email protected] | 16b52716 | 2010-08-15 18:37:10 | [diff] [blame] | 54 | base::StringToDouble(resource_string, &val); |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 55 | return Value::CreateRealValue(val); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | case Value::TYPE_STRING: { |
| 59 | return Value::CreateStringValue(resource_string); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | default: { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 63 | NOTREACHED() << |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 64 | "list and dictionary types can not have default locale values"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | NOTREACHED(); |
| 68 | return Value::CreateNullValue(); |
| 69 | } |
| 70 | |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 71 | // Forwards a notification after a PostMessage so that we can wait for the |
| 72 | // MessageLoop to run. |
| 73 | void NotifyReadError(PrefService* pref, int message_id) { |
| 74 | Source<PrefService> source(pref); |
| 75 | NotificationService::current()->Notify(NotificationType::PROFILE_ERROR, |
| 76 | source, Details<int>(&message_id)); |
| 77 | } |
| 78 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 79 | } // namespace |
| 80 | |
[email protected] | db198b2 | 2010-07-12 16:48:49 | [diff] [blame] | 81 | // static |
[email protected] | a9c23a5 | 2010-08-04 09:13:44 | [diff] [blame] | 82 | PrefService* PrefService::CreatePrefService(const FilePath& pref_filename, |
| 83 | Profile* profile) { |
[email protected] | 1d01d41 | 2010-08-20 00:36:01 | [diff] [blame] | 84 | #if defined(OS_LINUX) |
| 85 | // We'd like to see what fraction of our users have the preferences |
| 86 | // stored on a network file system, as we've had no end of troubles |
| 87 | // with NFS/AFS. |
| 88 | // TODO(evanm): remove this once we've collected state. |
| 89 | file_util::FileSystemType fstype; |
| 90 | if (file_util::GetFileSystemType(pref_filename.DirName(), &fstype)) { |
| 91 | UMA_HISTOGRAM_ENUMERATION("PrefService.FileSystemType", |
| 92 | static_cast<int>(fstype), |
| 93 | file_util::FILE_SYSTEM_TYPE_COUNT); |
| 94 | } |
| 95 | #endif |
| 96 | |
[email protected] | fa26b3d5 | 2010-08-06 08:51:50 | [diff] [blame] | 97 | return new PrefService( |
| 98 | PrefValueStore::CreatePrefValueStore(pref_filename, profile, false)); |
[email protected] | db198b2 | 2010-07-12 16:48:49 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // static |
[email protected] | fa26b3d5 | 2010-08-06 08:51:50 | [diff] [blame] | 102 | PrefService* PrefService::CreateUserPrefService(const FilePath& pref_filename) { |
| 103 | return new PrefService( |
| 104 | PrefValueStore::CreatePrefValueStore(pref_filename, NULL, true)); |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | PrefService::PrefService(PrefValueStore* pref_value_store) |
| 108 | : pref_value_store_(pref_value_store) { |
[email protected] | d81288a0 | 2010-08-18 07:25:50 | [diff] [blame] | 109 | pref_notifier_.reset(new PrefNotifier(this, pref_value_store)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 110 | InitFromStorage(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | PrefService::~PrefService() { |
| 114 | DCHECK(CalledOnValidThread()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 115 | STLDeleteContainerPointers(prefs_.begin(), prefs_.end()); |
| 116 | prefs_.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 117 | } |
| 118 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 119 | void PrefService::InitFromStorage() { |
| 120 | PrefStore::PrefReadError error = LoadPersistentPrefs(); |
| 121 | if (error == PrefStore::PREF_READ_ERROR_NONE) |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 122 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 123 | |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 124 | // Failing to load prefs on startup is a bad thing(TM). See bug 38352 for |
| 125 | // an example problem that this can cause. |
| 126 | // Do some diagnosis and try to avoid losing data. |
| 127 | int message_id = 0; |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 128 | if (error <= PrefStore::PREF_READ_ERROR_JSON_TYPE) { |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 129 | message_id = IDS_PREFERENCES_CORRUPT_ERROR; |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 130 | } else if (error != PrefStore::PREF_READ_ERROR_NO_FILE) { |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 131 | message_id = IDS_PREFERENCES_UNREADABLE_ERROR; |
| 132 | } |
| 133 | |
| 134 | if (message_id) { |
| 135 | ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, |
| 136 | NewRunnableFunction(&NotifyReadError, this, message_id)); |
| 137 | } |
| 138 | UMA_HISTOGRAM_ENUMERATION("PrefService.ReadError", error, 20); |
| 139 | } |
| 140 | |
| 141 | bool PrefService::ReloadPersistentPrefs() { |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 142 | return (LoadPersistentPrefs() == PrefStore::PREF_READ_ERROR_NONE); |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 143 | } |
| 144 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 145 | PrefStore::PrefReadError PrefService::LoadPersistentPrefs() { |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 146 | DCHECK(CalledOnValidThread()); |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 147 | |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 148 | PrefStore::PrefReadError pref_error = pref_value_store_->ReadPrefs(); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 149 | |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 150 | for (PreferenceSet::iterator it = prefs_.begin(); |
| 151 | it != prefs_.end(); ++it) { |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 152 | (*it)->pref_value_store_ = pref_value_store_.get(); |
[email protected] | 7aa0a96 | 2010-04-21 17:24:42 | [diff] [blame] | 153 | } |
| 154 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 155 | return pref_error; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 156 | } |
| 157 | |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 158 | bool PrefService::SavePersistentPrefs() { |
| 159 | DCHECK(CalledOnValidThread()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 160 | |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 161 | return pref_value_store_->WritePrefs(); |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 162 | } |
| 163 | |
[email protected] | 6c116404 | 2009-05-08 14:41:08 | [diff] [blame] | 164 | void PrefService::ScheduleSavePersistentPrefs() { |
[email protected] | 6faa0e0d | 2009-04-28 06:50:36 | [diff] [blame] | 165 | DCHECK(CalledOnValidThread()); |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 166 | |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 167 | pref_value_store_->ScheduleWritePrefs(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 168 | } |
| 169 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 170 | void PrefService::RegisterBooleanPref(const char* path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 171 | bool default_value) { |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 172 | Preference* pref = new Preference(pref_value_store_.get(), path, |
| 173 | Value::CreateBooleanValue(default_value)); |
| 174 | RegisterPreference(pref); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 175 | } |
| 176 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 177 | void PrefService::RegisterIntegerPref(const char* path, int default_value) { |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 178 | Preference* pref = new Preference(pref_value_store_.get(), path, |
| 179 | Value::CreateIntegerValue(default_value)); |
| 180 | RegisterPreference(pref); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 181 | } |
| 182 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 183 | void PrefService::RegisterRealPref(const char* path, double default_value) { |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 184 | Preference* pref = new Preference(pref_value_store_.get(), path, |
| 185 | Value::CreateRealValue(default_value)); |
| 186 | RegisterPreference(pref); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 187 | } |
| 188 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 189 | void PrefService::RegisterStringPref(const char* path, |
[email protected] | 20ce516d | 2010-06-18 02:20:04 | [diff] [blame] | 190 | const std::string& default_value) { |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 191 | Preference* pref = new Preference(pref_value_store_.get(), path, |
| 192 | Value::CreateStringValue(default_value)); |
| 193 | RegisterPreference(pref); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 194 | } |
| 195 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 196 | void PrefService::RegisterFilePathPref(const char* path, |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 197 | const FilePath& default_value) { |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 198 | Preference* pref = new Preference(pref_value_store_.get(), path, |
| 199 | Value::CreateStringValue(default_value.value())); |
| 200 | RegisterPreference(pref); |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 201 | } |
| 202 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 203 | void PrefService::RegisterListPref(const char* path) { |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 204 | Preference* pref = new Preference(pref_value_store_.get(), path, |
| 205 | new ListValue); |
| 206 | RegisterPreference(pref); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 207 | } |
| 208 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 209 | void PrefService::RegisterDictionaryPref(const char* path) { |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 210 | Preference* pref = new Preference(pref_value_store_.get(), path, |
| 211 | new DictionaryValue()); |
| 212 | RegisterPreference(pref); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 213 | } |
| 214 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 215 | void PrefService::RegisterLocalizedBooleanPref(const char* path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 216 | int locale_default_message_id) { |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 217 | Preference* pref = new Preference(pref_value_store_.get(), path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 218 | CreateLocaleDefaultValue(Value::TYPE_BOOLEAN, locale_default_message_id)); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 219 | RegisterPreference(pref); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 220 | } |
| 221 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 222 | void PrefService::RegisterLocalizedIntegerPref(const char* path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 223 | int locale_default_message_id) { |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 224 | Preference* pref = new Preference(pref_value_store_.get(), path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 225 | CreateLocaleDefaultValue(Value::TYPE_INTEGER, locale_default_message_id)); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 226 | RegisterPreference(pref); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 227 | } |
| 228 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 229 | void PrefService::RegisterLocalizedRealPref(const char* path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 230 | int locale_default_message_id) { |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 231 | Preference* pref = new Preference(pref_value_store_.get(), path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 232 | CreateLocaleDefaultValue(Value::TYPE_REAL, locale_default_message_id)); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 233 | RegisterPreference(pref); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 234 | } |
| 235 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 236 | void PrefService::RegisterLocalizedStringPref(const char* path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 237 | int locale_default_message_id) { |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 238 | Preference* pref = new Preference(pref_value_store_.get(), path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 239 | CreateLocaleDefaultValue(Value::TYPE_STRING, locale_default_message_id)); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 240 | RegisterPreference(pref); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 241 | } |
| 242 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 243 | bool PrefService::GetBoolean(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 244 | DCHECK(CalledOnValidThread()); |
| 245 | |
| 246 | bool result = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 247 | |
| 248 | const Preference* pref = FindPreference(path); |
| 249 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 250 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 251 | return result; |
| 252 | } |
| 253 | bool rv = pref->GetValue()->GetAsBoolean(&result); |
| 254 | DCHECK(rv); |
| 255 | return result; |
| 256 | } |
| 257 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 258 | int PrefService::GetInteger(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 259 | DCHECK(CalledOnValidThread()); |
| 260 | |
| 261 | int result = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 262 | |
| 263 | const Preference* pref = FindPreference(path); |
| 264 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 265 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 266 | return result; |
| 267 | } |
| 268 | bool rv = pref->GetValue()->GetAsInteger(&result); |
| 269 | DCHECK(rv); |
| 270 | return result; |
| 271 | } |
| 272 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 273 | double PrefService::GetReal(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 274 | DCHECK(CalledOnValidThread()); |
| 275 | |
| 276 | double result = 0.0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 277 | |
| 278 | const Preference* pref = FindPreference(path); |
| 279 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 280 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 281 | return result; |
| 282 | } |
| 283 | bool rv = pref->GetValue()->GetAsReal(&result); |
| 284 | DCHECK(rv); |
| 285 | return result; |
| 286 | } |
| 287 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 288 | std::string PrefService::GetString(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 289 | DCHECK(CalledOnValidThread()); |
| 290 | |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 291 | std::string result; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 292 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 293 | const Preference* pref = FindPreference(path); |
| 294 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 295 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 296 | return result; |
| 297 | } |
| 298 | bool rv = pref->GetValue()->GetAsString(&result); |
| 299 | DCHECK(rv); |
| 300 | return result; |
| 301 | } |
| 302 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 303 | FilePath PrefService::GetFilePath(const char* path) const { |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 304 | DCHECK(CalledOnValidThread()); |
| 305 | |
| 306 | FilePath::StringType result; |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 307 | |
| 308 | const Preference* pref = FindPreference(path); |
| 309 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 310 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 311 | return FilePath(result); |
| 312 | } |
| 313 | bool rv = pref->GetValue()->GetAsString(&result); |
| 314 | DCHECK(rv); |
[email protected] | 66de4f09 | 2009-09-04 23:59:40 | [diff] [blame] | 315 | #if defined(OS_POSIX) |
| 316 | // We store filepaths as UTF8, so convert it back to the system type. |
| 317 | result = base::SysWideToNativeMB(UTF8ToWide(result)); |
| 318 | #endif |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 319 | return FilePath(result); |
| 320 | } |
| 321 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 322 | bool PrefService::HasPrefPath(const char* path) const { |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 323 | return pref_value_store_->HasPrefPath(path); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | const PrefService::Preference* PrefService::FindPreference( |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 327 | const char* pref_name) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 328 | DCHECK(CalledOnValidThread()); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 329 | Preference p(NULL, pref_name, NULL); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 330 | PreferenceSet::const_iterator it = prefs_.find(&p); |
| 331 | return it == prefs_.end() ? NULL : *it; |
| 332 | } |
| 333 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 334 | bool PrefService::IsManagedPreference(const char* pref_name) const { |
[email protected] | d90de1c0 | 2010-07-19 19:50:48 | [diff] [blame] | 335 | const Preference* pref = FindPreference(pref_name); |
| 336 | if (pref && pref->IsManaged()) { |
| 337 | return true; |
| 338 | } |
| 339 | return false; |
| 340 | } |
| 341 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 342 | const DictionaryValue* PrefService::GetDictionary(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 343 | DCHECK(CalledOnValidThread()); |
| 344 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 345 | const Preference* pref = FindPreference(path); |
| 346 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 347 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 348 | return NULL; |
| 349 | } |
| 350 | const Value* value = pref->GetValue(); |
| 351 | if (value->GetType() == Value::TYPE_NULL) |
| 352 | return NULL; |
| 353 | return static_cast<const DictionaryValue*>(value); |
| 354 | } |
| 355 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 356 | const ListValue* PrefService::GetList(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 357 | DCHECK(CalledOnValidThread()); |
| 358 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 359 | const Preference* pref = FindPreference(path); |
| 360 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 361 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 362 | return NULL; |
| 363 | } |
| 364 | const Value* value = pref->GetValue(); |
| 365 | if (value->GetType() == Value::TYPE_NULL) |
| 366 | return NULL; |
| 367 | return static_cast<const ListValue*>(value); |
| 368 | } |
| 369 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 370 | void PrefService::AddPrefObserver(const char* path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 371 | NotificationObserver* obs) { |
[email protected] | d81288a0 | 2010-08-18 07:25:50 | [diff] [blame] | 372 | pref_notifier_->AddPrefObserver(path, obs); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 373 | } |
| 374 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 375 | void PrefService::RemovePrefObserver(const char* path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 376 | NotificationObserver* obs) { |
[email protected] | d81288a0 | 2010-08-18 07:25:50 | [diff] [blame] | 377 | pref_notifier_->RemovePrefObserver(path, obs); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 378 | } |
| 379 | |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 380 | void PrefService::RegisterPreference(Preference* pref) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 381 | DCHECK(CalledOnValidThread()); |
| 382 | |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 383 | if (FindPreference(pref->name().c_str())) { |
| 384 | NOTREACHED() << "Tried to register duplicate pref " << pref->name(); |
| 385 | delete pref; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 386 | return; |
| 387 | } |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 388 | prefs_.insert(pref); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 389 | } |
| 390 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 391 | void PrefService::ClearPref(const char* path) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 392 | DCHECK(CalledOnValidThread()); |
| 393 | |
| 394 | const Preference* pref = FindPreference(path); |
| 395 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 396 | NOTREACHED() << "Trying to clear an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 397 | return; |
| 398 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 399 | Value* value; |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 400 | bool has_old_value = pref_value_store_->GetValue(path, &value); |
| 401 | pref_value_store_->RemoveUserPrefValue(path); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 402 | |
[email protected] | d81288a0 | 2010-08-18 07:25:50 | [diff] [blame] | 403 | // TODO(pamg): Removing the user value when there's a higher-priority setting |
| 404 | // doesn't change the value and shouldn't fire observers. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 405 | if (has_old_value) |
[email protected] | d81288a0 | 2010-08-18 07:25:50 | [diff] [blame] | 406 | pref_notifier_->FireObservers(path); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 407 | } |
| 408 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 409 | void PrefService::Set(const char* path, const Value& value) { |
[email protected] | a048d7e4 | 2009-12-01 01:02:39 | [diff] [blame] | 410 | DCHECK(CalledOnValidThread()); |
| 411 | |
| 412 | const Preference* pref = FindPreference(path); |
| 413 | if (!pref) { |
| 414 | NOTREACHED() << "Trying to write an unregistered pref: " << path; |
| 415 | return; |
| 416 | } |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 417 | |
| 418 | // Allow dictionary and list types to be set to null. |
| 419 | if (value.GetType() == Value::TYPE_NULL && |
| 420 | (pref->type() == Value::TYPE_DICTIONARY || |
| 421 | pref->type() == Value::TYPE_LIST)) { |
| 422 | scoped_ptr<Value> old_value(GetPrefCopy(path)); |
| 423 | if (!old_value->Equals(&value)) { |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 424 | pref_value_store_->RemoveUserPrefValue(path); |
[email protected] | d81288a0 | 2010-08-18 07:25:50 | [diff] [blame] | 425 | pref_notifier_->FireObservers(path); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 426 | } |
| 427 | return; |
| 428 | } |
| 429 | |
[email protected] | a048d7e4 | 2009-12-01 01:02:39 | [diff] [blame] | 430 | if (pref->type() != value.GetType()) { |
| 431 | NOTREACHED() << "Wrong type for Set: " << path; |
| 432 | } |
| 433 | |
| 434 | scoped_ptr<Value> old_value(GetPrefCopy(path)); |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 435 | pref_value_store_->SetUserPrefValue(path, value.DeepCopy()); |
[email protected] | a048d7e4 | 2009-12-01 01:02:39 | [diff] [blame] | 436 | |
[email protected] | d81288a0 | 2010-08-18 07:25:50 | [diff] [blame] | 437 | pref_notifier_->OnUserPreferenceSet(path, old_value.get()); |
[email protected] | a048d7e4 | 2009-12-01 01:02:39 | [diff] [blame] | 438 | } |
| 439 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 440 | void PrefService::SetBoolean(const char* path, bool value) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 441 | DCHECK(CalledOnValidThread()); |
| 442 | |
| 443 | const Preference* pref = FindPreference(path); |
| 444 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 445 | NOTREACHED() << "Trying to write an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 446 | return; |
| 447 | } |
[email protected] | 91ae7e3 | 2010-04-23 12:58:50 | [diff] [blame] | 448 | if (pref->IsManaged()) { |
| 449 | NOTREACHED() << "Preference is managed: " << path; |
| 450 | return; |
| 451 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 452 | if (pref->type() != Value::TYPE_BOOLEAN) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 453 | NOTREACHED() << "Wrong type for SetBoolean: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 454 | return; |
| 455 | } |
| 456 | |
| 457 | scoped_ptr<Value> old_value(GetPrefCopy(path)); |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 458 | Value* new_value = Value::CreateBooleanValue(value); |
| 459 | pref_value_store_->SetUserPrefValue(path, new_value); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 460 | |
[email protected] | d81288a0 | 2010-08-18 07:25:50 | [diff] [blame] | 461 | pref_notifier_->OnUserPreferenceSet(path, old_value.get()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 462 | } |
| 463 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 464 | void PrefService::SetInteger(const char* path, int value) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 465 | DCHECK(CalledOnValidThread()); |
| 466 | |
| 467 | const Preference* pref = FindPreference(path); |
| 468 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 469 | NOTREACHED() << "Trying to write an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 470 | return; |
| 471 | } |
[email protected] | 91ae7e3 | 2010-04-23 12:58:50 | [diff] [blame] | 472 | if (pref->IsManaged()) { |
| 473 | NOTREACHED() << "Preference is managed: " << path; |
| 474 | return; |
| 475 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 476 | if (pref->type() != Value::TYPE_INTEGER) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 477 | NOTREACHED() << "Wrong type for SetInteger: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 478 | return; |
| 479 | } |
| 480 | |
| 481 | scoped_ptr<Value> old_value(GetPrefCopy(path)); |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 482 | Value* new_value = Value::CreateIntegerValue(value); |
| 483 | pref_value_store_->SetUserPrefValue(path, new_value); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 484 | |
[email protected] | d81288a0 | 2010-08-18 07:25:50 | [diff] [blame] | 485 | pref_notifier_->OnUserPreferenceSet(path, old_value.get()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 486 | } |
| 487 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 488 | void PrefService::SetReal(const char* path, double value) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 489 | DCHECK(CalledOnValidThread()); |
| 490 | |
| 491 | const Preference* pref = FindPreference(path); |
| 492 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 493 | NOTREACHED() << "Trying to write an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 494 | return; |
| 495 | } |
[email protected] | 91ae7e3 | 2010-04-23 12:58:50 | [diff] [blame] | 496 | if (pref->IsManaged()) { |
| 497 | NOTREACHED() << "Preference is managed: " << path; |
| 498 | return; |
| 499 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 500 | if (pref->type() != Value::TYPE_REAL) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 501 | NOTREACHED() << "Wrong type for SetReal: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 502 | return; |
| 503 | } |
| 504 | |
| 505 | scoped_ptr<Value> old_value(GetPrefCopy(path)); |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 506 | Value* new_value = Value::CreateRealValue(value); |
| 507 | pref_value_store_->SetUserPrefValue(path, new_value); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 508 | |
[email protected] | d81288a0 | 2010-08-18 07:25:50 | [diff] [blame] | 509 | pref_notifier_->OnUserPreferenceSet(path, old_value.get()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 510 | } |
| 511 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 512 | void PrefService::SetString(const char* path, const std::string& value) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 513 | DCHECK(CalledOnValidThread()); |
| 514 | |
| 515 | const Preference* pref = FindPreference(path); |
| 516 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 517 | NOTREACHED() << "Trying to write an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 518 | return; |
| 519 | } |
[email protected] | 91ae7e3 | 2010-04-23 12:58:50 | [diff] [blame] | 520 | if (pref->IsManaged()) { |
| 521 | NOTREACHED() << "Preference is managed: " << path; |
| 522 | return; |
| 523 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 524 | if (pref->type() != Value::TYPE_STRING) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 525 | NOTREACHED() << "Wrong type for SetString: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 526 | return; |
| 527 | } |
| 528 | |
| 529 | scoped_ptr<Value> old_value(GetPrefCopy(path)); |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 530 | Value* new_value = Value::CreateStringValue(value); |
| 531 | pref_value_store_->SetUserPrefValue(path, new_value); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 532 | |
[email protected] | d81288a0 | 2010-08-18 07:25:50 | [diff] [blame] | 533 | pref_notifier_->OnUserPreferenceSet(path, old_value.get()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 534 | } |
| 535 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 536 | void PrefService::SetFilePath(const char* path, const FilePath& value) { |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 537 | DCHECK(CalledOnValidThread()); |
| 538 | |
| 539 | const Preference* pref = FindPreference(path); |
| 540 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 541 | NOTREACHED() << "Trying to write an unregistered pref: " << path; |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 542 | return; |
| 543 | } |
[email protected] | 91ae7e3 | 2010-04-23 12:58:50 | [diff] [blame] | 544 | if (pref->IsManaged()) { |
| 545 | NOTREACHED() << "Preference is managed: " << path; |
| 546 | return; |
| 547 | } |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 548 | if (pref->type() != Value::TYPE_STRING) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 549 | NOTREACHED() << "Wrong type for SetFilePath: " << path; |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 550 | return; |
| 551 | } |
| 552 | |
| 553 | scoped_ptr<Value> old_value(GetPrefCopy(path)); |
[email protected] | 66de4f09 | 2009-09-04 23:59:40 | [diff] [blame] | 554 | #if defined(OS_POSIX) |
| 555 | // Value::SetString only knows about UTF8 strings, so convert the path from |
| 556 | // the system native value to UTF8. |
| 557 | std::string path_utf8 = WideToUTF8(base::SysNativeMBToWide(value.value())); |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 558 | Value* new_value = Value::CreateStringValue(path_utf8); |
| 559 | pref_value_store_->SetUserPrefValue(path, new_value); |
[email protected] | 66de4f09 | 2009-09-04 23:59:40 | [diff] [blame] | 560 | #else |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 561 | Value* new_value = Value::CreateStringValue(value.value()); |
| 562 | pref_value_store_->SetUserPrefValue(path, new_value); |
[email protected] | 66de4f09 | 2009-09-04 23:59:40 | [diff] [blame] | 563 | #endif |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 564 | |
[email protected] | d81288a0 | 2010-08-18 07:25:50 | [diff] [blame] | 565 | pref_notifier_->OnUserPreferenceSet(path, old_value.get()); |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 566 | } |
| 567 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 568 | void PrefService::SetInt64(const char* path, int64 value) { |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 569 | DCHECK(CalledOnValidThread()); |
| 570 | |
| 571 | const Preference* pref = FindPreference(path); |
| 572 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 573 | NOTREACHED() << "Trying to write an unregistered pref: " << path; |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 574 | return; |
| 575 | } |
[email protected] | 91ae7e3 | 2010-04-23 12:58:50 | [diff] [blame] | 576 | if (pref->IsManaged()) { |
| 577 | NOTREACHED() << "Preference is managed: " << path; |
| 578 | return; |
| 579 | } |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 580 | if (pref->type() != Value::TYPE_STRING) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 581 | NOTREACHED() << "Wrong type for SetInt64: " << path; |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 582 | return; |
| 583 | } |
| 584 | |
| 585 | scoped_ptr<Value> old_value(GetPrefCopy(path)); |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 586 | Value* new_value = Value::CreateStringValue(base::Int64ToString(value)); |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 587 | pref_value_store_->SetUserPrefValue(path, new_value); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 588 | |
[email protected] | d81288a0 | 2010-08-18 07:25:50 | [diff] [blame] | 589 | pref_notifier_->OnUserPreferenceSet(path, old_value.get()); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 590 | } |
| 591 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 592 | int64 PrefService::GetInt64(const char* path) const { |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 593 | DCHECK(CalledOnValidThread()); |
| 594 | |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 595 | const Preference* pref = FindPreference(path); |
| 596 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 597 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
[email protected] | c345330 | 2009-12-01 01:33:08 | [diff] [blame] | 598 | return 0; |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 599 | } |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 600 | std::string result("0"); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 601 | bool rv = pref->GetValue()->GetAsString(&result); |
| 602 | DCHECK(rv); |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 603 | |
| 604 | int64 val; |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 605 | base::StringToInt64(result, &val); |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 606 | return val; |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 607 | } |
| 608 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 609 | void PrefService::RegisterInt64Pref(const char* path, int64 default_value) { |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 610 | Preference* pref = new Preference(pref_value_store_.get(), path, |
| 611 | Value::CreateStringValue(base::Int64ToString(default_value))); |
| 612 | RegisterPreference(pref); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 613 | } |
| 614 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 615 | DictionaryValue* PrefService::GetMutableDictionary(const char* path) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 616 | DCHECK(CalledOnValidThread()); |
| 617 | |
| 618 | const Preference* pref = FindPreference(path); |
| 619 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 620 | NOTREACHED() << "Trying to get an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 621 | return NULL; |
| 622 | } |
| 623 | if (pref->type() != Value::TYPE_DICTIONARY) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 624 | NOTREACHED() << "Wrong type for GetMutableDictionary: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 625 | return NULL; |
| 626 | } |
| 627 | |
| 628 | DictionaryValue* dict = NULL; |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 629 | Value* tmp_value = NULL; |
[email protected] | 9176124 | 2010-06-10 15:35:37 | [diff] [blame] | 630 | if (!pref_value_store_->GetValue(path, &tmp_value) || |
| 631 | !tmp_value->IsType(Value::TYPE_DICTIONARY)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 632 | dict = new DictionaryValue; |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 633 | pref_value_store_->SetUserPrefValue(path, dict); |
| 634 | } else { |
| 635 | dict = static_cast<DictionaryValue*>(tmp_value); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 636 | } |
| 637 | return dict; |
| 638 | } |
| 639 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 640 | ListValue* PrefService::GetMutableList(const char* path) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 641 | DCHECK(CalledOnValidThread()); |
| 642 | |
| 643 | const Preference* pref = FindPreference(path); |
| 644 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 645 | NOTREACHED() << "Trying to get an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 646 | return NULL; |
| 647 | } |
| 648 | if (pref->type() != Value::TYPE_LIST) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 649 | NOTREACHED() << "Wrong type for GetMutableList: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 650 | return NULL; |
| 651 | } |
| 652 | |
| 653 | ListValue* list = NULL; |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 654 | Value* tmp_value = NULL; |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 655 | if (!pref_value_store_->GetValue(path, &tmp_value)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 656 | list = new ListValue; |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 657 | pref_value_store_->SetUserPrefValue(path, list); |
| 658 | } else { |
| 659 | list = static_cast<ListValue*>(tmp_value); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 660 | } |
| 661 | return list; |
| 662 | } |
| 663 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 664 | Value* PrefService::GetPrefCopy(const char* path) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 665 | DCHECK(CalledOnValidThread()); |
| 666 | |
| 667 | const Preference* pref = FindPreference(path); |
| 668 | DCHECK(pref); |
| 669 | return pref->GetValue()->DeepCopy(); |
| 670 | } |
| 671 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 672 | /////////////////////////////////////////////////////////////////////////////// |
| 673 | // PrefService::Preference |
| 674 | |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 675 | PrefService::Preference::Preference(PrefValueStore* pref_value_store, |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 676 | const char* name, |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 677 | Value* default_value) |
| 678 | : type_(Value::TYPE_NULL), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 679 | name_(name), |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 680 | default_value_(default_value), |
| 681 | pref_value_store_(pref_value_store) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 682 | DCHECK(name); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 683 | |
| 684 | if (default_value) { |
| 685 | type_ = default_value->GetType(); |
| 686 | DCHECK(type_ != Value::TYPE_NULL && type_ != Value::TYPE_BINARY) << |
| 687 | "invalid preference type: " << type_; |
| 688 | } |
| 689 | |
| 690 | // We set the default value of lists and dictionaries to be null so it's |
| 691 | // easier for callers to check for empty list/dict prefs. |
| 692 | if (Value::TYPE_LIST == type_ || Value::TYPE_DICTIONARY == type_) |
| 693 | default_value_.reset(Value::CreateNullValue()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | const Value* PrefService::Preference::GetValue() const { |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 697 | DCHECK(NULL != pref_value_store_) << |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 698 | "Must register pref before getting its value"; |
| 699 | |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 700 | Value* temp_value = NULL; |
| 701 | if (pref_value_store_->GetValue(name_, &temp_value) && |
| 702 | temp_value->GetType() == type_) { |
| 703 | return temp_value; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 704 | } |
| 705 | |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 706 | // Pref not found, just return the app default. |
| 707 | return default_value_.get(); |
[email protected] | 3793923 | 2010-09-09 07:42:35 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | bool PrefService::Preference::IsDefaultValue() const { |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 711 | DCHECK(default_value_.get()); |
| 712 | return default_value_->Equals(GetValue()); |
| 713 | } |
| 714 | |
| 715 | bool PrefService::Preference::IsManaged() const { |
| 716 | return pref_value_store_->PrefValueInManagedStore(name_.c_str()); |
| 717 | } |
| 718 | |
| 719 | bool PrefService::Preference::HasExtensionSetting() const { |
| 720 | return pref_value_store_->PrefValueInExtensionStore(name_.c_str()); |
| 721 | } |
| 722 | |
| 723 | bool PrefService::Preference::HasUserSetting() const { |
| 724 | return pref_value_store_->PrefValueInUserStore(name_.c_str()); |
| 725 | } |
| 726 | |
| 727 | bool PrefService::Preference::IsExtensionControlled() const { |
| 728 | return pref_value_store_->PrefValueFromExtensionStore(name_.c_str()); |
| 729 | } |
| 730 | |
| 731 | bool PrefService::Preference::IsUserControlled() const { |
| 732 | return pref_value_store_->PrefValueFromUserStore(name_.c_str()); |
[email protected] | d7449e8 | 2010-07-14 11:42:35 | [diff] [blame] | 733 | } |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 734 | |
| 735 | bool PrefService::Preference::IsUserModifiable() const { |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame^] | 736 | return pref_value_store_->PrefValueUserModifiable(name_.c_str()); |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 737 | } |