Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(221)

Side by Side Diff: chrome/browser/prefs/overlay_user_pref_store.cc

Issue 8515002: Take out and generalize user prefs overriding from IncognitoUserPrefStore. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 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 "chrome/browser/prefs/incognito_user_pref_store.h"
6
7 #include "base/memory/scoped_ptr.h"
8 #include "base/values.h"
9
10 OverlayUserPrefStore::OverlayUserPrefStore(
11 PersistentPrefStore* underlay)
12 : underlay_(underlay) {
13 underlay_->AddObserver(this);
14 }
15
16 OverlayUserPrefStore::~OverlayUserPrefStore() {
17 underlay_->RemoveObserver(this);
18 }
19
20 bool OverlayUserPrefStore::IsSetInOverlay(const std::string& key) const {
21 return overlay_.GetValue(key, NULL);
22 }
23
24 void OverlayUserPrefStore::AddObserver(PrefStore::Observer* observer) {
25 observers_.AddObserver(observer);
26 }
27
28 void OverlayUserPrefStore::RemoveObserver(PrefStore::Observer* observer) {
29 observers_.RemoveObserver(observer);
30 }
31
32 bool OverlayUserPrefStore::IsInitializationComplete() const {
33 return underlay_->IsInitializationComplete();
34 }
35
36 PrefStore::ReadResult OverlayUserPrefStore::GetValue(
37 const std::string& key,
38 const Value** result) const {
39 // If the |key| shall NOT be stored in the overlay store, there must not
40 // be an entry.
41 DCHECK(ShallBeStoredInOverlay(key) || !overlay_.GetValue(key, NULL));
42
43 if (overlay_.GetValue(key, result))
44 return READ_OK;
45 return underlay_->GetValue(GetUnderlayKey(key), result);
46 }
47
48 PrefStore::ReadResult OverlayUserPrefStore::GetMutableValue(
49 const std::string& key,
50 Value** result) {
51 if (!ShallBeStoredInOverlay(key))
52 return underlay_->GetMutableValue(GetUnderlayKey(key), result);
53
54 if (overlay_.GetValue(key, result))
55 return READ_OK;
56
57 // Try to create copy of underlay if the overlay does not contain a value.
58 Value* underlay_value = NULL;
59 PrefStore::ReadResult read_result =
60 underlay_->GetMutableValue(GetUnderlayKey(key), &underlay_value);
61 if (read_result != READ_OK)
62 return read_result;
63
64 *result = underlay_value->DeepCopy();
65 overlay_.SetValue(key, *result);
66 return READ_OK;
67 }
68
69 void OverlayUserPrefStore::SetValue(const std::string& key,
70 Value* value) {
71 if (!ShallBeStoredInOverlay(key)) {
72 underlay_->SetValue(GetUnderlayKey(key), value);
73 return;
74 }
75
76 if (overlay_.SetValue(key, value))
77 ReportValueChanged(key);
78 }
79
80 void OverlayUserPrefStore::SetValueSilently(const std::string& key,
81 Value* value) {
82 if (!ShallBeStoredInOverlay(key)) {
83 underlay_->SetValueSilently(GetUnderlayKey(key), value);
84 return;
85 }
86
87 overlay_.SetValue(key, value);
88 }
89
90 void OverlayUserPrefStore::RemoveValue(const std::string& key) {
91 if (!ShallBeStoredInOverlay(key)) {
92 underlay_->RemoveValue(GetUnderlayKey(key));
93 return;
94 }
95
96 if (overlay_.RemoveValue(key))
97 ReportValueChanged(key);
98 }
99
100 bool OverlayUserPrefStore::ReadOnly() const {
101 return false;
102 }
103
104 PersistentPrefStore::PrefReadError OverlayUserPrefStore::ReadPrefs() {
105 // We do not read intentionally.
106 OnInitializationCompleted(true);
107 return PersistentPrefStore::PREF_READ_ERROR_NONE;
108 }
109
110 void OverlayUserPrefStore::ReadPrefsAsync(
111 ReadErrorDelegate* error_delegate_raw) {
112 scoped_ptr<ReadErrorDelegate> error_delegate(error_delegate_raw);
113 // We do not read intentionally.
114 OnInitializationCompleted(true);
115 }
116
117 bool OverlayUserPrefStore::WritePrefs() {
118 // We do not write our content intentionally.
119 return true;
120 }
121
122 void OverlayUserPrefStore::ScheduleWritePrefs() {
123 underlay_->ScheduleWritePrefs();
124 // We do not write our content intentionally.
125 }
126
127 void OverlayUserPrefStore::CommitPendingWrite() {
128 underlay_->CommitPendingWrite();
129 // We do not write our content intentionally.
130 }
131
132 void OverlayUserPrefStore::ReportValueChanged(const std::string& key) {
133 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key));
134 }
135
136 void OverlayUserPrefStore::RegisterOverlayProperty(const std::string& key) {
137 RegisterOverlayProperty(key, key);
138 }
139
140 void OverlayUserPrefStore::RegisterOverlayProperty(
141 const std::string& overlay_key,
142 const std::string& underlay_key) {
143 DCHECK(!overlay_key.empty()) << "Overlay key is empty";
144 DCHECK(overlay_to_underlay_names_map_.find(overlay_key) ==
145 overlay_to_underlay_names_map_.end()) <<
146 "Overlay key already registered";
147 DCHECK(!underlay_key.empty()) << "Underlay key is empty";
148 DCHECK(underlay_to_overlay_names_map_.find(underlay_key) ==
149 underlay_to_overlay_names_map_.end()) <<
150 "Underlay key already registered";
151 overlay_to_underlay_names_map_[overlay_key] = underlay_key;
152 underlay_to_overlay_names_map_[underlay_key] = overlay_key;
153 }
154
155 void OverlayUserPrefStore::OnPrefValueChanged(const std::string& key) {
156 if (!overlay_.GetValue(GetOverlayKey(key), NULL))
157 ReportValueChanged(GetOverlayKey(key));
158 }
159
160 void OverlayUserPrefStore::OnInitializationCompleted(bool succeeded) {
161 FOR_EACH_OBSERVER(PrefStore::Observer, observers_,
162 OnInitializationCompleted(succeeded));
163 }
164
165 const std::string& OverlayUserPrefStore::GetOverlayKey(
166 const std::string& underlay_key) const {
167 NamesMap::const_iterator i =
168 underlay_to_overlay_names_map_.find(underlay_key);
169 return i != underlay_to_overlay_names_map_.end() ? i->second : underlay_key;
170 }
171
172 const std::string& OverlayUserPrefStore::GetUnderlayKey(
173 const std::string& overlay_key) const {
174 NamesMap::const_iterator i =
175 overlay_to_underlay_names_map_.find(overlay_key);
176 return i != overlay_to_underlay_names_map_.end() ? i->second : overlay_key;
177 }
178
179 bool OverlayUserPrefStore::ShallBeStoredInOverlay(
180 const std::string& key) const {
181 return overlay_to_underlay_names_map_.find(key) !=
182 overlay_to_underlay_names_map_.end();
183 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698