summaryrefslogtreecommitdiff
path: root/chromium/components/prefs/overlay_user_pref_store.cc
blob: 2b45d8c6449eff21f4f19a2fa94de736caa4d618 (plain)
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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// 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 <utility>

#include "base/memory/ptr_util.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(const std::string& 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_;
  OverlayUserPrefStore* const 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(const std::string& 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_.might_have_observers();
}

bool OverlayUserPrefStore::IsInitializationComplete() const {
  return persistent_user_pref_store_->IsInitializationComplete() &&
         ephemeral_user_pref_store_->IsInitializationComplete();
}

bool OverlayUserPrefStore::GetValue(const std::string& 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);
}

std::unique_ptr<base::DictionaryValue> 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::unique_ptr<base::Value> out_value;
    persistent_values->Remove(key, &out_value);
    if (out_value) {
      values->Set(key, std::move(out_value));
    }
  }
  return values;
}

bool OverlayUserPrefStore::GetMutableValue(const std::string& key,
                                           base::Value** result) {
  if (ShallBeStoredInPersistent(key))
    return persistent_user_pref_store_->GetMutableValue(key, result);

  written_ephemeral_names_.insert(key);
  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;

  *result = persistent_value->DeepCopy();
  ephemeral_user_pref_store_->SetValue(
      key, base::WrapUnique(*result),
      WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
  return true;
}

void OverlayUserPrefStore::SetValue(const std::string& key,
                                    std::unique_ptr<base::Value> value,
                                    uint32_t flags) {
  if (ShallBeStoredInPersistent(key)) {
    persistent_user_pref_store_->SetValue(key, std::move(value), flags);
    return;
  }

  // TODO(https://crbug.com/861722): 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().
  written_ephemeral_names_.insert(key);
  ephemeral_user_pref_store_->SetValue(key, std::move(value), flags);
}

void OverlayUserPrefStore::SetValueSilently(const std::string& key,
                                            std::unique_ptr<base::Value> value,
                                            uint32_t flags) {
  if (ShallBeStoredInPersistent(key)) {
    persistent_user_pref_store_->SetValueSilently(key, std::move(value), flags);
    return;
  }

  written_ephemeral_names_.insert(key);
  ephemeral_user_pref_store_->SetValueSilently(key, std::move(value), flags);
}

void OverlayUserPrefStore::RemoveValue(const std::string& key, uint32_t flags) {
  if (ShallBeStoredInPersistent(key)) {
    persistent_user_pref_store_->RemoveValue(key, flags);
    return;
  }

  written_ephemeral_names_.insert(key);
  ephemeral_user_pref_store_->RemoveValue(key, flags);
}

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 done_callback) {
  persistent_user_pref_store_->CommitPendingWrite(std::move(done_callback));
  // We do not write our content intentionally.
}

void OverlayUserPrefStore::SchedulePendingLossyWrites() {
  persistent_user_pref_store_->SchedulePendingLossyWrites();
}

void OverlayUserPrefStore::ReportValueChanged(const std::string& key,
                                              uint32_t flags) {
  for (PrefStore::Observer& observer : observers_)
    observer.OnPrefValueChanged(key);
}

void OverlayUserPrefStore::RegisterPersistentPref(const std::string& 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(key);
}

void OverlayUserPrefStore::ClearMutableValues() {
  for (const auto& key : written_ephemeral_names_) {
    ephemeral_user_pref_store_->RemoveValue(
        key, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
  }
}

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,
                                              const std::string& 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(
    const std::string& key) const {
  return persistent_names_set_.find(key) != persistent_names_set_.end();
}