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_CLIENT_SESSION_H_
6 #define REMOTING_HOST_CLIENT_SESSION_H_
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"
33 class SingleThreadTaskRunner
;
40 class DesktopEnvironment
;
41 class DesktopEnvironmentFactory
;
47 // A ClientSession keeps a reference to a connection to a client, and maintains
50 : public base::NonThreadSafe
,
51 public protocol::HostStub
,
52 public protocol::ConnectionToClient::EventHandler
,
53 public ClientSessionControl
{
55 // Callback interface for passing events to the ChromotingHost.
58 // Called after authentication has started.
59 virtual void OnSessionAuthenticating(ClientSession
* client
) = 0;
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;
65 // Called after we've finished connecting all channels.
66 virtual void OnSessionChannelsConnected(ClientSession
* client
) = 0;
68 // Called after authentication has failed. Must not tear down this
69 // object. OnSessionClosed() is notified after this handler
71 virtual void OnSessionAuthenticationFailed(ClientSession
* client
) = 0;
73 // Called after connection has failed or after the client closed it.
74 virtual void OnSessionClosed(ClientSession
* client
) = 0;
76 // Called on notification of a route change event, when a channel is
78 virtual void OnSessionRouteChange(
79 ClientSession
* client
,
80 const std::string
& channel_name
,
81 const protocol::TransportRoute
& route
) = 0;
84 virtual ~EventHandler() {}
87 // |event_handler| and |desktop_environment_factory| must outlive |this|.
88 // All |HostExtension|s in |extensions| must outlive |this|.
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();
104 // Returns the set of capabilities negotiated between client and host.
105 const std::string
& capabilities() const { return capabilities_
; }
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
;
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
;
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
;
145 void SetGnubbyAuthHandlerForTesting(GnubbyAuthHandler
* gnubby_auth_handler
);
147 protocol::ConnectionToClient
* connection() const {
148 return connection_
.get();
151 bool is_authenticated() { return auth_input_filter_
.enabled(); }
153 const std::string
* client_capabilities() const {
154 return client_capabilities_
.get();
158 // Creates a proxy for sending clipboard events to the client.
159 scoped_ptr
<protocol::ClipboardStub
> CreateClipboardProxy();
161 // Creates an audio encoder for the specified configuration.
162 static scoped_ptr
<AudioEncoder
> CreateAudioEncoder(
163 const protocol::SessionConfig
& config
);
165 // Creates a video encoder for the specified configuration.
166 static scoped_ptr
<VideoEncoder
> CreateVideoEncoder(
167 const protocol::SessionConfig
& config
);
169 EventHandler
* event_handler_
;
171 // The connection to the client.
172 scoped_ptr
<protocol::ConnectionToClient
> connection_
;
174 std::string client_jid_
;
176 // Used to disable callbacks to |this| once DisconnectSession() has been
178 base::WeakPtrFactory
<ClientSessionControl
> control_factory_
;
180 // Used to create a DesktopEnvironment instance for this session.
181 DesktopEnvironmentFactory
* desktop_environment_factory_
;
183 // The DesktopEnvironment instance for this session.
184 scoped_ptr
<DesktopEnvironment
> desktop_environment_
;
186 // Filter used as the final element in the input pipeline.
187 protocol::InputFilter host_input_filter_
;
189 // Tracker used to release pressed keys and buttons when disconnecting.
190 protocol::InputEventTracker input_tracker_
;
192 // Filter used to disable remote inputs during local input activity.
193 RemoteInputFilter remote_input_filter_
;
195 // Filter used to clamp mouse events to the current display dimensions.
196 MouseClampingFilter mouse_clamping_filter_
;
198 // Filter to used to stop clipboard items sent from the client being echoed
199 // back to it. It is the final element in the clipboard (client -> host)
201 protocol::ClipboardEchoFilter clipboard_echo_filter_
;
203 // Filters used to manage enabling & disabling of input & clipboard.
204 protocol::InputFilter disable_input_filter_
;
205 protocol::ClipboardFilter disable_clipboard_filter_
;
207 // Filters used to disable input & clipboard when we're not authenticated.
208 protocol::InputFilter auth_input_filter_
;
209 protocol::ClipboardFilter auth_clipboard_filter_
;
211 // Factory for weak pointers to the client clipboard stub.
212 // This must appear after |clipboard_echo_filter_|, so that it won't outlive
214 base::WeakPtrFactory
<protocol::ClipboardStub
> client_clipboard_factory_
;
216 // The maximum duration of this session.
217 // There is no maximum if this value is <= 0.
218 base::TimeDelta max_duration_
;
220 // A timer that triggers a disconnect when the maximum session duration
222 base::OneShotTimer
<ClientSession
> max_duration_timer_
;
224 scoped_refptr
<base::SingleThreadTaskRunner
> audio_task_runner_
;
225 scoped_refptr
<base::SingleThreadTaskRunner
> input_task_runner_
;
226 scoped_refptr
<base::SingleThreadTaskRunner
> video_capture_task_runner_
;
227 scoped_refptr
<base::SingleThreadTaskRunner
> video_encode_task_runner_
;
228 scoped_refptr
<base::SingleThreadTaskRunner
> network_task_runner_
;
229 scoped_refptr
<base::SingleThreadTaskRunner
> ui_task_runner_
;
231 // Schedulers for audio and video capture.
232 // |video_scheduler_| may be NULL if the video channel is not required - see
233 // ResetVideoPipeline().
234 scoped_refptr
<AudioScheduler
> audio_scheduler_
;
235 scoped_refptr
<VideoScheduler
> video_scheduler_
;
237 // The set of all capabilities supported by the client.
238 scoped_ptr
<std::string
> client_capabilities_
;
240 // The set of all capabilities supported by the host.
241 std::string host_capabilities_
;
243 // The set of all capabilities negotiated between client and host.
244 std::string capabilities_
;
246 // Used to inject mouse and keyboard input and handle clipboard events.
247 scoped_ptr
<InputInjector
> input_injector_
;
249 // Used to apply client-requested changes in screen resolution.
250 scoped_ptr
<ScreenControls
> screen_controls_
;
252 // The pairing registry for PIN-less authentication.
253 scoped_refptr
<protocol::PairingRegistry
> pairing_registry_
;
255 // Used to proxy gnubby auth traffic.
256 scoped_ptr
<GnubbyAuthHandler
> gnubby_auth_handler_
;
258 // Used to manage extension functionality.
259 scoped_ptr
<HostExtensionSessionManager
> extension_manager_
;
261 // Used to store video channel pause & lossless parameters.
263 bool lossless_video_encode_
;
264 bool lossless_video_color_
;
266 DISALLOW_COPY_AND_ASSIGN(ClientSession
);
269 } // namespace remoting
271 #endif // REMOTING_HOST_CLIENT_SESSION_H_