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