| Index: chrome/browser/chromeos/policy/policy_oauth2_token_fetcher.cc
|
| diff --git a/chrome/browser/chromeos/policy/policy_oauth2_token_fetcher.cc b/chrome/browser/chromeos/policy/policy_oauth2_token_fetcher.cc
|
| index a656291564b206988b03532ac63df0051bdfb7b6..6c81f188888f6c430dd3895a2156f42e8b9a9f6a 100644
|
| --- a/chrome/browser/chromeos/policy/policy_oauth2_token_fetcher.cc
|
| +++ b/chrome/browser/chromeos/policy/policy_oauth2_token_fetcher.cc
|
| @@ -4,12 +4,10 @@
|
|
|
| #include "chrome/browser/chromeos/policy/policy_oauth2_token_fetcher.h"
|
|
|
| -#include <memory>
|
| #include <vector>
|
|
|
| #include "base/bind.h"
|
| #include "base/logging.h"
|
| -#include "base/memory/weak_ptr.h"
|
| #include "base/strings/string_util.h"
|
| #include "content/public/browser/browser_thread.h"
|
| #include "google_apis/gaia/gaia_auth_fetcher.h"
|
| @@ -25,109 +23,21 @@
|
|
|
| namespace {
|
|
|
| -// If true, fake policy tokens will be sent instead of making network requests.
|
| -bool use_fake_tokens_for_testing_ = false;
|
| -
|
| // Max retry count for token fetching requests.
|
| const int kMaxRequestAttemptCount = 5;
|
|
|
| // OAuth token request retry delay in milliseconds.
|
| const int kRequestRestartDelay = 3000;
|
|
|
| -class PolicyOAuth2TokenFetcherImpl : public PolicyOAuth2TokenFetcher,
|
| - public GaiaAuthConsumer,
|
| - public OAuth2AccessTokenConsumer {
|
| - public:
|
| - PolicyOAuth2TokenFetcherImpl();
|
| - ~PolicyOAuth2TokenFetcherImpl() override;
|
| +} // namespace
|
|
|
| - private:
|
| - // PolicyOAuth2TokenFetcher overrides.
|
| - void StartWithSigninContext(
|
| - net::URLRequestContextGetter* auth_context_getter,
|
| - net::URLRequestContextGetter* system_context_getter,
|
| - const TokenCallback& callback) override;
|
| - void StartWithAuthCode(const std::string& auth_code,
|
| - net::URLRequestContextGetter* system_context_getter,
|
| - const TokenCallback& callback) override;
|
| - void StartWithRefreshToken(
|
| - const std::string& oauth2_refresh_token,
|
| - net::URLRequestContextGetter* system_context_getter,
|
| - const TokenCallback& callback) override;
|
| +PolicyOAuth2TokenFetcher::PolicyOAuth2TokenFetcher() {
|
| +}
|
|
|
| - // Returns true if we have previously attempted to fetch tokens with this
|
| - // class and failed.
|
| - bool Failed() const override { return failed_; }
|
| +PolicyOAuth2TokenFetcher::~PolicyOAuth2TokenFetcher() {
|
| +}
|
|
|
| - const std::string& OAuth2RefreshToken() const override {
|
| - return oauth2_refresh_token_;
|
| - }
|
| - const std::string& OAuth2AccessToken() const override {
|
| - return oauth2_access_token_;
|
| - }
|
| -
|
| - // GaiaAuthConsumer overrides.
|
| - void OnClientOAuthSuccess(
|
| - const GaiaAuthConsumer::ClientOAuthResult& oauth_tokens) override;
|
| - void OnClientOAuthFailure(const GoogleServiceAuthError& error) override;
|
| -
|
| - // OAuth2AccessTokenConsumer overrides.
|
| - void OnGetTokenSuccess(const std::string& access_token,
|
| - const base::Time& expiration_time) override;
|
| - void OnGetTokenFailure(const GoogleServiceAuthError& error) override;
|
| -
|
| - // Starts fetching OAuth2 refresh token.
|
| - void StartFetchingRefreshToken();
|
| -
|
| - // Starts fetching OAuth2 access token for the device management service.
|
| - void StartFetchingAccessToken();
|
| -
|
| - // Decides how to proceed on GAIA |error|. If the error looks temporary,
|
| - // retries |task| until max retry count is reached.
|
| - // If retry count runs out, or error condition is unrecoverable, it calls
|
| - // Delegate::OnOAuth2TokenFetchFailed().
|
| - void RetryOnError(const GoogleServiceAuthError& error,
|
| - const base::Closure& task);
|
| -
|
| - // Passes |token| and |error| to the |callback_|.
|
| - void ForwardPolicyToken(const std::string& token,
|
| - const GoogleServiceAuthError& error);
|
| -
|
| - // Auth code which is used to retreive a refresh token.
|
| - std::string auth_code_;
|
| -
|
| - scoped_refptr<net::URLRequestContextGetter> auth_context_getter_;
|
| - scoped_refptr<net::URLRequestContextGetter> system_context_getter_;
|
| - std::unique_ptr<GaiaAuthFetcher> refresh_token_fetcher_;
|
| - std::unique_ptr<OAuth2AccessTokenFetcher> access_token_fetcher_;
|
| -
|
| - // OAuth2 refresh token. Could come either from the outside or through
|
| - // refresh token fetching flow within this class.
|
| - std::string oauth2_refresh_token_;
|
| -
|
| - // OAuth2 access token.
|
| - std::string oauth2_access_token_;
|
| -
|
| - // The retry counter. Increment this only when failure happened.
|
| - int retry_count_ = 0;
|
| -
|
| - // True if we have already failed to fetch the policy.
|
| - bool failed_ = false;
|
| -
|
| - // The callback to invoke when done.
|
| - TokenCallback callback_;
|
| -
|
| - base::WeakPtrFactory<PolicyOAuth2TokenFetcherImpl> weak_ptr_factory_;
|
| -
|
| - DISALLOW_COPY_AND_ASSIGN(PolicyOAuth2TokenFetcherImpl);
|
| -};
|
| -
|
| -PolicyOAuth2TokenFetcherImpl::PolicyOAuth2TokenFetcherImpl()
|
| - : weak_ptr_factory_(this) {}
|
| -
|
| -PolicyOAuth2TokenFetcherImpl::~PolicyOAuth2TokenFetcherImpl() {}
|
| -
|
| -void PolicyOAuth2TokenFetcherImpl::StartWithSigninContext(
|
| +void PolicyOAuth2TokenFetcher::StartWithSigninContext(
|
| net::URLRequestContextGetter* auth_context_getter,
|
| net::URLRequestContextGetter* system_context_getter,
|
| const TokenCallback& callback) {
|
| @@ -139,7 +49,7 @@
|
| StartFetchingRefreshToken();
|
| }
|
|
|
| -void PolicyOAuth2TokenFetcherImpl::StartWithAuthCode(
|
| +void PolicyOAuth2TokenFetcher::StartWithAuthCode(
|
| const std::string& auth_code,
|
| net::URLRequestContextGetter* system_context_getter,
|
| const TokenCallback& callback) {
|
| @@ -151,7 +61,7 @@
|
| StartFetchingRefreshToken();
|
| }
|
|
|
| -void PolicyOAuth2TokenFetcherImpl::StartWithRefreshToken(
|
| +void PolicyOAuth2TokenFetcher::StartWithRefreshToken(
|
| const std::string& oauth2_refresh_token,
|
| net::URLRequestContextGetter* system_context_getter,
|
| const TokenCallback& callback) {
|
| @@ -163,7 +73,7 @@
|
| StartFetchingAccessToken();
|
| }
|
|
|
| -void PolicyOAuth2TokenFetcherImpl::StartFetchingRefreshToken() {
|
| +void PolicyOAuth2TokenFetcher::StartFetchingRefreshToken() {
|
| if (auth_code_.empty()) {
|
| refresh_token_fetcher_.reset(new GaiaAuthFetcher(
|
| this, GaiaConstants::kChromeSource, auth_context_getter_.get()));
|
| @@ -176,7 +86,7 @@
|
| }
|
| }
|
|
|
| -void PolicyOAuth2TokenFetcherImpl::StartFetchingAccessToken() {
|
| +void PolicyOAuth2TokenFetcher::StartFetchingAccessToken() {
|
| std::vector<std::string> scopes;
|
| scopes.push_back(GaiaConstants::kDeviceManagementServiceOAuth);
|
| scopes.push_back(GaiaConstants::kOAuthWrapBridgeUserInfoScope);
|
| @@ -190,7 +100,7 @@
|
| scopes);
|
| }
|
|
|
| -void PolicyOAuth2TokenFetcherImpl::OnClientOAuthSuccess(
|
| +void PolicyOAuth2TokenFetcher::OnClientOAuthSuccess(
|
| const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens) {
|
| VLOG(1) << "OAuth2 tokens for policy fetching succeeded.";
|
| oauth2_refresh_token_ = oauth2_tokens.refresh_token;
|
| @@ -198,17 +108,16 @@
|
| StartFetchingAccessToken();
|
| }
|
|
|
| -void PolicyOAuth2TokenFetcherImpl::OnClientOAuthFailure(
|
| +void PolicyOAuth2TokenFetcher::OnClientOAuthFailure(
|
| const GoogleServiceAuthError& error) {
|
| VLOG(1) << "OAuth2 tokens fetch for policy fetch failed! (error = "
|
| << error.state() << ")";
|
| - RetryOnError(
|
| - error,
|
| - base::Bind(&PolicyOAuth2TokenFetcherImpl::StartFetchingRefreshToken,
|
| - weak_ptr_factory_.GetWeakPtr()));
|
| + RetryOnError(error,
|
| + base::Bind(&PolicyOAuth2TokenFetcher::StartFetchingRefreshToken,
|
| + AsWeakPtr()));
|
| }
|
|
|
| -void PolicyOAuth2TokenFetcherImpl::OnGetTokenSuccess(
|
| +void PolicyOAuth2TokenFetcher::OnGetTokenSuccess(
|
| const std::string& access_token,
|
| const base::Time& expiration_time) {
|
| VLOG(1) << "OAuth2 access token (device management) fetching succeeded.";
|
| @@ -217,17 +126,16 @@
|
| GoogleServiceAuthError(GoogleServiceAuthError::NONE));
|
| }
|
|
|
| -void PolicyOAuth2TokenFetcherImpl::OnGetTokenFailure(
|
| +void PolicyOAuth2TokenFetcher::OnGetTokenFailure(
|
| const GoogleServiceAuthError& error) {
|
| LOG(ERROR) << "OAuth2 access token (device management) fetching failed!";
|
| - RetryOnError(
|
| - error, base::Bind(&PolicyOAuth2TokenFetcherImpl::StartFetchingAccessToken,
|
| - weak_ptr_factory_.GetWeakPtr()));
|
| + RetryOnError(error,
|
| + base::Bind(&PolicyOAuth2TokenFetcher::StartFetchingAccessToken,
|
| + AsWeakPtr()));
|
| }
|
|
|
| -void PolicyOAuth2TokenFetcherImpl::RetryOnError(
|
| - const GoogleServiceAuthError& error,
|
| - const base::Closure& task) {
|
| +void PolicyOAuth2TokenFetcher::RetryOnError(const GoogleServiceAuthError& error,
|
| + const base::Closure& task) {
|
| DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
| if (error.IsTransientError() && retry_count_ < kMaxRequestAttemptCount) {
|
| retry_count_++;
|
| @@ -245,78 +153,11 @@
|
| ForwardPolicyToken(std::string(), error);
|
| }
|
|
|
| -void PolicyOAuth2TokenFetcherImpl::ForwardPolicyToken(
|
| +void PolicyOAuth2TokenFetcher::ForwardPolicyToken(
|
| const std::string& token,
|
| const GoogleServiceAuthError& error) {
|
| if (!callback_.is_null())
|
| callback_.Run(token, error);
|
| }
|
|
|
| -// Fake token fetcher that immediately returns tokens without making network
|
| -// requests.
|
| -class PolicyOAuth2TokenFetcherFake : public PolicyOAuth2TokenFetcher {
|
| - public:
|
| - PolicyOAuth2TokenFetcherFake() {}
|
| - ~PolicyOAuth2TokenFetcherFake() override {}
|
| -
|
| - private:
|
| - // PolicyOAuth2TokenFetcher overrides.
|
| - void StartWithSigninContext(
|
| - net::URLRequestContextGetter* auth_context_getter,
|
| - net::URLRequestContextGetter* system_context_getter,
|
| - const TokenCallback& callback) override {
|
| - ForwardPolicyToken(callback);
|
| - }
|
| -
|
| - void StartWithAuthCode(const std::string& auth_code,
|
| - net::URLRequestContextGetter* system_context_getter,
|
| - const TokenCallback& callback) override {
|
| - ForwardPolicyToken(callback);
|
| - }
|
| -
|
| - void StartWithRefreshToken(
|
| - const std::string& oauth2_refresh_token,
|
| - net::URLRequestContextGetter* system_context_getter,
|
| - const TokenCallback& callback) override {
|
| - ForwardPolicyToken(callback);
|
| - }
|
| -
|
| - bool Failed() const override { return false; }
|
| - const std::string& OAuth2RefreshToken() const override {
|
| - return refresh_token_;
|
| - }
|
| - const std::string& OAuth2AccessToken() const override {
|
| - return access_token_;
|
| - }
|
| -
|
| - private:
|
| - void ForwardPolicyToken(const TokenCallback& callback) {
|
| - if (!callback.is_null())
|
| - callback.Run(access_token_, GoogleServiceAuthError::AuthErrorNone());
|
| - }
|
| -
|
| - const std::string refresh_token_ = "fake_refresh_token";
|
| - const std::string access_token_ = "fake_access_token";
|
| -
|
| - DISALLOW_COPY_AND_ASSIGN(PolicyOAuth2TokenFetcherFake);
|
| -};
|
| -
|
| -} // namespace
|
| -
|
| -// static
|
| -void PolicyOAuth2TokenFetcher::UseFakeTokensForTesting() {
|
| - use_fake_tokens_for_testing_ = true;
|
| -}
|
| -
|
| -// static
|
| -PolicyOAuth2TokenFetcher* PolicyOAuth2TokenFetcher::CreateInstance() {
|
| - if (use_fake_tokens_for_testing_)
|
| - return new PolicyOAuth2TokenFetcherFake();
|
| - return new PolicyOAuth2TokenFetcherImpl();
|
| -}
|
| -
|
| -PolicyOAuth2TokenFetcher::PolicyOAuth2TokenFetcher() {}
|
| -
|
| -PolicyOAuth2TokenFetcher::~PolicyOAuth2TokenFetcher() {}
|
| -
|
| } // namespace policy
|
|
|