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

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: wired policy with OAuth2 path 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 "chrome/browser/profiles/profile.h"
Joao da Silva 2013/01/11 16:45:07 Not needed
zel 2013/01/11 19:51:16 Done.
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 }
Joao da Silva 2013/01/11 16:45:07 This block is repeated in a couple of files. Could
62 auth_fetcher_.StartCookieForOAuthLoginTokenExchange("1");
63 }
64
65 void OAuth2TokenFetcher::StartOAuthLogin() {
66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
67 auth_fetcher_.StartOAuthLogin(
68 oauth_tokens_.access_token, GaiaConstants::kLSOService);
69 }
70
71 void OAuth2TokenFetcher::OnClientOAuthSuccess(
72 const GaiaAuthConsumer::ClientOAuthResult& oauth_tokens) {
73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
74 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
75 retry_count_ = 0;
76 oauth_tokens_ = oauth_tokens;
77 StartOAuthLogin();
78 }
79
80 void OAuth2TokenFetcher::OnClientOAuthFailure(
81 const GoogleServiceAuthError& error) {
82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
83 RetryOnError(error,
84 base::Bind(&OAuth2TokenFetcher::Start, AsWeakPtr()));
85 }
86
87 void OAuth2TokenFetcher::OnClientLoginSuccess(
88 const GaiaAuthConsumer::ClientLoginResult& credentials) {
89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
90 delegate_->OnOAuth2TokenAvailable(credentials, oauth_tokens_);
91 }
92
93 void OAuth2TokenFetcher::OnClientLoginFailure(
94 const GoogleServiceAuthError& error) {
95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
96 LOG(WARNING) << "OAuthLogin failed, error: " << error.ToString();
97 RetryOnError(error,
98 base::Bind(&OAuth2TokenFetcher::StartOAuthLogin, AsWeakPtr()));
99 }
100
101 void OAuth2TokenFetcher::RetryOnError(const GoogleServiceAuthError& error,
102 const base::Closure& task) {
103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
104 if ((error.state() == GoogleServiceAuthError::CONNECTION_FAILED ||
105 error.state() == GoogleServiceAuthError::SERVICE_UNAVAILABLE ||
106 error.state() == GoogleServiceAuthError::REQUEST_CANCELED) &&
107 retry_count_ < kMaxRequestAttemptCount) {
108 retry_count_++;
109 BrowserThread::PostDelayedTask(
110 BrowserThread::UI, FROM_HERE, task,
111 base::TimeDelta::FromMilliseconds(kRequestRestartDelay));
112 return;
113 }
114 LOG(WARNING) << "Unrecoverable error or retry count max reached.";
115 delegate_->OnOAuth2TokenFetchFailed();
116 }
117
118 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698