Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / remoting / host / client_session.h
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 disable callbacks to |this| once DisconnectSession() has been
177   // called.
178   base::WeakPtrFactory<ClientSessionControl> control_factory_;
179
180   // Used to create a DesktopEnvironment instance for this session.
181   DesktopEnvironmentFactory* desktop_environment_factory_;
182
183   // The DesktopEnvironment instance for this session.
184   scoped_ptr<DesktopEnvironment> desktop_environment_;
185
186   // Filter used as the final element in the input pipeline.
187   protocol::InputFilter host_input_filter_;
188
189   // Tracker used to release pressed keys and buttons when disconnecting.
190   protocol::InputEventTracker input_tracker_;
191
192   // Filter used to disable remote inputs during local input activity.
193   RemoteInputFilter remote_input_filter_;
194
195   // Filter used to clamp mouse events to the current display dimensions.
196   MouseClampingFilter mouse_clamping_filter_;
197
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)
200   // pipeline.
201   protocol::ClipboardEchoFilter clipboard_echo_filter_;
202
203   // Filters used to manage enabling & disabling of input & clipboard.
204   protocol::InputFilter disable_input_filter_;
205   protocol::ClipboardFilter disable_clipboard_filter_;
206
207   // Filters used to disable input & clipboard when we're not authenticated.
208   protocol::InputFilter auth_input_filter_;
209   protocol::ClipboardFilter auth_clipboard_filter_;
210
211   // Factory for weak pointers to the client clipboard stub.
212   // This must appear after |clipboard_echo_filter_|, so that it won't outlive
213   // it.
214   base::WeakPtrFactory<protocol::ClipboardStub> client_clipboard_factory_;
215
216   // The maximum duration of this session.
217   // There is no maximum if this value is <= 0.
218   base::TimeDelta max_duration_;
219
220   // A timer that triggers a disconnect when the maximum session duration
221   // is reached.
222   base::OneShotTimer<ClientSession> max_duration_timer_;
223
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_;
230
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_;
236
237   // The set of all capabilities supported by the client.
238   scoped_ptr<std::string> client_capabilities_;
239
240   // The set of all capabilities supported by the host.
241   std::string host_capabilities_;
242
243   // The set of all capabilities negotiated between client and host.
244   std::string capabilities_;
245
246   // Used to inject mouse and keyboard input and handle clipboard events.
247   scoped_ptr<InputInjector> input_injector_;
248
249   // Used to apply client-requested changes in screen resolution.
250   scoped_ptr<ScreenControls> screen_controls_;
251
252   // The pairing registry for PIN-less authentication.
253   scoped_refptr<protocol::PairingRegistry> pairing_registry_;
254
255   // Used to proxy gnubby auth traffic.
256   scoped_ptr<GnubbyAuthHandler> gnubby_auth_handler_;
257
258   // Used to manage extension functionality.
259   scoped_ptr<HostExtensionSessionManager> extension_manager_;
260
261   // Used to store video channel pause & lossless parameters.
262   bool pause_video_;
263   bool lossless_video_encode_;
264   bool lossless_video_color_;
265
266   DISALLOW_COPY_AND_ASSIGN(ClientSession);
267 };
268
269 }  // namespace remoting
270
271 #endif  // REMOTING_HOST_CLIENT_SESSION_H_