blob: c7e487de3e4a27cb9ff2a5f3d0c6d05a30accbd6 [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2012 The Chromium Authors
[email protected]39565dd2011-11-14 12:09:272// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
brettwf00b9b42016-02-01 22:11:385#include "components/prefs/overlay_user_pref_store.h"
[email protected]39565dd2011-11-14 12:09:276
dcheng5f043bc2016-04-22 19:09:067#include <memory>
Peter Kastingb2777ce2021-07-20 18:28:548#include <ostream>
Roland Bock24a7c862022-09-26 07:29:489#include <string>
danakj0c8d4aa2015-11-25 05:29:5810#include <utility>
11
dcheng5f043bc2016-04-22 19:09:0612#include "base/memory/ptr_util.h"
Keishi Hattori0e45c022021-11-27 09:25:5213#include "base/memory/raw_ptr.h"
David Sanders88b24432022-02-28 01:10:0214#include "base/observer_list.h"
Roland Bock24a7c862022-09-26 07:29:4815#include "base/strings/string_piece.h"
[email protected]39565dd2011-11-14 12:09:2716#include "base/values.h"
Johan Tibell0595f762017-05-25 23:54:5017#include "components/prefs/in_memory_pref_store.h"
[email protected]39565dd2011-11-14 12:09:2718
Johan Tibell0595f762017-05-25 23:54:5019// Allows us to monitor two pref stores and tell updates from them apart. It
20// essentially mimics a Callback for the Observer interface (e.g. it allows
21// binding additional arguments).
22class OverlayUserPrefStore::ObserverAdapter : public PrefStore::Observer {
23 public:
Ramin Halavatif623bafe92018-07-17 12:12:5824 ObserverAdapter(bool ephemeral, OverlayUserPrefStore* parent)
25 : ephemeral_user_pref_store_(ephemeral), parent_(parent) {}
Johan Tibell0595f762017-05-25 23:54:5026
27 // Methods of PrefStore::Observer.
28 void OnPrefValueChanged(const std::string& key) override {
Ramin Halavatif623bafe92018-07-17 12:12:5829 parent_->OnPrefValueChanged(ephemeral_user_pref_store_, key);
Johan Tibell0595f762017-05-25 23:54:5030 }
31 void OnInitializationCompleted(bool succeeded) override {
Ramin Halavatif623bafe92018-07-17 12:12:5832 parent_->OnInitializationCompleted(ephemeral_user_pref_store_, succeeded);
Johan Tibell0595f762017-05-25 23:54:5033 }
34
35 private:
Ramin Halavatif623bafe92018-07-17 12:12:5836 // Is the update for the ephemeral?
37 const bool ephemeral_user_pref_store_;
Keishi Hattori0e45c022021-11-27 09:25:5238 const raw_ptr<OverlayUserPrefStore> parent_;
Johan Tibell0595f762017-05-25 23:54:5039};
40
Ramin Halavatif623bafe92018-07-17 12:12:5841OverlayUserPrefStore::OverlayUserPrefStore(PersistentPrefStore* persistent)
42 : OverlayUserPrefStore(new InMemoryPrefStore(), persistent) {}
Johan Tibell0595f762017-05-25 23:54:5043
Ramin Halavatif623bafe92018-07-17 12:12:5844OverlayUserPrefStore::OverlayUserPrefStore(PersistentPrefStore* ephemeral,
45 PersistentPrefStore* persistent)
46 : ephemeral_pref_store_observer_(
Jinho Bang84b58bd2018-01-01 21:44:4847 std::make_unique<OverlayUserPrefStore::ObserverAdapter>(true, this)),
Ramin Halavatif623bafe92018-07-17 12:12:5848 persistent_pref_store_observer_(
Jinho Bang84b58bd2018-01-01 21:44:4849 std::make_unique<OverlayUserPrefStore::ObserverAdapter>(false, this)),
Ramin Halavatif623bafe92018-07-17 12:12:5850 ephemeral_user_pref_store_(ephemeral),
51 persistent_user_pref_store_(persistent) {
52 DCHECK(ephemeral->IsInitializationComplete());
53 ephemeral_user_pref_store_->AddObserver(ephemeral_pref_store_observer_.get());
54 persistent_user_pref_store_->AddObserver(
55 persistent_pref_store_observer_.get());
[email protected]39565dd2011-11-14 12:09:2756}
57
[email protected]39565dd2011-11-14 12:09:2758bool OverlayUserPrefStore::IsSetInOverlay(const std::string& key) const {
Ramin Halavatif623bafe92018-07-17 12:12:5859 return ephemeral_user_pref_store_->GetValue(key, nullptr);
[email protected]39565dd2011-11-14 12:09:2760}
61
62void OverlayUserPrefStore::AddObserver(PrefStore::Observer* observer) {
63 observers_.AddObserver(observer);
64}
65
66void OverlayUserPrefStore::RemoveObserver(PrefStore::Observer* observer) {
67 observers_.RemoveObserver(observer);
68}
69
[email protected]14e0ec62013-08-26 22:01:3970bool OverlayUserPrefStore::HasObservers() const {
Mitsuru Oshima28f9b6812021-01-20 19:33:0771 return !observers_.empty();
[email protected]d3b05ea2012-01-24 22:57:0572}
73
[email protected]39565dd2011-11-14 12:09:2774bool OverlayUserPrefStore::IsInitializationComplete() const {
Ramin Halavatif623bafe92018-07-17 12:12:5875 return persistent_user_pref_store_->IsInitializationComplete() &&
76 ephemeral_user_pref_store_->IsInitializationComplete();
[email protected]39565dd2011-11-14 12:09:2777}
78
Roland Bock24a7c862022-09-26 07:29:4879bool OverlayUserPrefStore::GetValue(base::StringPiece key,
[email protected]a43a667b2013-06-14 17:56:0880 const base::Value** result) const {
Ramin Halavatif623bafe92018-07-17 12:12:5881 // If the |key| shall NOT be stored in the ephemeral store, there must not
[email protected]39565dd2011-11-14 12:09:2782 // be an entry.
Ramin Halavatif623bafe92018-07-17 12:12:5883 DCHECK(!ShallBeStoredInPersistent(key) ||
84 !ephemeral_user_pref_store_->GetValue(key, nullptr));
[email protected]39565dd2011-11-14 12:09:2785
Ramin Halavatif623bafe92018-07-17 12:12:5886 if (ephemeral_user_pref_store_->GetValue(key, result))
[email protected]892f1d62012-11-08 18:24:3487 return true;
Ramin Halavatif623bafe92018-07-17 12:12:5888 return persistent_user_pref_store_->GetValue(key, result);
[email protected]39565dd2011-11-14 12:09:2789}
90
Matt Menke38ac93c2022-08-17 03:12:5991base::Value::Dict OverlayUserPrefStore::GetValues() const {
Ramin Halavatif623bafe92018-07-17 12:12:5892 auto values = ephemeral_user_pref_store_->GetValues();
93 auto persistent_values = persistent_user_pref_store_->GetValues();
94
95 // Output |values| are read from |ephemeral_user_pref_store_| (in-memory
96 // store). Then the values of preferences in |persistent_names_set_| are
97 // overwritten by the content of |persistent_user_pref_store_| (the persistent
98 // store).
99 for (const auto& key : persistent_names_set_) {
Matt Menke38ac93c2022-08-17 03:12:59100 absl::optional<base::Value> out_value =
101 persistent_values.ExtractByDottedPath(key);
Song Fangzhen61767882021-07-16 04:59:40102 if (out_value.has_value()) {
Matt Menke38ac93c2022-08-17 03:12:59103 values.SetByDottedPath(key, std::move(*out_value));
tibelle23659b42017-02-23 01:44:13104 }
105 }
106 return values;
107}
108
[email protected]892f1d62012-11-08 18:24:34109bool OverlayUserPrefStore::GetMutableValue(const std::string& key,
[email protected]a43a667b2013-06-14 17:56:08110 base::Value** result) {
Ramin Halavatif623bafe92018-07-17 12:12:58111 if (ShallBeStoredInPersistent(key))
112 return persistent_user_pref_store_->GetMutableValue(key, result);
[email protected]39565dd2011-11-14 12:09:27113
Ramin Halavatif623bafe92018-07-17 12:12:58114 written_ephemeral_names_.insert(key);
115 if (ephemeral_user_pref_store_->GetMutableValue(key, result))
[email protected]892f1d62012-11-08 18:24:34116 return true;
[email protected]39565dd2011-11-14 12:09:27117
Ramin Halavatif623bafe92018-07-17 12:12:58118 // Try to create copy of persistent if the ephemeral does not contain a value.
119 base::Value* persistent_value = nullptr;
120 if (!persistent_user_pref_store_->GetMutableValue(key, &persistent_value))
[email protected]892f1d62012-11-08 18:24:34121 return false;
[email protected]39565dd2011-11-14 12:09:27122
Ramin Halavatif623bafe92018-07-17 12:12:58123 ephemeral_user_pref_store_->SetValue(
Roland Bocke01119d2022-09-29 01:21:41124 key, persistent_value->Clone(),
Ramin Halavatif623bafe92018-07-17 12:12:58125 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
Vladislav Kuzkokov8ee75fbc2019-01-14 14:14:04126 ephemeral_user_pref_store_->GetMutableValue(key, result);
[email protected]892f1d62012-11-08 18:24:34127 return true;
[email protected]39565dd2011-11-14 12:09:27128}
129
130void OverlayUserPrefStore::SetValue(const std::string& key,
Roland Bocke01119d2022-09-29 01:21:41131 base::Value value,
avi9ef8bb02015-12-24 05:29:36132 uint32_t flags) {
Ramin Halavatif623bafe92018-07-17 12:12:58133 if (ShallBeStoredInPersistent(key)) {
134 persistent_user_pref_store_->SetValue(key, std::move(value), flags);
[email protected]39565dd2011-11-14 12:09:27135 return;
136 }
137
Ramin Halavatif623bafe92018-07-17 12:12:58138 // TODO(https://crbug.com/861722): If we always store in in-memory storage
139 // and conditionally also stored in persistent one, we wouldn't have to do a
140 // complex merge in GetValues().
141 written_ephemeral_names_.insert(key);
142 ephemeral_user_pref_store_->SetValue(key, std::move(value), flags);
[email protected]39565dd2011-11-14 12:09:27143}
144
145void OverlayUserPrefStore::SetValueSilently(const std::string& key,
Roland Bocke01119d2022-09-29 01:21:41146 base::Value value,
avi9ef8bb02015-12-24 05:29:36147 uint32_t flags) {
Ramin Halavatif623bafe92018-07-17 12:12:58148 if (ShallBeStoredInPersistent(key)) {
149 persistent_user_pref_store_->SetValueSilently(key, std::move(value), flags);
[email protected]39565dd2011-11-14 12:09:27150 return;
151 }
152
Ramin Halavatif623bafe92018-07-17 12:12:58153 written_ephemeral_names_.insert(key);
154 ephemeral_user_pref_store_->SetValueSilently(key, std::move(value), flags);
[email protected]39565dd2011-11-14 12:09:27155}
156
avi9ef8bb02015-12-24 05:29:36157void OverlayUserPrefStore::RemoveValue(const std::string& key, uint32_t flags) {
Ramin Halavatif623bafe92018-07-17 12:12:58158 if (ShallBeStoredInPersistent(key)) {
159 persistent_user_pref_store_->RemoveValue(key, flags);
[email protected]39565dd2011-11-14 12:09:27160 return;
161 }
162
Ramin Halavatif623bafe92018-07-17 12:12:58163 written_ephemeral_names_.insert(key);
164 ephemeral_user_pref_store_->RemoveValue(key, flags);
[email protected]39565dd2011-11-14 12:09:27165}
166
Anatoliy Potapchuk5d9d8de2020-04-21 02:09:26167void OverlayUserPrefStore::RemoveValuesByPrefixSilently(
168 const std::string& prefix) {
169 NOTIMPLEMENTED();
170}
171
[email protected]39565dd2011-11-14 12:09:27172bool OverlayUserPrefStore::ReadOnly() const {
173 return false;
174}
175
[email protected]59c10712012-03-13 02:10:34176PersistentPrefStore::PrefReadError OverlayUserPrefStore::GetReadError() const {
177 return PersistentPrefStore::PREF_READ_ERROR_NONE;
178}
179
[email protected]39565dd2011-11-14 12:09:27180PersistentPrefStore::PrefReadError OverlayUserPrefStore::ReadPrefs() {
181 // We do not read intentionally.
Ramin Halavatif623bafe92018-07-17 12:12:58182 OnInitializationCompleted(/* ephemeral */ false, true);
[email protected]39565dd2011-11-14 12:09:27183 return PersistentPrefStore::PREF_READ_ERROR_NONE;
184}
185
186void OverlayUserPrefStore::ReadPrefsAsync(
187 ReadErrorDelegate* error_delegate_raw) {
dcheng5f043bc2016-04-22 19:09:06188 std::unique_ptr<ReadErrorDelegate> error_delegate(error_delegate_raw);
[email protected]39565dd2011-11-14 12:09:27189 // We do not read intentionally.
Ramin Halavatif623bafe92018-07-17 12:12:58190 OnInitializationCompleted(/* ephemeral */ false, true);
[email protected]39565dd2011-11-14 12:09:27191}
192
Gabriel Charette788eaf62018-08-07 20:11:46193void OverlayUserPrefStore::CommitPendingWrite(
194 base::OnceClosure reply_callback,
195 base::OnceClosure synchronous_done_callback) {
196 persistent_user_pref_store_->CommitPendingWrite(
197 std::move(reply_callback), std::move(synchronous_done_callback));
[email protected]39565dd2011-11-14 12:09:27198 // We do not write our content intentionally.
199}
200
benwells26730592015-05-28 13:08:08201void OverlayUserPrefStore::SchedulePendingLossyWrites() {
Ramin Halavatif623bafe92018-07-17 12:12:58202 persistent_user_pref_store_->SchedulePendingLossyWrites();
benwells26730592015-05-28 13:08:08203}
204
raymes76de1af2015-05-06 03:22:21205void OverlayUserPrefStore::ReportValueChanged(const std::string& key,
avi9ef8bb02015-12-24 05:29:36206 uint32_t flags) {
ericwilligers42b92c12016-10-24 20:21:13207 for (PrefStore::Observer& observer : observers_)
208 observer.OnPrefValueChanged(key);
[email protected]39565dd2011-11-14 12:09:27209}
210
Ramin Halavatif623bafe92018-07-17 12:12:58211void OverlayUserPrefStore::RegisterPersistentPref(const std::string& key) {
Johan Tibell6566c8ac2017-05-11 03:22:17212 DCHECK(!key.empty()) << "Key is empty";
Ramin Halavatif623bafe92018-07-17 12:12:58213 DCHECK(persistent_names_set_.find(key) == persistent_names_set_.end())
214 << "Key already registered: " << key;
215 persistent_names_set_.insert(key);
[email protected]39565dd2011-11-14 12:09:27216}
217
dvadym53fc0d42016-02-05 13:34:57218void OverlayUserPrefStore::ClearMutableValues() {
Ramin Halavatif623bafe92018-07-17 12:12:58219 for (const auto& key : written_ephemeral_names_) {
220 ephemeral_user_pref_store_->RemoveValue(
221 key, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
Johan Tibell0595f762017-05-25 23:54:50222 }
dvadym53fc0d42016-02-05 13:34:57223}
224
proberge45e347282017-08-16 21:24:05225void OverlayUserPrefStore::OnStoreDeletionFromDisk() {
Ramin Halavatif623bafe92018-07-17 12:12:58226 persistent_user_pref_store_->OnStoreDeletionFromDisk();
proberge45e347282017-08-16 21:24:05227}
228
[email protected]2dea5c02012-04-25 07:01:07229OverlayUserPrefStore::~OverlayUserPrefStore() {
Ramin Halavatif623bafe92018-07-17 12:12:58230 ephemeral_user_pref_store_->RemoveObserver(
231 ephemeral_pref_store_observer_.get());
232 persistent_user_pref_store_->RemoveObserver(
233 persistent_pref_store_observer_.get());
Johan Tibell0595f762017-05-25 23:54:50234}
235
Ramin Halavatif623bafe92018-07-17 12:12:58236void OverlayUserPrefStore::OnPrefValueChanged(bool ephemeral,
Johan Tibell0595f762017-05-25 23:54:50237 const std::string& key) {
Ramin Halavatif623bafe92018-07-17 12:12:58238 if (ephemeral) {
Johan Tibell0595f762017-05-25 23:54:50239 ReportValueChanged(key, DEFAULT_PREF_WRITE_FLAGS);
240 } else {
Ramin Halavatif623bafe92018-07-17 12:12:58241 if (!ephemeral_user_pref_store_->GetValue(key, nullptr))
Johan Tibell0595f762017-05-25 23:54:50242 ReportValueChanged(key, DEFAULT_PREF_WRITE_FLAGS);
243 }
244}
245
Ramin Halavatif623bafe92018-07-17 12:12:58246void OverlayUserPrefStore::OnInitializationCompleted(bool ephemeral,
Johan Tibell0595f762017-05-25 23:54:50247 bool succeeded) {
248 if (!IsInitializationComplete())
249 return;
250 for (PrefStore::Observer& observer : observers_)
251 observer.OnInitializationCompleted(succeeded);
[email protected]39565dd2011-11-14 12:09:27252}
253
Ramin Halavatif623bafe92018-07-17 12:12:58254bool OverlayUserPrefStore::ShallBeStoredInPersistent(
Roland Bock24a7c862022-09-26 07:29:48255 base::StringPiece key) const {
Ramin Halavatif623bafe92018-07-17 12:12:58256 return persistent_names_set_.find(key) != persistent_names_set_.end();
[email protected]39565dd2011-11-14 12:09:27257}