[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] | 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] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 64 | "list and dictionary types cannot 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) { |
[email protected] | 8bac235 | 2010-10-10 18:53:21 | [diff] [blame] | 135 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 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] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 152 | (*it)->pref_service_ = this; |
[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] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 172 | RegisterPreference(path, Value::CreateBooleanValue(default_value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 173 | } |
| 174 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 175 | void PrefService::RegisterIntegerPref(const char* path, int default_value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 176 | RegisterPreference(path, Value::CreateIntegerValue(default_value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 177 | } |
| 178 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 179 | void PrefService::RegisterRealPref(const char* path, double default_value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 180 | RegisterPreference(path, Value::CreateRealValue(default_value)); |
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::RegisterStringPref(const char* path, |
[email protected] | 20ce516d | 2010-06-18 02:20:04 | [diff] [blame] | 184 | const std::string& default_value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 185 | RegisterPreference(path, Value::CreateStringValue(default_value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 186 | } |
| 187 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 188 | void PrefService::RegisterFilePathPref(const char* path, |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 189 | const FilePath& default_value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 190 | RegisterPreference(path, Value::CreateStringValue(default_value.value())); |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 191 | } |
| 192 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 193 | void PrefService::RegisterListPref(const char* path) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 194 | RegisterPreference(path, new ListValue()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 195 | } |
| 196 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 197 | void PrefService::RegisterDictionaryPref(const char* path) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 198 | RegisterPreference(path, new DictionaryValue()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 199 | } |
| 200 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 201 | void PrefService::RegisterLocalizedBooleanPref(const char* path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 202 | int locale_default_message_id) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 203 | RegisterPreference( |
| 204 | path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 205 | CreateLocaleDefaultValue(Value::TYPE_BOOLEAN, locale_default_message_id)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 206 | } |
| 207 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 208 | void PrefService::RegisterLocalizedIntegerPref(const char* path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 209 | int locale_default_message_id) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 210 | RegisterPreference( |
| 211 | path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 212 | CreateLocaleDefaultValue(Value::TYPE_INTEGER, locale_default_message_id)); |
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::RegisterLocalizedRealPref(const char* path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 216 | int locale_default_message_id) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 217 | RegisterPreference( |
| 218 | path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 219 | CreateLocaleDefaultValue(Value::TYPE_REAL, locale_default_message_id)); |
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::RegisterLocalizedStringPref(const char* path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 223 | int locale_default_message_id) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 224 | RegisterPreference( |
| 225 | path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 226 | CreateLocaleDefaultValue(Value::TYPE_STRING, locale_default_message_id)); |
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 | bool PrefService::GetBoolean(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 230 | DCHECK(CalledOnValidThread()); |
| 231 | |
| 232 | bool result = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 233 | |
| 234 | const Preference* pref = FindPreference(path); |
| 235 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 236 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 237 | return result; |
| 238 | } |
| 239 | bool rv = pref->GetValue()->GetAsBoolean(&result); |
| 240 | DCHECK(rv); |
| 241 | return result; |
| 242 | } |
| 243 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 244 | int PrefService::GetInteger(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 245 | DCHECK(CalledOnValidThread()); |
| 246 | |
| 247 | int result = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 248 | |
| 249 | const Preference* pref = FindPreference(path); |
| 250 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 251 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 252 | return result; |
| 253 | } |
| 254 | bool rv = pref->GetValue()->GetAsInteger(&result); |
| 255 | DCHECK(rv); |
| 256 | return result; |
| 257 | } |
| 258 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 259 | double PrefService::GetReal(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 260 | DCHECK(CalledOnValidThread()); |
| 261 | |
| 262 | double result = 0.0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 263 | |
| 264 | const Preference* pref = FindPreference(path); |
| 265 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 266 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 267 | return result; |
| 268 | } |
| 269 | bool rv = pref->GetValue()->GetAsReal(&result); |
| 270 | DCHECK(rv); |
| 271 | return result; |
| 272 | } |
| 273 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 274 | std::string PrefService::GetString(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 275 | DCHECK(CalledOnValidThread()); |
| 276 | |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 277 | std::string result; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 278 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 279 | const Preference* pref = FindPreference(path); |
| 280 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 281 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 282 | return result; |
| 283 | } |
| 284 | bool rv = pref->GetValue()->GetAsString(&result); |
| 285 | DCHECK(rv); |
| 286 | return result; |
| 287 | } |
| 288 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 289 | FilePath PrefService::GetFilePath(const char* path) const { |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 290 | DCHECK(CalledOnValidThread()); |
| 291 | |
| 292 | FilePath::StringType result; |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 293 | |
| 294 | const Preference* pref = FindPreference(path); |
| 295 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 296 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 297 | return FilePath(result); |
| 298 | } |
| 299 | bool rv = pref->GetValue()->GetAsString(&result); |
| 300 | DCHECK(rv); |
[email protected] | 66de4f09 | 2009-09-04 23:59:40 | [diff] [blame] | 301 | #if defined(OS_POSIX) |
| 302 | // We store filepaths as UTF8, so convert it back to the system type. |
| 303 | result = base::SysWideToNativeMB(UTF8ToWide(result)); |
| 304 | #endif |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 305 | return FilePath(result); |
| 306 | } |
| 307 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 308 | bool PrefService::HasPrefPath(const char* path) const { |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 309 | return pref_value_store_->HasPrefPath(path); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | const PrefService::Preference* PrefService::FindPreference( |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 313 | const char* pref_name) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 314 | DCHECK(CalledOnValidThread()); |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 315 | Preference p(this, pref_name); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 316 | PreferenceSet::const_iterator it = prefs_.find(&p); |
| 317 | return it == prefs_.end() ? NULL : *it; |
| 318 | } |
| 319 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 320 | bool PrefService::IsManagedPreference(const char* pref_name) const { |
[email protected] | d90de1c0 | 2010-07-19 19:50:48 | [diff] [blame] | 321 | const Preference* pref = FindPreference(pref_name); |
| 322 | if (pref && pref->IsManaged()) { |
| 323 | return true; |
| 324 | } |
| 325 | return false; |
| 326 | } |
| 327 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 328 | const DictionaryValue* PrefService::GetDictionary(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 329 | DCHECK(CalledOnValidThread()); |
| 330 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 331 | const Preference* pref = FindPreference(path); |
| 332 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 333 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 334 | return NULL; |
| 335 | } |
| 336 | const Value* value = pref->GetValue(); |
| 337 | if (value->GetType() == Value::TYPE_NULL) |
| 338 | return NULL; |
| 339 | return static_cast<const DictionaryValue*>(value); |
| 340 | } |
| 341 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 342 | const ListValue* PrefService::GetList(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 ListValue*>(value); |
| 354 | } |
| 355 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 356 | void PrefService::AddPrefObserver(const char* path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 357 | NotificationObserver* obs) { |
[email protected] | d81288a0 | 2010-08-18 07:25:50 | [diff] [blame] | 358 | pref_notifier_->AddPrefObserver(path, obs); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 359 | } |
| 360 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 361 | void PrefService::RemovePrefObserver(const char* path, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 362 | NotificationObserver* obs) { |
[email protected] | d81288a0 | 2010-08-18 07:25:50 | [diff] [blame] | 363 | pref_notifier_->RemovePrefObserver(path, obs); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 364 | } |
| 365 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 366 | void PrefService::RegisterPreference(const char* path, Value* default_value) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 367 | DCHECK(CalledOnValidThread()); |
| 368 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 369 | // The main code path takes ownership, but most don't. We'll be safe. |
| 370 | scoped_ptr<Value> scoped_value(default_value); |
| 371 | |
| 372 | if (FindPreference(path)) { |
| 373 | NOTREACHED() << "Tried to register duplicate pref " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 374 | return; |
| 375 | } |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 376 | |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 377 | Value::ValueType orig_type = default_value->GetType(); |
| 378 | DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) << |
| 379 | "invalid preference type: " << orig_type; |
| 380 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 381 | // We set the default value of dictionaries and lists to be null so it's |
| 382 | // easier for callers to check for empty dict/list prefs. The PrefValueStore |
| 383 | // accepts ownership of the value (null or default_value). |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 384 | if (Value::TYPE_LIST == orig_type || Value::TYPE_DICTIONARY == orig_type) { |
| 385 | pref_value_store_->SetDefaultPrefValue(path, Value::CreateNullValue()); |
| 386 | } else { |
| 387 | // Hand off ownership. |
| 388 | pref_value_store_->SetDefaultPrefValue(path, scoped_value.release()); |
| 389 | } |
| 390 | |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 391 | pref_value_store_->RegisterPreferenceType(path, orig_type); |
| 392 | prefs_.insert(new Preference(this, path)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 393 | } |
| 394 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 395 | void PrefService::ClearPref(const char* path) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 396 | DCHECK(CalledOnValidThread()); |
| 397 | |
| 398 | const Preference* pref = FindPreference(path); |
| 399 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 400 | NOTREACHED() << "Trying to clear an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 401 | return; |
| 402 | } |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 403 | if (pref_value_store_->RemoveUserPrefValue(path)) |
| 404 | pref_notifier_->OnUserPreferenceSet(path); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 405 | } |
| 406 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 407 | void PrefService::Set(const char* path, const Value& value) { |
[email protected] | a048d7e4 | 2009-12-01 01:02:39 | [diff] [blame] | 408 | DCHECK(CalledOnValidThread()); |
| 409 | |
| 410 | const Preference* pref = FindPreference(path); |
| 411 | if (!pref) { |
| 412 | NOTREACHED() << "Trying to write an unregistered pref: " << path; |
| 413 | return; |
| 414 | } |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 415 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 416 | // Allow dictionary and list types to be set to null, which removes their |
| 417 | // user values. |
| 418 | bool value_changed = false; |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 419 | if (value.GetType() == Value::TYPE_NULL && |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 420 | (pref->GetType() == Value::TYPE_DICTIONARY || |
| 421 | pref->GetType() == Value::TYPE_LIST)) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 422 | value_changed = pref_value_store_->RemoveUserPrefValue(path); |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 423 | } else if (pref->GetType() != value.GetType()) { |
| 424 | NOTREACHED() << "Trying to set pref " << path |
| 425 | << " of type " << pref->GetType() |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 426 | << " to value of type " << value.GetType(); |
| 427 | } else { |
| 428 | value_changed = pref_value_store_->SetUserPrefValue(path, value.DeepCopy()); |
[email protected] | ecde274 | 2010-04-02 17:36:18 | [diff] [blame] | 429 | } |
| 430 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 431 | if (value_changed) |
| 432 | pref_notifier_->OnUserPreferenceSet(path); |
[email protected] | a048d7e4 | 2009-12-01 01:02:39 | [diff] [blame] | 433 | } |
| 434 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 435 | void PrefService::SetBoolean(const char* path, bool value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 436 | SetUserPrefValue(path, Value::CreateBooleanValue(value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 437 | } |
| 438 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 439 | void PrefService::SetInteger(const char* path, int value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 440 | SetUserPrefValue(path, Value::CreateIntegerValue(value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 441 | } |
| 442 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 443 | void PrefService::SetReal(const char* path, double value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 444 | SetUserPrefValue(path, Value::CreateRealValue(value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 445 | } |
| 446 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 447 | void PrefService::SetString(const char* path, const std::string& value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 448 | SetUserPrefValue(path, Value::CreateStringValue(value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 449 | } |
| 450 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 451 | void PrefService::SetFilePath(const char* path, const FilePath& value) { |
[email protected] | 66de4f09 | 2009-09-04 23:59:40 | [diff] [blame] | 452 | #if defined(OS_POSIX) |
| 453 | // Value::SetString only knows about UTF8 strings, so convert the path from |
| 454 | // the system native value to UTF8. |
| 455 | std::string path_utf8 = WideToUTF8(base::SysNativeMBToWide(value.value())); |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 456 | Value* new_value = Value::CreateStringValue(path_utf8); |
[email protected] | 66de4f09 | 2009-09-04 23:59:40 | [diff] [blame] | 457 | #else |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 458 | Value* new_value = Value::CreateStringValue(value.value()); |
[email protected] | 66de4f09 | 2009-09-04 23:59:40 | [diff] [blame] | 459 | #endif |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 460 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 461 | SetUserPrefValue(path, new_value); |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 462 | } |
| 463 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 464 | void PrefService::SetInt64(const char* path, int64 value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 465 | SetUserPrefValue(path, Value::CreateStringValue(base::Int64ToString(value))); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 466 | } |
| 467 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 468 | int64 PrefService::GetInt64(const char* path) const { |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 469 | DCHECK(CalledOnValidThread()); |
| 470 | |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 471 | const Preference* pref = FindPreference(path); |
| 472 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 473 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
[email protected] | c345330 | 2009-12-01 01:33:08 | [diff] [blame] | 474 | return 0; |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 475 | } |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 476 | std::string result("0"); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 477 | bool rv = pref->GetValue()->GetAsString(&result); |
| 478 | DCHECK(rv); |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 479 | |
| 480 | int64 val; |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 481 | base::StringToInt64(result, &val); |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 482 | return val; |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 483 | } |
| 484 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 485 | void PrefService::RegisterInt64Pref(const char* path, int64 default_value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 486 | RegisterPreference( |
| 487 | path, Value::CreateStringValue(base::Int64ToString(default_value))); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 488 | } |
| 489 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 490 | DictionaryValue* PrefService::GetMutableDictionary(const char* path) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 491 | DCHECK(CalledOnValidThread()); |
| 492 | |
| 493 | const Preference* pref = FindPreference(path); |
| 494 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 495 | NOTREACHED() << "Trying to get an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 496 | return NULL; |
| 497 | } |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 498 | if (pref->GetType() != Value::TYPE_DICTIONARY) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 499 | NOTREACHED() << "Wrong type for GetMutableDictionary: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 500 | return NULL; |
| 501 | } |
| 502 | |
| 503 | DictionaryValue* dict = NULL; |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 504 | Value* tmp_value = NULL; |
[email protected] | e025089 | 2010-10-01 18:57:53 | [diff] [blame] | 505 | // Look for an existing preference in the user store. If it doesn't |
| 506 | // exist or isn't the correct type, create a new user preference. |
| 507 | if (!pref_value_store_->GetUserValue(path, &tmp_value) || |
[email protected] | 9176124 | 2010-06-10 15:35:37 | [diff] [blame] | 508 | !tmp_value->IsType(Value::TYPE_DICTIONARY)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 509 | dict = new DictionaryValue; |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 510 | pref_value_store_->SetUserPrefValue(path, dict); |
| 511 | } else { |
| 512 | dict = static_cast<DictionaryValue*>(tmp_value); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 513 | } |
| 514 | return dict; |
| 515 | } |
| 516 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 517 | ListValue* PrefService::GetMutableList(const char* path) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 518 | DCHECK(CalledOnValidThread()); |
| 519 | |
| 520 | const Preference* pref = FindPreference(path); |
| 521 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 522 | NOTREACHED() << "Trying to get an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 523 | return NULL; |
| 524 | } |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 525 | if (pref->GetType() != Value::TYPE_LIST) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 526 | NOTREACHED() << "Wrong type for GetMutableList: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 527 | return NULL; |
| 528 | } |
| 529 | |
| 530 | ListValue* list = NULL; |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 531 | Value* tmp_value = NULL; |
[email protected] | e025089 | 2010-10-01 18:57:53 | [diff] [blame] | 532 | // Look for an existing preference in the user store. If it doesn't |
| 533 | // exist or isn't the correct type, create a new user preference. |
| 534 | if (!pref_value_store_->GetUserValue(path, &tmp_value) || |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 535 | !tmp_value->IsType(Value::TYPE_LIST)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 536 | list = new ListValue; |
[email protected] | d8b08c9 | 2010-06-07 13:13:28 | [diff] [blame] | 537 | pref_value_store_->SetUserPrefValue(path, list); |
| 538 | } else { |
| 539 | list = static_cast<ListValue*>(tmp_value); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 540 | } |
| 541 | return list; |
| 542 | } |
| 543 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 544 | Value* PrefService::GetPrefCopy(const char* path) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 545 | DCHECK(CalledOnValidThread()); |
| 546 | |
| 547 | const Preference* pref = FindPreference(path); |
| 548 | DCHECK(pref); |
| 549 | return pref->GetValue()->DeepCopy(); |
| 550 | } |
| 551 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 552 | void PrefService::SetUserPrefValue(const char* path, Value* new_value) { |
| 553 | DCHECK(CalledOnValidThread()); |
| 554 | |
| 555 | const Preference* pref = FindPreference(path); |
| 556 | if (!pref) { |
| 557 | NOTREACHED() << "Trying to write an unregistered pref: " << path; |
| 558 | return; |
| 559 | } |
| 560 | if (pref->IsManaged()) { |
| 561 | NOTREACHED() << "Preference is managed: " << path; |
| 562 | return; |
| 563 | } |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 564 | if (pref->GetType() != new_value->GetType()) { |
| 565 | NOTREACHED() << "Trying to set pref " << path |
| 566 | << " of type " << pref->GetType() |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 567 | << " to value of type " << new_value->GetType(); |
| 568 | return; |
| 569 | } |
| 570 | |
| 571 | if (pref_value_store_->SetUserPrefValue(path, new_value)) |
| 572 | pref_notifier_->OnUserPreferenceSet(path); |
| 573 | } |
| 574 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 575 | /////////////////////////////////////////////////////////////////////////////// |
| 576 | // PrefService::Preference |
| 577 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 578 | PrefService::Preference::Preference(const PrefService* service, |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 579 | const char* name) |
| 580 | : name_(name), |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 581 | pref_service_(service) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 582 | DCHECK(name); |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 583 | DCHECK(service); |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | Value::ValueType PrefService::Preference::GetType() const { |
| 587 | return pref_service_->pref_value_store_->GetRegisteredType(name_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | const Value* PrefService::Preference::GetValue() const { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 591 | DCHECK(pref_service_->FindPreference(name_.c_str())) << |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 592 | "Must register pref before getting its value"; |
| 593 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 594 | Value* found_value = NULL; |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 595 | if (pref_service_->pref_value_store_->GetValue(name_, &found_value)) |
| 596 | return found_value; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 597 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 598 | // Every registered preference has at least a default value. |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 599 | NOTREACHED() << "no valid value found for registered pref " << name_; |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 600 | return NULL; |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | bool PrefService::Preference::IsManaged() const { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 604 | return pref_service_->pref_value_store_-> |
| 605 | PrefValueInManagedStore(name_.c_str()); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | bool PrefService::Preference::HasExtensionSetting() const { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 609 | return pref_service_->pref_value_store_-> |
| 610 | PrefValueInExtensionStore(name_.c_str()); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | bool PrefService::Preference::HasUserSetting() const { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 614 | return pref_service_->pref_value_store_-> |
| 615 | PrefValueInUserStore(name_.c_str()); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | bool PrefService::Preference::IsExtensionControlled() const { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 619 | return pref_service_->pref_value_store_-> |
| 620 | PrefValueFromExtensionStore(name_.c_str()); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | bool PrefService::Preference::IsUserControlled() const { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 624 | return pref_service_->pref_value_store_-> |
| 625 | PrefValueFromUserStore(name_.c_str()); |
| 626 | } |
| 627 | |
| 628 | bool PrefService::Preference::IsDefaultValue() const { |
| 629 | return pref_service_->pref_value_store_-> |
| 630 | PrefValueFromDefaultStore(name_.c_str()); |
[email protected] | d7449e8 | 2010-07-14 11:42:35 | [diff] [blame] | 631 | } |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 632 | |
| 633 | bool PrefService::Preference::IsUserModifiable() const { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 634 | return pref_service_->pref_value_store_-> |
| 635 | PrefValueUserModifiable(name_.c_str()); |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 636 | } |