| 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..82c52a65f70d7c0dbc05a512e530feee6323351a
|
| --- /dev/null
|
| +++ b/chrome/browser/chromeos/login/oauth2_token_fetcher.cc
|
| @@ -0,0 +1,119 @@
|
| +// 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"
|
| +#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,
|
| + Profile* auth_profile)
|
| + : delegate_(delegate),
|
| + auth_profile_(auth_profile),
|
| + auth_fetcher_(this, GaiaConstants::kChromeSource,
|
| + auth_profile_->GetRequestContext()),
|
| + 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;
|
| + }
|
| + }
|
| + 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!";
|
| + 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
|
|
|