| 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" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "google_apis/gaia/gaia_constants.h" | 14 #include "google_apis/gaia/gaia_constants.h" |
| 15 #include "google_apis/gaia/gaia_urls.h" | 15 #include "google_apis/gaia/gaia_urls.h" |
| 16 #include "google_apis/gaia/google_service_auth_error.h" | 16 #include "google_apis/gaia/google_service_auth_error.h" |
| 17 #include "google_apis/gaia/oauth2_token_service.h" | 17 #include "google_apis/gaia/oauth2_token_service.h" |
| 18 | 18 |
| 19 #if defined(OS_ANDROID) | 19 #if !defined(OS_ANDROID) |
| 20 #include "chrome/browser/signin/android_profile_oauth2_token_service.h" | |
| 21 #else | |
| 22 #include "google_apis/gaia/oauth2_access_token_consumer.h" | 20 #include "google_apis/gaia/oauth2_access_token_consumer.h" |
| 23 #include "google_apis/gaia/oauth2_access_token_fetcher.h" | 21 #include "google_apis/gaia/oauth2_access_token_fetcher.h" |
| 24 #endif | 22 #endif |
| 25 | 23 |
| 26 namespace policy { | 24 namespace policy { |
| 27 | 25 |
| 28 // OAuth2 scope for the userinfo service. | 26 // OAuth2 scope for the userinfo service. |
| 29 const char kServiceScopeGetUserInfo[] = | 27 const char kServiceScopeGetUserInfo[] = |
| 30 "https://www.googleapis.com/auth/userinfo.email"; | 28 "https://www.googleapis.com/auth/userinfo.email"; |
| 31 | 29 |
| 32 // The key under which the hosted-domain value is stored in the UserInfo | 30 // The key under which the hosted-domain value is stored in the UserInfo |
| 33 // response. | 31 // response. |
| 34 const char kGetHostedDomainKey[] = "hd"; | 32 const char kGetHostedDomainKey[] = "hd"; |
| 35 | 33 |
| 36 typedef base::Callback<void(const std::string&)> StringCallback; | 34 typedef base::Callback<void(const std::string&)> StringCallback; |
| 37 | 35 |
| 38 // This class fetches an OAuth2 token scoped for the userinfo and DM services. | 36 // 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 | 37 // 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 | 38 // that is not yet logged in to allow fetching the token before the sign-in |
| 41 // process is finished. | 39 // process is finished. |
| 42 class CloudPolicyClientRegistrationHelper::TokenServiceHelper | 40 class CloudPolicyClientRegistrationHelper::TokenServiceHelper |
| 43 : public OAuth2TokenService::Consumer { | 41 : public OAuth2TokenService::Consumer { |
| 44 public: | 42 public: |
| 45 TokenServiceHelper(); | 43 TokenServiceHelper(); |
| 46 | 44 |
| 47 void FetchAccessToken( | 45 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, | 46 OAuth2TokenService* token_service, |
| 54 #endif | |
| 55 const std::string& username, | 47 const std::string& username, |
| 56 const StringCallback& callback); | 48 const StringCallback& callback); |
| 57 | 49 |
| 58 private: | 50 private: |
| 59 // OAuth2TokenService::Consumer implementation: | 51 // OAuth2TokenService::Consumer implementation: |
| 60 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | 52 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
| 61 const std::string& access_token, | 53 const std::string& access_token, |
| 62 const base::Time& expiration_time) OVERRIDE; | 54 const base::Time& expiration_time) OVERRIDE; |
| 63 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, | 55 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
| 64 const GoogleServiceAuthError& error) OVERRIDE; | 56 const GoogleServiceAuthError& error) OVERRIDE; |
| 65 | 57 |
| 66 StringCallback callback_; | 58 StringCallback callback_; |
| 67 scoped_ptr<OAuth2TokenService::Request> token_request_; | 59 scoped_ptr<OAuth2TokenService::Request> token_request_; |
| 68 }; | 60 }; |
| 69 | 61 |
| 70 CloudPolicyClientRegistrationHelper::TokenServiceHelper::TokenServiceHelper() {} | 62 CloudPolicyClientRegistrationHelper::TokenServiceHelper::TokenServiceHelper() {} |
| 71 | 63 |
| 72 void CloudPolicyClientRegistrationHelper::TokenServiceHelper::FetchAccessToken( | 64 void CloudPolicyClientRegistrationHelper::TokenServiceHelper::FetchAccessToken( |
| 73 #if defined(OS_ANDROID) | |
| 74 AndroidProfileOAuth2TokenService* token_service, | |
| 75 #else | |
| 76 OAuth2TokenService* token_service, | 65 OAuth2TokenService* token_service, |
| 77 #endif | 66 const std::string& account_id, |
| 78 const std::string& username, | |
| 79 const StringCallback& callback) { | 67 const StringCallback& callback) { |
| 80 DCHECK(!token_request_); | 68 DCHECK(!token_request_); |
| 81 // Either the caller must supply a username, or the user must be signed in | 69 // Either the caller must supply a username, or the user must be signed in |
| 82 // already. | 70 // already. |
| 83 DCHECK(!username.empty() || token_service->RefreshTokenIsAvailable()); | 71 DCHECK(!account_id.empty()); |
| 72 DCHECK(token_service->RefreshTokenIsAvailable(account_id)); |
| 73 |
| 84 callback_ = callback; | 74 callback_ = callback; |
| 85 | 75 |
| 86 OAuth2TokenService::ScopeSet scopes; | 76 OAuth2TokenService::ScopeSet scopes; |
| 87 scopes.insert(GaiaConstants::kDeviceManagementServiceOAuth); | 77 scopes.insert(GaiaConstants::kDeviceManagementServiceOAuth); |
| 88 scopes.insert(kServiceScopeGetUserInfo); | 78 scopes.insert(kServiceScopeGetUserInfo); |
| 89 | 79 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 } | 80 } |
| 97 | 81 |
| 98 void CloudPolicyClientRegistrationHelper::TokenServiceHelper::OnGetTokenSuccess( | 82 void CloudPolicyClientRegistrationHelper::TokenServiceHelper::OnGetTokenSuccess( |
| 99 const OAuth2TokenService::Request* request, | 83 const OAuth2TokenService::Request* request, |
| 100 const std::string& access_token, | 84 const std::string& access_token, |
| 101 const base::Time& expiration_time) { | 85 const base::Time& expiration_time) { |
| 102 DCHECK_EQ(token_request_.get(), request); | 86 DCHECK_EQ(token_request_.get(), request); |
| 103 callback_.Run(access_token); | 87 callback_.Run(access_token); |
| 104 } | 88 } |
| 105 | 89 |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 | 172 |
| 189 CloudPolicyClientRegistrationHelper::~CloudPolicyClientRegistrationHelper() { | 173 CloudPolicyClientRegistrationHelper::~CloudPolicyClientRegistrationHelper() { |
| 190 // Clean up any pending observers in case the browser is shutdown while | 174 // Clean up any pending observers in case the browser is shutdown while |
| 191 // trying to register for policy. | 175 // trying to register for policy. |
| 192 if (client_) | 176 if (client_) |
| 193 client_->RemoveObserver(this); | 177 client_->RemoveObserver(this); |
| 194 } | 178 } |
| 195 | 179 |
| 196 | 180 |
| 197 void CloudPolicyClientRegistrationHelper::StartRegistration( | 181 void CloudPolicyClientRegistrationHelper::StartRegistration( |
| 198 #if defined(OS_ANDROID) | |
| 199 AndroidProfileOAuth2TokenService* token_service, | |
| 200 #else | |
| 201 OAuth2TokenService* token_service, | 182 OAuth2TokenService* token_service, |
| 202 #endif | 183 const std::string& account_id, |
| 203 const std::string& username, | |
| 204 const base::Closure& callback) { | 184 const base::Closure& callback) { |
| 205 DVLOG(1) << "Starting registration process with username"; | 185 DVLOG(1) << "Starting registration process with username"; |
| 206 DCHECK(!client_->is_registered()); | 186 DCHECK(!client_->is_registered()); |
| 207 callback_ = callback; | 187 callback_ = callback; |
| 208 client_->AddObserver(this); | 188 client_->AddObserver(this); |
| 209 | 189 |
| 210 token_service_helper_.reset(new TokenServiceHelper()); | 190 token_service_helper_.reset(new TokenServiceHelper()); |
| 211 token_service_helper_->FetchAccessToken( | 191 token_service_helper_->FetchAccessToken( |
| 212 token_service, | 192 token_service, |
| 213 username, | 193 account_id, |
| 214 base::Bind(&CloudPolicyClientRegistrationHelper::OnTokenFetched, | 194 base::Bind(&CloudPolicyClientRegistrationHelper::OnTokenFetched, |
| 215 base::Unretained(this))); | 195 base::Unretained(this))); |
| 216 } | 196 } |
| 217 | 197 |
| 218 #if !defined(OS_ANDROID) | 198 #if !defined(OS_ANDROID) |
| 219 void CloudPolicyClientRegistrationHelper::StartRegistrationWithLoginToken( | 199 void CloudPolicyClientRegistrationHelper::StartRegistrationWithLoginToken( |
| 220 const std::string& login_refresh_token, | 200 const std::string& login_refresh_token, |
| 221 const base::Closure& callback) { | 201 const base::Closure& callback) { |
| 222 DVLOG(1) << "Starting registration process with login token"; | 202 DVLOG(1) << "Starting registration process with login token"; |
| 223 DCHECK(!client_->is_registered()); | 203 DCHECK(!client_->is_registered()); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 void CloudPolicyClientRegistrationHelper::RequestCompleted() { | 291 void CloudPolicyClientRegistrationHelper::RequestCompleted() { |
| 312 if (client_) { | 292 if (client_) { |
| 313 client_->RemoveObserver(this); | 293 client_->RemoveObserver(this); |
| 314 // |client_| may be freed by the callback so clear it now. | 294 // |client_| may be freed by the callback so clear it now. |
| 315 client_ = NULL; | 295 client_ = NULL; |
| 316 callback_.Run(); | 296 callback_.Run(); |
| 317 } | 297 } |
| 318 } | 298 } |
| 319 | 299 |
| 320 } // namespace policy | 300 } // namespace policy |
| OLD | NEW |