| 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..35789e5356b578c1cfe52de4710f54d260e4a174 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,6 +25,12 @@
|
|
|
| namespace policy {
|
|
|
| +class CloudPolicyClientRegistrationHelper::TokenHelper {
|
| + public:
|
| + TokenHelper() {}
|
| + virtual ~TokenHelper() {}
|
| +};
|
| +
|
| namespace {
|
|
|
| // OAuth2 scope for the userinfo service.
|
| @@ -37,22 +43,26 @@ 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
|
| - : public OAuth2TokenService::Consumer {
|
| +// 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 TokenServiceHelper
|
| + : public CloudPolicyClientRegistrationHelper::TokenHelper,
|
| + 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 +76,33 @@ class CloudPolicyClientRegistrationHelper::TokenHelperAndroid
|
| scoped_ptr<OAuth2TokenService::Request> token_request_;
|
| };
|
|
|
| -CloudPolicyClientRegistrationHelper::TokenHelperAndroid::TokenHelperAndroid() {}
|
| +TokenServiceHelper::TokenServiceHelper() {}
|
|
|
| -void CloudPolicyClientRegistrationHelper::TokenHelperAndroid::FetchAccessToken(
|
| +void TokenServiceHelper::FetchAccessToken(
|
| +#if defined(OS_ANDROID)
|
| AndroidProfileOAuth2TokenService* token_service,
|
| +#else
|
| + OAuth2TokenService* token_service,
|
| +#endif
|
| const std::string& username,
|
| const StringCallback& callback) {
|
| + DCHECK(!token_request_);
|
| + DCHECK(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 TokenServiceHelper::OnGetTokenSuccess(
|
| const OAuth2TokenService::Request* request,
|
| const std::string& access_token,
|
| const base::Time& expiration_time) {
|
| @@ -90,22 +110,24 @@ void CloudPolicyClientRegistrationHelper::TokenHelperAndroid::OnGetTokenSuccess(
|
| callback_.Run(access_token);
|
| }
|
|
|
| -void CloudPolicyClientRegistrationHelper::TokenHelperAndroid::OnGetTokenFailure(
|
| +void 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
|
| - : public OAuth2AccessTokenConsumer {
|
| +// 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 LoginTokenHelper
|
| + : public CloudPolicyClientRegistrationHelper::TokenHelper,
|
| + public OAuth2AccessTokenConsumer {
|
| public:
|
| - TokenHelper();
|
| + LoginTokenHelper();
|
|
|
| void FetchAccessToken(const std::string& login_refresh_token,
|
| net::URLRequestContextGetter* context,
|
| @@ -122,12 +144,13 @@ class CloudPolicyClientRegistrationHelper::TokenHelper
|
| scoped_ptr<OAuth2AccessTokenFetcher> oauth2_access_token_fetcher_;
|
| };
|
|
|
| -CloudPolicyClientRegistrationHelper::TokenHelper::TokenHelper() {}
|
| +LoginTokenHelper::LoginTokenHelper() {}
|
|
|
| -void CloudPolicyClientRegistrationHelper::TokenHelper::FetchAccessToken(
|
| +void 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,19 +168,19 @@ void CloudPolicyClientRegistrationHelper::TokenHelper::FetchAccessToken(
|
| scopes);
|
| }
|
|
|
| -void CloudPolicyClientRegistrationHelper::TokenHelper::OnGetTokenSuccess(
|
| - const std::string& access_token,
|
| - const base::Time& expiration_time) {
|
| +void LoginTokenHelper::OnGetTokenSuccess(const std::string& access_token,
|
| + const base::Time& expiration_time) {
|
| callback_.Run(access_token);
|
| }
|
|
|
| -void CloudPolicyClientRegistrationHelper::TokenHelper::OnGetTokenFailure(
|
| - const GoogleServiceAuthError& error) {
|
| +void LoginTokenHelper::OnGetTokenFailure(const GoogleServiceAuthError& error) {
|
| callback_.Run("");
|
| }
|
|
|
| #endif
|
|
|
| +} // namespace
|
| +
|
| CloudPolicyClientRegistrationHelper::CloudPolicyClientRegistrationHelper(
|
| net::URLRequestContextGetter* context,
|
| CloudPolicyClient* client,
|
| @@ -178,10 +201,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 +215,16 @@ void CloudPolicyClientRegistrationHelper::StartRegistration(
|
| callback_ = callback;
|
| client_->AddObserver(this);
|
|
|
| - token_helper_.reset(new TokenHelperAndroid());
|
| - token_helper_->FetchAccessToken(
|
| + scoped_ptr<TokenServiceHelper> token_service_helper(new TokenServiceHelper());
|
| + token_service_helper->FetchAccessToken(
|
| token_service,
|
| username,
|
| base::Bind(&CloudPolicyClientRegistrationHelper::OnTokenFetched,
|
| base::Unretained(this)));
|
| + token_helper_ = token_service_helper.PassAs<TokenHelper>();
|
| }
|
|
|
| -#else
|
| -
|
| +#if !defined(OS_ANDROID)
|
| void CloudPolicyClientRegistrationHelper::StartRegistrationWithLoginToken(
|
| const std::string& login_refresh_token,
|
| const base::Closure& callback) {
|
| @@ -207,14 +233,14 @@ void CloudPolicyClientRegistrationHelper::StartRegistrationWithLoginToken(
|
| callback_ = callback;
|
| client_->AddObserver(this);
|
|
|
| - token_helper_.reset(new TokenHelper());
|
| - token_helper_->FetchAccessToken(
|
| + scoped_ptr<LoginTokenHelper> login_token_helper(new LoginTokenHelper());
|
| + login_token_helper->FetchAccessToken(
|
| login_refresh_token,
|
| context_,
|
| base::Bind(&CloudPolicyClientRegistrationHelper::OnTokenFetched,
|
| base::Unretained(this)));
|
| + token_helper_ = login_token_helper.PassAs<TokenHelper>();
|
| }
|
| -
|
| #endif
|
|
|
| void CloudPolicyClientRegistrationHelper::OnTokenFetched(
|
|
|