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_CLIENT_SESSION_H_ 6 #define REMOTING_HOST_CLIENT_SESSION_H_ 7 8 #include <string> 9 10 #include "base/memory/ref_counted.h" 11 #include "base/memory/weak_ptr.h" 12 #include "base/sequenced_task_runner_helpers.h" 13 #include "base/threading/non_thread_safe.h" 14 #include "base/time/time.h" 15 #include "base/timer/timer.h" 16 #include "remoting/host/client_session_control.h" 17 #include "remoting/host/gnubby_auth_handler.h" 18 #include "remoting/host/host_extension_session_manager.h" 19 #include "remoting/host/mouse_clamping_filter.h" 20 #include "remoting/host/remote_input_filter.h" 21 #include "remoting/protocol/clipboard_echo_filter.h" 22 #include "remoting/protocol/clipboard_filter.h" 23 #include "remoting/protocol/clipboard_stub.h" 24 #include "remoting/protocol/connection_to_client.h" 25 #include "remoting/protocol/host_stub.h" 26 #include "remoting/protocol/input_event_tracker.h" 27 #include "remoting/protocol/input_filter.h" 28 #include "remoting/protocol/input_stub.h" 29 #include "remoting/protocol/pairing_registry.h" 30 #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h" 31 32 namespace base { 33 class SingleThreadTaskRunner; 34 } // namespace base 35 36 namespace remoting { 37 38 class AudioEncoder; 39 class AudioScheduler; 40 class DesktopEnvironment; 41 class DesktopEnvironmentFactory; 42 class InputInjector; 43 class ScreenControls; 44 class VideoEncoder; 45 class VideoScheduler; 46 47 // A ClientSession keeps a reference to a connection to a client, and maintains 48 // per-client state. 49 class ClientSession 50 : public base::NonThreadSafe, 51 public protocol::HostStub, 52 public protocol::ConnectionToClient::EventHandler, 53 public ClientSessionControl { 54 public: 55 // Callback interface for passing events to the ChromotingHost. 56 class EventHandler { 57 public: 58 // Called after authentication has started. 59 virtual void OnSessionAuthenticating(ClientSession* client) = 0; 60 61 // Called after authentication has finished successfully. Returns true if 62 // the connection is allowed, or false otherwise. 63 virtual bool OnSessionAuthenticated(ClientSession* client) = 0; 64 65 // Called after we've finished connecting all channels. 66 virtual void OnSessionChannelsConnected(ClientSession* client) = 0; 67 68 // Called after authentication has failed. Must not tear down this 69 // object. OnSessionClosed() is notified after this handler 70 // returns. 71 virtual void OnSessionAuthenticationFailed(ClientSession* client) = 0; 72 73 // Called after connection has failed or after the client closed it. 74 virtual void OnSessionClosed(ClientSession* client) = 0; 75 76 // Called on notification of a route change event, when a channel is 77 // connected. 78 virtual void OnSessionRouteChange( 79 ClientSession* client, 80 const std::string& channel_name, 81 const protocol::TransportRoute& route) = 0; 82 83 protected: 84 virtual ~EventHandler() {} 85 }; 86 87 // |event_handler| and |desktop_environment_factory| must outlive |this|. 88 // All |HostExtension|s in |extensions| must outlive |this|. 89 ClientSession( 90 EventHandler* event_handler, 91 scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner, 92 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner, 93 scoped_refptr<base::SingleThreadTaskRunner> video_capture_task_runner, 94 scoped_refptr<base::SingleThreadTaskRunner> video_encode_task_runner, 95 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner, 96 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, 97 scoped_ptr<protocol::ConnectionToClient> connection, 98 DesktopEnvironmentFactory* desktop_environment_factory, 99 const base::TimeDelta& max_duration, 100 scoped_refptr<protocol::PairingRegistry> pairing_registry, 101 const std::vector<HostExtension*>& extensions); 102 virtual ~ClientSession(); 103 104 // Returns the set of capabilities negotiated between client and host. 105 const std::string& capabilities() const { return capabilities_; } 106 107 // protocol::HostStub interface. 108 virtual void NotifyClientResolution( 109 const protocol::ClientResolution& resolution) OVERRIDE; 110 virtual void ControlVideo( 111 const protocol::VideoControl& video_control) OVERRIDE; 112 virtual void ControlAudio( 113 const protocol::AudioControl& audio_control) OVERRIDE; 114 virtual void SetCapabilities( 115 const protocol::Capabilities& capabilities) OVERRIDE; 116 virtual void RequestPairing( 117 const remoting::protocol::PairingRequest& pairing_request) OVERRIDE; 118 virtual void DeliverClientMessage( 119 const protocol::ExtensionMessage& message) OVERRIDE; 120 121 // protocol::ConnectionToClient::EventHandler interface. 122 virtual void OnConnectionAuthenticating( 123 protocol::ConnectionToClient* connection) OVERRIDE; 124 virtual void OnConnectionAuthenticated( 125 protocol::ConnectionToClient* connection) OVERRIDE; 126 virtual void OnConnectionChannelsConnected( 127 protocol::ConnectionToClient* connection) OVERRIDE; 128 virtual void OnConnectionClosed(protocol::ConnectionToClient* connection, 129 protocol::ErrorCode error) OVERRIDE; 130 virtual void OnSequenceNumberUpdated( 131 protocol::ConnectionToClient* connection, int64 sequence_number) OVERRIDE; 132 virtual void OnRouteChange( 133 protocol::ConnectionToClient* connection, 134 const std::string& channel_name, 135 const protocol::TransportRoute& route) OVERRIDE; 136 137 // ClientSessionControl interface. 138 virtual const std::string& client_jid() const OVERRIDE; 139 virtual void DisconnectSession() OVERRIDE; 140 virtual void OnLocalMouseMoved( 141 const webrtc::DesktopVector& position) OVERRIDE; 142 virtual void SetDisableInputs(bool disable_inputs) OVERRIDE; 143 virtual void ResetVideoPipeline() OVERRIDE; 144 145 void SetGnubbyAuthHandlerForTesting(GnubbyAuthHandler* gnubby_auth_handler); 146 147 protocol::ConnectionToClient* connection() const { 148 return connection_.get(); 149 } 150 151 bool is_authenticated() { return auth_input_filter_.enabled(); } 152 153 const std::string* client_capabilities() const { 154 return client_capabilities_.get(); 155 } 156 157 private: 158 // Creates a proxy for sending clipboard events to the client. 159 scoped_ptr<protocol::ClipboardStub> CreateClipboardProxy(); 160 161 // Creates an audio encoder for the specified configuration. 162 static scoped_ptr<AudioEncoder> CreateAudioEncoder( 163 const protocol::SessionConfig& config); 164 165 // Creates a video encoder for the specified configuration. 166 static scoped_ptr<VideoEncoder> CreateVideoEncoder( 167 const protocol::SessionConfig& config); 168 169 EventHandler* event_handler_; 170 171 // The connection to the client. 172 scoped_ptr<protocol::ConnectionToClient> connection_; 173 174 std::string client_jid_; 175 176 // Used to create a DesktopEnvironment instance for this session. 177 DesktopEnvironmentFactory* desktop_environment_factory_; 178 179 // The DesktopEnvironment instance for this session. 180 scoped_ptr<DesktopEnvironment> desktop_environment_; 181 182 // Filter used as the final element in the input pipeline. 183 protocol::InputFilter host_input_filter_; 184 185 // Tracker used to release pressed keys and buttons when disconnecting. 186 protocol::InputEventTracker input_tracker_; 187 188 // Filter used to disable remote inputs during local input activity. 189 RemoteInputFilter remote_input_filter_; 190 191 // Filter used to clamp mouse events to the current display dimensions. 192 MouseClampingFilter mouse_clamping_filter_; 193 194 // Filter to used to stop clipboard items sent from the client being echoed 195 // back to it. It is the final element in the clipboard (client -> host) 196 // pipeline. 197 protocol::ClipboardEchoFilter clipboard_echo_filter_; 198 199 // Filters used to manage enabling & disabling of input & clipboard. 200 protocol::InputFilter disable_input_filter_; 201 protocol::ClipboardFilter disable_clipboard_filter_; 202 203 // Filters used to disable input & clipboard when we're not authenticated. 204 protocol::InputFilter auth_input_filter_; 205 protocol::ClipboardFilter auth_clipboard_filter_; 206 207 // Factory for weak pointers to the client clipboard stub. 208 // This must appear after |clipboard_echo_filter_|, so that it won't outlive 209 // it. 210 base::WeakPtrFactory<protocol::ClipboardStub> client_clipboard_factory_; 211 212 // The maximum duration of this session. 213 // There is no maximum if this value is <= 0. 214 base::TimeDelta max_duration_; 215 216 // A timer that triggers a disconnect when the maximum session duration 217 // is reached. 218 base::OneShotTimer<ClientSession> max_duration_timer_; 219 220 scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner_; 221 scoped_refptr<base::SingleThreadTaskRunner> input_task_runner_; 222 scoped_refptr<base::SingleThreadTaskRunner> video_capture_task_runner_; 223 scoped_refptr<base::SingleThreadTaskRunner> video_encode_task_runner_; 224 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; 225 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; 226 227 // Schedulers for audio and video capture. 228 // |video_scheduler_| may be NULL if the video channel is not required - see 229 // ResetVideoPipeline(). 230 scoped_refptr<AudioScheduler> audio_scheduler_; 231 scoped_refptr<VideoScheduler> video_scheduler_; 232 233 // The set of all capabilities supported by the client. 234 scoped_ptr<std::string> client_capabilities_; 235 236 // The set of all capabilities supported by the host. 237 std::string host_capabilities_; 238 239 // The set of all capabilities negotiated between client and host. 240 std::string capabilities_; 241 242 // Used to inject mouse and keyboard input and handle clipboard events. 243 scoped_ptr<InputInjector> input_injector_; 244 245 // Used to apply client-requested changes in screen resolution. 246 scoped_ptr<ScreenControls> screen_controls_; 247 248 // The pairing registry for PIN-less authentication. 249 scoped_refptr<protocol::PairingRegistry> pairing_registry_; 250 251 // Used to proxy gnubby auth traffic. 252 scoped_ptr<GnubbyAuthHandler> gnubby_auth_handler_; 253 254 // Used to manage extension functionality. 255 scoped_ptr<HostExtensionSessionManager> extension_manager_; 256 257 // Used to store video channel pause & lossless parameters. 258 bool pause_video_; 259 bool lossless_video_encode_; 260 bool lossless_video_color_; 261 262 // Used to disable callbacks to |this| once DisconnectSession() has been 263 // called. 264 base::WeakPtrFactory<ClientSessionControl> weak_factory_; 265 266 DISALLOW_COPY_AND_ASSIGN(ClientSession); 267 }; 268 269 } // namespace remoting 270 271 #endif // REMOTING_HOST_CLIENT_SESSION_H_ 272