[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 | |
brettw | f00b9b4 | 2016-02-01 22:11:38 | [diff] [blame] | 5 | #include "components/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> |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 8 | #include <utility> |
[email protected] | 1cb92b8 | 2010-03-08 23:12:15 | [diff] [blame] | 9 | |
[email protected] | bebe6943 | 2011-09-28 18:36:45 | [diff] [blame] | 10 | #include "base/bind.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 12 | #include "base/location.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 13 | #include "base/logging.h" |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 14 | #include "base/memory/ptr_util.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 15 | #include "base/metrics/histogram.h" |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 16 | #include "base/single_thread_task_runner.h" |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 17 | #include "base/stl_util.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 18 | #include "base/strings/string_number_conversions.h" |
[email protected] | d529cb0 | 2013-06-10 19:06:57 | [diff] [blame] | 19 | #include "base/strings/string_util.h" |
gab | 7966d31 | 2016-05-11 20:35:01 | [diff] [blame] | 20 | #include "base/threading/thread_task_runner_handle.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" |
brettw | f00b9b4 | 2016-02-01 22:11:38 | [diff] [blame] | 23 | #include "components/prefs/default_pref_store.h" |
| 24 | #include "components/prefs/pref_notifier_impl.h" |
| 25 | #include "components/prefs/pref_registry.h" |
| 26 | #include "components/prefs/pref_value_store.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 27 | |
| 28 | namespace { |
| 29 | |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 30 | class ReadErrorHandler : public PersistentPrefStore::ReadErrorDelegate { |
| 31 | public: |
[email protected] | 70a317a | 2012-12-19 20:59:33 | [diff] [blame] | 32 | ReadErrorHandler(base::Callback<void(PersistentPrefStore::PrefReadError)> cb) |
| 33 | : callback_(cb) {} |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 34 | |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 35 | void OnError(PersistentPrefStore::PrefReadError error) override { |
[email protected] | 70a317a | 2012-12-19 20:59:33 | [diff] [blame] | 36 | callback_.Run(error); |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 37 | } |
[email protected] | 70a317a | 2012-12-19 20:59:33 | [diff] [blame] | 38 | |
| 39 | private: |
| 40 | base::Callback<void(PersistentPrefStore::PrefReadError)> callback_; |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 41 | }; |
| 42 | |
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 43 | // Returns the WriteablePrefStore::PrefWriteFlags for the pref with the given |
| 44 | // |path|. |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 45 | uint32_t GetWriteFlags(const PrefService::Preference* pref) { |
| 46 | uint32_t write_flags = WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS; |
raymes | f3a929b0 | 2015-05-07 03:54:45 | [diff] [blame] | 47 | |
| 48 | if (!pref) |
| 49 | return write_flags; |
| 50 | |
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 51 | if (pref->registration_flags() & PrefRegistry::LOSSY_PREF) |
raymes | f3a929b0 | 2015-05-07 03:54:45 | [diff] [blame] | 52 | write_flags |= WriteablePrefStore::LOSSY_PREF_WRITE_FLAG; |
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 53 | return write_flags; |
| 54 | } |
| 55 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 56 | } // namespace |
| 57 | |
[email protected] | 70a317a | 2012-12-19 20:59:33 | [diff] [blame] | 58 | PrefService::PrefService( |
| 59 | PrefNotifierImpl* pref_notifier, |
| 60 | PrefValueStore* pref_value_store, |
| 61 | PersistentPrefStore* user_prefs, |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 62 | PrefRegistry* pref_registry, |
[email protected] | 70a317a | 2012-12-19 20:59:33 | [diff] [blame] | 63 | base::Callback<void(PersistentPrefStore::PrefReadError)> |
| 64 | read_error_callback, |
| 65 | bool async) |
[email protected] | 361d37f6 | 2011-11-22 10:37:02 | [diff] [blame] | 66 | : pref_notifier_(pref_notifier), |
| 67 | pref_value_store_(pref_value_store), |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 68 | pref_registry_(pref_registry), |
[email protected] | 361d37f6 | 2011-11-22 10:37:02 | [diff] [blame] | 69 | user_pref_store_(user_prefs), |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 70 | read_error_callback_(read_error_callback) { |
[email protected] | 361d37f6 | 2011-11-22 10:37:02 | [diff] [blame] | 71 | pref_notifier_->SetPrefService(this); |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 72 | |
battre | 0a69def | 2015-01-05 18:50:19 | [diff] [blame] | 73 | // TODO(battre): This is a check for crbug.com/435208 to make sure that |
| 74 | // access violations are caused by a use-after-free bug and not by an |
| 75 | // initialization bug. |
| 76 | CHECK(pref_registry_); |
| 77 | CHECK(pref_value_store_); |
| 78 | |
[email protected] | 361d37f6 | 2011-11-22 10:37:02 | [diff] [blame] | 79 | InitFromStorage(async); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 80 | } |
| 81 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 82 | PrefService::~PrefService() { |
| 83 | DCHECK(CalledOnValidThread()); |
[email protected] | a98ce126 | 2011-01-28 13:20:23 | [diff] [blame] | 84 | |
| 85 | // Reset pointers so accesses after destruction reliably crash. |
| 86 | pref_value_store_.reset(); |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 87 | pref_registry_ = NULL; |
[email protected] | a98ce126 | 2011-01-28 13:20:23 | [diff] [blame] | 88 | user_pref_store_ = NULL; |
[email protected] | 42f2378 | 2012-06-08 19:11:53 | [diff] [blame] | 89 | pref_notifier_.reset(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 90 | } |
| 91 | |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 92 | void PrefService::InitFromStorage(bool async) { |
[email protected] | 38d1aab | 2014-03-29 17:48:49 | [diff] [blame] | 93 | if (user_pref_store_->IsInitializationComplete()) { |
| 94 | read_error_callback_.Run(user_pref_store_->GetReadError()); |
| 95 | } else if (!async) { |
[email protected] | 70a317a | 2012-12-19 20:59:33 | [diff] [blame] | 96 | read_error_callback_.Run(user_pref_store_->ReadPrefs()); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 97 | } else { |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 98 | // Guarantee that initialization happens after this function returned. |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 99 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 100 | FROM_HERE, |
tzik | 07cace4 | 2016-09-01 04:21:25 | [diff] [blame] | 101 | base::Bind(&PersistentPrefStore::ReadPrefsAsync, user_pref_store_, |
[email protected] | 70a317a | 2012-12-19 20:59:33 | [diff] [blame] | 102 | new ReadErrorHandler(read_error_callback_))); |
[email protected] | 844a100 | 2011-04-19 11:37:21 | [diff] [blame] | 103 | } |
[email protected] | ba39967 | 2010-04-06 15:42:39 | [diff] [blame] | 104 | } |
| 105 | |
[email protected] | 3826fed | 2011-03-25 10:59:56 | [diff] [blame] | 106 | void PrefService::CommitPendingWrite() { |
| 107 | DCHECK(CalledOnValidThread()); |
| 108 | user_pref_store_->CommitPendingWrite(); |
| 109 | } |
| 110 | |
benwells | 2673059 | 2015-05-28 13:08:08 | [diff] [blame] | 111 | void PrefService::SchedulePendingLossyWrites() { |
| 112 | DCHECK(CalledOnValidThread()); |
| 113 | user_pref_store_->SchedulePendingLossyWrites(); |
| 114 | } |
| 115 | |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 116 | bool PrefService::GetBoolean(const std::string& path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 117 | DCHECK(CalledOnValidThread()); |
| 118 | |
| 119 | bool result = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 120 | |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 121 | const base::Value* value = GetPreferenceValue(path); |
| 122 | if (!value) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 123 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 124 | return result; |
| 125 | } |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 126 | bool rv = value->GetAsBoolean(&result); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 127 | DCHECK(rv); |
| 128 | return result; |
| 129 | } |
| 130 | |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 131 | int PrefService::GetInteger(const std::string& path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 132 | DCHECK(CalledOnValidThread()); |
| 133 | |
| 134 | int result = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 135 | |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 136 | const base::Value* value = GetPreferenceValue(path); |
| 137 | if (!value) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 138 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 139 | return result; |
| 140 | } |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 141 | bool rv = value->GetAsInteger(&result); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 142 | DCHECK(rv); |
| 143 | return result; |
| 144 | } |
| 145 | |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 146 | double PrefService::GetDouble(const std::string& path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 147 | DCHECK(CalledOnValidThread()); |
| 148 | |
| 149 | double result = 0.0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 150 | |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 151 | const base::Value* value = GetPreferenceValue(path); |
| 152 | if (!value) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 153 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 154 | return result; |
| 155 | } |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 156 | bool rv = value->GetAsDouble(&result); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 157 | DCHECK(rv); |
| 158 | return result; |
| 159 | } |
| 160 | |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 161 | std::string PrefService::GetString(const std::string& path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 162 | DCHECK(CalledOnValidThread()); |
| 163 | |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 164 | std::string result; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 165 | |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 166 | const base::Value* value = GetPreferenceValue(path); |
| 167 | if (!value) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 168 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 169 | return result; |
| 170 | } |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 171 | bool rv = value->GetAsString(&result); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 172 | DCHECK(rv); |
| 173 | return result; |
| 174 | } |
| 175 | |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 176 | base::FilePath PrefService::GetFilePath(const std::string& path) const { |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 177 | DCHECK(CalledOnValidThread()); |
| 178 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 179 | base::FilePath result; |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 180 | |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 181 | const base::Value* value = GetPreferenceValue(path); |
| 182 | if (!value) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 183 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 184 | return base::FilePath(result); |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 185 | } |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 186 | bool rv = base::GetValueAsFilePath(*value, &result); |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 187 | DCHECK(rv); |
[email protected] | 68d9d35 | 2011-02-21 16:35:04 | [diff] [blame] | 188 | return result; |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 189 | } |
| 190 | |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 191 | bool PrefService::HasPrefPath(const std::string& path) const { |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 192 | const Preference* pref = FindPreference(path); |
| 193 | return pref && !pref->IsDefaultValue(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 194 | } |
| 195 | |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 196 | std::unique_ptr<base::DictionaryValue> PrefService::GetPreferenceValues() |
| 197 | const { |
[email protected] | ebd0b02 | 2011-01-27 13:24:14 | [diff] [blame] | 198 | DCHECK(CalledOnValidThread()); |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 199 | std::unique_ptr<base::DictionaryValue> out(new base::DictionaryValue); |
asvitkine | 36ff0c582 | 2014-11-25 22:58:18 | [diff] [blame] | 200 | for (const auto& it : *pref_registry_) { |
estade | a68b044 | 2015-05-12 18:11:50 | [diff] [blame] | 201 | out->Set(it.first, GetPreferenceValue(it.first)->CreateDeepCopy()); |
asvitkine | 36ff0c582 | 2014-11-25 22:58:18 | [diff] [blame] | 202 | } |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 203 | return out; |
asvitkine | 36ff0c582 | 2014-11-25 22:58:18 | [diff] [blame] | 204 | } |
| 205 | |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 206 | std::unique_ptr<base::DictionaryValue> |
| 207 | PrefService::GetPreferenceValuesOmitDefaults() const { |
asvitkine | 36ff0c582 | 2014-11-25 22:58:18 | [diff] [blame] | 208 | DCHECK(CalledOnValidThread()); |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 209 | std::unique_ptr<base::DictionaryValue> out(new base::DictionaryValue); |
asvitkine | 36ff0c582 | 2014-11-25 22:58:18 | [diff] [blame] | 210 | for (const auto& it : *pref_registry_) { |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 211 | const Preference* pref = FindPreference(it.first); |
asvitkine | 36ff0c582 | 2014-11-25 22:58:18 | [diff] [blame] | 212 | if (pref->IsDefaultValue()) |
| 213 | continue; |
estade | a68b044 | 2015-05-12 18:11:50 | [diff] [blame] | 214 | out->Set(it.first, pref->GetValue()->CreateDeepCopy()); |
[email protected] | ebd0b02 | 2011-01-27 13:24:14 | [diff] [blame] | 215 | } |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 216 | return out; |
[email protected] | ebd0b02 | 2011-01-27 13:24:14 | [diff] [blame] | 217 | } |
| 218 | |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 219 | std::unique_ptr<base::DictionaryValue> |
[email protected] | ddf421c3 | 2013-11-01 00:52:35 | [diff] [blame] | 220 | PrefService::GetPreferenceValuesWithoutPathExpansion() const { |
[email protected] | 2a1965c | 2013-10-02 16:07:01 | [diff] [blame] | 221 | DCHECK(CalledOnValidThread()); |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 222 | std::unique_ptr<base::DictionaryValue> out(new base::DictionaryValue); |
asvitkine | 36ff0c582 | 2014-11-25 22:58:18 | [diff] [blame] | 223 | for (const auto& it : *pref_registry_) { |
| 224 | const base::Value* value = GetPreferenceValue(it.first); |
[email protected] | 2a1965c | 2013-10-02 16:07:01 | [diff] [blame] | 225 | DCHECK(value); |
estade | a68b044 | 2015-05-12 18:11:50 | [diff] [blame] | 226 | out->SetWithoutPathExpansion(it.first, value->CreateDeepCopy()); |
[email protected] | 2a1965c | 2013-10-02 16:07:01 | [diff] [blame] | 227 | } |
danakj | 0c8d4aa | 2015-11-25 05:29:58 | [diff] [blame] | 228 | return out; |
[email protected] | 2a1965c | 2013-10-02 16:07:01 | [diff] [blame] | 229 | } |
| 230 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 231 | const PrefService::Preference* PrefService::FindPreference( |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 232 | const std::string& pref_name) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 233 | DCHECK(CalledOnValidThread()); |
[email protected] | d15d568 | 2012-10-23 17:50:42 | [diff] [blame] | 234 | PreferenceMap::iterator it = prefs_map_.find(pref_name); |
| 235 | if (it != prefs_map_.end()) |
| 236 | return &(it->second); |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 237 | const base::Value* default_value = NULL; |
| 238 | if (!pref_registry_->defaults()->GetValue(pref_name, &default_value)) |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 239 | return NULL; |
[email protected] | d15d568 | 2012-10-23 17:50:42 | [diff] [blame] | 240 | it = prefs_map_.insert( |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 241 | std::make_pair(pref_name, Preference( |
| 242 | this, pref_name, default_value->GetType()))).first; |
[email protected] | d15d568 | 2012-10-23 17:50:42 | [diff] [blame] | 243 | return &(it->second); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 244 | } |
| 245 | |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 246 | bool PrefService::ReadOnly() const { |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 247 | return user_pref_store_->ReadOnly(); |
[email protected] | acd78969c | 2010-12-08 09:49:11 | [diff] [blame] | 248 | } |
| 249 | |
[email protected] | 59c1071 | 2012-03-13 02:10:34 | [diff] [blame] | 250 | PrefService::PrefInitializationStatus PrefService::GetInitializationStatus() |
| 251 | const { |
| 252 | if (!user_pref_store_->IsInitializationComplete()) |
| 253 | return INITIALIZATION_STATUS_WAITING; |
| 254 | |
| 255 | switch (user_pref_store_->GetReadError()) { |
| 256 | case PersistentPrefStore::PREF_READ_ERROR_NONE: |
| 257 | return INITIALIZATION_STATUS_SUCCESS; |
| 258 | case PersistentPrefStore::PREF_READ_ERROR_NO_FILE: |
[email protected] | 88c6fb5 | 2013-04-09 10:39:18 | [diff] [blame] | 259 | return INITIALIZATION_STATUS_CREATED_NEW_PREF_STORE; |
[email protected] | 59c1071 | 2012-03-13 02:10:34 | [diff] [blame] | 260 | default: |
| 261 | return INITIALIZATION_STATUS_ERROR; |
| 262 | } |
| 263 | } |
| 264 | |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 265 | bool PrefService::IsManagedPreference(const std::string& pref_name) const { |
[email protected] | d90de1c0 | 2010-07-19 19:50:48 | [diff] [blame] | 266 | const Preference* pref = FindPreference(pref_name); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 267 | return pref && pref->IsManaged(); |
[email protected] | d90de1c0 | 2010-07-19 19:50:48 | [diff] [blame] | 268 | } |
| 269 | |
khannans | b2025b7 | 2014-12-19 16:04:25 | [diff] [blame] | 270 | bool PrefService::IsPreferenceManagedByCustodian( |
| 271 | const std::string& pref_name) const { |
| 272 | const Preference* pref = FindPreference(pref_name); |
| 273 | return pref && pref->IsManagedByCustodian(); |
| 274 | } |
| 275 | |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 276 | bool PrefService::IsUserModifiablePreference( |
| 277 | const std::string& pref_name) const { |
[email protected] | d6bbd293 | 2012-03-19 17:10:07 | [diff] [blame] | 278 | const Preference* pref = FindPreference(pref_name); |
| 279 | return pref && pref->IsUserModifiable(); |
| 280 | } |
| 281 | |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 282 | const base::DictionaryValue* PrefService::GetDictionary( |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 283 | const std::string& path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 284 | DCHECK(CalledOnValidThread()); |
| 285 | |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 286 | const base::Value* value = GetPreferenceValue(path); |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 287 | if (!value) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 288 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 289 | return NULL; |
| 290 | } |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 291 | if (value->GetType() != base::Value::Type::DICTIONARY) { |
[email protected] | 11b040b | 2011-02-02 12:42:25 | [diff] [blame] | 292 | NOTREACHED(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 293 | return NULL; |
[email protected] | 11b040b | 2011-02-02 12:42:25 | [diff] [blame] | 294 | } |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 295 | return static_cast<const base::DictionaryValue*>(value); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 296 | } |
| 297 | |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 298 | const base::Value* PrefService::GetUserPrefValue( |
| 299 | const std::string& path) const { |
[email protected] | 1a5a31f | 2012-04-26 20:21:34 | [diff] [blame] | 300 | DCHECK(CalledOnValidThread()); |
| 301 | |
| 302 | const Preference* pref = FindPreference(path); |
| 303 | if (!pref) { |
| 304 | NOTREACHED() << "Trying to get an unregistered pref: " << path; |
| 305 | return NULL; |
| 306 | } |
| 307 | |
| 308 | // Look for an existing preference in the user store. If it doesn't |
| 309 | // exist, return NULL. |
| 310 | base::Value* value = NULL; |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 311 | if (!user_pref_store_->GetMutableValue(path, &value)) |
[email protected] | 1a5a31f | 2012-04-26 20:21:34 | [diff] [blame] | 312 | return NULL; |
[email protected] | 1a5a31f | 2012-04-26 20:21:34 | [diff] [blame] | 313 | |
| 314 | if (!value->IsType(pref->GetType())) { |
| 315 | NOTREACHED() << "Pref value type doesn't match registered type."; |
| 316 | return NULL; |
| 317 | } |
| 318 | |
| 319 | return value; |
| 320 | } |
| 321 | |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 322 | void PrefService::SetDefaultPrefValue(const std::string& path, |
[email protected] | 5879cef | 2013-03-02 17:02:25 | [diff] [blame] | 323 | base::Value* value) { |
| 324 | DCHECK(CalledOnValidThread()); |
| 325 | pref_registry_->SetDefaultPrefValue(path, value); |
| 326 | } |
| 327 | |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 328 | const base::Value* PrefService::GetDefaultPrefValue( |
| 329 | const std::string& path) const { |
[email protected] | 35a6fd1 | 2012-05-28 18:08:08 | [diff] [blame] | 330 | DCHECK(CalledOnValidThread()); |
| 331 | // Lookup the preference in the default store. |
| 332 | const base::Value* value = NULL; |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 333 | if (!pref_registry_->defaults()->GetValue(path, &value)) { |
[email protected] | 35a6fd1 | 2012-05-28 18:08:08 | [diff] [blame] | 334 | NOTREACHED() << "Default value missing for pref: " << path; |
| 335 | return NULL; |
| 336 | } |
| 337 | return value; |
| 338 | } |
| 339 | |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 340 | const base::ListValue* PrefService::GetList(const std::string& path) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 341 | DCHECK(CalledOnValidThread()); |
| 342 | |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 343 | const base::Value* value = GetPreferenceValue(path); |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 344 | if (!value) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 345 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 346 | return NULL; |
| 347 | } |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 348 | if (value->GetType() != base::Value::Type::LIST) { |
[email protected] | 11b040b | 2011-02-02 12:42:25 | [diff] [blame] | 349 | NOTREACHED(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 350 | return NULL; |
[email protected] | 11b040b | 2011-02-02 12:42:25 | [diff] [blame] | 351 | } |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 352 | return static_cast<const base::ListValue*>(value); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 353 | } |
| 354 | |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 355 | void PrefService::AddPrefObserver(const std::string& path, PrefObserver* obs) { |
[email protected] | d81288a0 | 2010-08-18 07:25:50 | [diff] [blame] | 356 | pref_notifier_->AddPrefObserver(path, obs); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 357 | } |
| 358 | |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 359 | void PrefService::RemovePrefObserver(const std::string& path, |
| 360 | PrefObserver* obs) { |
[email protected] | d81288a0 | 2010-08-18 07:25:50 | [diff] [blame] | 361 | pref_notifier_->RemovePrefObserver(path, obs); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 362 | } |
| 363 | |
[email protected] | a6a7ced | 2012-11-01 17:24:18 | [diff] [blame] | 364 | void PrefService::AddPrefInitObserver(base::Callback<void(bool)> obs) { |
| 365 | pref_notifier_->AddInitObserver(obs); |
| 366 | } |
| 367 | |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 368 | PrefRegistry* PrefService::DeprecatedGetPrefRegistry() { |
| 369 | return pref_registry_.get(); |
| 370 | } |
| 371 | |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 372 | void PrefService::ClearPref(const std::string& path) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 373 | DCHECK(CalledOnValidThread()); |
| 374 | |
| 375 | const Preference* pref = FindPreference(path); |
| 376 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 377 | NOTREACHED() << "Trying to clear an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 378 | return; |
| 379 | } |
raymes | f3a929b0 | 2015-05-07 03:54:45 | [diff] [blame] | 380 | user_pref_store_->RemoveValue(path, GetWriteFlags(pref)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 381 | } |
| 382 | |
dvadym | 53fc0d4 | 2016-02-05 13:34:57 | [diff] [blame] | 383 | void PrefService::ClearMutableValues() { |
| 384 | user_pref_store_->ClearMutableValues(); |
| 385 | } |
| 386 | |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 387 | void PrefService::Set(const std::string& path, const base::Value& value) { |
vabr | bce355c | 2017-03-23 18:52:43 | [diff] [blame] | 388 | SetUserPrefValue(path, value.CreateDeepCopy()); |
[email protected] | a048d7e4 | 2009-12-01 01:02:39 | [diff] [blame] | 389 | } |
| 390 | |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 391 | void PrefService::SetBoolean(const std::string& path, bool value) { |
vabr | bce355c | 2017-03-23 18:52:43 | [diff] [blame] | 392 | SetUserPrefValue(path, base::MakeUnique<base::Value>(value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 393 | } |
| 394 | |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 395 | void PrefService::SetInteger(const std::string& path, int value) { |
vabr | bce355c | 2017-03-23 18:52:43 | [diff] [blame] | 396 | SetUserPrefValue(path, base::MakeUnique<base::Value>(value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 397 | } |
| 398 | |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 399 | void PrefService::SetDouble(const std::string& path, double value) { |
vabr | bce355c | 2017-03-23 18:52:43 | [diff] [blame] | 400 | SetUserPrefValue(path, base::MakeUnique<base::Value>(value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 401 | } |
| 402 | |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 403 | void PrefService::SetString(const std::string& path, const std::string& value) { |
vabr | bce355c | 2017-03-23 18:52:43 | [diff] [blame] | 404 | SetUserPrefValue(path, base::MakeUnique<base::Value>(value)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 405 | } |
| 406 | |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 407 | void PrefService::SetFilePath(const std::string& path, |
| 408 | const base::FilePath& value) { |
[email protected] | 8703b2b | 2011-03-15 09:51:50 | [diff] [blame] | 409 | SetUserPrefValue(path, base::CreateFilePathValue(value)); |
[email protected] | b963600 | 2009-03-04 00:05:25 | [diff] [blame] | 410 | } |
| 411 | |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 412 | void PrefService::SetInt64(const std::string& path, int64_t value) { |
vabr | bce355c | 2017-03-23 18:52:43 | [diff] [blame] | 413 | SetUserPrefValue(path, |
| 414 | base::MakeUnique<base::Value>(base::Int64ToString(value))); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 415 | } |
| 416 | |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 417 | int64_t PrefService::GetInt64(const std::string& path) const { |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 418 | DCHECK(CalledOnValidThread()); |
| 419 | |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 420 | const base::Value* value = GetPreferenceValue(path); |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 421 | if (!value) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 422 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
[email protected] | c345330 | 2009-12-01 01:33:08 | [diff] [blame] | 423 | return 0; |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 424 | } |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 425 | std::string result("0"); |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 426 | bool rv = value->GetAsString(&result); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 427 | DCHECK(rv); |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 428 | |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 429 | int64_t val; |
[email protected] | dc9a676 | 2010-08-16 07:13:53 | [diff] [blame] | 430 | base::StringToInt64(result, &val); |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 431 | return val; |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 432 | } |
| 433 | |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 434 | void PrefService::SetUint64(const std::string& path, uint64_t value) { |
vabr | bce355c | 2017-03-23 18:52:43 | [diff] [blame] | 435 | SetUserPrefValue(path, |
| 436 | base::MakeUnique<base::Value>(base::Uint64ToString(value))); |
[email protected] | 3cbe081 | 2012-07-03 02:51:43 | [diff] [blame] | 437 | } |
| 438 | |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 439 | uint64_t PrefService::GetUint64(const std::string& path) const { |
[email protected] | 3cbe081 | 2012-07-03 02:51:43 | [diff] [blame] | 440 | DCHECK(CalledOnValidThread()); |
| 441 | |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 442 | const base::Value* value = GetPreferenceValue(path); |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 443 | if (!value) { |
[email protected] | 3cbe081 | 2012-07-03 02:51:43 | [diff] [blame] | 444 | NOTREACHED() << "Trying to read an unregistered pref: " << path; |
| 445 | return 0; |
| 446 | } |
| 447 | std::string result("0"); |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 448 | bool rv = value->GetAsString(&result); |
[email protected] | 3cbe081 | 2012-07-03 02:51:43 | [diff] [blame] | 449 | DCHECK(rv); |
| 450 | |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 451 | uint64_t val; |
[email protected] | 3cbe081 | 2012-07-03 02:51:43 | [diff] [blame] | 452 | base::StringToUint64(result, &val); |
| 453 | return val; |
| 454 | } |
| 455 | |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 456 | base::Value* PrefService::GetMutableUserPref(const std::string& path, |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 457 | base::Value::Type type) { |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 458 | CHECK(type == base::Value::Type::DICTIONARY || |
| 459 | type == base::Value::Type::LIST); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 460 | DCHECK(CalledOnValidThread()); |
| 461 | |
| 462 | const Preference* pref = FindPreference(path); |
| 463 | if (!pref) { |
[email protected] | b154e6f | 2009-03-06 01:52:40 | [diff] [blame] | 464 | NOTREACHED() << "Trying to get an unregistered pref: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 465 | return NULL; |
| 466 | } |
[email protected] | 26418b7 | 2011-03-30 14:07:39 | [diff] [blame] | 467 | if (pref->GetType() != type) { |
| 468 | NOTREACHED() << "Wrong type for GetMutableValue: " << path; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 469 | return NULL; |
| 470 | } |
| 471 | |
[email protected] | e025089 | 2010-10-01 18:57:53 | [diff] [blame] | 472 | // Look for an existing preference in the user store. If it doesn't |
| 473 | // exist or isn't the correct type, create a new user preference. |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 474 | base::Value* value = NULL; |
[email protected] | 892f1d6 | 2012-11-08 18:24:34 | [diff] [blame] | 475 | if (!user_pref_store_->GetMutableValue(path, &value) || |
[email protected] | 26418b7 | 2011-03-30 14:07:39 | [diff] [blame] | 476 | !value->IsType(type)) { |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 477 | if (type == base::Value::Type::DICTIONARY) { |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 478 | value = new base::DictionaryValue; |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 479 | } else if (type == base::Value::Type::LIST) { |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 480 | value = new base::ListValue; |
[email protected] | 26418b7 | 2011-03-30 14:07:39 | [diff] [blame] | 481 | } else { |
| 482 | NOTREACHED(); |
| 483 | } |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 484 | user_pref_store_->SetValueSilently(path, base::WrapUnique(value), |
estade | 0bd407f | 2015-06-26 18:16:18 | [diff] [blame] | 485 | GetWriteFlags(pref)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 486 | } |
[email protected] | 26418b7 | 2011-03-30 14:07:39 | [diff] [blame] | 487 | return value; |
| 488 | } |
| 489 | |
[email protected] | 68bf41a | 2011-03-25 16:38:31 | [diff] [blame] | 490 | void PrefService::ReportUserPrefChanged(const std::string& key) { |
[email protected] | 86bb4e65 | 2014-07-10 17:09:51 | [diff] [blame] | 491 | DCHECK(CalledOnValidThread()); |
raymes | f3a929b0 | 2015-05-07 03:54:45 | [diff] [blame] | 492 | user_pref_store_->ReportValueChanged(key, GetWriteFlags(FindPreference(key))); |
[email protected] | f89ee34 | 2011-03-07 09:28:27 | [diff] [blame] | 493 | } |
| 494 | |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 495 | void PrefService::SetUserPrefValue(const std::string& path, |
vabr | bce355c | 2017-03-23 18:52:43 | [diff] [blame] | 496 | std::unique_ptr<base::Value> new_value) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 497 | DCHECK(CalledOnValidThread()); |
| 498 | |
| 499 | const Preference* pref = FindPreference(path); |
| 500 | if (!pref) { |
| 501 | NOTREACHED() << "Trying to write an unregistered pref: " << path; |
| 502 | return; |
| 503 | } |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 504 | if (pref->GetType() != new_value->GetType()) { |
| 505 | NOTREACHED() << "Trying to set pref " << path |
| 506 | << " of type " << pref->GetType() |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 507 | << " to value of type " << new_value->GetType(); |
| 508 | return; |
| 509 | } |
| 510 | |
vabr | bce355c | 2017-03-23 18:52:43 | [diff] [blame] | 511 | user_pref_store_->SetValue(path, std::move(new_value), GetWriteFlags(pref)); |
[email protected] | 73c4793 | 2010-12-06 18:13:43 | [diff] [blame] | 512 | } |
| 513 | |
[email protected] | 5b19952 | 2012-12-22 17:24:44 | [diff] [blame] | 514 | void PrefService::UpdateCommandLinePrefStore(PrefStore* command_line_store) { |
| 515 | pref_value_store_->UpdateCommandLinePrefStore(command_line_store); |
[email protected] | d3b05ea | 2012-01-24 22:57:05 | [diff] [blame] | 516 | } |
| 517 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 518 | /////////////////////////////////////////////////////////////////////////////// |
| 519 | // PrefService::Preference |
| 520 | |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 521 | PrefService::Preference::Preference(const PrefService* service, |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 522 | const std::string& name, |
[email protected] | bab1c13f | 2011-08-12 20:59:02 | [diff] [blame] | 523 | base::Value::Type type) |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 524 | : name_(name), type_(type), pref_service_(service) { |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 525 | DCHECK(service); |
raymes | 76de1af | 2015-05-06 03:22:21 | [diff] [blame] | 526 | // Cache the registration flags at creation time to avoid multiple map lookups |
| 527 | // later. |
| 528 | registration_flags_ = service->pref_registry_->GetRegistrationFlags(name_); |
[email protected] | 99cc9a0 | 2010-09-17 07:53:28 | [diff] [blame] | 529 | } |
| 530 | |
[email protected] | fb8fdf1 | 2012-08-21 16:28:20 | [diff] [blame] | 531 | const std::string PrefService::Preference::name() const { |
| 532 | return name_; |
| 533 | } |
| 534 | |
[email protected] | bab1c13f | 2011-08-12 20:59:02 | [diff] [blame] | 535 | base::Value::Type PrefService::Preference::GetType() const { |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 536 | return type_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 537 | } |
| 538 | |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 539 | const base::Value* PrefService::Preference::GetValue() const { |
| 540 | const base::Value* result= pref_service_->GetPreferenceValue(name_); |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 541 | DCHECK(result) << "Must register pref before getting its value"; |
| 542 | return result; |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 543 | } |
| 544 | |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 545 | const base::Value* PrefService::Preference::GetRecommendedValue() const { |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 546 | DCHECK(pref_service_->FindPreference(name_)) |
| 547 | << "Must register pref before getting its value"; |
[email protected] | 7ca0f36 | 2012-07-30 10:14:03 | [diff] [blame] | 548 | |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 549 | const base::Value* found_value = NULL; |
[email protected] | 7ca0f36 | 2012-07-30 10:14:03 | [diff] [blame] | 550 | if (pref_value_store()->GetRecommendedValue(name_, type_, &found_value)) { |
| 551 | DCHECK(found_value->IsType(type_)); |
| 552 | return found_value; |
| 553 | } |
| 554 | |
| 555 | // The pref has no recommended value. |
| 556 | return NULL; |
| 557 | } |
| 558 | |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 559 | bool PrefService::Preference::IsManaged() const { |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 560 | return pref_value_store()->PrefValueInManagedStore(name_); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 561 | } |
| 562 | |
khannans | b2025b7 | 2014-12-19 16:04:25 | [diff] [blame] | 563 | bool PrefService::Preference::IsManagedByCustodian() const { |
iceman | 4179f17c | 2017-03-29 19:50:09 | [diff] [blame^] | 564 | return pref_value_store()->PrefValueInSupervisedStore(name_); |
khannans | b2025b7 | 2014-12-19 16:04:25 | [diff] [blame] | 565 | } |
| 566 | |
[email protected] | a543728 | 2011-12-12 12:33:21 | [diff] [blame] | 567 | bool PrefService::Preference::IsRecommended() const { |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 568 | return pref_value_store()->PrefValueFromRecommendedStore(name_); |
[email protected] | a543728 | 2011-12-12 12:33:21 | [diff] [blame] | 569 | } |
| 570 | |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 571 | bool PrefService::Preference::HasExtensionSetting() const { |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 572 | return pref_value_store()->PrefValueInExtensionStore(name_); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | bool PrefService::Preference::HasUserSetting() const { |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 576 | return pref_value_store()->PrefValueInUserStore(name_); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | bool PrefService::Preference::IsExtensionControlled() const { |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 580 | return pref_value_store()->PrefValueFromExtensionStore(name_); |
[email protected] | 40a47c16 | 2010-09-09 11:14:01 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | bool PrefService::Preference::IsUserControlled() const { |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 584 | return pref_value_store()->PrefValueFromUserStore(name_); |
[email protected] | c3b54f37 | 2010-09-14 08:25:07 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | bool PrefService::Preference::IsDefaultValue() const { |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 588 | return pref_value_store()->PrefValueFromDefaultStore(name_); |
[email protected] | d7449e8 | 2010-07-14 11:42:35 | [diff] [blame] | 589 | } |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 590 | |
| 591 | bool PrefService::Preference::IsUserModifiable() const { |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 592 | return pref_value_store()->PrefValueUserModifiable(name_); |
[email protected] | 74379bc5 | 2010-07-21 13:54:08 | [diff] [blame] | 593 | } |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 594 | |
| 595 | bool PrefService::Preference::IsExtensionModifiable() const { |
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 596 | return pref_value_store()->PrefValueExtensionModifiable(name_); |
[email protected] | 9a28f13 | 2011-02-24 21:15:16 | [diff] [blame] | 597 | } |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 598 | |
| 599 | const base::Value* PrefService::GetPreferenceValue( |
| 600 | const std::string& path) const { |
| 601 | DCHECK(CalledOnValidThread()); |
battre | 0a69def | 2015-01-05 18:50:19 | [diff] [blame] | 602 | |
| 603 | // TODO(battre): This is a check for crbug.com/435208. After analyzing some |
| 604 | // crash dumps it looks like the PrefService is accessed even though it has |
| 605 | // been cleared already. |
| 606 | CHECK(pref_registry_); |
| 607 | CHECK(pref_registry_->defaults()); |
| 608 | CHECK(pref_value_store_); |
| 609 | |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 610 | const base::Value* default_value = NULL; |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 611 | if (pref_registry_->defaults()->GetValue(path, &default_value)) { |
[email protected] | a43a667b | 2013-06-14 17:56:08 | [diff] [blame] | 612 | const base::Value* found_value = NULL; |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 613 | base::Value::Type default_type = default_value->GetType(); |
| 614 | if (pref_value_store_->GetValue(path, default_type, &found_value)) { |
| 615 | DCHECK(found_value->IsType(default_type)); |
| 616 | return found_value; |
| 617 | } else { |
| 618 | // Every registered preference has at least a default value. |
| 619 | NOTREACHED() << "no valid value found for registered pref " << path; |
| 620 | } |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 621 | } |
| 622 | |
[email protected] | 18038f8 | 2012-11-20 13:46:58 | [diff] [blame] | 623 | return NULL; |
| 624 | } |