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

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

Issue 11243002: Move the bits of Prefs where production code has only trivially easy (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address review comments, fix gypi problem Created 8 years, 2 months 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) 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 "chrome/browser/prefs/overlay_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 bool OverlayUserPrefStore::IsSetInOverlay(const std::string& key) const {
17 return overlay_.GetValue(key, NULL);
18 }
19
20 void OverlayUserPrefStore::AddObserver(PrefStore::Observer* observer) {
21 observers_.AddObserver(observer);
22 }
23
24 void OverlayUserPrefStore::RemoveObserver(PrefStore::Observer* observer) {
25 observers_.RemoveObserver(observer);
26 }
27
28 size_t OverlayUserPrefStore::NumberOfObservers() const {
29 return observers_.size();
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 void OverlayUserPrefStore::MarkNeedsEmptyValue(const std::string& key) {
101 if (!ShallBeStoredInOverlay(key))
102 underlay_->MarkNeedsEmptyValue(key);
103 }
104
105 bool OverlayUserPrefStore::ReadOnly() const {
106 return false;
107 }
108
109 PersistentPrefStore::PrefReadError OverlayUserPrefStore::GetReadError() const {
110 return PersistentPrefStore::PREF_READ_ERROR_NONE;
111 }
112
113 PersistentPrefStore::PrefReadError OverlayUserPrefStore::ReadPrefs() {
114 // We do not read intentionally.
115 OnInitializationCompleted(true);
116 return PersistentPrefStore::PREF_READ_ERROR_NONE;
117 }
118
119 void OverlayUserPrefStore::ReadPrefsAsync(
120 ReadErrorDelegate* error_delegate_raw) {
121 scoped_ptr<ReadErrorDelegate> error_delegate(error_delegate_raw);
122 // We do not read intentionally.
123 OnInitializationCompleted(true);
124 }
125
126 void OverlayUserPrefStore::CommitPendingWrite() {
127 underlay_->CommitPendingWrite();
128 // We do not write our content intentionally.
129 }
130
131 void OverlayUserPrefStore::ReportValueChanged(const std::string& key) {
132 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key));
133 }
134
135 void OverlayUserPrefStore::OnPrefValueChanged(const std::string& key) {
136 if (!overlay_.GetValue(GetOverlayKey(key), NULL))
137 ReportValueChanged(GetOverlayKey(key));
138 }
139
140 void OverlayUserPrefStore::OnInitializationCompleted(bool succeeded) {
141 FOR_EACH_OBSERVER(PrefStore::Observer, observers_,
142 OnInitializationCompleted(succeeded));
143 }
144
145 void OverlayUserPrefStore::RegisterOverlayPref(const std::string& key) {
146 RegisterOverlayPref(key, key);
147 }
148
149 void OverlayUserPrefStore::RegisterOverlayPref(
150 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
164 OverlayUserPrefStore::~OverlayUserPrefStore() {
165 underlay_->RemoveObserver(this);
166 }
167
168 const 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
175 const 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
182 bool 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 }
OLDNEW
« no previous file with comments | « chrome/browser/prefs/overlay_user_pref_store.h ('k') | chrome/browser/prefs/overlay_user_pref_store_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698