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 ~ClientSession() override
;
104 // Returns the set of capabilities negotiated between client and host.
105 const std::string
& capabilities() const { return capabilities_
; }
107 // protocol::HostStub interface.
108 void NotifyClientResolution(
109 const protocol::ClientResolution
& resolution
) override
;
110 void ControlVideo(const protocol::VideoControl
& video_control
) override
;
111 void ControlAudio(const protocol::AudioControl
& audio_control
) override
;
112 void SetCapabilities(const protocol::Capabilities
& capabilities
) override
;
114 const remoting::protocol::PairingRequest
& pairing_request
) override
;
115 void DeliverClientMessage(const protocol::ExtensionMessage
& message
) override
;
117 // protocol::ConnectionToClient::EventHandler interface.
118 void OnConnectionAuthenticating(
119 protocol::ConnectionToClient
* connection
) override
;
120 void OnConnectionAuthenticated(
121 protocol::ConnectionToClient
* connection
) override
;
122 void OnConnectionChannelsConnected(
123 protocol::ConnectionToClient
* connection
) override
;
124 void OnConnectionClosed(protocol::ConnectionToClient
* connection
,
125 protocol::ErrorCode error
) override
;
126 void OnSequenceNumberUpdated(protocol::ConnectionToClient
* connection
,
127 int64 sequence_number
) override
;
128 void OnRouteChange(protocol::ConnectionToClient
* connection
,
129 const std::string
& channel_name
,
130 const protocol::TransportRoute
& route
) override
;
132 // ClientSessionControl interface.
133 const std::string
& client_jid() const override
;
134 void DisconnectSession() override
;
135 void OnLocalMouseMoved(const webrtc::DesktopVector
& position
) override
;
136 void SetDisableInputs(bool disable_inputs
) override
;
137 void ResetVideoPipeline() override
;
139 void SetGnubbyAuthHandlerForTesting(GnubbyAuthHandler
* gnubby_auth_handler
);
141 protocol::ConnectionToClient
* connection() const {
142 return connection_
.get();
145 bool is_authenticated() { return auth_input_filter_
.enabled(); }
147 const std::string
* client_capabilities() const {
148 return client_capabilities_
.get();
152 // Creates a proxy for sending clipboard events to the client.
153 scoped_ptr
<protocol::ClipboardStub
> CreateClipboardProxy();
155 // Creates an audio encoder for the specified configuration.
156 static scoped_ptr
<AudioEncoder
> CreateAudioEncoder(
157 const protocol::SessionConfig
& config
);
159 // Creates a video encoder for the specified configuration.
160 static scoped_ptr
<VideoEncoder
> CreateVideoEncoder(
161 const protocol::SessionConfig
& config
);
163 EventHandler
* event_handler_
;
165 // The connection to the client.
166 scoped_ptr
<protocol::ConnectionToClient
> connection_
;
168 std::string client_jid_
;
170 // Used to create a DesktopEnvironment instance for this session.
171 DesktopEnvironmentFactory
* desktop_environment_factory_
;
173 // The DesktopEnvironment instance for this session.
174 scoped_ptr
<DesktopEnvironment
> desktop_environment_
;
176 // Filter used as the final element in the input pipeline.
177 protocol::InputFilter host_input_filter_
;
179 // Tracker used to release pressed keys and buttons when disconnecting.
180 protocol::InputEventTracker input_tracker_
;
182 // Filter used to disable remote inputs during local input activity.
183 RemoteInputFilter remote_input_filter_
;
185 // Filter used to clamp mouse events to the current display dimensions.
186 MouseClampingFilter mouse_clamping_filter_
;
188 // Filter to used to stop clipboard items sent from the client being echoed
189 // back to it. It is the final element in the clipboard (client -> host)
191 protocol::ClipboardEchoFilter clipboard_echo_filter_
;
193 // Filters used to manage enabling & disabling of input & clipboard.
194 protocol::InputFilter disable_input_filter_
;
195 protocol::ClipboardFilter disable_clipboard_filter_
;
197 // Filters used to disable input & clipboard when we're not authenticated.
198 protocol::InputFilter auth_input_filter_
;
199 protocol::ClipboardFilter auth_clipboard_filter_
;
201 // Factory for weak pointers to the client clipboard stub.
202 // This must appear after |clipboard_echo_filter_|, so that it won't outlive
204 base::WeakPtrFactory
<protocol::ClipboardStub
> client_clipboard_factory_
;
206 // The maximum duration of this session.
207 // There is no maximum if this value is <= 0.
208 base::TimeDelta max_duration_
;
210 // A timer that triggers a disconnect when the maximum session duration
212 base::OneShotTimer
<ClientSession
> max_duration_timer_
;
214 scoped_refptr
<base::SingleThreadTaskRunner
> audio_task_runner_
;
215 scoped_refptr
<base::SingleThreadTaskRunner
> input_task_runner_
;
216 scoped_refptr
<base::SingleThreadTaskRunner
> video_capture_task_runner_
;
217 scoped_refptr
<base::SingleThreadTaskRunner
> video_encode_task_runner_
;
218 scoped_refptr
<base::SingleThreadTaskRunner
> network_task_runner_
;
219 scoped_refptr
<base::SingleThreadTaskRunner
> ui_task_runner_
;
221 // Schedulers for audio and video capture.
222 // |video_scheduler_| may be NULL if the video channel is not required - see
223 // ResetVideoPipeline().
224 scoped_refptr
<AudioScheduler
> audio_scheduler_
;
225 scoped_refptr
<VideoScheduler
> video_scheduler_
;
227 // The set of all capabilities supported by the client.
228 scoped_ptr
<std::string
> client_capabilities_
;
230 // The set of all capabilities supported by the host.
231 std::string host_capabilities_
;
233 // The set of all capabilities negotiated between client and host.
234 std::string capabilities_
;
236 // Used to inject mouse and keyboard input and handle clipboard events.
237 scoped_ptr
<InputInjector
> input_injector_
;
239 // Used to apply client-requested changes in screen resolution.
240 scoped_ptr
<ScreenControls
> screen_controls_
;
242 // The pairing registry for PIN-less authentication.
243 scoped_refptr
<protocol::PairingRegistry
> pairing_registry_
;
245 // Used to proxy gnubby auth traffic.
246 scoped_ptr
<GnubbyAuthHandler
> gnubby_auth_handler_
;
248 // Used to manage extension functionality.
249 scoped_ptr
<HostExtensionSessionManager
> extension_manager_
;
251 // Used to store video channel pause & lossless parameters.
253 bool lossless_video_encode_
;
254 bool lossless_video_color_
;
256 // Used to disable callbacks to |this| once DisconnectSession() has been
258 base::WeakPtrFactory
<ClientSessionControl
> weak_factory_
;
260 DISALLOW_COPY_AND_ASSIGN(ClientSession
);
263 } // namespace remoting
265 #endif // REMOTING_HOST_CLIENT_SESSION_H_