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