| Index: chrome/browser/policy/cloud/cloud_policy_client_registration_helper.cc
|
| diff --git a/chrome/browser/policy/cloud/cloud_policy_client_registration_helper.cc b/chrome/browser/policy/cloud/cloud_policy_client_registration_helper.cc
|
| index 224977f651b3dc0e671cd55a82fbaccf6296a8ae..0f0e7be373e6a1050c4ddd715cf2af4a2e4e379c 100644
|
| --- a/chrome/browser/policy/cloud/cloud_policy_client_registration_helper.cc
|
| +++ b/chrome/browser/policy/cloud/cloud_policy_client_registration_helper.cc
|
| @@ -11,13 +11,13 @@
|
| #include "base/logging.h"
|
| #include "base/time/time.h"
|
| #include "base/values.h"
|
| +#include "chrome/browser/signin/oauth2_token_service.h"
|
| #include "google_apis/gaia/gaia_constants.h"
|
| #include "google_apis/gaia/gaia_urls.h"
|
| #include "google_apis/gaia/google_service_auth_error.h"
|
|
|
| #if defined(OS_ANDROID)
|
| #include "chrome/browser/signin/android_profile_oauth2_token_service.h"
|
| -#include "chrome/browser/signin/oauth2_token_service.h"
|
| #else
|
| #include "google_apis/gaia/oauth2_access_token_consumer.h"
|
| #include "google_apis/gaia/oauth2_access_token_fetcher.h"
|
| @@ -25,8 +25,6 @@
|
|
|
| namespace policy {
|
|
|
| -namespace {
|
| -
|
| // OAuth2 scope for the userinfo service.
|
| const char kServiceScopeGetUserInfo[] =
|
| "https://www.googleapis.com/auth/userinfo.email";
|
| @@ -37,22 +35,25 @@ const char kGetHostedDomainKey[] = "hd";
|
|
|
| typedef base::Callback<void(const std::string&)> StringCallback;
|
|
|
| -} // namespace
|
| -
|
| -#if defined(OS_ANDROID)
|
| -
|
| // This class fetches an OAuth2 token scoped for the userinfo and DM services.
|
| -// The AccountManager is used to mint the token on the Java side, given the
|
| -// username of an account that is known to exist on the device.
|
| -// This allows fetching the token before the sign-in process is finished.
|
| -class CloudPolicyClientRegistrationHelper::TokenHelperAndroid
|
| +// On Android, we use a special API to allow us to fetch a token for an account
|
| +// that is not yet logged in to allow fetching the token before the sign-in
|
| +// process is finished.
|
| +class CloudPolicyClientRegistrationHelper::TokenServiceHelper
|
| : public OAuth2TokenService::Consumer {
|
| public:
|
| - TokenHelperAndroid();
|
| + TokenServiceHelper();
|
|
|
| - void FetchAccessToken(AndroidProfileOAuth2TokenService* token_service,
|
| - const std::string& username,
|
| - const StringCallback& callback);
|
| + void FetchAccessToken(
|
| +#if defined(OS_ANDROID)
|
| + // TODO(atwilson): Remove this when StartRequestForUsername() is merged
|
| + // into the base OAuth2TokenService class.
|
| + AndroidProfileOAuth2TokenService* token_service,
|
| +#else
|
| + OAuth2TokenService* token_service,
|
| +#endif
|
| + const std::string& username,
|
| + const StringCallback& callback);
|
|
|
| private:
|
| // OAuth2TokenService::Consumer implementation:
|
| @@ -66,23 +67,35 @@ class CloudPolicyClientRegistrationHelper::TokenHelperAndroid
|
| scoped_ptr<OAuth2TokenService::Request> token_request_;
|
| };
|
|
|
| -CloudPolicyClientRegistrationHelper::TokenHelperAndroid::TokenHelperAndroid() {}
|
| +CloudPolicyClientRegistrationHelper::TokenServiceHelper::TokenServiceHelper() {}
|
|
|
| -void CloudPolicyClientRegistrationHelper::TokenHelperAndroid::FetchAccessToken(
|
| +void CloudPolicyClientRegistrationHelper::TokenServiceHelper::FetchAccessToken(
|
| +#if defined(OS_ANDROID)
|
| AndroidProfileOAuth2TokenService* token_service,
|
| +#else
|
| + OAuth2TokenService* token_service,
|
| +#endif
|
| const std::string& username,
|
| const StringCallback& callback) {
|
| + DCHECK(!token_request_);
|
| + // Either the caller must supply a username, or the user must be signed in
|
| + // already.
|
| + DCHECK(!username.empty() || token_service->RefreshTokenIsAvailable());
|
| callback_ = callback;
|
|
|
| OAuth2TokenService::ScopeSet scopes;
|
| scopes.insert(GaiaConstants::kDeviceManagementServiceOAuth);
|
| scopes.insert(kServiceScopeGetUserInfo);
|
|
|
| +#if defined(OS_ANDROID)
|
| token_request_ =
|
| token_service->StartRequestForUsername(username, scopes, this);
|
| +#else
|
| + token_request_ = token_service->StartRequest(scopes, this);
|
| +#endif
|
| }
|
|
|
| -void CloudPolicyClientRegistrationHelper::TokenHelperAndroid::OnGetTokenSuccess(
|
| +void CloudPolicyClientRegistrationHelper::TokenServiceHelper::OnGetTokenSuccess(
|
| const OAuth2TokenService::Request* request,
|
| const std::string& access_token,
|
| const base::Time& expiration_time) {
|
| @@ -90,22 +103,23 @@ void CloudPolicyClientRegistrationHelper::TokenHelperAndroid::OnGetTokenSuccess(
|
| callback_.Run(access_token);
|
| }
|
|
|
| -void CloudPolicyClientRegistrationHelper::TokenHelperAndroid::OnGetTokenFailure(
|
| +void CloudPolicyClientRegistrationHelper::TokenServiceHelper::OnGetTokenFailure(
|
| const OAuth2TokenService::Request* request,
|
| const GoogleServiceAuthError& error) {
|
| DCHECK_EQ(token_request_.get(), request);
|
| callback_.Run("");
|
| }
|
|
|
| -#else
|
| -
|
| +#if !defined(OS_ANDROID)
|
| // This class fetches the OAuth2 token scoped for the userinfo and DM services.
|
| // It uses an OAuth2AccessTokenFetcher to fetch it, given a login refresh token
|
| -// that can be used to authorize that request.
|
| -class CloudPolicyClientRegistrationHelper::TokenHelper
|
| +// that can be used to authorize that request. This class is not needed on
|
| +// Android because we can use OAuth2TokenService to fetch tokens for accounts
|
| +// even before they are signed in.
|
| +class CloudPolicyClientRegistrationHelper::LoginTokenHelper
|
| : public OAuth2AccessTokenConsumer {
|
| public:
|
| - TokenHelper();
|
| + LoginTokenHelper();
|
|
|
| void FetchAccessToken(const std::string& login_refresh_token,
|
| net::URLRequestContextGetter* context,
|
| @@ -122,12 +136,13 @@ class CloudPolicyClientRegistrationHelper::TokenHelper
|
| scoped_ptr<OAuth2AccessTokenFetcher> oauth2_access_token_fetcher_;
|
| };
|
|
|
| -CloudPolicyClientRegistrationHelper::TokenHelper::TokenHelper() {}
|
| +CloudPolicyClientRegistrationHelper::LoginTokenHelper::LoginTokenHelper() {}
|
|
|
| -void CloudPolicyClientRegistrationHelper::TokenHelper::FetchAccessToken(
|
| +void CloudPolicyClientRegistrationHelper::LoginTokenHelper::FetchAccessToken(
|
| const std::string& login_refresh_token,
|
| net::URLRequestContextGetter* context,
|
| const StringCallback& callback) {
|
| + DCHECK(!oauth2_access_token_fetcher_);
|
| callback_ = callback;
|
|
|
| // Start fetching an OAuth2 access token for the device management and
|
| @@ -145,13 +160,13 @@ void CloudPolicyClientRegistrationHelper::TokenHelper::FetchAccessToken(
|
| scopes);
|
| }
|
|
|
| -void CloudPolicyClientRegistrationHelper::TokenHelper::OnGetTokenSuccess(
|
| +void CloudPolicyClientRegistrationHelper::LoginTokenHelper::OnGetTokenSuccess(
|
| const std::string& access_token,
|
| const base::Time& expiration_time) {
|
| callback_.Run(access_token);
|
| }
|
|
|
| -void CloudPolicyClientRegistrationHelper::TokenHelper::OnGetTokenFailure(
|
| +void CloudPolicyClientRegistrationHelper::LoginTokenHelper::OnGetTokenFailure(
|
| const GoogleServiceAuthError& error) {
|
| callback_.Run("");
|
| }
|
| @@ -178,10 +193,13 @@ CloudPolicyClientRegistrationHelper::~CloudPolicyClientRegistrationHelper() {
|
| client_->RemoveObserver(this);
|
| }
|
|
|
| -#if defined(OS_ANDROID)
|
|
|
| void CloudPolicyClientRegistrationHelper::StartRegistration(
|
| +#if defined(OS_ANDROID)
|
| AndroidProfileOAuth2TokenService* token_service,
|
| +#else
|
| + OAuth2TokenService* token_service,
|
| +#endif
|
| const std::string& username,
|
| const base::Closure& callback) {
|
| DVLOG(1) << "Starting registration process with username";
|
| @@ -189,16 +207,15 @@ void CloudPolicyClientRegistrationHelper::StartRegistration(
|
| callback_ = callback;
|
| client_->AddObserver(this);
|
|
|
| - token_helper_.reset(new TokenHelperAndroid());
|
| - token_helper_->FetchAccessToken(
|
| + token_service_helper_.reset(new TokenServiceHelper());
|
| + token_service_helper_->FetchAccessToken(
|
| token_service,
|
| username,
|
| base::Bind(&CloudPolicyClientRegistrationHelper::OnTokenFetched,
|
| base::Unretained(this)));
|
| }
|
|
|
| -#else
|
| -
|
| +#if !defined(OS_ANDROID)
|
| void CloudPolicyClientRegistrationHelper::StartRegistrationWithLoginToken(
|
| const std::string& login_refresh_token,
|
| const base::Closure& callback) {
|
| @@ -207,19 +224,22 @@ void CloudPolicyClientRegistrationHelper::StartRegistrationWithLoginToken(
|
| callback_ = callback;
|
| client_->AddObserver(this);
|
|
|
| - token_helper_.reset(new TokenHelper());
|
| - token_helper_->FetchAccessToken(
|
| + login_token_helper_.reset(
|
| + new CloudPolicyClientRegistrationHelper::LoginTokenHelper());
|
| + login_token_helper_->FetchAccessToken(
|
| login_refresh_token,
|
| context_,
|
| base::Bind(&CloudPolicyClientRegistrationHelper::OnTokenFetched,
|
| base::Unretained(this)));
|
| }
|
| -
|
| #endif
|
|
|
| void CloudPolicyClientRegistrationHelper::OnTokenFetched(
|
| const std::string& access_token) {
|
| - token_helper_.reset();
|
| +#if !defined(OS_ANDROID)
|
| + login_token_helper_.reset();
|
| +#endif
|
| + token_service_helper_.reset();
|
|
|
| if (access_token.empty()) {
|
| DLOG(WARNING) << "Could not fetch access token for "
|
|
|