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/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "chromeos/network/network_handler.h" | 9 #include "chromeos/network/network_handler.h" |
10 #include "chromeos/network/network_state.h" | 10 #include "chromeos/network/network_state.h" |
(...skipping 21 matching lines...) Expand all Loading... |
32 net::URLRequestContextGetter* context_getter) | 32 net::URLRequestContextGetter* context_getter) |
33 : delegate_(delegate), | 33 : delegate_(delegate), |
34 auth_fetcher_(this, GaiaConstants::kChromeSource, context_getter), | 34 auth_fetcher_(this, GaiaConstants::kChromeSource, context_getter), |
35 retry_count_(0) { | 35 retry_count_(0) { |
36 DCHECK(delegate); | 36 DCHECK(delegate); |
37 } | 37 } |
38 | 38 |
39 OAuth2TokenFetcher::~OAuth2TokenFetcher() { | 39 OAuth2TokenFetcher::~OAuth2TokenFetcher() { |
40 } | 40 } |
41 | 41 |
42 void OAuth2TokenFetcher::StartExchangeFromCookies() { | 42 void OAuth2TokenFetcher::StartExchangeFromCookies( |
| 43 const std::string& session_index) { |
43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
44 | 45 session_index_ = session_index; |
45 // Delay the verification if the network is not connected or on a captive | 46 // Delay the verification if the network is not connected or on a captive |
46 // portal. | 47 // portal. |
47 const NetworkState* default_network = | 48 const NetworkState* default_network = |
48 NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); | 49 NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); |
49 if (!default_network || | 50 if (!default_network || |
50 default_network->connection_state() == shill::kStatePortal) { | 51 default_network->connection_state() == shill::kStatePortal) { |
51 // If network is offline, defer the token fetching until online. | 52 // If network is offline, defer the token fetching until online. |
52 VLOG(1) << "Network is offline. Deferring OAuth2 token fetch."; | 53 VLOG(1) << "Network is offline. Deferring OAuth2 token fetch."; |
53 BrowserThread::PostDelayedTask( | 54 BrowserThread::PostDelayedTask( |
54 BrowserThread::UI, FROM_HERE, | 55 BrowserThread::UI, |
| 56 FROM_HERE, |
55 base::Bind(&OAuth2TokenFetcher::StartExchangeFromCookies, | 57 base::Bind(&OAuth2TokenFetcher::StartExchangeFromCookies, |
56 AsWeakPtr()), | 58 AsWeakPtr(), |
| 59 session_index), |
57 base::TimeDelta::FromMilliseconds(kRequestRestartDelay)); | 60 base::TimeDelta::FromMilliseconds(kRequestRestartDelay)); |
58 return; | 61 return; |
59 } | 62 } |
60 auth_fetcher_.StartCookieForOAuthLoginTokenExchange(std::string()); | 63 auth_fetcher_.StartCookieForOAuthLoginTokenExchange(session_index); |
61 } | 64 } |
62 | 65 |
63 void OAuth2TokenFetcher::StartExchangeFromAuthCode( | 66 void OAuth2TokenFetcher::StartExchangeFromAuthCode( |
64 const std::string& auth_code) { | 67 const std::string& auth_code) { |
65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
66 auth_code_ = auth_code; | 69 auth_code_ = auth_code; |
67 // Delay the verification if the network is not connected or on a captive | 70 // Delay the verification if the network is not connected or on a captive |
68 // portal. | 71 // portal. |
69 const NetworkState* default_network = | 72 const NetworkState* default_network = |
70 NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); | 73 NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); |
(...skipping 17 matching lines...) Expand all Loading... |
88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
89 VLOG(1) << "Got OAuth2 tokens!"; | 92 VLOG(1) << "Got OAuth2 tokens!"; |
90 retry_count_ = 0; | 93 retry_count_ = 0; |
91 oauth_tokens_ = oauth_tokens; | 94 oauth_tokens_ = oauth_tokens; |
92 delegate_->OnOAuth2TokensAvailable(oauth_tokens_); | 95 delegate_->OnOAuth2TokensAvailable(oauth_tokens_); |
93 } | 96 } |
94 | 97 |
95 void OAuth2TokenFetcher::OnClientOAuthFailure( | 98 void OAuth2TokenFetcher::OnClientOAuthFailure( |
96 const GoogleServiceAuthError& error) { | 99 const GoogleServiceAuthError& error) { |
97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
98 RetryOnError( | 101 RetryOnError(error, |
99 error, | 102 auth_code_.empty() |
100 auth_code_.empty() ? | 103 ? base::Bind(&OAuth2TokenFetcher::StartExchangeFromCookies, |
101 base::Bind(&OAuth2TokenFetcher::StartExchangeFromCookies, | 104 AsWeakPtr(), |
102 AsWeakPtr()) : | 105 session_index_) |
103 base::Bind(&OAuth2TokenFetcher::StartExchangeFromAuthCode, | 106 : base::Bind(&OAuth2TokenFetcher::StartExchangeFromAuthCode, |
104 AsWeakPtr(), auth_code_), | 107 AsWeakPtr(), |
105 base::Bind(&Delegate::OnOAuth2TokensFetchFailed, | 108 auth_code_), |
106 base::Unretained(delegate_))); | 109 base::Bind(&Delegate::OnOAuth2TokensFetchFailed, |
| 110 base::Unretained(delegate_))); |
107 } | 111 } |
108 | 112 |
109 void OAuth2TokenFetcher::RetryOnError(const GoogleServiceAuthError& error, | 113 void OAuth2TokenFetcher::RetryOnError(const GoogleServiceAuthError& error, |
110 const base::Closure& task, | 114 const base::Closure& task, |
111 const base::Closure& error_handler) { | 115 const base::Closure& error_handler) { |
112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
113 if ((error.state() == GoogleServiceAuthError::CONNECTION_FAILED || | 117 if ((error.state() == GoogleServiceAuthError::CONNECTION_FAILED || |
114 error.state() == GoogleServiceAuthError::SERVICE_UNAVAILABLE || | 118 error.state() == GoogleServiceAuthError::SERVICE_UNAVAILABLE || |
115 error.state() == GoogleServiceAuthError::REQUEST_CANCELED) && | 119 error.state() == GoogleServiceAuthError::REQUEST_CANCELED) && |
116 retry_count_ < kMaxRequestAttemptCount) { | 120 retry_count_ < kMaxRequestAttemptCount) { |
117 retry_count_++; | 121 retry_count_++; |
118 BrowserThread::PostDelayedTask( | 122 BrowserThread::PostDelayedTask( |
119 BrowserThread::UI, FROM_HERE, task, | 123 BrowserThread::UI, FROM_HERE, task, |
120 base::TimeDelta::FromMilliseconds(kRequestRestartDelay)); | 124 base::TimeDelta::FromMilliseconds(kRequestRestartDelay)); |
121 return; | 125 return; |
122 } | 126 } |
123 LOG(ERROR) << "Unrecoverable error or retry count max reached. State: " | 127 LOG(ERROR) << "Unrecoverable error or retry count max reached. State: " |
124 << error.state() << ", network error: " << error.network_error() | 128 << error.state() << ", network error: " << error.network_error() |
125 << ", message: " << error.error_message(); | 129 << ", message: " << error.error_message(); |
126 error_handler.Run(); | 130 error_handler.Run(); |
127 } | 131 } |
128 | 132 |
129 } // namespace chromeos | 133 } // namespace chromeos |
OLD | NEW |