Chromium Code Reviews| Index: chrome/browser/chromeos/login/oauth2_token_fetcher.cc |
| diff --git a/chrome/browser/chromeos/login/oauth2_token_fetcher.cc b/chrome/browser/chromeos/login/oauth2_token_fetcher.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a6f3318305f8b6eaae76d62452ebf4e12dad9264 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/login/oauth2_token_fetcher.cc |
| @@ -0,0 +1,118 @@ |
| +// Copyright (c) 2013 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/login/oauth2_token_fetcher.h" |
| + |
| +#include "base/logging.h" |
| +#include "chrome/browser/chromeos/cros/cros_library.h" |
| +#include "chrome/browser/chromeos/cros/network_library.h" |
| +#include "chrome/browser/profiles/profile.h" |
|
Joao da Silva
2013/01/11 16:45:07
Not needed
zel
2013/01/11 19:51:16
Done.
|
| +#include "content/public/browser/browser_thread.h" |
| +#include "google_apis/gaia/gaia_constants.h" |
| +#include "google_apis/gaia/google_service_auth_error.h" |
| + |
| +using content::BrowserThread; |
| + |
| +namespace { |
| + |
| +// OAuth token request max retry count. |
| +const int kMaxRequestAttemptCount = 5; |
| +// OAuth token request retry delay in milliseconds. |
| +const int kRequestRestartDelay = 3000; |
| + |
| +// The service scope of the OAuth v2 token that ChromeOS login will be |
| +// requesting. |
| +const char kServiceScopeChromeOS[] = |
| + "https://www.googleapis.com/auth/chromesync"; |
| + |
| +} // namespace |
| + |
| +namespace chromeos { |
| + |
| +OAuth2TokenFetcher::OAuth2TokenFetcher( |
| + OAuth2TokenFetcher::Delegate* delegate, |
| + net::URLRequestContextGetter* context_getter) |
| + : delegate_(delegate), |
| + auth_fetcher_(this, GaiaConstants::kChromeSource, context_getter), |
| + retry_count_(0) { |
| + DCHECK(delegate); |
| +} |
| + |
| +OAuth2TokenFetcher::~OAuth2TokenFetcher() { |
| +} |
| + |
| +void OAuth2TokenFetcher::Start() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + if (CrosLibrary::Get()->libcros_loaded()) { |
| + // Delay the verification if the network is not connected or on a captive |
| + // portal. |
| + const Network* network = |
| + CrosLibrary::Get()->GetNetworkLibrary()->active_network(); |
| + if (!network || !network->connected() || network->restricted_pool()) { |
| + // If network is offline, defer the token fetching until online. |
| + VLOG(1) << "Network is offline. Deferring OAuth1 token fetch."; |
| + BrowserThread::PostDelayedTask( |
| + BrowserThread::UI, FROM_HERE, |
| + base::Bind(&OAuth2TokenFetcher::Start, AsWeakPtr()), |
| + base::TimeDelta::FromMilliseconds(kRequestRestartDelay)); |
| + return; |
| + } |
| + } |
|
Joao da Silva
2013/01/11 16:45:07
This block is repeated in a couple of files. Could
|
| + auth_fetcher_.StartCookieForOAuthLoginTokenExchange("1"); |
| +} |
| + |
| +void OAuth2TokenFetcher::StartOAuthLogin() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + auth_fetcher_.StartOAuthLogin( |
| + oauth_tokens_.access_token, GaiaConstants::kLSOService); |
| +} |
| + |
| +void OAuth2TokenFetcher::OnClientOAuthSuccess( |
| + const GaiaAuthConsumer::ClientOAuthResult& oauth_tokens) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + LOG(WARNING) << "Got OAuth2 tokens!"; |
|
Joao da Silva
2013/01/11 16:45:07
vlog?
zel
2013/01/11 19:51:16
Intentionally left as WARNING so we can get it in
|
| + retry_count_ = 0; |
| + oauth_tokens_ = oauth_tokens; |
| + StartOAuthLogin(); |
| +} |
| + |
| +void OAuth2TokenFetcher::OnClientOAuthFailure( |
| + const GoogleServiceAuthError& error) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + RetryOnError(error, |
| + base::Bind(&OAuth2TokenFetcher::Start, AsWeakPtr())); |
| +} |
| + |
| +void OAuth2TokenFetcher::OnClientLoginSuccess( |
| + const GaiaAuthConsumer::ClientLoginResult& credentials) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + delegate_->OnOAuth2TokenAvailable(credentials, oauth_tokens_); |
| +} |
| + |
| +void OAuth2TokenFetcher::OnClientLoginFailure( |
| + const GoogleServiceAuthError& error) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + LOG(WARNING) << "OAuthLogin failed, error: " << error.ToString(); |
| + RetryOnError(error, |
| + base::Bind(&OAuth2TokenFetcher::StartOAuthLogin, AsWeakPtr())); |
| +} |
| + |
| +void OAuth2TokenFetcher::RetryOnError(const GoogleServiceAuthError& error, |
| + const base::Closure& task) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + if ((error.state() == GoogleServiceAuthError::CONNECTION_FAILED || |
| + error.state() == GoogleServiceAuthError::SERVICE_UNAVAILABLE || |
| + error.state() == GoogleServiceAuthError::REQUEST_CANCELED) && |
| + retry_count_ < kMaxRequestAttemptCount) { |
| + retry_count_++; |
| + BrowserThread::PostDelayedTask( |
| + BrowserThread::UI, FROM_HERE, task, |
| + base::TimeDelta::FromMilliseconds(kRequestRestartDelay)); |
| + return; |
| + } |
| + LOG(WARNING) << "Unrecoverable error or retry count max reached."; |
| + delegate_->OnOAuth2TokenFetchFailed(); |
| +} |
| + |
| +} // namespace chromeos |