OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/login/signed_settings_helper.h" | 5 #include "chrome/browser/chromeos/login/signed_settings_helper.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 // Note that the context could be released when op_->Execute() returns. | 42 // Note that the context could be released when op_->Execute() returns. |
43 // So keep a local copy of delegate and executing flag to use after | 43 // So keep a local copy of delegate and executing flag to use after |
44 // the call. | 44 // the call. |
45 Delegate* delegate = delegate_; | 45 Delegate* delegate = delegate_; |
46 executing_ = true; | 46 executing_ = true; |
47 op_->Execute(); | 47 op_->Execute(); |
48 if (delegate) | 48 if (delegate) |
49 delegate->OnOpStarted(this); | 49 delegate->OnOpStarted(this); |
50 } | 50 } |
51 | 51 |
52 // Cancels the callback. | |
53 void CancelCallback() { | |
54 callback_ = NULL; | |
55 } | |
56 | |
57 // Cancels the callback and cancels the op if it is not executing. | 52 // Cancels the callback and cancels the op if it is not executing. |
58 void Cancel() { | 53 void Cancel() { |
59 CancelCallback(); | |
60 | |
61 if (!executing_) | 54 if (!executing_) |
62 OnOpCompleted(); | 55 OnOpCompleted(); |
63 } | 56 } |
64 | 57 |
65 // Accessors. | 58 // Accessors. |
66 SignedSettings* op() const { | 59 SignedSettings* op() const { |
67 return op_.get(); | 60 return op_.get(); |
68 } | 61 } |
69 | 62 |
70 SignedSettingsHelper::Callback* callback() const { | |
71 return callback_; | |
72 } | |
73 | |
74 void set_delegate(Delegate* delegate) { | 63 void set_delegate(Delegate* delegate) { |
75 delegate_ = delegate; | 64 delegate_ = delegate; |
76 } | 65 } |
77 | 66 |
78 protected: | 67 protected: |
79 OpContext(SignedSettingsHelper::Callback* callback, | 68 explicit OpContext(Delegate* delegate) |
80 Delegate* delegate) | |
81 : executing_(false), | 69 : executing_(false), |
82 delegate_(delegate), | 70 delegate_(delegate) { |
83 callback_(callback) { | |
84 } | 71 } |
85 | 72 |
86 // Creates the op to execute. | 73 // Creates the op to execute. |
87 virtual void CreateOp() = 0; | 74 virtual void CreateOp() = 0; |
88 | 75 |
89 // Callback on op completion. | 76 // Callback on op completion. |
90 virtual void OnOpCompleted() { | 77 virtual void OnOpCompleted() { |
91 if (delegate_) | 78 if (delegate_) |
92 delegate_->OnOpCompleted(this); | 79 delegate_->OnOpCompleted(this); |
93 | 80 |
94 delete this; | 81 delete this; |
95 } | 82 } |
96 | 83 |
97 bool executing_; | 84 bool executing_; |
98 Delegate* delegate_; | 85 Delegate* delegate_; |
99 | 86 |
100 scoped_refptr<SignedSettings> op_; | 87 scoped_refptr<SignedSettings> op_; |
101 SignedSettingsHelper::Callback* callback_; | |
102 }; | 88 }; |
103 | 89 |
104 class StorePropertyOpContext : public SignedSettings::Delegate<bool>, | 90 class StorePolicyOpContext |
105 public OpContext { | 91 : public SignedSettings::Delegate<bool>, |
106 public: | |
107 StorePropertyOpContext(const std::string& name, | |
108 const base::Value& value, | |
109 SignedSettingsHelper::Callback* callback, | |
110 OpContext::Delegate* delegate) | |
111 : OpContext(callback, delegate), | |
112 name_(name), | |
113 value_(value.DeepCopy()) { | |
114 } | |
115 | |
116 // chromeos::SignedSettings::Delegate implementation | |
117 virtual void OnSettingsOpCompleted(SignedSettings::ReturnCode code, | |
118 bool unused) OVERRIDE { | |
119 VLOG(2) << "OnSettingsOpCompleted, code = " << code; | |
120 if (callback_) | |
121 callback_->OnStorePropertyCompleted(code, name_, *value_); | |
122 OnOpCompleted(); | |
123 } | |
124 | |
125 protected: | |
126 // OpContext implemenetation | |
127 virtual void CreateOp() OVERRIDE { | |
128 op_ = SignedSettings::CreateStorePropertyOp(name_, *value_, this); | |
129 } | |
130 | |
131 private: | |
132 std::string name_; | |
133 scoped_ptr<base::Value> value_; | |
134 | |
135 DISALLOW_COPY_AND_ASSIGN(StorePropertyOpContext); | |
136 }; | |
137 | |
138 class RetrievePropertyOpContext | |
139 : public SignedSettings::Delegate<const base::Value*>, | |
140 public OpContext { | 92 public OpContext { |
141 public: | 93 public: |
142 RetrievePropertyOpContext(const std::string& name, | |
143 SignedSettingsHelper::Callback* callback, | |
144 OpContext::Delegate* delegate) | |
145 : OpContext(callback, delegate), | |
146 name_(name) { | |
147 } | |
148 | |
149 // chromeos::SignedSettings::Delegate implementation | |
150 virtual void OnSettingsOpCompleted(SignedSettings::ReturnCode code, | |
151 const base::Value* value) OVERRIDE { | |
152 if (callback_) | |
153 callback_->OnRetrievePropertyCompleted(code, name_, value); | |
154 | |
155 OnOpCompleted(); | |
156 } | |
157 | |
158 protected: | |
159 // OpContext implemenetation | |
160 virtual void CreateOp() OVERRIDE { | |
161 op_ = SignedSettings::CreateRetrievePropertyOp(name_, this); | |
162 } | |
163 | |
164 private: | |
165 std::string name_; | |
166 | |
167 DISALLOW_COPY_AND_ASSIGN(RetrievePropertyOpContext); | |
168 }; | |
169 | |
170 class StorePolicyOpContext : public SignedSettings::Delegate<bool>, | |
171 public OpContext { | |
172 public: | |
173 StorePolicyOpContext(const em::PolicyFetchResponse& policy, | 94 StorePolicyOpContext(const em::PolicyFetchResponse& policy, |
174 SignedSettingsHelper::Callback* callback, | 95 SignedSettingsHelper::StorePolicyCallback callback, |
175 OpContext::Delegate* delegate) | 96 OpContext::Delegate* delegate) |
176 : OpContext(callback, delegate), | 97 : OpContext(delegate), |
| 98 callback_(callback), |
177 policy_(policy) { | 99 policy_(policy) { |
178 } | 100 } |
179 | 101 |
180 // chromeos::SignedSettings::Delegate implementation | 102 // chromeos::SignedSettings::Delegate implementation |
181 virtual void OnSettingsOpCompleted(SignedSettings::ReturnCode code, | 103 virtual void OnSettingsOpCompleted(SignedSettings::ReturnCode code, |
182 bool unused) OVERRIDE { | 104 bool unused) OVERRIDE { |
183 VLOG(2) << "OnSettingsOpCompleted, code = " << code; | 105 VLOG(2) << "OnSettingsOpCompleted, code = " << code; |
184 if (callback_) | 106 callback_.Run(code); |
185 callback_->OnStorePolicyCompleted(code); | |
186 OnOpCompleted(); | 107 OnOpCompleted(); |
187 } | 108 } |
188 | 109 |
189 protected: | 110 protected: |
190 // OpContext implementation | 111 // OpContext implementation |
191 virtual void CreateOp() OVERRIDE { | 112 virtual void CreateOp() OVERRIDE { |
192 op_ = SignedSettings::CreateStorePolicyOp(&policy_, this); | 113 op_ = SignedSettings::CreateStorePolicyOp(&policy_, this); |
193 } | 114 } |
194 | 115 |
195 private: | 116 private: |
| 117 SignedSettingsHelper::StorePolicyCallback callback_; |
196 em::PolicyFetchResponse policy_; | 118 em::PolicyFetchResponse policy_; |
| 119 |
197 DISALLOW_COPY_AND_ASSIGN(StorePolicyOpContext); | 120 DISALLOW_COPY_AND_ASSIGN(StorePolicyOpContext); |
198 }; | 121 }; |
199 | 122 |
200 class RetrievePolicyOpContext | 123 class RetrievePolicyOpContext |
201 : public SignedSettings::Delegate<const em::PolicyFetchResponse&>, | 124 : public SignedSettings::Delegate<const em::PolicyFetchResponse&>, |
202 public OpContext { | 125 public OpContext { |
203 public: | 126 public: |
204 RetrievePolicyOpContext(SignedSettingsHelper::Callback* callback, | 127 RetrievePolicyOpContext(SignedSettingsHelper::RetrievePolicyCallback callback, |
205 OpContext::Delegate* delegate) | 128 OpContext::Delegate* delegate) |
206 : OpContext(callback, delegate) { | 129 : OpContext(delegate), |
| 130 callback_(callback){ |
207 } | 131 } |
208 | 132 |
209 // chromeos::SignedSettings::Delegate implementation | 133 // chromeos::SignedSettings::Delegate implementation |
210 virtual void OnSettingsOpCompleted( | 134 virtual void OnSettingsOpCompleted( |
211 SignedSettings::ReturnCode code, | 135 SignedSettings::ReturnCode code, |
212 const em::PolicyFetchResponse& policy) OVERRIDE { | 136 const em::PolicyFetchResponse& policy) OVERRIDE { |
213 if (callback_) | 137 callback_.Run(code, policy); |
214 callback_->OnRetrievePolicyCompleted(code, policy); | |
215 OnOpCompleted(); | 138 OnOpCompleted(); |
216 } | 139 } |
217 | 140 |
218 protected: | 141 protected: |
219 // OpContext implementation | 142 // OpContext implementation |
220 virtual void CreateOp() OVERRIDE { | 143 virtual void CreateOp() OVERRIDE { |
221 op_ = SignedSettings::CreateRetrievePolicyOp(this); | 144 op_ = SignedSettings::CreateRetrievePolicyOp(this); |
222 } | 145 } |
223 | 146 |
224 private: | 147 private: |
| 148 SignedSettingsHelper::RetrievePolicyCallback callback_; |
| 149 |
225 DISALLOW_COPY_AND_ASSIGN(RetrievePolicyOpContext); | 150 DISALLOW_COPY_AND_ASSIGN(RetrievePolicyOpContext); |
226 }; | 151 }; |
227 | 152 |
228 } // namespace | 153 } // namespace |
229 | 154 |
230 | 155 |
231 class SignedSettingsHelperImpl : public SignedSettingsHelper, | 156 class SignedSettingsHelperImpl : public SignedSettingsHelper, |
232 public OpContext::Delegate { | 157 public OpContext::Delegate { |
233 public: | 158 public: |
234 // SignedSettingsHelper implementation | 159 // SignedSettingsHelper implementation |
235 virtual void StartStorePropertyOp(const std::string& name, | |
236 const base::Value& value, | |
237 Callback* callback) OVERRIDE; | |
238 virtual void StartRetrieveProperty(const std::string& name, | |
239 Callback* callback) OVERRIDE; | |
240 virtual void StartStorePolicyOp(const em::PolicyFetchResponse& policy, | 160 virtual void StartStorePolicyOp(const em::PolicyFetchResponse& policy, |
241 Callback* callback) OVERRIDE; | 161 StorePolicyCallback callback) OVERRIDE; |
242 virtual void StartRetrievePolicyOp(Callback* callback) OVERRIDE; | 162 virtual void StartRetrievePolicyOp(RetrievePolicyCallback callback) OVERRIDE; |
243 virtual void CancelCallback(Callback* callback) OVERRIDE; | |
244 | 163 |
245 // OpContext::Delegate implementation | 164 // OpContext::Delegate implementation |
246 virtual void OnOpCreated(OpContext* context); | 165 virtual void OnOpCreated(OpContext* context); |
247 virtual void OnOpStarted(OpContext* context); | 166 virtual void OnOpStarted(OpContext* context); |
248 virtual void OnOpCompleted(OpContext* context); | 167 virtual void OnOpCompleted(OpContext* context); |
249 | 168 |
250 private: | 169 private: |
251 SignedSettingsHelperImpl(); | 170 SignedSettingsHelperImpl(); |
252 ~SignedSettingsHelperImpl(); | 171 virtual ~SignedSettingsHelperImpl(); |
253 | 172 |
254 void AddOpContext(OpContext* context); | 173 void AddOpContext(OpContext* context); |
255 void ClearAll(); | 174 void ClearAll(); |
256 | 175 |
257 std::vector<OpContext*> pending_contexts_; | 176 std::vector<OpContext*> pending_contexts_; |
258 | 177 |
259 friend struct base::DefaultLazyInstanceTraits<SignedSettingsHelperImpl>; | 178 friend struct base::DefaultLazyInstanceTraits<SignedSettingsHelperImpl>; |
260 DISALLOW_COPY_AND_ASSIGN(SignedSettingsHelperImpl); | 179 DISALLOW_COPY_AND_ASSIGN(SignedSettingsHelperImpl); |
261 }; | 180 }; |
262 | 181 |
263 static base::LazyInstance<SignedSettingsHelperImpl> | 182 static base::LazyInstance<SignedSettingsHelperImpl> |
264 g_signed_settings_helper_impl = LAZY_INSTANCE_INITIALIZER; | 183 g_signed_settings_helper_impl = LAZY_INSTANCE_INITIALIZER; |
265 | 184 |
266 SignedSettingsHelperImpl::SignedSettingsHelperImpl() { | 185 SignedSettingsHelperImpl::SignedSettingsHelperImpl() { |
267 } | 186 } |
268 | 187 |
269 SignedSettingsHelperImpl::~SignedSettingsHelperImpl() { | 188 SignedSettingsHelperImpl::~SignedSettingsHelperImpl() { |
270 if (!pending_contexts_.empty()) { | 189 if (!pending_contexts_.empty()) { |
271 LOG(WARNING) << "SignedSettingsHelperImpl shutdown with pending ops, " | 190 LOG(WARNING) << "SignedSettingsHelperImpl shutdown with pending ops, " |
272 << "changes will be lost."; | 191 << "changes will be lost."; |
273 ClearAll(); | 192 ClearAll(); |
274 } | 193 } |
275 } | 194 } |
276 | 195 |
277 void SignedSettingsHelperImpl::StartStorePropertyOp( | |
278 const std::string& name, | |
279 const base::Value& value, | |
280 SignedSettingsHelper::Callback* callback) { | |
281 AddOpContext(new StorePropertyOpContext( | |
282 name, | |
283 value, | |
284 callback, | |
285 this)); | |
286 } | |
287 | |
288 void SignedSettingsHelperImpl::StartRetrieveProperty( | |
289 const std::string& name, | |
290 SignedSettingsHelper::Callback* callback) { | |
291 AddOpContext(new RetrievePropertyOpContext( | |
292 name, | |
293 callback, | |
294 this)); | |
295 } | |
296 | |
297 void SignedSettingsHelperImpl::StartStorePolicyOp( | 196 void SignedSettingsHelperImpl::StartStorePolicyOp( |
298 const em::PolicyFetchResponse& policy, | 197 const em::PolicyFetchResponse& policy, |
299 SignedSettingsHelper::Callback* callback) { | 198 StorePolicyCallback callback) { |
300 AddOpContext(new StorePolicyOpContext(policy, callback, this)); | 199 AddOpContext(new StorePolicyOpContext(policy, callback, this)); |
301 } | 200 } |
302 | 201 |
303 void SignedSettingsHelperImpl::StartRetrievePolicyOp( | 202 void SignedSettingsHelperImpl::StartRetrievePolicyOp( |
304 SignedSettingsHelper::Callback* callback) { | 203 RetrievePolicyCallback callback) { |
305 AddOpContext(new RetrievePolicyOpContext(callback, this)); | 204 AddOpContext(new RetrievePolicyOpContext(callback, this)); |
306 } | 205 } |
307 | 206 |
308 void SignedSettingsHelperImpl::CancelCallback( | |
309 SignedSettingsHelper::Callback* callback) { | |
310 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
311 | |
312 for (size_t i = 0; i < pending_contexts_.size(); ++i) { | |
313 if (pending_contexts_[i]->callback() == callback) { | |
314 pending_contexts_[i]->CancelCallback(); | |
315 } | |
316 } | |
317 } | |
318 | |
319 void SignedSettingsHelperImpl::AddOpContext(OpContext* context) { | 207 void SignedSettingsHelperImpl::AddOpContext(OpContext* context) { |
320 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 208 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
321 CHECK(context); | 209 CHECK(context); |
322 | 210 |
323 pending_contexts_.push_back(context); | 211 pending_contexts_.push_back(context); |
324 if (pending_contexts_.size() == 1) | 212 if (pending_contexts_.size() == 1) |
325 context->Execute(); | 213 context->Execute(); |
326 } | 214 } |
327 | 215 |
328 void SignedSettingsHelperImpl::ClearAll() { | 216 void SignedSettingsHelperImpl::ClearAll() { |
(...skipping 26 matching lines...) Expand all Loading... |
355 | 243 |
356 if (test_delegate_) | 244 if (test_delegate_) |
357 test_delegate_->OnOpCompleted(context->op()); | 245 test_delegate_->OnOpCompleted(context->op()); |
358 } | 246 } |
359 | 247 |
360 SignedSettingsHelper* SignedSettingsHelper::Get() { | 248 SignedSettingsHelper* SignedSettingsHelper::Get() { |
361 return g_signed_settings_helper_impl.Pointer(); | 249 return g_signed_settings_helper_impl.Pointer(); |
362 } | 250 } |
363 | 251 |
364 } // namespace chromeos | 252 } // namespace chromeos |
OLD | NEW |