Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chrome / browser / chromeos / login / signin / oauth2_token_fetcher.cc
blobc2ff8b393abe023c78ed631fb5069a7bce25b6be
1 // Copyright 2014 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/login/signin/oauth2_token_fetcher.h"
7 #include "base/logging.h"
8 #include "base/strings/string_util.h"
9 #include "chromeos/network/network_handler.h"
10 #include "chromeos/network/network_state.h"
11 #include "chromeos/network/network_state_handler.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "google_apis/gaia/gaia_constants.h"
14 #include "google_apis/gaia/google_service_auth_error.h"
15 #include "third_party/cros_system_api/dbus/service_constants.h"
17 using content::BrowserThread;
19 namespace {
21 // OAuth token request max retry count.
22 const int kMaxRequestAttemptCount = 5;
23 // OAuth token request retry delay in milliseconds.
24 const int kRequestRestartDelay = 3000;
26 } // namespace
28 namespace chromeos {
30 OAuth2TokenFetcher::OAuth2TokenFetcher(
31 OAuth2TokenFetcher::Delegate* delegate,
32 net::URLRequestContextGetter* context_getter)
33 : delegate_(delegate),
34 auth_fetcher_(this, GaiaConstants::kChromeSource, context_getter),
35 retry_count_(0) {
36 DCHECK(delegate);
39 OAuth2TokenFetcher::~OAuth2TokenFetcher() {
42 void OAuth2TokenFetcher::StartExchangeFromCookies(
43 const std::string& session_index,
44 const std::string& signin_scoped_device_id) {
45 DCHECK_CURRENTLY_ON(BrowserThread::UI);
46 session_index_ = session_index;
47 signin_scoped_device_id_ = signin_scoped_device_id;
48 // Delay the verification if the network is not connected or on a captive
49 // portal.
50 const NetworkState* default_network =
51 NetworkHandler::Get()->network_state_handler()->DefaultNetwork();
52 if (!default_network || default_network->is_captive_portal()) {
53 // If network is offline, defer the token fetching until online.
54 VLOG(1) << "Network is offline. Deferring OAuth2 token fetch.";
55 BrowserThread::PostDelayedTask(
56 BrowserThread::UI,
57 FROM_HERE,
58 base::Bind(&OAuth2TokenFetcher::StartExchangeFromCookies,
59 AsWeakPtr(),
60 session_index,
61 signin_scoped_device_id),
62 base::TimeDelta::FromMilliseconds(kRequestRestartDelay));
63 return;
65 auth_fetcher_.StartCookieForOAuthLoginTokenExchangeWithDeviceId(
66 session_index, signin_scoped_device_id);
69 void OAuth2TokenFetcher::StartExchangeFromAuthCode(
70 const std::string& auth_code,
71 const std::string& signin_scoped_device_id) {
72 DCHECK_CURRENTLY_ON(BrowserThread::UI);
73 auth_code_ = auth_code;
74 signin_scoped_device_id_ = signin_scoped_device_id;
75 auth_fetcher_.StartAuthCodeForOAuth2TokenExchangeWithDeviceId(
76 auth_code, signin_scoped_device_id);
79 void OAuth2TokenFetcher::OnClientOAuthSuccess(
80 const GaiaAuthConsumer::ClientOAuthResult& oauth_tokens) {
81 DCHECK_CURRENTLY_ON(BrowserThread::UI);
82 VLOG(1) << "Got OAuth2 tokens!";
83 retry_count_ = 0;
84 oauth_tokens_ = oauth_tokens;
85 delegate_->OnOAuth2TokensAvailable(oauth_tokens_);
88 void OAuth2TokenFetcher::OnClientOAuthFailure(
89 const GoogleServiceAuthError& error) {
90 DCHECK_CURRENTLY_ON(BrowserThread::UI);
91 RetryOnError(
92 error,
93 auth_code_.empty()
94 ? base::Bind(&OAuth2TokenFetcher::StartExchangeFromCookies,
95 AsWeakPtr(), session_index_, signin_scoped_device_id_)
96 : base::Bind(&OAuth2TokenFetcher::StartExchangeFromAuthCode,
97 AsWeakPtr(), auth_code_, signin_scoped_device_id_),
98 base::Bind(&Delegate::OnOAuth2TokensFetchFailed,
99 base::Unretained(delegate_)));
102 void OAuth2TokenFetcher::RetryOnError(const GoogleServiceAuthError& error,
103 const base::Closure& task,
104 const base::Closure& error_handler) {
105 DCHECK_CURRENTLY_ON(BrowserThread::UI);
106 if (error.IsTransientError() && retry_count_ < kMaxRequestAttemptCount) {
107 retry_count_++;
108 BrowserThread::PostDelayedTask(
109 BrowserThread::UI, FROM_HERE, task,
110 base::TimeDelta::FromMilliseconds(kRequestRestartDelay));
111 return;
113 LOG(ERROR) << "Unrecoverable error or retry count max reached. State: "
114 << error.state() << ", network error: " << error.network_error()
115 << ", message: " << error.error_message();
116 error_handler.Run();
119 } // namespace chromeos