Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 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 "chrome/browser/chromeos/cros/cros_library.h" | |
| 9 #include "chrome/browser/chromeos/cros/network_library.h" | |
| 10 #include "chrome/browser/profiles/profile.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(OAuth2TokenFetcher::Delegate* delegate, | |
| 34 Profile* auth_profile) | |
| 35 : delegate_(delegate), | |
| 36 auth_profile_(auth_profile), | |
| 37 auth_fetcher_(this, GaiaConstants::kChromeSource, | |
| 38 auth_profile_->GetRequestContext()), | |
| 39 retry_count_(0) { | |
| 40 DCHECK(delegate); | |
| 41 } | |
| 42 | |
| 43 OAuth2TokenFetcher::~OAuth2TokenFetcher() { | |
| 44 } | |
| 45 | |
| 46 void OAuth2TokenFetcher::Start() { | |
| 47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 48 if (CrosLibrary::Get()->libcros_loaded()) { | |
| 49 // Delay the verification if the network is not connected or on a captive | |
| 50 // portal. | |
| 51 const Network* network = | |
| 52 CrosLibrary::Get()->GetNetworkLibrary()->active_network(); | |
| 53 if (!network || !network->connected() || network->restricted_pool()) { | |
| 54 // If network is offline, defer the token fetching until online. | |
| 55 VLOG(1) << "Network is offline. Deferring OAuth1 token fetch."; | |
| 56 BrowserThread::PostDelayedTask( | |
| 57 BrowserThread::UI, FROM_HERE, | |
| 58 base::Bind(&OAuth2TokenFetcher::Start, AsWeakPtr()), | |
| 59 base::TimeDelta::FromMilliseconds(kRequestRestartDelay)); | |
| 60 return; | |
| 61 } | |
| 62 } | |
| 63 auth_fetcher_.StartCookieForOAuthLoginTokenExchange("1"); | |
|
xiyuan
2013/01/07 23:07:55
Is it proper to use a hard-coded "1" as session in
zel
2013/01/08 02:05:41
Yes, because the current session that came from GA
| |
| 64 } | |
| 65 | |
| 66 void OAuth2TokenFetcher::StartOAuthLogin() { | |
| 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 68 auth_fetcher_.StartOAuthLogin( | |
| 69 oauth_tokens_.access_token, GaiaConstants::kLSOService); | |
| 70 } | |
| 71 | |
| 72 void OAuth2TokenFetcher::OnClientOAuthSuccess( | |
| 73 const GaiaAuthConsumer::ClientOAuthResult& oauth_tokens) { | |
| 74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 75 LOG(WARNING) << "Got OAuth2 tokens!"; | |
| 76 retry_count_ = 0; | |
| 77 oauth_tokens_ = oauth_tokens; | |
| 78 StartOAuthLogin(); | |
| 79 } | |
| 80 | |
| 81 void OAuth2TokenFetcher::OnClientOAuthFailure( | |
| 82 const GoogleServiceAuthError& error) { | |
| 83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 84 RetryOnError(error, | |
| 85 base::Bind(&OAuth2TokenFetcher::Start, AsWeakPtr())); | |
| 86 } | |
| 87 | |
| 88 void OAuth2TokenFetcher::OnClientLoginSuccess( | |
| 89 const GaiaAuthConsumer::ClientLoginResult& credentials) { | |
| 90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 91 delegate_->OnOAuth2TokenAvailable(credentials, oauth_tokens_); | |
| 92 } | |
| 93 | |
| 94 void OAuth2TokenFetcher::OnClientLoginFailure( | |
| 95 const GoogleServiceAuthError& error) { | |
| 96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 97 LOG(WARNING) << "OAuthLogin failed, error: " << error.ToString(); | |
| 98 RetryOnError(error, | |
| 99 base::Bind(&OAuth2TokenFetcher::StartOAuthLogin, AsWeakPtr())); | |
| 100 } | |
| 101 | |
| 102 void OAuth2TokenFetcher::RetryOnError(const GoogleServiceAuthError& error, | |
| 103 const base::Closure& task) { | |
| 104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 105 if ((error.state() == GoogleServiceAuthError::CONNECTION_FAILED || | |
| 106 error.state() == GoogleServiceAuthError::SERVICE_UNAVAILABLE || | |
| 107 error.state() == GoogleServiceAuthError::REQUEST_CANCELED) && | |
| 108 retry_count_++ < kMaxRequestAttemptCount) { | |
|
xiyuan
2013/01/07 23:07:55
Get rid of '++' here and fix alignment.
zel
2013/01/08 02:05:41
Done.
| |
| 109 retry_count_++; | |
| 110 BrowserThread::PostDelayedTask( | |
| 111 BrowserThread::UI, FROM_HERE, task, | |
| 112 base::TimeDelta::FromMilliseconds(kRequestRestartDelay)); | |
| 113 return; | |
| 114 } | |
| 115 LOG(WARNING) << "Unrecoverable error or retry count max reached."; | |
| 116 delegate_->OnOAuth2TokenFetchFailed(); | |
| 117 } | |
| 118 | |
| 119 } // namespace chromeos | |
| OLD | NEW |