[email protected] | 32c3c75 | 2012-01-05 17:33:47 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 5 | #include "base/prefs/pref_service.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
[email protected] | 1cb92b8 | 2010-03-08 23:12:15 | [diff] [blame] | 7 | #include <algorithm> |
[email protected] | 1cb92b8 | 2010-03-08 23:12:15 | [diff] [blame] | 8 | |
[email protected] | bebe6943 | 2011-09-28 18:36:45 | [diff] [blame] | 9 | #include "base/bind.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 10 | #include "base/files/file_path.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 11 | #include "base/logging.h" |
| 12 | #include "base/message_loop.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 13 | #include "base/metrics/histogram.h" |
[email protected] | 03b9b4e | 2012-10-22 20:01:52 | [diff] [blame] | 14 | #include "base/prefs/default_pref_store.h" |
[email protected] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 15 | #include "base/prefs/pref_notifier_impl.h" |
| 16 | #include "base/prefs/pref_registry.h" |
| 17 | #include "base/prefs/pref_value_store.h" |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 18 | #include "base/stl_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 19 | #include "base/string_util.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 20 | #include "base/strings/string_number_conversions.h" |
[email protected] | 8703b2b | 2011-03-15 09:51:50 | [diff] [blame] | 21 | #include "base/value_conversions.h" |
[email protected] | 66de4f09 | 2009-09-04 23:59:40 | [diff] [blame] | 22 | #include "build/build_config.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 23 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 24 | namespace { |
| 25 | |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 26 | class ReadErrorHandler : public PersistentPrefStore::ReadErrorDelegate { |
| 27 | public: |
[email protected] | 70a317a | 2012-12-19 20:59:33 | [diff] [blame] | 28 | ReadErrorHandler(base::Callback<void(PersistentPrefStore::PrefReadError)> cb) |
| 29 | : callback_(cb) {} |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 30 | |
[email protected] | b94584a | 2013-02-07 03:02:08 | [diff] [blame] | 31 | virtual void OnError(PersistentPrefStore::PrefReadError error) OVERRIDE { |
[email protected] | 70a317a | 2012-12-19 20:59:33 | [diff] [blame] | 32 | callback_.Run(error); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 33 | } |
[email protected] | 70a317a | 2012-12-19 20:59:33 | [diff] [blame] | 34 | |
| 35 | private: |
| 36 | base::Callback<void(PersistentPrefStore::PrefReadError)> callback_; |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 37 | }; |
| 38 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 39 | } // namespace |
| 40 | |
[email protected] | 70a317a | 2012-12-19 20:59:33 | [diff] [blame] | 41 | PrefService::PrefService( |
| 42 | PrefNotifierImpl* pref_notifier, |
| 43 | PrefValueStore* pref_value_store, |
| 44 | PersistentPrefStore* user_prefs, |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 45 | PrefRegistry* pref_registry, |
[email protected] | 70a317a | 2012-12-19 20:59:33 | [diff] [blame] | 46 | base::Callback<void(PersistentPrefStore::PrefReadError)> |
| 47 | read_error_callback, |
| 48 | bool async) |
[email protected] | 361d37f6 | 2011-11-22 10:37:02 | [diff] [blame] | 49 | : pref_notifier_(pref_notifier), |
| 50 | pref_value_store_(pref_value_store), |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 51 | pref_registry_(pref_registry), |
[email protected] | 361d37f6 | 2011-11-22 10:37:02 | [diff] [blame] | 52 | user_pref_store_(user_prefs), |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 53 | read_error_callback_(read_error_callback) { |
[email protected] | 361d37f6 | 2011-11-22 10:37:02 | [diff] [blame] | 54 | pref_notifier_->SetPrefService(this); |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 55 | |
| 56 | pref_registry_->SetRegistrationCallback( |
| 57 | base::Bind(&PrefService::AddRegisteredPreference, |
| 58 | base::Unretained(this))); |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 59 | AddInitialPreferences(); |
| 60 | |
[email protected] | 361d37f6 | 2011-11-22 10:37:02 | [diff] [blame] | 61 | InitFromStorage(async); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 62 | } |
| 63 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 64 | PrefService::~PrefService() { |
| 65 | DCHECK(CalledOnValidThread()); |
[email protected] | a98ce126 | 2011-01-28 13:20:23 | [diff] [blame] | 66 | |
[email protected] | eeec6ca | 2013-02-21 15:10:43 | [diff] [blame] | 67 | // Remove our callback, setting a NULL one. |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 68 | pref_registry_->SetRegistrationCallback(PrefRegistry::RegistrationCallback()); |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 69 | |
[email protected] | a98ce126 | 2011-01-28 13:20:23 | [diff] [blame] | 70 | // Reset pointers so accesses after destruction reliably crash. |
| 71 | pref_value_store_.reset(); |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 72 | pref_registry_ = NULL; |
[email protected] | a98ce126 | 2011-01-28 13:20:23 | [diff] [blame] | 73 | user_pref_store_ = NULL; |
[email protected] | 42f2378 | 2012-06-08 19:11:53 | [diff] [blame] | 74 | pref_notifier_.reset(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 75 | } |
| 76 | |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 77 | void PrefService::InitFromStorage(bool async) { |
| 78 | if (!async) { |
[email protected] | 70a317a | 2012-12-19 20:59:33 | [diff] [blame] | 79 | read_error_callback_.Run(user_pref_store_->ReadPrefs()); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 80 | } else { |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 81 | // Guarantee that initialization happens after this function returned. |
| 82 | MessageLoop::current()->PostTask( |
| 83 | FROM_HERE, |
[email protected] | bebe6943 | 2011-09-28 18:36:45 | [diff] [blame] | 84 | base::Bind(&PersistentPrefStore::ReadPrefsAsync, |
| 85 | user_pref_store_.get(), |
[email protected] | 70a317a | 2012-12-19 20:59:33 | [diff] [blame] | 86 | new ReadErrorHandler(read_error_callback_))); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 87 | } |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | bool PrefService::ReloadPersistentPrefs() { |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 91 | return user_pref_store_->ReadPrefs() == |
| 92 | PersistentPrefStore::PREF_READ_ERROR_NONE; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 93 | } |
| 94 | |
[email protected] | 3826fed | 2011-03-25 10:59:56 | [diff] [blame] | 95 | void PrefService::CommitPendingWrite() { |
| 96 | DCHECK(CalledOnValidThread()); |
| 97 | user_pref_store_->CommitPendingWrite(); |
| 98 | } |
| 99 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 100 | bool PrefService::GetBoolean(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 101 | DCHECK(CalledOnValidThread()); |
| 102 | |
| 103 | bool result = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 104 | |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 105 | const base::Value* value = GetPreferenceValue(path); |
| 106 | if (!value) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 107 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 108 | return result; |
| 109 | } |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 110 | bool rv = value->GetAsBoolean(&result); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 111 | DCHECK(rv); |
| 112 | return result; |
| 113 | } |
| 114 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 115 | int PrefService::GetInteger(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 116 | DCHECK(CalledOnValidThread()); |
| 117 | |
| 118 | int result = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 119 | |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 120 | const base::Value* value = GetPreferenceValue(path); |
| 121 | if (!value) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 122 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 123 | return result; |
| 124 | } |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 125 | bool rv = value->GetAsInteger(&result); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 126 | DCHECK(rv); |
| 127 | return result; |
| 128 | } |
| 129 | |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 130 | double PrefService::GetDouble(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 131 | DCHECK(CalledOnValidThread()); |
| 132 | |
| 133 | double result = 0.0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 134 | |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 135 | const base::Value* value = GetPreferenceValue(path); |
| 136 | if (!value) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 137 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 138 | return result; |
| 139 | } |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 140 | bool rv = value->GetAsDouble(&result); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 141 | DCHECK(rv); |
| 142 | return result; |
| 143 | } |
| 144 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 145 | std::string PrefService::GetString(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 146 | DCHECK(CalledOnValidThread()); |
| 147 | |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 148 | std::string result; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 149 | |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 150 | const base::Value* value = GetPreferenceValue(path); |
| 151 | if (!value) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 152 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 153 | return result; |
| 154 | } |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 155 | bool rv = value->GetAsString(&result); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 156 | DCHECK(rv); |
| 157 | return result; |
| 158 | } |
| 159 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 160 | base::FilePath PrefService::GetFilePath(const char* path) const { |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 161 | DCHECK(CalledOnValidThread()); |
| 162 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 163 | base::FilePath result; |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 164 | |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 165 | const base::Value* value = GetPreferenceValue(path); |
| 166 | if (!value) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 167 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 168 | return base::FilePath(result); |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 169 | } |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 170 | bool rv = base::GetValueAsFilePath(*value, &result); |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 171 | DCHECK(rv); |
[email protected] | 68d9d35 | 2011-02-21 16:35:04 | [diff] [blame] | 172 | return result; |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 173 | } |
| 174 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 175 | bool PrefService::HasPrefPath(const char* path) const { |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 176 | const Preference* pref = FindPreference(path); |
| 177 | return pref && !pref->IsDefaultValue(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 178 | } |
| 179 | |
[email protected] | ebd0b02 | 2011-01-27 13:24:14 | [diff] [blame] | 180 | DictionaryValue* PrefService::GetPreferenceValues() const { |
| 181 | DCHECK(CalledOnValidThread()); |
| 182 | DictionaryValue* out = new DictionaryValue; |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 183 | PrefRegistry::const_iterator i = pref_registry_->begin(); |
| 184 | for (; i != pref_registry_->end(); ++i) { |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 185 | const Value* value = GetPreferenceValue(i->first); |
[email protected] | 8874e0217 | 2011-03-11 19:44:08 | [diff] [blame] | 186 | DCHECK(value); |
[email protected] | ebd0b02 | 2011-01-27 13:24:14 | [diff] [blame] | 187 | out->Set(i->first, value->DeepCopy()); |
| 188 | } |
| 189 | return out; |
| 190 | } |
| 191 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 192 | const PrefService::Preference* PrefService::FindPreference( |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 193 | const char* pref_name) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 194 | DCHECK(CalledOnValidThread()); |
[email protected] | d15d568 | 2012-10-23 17:50:42 | [diff] [blame] | 195 | PreferenceMap::iterator it = prefs_map_.find(pref_name); |
| 196 | if (it != prefs_map_.end()) |
| 197 | return &(it->second); |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 198 | const base::Value* default_value = NULL; |
| 199 | if (!pref_registry_->defaults()->GetValue(pref_name, &default_value)) |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 200 | return NULL; |
[email protected] | d15d568 | 2012-10-23 17:50:42 | [diff] [blame] | 201 | it = prefs_map_.insert( |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 202 | std::make_pair(pref_name, Preference( |
| 203 | this, pref_name, default_value->GetType()))).first; |
[email protected] | d15d568 | 2012-10-23 17:50:42 | [diff] [blame] | 204 | return &(it->second); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 205 | } |
| 206 | |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 207 | bool PrefService::ReadOnly() const { |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 208 | return user_pref_store_->ReadOnly(); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 209 | } |
| 210 | |
[email protected] | 59c1071 | 2012-03-13 02:10:34 | [diff] [blame] | 211 | PrefService::PrefInitializationStatus PrefService::GetInitializationStatus() |
| 212 | const { |
| 213 | if (!user_pref_store_->IsInitializationComplete()) |
| 214 | return INITIALIZATION_STATUS_WAITING; |
| 215 | |
| 216 | switch (user_pref_store_->GetReadError()) { |
| 217 | case PersistentPrefStore::PREF_READ_ERROR_NONE: |
| 218 | return INITIALIZATION_STATUS_SUCCESS; |
| 219 | case PersistentPrefStore::PREF_READ_ERROR_NO_FILE: |
[email protected] | 88c6fb5 | 2013-04-09 10:39:18 | [diff] [blame^] | 220 | return INITIALIZATION_STATUS_CREATED_NEW_PREF_STORE; |
[email protected] | 59c1071 | 2012-03-13 02:10:34 | [diff] [blame] | 221 | default: |
| 222 | return INITIALIZATION_STATUS_ERROR; |
| 223 | } |
| 224 | } |
| 225 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 226 | bool PrefService::IsManagedPreference(const char* pref_name) const { |
[email protected] | d90de1c0 | 2010-07-19 19:50:48 | [diff] [blame] | 227 | const Preference* pref = FindPreference(pref_name); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 228 | return pref && pref->IsManaged(); |
[email protected] | d90de1c0 | 2010-07-19 19:50:48 | [diff] [blame] | 229 | } |
| 230 | |
[email protected] | d6bbd293 | 2012-03-19 17:10:07 | [diff] [blame] | 231 | bool PrefService::IsUserModifiablePreference(const char* pref_name) const { |
| 232 | const Preference* pref = FindPreference(pref_name); |
| 233 | return pref && pref->IsUserModifiable(); |
| 234 | } |
| 235 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 236 | const DictionaryValue* PrefService::GetDictionary(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 237 | DCHECK(CalledOnValidThread()); |
| 238 | |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 239 | const Value* value = GetPreferenceValue(path); |
| 240 | if (!value) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 241 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 242 | return NULL; |
| 243 | } |
[email protected] | 11b040b | 2011-02-02 12:42:25 | [diff] [blame] | 244 | if (value->GetType() != Value::TYPE_DICTIONARY) { |
| 245 | NOTREACHED(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 246 | return NULL; |
[email protected] | 11b040b | 2011-02-02 12:42:25 | [diff] [blame] | 247 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 248 | return static_cast<const DictionaryValue*>(value); |
| 249 | } |
| 250 | |
[email protected] | 1a5a31f | 2012-04-26 20:21:34 | [diff] [blame] | 251 | const base::Value* PrefService::GetUserPrefValue(const char* path) const { |
| 252 | DCHECK(CalledOnValidThread()); |
| 253 | |
| 254 | const Preference* pref = FindPreference(path); |
| 255 | if (!pref) { |
| 256 | NOTREACHED() << "Trying to get an unregistered pref: " << path; |
| 257 | return NULL; |
| 258 | } |
| 259 | |
| 260 | // Look for an existing preference in the user store. If it doesn't |
| 261 | // exist, return NULL. |
| 262 | base::Value* value = NULL; |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 263 | if (!user_pref_store_->GetMutableValue(path, &value)) |
[email protected] | 1a5a31f | 2012-04-26 20:21:34 | [diff] [blame] | 264 | return NULL; |
[email protected] | 1a5a31f | 2012-04-26 20:21:34 | [diff] [blame] | 265 | |
| 266 | if (!value->IsType(pref->GetType())) { |
| 267 | NOTREACHED() << "Pref value type doesn't match registered type."; |
| 268 | return NULL; |
| 269 | } |
| 270 | |
| 271 | return value; |
| 272 | } |
| 273 | |
[email protected] | 5879cef | 2013-03-02 17:02:25 | [diff] [blame] | 274 | void PrefService::SetDefaultPrefValue(const char* path, |
| 275 | base::Value* value) { |
| 276 | DCHECK(CalledOnValidThread()); |
| 277 | pref_registry_->SetDefaultPrefValue(path, value); |
| 278 | } |
| 279 | |
[email protected] | 35a6fd1 | 2012-05-28 18:08:08 | [diff] [blame] | 280 | const base::Value* PrefService::GetDefaultPrefValue(const char* path) const { |
| 281 | DCHECK(CalledOnValidThread()); |
| 282 | // Lookup the preference in the default store. |
| 283 | const base::Value* value = NULL; |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 284 | if (!pref_registry_->defaults()->GetValue(path, &value)) { |
[email protected] | 35a6fd1 | 2012-05-28 18:08:08 | [diff] [blame] | 285 | NOTREACHED() << "Default value missing for pref: " << path; |
| 286 | return NULL; |
| 287 | } |
| 288 | return value; |
| 289 | } |
| 290 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 291 | const ListValue* PrefService::GetList(const char* path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 292 | DCHECK(CalledOnValidThread()); |
| 293 | |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 294 | const Value* value = GetPreferenceValue(path); |
| 295 | if (!value) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 296 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 297 | return NULL; |
| 298 | } |
[email protected] | 11b040b | 2011-02-02 12:42:25 | [diff] [blame] | 299 | if (value->GetType() != Value::TYPE_LIST) { |
| 300 | NOTREACHED(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 301 | return NULL; |
[email protected] | 11b040b | 2011-02-02 12:42:25 | [diff] [blame] | 302 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 303 | return static_cast<const ListValue*>(value); |
| 304 | } |
| 305 | |
[email protected] | a6a7ced | 2012-11-01 17:24:18 | [diff] [blame] | 306 | void PrefService::AddPrefObserver(const char* path, PrefObserver* obs) { |
[email protected] | d81288a0 | 2010-08-18 07:25:50 | [diff] [blame] | 307 | pref_notifier_->AddPrefObserver(path, obs); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 308 | } |
| 309 | |
[email protected] | a6a7ced | 2012-11-01 17:24:18 | [diff] [blame] | 310 | void PrefService::RemovePrefObserver(const char* path, PrefObserver* obs) { |
[email protected] | d81288a0 | 2010-08-18 07:25:50 | [diff] [blame] | 311 | pref_notifier_->RemovePrefObserver(path, obs); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 312 | } |
| 313 | |
[email protected] | a6a7ced | 2012-11-01 17:24:18 | [diff] [blame] | 314 | void PrefService::AddPrefInitObserver(base::Callback<void(bool)> obs) { |
| 315 | pref_notifier_->AddInitObserver(obs); |
| 316 | } |
| 317 | |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 318 | PrefRegistry* PrefService::DeprecatedGetPrefRegistry() { |
| 319 | return pref_registry_.get(); |
| 320 | } |
| 321 | |
| 322 | void PrefService::AddInitialPreferences() { |
| 323 | for (PrefRegistry::const_iterator it = pref_registry_->begin(); |
| 324 | it != pref_registry_->end(); |
| 325 | ++it) { |
| 326 | AddRegisteredPreference(it->first.c_str(), it->second); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | // TODO(joi): Once MarkNeedsEmptyValue is gone, we can probably |
| 331 | // completely get rid of this method. There will be one difference in |
| 332 | // semantics; currently all registered preferences are stored right |
| 333 | // away in the prefs_map_, if we remove this they would be stored only |
| 334 | // opportunistically. |
| 335 | void PrefService::AddRegisteredPreference(const char* path, |
| 336 | Value* default_value) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 337 | DCHECK(CalledOnValidThread()); |
| 338 | |
[email protected] | ea3e497 | 2012-04-12 03:41:37 | [diff] [blame] | 339 | // For ListValue and DictionaryValue with non empty default, empty value |
| 340 | // for |path| needs to be persisted in |user_pref_store_|. So that |
| 341 | // non empty default is not used when user sets an empty ListValue or |
| 342 | // DictionaryValue. |
| 343 | bool needs_empty_value = false; |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 344 | base::Value::Type orig_type = default_value->GetType(); |
[email protected] | ea3e497 | 2012-04-12 03:41:37 | [diff] [blame] | 345 | if (orig_type == base::Value::TYPE_LIST) { |
| 346 | const base::ListValue* list = NULL; |
| 347 | if (default_value->GetAsList(&list) && !list->empty()) |
| 348 | needs_empty_value = true; |
| 349 | } else if (orig_type == base::Value::TYPE_DICTIONARY) { |
| 350 | const base::DictionaryValue* dict = NULL; |
| 351 | if (default_value->GetAsDictionary(&dict) && !dict->empty()) |
| 352 | needs_empty_value = true; |
| 353 | } |
| 354 | if (needs_empty_value) |
| 355 | user_pref_store_->MarkNeedsEmptyValue(path); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 356 | } |
| 357 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 358 | void PrefService::ClearPref(const char* path) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 359 | DCHECK(CalledOnValidThread()); |
| 360 | |
| 361 | const Preference* pref = FindPreference(path); |
| 362 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 363 | NOTREACHED() << "Trying to clear an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 364 | return; |
| 365 | } |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 366 | user_pref_store_->RemoveValue(path); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 367 | } |
| 368 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 369 | void PrefService::Set(const char* path, const Value& value) { |
[email protected] | b99c41c | 2011-04-27 15:18:48 | [diff] [blame] | 370 | SetUserPrefValue(path, value.DeepCopy()); |
[email protected] | a048d7e4 | 2009-12-01 01:02:39 | [diff] [blame] | 371 | } |
| 372 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 373 | void PrefService::SetBoolean(const char* path, bool value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 374 | SetUserPrefValue(path, Value::CreateBooleanValue(value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 375 | } |
| 376 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 377 | void PrefService::SetInteger(const char* path, int value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 378 | SetUserPrefValue(path, Value::CreateIntegerValue(value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 379 | } |
| 380 | |
[email protected] | fb534c9 | 2011-02-01 01:02:07 | [diff] [blame] | 381 | void PrefService::SetDouble(const char* path, double value) { |
| 382 | SetUserPrefValue(path, Value::CreateDoubleValue(value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 383 | } |
| 384 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 385 | void PrefService::SetString(const char* path, const std::string& value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 386 | SetUserPrefValue(path, Value::CreateStringValue(value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 387 | } |
| 388 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 389 | void PrefService::SetFilePath(const char* path, const base::FilePath& value) { |
[email protected] | 8703b2b | 2011-03-15 09:51:50 | [diff] [blame] | 390 | SetUserPrefValue(path, base::CreateFilePathValue(value)); |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 391 | } |
| 392 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 393 | void PrefService::SetInt64(const char* path, int64 value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 394 | SetUserPrefValue(path, Value::CreateStringValue(base::Int64ToString(value))); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 395 | } |
| 396 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 397 | int64 PrefService::GetInt64(const char* path) const { |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 398 | DCHECK(CalledOnValidThread()); |
| 399 | |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 400 | const Value* value = GetPreferenceValue(path); |
| 401 | if (!value) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 402 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
[email protected] | c345330 | 2009-12-01 01:33:08 | [diff] [blame] | 403 | return 0; |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 404 | } |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 405 | std::string result("0"); |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 406 | bool rv = value->GetAsString(&result); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 407 | DCHECK(rv); |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 408 | |
| 409 | int64 val; |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 410 | base::StringToInt64(result, &val); |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 411 | return val; |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 412 | } |
| 413 | |
[email protected] | 3cbe081 | 2012-07-03 02:51:43 | [diff] [blame] | 414 | void PrefService::SetUint64(const char* path, uint64 value) { |
| 415 | SetUserPrefValue(path, Value::CreateStringValue(base::Uint64ToString(value))); |
| 416 | } |
| 417 | |
| 418 | uint64 PrefService::GetUint64(const char* path) const { |
| 419 | DCHECK(CalledOnValidThread()); |
| 420 | |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 421 | const Value* value = GetPreferenceValue(path); |
| 422 | if (!value) { |
[email protected] | 3cbe081 | 2012-07-03 02:51:43 | [diff] [blame] | 423 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
| 424 | return 0; |
| 425 | } |
| 426 | std::string result("0"); |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 427 | bool rv = value->GetAsString(&result); |
[email protected] | 3cbe081 | 2012-07-03 02:51:43 | [diff] [blame] | 428 | DCHECK(rv); |
| 429 | |
| 430 | uint64 val; |
| 431 | base::StringToUint64(result, &val); |
| 432 | return val; |
| 433 | } |
| 434 | |
[email protected] | 26418b7 | 2011-03-30 14:07:39 | [diff] [blame] | 435 | Value* PrefService::GetMutableUserPref(const char* path, |
[email protected] | bab1c13f | 2011-08-12 20:59:02 | [diff] [blame] | 436 | base::Value::Type type) { |
[email protected] | 26418b7 | 2011-03-30 14:07:39 | [diff] [blame] | 437 | CHECK(type == Value::TYPE_DICTIONARY || type == Value::TYPE_LIST); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 438 | DCHECK(CalledOnValidThread()); |
| 439 | |
| 440 | const Preference* pref = FindPreference(path); |
| 441 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 442 | NOTREACHED() << "Trying to get an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 443 | return NULL; |
| 444 | } |
[email protected] | 26418b7 | 2011-03-30 14:07:39 | [diff] [blame] | 445 | if (pref->GetType() != type) { |
| 446 | NOTREACHED() << "Wrong type for GetMutableValue: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 447 | return NULL; |
| 448 | } |
| 449 | |
[email protected] | e025089 | 2010-10-01 18:57:53 | [diff] [blame] | 450 | // Look for an existing preference in the user store. If it doesn't |
| 451 | // exist or isn't the correct type, create a new user preference. |
[email protected] | 26418b7 | 2011-03-30 14:07:39 | [diff] [blame] | 452 | Value* value = NULL; |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 453 | if (!user_pref_store_->GetMutableValue(path, &value) || |
[email protected] | 26418b7 | 2011-03-30 14:07:39 | [diff] [blame] | 454 | !value->IsType(type)) { |
| 455 | if (type == Value::TYPE_DICTIONARY) { |
| 456 | value = new DictionaryValue; |
| 457 | } else if (type == Value::TYPE_LIST) { |
| 458 | value = new ListValue; |
| 459 | } else { |
| 460 | NOTREACHED(); |
| 461 | } |
| 462 | user_pref_store_->SetValueSilently(path, value); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 463 | } |
[email protected] | 26418b7 | 2011-03-30 14:07:39 | [diff] [blame] | 464 | return value; |
| 465 | } |
| 466 | |
[email protected] | 68bf41a | 2011-03-25 16:38:31 | [diff] [blame] | 467 | void PrefService::ReportUserPrefChanged(const std::string& key) { |
[email protected] | f89ee34 | 2011-03-07 09:28:27 | [diff] [blame] | 468 | user_pref_store_->ReportValueChanged(key); |
| 469 | } |
| 470 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 471 | void PrefService::SetUserPrefValue(const char* path, Value* new_value) { |
[email protected] | b99c41c | 2011-04-27 15:18:48 | [diff] [blame] | 472 | scoped_ptr<Value> owned_value(new_value); |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 473 | DCHECK(CalledOnValidThread()); |
| 474 | |
| 475 | const Preference* pref = FindPreference(path); |
| 476 | if (!pref) { |
| 477 | NOTREACHED() << "Trying to write an unregistered pref: " << path; |
| 478 | return; |
| 479 | } |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 480 | if (pref->GetType() != new_value->GetType()) { |
| 481 | NOTREACHED() << "Trying to set pref " << path |
| 482 | << " of type " << pref->GetType() |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 483 | << " to value of type " << new_value->GetType(); |
| 484 | return; |
| 485 | } |
| 486 | |
[email protected] | b99c41c | 2011-04-27 15:18:48 | [diff] [blame] | 487 | user_pref_store_->SetValue(path, owned_value.release()); |
[email protected] | 73c4793 | 2010-12-06 18:13:43 | [diff] [blame] | 488 | } |
| 489 | |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 490 | void PrefService::UpdateCommandLinePrefStore(PrefStore* command_line_store) { |
| 491 | pref_value_store_->UpdateCommandLinePrefStore(command_line_store); |
[email protected] | d3b05ea | 2012-01-24 22:57:05 | [diff] [blame] | 492 | } |
| 493 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 494 | /////////////////////////////////////////////////////////////////////////////// |
| 495 | // PrefService::Preference |
| 496 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 497 | PrefService::Preference::Preference(const PrefService* service, |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 498 | const char* name, |
[email protected] | bab1c13f | 2011-08-12 20:59:02 | [diff] [blame] | 499 | base::Value::Type type) |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 500 | : name_(name), |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 501 | type_(type), |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 502 | pref_service_(service) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 503 | DCHECK(name); |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 504 | DCHECK(service); |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 505 | } |
| 506 | |
[email protected] | fb8fdf1 | 2012-08-21 16:28:20 | [diff] [blame] | 507 | const std::string PrefService::Preference::name() const { |
| 508 | return name_; |
| 509 | } |
| 510 | |
[email protected] | bab1c13f | 2011-08-12 20:59:02 | [diff] [blame] | 511 | base::Value::Type PrefService::Preference::GetType() const { |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 512 | return type_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | const Value* PrefService::Preference::GetValue() const { |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 516 | const Value* result= pref_service_->GetPreferenceValue(name_); |
| 517 | DCHECK(result) << "Must register pref before getting its value"; |
| 518 | return result; |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 519 | } |
| 520 | |
[email protected] | 7ca0f36 | 2012-07-30 10:14:03 | [diff] [blame] | 521 | const Value* PrefService::Preference::GetRecommendedValue() const { |
| 522 | DCHECK(pref_service_->FindPreference(name_.c_str())) << |
| 523 | "Must register pref before getting its value"; |
| 524 | |
| 525 | const Value* found_value = NULL; |
| 526 | if (pref_value_store()->GetRecommendedValue(name_, type_, &found_value)) { |
| 527 | DCHECK(found_value->IsType(type_)); |
| 528 | return found_value; |
| 529 | } |
| 530 | |
| 531 | // The pref has no recommended value. |
| 532 | return NULL; |
| 533 | } |
| 534 | |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 535 | bool PrefService::Preference::IsManaged() const { |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 536 | return pref_value_store()->PrefValueInManagedStore(name_.c_str()); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 537 | } |
| 538 | |
[email protected] | a543728 | 2011-12-12 12:33:21 | [diff] [blame] | 539 | bool PrefService::Preference::IsRecommended() const { |
| 540 | return pref_value_store()->PrefValueFromRecommendedStore(name_.c_str()); |
| 541 | } |
| 542 | |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 543 | bool PrefService::Preference::HasExtensionSetting() const { |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 544 | return pref_value_store()->PrefValueInExtensionStore(name_.c_str()); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | bool PrefService::Preference::HasUserSetting() const { |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 548 | return pref_value_store()->PrefValueInUserStore(name_.c_str()); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | bool PrefService::Preference::IsExtensionControlled() const { |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 552 | return pref_value_store()->PrefValueFromExtensionStore(name_.c_str()); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | bool PrefService::Preference::IsUserControlled() const { |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 556 | return pref_value_store()->PrefValueFromUserStore(name_.c_str()); |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | bool PrefService::Preference::IsDefaultValue() const { |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 560 | return pref_value_store()->PrefValueFromDefaultStore(name_.c_str()); |
[email protected] | d7449e8 | 2010-07-14 11:42:35 | [diff] [blame] | 561 | } |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 562 | |
| 563 | bool PrefService::Preference::IsUserModifiable() const { |
[email protected] | 887288f0 | 2011-02-04 22:52:46 | [diff] [blame] | 564 | return pref_value_store()->PrefValueUserModifiable(name_.c_str()); |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 565 | } |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 566 | |
| 567 | bool PrefService::Preference::IsExtensionModifiable() const { |
| 568 | return pref_value_store()->PrefValueExtensionModifiable(name_.c_str()); |
| 569 | } |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 570 | |
| 571 | const base::Value* PrefService::GetPreferenceValue( |
| 572 | const std::string& path) const { |
| 573 | DCHECK(CalledOnValidThread()); |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 574 | const Value* default_value = NULL; |
| 575 | if (pref_registry_->defaults()->GetValue(path, &default_value)) { |
| 576 | const Value* found_value = NULL; |
| 577 | base::Value::Type default_type = default_value->GetType(); |
| 578 | if (pref_value_store_->GetValue(path, default_type, &found_value)) { |
| 579 | DCHECK(found_value->IsType(default_type)); |
| 580 | return found_value; |
| 581 | } else { |
| 582 | // Every registered preference has at least a default value. |
| 583 | NOTREACHED() << "no valid value found for registered pref " << path; |
| 584 | } |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 585 | } |
| 586 | |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 587 | return NULL; |
| 588 | } |