blob: dd5b68e0a636438275a3f544d0881b6a53fdb438 [file] [log] [blame]
[email protected]32c3c752012-01-05 17:33:471// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[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
[email protected]03b9b4e2012-10-22 20:01:525#include "base/prefs/overlay_user_pref_store.h"
[email protected]39565dd2011-11-14 12:09:276
danakj0c8d4aa2015-11-25 05:29:587#include <utility>
8
[email protected]39565dd2011-11-14 12:09:279#include "base/memory/scoped_ptr.h"
10#include "base/values.h"
11
12OverlayUserPrefStore::OverlayUserPrefStore(
13 PersistentPrefStore* underlay)
14 : underlay_(underlay) {
15 underlay_->AddObserver(this);
16}
17
[email protected]39565dd2011-11-14 12:09:2718bool OverlayUserPrefStore::IsSetInOverlay(const std::string& key) const {
19 return overlay_.GetValue(key, NULL);
20}
21
22void OverlayUserPrefStore::AddObserver(PrefStore::Observer* observer) {
23 observers_.AddObserver(observer);
24}
25
26void OverlayUserPrefStore::RemoveObserver(PrefStore::Observer* observer) {
27 observers_.RemoveObserver(observer);
28}
29
[email protected]14e0ec62013-08-26 22:01:3930bool OverlayUserPrefStore::HasObservers() const {
31 return observers_.might_have_observers();
[email protected]d3b05ea2012-01-24 22:57:0532}
33
[email protected]39565dd2011-11-14 12:09:2734bool OverlayUserPrefStore::IsInitializationComplete() const {
35 return underlay_->IsInitializationComplete();
36}
37
[email protected]892f1d62012-11-08 18:24:3438bool OverlayUserPrefStore::GetValue(const std::string& key,
[email protected]a43a667b2013-06-14 17:56:0839 const base::Value** result) const {
[email protected]39565dd2011-11-14 12:09:2740 // If the |key| shall NOT be stored in the overlay store, there must not
41 // be an entry.
42 DCHECK(ShallBeStoredInOverlay(key) || !overlay_.GetValue(key, NULL));
43
44 if (overlay_.GetValue(key, result))
[email protected]892f1d62012-11-08 18:24:3445 return true;
[email protected]39565dd2011-11-14 12:09:2746 return underlay_->GetValue(GetUnderlayKey(key), result);
47}
48
[email protected]892f1d62012-11-08 18:24:3449bool OverlayUserPrefStore::GetMutableValue(const std::string& key,
[email protected]a43a667b2013-06-14 17:56:0850 base::Value** result) {
[email protected]39565dd2011-11-14 12:09:2751 if (!ShallBeStoredInOverlay(key))
52 return underlay_->GetMutableValue(GetUnderlayKey(key), result);
53
54 if (overlay_.GetValue(key, result))
[email protected]892f1d62012-11-08 18:24:3455 return true;
[email protected]39565dd2011-11-14 12:09:2756
57 // Try to create copy of underlay if the overlay does not contain a value.
[email protected]a43a667b2013-06-14 17:56:0858 base::Value* underlay_value = NULL;
[email protected]892f1d62012-11-08 18:24:3459 if (!underlay_->GetMutableValue(GetUnderlayKey(key), &underlay_value))
60 return false;
[email protected]39565dd2011-11-14 12:09:2761
62 *result = underlay_value->DeepCopy();
estade0bd407f2015-06-26 18:16:1863 overlay_.SetValue(key, make_scoped_ptr(*result));
[email protected]892f1d62012-11-08 18:24:3464 return true;
[email protected]39565dd2011-11-14 12:09:2765}
66
67void OverlayUserPrefStore::SetValue(const std::string& key,
estade0bd407f2015-06-26 18:16:1868 scoped_ptr<base::Value> value,
avi9ef8bb02015-12-24 05:29:3669 uint32_t flags) {
[email protected]39565dd2011-11-14 12:09:2770 if (!ShallBeStoredInOverlay(key)) {
danakj0c8d4aa2015-11-25 05:29:5871 underlay_->SetValue(GetUnderlayKey(key), std::move(value), flags);
[email protected]39565dd2011-11-14 12:09:2772 return;
73 }
74
danakj0c8d4aa2015-11-25 05:29:5875 if (overlay_.SetValue(key, std::move(value)))
raymes76de1af2015-05-06 03:22:2176 ReportValueChanged(key, flags);
[email protected]39565dd2011-11-14 12:09:2777}
78
79void OverlayUserPrefStore::SetValueSilently(const std::string& key,
estade0bd407f2015-06-26 18:16:1880 scoped_ptr<base::Value> value,
avi9ef8bb02015-12-24 05:29:3681 uint32_t flags) {
[email protected]39565dd2011-11-14 12:09:2782 if (!ShallBeStoredInOverlay(key)) {
danakj0c8d4aa2015-11-25 05:29:5883 underlay_->SetValueSilently(GetUnderlayKey(key), std::move(value), flags);
[email protected]39565dd2011-11-14 12:09:2784 return;
85 }
86
danakj0c8d4aa2015-11-25 05:29:5887 overlay_.SetValue(key, std::move(value));
[email protected]39565dd2011-11-14 12:09:2788}
89
avi9ef8bb02015-12-24 05:29:3690void OverlayUserPrefStore::RemoveValue(const std::string& key, uint32_t flags) {
[email protected]39565dd2011-11-14 12:09:2791 if (!ShallBeStoredInOverlay(key)) {
raymes76de1af2015-05-06 03:22:2192 underlay_->RemoveValue(GetUnderlayKey(key), flags);
[email protected]39565dd2011-11-14 12:09:2793 return;
94 }
95
96 if (overlay_.RemoveValue(key))
raymes76de1af2015-05-06 03:22:2197 ReportValueChanged(key, flags);
[email protected]39565dd2011-11-14 12:09:2798}
99
100bool OverlayUserPrefStore::ReadOnly() const {
101 return false;
102}
103
[email protected]59c10712012-03-13 02:10:34104PersistentPrefStore::PrefReadError OverlayUserPrefStore::GetReadError() const {
105 return PersistentPrefStore::PREF_READ_ERROR_NONE;
106}
107
[email protected]39565dd2011-11-14 12:09:27108PersistentPrefStore::PrefReadError OverlayUserPrefStore::ReadPrefs() {
109 // We do not read intentionally.
110 OnInitializationCompleted(true);
111 return PersistentPrefStore::PREF_READ_ERROR_NONE;
112}
113
114void OverlayUserPrefStore::ReadPrefsAsync(
115 ReadErrorDelegate* error_delegate_raw) {
116 scoped_ptr<ReadErrorDelegate> error_delegate(error_delegate_raw);
117 // We do not read intentionally.
118 OnInitializationCompleted(true);
119}
120
[email protected]39565dd2011-11-14 12:09:27121void OverlayUserPrefStore::CommitPendingWrite() {
122 underlay_->CommitPendingWrite();
123 // We do not write our content intentionally.
124}
125
benwells26730592015-05-28 13:08:08126void OverlayUserPrefStore::SchedulePendingLossyWrites() {
127 underlay_->SchedulePendingLossyWrites();
128}
129
raymes76de1af2015-05-06 03:22:21130void OverlayUserPrefStore::ReportValueChanged(const std::string& key,
avi9ef8bb02015-12-24 05:29:36131 uint32_t flags) {
[email protected]39565dd2011-11-14 12:09:27132 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key));
133}
134
[email protected]2dea5c02012-04-25 07:01:07135void OverlayUserPrefStore::OnPrefValueChanged(const std::string& key) {
136 if (!overlay_.GetValue(GetOverlayKey(key), NULL))
raymes76de1af2015-05-06 03:22:21137 ReportValueChanged(GetOverlayKey(key), DEFAULT_PREF_WRITE_FLAGS);
[email protected]2dea5c02012-04-25 07:01:07138}
139
140void OverlayUserPrefStore::OnInitializationCompleted(bool succeeded) {
141 FOR_EACH_OBSERVER(PrefStore::Observer, observers_,
142 OnInitializationCompleted(succeeded));
143}
144
[email protected]32c3c752012-01-05 17:33:47145void OverlayUserPrefStore::RegisterOverlayPref(const std::string& key) {
146 RegisterOverlayPref(key, key);
[email protected]39565dd2011-11-14 12:09:27147}
148
[email protected]32c3c752012-01-05 17:33:47149void OverlayUserPrefStore::RegisterOverlayPref(
[email protected]39565dd2011-11-14 12:09:27150 const std::string& overlay_key,
151 const std::string& underlay_key) {
152 DCHECK(!overlay_key.empty()) << "Overlay key is empty";
153 DCHECK(overlay_to_underlay_names_map_.find(overlay_key) ==
154 overlay_to_underlay_names_map_.end()) <<
155 "Overlay key already registered";
156 DCHECK(!underlay_key.empty()) << "Underlay key is empty";
157 DCHECK(underlay_to_overlay_names_map_.find(underlay_key) ==
158 underlay_to_overlay_names_map_.end()) <<
159 "Underlay key already registered";
160 overlay_to_underlay_names_map_[overlay_key] = underlay_key;
161 underlay_to_overlay_names_map_[underlay_key] = overlay_key;
162}
163
[email protected]2dea5c02012-04-25 07:01:07164OverlayUserPrefStore::~OverlayUserPrefStore() {
165 underlay_->RemoveObserver(this);
[email protected]39565dd2011-11-14 12:09:27166}
167
168const std::string& OverlayUserPrefStore::GetOverlayKey(
169 const std::string& underlay_key) const {
170 NamesMap::const_iterator i =
171 underlay_to_overlay_names_map_.find(underlay_key);
172 return i != underlay_to_overlay_names_map_.end() ? i->second : underlay_key;
173}
174
175const std::string& OverlayUserPrefStore::GetUnderlayKey(
176 const std::string& overlay_key) const {
177 NamesMap::const_iterator i =
178 overlay_to_underlay_names_map_.find(overlay_key);
179 return i != overlay_to_underlay_names_map_.end() ? i->second : overlay_key;
180}
181
182bool OverlayUserPrefStore::ShallBeStoredInOverlay(
183 const std::string& key) const {
184 return overlay_to_underlay_names_map_.find(key) !=
185 overlay_to_underlay_names_map_.end();
186}