blob: 4c236f10dd0da84240d1e790c26099f9160756e2 [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/prefs/overlay_user_pref_store.h"
6
7#include "base/memory/scoped_ptr.h"
8#include "base/values.h"
9
10OverlayUserPrefStore::OverlayUserPrefStore(
11 PersistentPrefStore* underlay)
12 : underlay_(underlay) {
13 underlay_->AddObserver(this);
14}
15
16bool OverlayUserPrefStore::IsSetInOverlay(const std::string& key) const {
17 return overlay_.GetValue(key, NULL);
18}
19
20void OverlayUserPrefStore::AddObserver(PrefStore::Observer* observer) {
21 observers_.AddObserver(observer);
22}
23
24void OverlayUserPrefStore::RemoveObserver(PrefStore::Observer* observer) {
25 observers_.RemoveObserver(observer);
26}
27
28bool OverlayUserPrefStore::HasObservers() const {
29 return observers_.might_have_observers();
30}
31
32bool OverlayUserPrefStore::IsInitializationComplete() const {
33 return underlay_->IsInitializationComplete();
34}
35
36bool OverlayUserPrefStore::GetValue(const std::string& key,
37 const base::Value** result) const {
38 // If the |key| shall NOT be stored in the overlay store, there must not
39 // be an entry.
40 DCHECK(ShallBeStoredInOverlay(key) || !overlay_.GetValue(key, NULL));
41
42 if (overlay_.GetValue(key, result))
43 return true;
44 return underlay_->GetValue(GetUnderlayKey(key), result);
45}
46
47bool OverlayUserPrefStore::GetMutableValue(const std::string& key,
48 base::Value** result) {
49 if (!ShallBeStoredInOverlay(key))
50 return underlay_->GetMutableValue(GetUnderlayKey(key), result);
51
52 if (overlay_.GetValue(key, result))
53 return true;
54
55 // Try to create copy of underlay if the overlay does not contain a value.
56 base::Value* underlay_value = NULL;
57 if (!underlay_->GetMutableValue(GetUnderlayKey(key), &underlay_value))
58 return false;
59
60 *result = underlay_value->DeepCopy();
61 overlay_.SetValue(key, *result);
62 return true;
63}
64
65void OverlayUserPrefStore::SetValue(const std::string& key,
James Robinsonc8f302a2015-05-14 16:38:33 -070066 base::Value* value,
67 uint32 flags) {
James Robinson646469d2014-10-03 15:33:28 -070068 if (!ShallBeStoredInOverlay(key)) {
James Robinsonc8f302a2015-05-14 16:38:33 -070069 underlay_->SetValue(GetUnderlayKey(key), value, flags);
James Robinson646469d2014-10-03 15:33:28 -070070 return;
71 }
72
73 if (overlay_.SetValue(key, value))
James Robinsonc8f302a2015-05-14 16:38:33 -070074 ReportValueChanged(key, flags);
James Robinson646469d2014-10-03 15:33:28 -070075}
76
77void OverlayUserPrefStore::SetValueSilently(const std::string& key,
James Robinsonc8f302a2015-05-14 16:38:33 -070078 base::Value* value,
79 uint32 flags) {
James Robinson646469d2014-10-03 15:33:28 -070080 if (!ShallBeStoredInOverlay(key)) {
James Robinsonc8f302a2015-05-14 16:38:33 -070081 underlay_->SetValueSilently(GetUnderlayKey(key), value, flags);
James Robinson646469d2014-10-03 15:33:28 -070082 return;
83 }
84
85 overlay_.SetValue(key, value);
86}
87
James Robinsonc8f302a2015-05-14 16:38:33 -070088void OverlayUserPrefStore::RemoveValue(const std::string& key, uint32 flags) {
James Robinson646469d2014-10-03 15:33:28 -070089 if (!ShallBeStoredInOverlay(key)) {
James Robinsonc8f302a2015-05-14 16:38:33 -070090 underlay_->RemoveValue(GetUnderlayKey(key), flags);
James Robinson646469d2014-10-03 15:33:28 -070091 return;
92 }
93
94 if (overlay_.RemoveValue(key))
James Robinsonc8f302a2015-05-14 16:38:33 -070095 ReportValueChanged(key, flags);
James Robinson646469d2014-10-03 15:33:28 -070096}
97
98bool OverlayUserPrefStore::ReadOnly() const {
99 return false;
100}
101
102PersistentPrefStore::PrefReadError OverlayUserPrefStore::GetReadError() const {
103 return PersistentPrefStore::PREF_READ_ERROR_NONE;
104}
105
106PersistentPrefStore::PrefReadError OverlayUserPrefStore::ReadPrefs() {
107 // We do not read intentionally.
108 OnInitializationCompleted(true);
109 return PersistentPrefStore::PREF_READ_ERROR_NONE;
110}
111
112void OverlayUserPrefStore::ReadPrefsAsync(
113 ReadErrorDelegate* error_delegate_raw) {
114 scoped_ptr<ReadErrorDelegate> error_delegate(error_delegate_raw);
115 // We do not read intentionally.
116 OnInitializationCompleted(true);
117}
118
119void OverlayUserPrefStore::CommitPendingWrite() {
120 underlay_->CommitPendingWrite();
121 // We do not write our content intentionally.
122}
123
Viet-Trung Luu235cf3d2015-06-11 10:01:25 -0700124void OverlayUserPrefStore::SchedulePendingLossyWrites() {
125 underlay_->SchedulePendingLossyWrites();
126}
127
James Robinsonc8f302a2015-05-14 16:38:33 -0700128void OverlayUserPrefStore::ReportValueChanged(const std::string& key,
129 uint32 flags) {
James Robinson646469d2014-10-03 15:33:28 -0700130 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key));
131}
132
133void OverlayUserPrefStore::OnPrefValueChanged(const std::string& key) {
134 if (!overlay_.GetValue(GetOverlayKey(key), NULL))
James Robinsonc8f302a2015-05-14 16:38:33 -0700135 ReportValueChanged(GetOverlayKey(key), DEFAULT_PREF_WRITE_FLAGS);
James Robinson646469d2014-10-03 15:33:28 -0700136}
137
138void OverlayUserPrefStore::OnInitializationCompleted(bool succeeded) {
139 FOR_EACH_OBSERVER(PrefStore::Observer, observers_,
140 OnInitializationCompleted(succeeded));
141}
142
143void OverlayUserPrefStore::RegisterOverlayPref(const std::string& key) {
144 RegisterOverlayPref(key, key);
145}
146
147void OverlayUserPrefStore::RegisterOverlayPref(
148 const std::string& overlay_key,
149 const std::string& underlay_key) {
150 DCHECK(!overlay_key.empty()) << "Overlay key is empty";
151 DCHECK(overlay_to_underlay_names_map_.find(overlay_key) ==
152 overlay_to_underlay_names_map_.end()) <<
153 "Overlay key already registered";
154 DCHECK(!underlay_key.empty()) << "Underlay key is empty";
155 DCHECK(underlay_to_overlay_names_map_.find(underlay_key) ==
156 underlay_to_overlay_names_map_.end()) <<
157 "Underlay key already registered";
158 overlay_to_underlay_names_map_[overlay_key] = underlay_key;
159 underlay_to_overlay_names_map_[underlay_key] = overlay_key;
160}
161
162OverlayUserPrefStore::~OverlayUserPrefStore() {
163 underlay_->RemoveObserver(this);
164}
165
166const std::string& OverlayUserPrefStore::GetOverlayKey(
167 const std::string& underlay_key) const {
168 NamesMap::const_iterator i =
169 underlay_to_overlay_names_map_.find(underlay_key);
170 return i != underlay_to_overlay_names_map_.end() ? i->second : underlay_key;
171}
172
173const std::string& OverlayUserPrefStore::GetUnderlayKey(
174 const std::string& overlay_key) const {
175 NamesMap::const_iterator i =
176 overlay_to_underlay_names_map_.find(overlay_key);
177 return i != overlay_to_underlay_names_map_.end() ? i->second : overlay_key;
178}
179
180bool OverlayUserPrefStore::ShallBeStoredInOverlay(
181 const std::string& key) const {
182 return overlay_to_underlay_names_map_.find(key) !=
183 overlay_to_underlay_names_map_.end();
184}