OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "remoting/host/signaling_connector.h" | 5 #include "remoting/host/signaling_connector.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "google_apis/google_api_keys.h" | |
9 #include "net/url_request/url_fetcher.h" | 10 #include "net/url_request/url_fetcher.h" |
10 #include "net/url_request/url_request_context_getter.h" | 11 #include "net/url_request/url_request_context_getter.h" |
11 #include "remoting/host/dns_blackhole_checker.h" | 12 #include "remoting/host/dns_blackhole_checker.h" |
12 | 13 |
13 namespace remoting { | 14 namespace remoting { |
14 | 15 |
15 namespace { | 16 namespace { |
16 | 17 |
17 // The delay between reconnect attempts will increase exponentially up | 18 // The delay between reconnect attempts will increase exponentially up |
18 // to the maximum specified here. | 19 // to the maximum specified here. |
19 const int kMaxReconnectDelaySeconds = 10 * 60; | 20 const int kMaxReconnectDelaySeconds = 10 * 60; |
20 | 21 |
21 // Time when we we try to update OAuth token before its expiration. | 22 // Time when we we try to update OAuth token before its expiration. |
22 const int kTokenUpdateTimeBeforeExpirySeconds = 60; | 23 const int kTokenUpdateTimeBeforeExpirySeconds = 60; |
23 | 24 |
24 } // namespace | 25 } // namespace |
25 | 26 |
26 SignalingConnector::OAuthCredentials::OAuthCredentials( | 27 SignalingConnector::OAuthCredentials::OAuthCredentials( |
27 const std::string& login_value, | 28 const std::string& login_value, |
28 const std::string& refresh_token_value, | 29 const std::string& refresh_token_value) |
29 const OAuthClientInfo& client_info_value) | |
30 : login(login_value), | 30 : login(login_value), |
31 refresh_token(refresh_token_value), | 31 refresh_token(refresh_token_value) { |
32 client_info(client_info_value) { | |
33 } | 32 } |
34 | 33 |
35 SignalingConnector::SignalingConnector( | 34 SignalingConnector::SignalingConnector( |
36 XmppSignalStrategy* signal_strategy, | 35 XmppSignalStrategy* signal_strategy, |
37 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, | 36 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, |
38 scoped_ptr<DnsBlackholeChecker> dns_blackhole_checker, | 37 scoped_ptr<DnsBlackholeChecker> dns_blackhole_checker, |
39 const base::Closure& auth_failed_callback) | 38 const base::Closure& auth_failed_callback) |
40 : signal_strategy_(signal_strategy), | 39 : signal_strategy_(signal_strategy), |
41 url_request_context_getter_(url_request_context_getter), | 40 url_request_context_getter_(url_request_context_getter), |
42 auth_failed_callback_(auth_failed_callback), | 41 auth_failed_callback_(auth_failed_callback), |
(...skipping 10 matching lines...) Expand all Loading... | |
53 | 52 |
54 SignalingConnector::~SignalingConnector() { | 53 SignalingConnector::~SignalingConnector() { |
55 signal_strategy_->RemoveListener(this); | 54 signal_strategy_->RemoveListener(this); |
56 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); | 55 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); |
57 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); | 56 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); |
58 } | 57 } |
59 | 58 |
60 void SignalingConnector::EnableOAuth( | 59 void SignalingConnector::EnableOAuth( |
61 scoped_ptr<OAuthCredentials> oauth_credentials) { | 60 scoped_ptr<OAuthCredentials> oauth_credentials) { |
62 oauth_credentials_ = oauth_credentials.Pass(); | 61 oauth_credentials_ = oauth_credentials.Pass(); |
63 gaia_oauth_client_.reset(new GaiaOAuthClient( | 62 gaia_oauth_client_.reset(new gaia::GaiaOAuthClient( |
64 OAuthProviderInfo::GetDefault(), url_request_context_getter_)); | 63 gaia::kGaiaOAuth2Url, url_request_context_getter_)); |
65 } | 64 } |
66 | 65 |
67 void SignalingConnector::OnSignalStrategyStateChange( | 66 void SignalingConnector::OnSignalStrategyStateChange( |
68 SignalStrategy::State state) { | 67 SignalStrategy::State state) { |
69 DCHECK(CalledOnValidThread()); | 68 DCHECK(CalledOnValidThread()); |
70 | 69 |
71 if (state == SignalStrategy::CONNECTED) { | 70 if (state == SignalStrategy::CONNECTED) { |
72 LOG(INFO) << "Signaling connected."; | 71 LOG(INFO) << "Signaling connected."; |
73 reconnect_attempts_ = 0; | 72 reconnect_attempts_ = 0; |
74 } else if (state == SignalStrategy::DISCONNECTED) { | 73 } else if (state == SignalStrategy::DISCONNECTED) { |
(...skipping 23 matching lines...) Expand all Loading... | |
98 ResetAndTryReconnect(); | 97 ResetAndTryReconnect(); |
99 } | 98 } |
100 } | 99 } |
101 | 100 |
102 void SignalingConnector::OnIPAddressChanged() { | 101 void SignalingConnector::OnIPAddressChanged() { |
103 DCHECK(CalledOnValidThread()); | 102 DCHECK(CalledOnValidThread()); |
104 LOG(INFO) << "IP address has changed."; | 103 LOG(INFO) << "IP address has changed."; |
105 ResetAndTryReconnect(); | 104 ResetAndTryReconnect(); |
106 } | 105 } |
107 | 106 |
108 void SignalingConnector::OnRefreshTokenResponse(const std::string& user_email, | 107 void SignalingConnector::OnGetTokensResponse(const std::string& user_email, |
109 const std::string& access_token, | 108 const std::string& access_token, |
110 int expires_seconds) { | 109 int expires_seconds) { |
110 NOTREACHED(); | |
111 } | |
112 | |
113 void SignalingConnector::OnRefreshTokenResponse( | |
114 const std::string& access_token, | |
115 int expires_seconds) { | |
111 DCHECK(CalledOnValidThread()); | 116 DCHECK(CalledOnValidThread()); |
112 DCHECK(oauth_credentials_.get()); | 117 DCHECK(oauth_credentials_.get()); |
113 LOG(INFO) << "Received OAuth token."; | 118 LOG(INFO) << "Received OAuth token."; |
114 | 119 |
120 signal_strategy_->SetAuthInfo(oauth_credentials_->login, | |
simonmorris
2012/10/25 06:37:40
Consider moving this where it was before, after th
Sergey Ulanov
2012/10/26 21:05:00
Done.
| |
121 access_token, "oauth2"); | |
122 auth_token_expiry_time_ = base::Time::Now() + | |
123 base::TimeDelta::FromSeconds(expires_seconds) - | |
124 base::TimeDelta::FromSeconds(kTokenUpdateTimeBeforeExpirySeconds); | |
125 | |
126 gaia_oauth_client_->GetUserInfo(access_token, 1, this); | |
127 } | |
128 | |
129 void SignalingConnector::OnGetUserInfoResponse(const std::string& user_email) { | |
130 DCHECK(CalledOnValidThread()); | |
131 DCHECK(oauth_credentials_.get()); | |
132 LOG(INFO) << "Received user info."; | |
133 | |
115 if (user_email != oauth_credentials_->login) { | 134 if (user_email != oauth_credentials_->login) { |
116 LOG(ERROR) << "OAuth token and email address do not refer to " | 135 LOG(ERROR) << "OAuth token and email address do not refer to " |
117 "the same account."; | 136 "the same account."; |
118 auth_failed_callback_.Run(); | 137 auth_failed_callback_.Run(); |
119 return; | 138 return; |
120 } | 139 } |
121 | 140 |
122 refreshing_oauth_token_ = false; | 141 refreshing_oauth_token_ = false; |
123 auth_token_expiry_time_ = base::Time::Now() + | |
124 base::TimeDelta::FromSeconds(expires_seconds) - | |
125 base::TimeDelta::FromSeconds(kTokenUpdateTimeBeforeExpirySeconds); | |
126 signal_strategy_->SetAuthInfo(oauth_credentials_->login, | |
127 access_token, "oauth2"); | |
128 | 142 |
129 // Now that we've got the new token, try to connect using it. | 143 // Now that we've refreshed the token and verified that it's for the correct |
144 // user account, try to connect using the new token. | |
130 DCHECK_EQ(signal_strategy_->GetState(), SignalStrategy::DISCONNECTED); | 145 DCHECK_EQ(signal_strategy_->GetState(), SignalStrategy::DISCONNECTED); |
131 signal_strategy_->Connect(); | 146 signal_strategy_->Connect(); |
132 } | 147 } |
133 | 148 |
134 void SignalingConnector::OnOAuthError() { | 149 void SignalingConnector::OnOAuthError() { |
135 DCHECK(CalledOnValidThread()); | 150 DCHECK(CalledOnValidThread()); |
136 LOG(ERROR) << "OAuth: invalid credentials."; | 151 LOG(ERROR) << "OAuth: invalid credentials."; |
137 refreshing_oauth_token_ = false; | 152 refreshing_oauth_token_ = false; |
138 reconnect_attempts_++; | 153 reconnect_attempts_++; |
139 auth_failed_callback_.Run(); | 154 auth_failed_callback_.Run(); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
202 signal_strategy_->Connect(); | 217 signal_strategy_->Connect(); |
203 } | 218 } |
204 } | 219 } |
205 } | 220 } |
206 | 221 |
207 void SignalingConnector::RefreshOAuthToken() { | 222 void SignalingConnector::RefreshOAuthToken() { |
208 DCHECK(CalledOnValidThread()); | 223 DCHECK(CalledOnValidThread()); |
209 LOG(INFO) << "Refreshing OAuth token."; | 224 LOG(INFO) << "Refreshing OAuth token."; |
210 DCHECK(!refreshing_oauth_token_); | 225 DCHECK(!refreshing_oauth_token_); |
211 | 226 |
227 gaia::OAuthClientInfo client_info = { | |
rmsousa
2012/10/24 23:18:47
I'm not sure if we should move the client id/secre
Sergey Ulanov
2012/10/24 23:31:30
It's likely that we won't need to use different cl
| |
228 google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING), | |
229 google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING), | |
230 // Redirect URL is only used when getting tokens from auth code. It | |
231 // is not required when getting access tokens. | |
232 "" | |
233 }; | |
234 | |
212 refreshing_oauth_token_ = true; | 235 refreshing_oauth_token_ = true; |
213 gaia_oauth_client_->RefreshToken( | 236 gaia_oauth_client_->RefreshToken( |
214 oauth_credentials_->client_info, | 237 client_info, oauth_credentials_->refresh_token, 1, this); |
215 oauth_credentials_->refresh_token, this); | |
216 } | 238 } |
217 | 239 |
218 } // namespace remoting | 240 } // namespace remoting |
OLD | NEW |