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