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