Upstream version 10.39.233.0
[platform/framework/web/crosswalk.git] / src / remoting / host / desktop_session_agent.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_DESKTOP_SESSION_AGENT_H_
6 #define REMOTING_HOST_DESKTOP_SESSION_AGENT_H_
7
8 #include <map>
9
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "ipc/ipc_listener.h"
17 #include "ipc/ipc_platform_file.h"
18 #include "remoting/host/client_session_control.h"
19 #include "remoting/protocol/clipboard_stub.h"
20 #include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h"
21 #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h"
22 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor_monitor.h"
23
24 namespace IPC {
25 class ChannelProxy;
26 class Message;
27 }  // namespace IPC
28
29 namespace remoting {
30
31 class AudioCapturer;
32 class AudioPacket;
33 class AutoThreadTaskRunner;
34 class DesktopEnvironment;
35 class DesktopEnvironmentFactory;
36 class InputInjector;
37 class RemoteInputFilter;
38 class ScreenControls;
39 class ScreenResolution;
40
41 namespace protocol {
42 class InputEventTracker;
43 }  // namespace protocol
44
45 // Provides screen/audio capturing and input injection services for
46 // the network process.
47 class DesktopSessionAgent
48     : public base::RefCountedThreadSafe<DesktopSessionAgent>,
49       public IPC::Listener,
50       public webrtc::DesktopCapturer::Callback,
51       public webrtc::MouseCursorMonitor::Callback,
52       public ClientSessionControl {
53  public:
54   class Delegate {
55    public:
56     virtual ~Delegate();
57
58     // Returns an instance of desktop environment factory used.
59     virtual DesktopEnvironmentFactory& desktop_environment_factory() = 0;
60
61     // Notifies the delegate that the network-to-desktop channel has been
62     // disconnected.
63     virtual void OnNetworkProcessDisconnected() = 0;
64   };
65
66   DesktopSessionAgent(
67       scoped_refptr<AutoThreadTaskRunner> audio_capture_task_runner,
68       scoped_refptr<AutoThreadTaskRunner> caller_task_runner,
69       scoped_refptr<AutoThreadTaskRunner> input_task_runner,
70       scoped_refptr<AutoThreadTaskRunner> io_task_runner,
71       scoped_refptr<AutoThreadTaskRunner> video_capture_task_runner);
72
73   // IPC::Listener implementation.
74   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
75   virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
76   virtual void OnChannelError() OVERRIDE;
77
78   // webrtc::DesktopCapturer::Callback implementation.
79   virtual webrtc::SharedMemory* CreateSharedMemory(size_t size) OVERRIDE;
80   virtual void OnCaptureCompleted(webrtc::DesktopFrame* frame) OVERRIDE;
81
82   // webrtc::MouseCursorMonitor::Callback implementation.
83   virtual void OnMouseCursor(webrtc::MouseCursor* cursor) OVERRIDE;
84   virtual void OnMouseCursorPosition(
85       webrtc::MouseCursorMonitor::CursorState state,
86       const webrtc::DesktopVector& position) OVERRIDE;
87
88   // Forwards a local clipboard event though the IPC channel to the network
89   // process.
90   void InjectClipboardEvent(const protocol::ClipboardEvent& event);
91
92   // Forwards an audio packet though the IPC channel to the network process.
93   void ProcessAudioPacket(scoped_ptr<AudioPacket> packet);
94
95   // Creates desktop integration components and a connected IPC channel to be
96   // used to access them. The client end of the channel is returned in
97   // the variable pointed by |desktop_pipe_out|.
98   bool Start(const base::WeakPtr<Delegate>& delegate,
99              IPC::PlatformFileForTransit* desktop_pipe_out);
100
101   // Stops the agent asynchronously.
102   void Stop();
103
104  protected:
105   friend class base::RefCountedThreadSafe<DesktopSessionAgent>;
106
107   virtual ~DesktopSessionAgent();
108
109   // ClientSessionControl interface.
110   virtual const std::string& client_jid() const OVERRIDE;
111   virtual void DisconnectSession() OVERRIDE;
112   virtual void OnLocalMouseMoved(
113     const webrtc::DesktopVector& position) OVERRIDE;
114   virtual void SetDisableInputs(bool disable_inputs) OVERRIDE;
115   virtual void ResetVideoPipeline() OVERRIDE;
116
117   // Handles StartSessionAgent request from the client.
118   void OnStartSessionAgent(const std::string& authenticated_jid,
119                            const ScreenResolution& resolution,
120                            bool virtual_terminal);
121
122   // Handles CaptureFrame requests from the client.
123   void OnCaptureFrame();
124
125   // Handles SharedBufferCreated notification from the client.
126   void OnSharedBufferCreated(int id);
127
128   // Handles event executor requests from the client.
129   void OnInjectClipboardEvent(const std::string& serialized_event);
130   void OnInjectKeyEvent(const std::string& serialized_event);
131   void OnInjectTextEvent(const std::string& serialized_event);
132   void OnInjectMouseEvent(const std::string& serialized_event);
133
134   // Handles ChromotingNetworkDesktopMsg_SetScreenResolution request from
135   // the client.
136   void SetScreenResolution(const ScreenResolution& resolution);
137
138   // Sends a message to the network process.
139   void SendToNetwork(IPC::Message* message);
140
141   // Posted to |audio_capture_task_runner_| to start the audio capturer.
142   void StartAudioCapturer();
143
144   // Posted to |audio_capture_task_runner_| to stop the audio capturer.
145   void StopAudioCapturer();
146
147   // Posted to |video_capture_task_runner_| to start the video capturer and the
148   // mouse cursor monitor.
149   void StartVideoCapturerAndMouseMonitor();
150
151   // Posted to |video_capture_task_runner_| to stop the video capturer and the
152   // mouse cursor monitor.
153   void StopVideoCapturerAndMouseMonitor();
154
155  private:
156   class SharedBuffer;
157   friend class SharedBuffer;
158
159   // Called by SharedBuffer when it's destroyed.
160   void OnSharedBufferDeleted(int id);
161
162   // Task runner dedicated to running methods of |audio_capturer_|.
163   scoped_refptr<AutoThreadTaskRunner> audio_capture_task_runner_;
164
165   // Task runner on which public methods of this class should be called.
166   scoped_refptr<AutoThreadTaskRunner> caller_task_runner_;
167
168   // Task runner on which keyboard/mouse input is injected.
169   scoped_refptr<AutoThreadTaskRunner> input_task_runner_;
170
171   // Task runner used by the IPC channel.
172   scoped_refptr<AutoThreadTaskRunner> io_task_runner_;
173
174   // Task runner dedicated to running methods of |video_capturer_|.
175   scoped_refptr<AutoThreadTaskRunner> video_capture_task_runner_;
176
177   // Captures audio output.
178   scoped_ptr<AudioCapturer> audio_capturer_;
179
180   std::string client_jid_;
181
182   base::WeakPtr<Delegate> delegate_;
183
184   // The DesktopEnvironment instance used by this agent.
185   scoped_ptr<DesktopEnvironment> desktop_environment_;
186
187   // Executes keyboard, mouse and clipboard events.
188   scoped_ptr<InputInjector> input_injector_;
189
190   // Tracker used to release pressed keys and buttons when disconnecting.
191   scoped_ptr<protocol::InputEventTracker> input_tracker_;
192
193   // Filter used to disable remote inputs during local input activity.
194   scoped_ptr<RemoteInputFilter> remote_input_filter_;
195
196   // Used to apply client-requested changes in screen resolution.
197   scoped_ptr<ScreenControls> screen_controls_;
198
199   // IPC channel connecting the desktop process with the network process.
200   scoped_ptr<IPC::ChannelProxy> network_channel_;
201
202   // The client end of the network-to-desktop pipe. It is kept alive until
203   // the network process connects to the pipe.
204   base::File desktop_pipe_;
205
206   // Size of the most recent captured video frame.
207   webrtc::DesktopSize current_size_;
208
209   // Next shared buffer ID to be used.
210   int next_shared_buffer_id_;
211
212   // The number of currently allocated shared buffers.
213   int shared_buffers_;
214
215   // True if the desktop session agent has been started.
216   bool started_;
217
218   // Captures the screen.
219   scoped_ptr<webrtc::DesktopCapturer> video_capturer_;
220
221   // Captures mouse shapes.
222   scoped_ptr<webrtc::MouseCursorMonitor> mouse_cursor_monitor_;
223
224   // Keep reference to the last frame sent to make sure shared buffer is alive
225   // before it's received.
226   scoped_ptr<webrtc::DesktopFrame> last_frame_;
227
228   // Used to disable callbacks to |this|.
229   base::WeakPtrFactory<DesktopSessionAgent> weak_factory_;
230
231   DISALLOW_COPY_AND_ASSIGN(DesktopSessionAgent);
232 };
233
234 }  // namespace remoting
235
236 #endif  // REMOTING_HOST_DESKTOP_SESSION_AGENT_H_