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