38f97537bcdf9cfab295d77893bef6b9a09105b9
[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   ~ClientSession() override;
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   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;
113   void RequestPairing(
114       const remoting::protocol::PairingRequest& pairing_request) override;
115   void DeliverClientMessage(const protocol::ExtensionMessage& message) override;
116
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;
131
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;
138
139   void SetGnubbyAuthHandlerForTesting(GnubbyAuthHandler* gnubby_auth_handler);
140
141   protocol::ConnectionToClient* connection() const {
142     return connection_.get();
143   }
144
145   bool is_authenticated() { return auth_input_filter_.enabled();  }
146
147   const std::string* client_capabilities() const {
148     return client_capabilities_.get();
149   }
150
151  private:
152   // Creates a proxy for sending clipboard events to the client.
153   scoped_ptr<protocol::ClipboardStub> CreateClipboardProxy();
154
155   // Creates an audio encoder for the specified configuration.
156   static scoped_ptr<AudioEncoder> CreateAudioEncoder(
157       const protocol::SessionConfig& config);
158
159   // Creates a video encoder for the specified configuration.
160   static scoped_ptr<VideoEncoder> CreateVideoEncoder(
161       const protocol::SessionConfig& config);
162
163   EventHandler* event_handler_;
164
165   // The connection to the client.
166   scoped_ptr<protocol::ConnectionToClient> connection_;
167
168   std::string client_jid_;
169
170   // Used to create a DesktopEnvironment instance for this session.
171   DesktopEnvironmentFactory* desktop_environment_factory_;
172
173   // The DesktopEnvironment instance for this session.
174   scoped_ptr<DesktopEnvironment> desktop_environment_;
175
176   // Filter used as the final element in the input pipeline.
177   protocol::InputFilter host_input_filter_;
178
179   // Tracker used to release pressed keys and buttons when disconnecting.
180   protocol::InputEventTracker input_tracker_;
181
182   // Filter used to disable remote inputs during local input activity.
183   RemoteInputFilter remote_input_filter_;
184
185   // Filter used to clamp mouse events to the current display dimensions.
186   MouseClampingFilter mouse_clamping_filter_;
187
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)
190   // pipeline.
191   protocol::ClipboardEchoFilter clipboard_echo_filter_;
192
193   // Filters used to manage enabling & disabling of input & clipboard.
194   protocol::InputFilter disable_input_filter_;
195   protocol::ClipboardFilter disable_clipboard_filter_;
196
197   // Filters used to disable input & clipboard when we're not authenticated.
198   protocol::InputFilter auth_input_filter_;
199   protocol::ClipboardFilter auth_clipboard_filter_;
200
201   // Factory for weak pointers to the client clipboard stub.
202   // This must appear after |clipboard_echo_filter_|, so that it won't outlive
203   // it.
204   base::WeakPtrFactory<protocol::ClipboardStub> client_clipboard_factory_;
205
206   // The maximum duration of this session.
207   // There is no maximum if this value is <= 0.
208   base::TimeDelta max_duration_;
209
210   // A timer that triggers a disconnect when the maximum session duration
211   // is reached.
212   base::OneShotTimer<ClientSession> max_duration_timer_;
213
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_;
220
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_;
226
227   // The set of all capabilities supported by the client.
228   scoped_ptr<std::string> client_capabilities_;
229
230   // The set of all capabilities supported by the host.
231   std::string host_capabilities_;
232
233   // The set of all capabilities negotiated between client and host.
234   std::string capabilities_;
235
236   // Used to inject mouse and keyboard input and handle clipboard events.
237   scoped_ptr<InputInjector> input_injector_;
238
239   // Used to apply client-requested changes in screen resolution.
240   scoped_ptr<ScreenControls> screen_controls_;
241
242   // The pairing registry for PIN-less authentication.
243   scoped_refptr<protocol::PairingRegistry> pairing_registry_;
244
245   // Used to proxy gnubby auth traffic.
246   scoped_ptr<GnubbyAuthHandler> gnubby_auth_handler_;
247
248   // Used to manage extension functionality.
249   scoped_ptr<HostExtensionSessionManager> extension_manager_;
250
251   // Used to store video channel pause & lossless parameters.
252   bool pause_video_;
253   bool lossless_video_encode_;
254   bool lossless_video_color_;
255
256   // Used to disable callbacks to |this| once DisconnectSession() has been
257   // called.
258   base::WeakPtrFactory<ClientSessionControl> weak_factory_;
259
260   DISALLOW_COPY_AND_ASSIGN(ClientSession);
261 };
262
263 }  // namespace remoting
264
265 #endif  // REMOTING_HOST_CLIENT_SESSION_H_