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