Chromium Code Reviews| 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 "net/url_request/url_fetcher.h" | |
| 9 #include "remoting/host/chromoting_host_context.h" | 10 #include "remoting/host/chromoting_host_context.h" |
| 11 #include "remoting/host/connection_block_checker.h" | |
| 12 #include "remoting/host/constants.h" | |
| 10 #include "remoting/host/url_request_context.h" | 13 #include "remoting/host/url_request_context.h" |
| 11 | 14 |
| 12 namespace remoting { | 15 namespace remoting { |
| 13 | 16 |
| 14 namespace { | 17 namespace { |
| 15 | 18 |
| 16 // The delay between reconnect attempts will increase exponentially up | 19 // The delay between reconnect attempts will increase exponentially up |
| 17 // to the maximum specified here. | 20 // to the maximum specified here. |
| 18 const int kMaxReconnectDelaySeconds = 10 * 60; | 21 const int kMaxReconnectDelaySeconds = 10 * 60; |
| 19 | 22 |
| 20 // Time when we we try to update OAuth token before its expiration. | 23 // Time when we we try to update OAuth token before its expiration. |
| 21 const int kTokenUpdateTimeBeforeExpirySeconds = 60; | 24 const int kTokenUpdateTimeBeforeExpirySeconds = 60; |
| 22 | 25 |
| 23 } // namespace | 26 } // namespace |
| 24 | 27 |
| 25 SignalingConnector::OAuthCredentials::OAuthCredentials( | 28 SignalingConnector::OAuthCredentials::OAuthCredentials( |
| 26 const std::string& login_value, | 29 const std::string& login_value, |
| 27 const std::string& refresh_token_value, | 30 const std::string& refresh_token_value, |
| 28 const OAuthClientInfo& client_info_value) | 31 const OAuthClientInfo& client_info_value) |
| 29 : login(login_value), | 32 : login(login_value), |
| 30 refresh_token(refresh_token_value), | 33 refresh_token(refresh_token_value), |
| 31 client_info(client_info_value) { | 34 client_info(client_info_value) { |
| 32 } | 35 } |
| 33 | 36 |
| 34 SignalingConnector::SignalingConnector( | 37 SignalingConnector::SignalingConnector( |
| 35 XmppSignalStrategy* signal_strategy, | 38 XmppSignalStrategy* signal_strategy, |
| 39 ChromotingHostContext* context, | |
| 40 std::string talkgadget_prefix, | |
| 36 const base::Closure& auth_failed_callback) | 41 const base::Closure& auth_failed_callback) |
| 37 : signal_strategy_(signal_strategy), | 42 : signal_strategy_(signal_strategy), |
| 43 context_(context), | |
| 38 auth_failed_callback_(auth_failed_callback), | 44 auth_failed_callback_(auth_failed_callback), |
| 39 reconnect_attempts_(0), | 45 reconnect_attempts_(0), |
| 40 refreshing_oauth_token_(false) { | 46 refreshing_oauth_token_(false) { |
| 41 DCHECK(!auth_failed_callback_.is_null()); | 47 DCHECK(!auth_failed_callback_.is_null()); |
| 42 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); | 48 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); |
| 43 net::NetworkChangeNotifier::AddIPAddressObserver(this); | 49 net::NetworkChangeNotifier::AddIPAddressObserver(this); |
| 44 signal_strategy_->AddListener(this); | 50 signal_strategy_->AddListener(this); |
| 51 connection_block_checker_.reset( | |
| 52 new ConnectionBlockChecker(context_, talkgadget_prefix)); | |
| 45 ScheduleTryReconnect(); | 53 ScheduleTryReconnect(); |
| 46 } | 54 } |
| 47 | 55 |
| 48 SignalingConnector::~SignalingConnector() { | 56 SignalingConnector::~SignalingConnector() { |
| 49 signal_strategy_->RemoveListener(this); | 57 signal_strategy_->RemoveListener(this); |
| 50 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); | 58 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); |
| 51 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); | 59 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); |
| 52 } | 60 } |
| 53 | 61 |
| 54 void SignalingConnector::EnableOAuth( | 62 void SignalingConnector::EnableOAuth( |
| 55 scoped_ptr<OAuthCredentials> oauth_credentials, | 63 scoped_ptr<OAuthCredentials> oauth_credentials) { |
| 56 net::URLRequestContextGetter* url_context) { | |
| 57 oauth_credentials_ = oauth_credentials.Pass(); | 64 oauth_credentials_ = oauth_credentials.Pass(); |
| 58 gaia_oauth_client_.reset(new GaiaOAuthClient( | 65 gaia_oauth_client_.reset(new GaiaOAuthClient( |
| 59 OAuthProviderInfo::GetDefault(), url_context)); | 66 OAuthProviderInfo::GetDefault(), context_->url_request_context_getter())); |
| 60 } | 67 } |
| 61 | 68 |
| 62 void SignalingConnector::OnSignalStrategyStateChange( | 69 void SignalingConnector::OnSignalStrategyStateChange( |
| 63 SignalStrategy::State state) { | 70 SignalStrategy::State state) { |
| 64 DCHECK(CalledOnValidThread()); | 71 DCHECK(CalledOnValidThread()); |
| 65 | 72 |
| 66 if (state == SignalStrategy::CONNECTED) { | 73 if (state == SignalStrategy::CONNECTED) { |
| 67 LOG(INFO) << "Signaling connected."; | 74 LOG(INFO) << "Signaling connected."; |
| 68 reconnect_attempts_ = 0; | 75 reconnect_attempts_ = 0; |
| 69 } else if (state == SignalStrategy::DISCONNECTED) { | 76 } else if (state == SignalStrategy::DISCONNECTED) { |
| 70 LOG(INFO) << "Signaling disconnected."; | 77 LOG(INFO) << "Signaling disconnected."; |
| 71 reconnect_attempts_++; | 78 reconnect_attempts_++; |
| 72 | 79 |
| 73 // If authentication failed then we have an invalid OAuth token, | 80 // If authentication failed then we have an invalid OAuth token, |
| 74 // inform the upper layer about it. | 81 // inform the upper layer about it. |
| 75 if (signal_strategy_->GetError() == SignalStrategy::AUTHENTICATION_FAILED) { | 82 if (signal_strategy_->GetError() == SignalStrategy::AUTHENTICATION_FAILED) { |
| 76 auth_failed_callback_.Run(); | 83 auth_failed_callback_.Run(); |
| 77 } else { | 84 } else { |
| 78 ScheduleTryReconnect(); | 85 ScheduleTryReconnect(); |
| 79 } | 86 } |
| 80 } | 87 } |
| 81 } | 88 } |
| 82 | 89 |
| 83 bool SignalingConnector::OnSignalStrategyIncomingStanza( | 90 bool SignalingConnector::OnSignalStrategyIncomingStanza( |
| 84 const buzz::XmlElement* stanza) { | 91 const buzz::XmlElement* stanza) { |
| 85 return false; | 92 return false; |
| 86 } | 93 } |
| 87 | 94 |
| 88 void SignalingConnector::OnIPAddressChanged() { | |
| 89 DCHECK(CalledOnValidThread()); | |
| 90 LOG(INFO) << "IP address has changed."; | |
| 91 ResetAndTryReconnect(); | |
| 92 } | |
| 93 | |
| 94 void SignalingConnector::OnConnectionTypeChanged( | 95 void SignalingConnector::OnConnectionTypeChanged( |
| 95 net::NetworkChangeNotifier::ConnectionType type) { | 96 net::NetworkChangeNotifier::ConnectionType type) { |
| 96 DCHECK(CalledOnValidThread()); | 97 DCHECK(CalledOnValidThread()); |
| 97 if (type != net::NetworkChangeNotifier::CONNECTION_NONE) { | 98 if (type != net::NetworkChangeNotifier::CONNECTION_NONE) { |
| 98 LOG(INFO) << "Network state changed to online."; | 99 LOG(INFO) << "Network state changed to online."; |
| 99 ResetAndTryReconnect(); | 100 ResetAndTryReconnect(); |
| 100 } | 101 } |
| 101 } | 102 } |
| 102 | 103 |
| 104 void SignalingConnector::OnIPAddressChanged() { | |
| 105 DCHECK(CalledOnValidThread()); | |
| 106 LOG(INFO) << "IP address has changed."; | |
| 107 ResetAndTryReconnect(); | |
| 108 } | |
| 109 | |
| 103 void SignalingConnector::OnRefreshTokenResponse(const std::string& user_email, | 110 void SignalingConnector::OnRefreshTokenResponse(const std::string& user_email, |
| 104 const std::string& access_token, | 111 const std::string& access_token, |
| 105 int expires_seconds) { | 112 int expires_seconds) { |
| 106 DCHECK(CalledOnValidThread()); | 113 DCHECK(CalledOnValidThread()); |
| 107 DCHECK(oauth_credentials_.get()); | 114 DCHECK(oauth_credentials_.get()); |
| 108 LOG(INFO) << "Received OAuth token."; | 115 LOG(INFO) << "Received OAuth token."; |
| 109 | 116 |
| 110 if (user_email != oauth_credentials_->login) { | 117 if (user_email != oauth_credentials_->login) { |
| 111 LOG(ERROR) << "OAuth token and email address do not refer to " | 118 LOG(ERROR) << "OAuth token and email address do not refer to " |
| 112 "the same account."; | 119 "the same account."; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 156 void SignalingConnector::ResetAndTryReconnect() { | 163 void SignalingConnector::ResetAndTryReconnect() { |
| 157 DCHECK(CalledOnValidThread()); | 164 DCHECK(CalledOnValidThread()); |
| 158 signal_strategy_->Disconnect(); | 165 signal_strategy_->Disconnect(); |
| 159 reconnect_attempts_ = 0; | 166 reconnect_attempts_ = 0; |
| 160 timer_.Stop(); | 167 timer_.Stop(); |
| 161 ScheduleTryReconnect(); | 168 ScheduleTryReconnect(); |
| 162 } | 169 } |
| 163 | 170 |
| 164 void SignalingConnector::TryReconnect() { | 171 void SignalingConnector::TryReconnect() { |
| 165 DCHECK(CalledOnValidThread()); | 172 DCHECK(CalledOnValidThread()); |
| 173 DCHECK(connection_block_checker_.get()); | |
| 174 | |
| 175 ConnectionBlockChecker::Status status; | |
| 176 status = connection_block_checker_->GetStatus(); | |
| 177 if (status == ConnectionBlockChecker::UNKNOWN) { | |
| 178 ScheduleTryReconnect(); | |
| 179 return; | |
| 180 } else if (status == ConnectionBlockChecker::DENY) { | |
| 181 return; | |
|
Sergey Ulanov
2012/08/28 00:33:42
This will keep the host running even though we kno
garykac
2012/08/29 00:07:15
Done.
| |
| 182 } | |
| 183 | |
| 166 if (signal_strategy_->GetState() == SignalStrategy::DISCONNECTED) { | 184 if (signal_strategy_->GetState() == SignalStrategy::DISCONNECTED) { |
| 167 bool need_new_auth_token = oauth_credentials_.get() && | 185 bool need_new_auth_token = oauth_credentials_.get() && |
| 168 (auth_token_expiry_time_.is_null() || | 186 (auth_token_expiry_time_.is_null() || |
| 169 base::Time::Now() >= auth_token_expiry_time_); | 187 base::Time::Now() >= auth_token_expiry_time_); |
| 170 if (need_new_auth_token) { | 188 if (need_new_auth_token) { |
| 171 RefreshOAuthToken(); | 189 RefreshOAuthToken(); |
| 172 } else { | 190 } else { |
| 173 LOG(INFO) << "Attempting to connect signaling."; | 191 LOG(INFO) << "Attempting to connect signaling."; |
| 174 signal_strategy_->Connect(); | 192 signal_strategy_->Connect(); |
| 175 } | 193 } |
| 176 } | 194 } |
| 177 } | 195 } |
| 178 | 196 |
| 179 void SignalingConnector::RefreshOAuthToken() { | 197 void SignalingConnector::RefreshOAuthToken() { |
| 180 DCHECK(CalledOnValidThread()); | 198 DCHECK(CalledOnValidThread()); |
| 181 LOG(INFO) << "Refreshing OAuth token."; | 199 LOG(INFO) << "Refreshing OAuth token."; |
| 182 DCHECK(!refreshing_oauth_token_); | 200 DCHECK(!refreshing_oauth_token_); |
| 183 | 201 |
| 184 refreshing_oauth_token_ = true; | 202 refreshing_oauth_token_ = true; |
| 185 gaia_oauth_client_->RefreshToken( | 203 gaia_oauth_client_->RefreshToken( |
| 186 oauth_credentials_->client_info, | 204 oauth_credentials_->client_info, |
| 187 oauth_credentials_->refresh_token, this); | 205 oauth_credentials_->refresh_token, this); |
| 188 } | 206 } |
| 189 | 207 |
| 190 } // namespace remoting | 208 } // namespace remoting |
| OLD | NEW |