Quota: Return a pair of usage and granted_quota on requesting quota.
[chromium-blink-merge.git] / remoting / host / signaling_connector.h
blob77474cd34e22b391cf05595b817a8e40a0e8e898
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.
5 #ifndef REMOTING_HOST_SIGNALING_CONNECTOR_H_
6 #define REMOTING_HOST_SIGNALING_CONNECTOR_H_
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"
16 namespace net {
17 class URLFetcher;
18 class URLRequestContextGetter;
19 } // namespace net
21 namespace remoting {
23 class DnsBlackholeChecker;
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 bool is_service_account);
46 // The user's account name (i.e. their email address).
47 std::string login;
49 // Token delegating authority to us to act as the user.
50 std::string refresh_token;
52 // Whether these credentials belong to a service account.
53 bool is_service_account;
56 // The |auth_failed_callback| is called when authentication fails.
57 SignalingConnector(
58 XmppSignalStrategy* signal_strategy,
59 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter,
60 scoped_ptr<DnsBlackholeChecker> dns_blackhole_checker,
61 const base::Closure& auth_failed_callback);
62 virtual ~SignalingConnector();
64 // May be called immediately after the constructor to enable OAuth
65 // access token updating.
66 void EnableOAuth(scoped_ptr<OAuthCredentials> oauth_credentials);
68 // SignalStrategy::Listener interface.
69 virtual void OnSignalStrategyStateChange(
70 SignalStrategy::State state) OVERRIDE;
71 virtual bool OnSignalStrategyIncomingStanza(
72 const buzz::XmlElement* stanza) OVERRIDE;
74 // NetworkChangeNotifier::ConnectionTypeObserver interface.
75 virtual void OnConnectionTypeChanged(
76 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
78 // NetworkChangeNotifier::IPAddressObserver interface.
79 virtual void OnIPAddressChanged() OVERRIDE;
81 // gaia::GaiaOAuthClient::Delegate interface.
82 virtual void OnGetTokensResponse(const std::string& user_email,
83 const std::string& access_token,
84 int expires_seconds) OVERRIDE;
85 virtual void OnRefreshTokenResponse(const std::string& access_token,
86 int expires_in_seconds) OVERRIDE;
87 virtual void OnGetUserEmailResponse(const std::string& user_email) OVERRIDE;
88 virtual void OnOAuthError() OVERRIDE;
89 virtual void OnNetworkError(int response_code) OVERRIDE;
91 private:
92 void ScheduleTryReconnect();
93 void ResetAndTryReconnect();
94 void TryReconnect();
95 void OnDnsBlackholeCheckerDone(bool allow);
97 void RefreshOAuthToken();
99 XmppSignalStrategy* signal_strategy_;
100 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
101 base::Closure auth_failed_callback_;
103 scoped_ptr<OAuthCredentials> oauth_credentials_;
104 scoped_ptr<gaia::GaiaOAuthClient> gaia_oauth_client_;
105 scoped_ptr<DnsBlackholeChecker> dns_blackhole_checker_;
107 // Number of times we tried to connect without success.
108 int reconnect_attempts_;
110 bool refreshing_oauth_token_;
111 std::string oauth_access_token_;
112 base::Time auth_token_expiry_time_;
114 base::OneShotTimer<SignalingConnector> timer_;
116 DISALLOW_COPY_AND_ASSIGN(SignalingConnector);
119 } // namespace remoting
121 #endif // REMOTING_HOST_SIGNALING_CONNECTOR_H_