Home | History | Annotate | Download | only in protocol
      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_PROTOCOL_CONNECTION_TO_HOST_H_
      6 #define REMOTING_PROTOCOL_CONNECTION_TO_HOST_H_
      7 
      8 #include <set>
      9 #include <string>
     10 
     11 #include "base/callback_forward.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/threading/non_thread_safe.h"
     15 #include "base/timer/timer.h"
     16 #include "remoting/proto/internal.pb.h"
     17 #include "remoting/protocol/clipboard_filter.h"
     18 #include "remoting/protocol/errors.h"
     19 #include "remoting/protocol/input_filter.h"
     20 #include "remoting/protocol/message_reader.h"
     21 #include "remoting/protocol/monitored_video_stub.h"
     22 #include "remoting/protocol/session.h"
     23 #include "remoting/protocol/session_config.h"
     24 #include "remoting/protocol/session_manager.h"
     25 #include "remoting/signaling/signal_strategy.h"
     26 
     27 namespace remoting {
     28 
     29 class XmppProxy;
     30 class VideoPacket;
     31 
     32 namespace protocol {
     33 
     34 class AudioReader;
     35 class AudioStub;
     36 class Authenticator;
     37 class ClientControlDispatcher;
     38 class ClientEventDispatcher;
     39 class ClientStub;
     40 class ClipboardStub;
     41 class HostStub;
     42 class InputStub;
     43 class SessionConfig;
     44 class TransportFactory;
     45 class ClientVideoDispatcher;
     46 class VideoStub;
     47 
     48 class ConnectionToHost : public SignalStrategy::Listener,
     49                          public SessionManager::Listener,
     50                          public Session::EventHandler,
     51                          public base::NonThreadSafe {
     52  public:
     53   // The UI implementations maintain corresponding definitions of this
     54   // enumeration in webapp/client_session.js and
     55   // android/java/src/org/chromium/chromoting/jni/JniInterface.java. Be sure to
     56   // update these locations if you make any changes to the ordering.
     57   enum State {
     58     INITIALIZING,
     59     CONNECTING,
     60     AUTHENTICATED,
     61     CONNECTED,
     62     FAILED,
     63     CLOSED,
     64   };
     65 
     66   class HostEventCallback {
     67    public:
     68     virtual ~HostEventCallback() {}
     69 
     70     // Called when state of the connection changes.
     71     virtual void OnConnectionState(State state, ErrorCode error) = 0;
     72 
     73     // Called when ready state of the connection changes. When |ready|
     74     // is set to false some data sent by the peers may be
     75     // delayed. This is used to indicate in the UI when connection is
     76     // temporarily broken.
     77     virtual void OnConnectionReady(bool ready) = 0;
     78 
     79     // Called when the route type (direct vs. STUN vs. proxied) changes.
     80     virtual void OnRouteChanged(const std::string& channel_name,
     81                                 const protocol::TransportRoute& route) = 0;
     82   };
     83 
     84   ConnectionToHost();
     85   virtual ~ConnectionToHost();
     86 
     87   // Allows to set a custom protocol configuration (e.g. for tests). Cannot be
     88   // called after Connect().
     89   void set_candidate_config(scoped_ptr<CandidateSessionConfig> config);
     90 
     91   // Set the stubs which will handle messages from the host.
     92   // The caller must ensure that stubs out-live the connection.
     93   // Unless otherwise specified, all stubs must be set before Connect()
     94   // is called.
     95   void set_client_stub(ClientStub* client_stub);
     96   void set_clipboard_stub(ClipboardStub* clipboard_stub);
     97   void set_video_stub(VideoStub* video_stub);
     98   // If no audio stub is specified then audio will not be requested.
     99   void set_audio_stub(AudioStub* audio_stub);
    100 
    101   // Initiates a connection to the host specified by |host_jid|.
    102   // |signal_strategy| is used to signal to the host, and must outlive the
    103   // connection. Data channels will be negotiated over |transport_factory|.
    104   // |authenticator| will be used to authenticate the session and data channels.
    105   // |event_callback| will be notified of changes in the state of the connection
    106   // and must outlive the ConnectionToHost.
    107   // Caller must set stubs (see below) before calling Connect.
    108   virtual void Connect(SignalStrategy* signal_strategy,
    109                        scoped_ptr<TransportFactory> transport_factory,
    110                        scoped_ptr<Authenticator> authenticator,
    111                        const std::string& host_jid,
    112                        HostEventCallback* event_callback);
    113 
    114   // Returns the session configuration that was negotiated with the host.
    115   virtual const SessionConfig& config();
    116 
    117   // Stubs for sending data to the host.
    118   virtual ClipboardStub* clipboard_forwarder();
    119   virtual HostStub* host_stub();
    120   virtual InputStub* input_stub();
    121 
    122   // SignalStrategy::StatusObserver interface.
    123   virtual void OnSignalStrategyStateChange(
    124       SignalStrategy::State state) OVERRIDE;
    125   virtual bool OnSignalStrategyIncomingStanza(
    126       const buzz::XmlElement* stanza) OVERRIDE;
    127 
    128   // SessionManager::Listener interface.
    129   virtual void OnSessionManagerReady() OVERRIDE;
    130   virtual void OnIncomingSession(
    131       Session* session,
    132       SessionManager::IncomingSessionResponse* response) OVERRIDE;
    133 
    134   // Session::EventHandler interface.
    135   virtual void OnSessionStateChange(Session::State state) OVERRIDE;
    136   virtual void OnSessionRouteChange(const std::string& channel_name,
    137                                     const TransportRoute& route) OVERRIDE;
    138 
    139   // MonitoredVideoStub::EventHandler interface.
    140   virtual void OnVideoChannelStatus(bool active);
    141 
    142   // Return the current state of ConnectionToHost.
    143   State state() const;
    144 
    145  private:
    146   // Callbacks for channel initialization
    147   void OnChannelInitialized(bool successful);
    148 
    149   void NotifyIfChannelsReady();
    150 
    151   void CloseOnError(ErrorCode error);
    152 
    153   // Stops writing in the channels.
    154   void CloseChannels();
    155 
    156   void SetState(State state, ErrorCode error);
    157 
    158   std::string host_jid_;
    159   std::string host_public_key_;
    160   scoped_ptr<Authenticator> authenticator_;
    161 
    162   HostEventCallback* event_callback_;
    163 
    164   scoped_ptr<CandidateSessionConfig> candidate_config_;
    165 
    166   // Stub for incoming messages.
    167   ClientStub* client_stub_;
    168   ClipboardStub* clipboard_stub_;
    169   AudioStub* audio_stub_;
    170 
    171   SignalStrategy* signal_strategy_;
    172   scoped_ptr<SessionManager> session_manager_;
    173   scoped_ptr<Session> session_;
    174   scoped_ptr<MonitoredVideoStub> monitored_video_stub_;
    175 
    176   scoped_ptr<ClientVideoDispatcher> video_dispatcher_;
    177   scoped_ptr<AudioReader> audio_reader_;
    178   scoped_ptr<ClientControlDispatcher> control_dispatcher_;
    179   scoped_ptr<ClientEventDispatcher> event_dispatcher_;
    180   ClipboardFilter clipboard_forwarder_;
    181   InputFilter event_forwarder_;
    182 
    183   // Internal state of the connection.
    184   State state_;
    185   ErrorCode error_;
    186 
    187  private:
    188   DISALLOW_COPY_AND_ASSIGN(ConnectionToHost);
    189 };
    190 
    191 }  // namespace protocol
    192 }  // namespace remoting
    193 
    194 #endif  // REMOTING_PROTOCOL_CONNECTION_TO_HOST_H_
    195