Revert of Use new people.get api instead of oauth2/v1/userinfo. (https://codereview...
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / policy_oauth2_token_fetcher.cc
blobabbab93c4acaeac85f00773ba641c2aecbf35934
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/policy/policy_oauth2_token_fetcher.h"
7 #include <vector>
9 #include "base/bind.h"
10 #include "base/logging.h"
11 #include "base/strings/string_util.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "google_apis/gaia/gaia_auth_fetcher.h"
14 #include "google_apis/gaia/gaia_constants.h"
15 #include "google_apis/gaia/gaia_urls.h"
16 #include "google_apis/gaia/google_service_auth_error.h"
17 #include "google_apis/gaia/oauth2_access_token_fetcher_impl.h"
18 #include "net/url_request/url_request_context_getter.h"
20 using content::BrowserThread;
22 namespace policy {
24 namespace {
26 // Max retry count for token fetching requests.
27 const int kMaxRequestAttemptCount = 5;
29 // OAuth token request retry delay in milliseconds.
30 const int kRequestRestartDelay = 3000;
32 } // namespace
34 PolicyOAuth2TokenFetcher::PolicyOAuth2TokenFetcher(
35 net::URLRequestContextGetter* auth_context_getter,
36 net::URLRequestContextGetter* system_context_getter,
37 const TokenCallback& callback)
38 : auth_context_getter_(auth_context_getter),
39 system_context_getter_(system_context_getter),
40 retry_count_(0),
41 failed_(false),
42 callback_(callback) {}
44 PolicyOAuth2TokenFetcher::~PolicyOAuth2TokenFetcher() {}
46 void PolicyOAuth2TokenFetcher::Start() {
47 retry_count_ = 0;
48 StartFetchingRefreshToken();
51 void PolicyOAuth2TokenFetcher::StartFetchingRefreshToken() {
52 refresh_token_fetcher_.reset(new GaiaAuthFetcher(
53 this, GaiaConstants::kChromeSource, auth_context_getter_.get()));
54 refresh_token_fetcher_->StartCookieForOAuthLoginTokenExchange(std::string());
57 void PolicyOAuth2TokenFetcher::StartFetchingAccessToken() {
58 std::vector<std::string> scopes;
59 scopes.push_back(GaiaConstants::kDeviceManagementServiceOAuth);
60 scopes.push_back(GaiaConstants::kOAuthWrapBridgeUserInfoScope);
61 access_token_fetcher_.reset(
62 new OAuth2AccessTokenFetcherImpl(this,
63 system_context_getter_.get(),
64 oauth2_refresh_token_));
65 access_token_fetcher_->Start(
66 GaiaUrls::GetInstance()->oauth2_chrome_client_id(),
67 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(),
68 scopes);
71 void PolicyOAuth2TokenFetcher::OnClientOAuthSuccess(
72 const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens) {
73 VLOG(1) << "OAuth2 tokens for policy fetching succeeded.";
74 oauth2_tokens_ = oauth2_tokens;
75 oauth2_refresh_token_ = oauth2_tokens.refresh_token;
76 retry_count_ = 0;
77 StartFetchingAccessToken();
80 void PolicyOAuth2TokenFetcher::OnClientOAuthFailure(
81 const GoogleServiceAuthError& error) {
82 VLOG(1) << "OAuth2 tokens fetch for policy fetch failed!";
83 RetryOnError(error,
84 base::Bind(&PolicyOAuth2TokenFetcher::StartFetchingRefreshToken,
85 AsWeakPtr()));
88 void PolicyOAuth2TokenFetcher::OnGetTokenSuccess(
89 const std::string& access_token,
90 const base::Time& expiration_time) {
91 VLOG(1) << "OAuth2 access token (device management) fetching succeeded.";
92 oauth2_access_token_ = access_token;
93 ForwardPolicyToken(access_token,
94 GoogleServiceAuthError(GoogleServiceAuthError::NONE));
97 void PolicyOAuth2TokenFetcher::OnGetTokenFailure(
98 const GoogleServiceAuthError& error) {
99 LOG(ERROR) << "OAuth2 access token (device management) fetching failed!";
100 RetryOnError(error,
101 base::Bind(&PolicyOAuth2TokenFetcher::StartFetchingAccessToken,
102 AsWeakPtr()));
105 void PolicyOAuth2TokenFetcher::RetryOnError(const GoogleServiceAuthError& error,
106 const base::Closure& task) {
107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
108 if ((error.state() == GoogleServiceAuthError::CONNECTION_FAILED ||
109 error.state() == GoogleServiceAuthError::SERVICE_UNAVAILABLE ||
110 error.state() == GoogleServiceAuthError::REQUEST_CANCELED) &&
111 retry_count_ < kMaxRequestAttemptCount) {
112 retry_count_++;
113 BrowserThread::PostDelayedTask(
114 BrowserThread::UI, FROM_HERE, task,
115 base::TimeDelta::FromMilliseconds(kRequestRestartDelay));
116 return;
118 LOG(ERROR) << "Unrecoverable error or retry count max reached.";
119 failed_ = true;
120 // Invoking the |callback_| signals to the owner of this object that it has
121 // completed, and the owner may delete this object on the callback method.
122 // So don't rely on |this| still being valid after ForwardPolicyToken()
123 // returns i.e. don't write to |failed_| or other fields.
124 ForwardPolicyToken(std::string(), error);
127 void PolicyOAuth2TokenFetcher::ForwardPolicyToken(
128 const std::string& token,
129 const GoogleServiceAuthError& error) {
130 if (!callback_.is_null())
131 callback_.Run(token, error);
134 } // namespace policy