• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "net/base/network_change_notifier.h"
13 #include "remoting/host/oauth_token_getter.h"
14 #include "remoting/signaling/xmpp_signal_strategy.h"
15 
16 namespace remoting {
17 
18 class DnsBlackholeChecker;
19 
20 // SignalingConnector listens for SignalStrategy status notifications
21 // and attempts to keep it connected when possible. When signalling is
22 // not connected it keeps trying to reconnect it until it is
23 // connected. It limits connection attempt rate using exponential
24 // backoff. It also monitors network state and reconnects signalling
25 // whenever connection type changes or IP address changes.
26 class SignalingConnector
27     : public base::SupportsWeakPtr<SignalingConnector>,
28       public base::NonThreadSafe,
29       public SignalStrategy::Listener,
30       public net::NetworkChangeNotifier::ConnectionTypeObserver,
31       public net::NetworkChangeNotifier::IPAddressObserver {
32  public:
33   // The |auth_failed_callback| is called when authentication fails.
34   SignalingConnector(
35       XmppSignalStrategy* signal_strategy,
36       scoped_ptr<DnsBlackholeChecker> dns_blackhole_checker,
37       const base::Closure& auth_failed_callback);
38   virtual ~SignalingConnector();
39 
40   // May be called immediately after the constructor to enable OAuth
41   // access token updating.
42   // |oauth_token_getter| must outlive SignalingConnector.
43   void EnableOAuth(OAuthTokenGetter* oauth_token_getter);
44 
45   // OAuthTokenGetter callback.
46   void OnAccessToken(OAuthTokenGetter::Status status,
47                      const std::string& user_email,
48                      const std::string& access_token);
49 
50   // SignalStrategy::Listener interface.
51   virtual void OnSignalStrategyStateChange(
52       SignalStrategy::State state) OVERRIDE;
53   virtual bool OnSignalStrategyIncomingStanza(
54       const buzz::XmlElement* stanza) OVERRIDE;
55 
56   // NetworkChangeNotifier::ConnectionTypeObserver interface.
57   virtual void OnConnectionTypeChanged(
58       net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
59 
60   // NetworkChangeNotifier::IPAddressObserver interface.
61   virtual void OnIPAddressChanged() OVERRIDE;
62 
63  private:
64   void OnNetworkError();
65   void ScheduleTryReconnect();
66   void ResetAndTryReconnect();
67   void TryReconnect();
68   void OnDnsBlackholeCheckerDone(bool allow);
69 
70   XmppSignalStrategy* signal_strategy_;
71   base::Closure auth_failed_callback_;
72   scoped_ptr<DnsBlackholeChecker> dns_blackhole_checker_;
73 
74   OAuthTokenGetter* oauth_token_getter_;
75 
76   // Number of times we tried to connect without success.
77   int reconnect_attempts_;
78 
79   base::OneShotTimer<SignalingConnector> timer_;
80 
81   DISALLOW_COPY_AND_ASSIGN(SignalingConnector);
82 };
83 
84 }  // namespace remoting
85 
86 #endif  // REMOTING_HOST_SIGNALING_CONNECTOR_H_
87