| 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" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "chrome/browser/chromeos/login/signed_settings.h" | 13 #include "chrome/browser/chromeos/login/signed_settings.h" |
| 14 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
| 14 #include "content/browser/browser_thread.h" | 15 #include "content/browser/browser_thread.h" |
| 15 | 16 |
| 16 namespace chromeos { | 17 namespace chromeos { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 class OpContext { | 21 class OpContext { |
| 21 public: | 22 public: |
| 22 class Delegate { | 23 class Delegate { |
| 23 public: | 24 public: |
| (...skipping 23 matching lines...) Expand all Loading... |
| 47 | 48 |
| 48 // Cancels the callback. | 49 // Cancels the callback. |
| 49 void CancelCallback() { | 50 void CancelCallback() { |
| 50 callback_ = NULL; | 51 callback_ = NULL; |
| 51 } | 52 } |
| 52 | 53 |
| 53 // Cancels the callback and cancels the op if it is not executing. | 54 // Cancels the callback and cancels the op if it is not executing. |
| 54 void Cancel() { | 55 void Cancel() { |
| 55 CancelCallback(); | 56 CancelCallback(); |
| 56 | 57 |
| 57 if (!executing_) | 58 if (!executing_) |
| 58 OnOpCompleted(); | 59 OnOpCompleted(); |
| 59 } | 60 } |
| 60 | 61 |
| 61 // Accessors. | 62 // Accessors. |
| 62 SignedSettings* op() const { | 63 SignedSettings* op() const { |
| 63 return op_.get(); | 64 return op_.get(); |
| 64 } | 65 } |
| 65 | 66 |
| 66 SignedSettingsHelper::Callback* callback() const { | 67 SignedSettingsHelper::Callback* callback() const { |
| 67 return callback_; | 68 return callback_; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 const std::string& email, | 111 const std::string& email, |
| 111 SignedSettingsHelper::Callback* callback, | 112 SignedSettingsHelper::Callback* callback, |
| 112 Delegate* delegate) | 113 Delegate* delegate) |
| 113 : OpContext(callback, delegate), | 114 : OpContext(callback, delegate), |
| 114 type_(type), | 115 type_(type), |
| 115 email_(email) { | 116 email_(email) { |
| 116 } | 117 } |
| 117 | 118 |
| 118 // chromeos::SignedSettings::Delegate implementation | 119 // chromeos::SignedSettings::Delegate implementation |
| 119 virtual void OnSettingsOpCompleted(SignedSettings::ReturnCode code, | 120 virtual void OnSettingsOpCompleted(SignedSettings::ReturnCode code, |
| 120 bool value) { | 121 bool value) OVERRIDE { |
| 121 if (callback_) { | 122 if (callback_) { |
| 122 switch (type_) { | 123 switch (type_) { |
| 123 case CHECK: | 124 case CHECK: |
| 124 callback_->OnCheckWhitelistCompleted(code, email_); | 125 callback_->OnCheckWhitelistCompleted(code, email_); |
| 125 break; | 126 break; |
| 126 case ADD: | 127 case ADD: |
| 127 callback_->OnWhitelistCompleted(code, email_); | 128 callback_->OnWhitelistCompleted(code, email_); |
| 128 break; | 129 break; |
| 129 case REMOVE: | 130 case REMOVE: |
| 130 callback_->OnUnwhitelistCompleted(code, email_); | 131 callback_->OnUnwhitelistCompleted(code, email_); |
| 131 break; | 132 break; |
| 132 default: | 133 default: |
| 133 LOG(ERROR) << "Unknown WhitelistOpContext type " << type_; | 134 LOG(ERROR) << "Unknown WhitelistOpContext type " << type_; |
| 134 break; | 135 break; |
| 135 } | 136 } |
| 136 } | 137 } |
| 137 OnOpCompleted(); | 138 OnOpCompleted(); |
| 138 } | 139 } |
| 139 | 140 |
| 140 protected: | 141 protected: |
| 141 // OpContext implemenetation | 142 // OpContext implemenetation |
| 142 virtual void CreateOp() { | 143 virtual void CreateOp() OVERRIDE { |
| 143 switch (type_) { | 144 switch (type_) { |
| 144 case CHECK: | 145 case CHECK: |
| 145 op_ = SignedSettings::CreateCheckWhitelistOp(email_, this); | 146 op_ = SignedSettings::CreateCheckWhitelistOp(email_, this); |
| 146 break; | 147 break; |
| 147 case ADD: | 148 case ADD: |
| 148 op_ = SignedSettings::CreateWhitelistOp(email_, true, this); | 149 op_ = SignedSettings::CreateWhitelistOp(email_, true, this); |
| 149 break; | 150 break; |
| 150 case REMOVE: | 151 case REMOVE: |
| 151 op_ = SignedSettings::CreateWhitelistOp(email_, false, this); | 152 op_ = SignedSettings::CreateWhitelistOp(email_, false, this); |
| 152 break; | 153 break; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 170 const std::string& value, | 171 const std::string& value, |
| 171 SignedSettingsHelper::Callback* callback, | 172 SignedSettingsHelper::Callback* callback, |
| 172 Delegate* delegate) | 173 Delegate* delegate) |
| 173 : OpContext(callback, delegate), | 174 : OpContext(callback, delegate), |
| 174 name_(name), | 175 name_(name), |
| 175 value_(value) { | 176 value_(value) { |
| 176 } | 177 } |
| 177 | 178 |
| 178 // chromeos::SignedSettings::Delegate implementation | 179 // chromeos::SignedSettings::Delegate implementation |
| 179 virtual void OnSettingsOpCompleted(SignedSettings::ReturnCode code, | 180 virtual void OnSettingsOpCompleted(SignedSettings::ReturnCode code, |
| 180 bool unused) { | 181 bool unused) OVERRIDE { |
| 181 VLOG(2) << "OnSettingsOpCompleted, code = " << code; | 182 VLOG(2) << "OnSettingsOpCompleted, code = " << code; |
| 182 if (callback_) | 183 if (callback_) |
| 183 callback_->OnStorePropertyCompleted(code, name_, value_); | 184 callback_->OnStorePropertyCompleted(code, name_, value_); |
| 184 OnOpCompleted(); | 185 OnOpCompleted(); |
| 185 } | 186 } |
| 186 | 187 |
| 187 protected: | 188 protected: |
| 188 // OpContext implemenetation | 189 // OpContext implemenetation |
| 189 virtual void CreateOp() { | 190 virtual void CreateOp() OVERRIDE { |
| 190 op_ = SignedSettings::CreateStorePropertyOp(name_, value_, this); | 191 op_ = SignedSettings::CreateStorePropertyOp(name_, value_, this); |
| 191 } | 192 } |
| 192 | 193 |
| 193 private: | 194 private: |
| 194 std::string name_; | 195 std::string name_; |
| 195 std::string value_; | 196 std::string value_; |
| 196 | 197 |
| 197 DISALLOW_COPY_AND_ASSIGN(StorePropertyOpContext); | 198 DISALLOW_COPY_AND_ASSIGN(StorePropertyOpContext); |
| 198 }; | 199 }; |
| 199 | 200 |
| 200 class RetrievePropertyOpContext | 201 class RetrievePropertyOpContext |
| 201 : public SignedSettings::Delegate<std::string>, | 202 : public SignedSettings::Delegate<std::string>, |
| 202 public OpContext { | 203 public OpContext { |
| 203 public: | 204 public: |
| 204 RetrievePropertyOpContext(const std::string& name, | 205 RetrievePropertyOpContext(const std::string& name, |
| 205 SignedSettingsHelper::Callback* callback, | 206 SignedSettingsHelper::Callback* callback, |
| 206 Delegate* delegate) | 207 Delegate* delegate) |
| 207 : OpContext(callback, delegate), | 208 : OpContext(callback, delegate), |
| 208 name_(name) { | 209 name_(name) { |
| 209 } | 210 } |
| 210 | 211 |
| 211 // chromeos::SignedSettings::Delegate implementation | 212 // chromeos::SignedSettings::Delegate implementation |
| 212 virtual void OnSettingsOpCompleted(SignedSettings::ReturnCode code, | 213 virtual void OnSettingsOpCompleted(SignedSettings::ReturnCode code, |
| 213 std::string value) { | 214 std::string value) OVERRIDE { |
| 214 if (callback_) | 215 if (callback_) |
| 215 callback_->OnRetrievePropertyCompleted(code, name_, value); | 216 callback_->OnRetrievePropertyCompleted(code, name_, value); |
| 216 | 217 |
| 217 OnOpCompleted(); | 218 OnOpCompleted(); |
| 218 } | 219 } |
| 219 | 220 |
| 220 protected: | 221 protected: |
| 221 // OpContext implemenetation | 222 // OpContext implemenetation |
| 222 virtual void CreateOp() { | 223 virtual void CreateOp() OVERRIDE { |
| 223 op_ = SignedSettings::CreateRetrievePropertyOp(name_, this); | 224 op_ = SignedSettings::CreateRetrievePropertyOp(name_, this); |
| 224 } | 225 } |
| 225 | 226 |
| 226 private: | 227 private: |
| 227 std::string name_; | 228 std::string name_; |
| 228 | 229 |
| 229 DISALLOW_COPY_AND_ASSIGN(RetrievePropertyOpContext); | 230 DISALLOW_COPY_AND_ASSIGN(RetrievePropertyOpContext); |
| 230 }; | 231 }; |
| 231 | 232 |
| 233 class StorePolicyOpContext : public SignedSettings::Delegate<bool>, |
| 234 public OpContext { |
| 235 public: |
| 236 StorePolicyOpContext(const em::PolicyFetchResponse& policy, |
| 237 SignedSettingsHelper::Callback* callback, |
| 238 Delegate* delegate) |
| 239 : OpContext(callback, delegate), |
| 240 policy_(policy) { |
| 241 } |
| 242 |
| 243 // chromeos::SignedSettings::Delegate implementation |
| 244 virtual void OnSettingsOpCompleted(SignedSettings::ReturnCode code, |
| 245 bool unused) OVERRIDE { |
| 246 VLOG(2) << "OnSettingsOpCompleted, code = " << code; |
| 247 if (callback_) |
| 248 callback_->OnStorePolicyCompleted(code); |
| 249 OnOpCompleted(); |
| 250 } |
| 251 |
| 252 protected: |
| 253 // OpContext implementation |
| 254 virtual void CreateOp() OVERRIDE { |
| 255 op_ = SignedSettings::CreateStorePolicyOp(&policy_, this); |
| 256 } |
| 257 |
| 258 private: |
| 259 em::PolicyFetchResponse policy_; |
| 260 DISALLOW_COPY_AND_ASSIGN(StorePolicyOpContext); |
| 261 }; |
| 262 |
| 263 class RetrievePolicyOpContext |
| 264 : public SignedSettings::Delegate<const em::PolicyFetchResponse&>, |
| 265 public OpContext { |
| 266 public: |
| 267 RetrievePolicyOpContext(SignedSettingsHelper::Callback* callback, |
| 268 Delegate* delegate) |
| 269 : OpContext(callback, delegate) { |
| 270 } |
| 271 |
| 272 // chromeos::SignedSettings::Delegate implementation |
| 273 virtual void OnSettingsOpCompleted( |
| 274 SignedSettings::ReturnCode code, |
| 275 const em::PolicyFetchResponse& policy) OVERRIDE { |
| 276 if (callback_) |
| 277 callback_->OnRetrievePolicyCompleted(code, policy); |
| 278 OnOpCompleted(); |
| 279 } |
| 280 |
| 281 protected: |
| 282 // OpContext implementation |
| 283 virtual void CreateOp() OVERRIDE { |
| 284 op_ = SignedSettings::CreateRetrievePolicyOp(this); |
| 285 } |
| 286 |
| 287 private: |
| 288 DISALLOW_COPY_AND_ASSIGN(RetrievePolicyOpContext); |
| 289 }; |
| 290 |
| 232 } // namespace | 291 } // namespace |
| 233 | 292 |
| 234 | 293 |
| 235 class SignedSettingsHelperImpl : public SignedSettingsHelper, | 294 class SignedSettingsHelperImpl : public SignedSettingsHelper, |
| 236 public OpContext::Delegate { | 295 public OpContext::Delegate { |
| 237 public: | 296 public: |
| 238 // SignedSettingsHelper implementation | 297 // SignedSettingsHelper implementation |
| 239 virtual void StartCheckWhitelistOp(const std::string& email, | 298 virtual void StartCheckWhitelistOp(const std::string& email, |
| 240 Callback* callback); | 299 Callback* callback) OVERRIDE; |
| 241 virtual void StartWhitelistOp(const std::string& email, | 300 virtual void StartWhitelistOp(const std::string& email, |
| 242 bool add_to_whitelist, | 301 bool add_to_whitelist, |
| 243 Callback* callback); | 302 Callback* callback) OVERRIDE; |
| 244 virtual void StartStorePropertyOp(const std::string& name, | 303 virtual void StartStorePropertyOp(const std::string& name, |
| 245 const std::string& value, | 304 const std::string& value, |
| 246 Callback* callback); | 305 Callback* callback) OVERRIDE; |
| 247 virtual void StartRetrieveProperty(const std::string& name, | 306 virtual void StartRetrieveProperty(const std::string& name, |
| 248 Callback* callback); | 307 Callback* callback) OVERRIDE; |
| 249 virtual void CancelCallback(Callback* callback); | 308 virtual void StartStorePolicyOp(const em::PolicyFetchResponse& policy, |
| 309 Callback* callback) OVERRIDE; |
| 310 virtual void StartRetrievePolicyOp(Callback* callback) OVERRIDE; |
| 311 virtual void CancelCallback(Callback* callback) OVERRIDE; |
| 250 | 312 |
| 251 // OpContext::Delegate implementation | 313 // OpContext::Delegate implementation |
| 252 virtual void OnOpCreated(OpContext* context); | 314 virtual void OnOpCreated(OpContext* context); |
| 253 virtual void OnOpStarted(OpContext* context); | 315 virtual void OnOpStarted(OpContext* context); |
| 254 virtual void OnOpCompleted(OpContext* context); | 316 virtual void OnOpCompleted(OpContext* context); |
| 255 | 317 |
| 256 private: | 318 private: |
| 257 SignedSettingsHelperImpl(); | 319 SignedSettingsHelperImpl(); |
| 258 ~SignedSettingsHelperImpl(); | 320 ~SignedSettingsHelperImpl(); |
| 259 | 321 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 AddOpContext(new WhitelistOpContext( | 359 AddOpContext(new WhitelistOpContext( |
| 298 add_to_whitelist ? WhitelistOpContext::ADD : WhitelistOpContext::REMOVE, | 360 add_to_whitelist ? WhitelistOpContext::ADD : WhitelistOpContext::REMOVE, |
| 299 email, | 361 email, |
| 300 callback, | 362 callback, |
| 301 this)); | 363 this)); |
| 302 } | 364 } |
| 303 | 365 |
| 304 void SignedSettingsHelperImpl::StartStorePropertyOp( | 366 void SignedSettingsHelperImpl::StartStorePropertyOp( |
| 305 const std::string& name, | 367 const std::string& name, |
| 306 const std::string& value, | 368 const std::string& value, |
| 307 SignedSettingsHelper::SignedSettingsHelper::Callback* callback) { | 369 SignedSettingsHelper::Callback* callback) { |
| 308 AddOpContext(new StorePropertyOpContext( | 370 AddOpContext(new StorePropertyOpContext( |
| 309 name, | 371 name, |
| 310 value, | 372 value, |
| 311 callback, | 373 callback, |
| 312 this)); | 374 this)); |
| 313 } | 375 } |
| 314 | 376 |
| 315 void SignedSettingsHelperImpl::StartRetrieveProperty( | 377 void SignedSettingsHelperImpl::StartRetrieveProperty( |
| 316 const std::string& name, | 378 const std::string& name, |
| 317 SignedSettingsHelper::Callback* callback) { | 379 SignedSettingsHelper::Callback* callback) { |
| 318 AddOpContext(new RetrievePropertyOpContext( | 380 AddOpContext(new RetrievePropertyOpContext( |
| 319 name, | 381 name, |
| 320 callback, | 382 callback, |
| 321 this)); | 383 this)); |
| 322 } | 384 } |
| 323 | 385 |
| 386 void SignedSettingsHelperImpl::StartStorePolicyOp( |
| 387 const em::PolicyFetchResponse& policy, |
| 388 SignedSettingsHelper::Callback* callback) { |
| 389 AddOpContext(new StorePolicyOpContext(policy, callback, this)); |
| 390 } |
| 391 |
| 392 void SignedSettingsHelperImpl::StartRetrievePolicyOp( |
| 393 SignedSettingsHelper::Callback* callback) { |
| 394 AddOpContext(new RetrievePolicyOpContext(callback, this)); |
| 395 } |
| 396 |
| 324 void SignedSettingsHelperImpl::CancelCallback( | 397 void SignedSettingsHelperImpl::CancelCallback( |
| 325 SignedSettingsHelper::Callback* callback) { | 398 SignedSettingsHelper::Callback* callback) { |
| 326 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 399 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 327 | 400 |
| 328 for (size_t i = 0; i < pending_contexts_.size(); ++i) { | 401 for (size_t i = 0; i < pending_contexts_.size(); ++i) { |
| 329 if (pending_contexts_[i]->callback() == callback) { | 402 if (pending_contexts_[i]->callback() == callback) { |
| 330 pending_contexts_[i]->CancelCallback(); | 403 pending_contexts_[i]->CancelCallback(); |
| 331 } | 404 } |
| 332 } | 405 } |
| 333 } | 406 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 371 | 444 |
| 372 if (test_delegate_) | 445 if (test_delegate_) |
| 373 test_delegate_->OnOpCompleted(context->op()); | 446 test_delegate_->OnOpCompleted(context->op()); |
| 374 } | 447 } |
| 375 | 448 |
| 376 SignedSettingsHelper* SignedSettingsHelper::Get() { | 449 SignedSettingsHelper* SignedSettingsHelper::Get() { |
| 377 return g_signed_settings_helper_impl.Pointer(); | 450 return g_signed_settings_helper_impl.Pointer(); |
| 378 } | 451 } |
| 379 | 452 |
| 380 } // namespace chromeos | 453 } // namespace chromeos |
| OLD | NEW |