| OLD | NEW |
| (Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/chromeos/login/oauth2_token_fetcher.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/string_util.h" |
| 9 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 10 #include "chrome/browser/chromeos/cros/network_library.h" |
| 11 #include "content/public/browser/browser_thread.h" |
| 12 #include "google_apis/gaia/gaia_constants.h" |
| 13 #include "google_apis/gaia/google_service_auth_error.h" |
| 14 |
| 15 using content::BrowserThread; |
| 16 |
| 17 namespace { |
| 18 |
| 19 // OAuth token request max retry count. |
| 20 const int kMaxRequestAttemptCount = 5; |
| 21 // OAuth token request retry delay in milliseconds. |
| 22 const int kRequestRestartDelay = 3000; |
| 23 |
| 24 // The service scope of the OAuth v2 token that ChromeOS login will be |
| 25 // requesting. |
| 26 const char kServiceScopeChromeOS[] = |
| 27 "https://www.googleapis.com/auth/chromesync"; |
| 28 |
| 29 } // namespace |
| 30 |
| 31 namespace chromeos { |
| 32 |
| 33 OAuth2TokenFetcher::OAuth2TokenFetcher( |
| 34 OAuth2TokenFetcher::Delegate* delegate, |
| 35 net::URLRequestContextGetter* context_getter) |
| 36 : delegate_(delegate), |
| 37 auth_fetcher_(this, GaiaConstants::kChromeSource, context_getter), |
| 38 retry_count_(0) { |
| 39 DCHECK(delegate); |
| 40 } |
| 41 |
| 42 OAuth2TokenFetcher::~OAuth2TokenFetcher() { |
| 43 } |
| 44 |
| 45 void OAuth2TokenFetcher::Start() { |
| 46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 47 if (CrosLibrary::Get()->libcros_loaded()) { |
| 48 // Delay the verification if the network is not connected or on a captive |
| 49 // portal. |
| 50 const Network* network = |
| 51 CrosLibrary::Get()->GetNetworkLibrary()->active_network(); |
| 52 if (!network || !network->connected() || network->restricted_pool()) { |
| 53 // If network is offline, defer the token fetching until online. |
| 54 VLOG(1) << "Network is offline. Deferring OAuth1 token fetch."; |
| 55 BrowserThread::PostDelayedTask( |
| 56 BrowserThread::UI, FROM_HERE, |
| 57 base::Bind(&OAuth2TokenFetcher::Start, AsWeakPtr()), |
| 58 base::TimeDelta::FromMilliseconds(kRequestRestartDelay)); |
| 59 return; |
| 60 } |
| 61 } |
| 62 auth_fetcher_.StartCookieForOAuthLoginTokenExchange(EmptyString()); |
| 63 } |
| 64 |
| 65 void OAuth2TokenFetcher::OnClientOAuthSuccess( |
| 66 const GaiaAuthConsumer::ClientOAuthResult& oauth_tokens) { |
| 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 68 LOG(INFO) << "Got OAuth2 tokens!"; |
| 69 retry_count_ = 0; |
| 70 oauth_tokens_ = oauth_tokens; |
| 71 delegate_->OnOAuth2TokensAvailable(oauth_tokens_); |
| 72 } |
| 73 |
| 74 void OAuth2TokenFetcher::OnClientOAuthFailure( |
| 75 const GoogleServiceAuthError& error) { |
| 76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 77 RetryOnError(error, |
| 78 base::Bind(&OAuth2TokenFetcher::Start, AsWeakPtr()), |
| 79 base::Bind(&Delegate::OnOAuth2TokensFetchFailed, |
| 80 base::Unretained(delegate_))); |
| 81 } |
| 82 |
| 83 void OAuth2TokenFetcher::RetryOnError(const GoogleServiceAuthError& error, |
| 84 const base::Closure& task, |
| 85 const base::Closure& error_handler) { |
| 86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 87 if ((error.state() == GoogleServiceAuthError::CONNECTION_FAILED || |
| 88 error.state() == GoogleServiceAuthError::SERVICE_UNAVAILABLE || |
| 89 error.state() == GoogleServiceAuthError::REQUEST_CANCELED) && |
| 90 retry_count_ < kMaxRequestAttemptCount) { |
| 91 retry_count_++; |
| 92 BrowserThread::PostDelayedTask( |
| 93 BrowserThread::UI, FROM_HERE, task, |
| 94 base::TimeDelta::FromMilliseconds(kRequestRestartDelay)); |
| 95 return; |
| 96 } |
| 97 LOG(INFO) << "Unrecoverable error or retry count max reached."; |
| 98 error_handler.Run(); |
| 99 } |
| 100 |
| 101 } // namespace chromeos |
| OLD | NEW |