| 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/constants.h" |
| 10 #include "remoting/host/url_request_context.h" | 12 #include "remoting/host/url_request_context.h" |
| 11 | 13 |
| 12 namespace remoting { | 14 namespace remoting { |
| 13 | 15 |
| 14 namespace { | 16 namespace { |
| 15 | 17 |
| 16 // The delay between reconnect attempts will increase exponentially up | 18 // The delay between reconnect attempts will increase exponentially up |
| 17 // to the maximum specified here. | 19 // to the maximum specified here. |
| 18 const int kMaxReconnectDelaySeconds = 10 * 60; | 20 const int kMaxReconnectDelaySeconds = 10 * 60; |
| 19 | 21 |
| 20 // 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. |
| 21 const int kTokenUpdateTimeBeforeExpirySeconds = 60; | 23 const int kTokenUpdateTimeBeforeExpirySeconds = 60; |
| 22 | 24 |
| 23 } // namespace | 25 } // namespace |
| 24 | 26 |
| 25 SignalingConnector::OAuthCredentials::OAuthCredentials( | 27 SignalingConnector::OAuthCredentials::OAuthCredentials( |
| 26 const std::string& login_value, | 28 const std::string& login_value, |
| 27 const std::string& refresh_token_value, | 29 const std::string& refresh_token_value, |
| 28 const OAuthClientInfo& client_info_value) | 30 const OAuthClientInfo& client_info_value) |
| 29 : login(login_value), | 31 : login(login_value), |
| 30 refresh_token(refresh_token_value), | 32 refresh_token(refresh_token_value), |
| 31 client_info(client_info_value) { | 33 client_info(client_info_value) { |
| 32 } | 34 } |
| 33 | 35 |
| 34 SignalingConnector::SignalingConnector( | 36 SignalingConnector::SignalingConnector( |
| 35 XmppSignalStrategy* signal_strategy, | 37 XmppSignalStrategy* signal_strategy, |
| 38 ChromotingHostContext* context, |
| 39 std::string talkgadget_prefix, |
| 36 const base::Closure& auth_failed_callback) | 40 const base::Closure& auth_failed_callback) |
| 37 : signal_strategy_(signal_strategy), | 41 : signal_strategy_(signal_strategy), |
| 42 context_(context), |
| 38 auth_failed_callback_(auth_failed_callback), | 43 auth_failed_callback_(auth_failed_callback), |
| 44 talkgadget_prefix_(talkgadget_prefix), |
| 45 found_talkgadget_(false), |
| 39 reconnect_attempts_(0), | 46 reconnect_attempts_(0), |
| 40 refreshing_oauth_token_(false) { | 47 refreshing_oauth_token_(false) { |
| 41 DCHECK(!auth_failed_callback_.is_null()); | 48 DCHECK(!auth_failed_callback_.is_null()); |
| 42 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); | 49 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); |
| 43 net::NetworkChangeNotifier::AddIPAddressObserver(this); | 50 net::NetworkChangeNotifier::AddIPAddressObserver(this); |
| 44 signal_strategy_->AddListener(this); | 51 signal_strategy_->AddListener(this); |
| 52 LOG(INFO) << "Creating SignalingConnector"; |
| 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) { | 64 LOG(INFO) << "Enabling OAuth"; |
| 57 oauth_credentials_ = oauth_credentials.Pass(); | 65 oauth_credentials_ = oauth_credentials.Pass(); |
| 58 gaia_oauth_client_.reset(new GaiaOAuthClient( | 66 gaia_oauth_client_.reset(new GaiaOAuthClient( |
| 59 OAuthProviderInfo::GetDefault(), url_context)); | 67 OAuthProviderInfo::GetDefault(), context_->url_request_context_getter())); |
| 60 } | 68 } |
| 61 | 69 |
| 70 // SignalStrategy::Listener interface. |
| 62 void SignalingConnector::OnSignalStrategyStateChange( | 71 void SignalingConnector::OnSignalStrategyStateChange( |
| 63 SignalStrategy::State state) { | 72 SignalStrategy::State state) { |
| 64 DCHECK(CalledOnValidThread()); | 73 DCHECK(CalledOnValidThread()); |
| 65 | 74 |
| 66 if (state == SignalStrategy::CONNECTED) { | 75 if (state == SignalStrategy::CONNECTED) { |
| 67 LOG(INFO) << "Signaling connected."; | 76 LOG(INFO) << "Signaling connected."; |
| 68 reconnect_attempts_ = 0; | 77 reconnect_attempts_ = 0; |
| 69 } else if (state == SignalStrategy::DISCONNECTED) { | 78 } else if (state == SignalStrategy::DISCONNECTED) { |
| 70 LOG(INFO) << "Signaling disconnected."; | 79 LOG(INFO) << "Signaling disconnected."; |
| 71 reconnect_attempts_++; | 80 reconnect_attempts_++; |
| 72 | 81 |
| 73 // If authentication failed then we have an invalid OAuth token, | 82 // If authentication failed then we have an invalid OAuth token, |
| 74 // inform the upper layer about it. | 83 // inform the upper layer about it. |
| 75 if (signal_strategy_->GetError() == SignalStrategy::AUTHENTICATION_FAILED) { | 84 if (signal_strategy_->GetError() == SignalStrategy::AUTHENTICATION_FAILED) { |
| 76 auth_failed_callback_.Run(); | 85 auth_failed_callback_.Run(); |
| 77 } else { | 86 } else { |
| 78 ScheduleTryReconnect(); | 87 ScheduleTryReconnect(); |
| 79 } | 88 } |
| 80 } | 89 } |
| 81 } | 90 } |
| 82 | 91 |
| 83 bool SignalingConnector::OnSignalStrategyIncomingStanza( | 92 bool SignalingConnector::OnSignalStrategyIncomingStanza( |
| 84 const buzz::XmlElement* stanza) { | 93 const buzz::XmlElement* stanza) { |
| 85 return false; | 94 return false; |
| 86 } | 95 } |
| 87 | 96 |
| 88 void SignalingConnector::OnIPAddressChanged() { | 97 // net::URLFetcherDelegate interface. |
| 89 DCHECK(CalledOnValidThread()); | 98 void SignalingConnector::OnURLFetchComplete(const net::URLFetcher* source) { |
| 90 LOG(INFO) << "IP address has changed."; | 99 LOG(INFO) << "URLFetch returned " << source->GetResponseCode(); |
| 91 ResetAndTryReconnect(); | 100 int response = source->GetResponseCode(); |
| 101 if (response == 200) { |
| 102 found_talkgadget_ = true; |
| 103 url_fetcher_.reset(NULL); |
| 104 TryReconnect(); |
| 105 return; |
| 106 } |
| 107 LOG(INFO) << "Scheduling a reconnect"; |
| 108 reconnect_attempts_++; |
| 109 ScheduleTryReconnect(); |
| 92 } | 110 } |
| 93 | 111 |
| 112 // NetworkChangeNotifier::ConnectionTypeObserver interface. |
| 94 void SignalingConnector::OnConnectionTypeChanged( | 113 void SignalingConnector::OnConnectionTypeChanged( |
| 95 net::NetworkChangeNotifier::ConnectionType type) { | 114 net::NetworkChangeNotifier::ConnectionType type) { |
| 96 DCHECK(CalledOnValidThread()); | 115 DCHECK(CalledOnValidThread()); |
| 97 if (type != net::NetworkChangeNotifier::CONNECTION_NONE) { | 116 if (type != net::NetworkChangeNotifier::CONNECTION_NONE) { |
| 98 LOG(INFO) << "Network state changed to online."; | 117 LOG(INFO) << "Network state changed to online."; |
| 99 ResetAndTryReconnect(); | 118 ResetAndTryReconnect(); |
| 100 } | 119 } |
| 101 } | 120 } |
| 102 | 121 |
| 122 // NetworkChangeNotifier::IPAddressObserver interface. |
| 123 void SignalingConnector::OnIPAddressChanged() { |
| 124 DCHECK(CalledOnValidThread()); |
| 125 LOG(INFO) << "IP address has changed."; |
| 126 ResetAndTryReconnect(); |
| 127 } |
| 128 |
| 129 // GaiaOAuthClient::Delegate interface. |
| 103 void SignalingConnector::OnRefreshTokenResponse(const std::string& user_email, | 130 void SignalingConnector::OnRefreshTokenResponse(const std::string& user_email, |
| 104 const std::string& access_token, | 131 const std::string& access_token, |
| 105 int expires_seconds) { | 132 int expires_seconds) { |
| 106 DCHECK(CalledOnValidThread()); | 133 DCHECK(CalledOnValidThread()); |
| 107 DCHECK(oauth_credentials_.get()); | 134 DCHECK(oauth_credentials_.get()); |
| 108 LOG(INFO) << "Received OAuth token."; | 135 LOG(INFO) << "Received OAuth token."; |
| 109 | 136 |
| 110 if (user_email != oauth_credentials_->login) { | 137 if (user_email != oauth_credentials_->login) { |
| 111 LOG(ERROR) << "OAuth token and email address do not refer to " | 138 LOG(ERROR) << "OAuth token and email address do not refer to " |
| 112 "the same account."; | 139 "the same account."; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 void SignalingConnector::ResetAndTryReconnect() { | 183 void SignalingConnector::ResetAndTryReconnect() { |
| 157 DCHECK(CalledOnValidThread()); | 184 DCHECK(CalledOnValidThread()); |
| 158 signal_strategy_->Disconnect(); | 185 signal_strategy_->Disconnect(); |
| 159 reconnect_attempts_ = 0; | 186 reconnect_attempts_ = 0; |
| 160 timer_.Stop(); | 187 timer_.Stop(); |
| 161 ScheduleTryReconnect(); | 188 ScheduleTryReconnect(); |
| 162 } | 189 } |
| 163 | 190 |
| 164 void SignalingConnector::TryReconnect() { | 191 void SignalingConnector::TryReconnect() { |
| 165 DCHECK(CalledOnValidThread()); | 192 DCHECK(CalledOnValidThread()); |
| 193 |
| 194 if (!found_talkgadget_) { |
| 195 std::string talkgadget_url("https://"); |
| 196 if (talkgadget_prefix_.empty()) { |
| 197 talkgadget_url += kDefaultHostTalkGadgetPrefix; |
| 198 } else { |
| 199 talkgadget_url += talkgadget_prefix_; |
| 200 } |
| 201 talkgadget_url += kTalkGadgetURL; |
| 202 LOG(INFO) << "Checking access to " << talkgadget_url; |
| 203 url_fetcher_.reset(net::URLFetcher::Create(GURL(talkgadget_url), |
| 204 net::URLFetcher::GET, this)); |
| 205 url_fetcher_->SetRequestContext(context_->url_request_context_getter()); |
| 206 url_fetcher_->Start(); |
| 207 return; |
| 208 } |
| 209 |
| 166 if (signal_strategy_->GetState() == SignalStrategy::DISCONNECTED) { | 210 if (signal_strategy_->GetState() == SignalStrategy::DISCONNECTED) { |
| 211 |
| 167 bool need_new_auth_token = oauth_credentials_.get() && | 212 bool need_new_auth_token = oauth_credentials_.get() && |
| 168 (auth_token_expiry_time_.is_null() || | 213 (auth_token_expiry_time_.is_null() || |
| 169 base::Time::Now() >= auth_token_expiry_time_); | 214 base::Time::Now() >= auth_token_expiry_time_); |
| 170 if (need_new_auth_token) { | 215 if (need_new_auth_token) { |
| 171 RefreshOAuthToken(); | 216 RefreshOAuthToken(); |
| 172 } else { | 217 } else { |
| 173 LOG(INFO) << "Attempting to connect signaling."; | 218 LOG(INFO) << "Attempting to connect signaling."; |
| 174 signal_strategy_->Connect(); | 219 signal_strategy_->Connect(); |
| 175 } | 220 } |
| 176 } | 221 } |
| 177 } | 222 } |
| 178 | 223 |
| 179 void SignalingConnector::RefreshOAuthToken() { | 224 void SignalingConnector::RefreshOAuthToken() { |
| 180 DCHECK(CalledOnValidThread()); | 225 DCHECK(CalledOnValidThread()); |
| 181 LOG(INFO) << "Refreshing OAuth token."; | 226 LOG(INFO) << "Refreshing OAuth token."; |
| 182 DCHECK(!refreshing_oauth_token_); | 227 DCHECK(!refreshing_oauth_token_); |
| 183 | 228 |
| 184 refreshing_oauth_token_ = true; | 229 refreshing_oauth_token_ = true; |
| 185 gaia_oauth_client_->RefreshToken( | 230 gaia_oauth_client_->RefreshToken( |
| 186 oauth_credentials_->client_info, | 231 oauth_credentials_->client_info, |
| 187 oauth_credentials_->refresh_token, this); | 232 oauth_credentials_->refresh_token, this); |
| 188 } | 233 } |
| 189 | 234 |
| 190 } // namespace remoting | 235 } // namespace remoting |
| OLD | NEW |