| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/policy/cloud/cloud_policy_client_registration_helper.h" | 5 #include "chrome/browser/policy/cloud/cloud_policy_client_registration_helper.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 // This class fetches an OAuth2 token scoped for the userinfo and DM services. | 38 // This class fetches an OAuth2 token scoped for the userinfo and DM services. |
| 39 // On Android, we use a special API to allow us to fetch a token for an account | 39 // On Android, we use a special API to allow us to fetch a token for an account |
| 40 // that is not yet logged in to allow fetching the token before the sign-in | 40 // that is not yet logged in to allow fetching the token before the sign-in |
| 41 // process is finished. | 41 // process is finished. |
| 42 class CloudPolicyClientRegistrationHelper::TokenServiceHelper | 42 class CloudPolicyClientRegistrationHelper::TokenServiceHelper |
| 43 : public OAuth2TokenService::Consumer { | 43 : public OAuth2TokenService::Consumer { |
| 44 public: | 44 public: |
| 45 TokenServiceHelper(); | 45 TokenServiceHelper(); |
| 46 | 46 |
| 47 void FetchAccessToken( | 47 void FetchAccessToken( |
| 48 #if defined(OS_ANDROID) | |
| 49 // TODO(atwilson): Remove this when StartRequestForUsername() is merged | |
| 50 // into the base OAuth2TokenService class. | |
| 51 AndroidProfileOAuth2TokenService* token_service, | |
| 52 #else | |
| 53 OAuth2TokenService* token_service, | 48 OAuth2TokenService* token_service, |
| 54 #endif | |
| 55 const std::string& username, | 49 const std::string& username, |
| 56 const StringCallback& callback); | 50 const StringCallback& callback); |
| 57 | 51 |
| 58 private: | 52 private: |
| 59 // OAuth2TokenService::Consumer implementation: | 53 // OAuth2TokenService::Consumer implementation: |
| 60 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | 54 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
| 61 const std::string& access_token, | 55 const std::string& access_token, |
| 62 const base::Time& expiration_time) OVERRIDE; | 56 const base::Time& expiration_time) OVERRIDE; |
| 63 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, | 57 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
| 64 const GoogleServiceAuthError& error) OVERRIDE; | 58 const GoogleServiceAuthError& error) OVERRIDE; |
| 65 | 59 |
| 66 StringCallback callback_; | 60 StringCallback callback_; |
| 67 scoped_ptr<OAuth2TokenService::Request> token_request_; | 61 scoped_ptr<OAuth2TokenService::Request> token_request_; |
| 68 }; | 62 }; |
| 69 | 63 |
| 70 CloudPolicyClientRegistrationHelper::TokenServiceHelper::TokenServiceHelper() {} | 64 CloudPolicyClientRegistrationHelper::TokenServiceHelper::TokenServiceHelper() {} |
| 71 | 65 |
| 72 void CloudPolicyClientRegistrationHelper::TokenServiceHelper::FetchAccessToken( | 66 void CloudPolicyClientRegistrationHelper::TokenServiceHelper::FetchAccessToken( |
| 73 #if defined(OS_ANDROID) | |
| 74 AndroidProfileOAuth2TokenService* token_service, | |
| 75 #else | |
| 76 OAuth2TokenService* token_service, | 67 OAuth2TokenService* token_service, |
| 77 #endif | 68 const std::string& account_id, |
| 78 const std::string& username, | |
| 79 const StringCallback& callback) { | 69 const StringCallback& callback) { |
| 80 DCHECK(!token_request_); | 70 DCHECK(!token_request_); |
| 81 // Either the caller must supply a username, or the user must be signed in | 71 // Either the caller must supply a username, or the user must be signed in |
| 82 // already. | 72 // already. |
| 83 DCHECK(!username.empty() || token_service->RefreshTokenIsAvailable()); | 73 DCHECK(!account_id.empty() || |
| 74 token_service->RefreshTokenIsAvailable(account_id)); |
| 84 callback_ = callback; | 75 callback_ = callback; |
| 85 | 76 |
| 86 OAuth2TokenService::ScopeSet scopes; | 77 OAuth2TokenService::ScopeSet scopes; |
| 87 scopes.insert(GaiaConstants::kDeviceManagementServiceOAuth); | 78 scopes.insert(GaiaConstants::kDeviceManagementServiceOAuth); |
| 88 scopes.insert(kServiceScopeGetUserInfo); | 79 scopes.insert(kServiceScopeGetUserInfo); |
| 89 | 80 token_request_ = token_service->StartRequest(account_id, scopes, this); |
| 90 #if defined(OS_ANDROID) | |
| 91 token_request_ = | |
| 92 token_service->StartRequestForUsername(username, scopes, this); | |
| 93 #else | |
| 94 token_request_ = token_service->StartRequest(scopes, this); | |
| 95 #endif | |
| 96 } | 81 } |
| 97 | 82 |
| 98 void CloudPolicyClientRegistrationHelper::TokenServiceHelper::OnGetTokenSuccess( | 83 void CloudPolicyClientRegistrationHelper::TokenServiceHelper::OnGetTokenSuccess( |
| 99 const OAuth2TokenService::Request* request, | 84 const OAuth2TokenService::Request* request, |
| 100 const std::string& access_token, | 85 const std::string& access_token, |
| 101 const base::Time& expiration_time) { | 86 const base::Time& expiration_time) { |
| 102 DCHECK_EQ(token_request_.get(), request); | 87 DCHECK_EQ(token_request_.get(), request); |
| 103 callback_.Run(access_token); | 88 callback_.Run(access_token); |
| 104 } | 89 } |
| 105 | 90 |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 | 173 |
| 189 CloudPolicyClientRegistrationHelper::~CloudPolicyClientRegistrationHelper() { | 174 CloudPolicyClientRegistrationHelper::~CloudPolicyClientRegistrationHelper() { |
| 190 // Clean up any pending observers in case the browser is shutdown while | 175 // Clean up any pending observers in case the browser is shutdown while |
| 191 // trying to register for policy. | 176 // trying to register for policy. |
| 192 if (client_) | 177 if (client_) |
| 193 client_->RemoveObserver(this); | 178 client_->RemoveObserver(this); |
| 194 } | 179 } |
| 195 | 180 |
| 196 | 181 |
| 197 void CloudPolicyClientRegistrationHelper::StartRegistration( | 182 void CloudPolicyClientRegistrationHelper::StartRegistration( |
| 198 #if defined(OS_ANDROID) | |
| 199 AndroidProfileOAuth2TokenService* token_service, | |
| 200 #else | |
| 201 OAuth2TokenService* token_service, | 183 OAuth2TokenService* token_service, |
| 202 #endif | 184 const std::string& account_id, |
| 203 const std::string& username, | |
| 204 const base::Closure& callback) { | 185 const base::Closure& callback) { |
| 205 DVLOG(1) << "Starting registration process with username"; | 186 DVLOG(1) << "Starting registration process with username"; |
| 206 DCHECK(!client_->is_registered()); | 187 DCHECK(!client_->is_registered()); |
| 207 callback_ = callback; | 188 callback_ = callback; |
| 208 client_->AddObserver(this); | 189 client_->AddObserver(this); |
| 209 | 190 |
| 210 token_service_helper_.reset(new TokenServiceHelper()); | 191 token_service_helper_.reset(new TokenServiceHelper()); |
| 211 token_service_helper_->FetchAccessToken( | 192 token_service_helper_->FetchAccessToken( |
| 212 token_service, | 193 token_service, |
| 213 username, | 194 account_id, |
| 214 base::Bind(&CloudPolicyClientRegistrationHelper::OnTokenFetched, | 195 base::Bind(&CloudPolicyClientRegistrationHelper::OnTokenFetched, |
| 215 base::Unretained(this))); | 196 base::Unretained(this))); |
| 216 } | 197 } |
| 217 | 198 |
| 218 #if !defined(OS_ANDROID) | 199 #if !defined(OS_ANDROID) |
| 219 void CloudPolicyClientRegistrationHelper::StartRegistrationWithLoginToken( | 200 void CloudPolicyClientRegistrationHelper::StartRegistrationWithLoginToken( |
| 220 const std::string& login_refresh_token, | 201 const std::string& login_refresh_token, |
| 221 const base::Closure& callback) { | 202 const base::Closure& callback) { |
| 222 DVLOG(1) << "Starting registration process with login token"; | 203 DVLOG(1) << "Starting registration process with login token"; |
| 223 DCHECK(!client_->is_registered()); | 204 DCHECK(!client_->is_registered()); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 void CloudPolicyClientRegistrationHelper::RequestCompleted() { | 292 void CloudPolicyClientRegistrationHelper::RequestCompleted() { |
| 312 if (client_) { | 293 if (client_) { |
| 313 client_->RemoveObserver(this); | 294 client_->RemoveObserver(this); |
| 314 // |client_| may be freed by the callback so clear it now. | 295 // |client_| may be freed by the callback so clear it now. |
| 315 client_ = NULL; | 296 client_ = NULL; |
| 316 callback_.Run(); | 297 callback_.Run(); |
| 317 } | 298 } |
| 318 } | 299 } |
| 319 | 300 |
| 320 } // namespace policy | 301 } // namespace policy |
| OLD | NEW |