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 3 // found in the LICENSE file. 4 5 #ifndef REMOTING_HOST_SIGNALING_CONNECTOR_H_ 6 #define REMOTING_HOST_SIGNALING_CONNECTOR_H_ 7 8 #include "base/basictypes.h" 9 #include "base/memory/weak_ptr.h" 10 #include "base/threading/non_thread_safe.h" 11 #include "base/timer/timer.h" 12 #include "google_apis/gaia/gaia_oauth_client.h" 13 #include "net/base/network_change_notifier.h" 14 #include "remoting/jingle_glue/xmpp_signal_strategy.h" 15 16 namespace net { 17 class URLFetcher; 18 class URLRequestContextGetter; 19 } // namespace net 20 21 namespace remoting { 22 23 class DnsBlackholeChecker; 24 25 // SignalingConnector listens for SignalStrategy status notifications 26 // and attempts to keep it connected when possible. When signalling is 27 // not connected it keeps trying to reconnect it until it is 28 // connected. It limits connection attempt rate using exponential 29 // backoff. It also monitors network state and reconnects signalling 30 // whenever connection type changes or IP address changes. 31 class SignalingConnector 32 : public base::SupportsWeakPtr<SignalingConnector>, 33 public base::NonThreadSafe, 34 public SignalStrategy::Listener, 35 public net::NetworkChangeNotifier::ConnectionTypeObserver, 36 public net::NetworkChangeNotifier::IPAddressObserver, 37 public gaia::GaiaOAuthClient::Delegate { 38 public: 39 // This structure contains information required to perform 40 // authentication to OAuth2. 41 struct OAuthCredentials { 42 OAuthCredentials(const std::string& login_value, 43 const std::string& refresh_token_value); 44 45 // The user's account name (i.e. their email address). 46 std::string login; 47 48 // Token delegating authority to us to act as the user. 49 std::string refresh_token; 50 }; 51 52 // The |auth_failed_callback| is called when authentication fails. 53 SignalingConnector( 54 XmppSignalStrategy* signal_strategy, 55 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, 56 scoped_ptr<DnsBlackholeChecker> dns_blackhole_checker, 57 const base::Closure& auth_failed_callback); 58 virtual ~SignalingConnector(); 59 60 // May be called immediately after the constructor to enable OAuth 61 // access token updating. 62 void EnableOAuth(scoped_ptr<OAuthCredentials> oauth_credentials); 63 64 // SignalStrategy::Listener interface. 65 virtual void OnSignalStrategyStateChange( 66 SignalStrategy::State state) OVERRIDE; 67 virtual bool OnSignalStrategyIncomingStanza( 68 const buzz::XmlElement* stanza) OVERRIDE; 69 70 // NetworkChangeNotifier::ConnectionTypeObserver interface. 71 virtual void OnConnectionTypeChanged( 72 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE; 73 74 // NetworkChangeNotifier::IPAddressObserver interface. 75 virtual void OnIPAddressChanged() OVERRIDE; 76 77 // gaia::GaiaOAuthClient::Delegate interface. 78 virtual void OnGetTokensResponse(const std::string& user_email, 79 const std::string& access_token, 80 int expires_seconds) OVERRIDE; 81 virtual void OnRefreshTokenResponse(const std::string& access_token, 82 int expires_in_seconds) OVERRIDE; 83 virtual void OnGetUserEmailResponse(const std::string& user_email) OVERRIDE; 84 virtual void OnOAuthError() OVERRIDE; 85 virtual void OnNetworkError(int response_code) OVERRIDE; 86 87 private: 88 void ScheduleTryReconnect(); 89 void ResetAndTryReconnect(); 90 void TryReconnect(); 91 void OnDnsBlackholeCheckerDone(bool allow); 92 93 void RefreshOAuthToken(); 94 95 XmppSignalStrategy* signal_strategy_; 96 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; 97 base::Closure auth_failed_callback_; 98 99 scoped_ptr<OAuthCredentials> oauth_credentials_; 100 scoped_ptr<gaia::GaiaOAuthClient> gaia_oauth_client_; 101 scoped_ptr<DnsBlackholeChecker> dns_blackhole_checker_; 102 103 // Number of times we tried to connect without success. 104 int reconnect_attempts_; 105 106 bool refreshing_oauth_token_; 107 std::string oauth_access_token_; 108 base::Time auth_token_expiry_time_; 109 110 base::OneShotTimer<SignalingConnector> timer_; 111 112 DISALLOW_COPY_AND_ASSIGN(SignalingConnector); 113 }; 114 115 } // namespace remoting 116 117 #endif // REMOTING_HOST_SIGNALING_CONNECTOR_H_ 118