Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(127)

Side by Side Diff: chrome/browser/chromeos/login/oauth2_token_fetcher.cc

Issue 11649055: OAuth2 sign-in flow for ChromeOS (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 "chrome/browser/chromeos/cros/cros_library.h"
9 #include "chrome/browser/chromeos/cros/network_library.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "google_apis/gaia/gaia_constants.h"
12 #include "google_apis/gaia/google_service_auth_error.h"
13
14 using content::BrowserThread;
15
16 namespace {
17
18 // OAuth token request max retry count.
19 const int kMaxRequestAttemptCount = 5;
20 // OAuth token request retry delay in milliseconds.
21 const int kRequestRestartDelay = 3000;
22
23 // The service scope of the OAuth v2 token that ChromeOS login will be
24 // requesting.
25 const char kServiceScopeChromeOS[] =
26 "https://www.googleapis.com/auth/chromesync";
27
28 } // namespace
29
30 namespace chromeos {
31
32 OAuth2TokenFetcher::OAuth2TokenFetcher(
33 OAuth2TokenFetcher::Delegate* delegate,
34 net::URLRequestContextGetter* context_getter)
35 : delegate_(delegate),
36 auth_fetcher_(this, GaiaConstants::kChromeSource, context_getter),
37 retry_count_(0) {
38 DCHECK(delegate);
39 }
40
41 OAuth2TokenFetcher::~OAuth2TokenFetcher() {
42 }
43
44 void OAuth2TokenFetcher::Start() {
45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
46 if (CrosLibrary::Get()->libcros_loaded()) {
47 // Delay the verification if the network is not connected or on a captive
48 // portal.
49 const Network* network =
50 CrosLibrary::Get()->GetNetworkLibrary()->active_network();
51 if (!network || !network->connected() || network->restricted_pool()) {
52 // If network is offline, defer the token fetching until online.
53 VLOG(1) << "Network is offline. Deferring OAuth1 token fetch.";
54 BrowserThread::PostDelayedTask(
55 BrowserThread::UI, FROM_HERE,
56 base::Bind(&OAuth2TokenFetcher::Start, AsWeakPtr()),
57 base::TimeDelta::FromMilliseconds(kRequestRestartDelay));
58 return;
59 }
60 }
61 auth_fetcher_.StartCookieForOAuthLoginTokenExchange("1");
62 }
63
64 void OAuth2TokenFetcher::StartOAuthLogin() {
65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
66 auth_fetcher_.StartOAuthLogin(
67 oauth_tokens_.access_token, GaiaConstants::kLSOService);
68 }
69
70 void OAuth2TokenFetcher::OnClientOAuthSuccess(
71 const GaiaAuthConsumer::ClientOAuthResult& oauth_tokens) {
72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
73 LOG(WARNING) << "Got OAuth2 tokens!";
74 retry_count_ = 0;
75 oauth_tokens_ = oauth_tokens;
76 StartOAuthLogin();
77 }
78
79 void OAuth2TokenFetcher::OnClientOAuthFailure(
80 const GoogleServiceAuthError& error) {
81 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
82 RetryOnError(error,
83 base::Bind(&OAuth2TokenFetcher::Start, AsWeakPtr()));
84 }
85
86 void OAuth2TokenFetcher::OnClientLoginSuccess(
87 const GaiaAuthConsumer::ClientLoginResult& credentials) {
88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
89 delegate_->OnOAuth2TokenAvailable(credentials, oauth_tokens_);
90 }
91
92 void OAuth2TokenFetcher::OnClientLoginFailure(
93 const GoogleServiceAuthError& error) {
94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
95 LOG(WARNING) << "OAuthLogin failed, error: " << error.ToString();
96 RetryOnError(error,
97 base::Bind(&OAuth2TokenFetcher::StartOAuthLogin, AsWeakPtr()));
98 }
99
100 void OAuth2TokenFetcher::RetryOnError(const GoogleServiceAuthError& error,
101 const base::Closure& task) {
102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
103 if ((error.state() == GoogleServiceAuthError::CONNECTION_FAILED ||
104 error.state() == GoogleServiceAuthError::SERVICE_UNAVAILABLE ||
105 error.state() == GoogleServiceAuthError::REQUEST_CANCELED) &&
106 retry_count_ < kMaxRequestAttemptCount) {
107 retry_count_++;
108 BrowserThread::PostDelayedTask(
109 BrowserThread::UI, FROM_HERE, task,
110 base::TimeDelta::FromMilliseconds(kRequestRestartDelay));
111 return;
112 }
113 LOG(WARNING) << "Unrecoverable error or retry count max reached.";
114 delegate_->OnOAuth2TokenFetchFailed();
115 }
116
117 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698