OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2014 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/tracked/protected_pref_store.h" | |
6 | |
7 #include "base/memory/ref_counted.h" | |
8 #include "base/values.h" | |
9 #include "chrome/browser/prefs/pref_hash_store.h" | |
10 #include "chrome/browser/prefs/pref_hash_store_transaction.h" | |
11 | |
12 namespace { | |
13 | |
14 // Observes ReadErrorDelegate events from the underlying stores and synthesizes | |
15 // an event for reporting to the external ReadErrorDelegate if necessary. | |
robertshield
2014/03/25 03:05:12
Please explicitly state that this class is not thr
erikwright (departed)
2014/03/25 20:28:26
Luckily the class is no longer needed.
| |
16 class SharedReadErrorDelegate | |
17 : public base::RefCounted<SharedReadErrorDelegate> { | |
18 public: | |
19 // Creates a SharedReadErrorDelegate that can observe multiple underlying | |
20 // PersistentPrefStores and report the first error from any of them to | |
21 // |external_delegate|. | |
22 explicit SharedReadErrorDelegate( | |
23 scoped_ptr<PersistentPrefStore::ReadErrorDelegate> external_delegate) | |
24 : sent_error_(false), external_delegate_(external_delegate.Pass()) {} | |
25 | |
26 // Makes a delegate that can observe one underlying store. | |
27 scoped_ptr<PersistentPrefStore::ReadErrorDelegate> MakeDelegate() { | |
28 return scoped_ptr<PersistentPrefStore::ReadErrorDelegate>( | |
29 new TeeErrorDelegate(this)); | |
30 } | |
31 | |
32 private: | |
33 // Observes the events from a single underlying store. | |
34 class TeeErrorDelegate : public PersistentPrefStore::ReadErrorDelegate { | |
35 public: | |
36 explicit TeeErrorDelegate( | |
37 const scoped_refptr<SharedReadErrorDelegate>& outer) | |
38 : outer_(outer) {} | |
39 | |
40 // PersistentPrefStore::ReadErrorDelegate implementation | |
41 virtual void OnError(PersistentPrefStore::PrefReadError read_error) | |
42 OVERRIDE { | |
43 // If this is the first error observed by this SharedReadErrorDelegate, | |
44 // report it to the external delegate. | |
45 if (outer_ && !outer_->sent_error_) { | |
46 outer_->sent_error_ = true; | |
47 outer_->external_delegate_->OnError(read_error); | |
48 } | |
49 } | |
50 | |
51 private: | |
52 scoped_refptr<SharedReadErrorDelegate> outer_; | |
53 }; | |
54 | |
55 friend class base::RefCounted<SharedReadErrorDelegate>; | |
56 ~SharedReadErrorDelegate() {} | |
57 | |
58 bool sent_error_; | |
59 scoped_ptr<PersistentPrefStore::ReadErrorDelegate> external_delegate_; | |
60 }; | |
61 | |
62 } // namespace | |
63 | |
64 ProtectedPrefStore::TeeObserver::TeeObserver(ProtectedPrefStore* outer) | |
65 : outer_(outer), | |
66 failed_sub_initializations_(0), | |
67 successful_sub_initializations_(0) {} | |
68 | |
69 void ProtectedPrefStore::TeeObserver::OnPrefValueChanged( | |
70 const std::string& key) { | |
71 // There is no need to tell clients about changes if they have not yet been | |
72 // told about initialization. | |
73 if (failed_sub_initializations_ + successful_sub_initializations_ == 2) { | |
74 FOR_EACH_OBSERVER( | |
75 PrefStore::Observer, outer_->observers_, OnPrefValueChanged(key)); | |
76 } | |
77 } | |
78 | |
79 void ProtectedPrefStore::TeeObserver::OnInitializationCompleted( | |
80 bool succeeded) { | |
81 if (succeeded) | |
82 ++successful_sub_initializations_; | |
83 else | |
84 ++failed_sub_initializations_; | |
85 | |
86 DCHECK_LE(failed_sub_initializations_ + successful_sub_initializations_, 2); | |
87 | |
88 if (failed_sub_initializations_ + successful_sub_initializations_ == 2 && | |
89 !outer_->on_initialization_.is_null()) { | |
90 outer_->on_initialization_.Run(); | |
91 } | |
92 | |
93 if (failed_sub_initializations_ + successful_sub_initializations_ == 2) { | |
94 FOR_EACH_OBSERVER( | |
95 PrefStore::Observer, | |
96 outer_->observers_, | |
97 OnInitializationCompleted(successful_sub_initializations_ == 2)); | |
98 } | |
robertshield
2014/03/25 03:05:12
Can you replace lines 88-98 with:
if (failed_sub
erikwright (departed)
2014/03/25 20:28:26
Done.
| |
99 } | |
100 | |
101 ProtectedPrefStore::ProtectedPrefStore( | |
102 const scoped_refptr<PersistentPrefStore>& unprotected_pref_store, | |
103 const scoped_refptr<PersistentPrefStore>& protected_pref_store, | |
104 const std::set<std::string>& protected_pref_names, | |
105 const base::Closure& on_initialization) | |
106 : unprotected_pref_store_(unprotected_pref_store), | |
107 protected_pref_store_(protected_pref_store), | |
108 protected_preference_names_(protected_pref_names), | |
109 on_initialization_(on_initialization), | |
110 tee_observer_(this) { | |
111 | |
112 unprotected_pref_store_->AddObserver(&tee_observer_); | |
113 protected_pref_store_->AddObserver(&tee_observer_); | |
114 } | |
115 | |
116 void ProtectedPrefStore::AddObserver(Observer* observer) { | |
117 observers_.AddObserver(observer); | |
118 } | |
119 | |
120 void ProtectedPrefStore::RemoveObserver(Observer* observer) { | |
121 observers_.RemoveObserver(observer); | |
122 } | |
123 | |
124 bool ProtectedPrefStore::HasObservers() const { | |
125 return observers_.might_have_observers(); | |
126 } | |
127 | |
128 bool ProtectedPrefStore::IsInitializationComplete() const { | |
129 return unprotected_pref_store_->IsInitializationComplete() && | |
130 protected_pref_store_->IsInitializationComplete(); | |
131 } | |
132 | |
133 bool ProtectedPrefStore::GetValue(const std::string& key, | |
134 const base::Value** result) const { | |
135 return StoreForKey(key)->GetValue(key, result); | |
136 } | |
137 | |
138 void ProtectedPrefStore::SetValue(const std::string& key, base::Value* value) { | |
139 StoreForKey(key)->SetValue(key, value); | |
140 } | |
141 | |
142 void ProtectedPrefStore::RemoveValue(const std::string& key) { | |
143 StoreForKey(key)->RemoveValue(key); | |
144 } | |
145 | |
146 bool ProtectedPrefStore::GetMutableValue(const std::string& key, | |
147 base::Value** result) { | |
148 return StoreForKey(key)->GetMutableValue(key, result); | |
149 } | |
150 | |
151 void ProtectedPrefStore::ReportValueChanged(const std::string& key) { | |
152 StoreForKey(key)->ReportValueChanged(key); | |
153 } | |
154 | |
155 void ProtectedPrefStore::SetValueSilently(const std::string& key, | |
156 base::Value* value) { | |
157 StoreForKey(key)->SetValueSilently(key, value); | |
158 } | |
159 | |
160 bool ProtectedPrefStore::ReadOnly() const { | |
161 return protected_pref_store_->ReadOnly() || | |
162 unprotected_pref_store_->ReadOnly(); | |
163 } | |
164 | |
165 PersistentPrefStore::PrefReadError ProtectedPrefStore::GetReadError() const { | |
166 PersistentPrefStore::PrefReadError read_error = | |
167 unprotected_pref_store_->GetReadError(); | |
168 return read_error != PersistentPrefStore::PREF_READ_ERROR_NONE ? | |
169 read_error : protected_pref_store_->GetReadError(); | |
robertshield
2014/03/25 03:05:12
nit: indent
erikwright (departed)
2014/03/25 20:28:26
Done.
| |
170 } | |
171 | |
172 PersistentPrefStore::PrefReadError ProtectedPrefStore::ReadPrefs() { | |
173 PersistentPrefStore::PrefReadError unprotected_read_error = | |
174 unprotected_pref_store_->ReadPrefs(); | |
175 PersistentPrefStore::PrefReadError protected_read_error = | |
176 protected_pref_store_->ReadPrefs(); | |
177 | |
178 return unprotected_read_error != PersistentPrefStore::PREF_READ_ERROR_NONE | |
179 ? unprotected_read_error | |
180 : protected_read_error; | |
181 } | |
182 | |
183 void ProtectedPrefStore::ReadPrefsAsync(ReadErrorDelegate* error_delegate) { | |
184 scoped_ptr<ReadErrorDelegate> unprotected_delegate; | |
185 scoped_ptr<ReadErrorDelegate> protected_delegate; | |
186 if (error_delegate) { | |
187 scoped_refptr<SharedReadErrorDelegate> shared_delegate( | |
188 new SharedReadErrorDelegate(make_scoped_ptr(error_delegate))); | |
189 unprotected_delegate.reset(shared_delegate->MakeDelegate().release()); | |
190 protected_delegate.reset(shared_delegate->MakeDelegate().release()); | |
191 } | |
192 unprotected_pref_store_->ReadPrefsAsync(unprotected_delegate.release()); | |
193 protected_pref_store_->ReadPrefsAsync(protected_delegate.release()); | |
194 } | |
195 | |
196 void ProtectedPrefStore::CommitPendingWrite() { | |
197 unprotected_pref_store_->CommitPendingWrite(); | |
198 protected_pref_store_->CommitPendingWrite(); | |
199 } | |
200 | |
201 ProtectedPrefStore::~ProtectedPrefStore() { | |
202 unprotected_pref_store_->RemoveObserver(&tee_observer_); | |
203 protected_pref_store_->RemoveObserver(&tee_observer_); | |
204 } | |
205 | |
206 const PersistentPrefStore* | |
207 ProtectedPrefStore::StoreForKey(const std::string& key) const { | |
208 if (ContainsKey(protected_preference_names_, key) || | |
209 protected_pref_store_->GetValue(key, NULL)) { | |
210 return protected_pref_store_.get(); | |
211 } | |
212 return unprotected_pref_store_.get(); | |
213 } | |
214 | |
215 PersistentPrefStore* ProtectedPrefStore::StoreForKey(const std::string& key) { | |
216 if (ContainsKey(protected_preference_names_, key)) | |
217 return protected_pref_store_.get(); | |
218 | |
219 // Check if this unprotected value was previously protected. If so, migrate it | |
220 // back to the unprotected store. | |
221 // It's hard to do this in a single pass at startup because PrefStore does not | |
222 // permit us to enumerate its contents. | |
223 const base::Value* value = NULL; | |
224 if (protected_pref_store_->GetValue(key, &value)) { | |
225 scoped_ptr<base::Value> migrated_value(value->DeepCopy()); | |
226 value = NULL; | |
227 unprotected_pref_store_->SetValue(key, migrated_value.release()); | |
228 unprotected_pref_store_->CommitPendingWrite(); | |
229 protected_pref_store_->RemoveValue(key); | |
230 protected_pref_store_->CommitPendingWrite(); | |
231 } | |
232 | |
233 return unprotected_pref_store_.get(); | |
234 } | |
OLD | NEW |