OLD | NEW |
| (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 "base/prefs/overlay_user_pref_store.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/values.h" | |
11 | |
12 OverlayUserPrefStore::OverlayUserPrefStore( | |
13 PersistentPrefStore* underlay) | |
14 : underlay_(underlay) { | |
15 underlay_->AddObserver(this); | |
16 } | |
17 | |
18 bool OverlayUserPrefStore::IsSetInOverlay(const std::string& key) const { | |
19 return overlay_.GetValue(key, NULL); | |
20 } | |
21 | |
22 void OverlayUserPrefStore::AddObserver(PrefStore::Observer* observer) { | |
23 observers_.AddObserver(observer); | |
24 } | |
25 | |
26 void OverlayUserPrefStore::RemoveObserver(PrefStore::Observer* observer) { | |
27 observers_.RemoveObserver(observer); | |
28 } | |
29 | |
30 bool OverlayUserPrefStore::HasObservers() const { | |
31 return observers_.might_have_observers(); | |
32 } | |
33 | |
34 bool OverlayUserPrefStore::IsInitializationComplete() const { | |
35 return underlay_->IsInitializationComplete(); | |
36 } | |
37 | |
38 bool OverlayUserPrefStore::GetValue(const std::string& key, | |
39 const base::Value** result) const { | |
40 // 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)) | |
45 return true; | |
46 return underlay_->GetValue(GetUnderlayKey(key), result); | |
47 } | |
48 | |
49 bool OverlayUserPrefStore::GetMutableValue(const std::string& key, | |
50 base::Value** result) { | |
51 if (!ShallBeStoredInOverlay(key)) | |
52 return underlay_->GetMutableValue(GetUnderlayKey(key), result); | |
53 | |
54 if (overlay_.GetValue(key, result)) | |
55 return true; | |
56 | |
57 // Try to create copy of underlay if the overlay does not contain a value. | |
58 base::Value* underlay_value = NULL; | |
59 if (!underlay_->GetMutableValue(GetUnderlayKey(key), &underlay_value)) | |
60 return false; | |
61 | |
62 *result = underlay_value->DeepCopy(); | |
63 overlay_.SetValue(key, make_scoped_ptr(*result)); | |
64 return true; | |
65 } | |
66 | |
67 void OverlayUserPrefStore::SetValue(const std::string& key, | |
68 scoped_ptr<base::Value> value, | |
69 uint32_t flags) { | |
70 if (!ShallBeStoredInOverlay(key)) { | |
71 underlay_->SetValue(GetUnderlayKey(key), std::move(value), flags); | |
72 return; | |
73 } | |
74 | |
75 if (overlay_.SetValue(key, std::move(value))) | |
76 ReportValueChanged(key, flags); | |
77 } | |
78 | |
79 void OverlayUserPrefStore::SetValueSilently(const std::string& key, | |
80 scoped_ptr<base::Value> value, | |
81 uint32_t flags) { | |
82 if (!ShallBeStoredInOverlay(key)) { | |
83 underlay_->SetValueSilently(GetUnderlayKey(key), std::move(value), flags); | |
84 return; | |
85 } | |
86 | |
87 overlay_.SetValue(key, std::move(value)); | |
88 } | |
89 | |
90 void OverlayUserPrefStore::RemoveValue(const std::string& key, uint32_t flags) { | |
91 if (!ShallBeStoredInOverlay(key)) { | |
92 underlay_->RemoveValue(GetUnderlayKey(key), flags); | |
93 return; | |
94 } | |
95 | |
96 if (overlay_.RemoveValue(key)) | |
97 ReportValueChanged(key, flags); | |
98 } | |
99 | |
100 bool OverlayUserPrefStore::ReadOnly() const { | |
101 return false; | |
102 } | |
103 | |
104 PersistentPrefStore::PrefReadError OverlayUserPrefStore::GetReadError() const { | |
105 return PersistentPrefStore::PREF_READ_ERROR_NONE; | |
106 } | |
107 | |
108 PersistentPrefStore::PrefReadError OverlayUserPrefStore::ReadPrefs() { | |
109 // We do not read intentionally. | |
110 OnInitializationCompleted(true); | |
111 return PersistentPrefStore::PREF_READ_ERROR_NONE; | |
112 } | |
113 | |
114 void 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 | |
121 void OverlayUserPrefStore::CommitPendingWrite() { | |
122 underlay_->CommitPendingWrite(); | |
123 // We do not write our content intentionally. | |
124 } | |
125 | |
126 void OverlayUserPrefStore::SchedulePendingLossyWrites() { | |
127 underlay_->SchedulePendingLossyWrites(); | |
128 } | |
129 | |
130 void OverlayUserPrefStore::ReportValueChanged(const std::string& key, | |
131 uint32_t flags) { | |
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), DEFAULT_PREF_WRITE_FLAGS); | |
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 } | |
OLD | NEW |