OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/login/oauth2_token_fetcher.h" | 5 #include "chrome/browser/chromeos/login/oauth2_token_fetcher.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "chrome/browser/chromeos/cros/cros_library.h" | 9 #include "chrome/browser/chromeos/cros/cros_library.h" |
10 #include "chrome/browser/chromeos/net/connectivity_state_helper.h" | 10 #include "chrome/browser/chromeos/net/connectivity_state_helper.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 net::URLRequestContextGetter* context_getter) | 35 net::URLRequestContextGetter* context_getter) |
36 : delegate_(delegate), | 36 : delegate_(delegate), |
37 auth_fetcher_(this, GaiaConstants::kChromeSource, context_getter), | 37 auth_fetcher_(this, GaiaConstants::kChromeSource, context_getter), |
38 retry_count_(0) { | 38 retry_count_(0) { |
39 DCHECK(delegate); | 39 DCHECK(delegate); |
40 } | 40 } |
41 | 41 |
42 OAuth2TokenFetcher::~OAuth2TokenFetcher() { | 42 OAuth2TokenFetcher::~OAuth2TokenFetcher() { |
43 } | 43 } |
44 | 44 |
45 void OAuth2TokenFetcher::Start() { | 45 void OAuth2TokenFetcher::StartExchangeFromCookies() { |
46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
47 | 47 |
48 // Delay the verification if the network is not connected or on a captive | 48 // Delay the verification if the network is not connected or on a captive |
49 // portal. | 49 // portal. |
50 ConnectivityStateHelper* csh = ConnectivityStateHelper::Get(); | 50 ConnectivityStateHelper* csh = ConnectivityStateHelper::Get(); |
51 if (!csh->DefaultNetworkOnline()) { | 51 if (!csh->DefaultNetworkOnline()) { |
52 // If network is offline, defer the token fetching until online. | 52 // If network is offline, defer the token fetching until online. |
53 VLOG(1) << "Network is offline. Deferring OAuth2 token fetch."; | 53 VLOG(1) << "Network is offline. Deferring OAuth2 token fetch."; |
54 BrowserThread::PostDelayedTask( | 54 BrowserThread::PostDelayedTask( |
55 BrowserThread::UI, FROM_HERE, | 55 BrowserThread::UI, FROM_HERE, |
56 base::Bind(&OAuth2TokenFetcher::Start, AsWeakPtr()), | 56 base::Bind(&OAuth2TokenFetcher::StartExchangeFromCookies, |
| 57 AsWeakPtr()), |
57 base::TimeDelta::FromMilliseconds(kRequestRestartDelay)); | 58 base::TimeDelta::FromMilliseconds(kRequestRestartDelay)); |
58 return; | 59 return; |
59 } | 60 } |
60 auth_fetcher_.StartCookieForOAuthLoginTokenExchange(EmptyString()); | 61 auth_fetcher_.StartCookieForOAuthLoginTokenExchange(EmptyString()); |
61 } | 62 } |
62 | 63 |
| 64 void OAuth2TokenFetcher::StartExchangeFromAuthCode( |
| 65 const std::string& auth_code) { |
| 66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 67 auth_code_ = auth_code; |
| 68 // Delay the verification if the network is not connected or on a captive |
| 69 // portal. |
| 70 ConnectivityStateHelper* csh = ConnectivityStateHelper::Get(); |
| 71 if (!csh->DefaultNetworkOnline()) { |
| 72 // If network is offline, defer the token fetching until online. |
| 73 VLOG(1) << "Network is offline. Deferring OAuth2 token fetch."; |
| 74 BrowserThread::PostDelayedTask( |
| 75 BrowserThread::UI, FROM_HERE, |
| 76 base::Bind(&OAuth2TokenFetcher::StartExchangeFromAuthCode, |
| 77 AsWeakPtr(), |
| 78 auth_code), |
| 79 base::TimeDelta::FromMilliseconds(kRequestRestartDelay)); |
| 80 return; |
| 81 } |
| 82 auth_fetcher_.StartAuthCodeForOAuth2TokenExchange(auth_code); |
| 83 } |
| 84 |
63 void OAuth2TokenFetcher::OnClientOAuthSuccess( | 85 void OAuth2TokenFetcher::OnClientOAuthSuccess( |
64 const GaiaAuthConsumer::ClientOAuthResult& oauth_tokens) { | 86 const GaiaAuthConsumer::ClientOAuthResult& oauth_tokens) { |
65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
66 LOG(INFO) << "Got OAuth2 tokens!"; | 88 LOG(INFO) << "Got OAuth2 tokens!"; |
67 retry_count_ = 0; | 89 retry_count_ = 0; |
68 oauth_tokens_ = oauth_tokens; | 90 oauth_tokens_ = oauth_tokens; |
69 delegate_->OnOAuth2TokensAvailable(oauth_tokens_); | 91 delegate_->OnOAuth2TokensAvailable(oauth_tokens_); |
70 } | 92 } |
71 | 93 |
72 void OAuth2TokenFetcher::OnClientOAuthFailure( | 94 void OAuth2TokenFetcher::OnClientOAuthFailure( |
73 const GoogleServiceAuthError& error) { | 95 const GoogleServiceAuthError& error) { |
74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
75 RetryOnError(error, | 97 RetryOnError( |
76 base::Bind(&OAuth2TokenFetcher::Start, AsWeakPtr()), | 98 error, |
77 base::Bind(&Delegate::OnOAuth2TokensFetchFailed, | 99 auth_code_.empty() ? |
78 base::Unretained(delegate_))); | 100 base::Bind(&OAuth2TokenFetcher::StartExchangeFromCookies, |
| 101 AsWeakPtr()) : |
| 102 base::Bind(&OAuth2TokenFetcher::StartExchangeFromAuthCode, |
| 103 AsWeakPtr(), auth_code_), |
| 104 base::Bind(&Delegate::OnOAuth2TokensFetchFailed, |
| 105 base::Unretained(delegate_))); |
79 } | 106 } |
80 | 107 |
81 void OAuth2TokenFetcher::RetryOnError(const GoogleServiceAuthError& error, | 108 void OAuth2TokenFetcher::RetryOnError(const GoogleServiceAuthError& error, |
82 const base::Closure& task, | 109 const base::Closure& task, |
83 const base::Closure& error_handler) { | 110 const base::Closure& error_handler) { |
84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
85 if ((error.state() == GoogleServiceAuthError::CONNECTION_FAILED || | 112 if ((error.state() == GoogleServiceAuthError::CONNECTION_FAILED || |
86 error.state() == GoogleServiceAuthError::SERVICE_UNAVAILABLE || | 113 error.state() == GoogleServiceAuthError::SERVICE_UNAVAILABLE || |
87 error.state() == GoogleServiceAuthError::REQUEST_CANCELED) && | 114 error.state() == GoogleServiceAuthError::REQUEST_CANCELED) && |
88 retry_count_ < kMaxRequestAttemptCount) { | 115 retry_count_ < kMaxRequestAttemptCount) { |
89 retry_count_++; | 116 retry_count_++; |
90 BrowserThread::PostDelayedTask( | 117 BrowserThread::PostDelayedTask( |
91 BrowserThread::UI, FROM_HERE, task, | 118 BrowserThread::UI, FROM_HERE, task, |
92 base::TimeDelta::FromMilliseconds(kRequestRestartDelay)); | 119 base::TimeDelta::FromMilliseconds(kRequestRestartDelay)); |
93 return; | 120 return; |
94 } | 121 } |
95 LOG(INFO) << "Unrecoverable error or retry count max reached."; | 122 LOG(INFO) << "Unrecoverable error or retry count max reached."; |
96 error_handler.Run(); | 123 error_handler.Run(); |
97 } | 124 } |
98 | 125 |
99 } // namespace chromeos | 126 } // namespace chromeos |
OLD | NEW |