Chromium Code Reviews| Index: chrome/browser/chromeos/settings/device_oauth2_token_service_delegate.cc |
| diff --git a/chrome/browser/chromeos/settings/device_oauth2_token_service.cc b/chrome/browser/chromeos/settings/device_oauth2_token_service_delegate.cc |
| similarity index 57% |
| copy from chrome/browser/chromeos/settings/device_oauth2_token_service.cc |
| copy to chrome/browser/chromeos/settings/device_oauth2_token_service_delegate.cc |
| index 1dc6bde934c3f50fbc53cff2d0e33b5f09c50b46..b1aecc4b06ab429cf2952da5f51631adfd14abfa 100644 |
| --- a/chrome/browser/chromeos/settings/device_oauth2_token_service.cc |
| +++ b/chrome/browser/chromeos/settings/device_oauth2_token_service_delegate.cc |
| @@ -1,8 +1,8 @@ |
| -// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -#include "chrome/browser/chromeos/settings/device_oauth2_token_service.h" |
| +#include "chrome/browser/chromeos/settings/device_oauth2_token_service_delegate.h" |
| #include <string> |
| #include <vector> |
| @@ -27,62 +27,48 @@ |
| namespace chromeos { |
| -struct DeviceOAuth2TokenService::PendingRequest { |
| - PendingRequest(const base::WeakPtr<RequestImpl>& request, |
| - const std::string& client_id, |
| - const std::string& client_secret, |
| - const ScopeSet& scopes) |
| - : request(request), |
| - client_id(client_id), |
| - client_secret(client_secret), |
| - scopes(scopes) {} |
| - |
| - const base::WeakPtr<RequestImpl> request; |
| - const std::string client_id; |
| - const std::string client_secret; |
| - const ScopeSet scopes; |
| -}; |
| - |
| -void DeviceOAuth2TokenService::OnServiceAccountIdentityChanged() { |
| +void DeviceOAuth2TokenServiceDelegate::OnServiceAccountIdentityChanged() { |
| if (!GetRobotAccountId().empty() && !refresh_token_.empty()) |
| FireRefreshTokenAvailable(GetRobotAccountId()); |
| } |
| -DeviceOAuth2TokenService::DeviceOAuth2TokenService( |
| +DeviceOAuth2TokenServiceDelegate::DeviceOAuth2TokenServiceDelegate( |
| net::URLRequestContextGetter* getter, |
| PrefService* local_state) |
| : url_request_context_getter_(getter), |
| local_state_(local_state), |
| state_(STATE_LOADING), |
| max_refresh_token_validation_retries_(3), |
| + has_pending_requests_(false), |
| + service_error_status_observer_(nullptr), |
| service_account_identity_subscription_( |
| - CrosSettings::Get()->AddSettingsObserver( |
| - kServiceAccountIdentity, |
| - base::Bind( |
| - &DeviceOAuth2TokenService::OnServiceAccountIdentityChanged, |
| - base::Unretained(this))).Pass()), |
| + CrosSettings::Get() |
| + ->AddSettingsObserver( |
| + kServiceAccountIdentity, |
| + base::Bind(&DeviceOAuth2TokenServiceDelegate:: |
| + OnServiceAccountIdentityChanged, |
| + base::Unretained(this))) |
| + .Pass()), |
| weak_ptr_factory_(this) { |
| // Pull in the system salt. |
| SystemSaltGetter::Get()->GetSystemSalt( |
| - base::Bind(&DeviceOAuth2TokenService::DidGetSystemSalt, |
| + base::Bind(&DeviceOAuth2TokenServiceDelegate::DidGetSystemSalt, |
| weak_ptr_factory_.GetWeakPtr())); |
| } |
| -DeviceOAuth2TokenService::~DeviceOAuth2TokenService() { |
| - FlushPendingRequests(false, GoogleServiceAuthError::REQUEST_CANCELED); |
| +DeviceOAuth2TokenServiceDelegate::~DeviceOAuth2TokenServiceDelegate() { |
| + if (service_error_status_observer_) |
| + service_error_status_observer_->OnServiceError( |
| + GoogleServiceAuthError::REQUEST_CANCELED); |
| FlushTokenSaveCallbacks(false); |
| } |
| -// static |
| -void DeviceOAuth2TokenService::RegisterPrefs(PrefRegistrySimple* registry) { |
| - registry->RegisterStringPref(prefs::kDeviceRobotAnyApiRefreshToken, |
| - std::string()); |
| -} |
| - |
| -void DeviceOAuth2TokenService::SetAndSaveRefreshToken( |
| +void DeviceOAuth2TokenServiceDelegate::SetAndSaveRefreshToken( |
| const std::string& refresh_token, |
| const StatusCallback& result_callback) { |
| - FlushPendingRequests(false, GoogleServiceAuthError::REQUEST_CANCELED); |
| + if (service_error_status_observer_) |
|
Mattias Nissler (ping if slow)
2015/07/01 12:34:13
nit: add curly braces for multi-line conditional s
gogerald1
2015/07/01 17:58:42
Done.
|
| + service_error_status_observer_->OnServiceError( |
| + GoogleServiceAuthError::REQUEST_CANCELED); |
| bool waiting_for_salt = state_ == STATE_LOADING; |
| refresh_token_ = refresh_token; |
| @@ -103,7 +89,7 @@ void DeviceOAuth2TokenService::SetAndSaveRefreshToken( |
| } |
| } |
| -bool DeviceOAuth2TokenService::RefreshTokenIsAvailable( |
| +bool DeviceOAuth2TokenServiceDelegate::RefreshTokenIsAvailable( |
| const std::string& account_id) const { |
| switch (state_) { |
| case STATE_NO_TOKEN: |
| @@ -120,22 +106,20 @@ bool DeviceOAuth2TokenService::RefreshTokenIsAvailable( |
| return false; |
| } |
| -std::string DeviceOAuth2TokenService::GetRobotAccountId() const { |
| +std::string DeviceOAuth2TokenServiceDelegate::GetRobotAccountId() const { |
| std::string result; |
| CrosSettings::Get()->GetString(kServiceAccountIdentity, &result); |
| return result; |
| } |
| -void DeviceOAuth2TokenService::OnRefreshTokenResponse( |
| +void DeviceOAuth2TokenServiceDelegate::OnRefreshTokenResponse( |
| const std::string& access_token, |
| int expires_in_seconds) { |
| - gaia_oauth_client_->GetTokenInfo( |
| - access_token, |
| - max_refresh_token_validation_retries_, |
| - this); |
| + gaia_oauth_client_->GetTokenInfo(access_token, |
| + max_refresh_token_validation_retries_, this); |
| } |
| -void DeviceOAuth2TokenService::OnGetTokenInfoResponse( |
| +void DeviceOAuth2TokenServiceDelegate::OnGetTokenInfoResponse( |
| scoped_ptr<base::DictionaryValue> token_info) { |
| std::string gaia_robot_id; |
| token_info->GetString("email", &gaia_robot_id); |
| @@ -144,22 +128,26 @@ void DeviceOAuth2TokenService::OnGetTokenInfoResponse( |
| CheckRobotAccountId(gaia_robot_id); |
| } |
| -void DeviceOAuth2TokenService::OnOAuthError() { |
| +void DeviceOAuth2TokenServiceDelegate::OnOAuthError() { |
| gaia_oauth_client_.reset(); |
| state_ = STATE_TOKEN_INVALID; |
| - FlushPendingRequests(false, GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
| + if (service_error_status_observer_) |
|
Mattias Nissler (ping if slow)
2015/07/01 12:34:13
nit: braces
gogerald1
2015/07/01 17:58:41
Done.
|
| + service_error_status_observer_->OnServiceError( |
| + GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
| } |
| -void DeviceOAuth2TokenService::OnNetworkError(int response_code) { |
| +void DeviceOAuth2TokenServiceDelegate::OnNetworkError(int response_code) { |
| gaia_oauth_client_.reset(); |
| // Go back to pending validation state. That'll allow a retry on subsequent |
| // token minting requests. |
| state_ = STATE_VALIDATION_PENDING; |
| - FlushPendingRequests(false, GoogleServiceAuthError::CONNECTION_FAILED); |
| + if (service_error_status_observer_) |
|
Mattias Nissler (ping if slow)
2015/07/01 12:34:13
nit: braces
gogerald1
2015/07/01 17:58:41
Done.
|
| + service_error_status_observer_->OnServiceError( |
| + GoogleServiceAuthError::CONNECTION_FAILED); |
| } |
| -std::string DeviceOAuth2TokenService::GetRefreshToken( |
| +std::string DeviceOAuth2TokenServiceDelegate::GetRefreshToken( |
| const std::string& account_id) const { |
| switch (state_) { |
| case STATE_LOADING: |
| @@ -181,45 +169,13 @@ std::string DeviceOAuth2TokenService::GetRefreshToken( |
| return std::string(); |
| } |
| -net::URLRequestContextGetter* DeviceOAuth2TokenService::GetRequestContext() { |
| +net::URLRequestContextGetter* |
| +DeviceOAuth2TokenServiceDelegate::GetRequestContext() const { |
| return url_request_context_getter_.get(); |
| } |
| -void DeviceOAuth2TokenService::FetchOAuth2Token( |
| - RequestImpl* request, |
| - const std::string& account_id, |
| - net::URLRequestContextGetter* getter, |
| - const std::string& client_id, |
| - const std::string& client_secret, |
| - const ScopeSet& scopes) { |
| - switch (state_) { |
| - case STATE_VALIDATION_PENDING: |
| - // If this is the first request for a token, start validation. |
| - StartValidation(); |
| - // fall through. |
| - case STATE_LOADING: |
| - case STATE_VALIDATION_STARTED: |
| - // Add a pending request that will be satisfied once validation completes. |
| - pending_requests_.push_back(new PendingRequest( |
| - request->AsWeakPtr(), client_id, client_secret, scopes)); |
| - return; |
| - case STATE_NO_TOKEN: |
| - FailRequest(request, GoogleServiceAuthError::USER_NOT_SIGNED_UP); |
| - return; |
| - case STATE_TOKEN_INVALID: |
| - FailRequest(request, GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
| - return; |
| - case STATE_TOKEN_VALID: |
| - // Pass through to OAuth2TokenService to satisfy the request. |
| - OAuth2TokenService::FetchOAuth2Token( |
| - request, account_id, getter, client_id, client_secret, scopes); |
| - return; |
| - } |
| - |
| - NOTREACHED() << "Unexpected state " << state_; |
| -} |
| - |
| -OAuth2AccessTokenFetcher* DeviceOAuth2TokenService::CreateAccessTokenFetcher( |
| +OAuth2AccessTokenFetcher* |
| +DeviceOAuth2TokenServiceDelegate::CreateAccessTokenFetcher( |
| const std::string& account_id, |
| net::URLRequestContextGetter* getter, |
| OAuth2AccessTokenConsumer* consumer) { |
| @@ -228,8 +184,7 @@ OAuth2AccessTokenFetcher* DeviceOAuth2TokenService::CreateAccessTokenFetcher( |
| return new OAuth2AccessTokenFetcherImpl(consumer, getter, refresh_token); |
| } |
| - |
| -void DeviceOAuth2TokenService::DidGetSystemSalt( |
| +void DeviceOAuth2TokenServiceDelegate::DidGetSystemSalt( |
| const std::string& system_salt) { |
| system_salt_ = system_salt; |
| @@ -266,7 +221,7 @@ void DeviceOAuth2TokenService::DidGetSystemSalt( |
| state_ = STATE_VALIDATION_PENDING; |
| // If there are pending requests, start a validation. |
| - if (!pending_requests_.empty()) |
| + if (has_pending_requests_) |
| StartValidation(); |
| // Announce the token. |
| @@ -274,14 +229,13 @@ void DeviceOAuth2TokenService::DidGetSystemSalt( |
| FireRefreshTokensLoaded(); |
| } |
| -void DeviceOAuth2TokenService::CheckRobotAccountId( |
| +void DeviceOAuth2TokenServiceDelegate::CheckRobotAccountId( |
| const std::string& gaia_robot_id) { |
| // Make sure the value returned by GetRobotAccountId has been validated |
| // against current device settings. |
| - switch (CrosSettings::Get()->PrepareTrustedValues(base::Bind( |
| - &DeviceOAuth2TokenService::CheckRobotAccountId, |
| - weak_ptr_factory_.GetWeakPtr(), |
| - gaia_robot_id))) { |
| + switch (CrosSettings::Get()->PrepareTrustedValues( |
| + base::Bind(&DeviceOAuth2TokenServiceDelegate::CheckRobotAccountId, |
| + weak_ptr_factory_.GetWeakPtr(), gaia_robot_id))) { |
| case CrosSettingsProvider::TRUSTED: |
| // All good, compare account ids below. |
| break; |
| @@ -293,14 +247,18 @@ void DeviceOAuth2TokenService::CheckRobotAccountId( |
| // There's no trusted account id, which is equivalent to no token present. |
| LOG(WARNING) << "Device settings permanently untrusted."; |
| state_ = STATE_NO_TOKEN; |
| - FlushPendingRequests(false, GoogleServiceAuthError::USER_NOT_SIGNED_UP); |
| + if (service_error_status_observer_) |
|
Mattias Nissler (ping if slow)
2015/07/01 12:34:13
nit: braces
gogerald1
2015/07/01 17:58:42
Done.
|
| + service_error_status_observer_->OnServiceError( |
| + GoogleServiceAuthError::USER_NOT_SIGNED_UP); |
| return; |
| } |
| std::string policy_robot_id = GetRobotAccountId(); |
| if (policy_robot_id == gaia_robot_id) { |
| state_ = STATE_TOKEN_VALID; |
| - FlushPendingRequests(true, GoogleServiceAuthError::NONE); |
| + if (service_error_status_observer_) |
|
Mattias Nissler (ping if slow)
2015/07/01 12:34:13
nit: braces
gogerald1
2015/07/01 17:58:41
Done.
|
| + service_error_status_observer_->OnServiceError( |
| + GoogleServiceAuthError::NONE); |
| } else { |
| if (gaia_robot_id.empty()) { |
| LOG(WARNING) << "Device service account owner in policy is empty."; |
| @@ -309,12 +267,13 @@ void DeviceOAuth2TokenService::CheckRobotAccountId( |
| << "refresh token owner \"" << gaia_robot_id << "\"."; |
| } |
| state_ = STATE_TOKEN_INVALID; |
| - FlushPendingRequests(false, |
| - GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
| + if (service_error_status_observer_) |
|
Mattias Nissler (ping if slow)
2015/07/01 12:34:13
nit: braces
Given the frequency of this code bloc
gogerald1
2015/07/01 17:58:41
Acknowledged. might be more efficient to inline sh
Mattias Nissler (ping if slow)
2015/07/01 18:50:04
I don't think efficiency is of any concern in this
|
| + service_error_status_observer_->OnServiceError( |
| + GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
| } |
| } |
| -void DeviceOAuth2TokenService::EncryptAndSaveToken() { |
| +void DeviceOAuth2TokenServiceDelegate::EncryptAndSaveToken() { |
| DCHECK_NE(state_, STATE_LOADING); |
| CryptohomeTokenEncryptor encryptor(system_salt_); |
| @@ -332,14 +291,14 @@ void DeviceOAuth2TokenService::EncryptAndSaveToken() { |
| FlushTokenSaveCallbacks(result); |
| } |
| -void DeviceOAuth2TokenService::StartValidation() { |
| +void DeviceOAuth2TokenServiceDelegate::StartValidation() { |
| DCHECK_EQ(state_, STATE_VALIDATION_PENDING); |
| DCHECK(!gaia_oauth_client_); |
| state_ = STATE_VALIDATION_STARTED; |
| - gaia_oauth_client_.reset(new gaia::GaiaOAuthClient( |
| - g_browser_process->system_request_context())); |
| + gaia_oauth_client_.reset( |
| + new gaia::GaiaOAuthClient(g_browser_process->system_request_context())); |
| GaiaUrls* gaia_urls = GaiaUrls::GetInstance(); |
| gaia::OAuthClientInfo client_info; |
| @@ -347,60 +306,23 @@ void DeviceOAuth2TokenService::StartValidation() { |
| client_info.client_secret = gaia_urls->oauth2_chrome_client_secret(); |
| gaia_oauth_client_->RefreshToken( |
| - client_info, |
| - refresh_token_, |
| + client_info, refresh_token_, |
| std::vector<std::string>(1, GaiaConstants::kOAuthWrapBridgeUserInfoScope), |
| - max_refresh_token_validation_retries_, |
| - this); |
| -} |
| - |
| -void DeviceOAuth2TokenService::FlushPendingRequests( |
| - bool token_is_valid, |
| - GoogleServiceAuthError::State error) { |
| - std::vector<PendingRequest*> requests; |
| - requests.swap(pending_requests_); |
| - for (std::vector<PendingRequest*>::iterator request(requests.begin()); |
| - request != requests.end(); |
| - ++request) { |
| - scoped_ptr<PendingRequest> scoped_request(*request); |
| - if (!scoped_request->request) |
| - continue; |
| - |
| - if (token_is_valid) { |
| - OAuth2TokenService::FetchOAuth2Token( |
| - scoped_request->request.get(), |
| - scoped_request->request->GetAccountId(), |
| - GetRequestContext(), |
| - scoped_request->client_id, |
| - scoped_request->client_secret, |
| - scoped_request->scopes); |
| - } else { |
| - FailRequest(scoped_request->request.get(), error); |
| - } |
| - } |
| + max_refresh_token_validation_retries_, this); |
| } |
| -void DeviceOAuth2TokenService::FlushTokenSaveCallbacks(bool result) { |
| +void DeviceOAuth2TokenServiceDelegate::FlushTokenSaveCallbacks(bool result) { |
| std::vector<StatusCallback> callbacks; |
| callbacks.swap(token_save_callbacks_); |
| for (std::vector<StatusCallback>::iterator callback(callbacks.begin()); |
| - callback != callbacks.end(); |
| - ++callback) { |
| + callback != callbacks.end(); ++callback) { |
| if (!callback->is_null()) |
| callback->Run(result); |
| } |
| } |
| -void DeviceOAuth2TokenService::FailRequest( |
| - RequestImpl* request, |
| - GoogleServiceAuthError::State error) { |
| - GoogleServiceAuthError auth_error(error); |
| - base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| - &RequestImpl::InformConsumer, |
| - request->AsWeakPtr(), |
| - auth_error, |
| - std::string(), |
| - base::Time())); |
| +void DeviceOAuth2TokenServiceDelegate::HasPendingRequests(bool has) { |
|
Mattias Nissler (ping if slow)
2015/07/01 12:34:13
nit: This should be RequestValidation() and should
gogerald1
2015/07/01 17:58:42
Done.
|
| + has_pending_requests_ = has; |
| } |
| } // namespace chromeos |