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