1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/prefs/overlay_user_pref_store.h"
#include <memory>
#include <ostream>
#include <string>
#include <string_view>
#include <utility>
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/notimplemented.h"
#include "base/observer_list.h"
#include "base/values.h"
#include "components/prefs/in_memory_pref_store.h"
// Allows us to monitor two pref stores and tell updates from them apart. It
// essentially mimics a Callback for the Observer interface (e.g. it allows
// binding additional arguments).
class OverlayUserPrefStore::ObserverAdapter : public PrefStore::Observer {
public:
ObserverAdapter(bool ephemeral, OverlayUserPrefStore* parent)
: ephemeral_user_pref_store_(ephemeral), parent_(parent) {}
// Methods of PrefStore::Observer.
void OnPrefValueChanged(std::string_view key) override {
parent_->OnPrefValueChanged(ephemeral_user_pref_store_, key);
}
void OnInitializationCompleted(bool succeeded) override {
parent_->OnInitializationCompleted(ephemeral_user_pref_store_, succeeded);
}
private:
// Is the update for the ephemeral?
const bool ephemeral_user_pref_store_;
const raw_ptr<OverlayUserPrefStore> parent_;
};
OverlayUserPrefStore::OverlayUserPrefStore(PersistentPrefStore* persistent)
: OverlayUserPrefStore(new InMemoryPrefStore(), persistent) {}
OverlayUserPrefStore::OverlayUserPrefStore(PersistentPrefStore* ephemeral,
PersistentPrefStore* persistent)
: ephemeral_pref_store_observer_(
std::make_unique<OverlayUserPrefStore::ObserverAdapter>(true, this)),
persistent_pref_store_observer_(
std::make_unique<OverlayUserPrefStore::ObserverAdapter>(false, this)),
ephemeral_user_pref_store_(ephemeral),
persistent_user_pref_store_(persistent) {
DCHECK(ephemeral->IsInitializationComplete());
ephemeral_user_pref_store_->AddObserver(ephemeral_pref_store_observer_.get());
persistent_user_pref_store_->AddObserver(
persistent_pref_store_observer_.get());
}
bool OverlayUserPrefStore::IsSetInOverlay(std::string_view key) const {
return ephemeral_user_pref_store_->GetValue(key, nullptr);
}
void OverlayUserPrefStore::AddObserver(PrefStore::Observer* observer) {
observers_.AddObserver(observer);
}
void OverlayUserPrefStore::RemoveObserver(PrefStore::Observer* observer) {
observers_.RemoveObserver(observer);
}
bool OverlayUserPrefStore::HasObservers() const {
return !observers_.empty();
}
bool OverlayUserPrefStore::IsInitializationComplete() const {
return persistent_user_pref_store_->IsInitializationComplete() &&
ephemeral_user_pref_store_->IsInitializationComplete();
}
bool OverlayUserPrefStore::GetValue(std::string_view key,
const base::Value** result) const {
// If the |key| shall NOT be stored in the ephemeral store, there must not
// be an entry.
DCHECK(!ShallBeStoredInPersistent(key) ||
!ephemeral_user_pref_store_->GetValue(key, nullptr));
if (ephemeral_user_pref_store_->GetValue(key, result))
return true;
return persistent_user_pref_store_->GetValue(key, result);
}
base::Value::Dict OverlayUserPrefStore::GetValues() const {
auto values = ephemeral_user_pref_store_->GetValues();
auto persistent_values = persistent_user_pref_store_->GetValues();
// Output |values| are read from |ephemeral_user_pref_store_| (in-memory
// store). Then the values of preferences in |persistent_names_set_| are
// overwritten by the content of |persistent_user_pref_store_| (the persistent
// store).
for (const auto& key : persistent_names_set_) {
std::optional<base::Value> out_value =
persistent_values.ExtractByDottedPath(key);
if (out_value.has_value()) {
values.SetByDottedPath(key, std::move(*out_value));
}
}
return values;
}
bool OverlayUserPrefStore::GetMutableValue(std::string_view key,
base::Value** result) {
if (ShallBeStoredInPersistent(key))
return persistent_user_pref_store_->GetMutableValue(key, result);
if (ephemeral_user_pref_store_->GetMutableValue(key, result))
return true;
// Try to create copy of persistent if the ephemeral does not contain a value.
base::Value* persistent_value = nullptr;
if (!persistent_user_pref_store_->GetMutableValue(key, &persistent_value))
return false;
ephemeral_user_pref_store_->SetValue(
key, persistent_value->Clone(),
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
ephemeral_user_pref_store_->GetMutableValue(key, result);
return true;
}
void OverlayUserPrefStore::SetValue(std::string_view key,
base::Value value,
uint32_t flags) {
if (ShallBeStoredInPersistent(key)) {
persistent_user_pref_store_->SetValue(key, std::move(value), flags);
return;
}
// TODO(crbug.com/40584094): If we always store in in-memory storage
// and conditionally also stored in persistent one, we wouldn't have to do a
// complex merge in GetValues().
ephemeral_user_pref_store_->SetValue(key, std::move(value), flags);
}
void OverlayUserPrefStore::SetValueSilently(std::string_view key,
base::Value value,
uint32_t flags) {
if (ShallBeStoredInPersistent(key)) {
persistent_user_pref_store_->SetValueSilently(key, std::move(value), flags);
return;
}
ephemeral_user_pref_store_->SetValueSilently(key, std::move(value), flags);
}
void OverlayUserPrefStore::RemoveValue(std::string_view key, uint32_t flags) {
if (ShallBeStoredInPersistent(key)) {
persistent_user_pref_store_->RemoveValue(key, flags);
return;
}
ephemeral_user_pref_store_->RemoveValue(key, flags);
}
void OverlayUserPrefStore::RemoveValuesByPrefixSilently(
std::string_view prefix) {
NOTIMPLEMENTED();
}
bool OverlayUserPrefStore::ReadOnly() const {
return false;
}
PersistentPrefStore::PrefReadError OverlayUserPrefStore::GetReadError() const {
return PersistentPrefStore::PREF_READ_ERROR_NONE;
}
PersistentPrefStore::PrefReadError OverlayUserPrefStore::ReadPrefs() {
// We do not read intentionally.
OnInitializationCompleted(/* ephemeral */ false, true);
return PersistentPrefStore::PREF_READ_ERROR_NONE;
}
void OverlayUserPrefStore::ReadPrefsAsync(
ReadErrorDelegate* error_delegate_raw) {
std::unique_ptr<ReadErrorDelegate> error_delegate(error_delegate_raw);
// We do not read intentionally.
OnInitializationCompleted(/* ephemeral */ false, true);
}
void OverlayUserPrefStore::CommitPendingWrite(
base::OnceClosure reply_callback,
base::OnceClosure synchronous_done_callback) {
persistent_user_pref_store_->CommitPendingWrite(
std::move(reply_callback), std::move(synchronous_done_callback));
// We do not write our content intentionally.
}
void OverlayUserPrefStore::SchedulePendingLossyWrites() {
persistent_user_pref_store_->SchedulePendingLossyWrites();
}
void OverlayUserPrefStore::ReportValueChanged(std::string_view key,
uint32_t flags) {
for (PrefStore::Observer& observer : observers_)
observer.OnPrefValueChanged(key);
}
void OverlayUserPrefStore::RegisterPersistentPref(std::string_view key) {
DCHECK(!key.empty()) << "Key is empty";
DCHECK(persistent_names_set_.find(key) == persistent_names_set_.end())
<< "Key already registered: " << key;
persistent_names_set_.insert(std::string(key));
}
void OverlayUserPrefStore::OnStoreDeletionFromDisk() {
persistent_user_pref_store_->OnStoreDeletionFromDisk();
}
OverlayUserPrefStore::~OverlayUserPrefStore() {
ephemeral_user_pref_store_->RemoveObserver(
ephemeral_pref_store_observer_.get());
persistent_user_pref_store_->RemoveObserver(
persistent_pref_store_observer_.get());
}
void OverlayUserPrefStore::OnPrefValueChanged(bool ephemeral,
std::string_view key) {
if (ephemeral) {
ReportValueChanged(key, DEFAULT_PREF_WRITE_FLAGS);
} else {
if (!ephemeral_user_pref_store_->GetValue(key, nullptr))
ReportValueChanged(key, DEFAULT_PREF_WRITE_FLAGS);
}
}
void OverlayUserPrefStore::OnInitializationCompleted(bool ephemeral,
bool succeeded) {
if (!IsInitializationComplete())
return;
for (PrefStore::Observer& observer : observers_)
observer.OnInitializationCompleted(succeeded);
}
bool OverlayUserPrefStore::ShallBeStoredInPersistent(
std::string_view key) const {
return persistent_names_set_.find(key) != persistent_names_set_.end();
}
bool OverlayUserPrefStore::HasReadErrorDelegate() const {
return persistent_user_pref_store_->HasReadErrorDelegate();
}
|