Bring the browser window to the front when clicking on deprecated accelerator notific...
[chromium-blink-merge.git] / remoting / host / desktop_session_proxy.h
blobbae2a80472e706f48b711dec4f288169dd32193f
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.
5 #ifndef REMOTING_HOST_DESKTOP_SESSION_PROXY_H_
6 #define REMOTING_HOST_DESKTOP_SESSION_PROXY_H_
8 #include <map>
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/process/process.h"
15 #include "base/sequenced_task_runner_helpers.h"
16 #include "ipc/ipc_listener.h"
17 #include "ipc/ipc_platform_file.h"
18 #include "remoting/host/audio_capturer.h"
19 #include "remoting/host/desktop_environment.h"
20 #include "remoting/host/screen_resolution.h"
21 #include "remoting/proto/event.pb.h"
22 #include "remoting/protocol/clipboard_stub.h"
23 #include "third_party/webrtc/modules/desktop_capture/screen_capturer.h"
25 namespace base {
26 class SingleThreadTaskRunner;
27 } // namespace base
29 namespace IPC {
30 class ChannelProxy;
31 class Message;
32 } // namespace IPC
34 namespace webrtc {
35 class MouseCursor;
36 } // namespace webrtc
38 struct SerializedDesktopFrame;
40 namespace remoting {
42 class AudioPacket;
43 class ClientSession;
44 class ClientSessionControl;
45 class DesktopSessionConnector;
46 struct DesktopSessionProxyTraits;
47 class IpcAudioCapturer;
48 class IpcMouseCursorMonitor;
49 class IpcVideoFrameCapturer;
50 class ScreenControls;
52 // DesktopSessionProxy is created by an owning DesktopEnvironment to route
53 // requests from stubs to the DesktopSessionAgent instance through
54 // the IPC channel. DesktopSessionProxy is owned both by the DesktopEnvironment
55 // and the stubs, since stubs can out-live their DesktopEnvironment.
57 // DesktopSessionProxy objects are ref-counted but are always deleted on
58 // the |caller_tast_runner_| thread. This makes it possible to continue
59 // to receive IPC messages after the ref-count has dropped to zero, until
60 // the proxy is deleted. DesktopSessionProxy must therefore avoid creating new
61 // references to the itself while handling IPC messages and desktop
62 // attach/detach notifications.
64 // All public methods of DesktopSessionProxy are called on
65 // the |caller_task_runner_| thread unless it is specified otherwise.
66 class DesktopSessionProxy
67 : public base::RefCountedThreadSafe<DesktopSessionProxy,
68 DesktopSessionProxyTraits>,
69 public IPC::Listener {
70 public:
71 DesktopSessionProxy(
72 scoped_refptr<base::SingleThreadTaskRunner> audio_capture_task_runner,
73 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner,
74 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
75 scoped_refptr<base::SingleThreadTaskRunner> video_capture_task_runner,
76 base::WeakPtr<ClientSessionControl> client_session_control,
77 base::WeakPtr<DesktopSessionConnector> desktop_session_connector,
78 bool virtual_terminal,
79 bool supports_touch_events);
81 // Mirrors DesktopEnvironment.
82 scoped_ptr<AudioCapturer> CreateAudioCapturer();
83 scoped_ptr<InputInjector> CreateInputInjector();
84 scoped_ptr<ScreenControls> CreateScreenControls();
85 scoped_ptr<webrtc::DesktopCapturer> CreateVideoCapturer();
86 scoped_ptr<webrtc::MouseCursorMonitor> CreateMouseCursorMonitor();
87 std::string GetCapabilities() const;
88 void SetCapabilities(const std::string& capabilities);
90 // IPC::Listener implementation.
91 bool OnMessageReceived(const IPC::Message& message) override;
92 void OnChannelConnected(int32 peer_pid) override;
93 void OnChannelError() override;
95 // Connects to the desktop session agent.
96 bool AttachToDesktop(base::Process desktop_process,
97 IPC::PlatformFileForTransit desktop_pipe);
99 // Closes the connection to the desktop session agent and cleans up
100 // the associated resources.
101 void DetachFromDesktop();
103 // Disconnects the client session that owns |this|.
104 void DisconnectSession();
106 // Stores |audio_capturer| to be used to post captured audio packets. Called
107 // on the |audio_capture_task_runner_| thread.
108 void SetAudioCapturer(const base::WeakPtr<IpcAudioCapturer>& audio_capturer);
110 // APIs used to implement the webrtc::DesktopCapturer interface. These must be
111 // called on the |video_capture_task_runner_| thread.
112 void CaptureFrame();
114 // Stores |video_capturer| to be used to post captured video frames. Called on
115 // the |video_capture_task_runner_| thread.
116 void SetVideoCapturer(
117 const base::WeakPtr<IpcVideoFrameCapturer> video_capturer);
119 // Stores |mouse_cursor_monitor| to be used to post mouse cursor changes.
120 // Called on the |video_capture_task_runner_| thread.
121 void SetMouseCursorMonitor(
122 const base::WeakPtr<IpcMouseCursorMonitor>& mouse_cursor_monitor);
124 // APIs used to implement the InputInjector interface.
125 void InjectClipboardEvent(const protocol::ClipboardEvent& event);
126 void InjectKeyEvent(const protocol::KeyEvent& event);
127 void InjectTextEvent(const protocol::TextEvent& event);
128 void InjectMouseEvent(const protocol::MouseEvent& event);
129 void InjectTouchEvent(const protocol::TouchEvent& event);
130 void StartInputInjector(scoped_ptr<protocol::ClipboardStub> client_clipboard);
132 // API used to implement the SessionController interface.
133 void SetScreenResolution(const ScreenResolution& resolution);
135 private:
136 friend class base::DeleteHelper<DesktopSessionProxy>;
137 friend struct DesktopSessionProxyTraits;
139 class IpcSharedBufferCore;
140 class IpcSharedBuffer;
141 typedef std::map<int, scoped_refptr<IpcSharedBufferCore> > SharedBuffers;
143 ~DesktopSessionProxy() override;
145 // Returns a shared buffer from the list of known buffers.
146 scoped_refptr<IpcSharedBufferCore> GetSharedBufferCore(int id);
148 // Handles AudioPacket notification from the desktop session agent.
149 void OnAudioPacket(const std::string& serialized_packet);
151 // Registers a new shared buffer created by the desktop process.
152 void OnCreateSharedBuffer(int id,
153 IPC::PlatformFileForTransit handle,
154 uint32 size);
156 // Drops a cached reference to the shared buffer.
157 void OnReleaseSharedBuffer(int id);
159 // Handles CaptureCompleted notification from the desktop session agent.
160 void OnCaptureCompleted(const SerializedDesktopFrame& serialized_frame);
162 // Handles MouseCursor notification from the desktop session agent.
163 void OnMouseCursor(const webrtc::MouseCursor& mouse_cursor);
165 // Handles InjectClipboardEvent request from the desktop integration process.
166 void OnInjectClipboardEvent(const std::string& serialized_event);
168 // Posts OnCaptureCompleted() to |video_capturer_| on the video thread,
169 // passing |frame|.
170 void PostCaptureCompleted(scoped_ptr<webrtc::DesktopFrame> frame);
172 // Posts OnMouseCursor() to |mouse_cursor_monitor_| on the video thread,
173 // passing |mouse_cursor|.
174 void PostMouseCursor(scoped_ptr<webrtc::MouseCursor> mouse_cursor);
176 // Sends a message to the desktop session agent. The message is silently
177 // deleted if the channel is broken.
178 void SendToDesktop(IPC::Message* message);
180 // Task runners:
181 // - |audio_capturer_| is called back on |audio_capture_task_runner_|.
182 // - public methods of this class (with some exceptions) are called on
183 // |caller_task_runner_|.
184 // - background I/O is served on |io_task_runner_|.
185 // - |video_capturer_| is called back on |video_capture_task_runner_|.
186 scoped_refptr<base::SingleThreadTaskRunner> audio_capture_task_runner_;
187 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_;
188 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
189 scoped_refptr<base::SingleThreadTaskRunner> video_capture_task_runner_;
191 // Points to the audio capturer receiving captured audio packets.
192 base::WeakPtr<IpcAudioCapturer> audio_capturer_;
194 // Points to the client stub passed to StartInputInjector().
195 scoped_ptr<protocol::ClipboardStub> client_clipboard_;
197 // Used to disconnect the client session.
198 base::WeakPtr<ClientSessionControl> client_session_control_;
200 // Used to create a desktop session and receive notifications every time
201 // the desktop process is replaced.
202 base::WeakPtr<DesktopSessionConnector> desktop_session_connector_;
204 // Points to the video capturer receiving captured video frames.
205 base::WeakPtr<IpcVideoFrameCapturer> video_capturer_;
207 // Points to the mouse cursor monitor receiving mouse cursor changes.
208 base::WeakPtr<IpcMouseCursorMonitor> mouse_cursor_monitor_;
210 // IPC channel to the desktop session agent.
211 scoped_ptr<IPC::ChannelProxy> desktop_channel_;
213 // Handle of the desktop process.
214 base::Process desktop_process_;
216 int pending_capture_frame_requests_;
218 // Shared memory buffers by Id. Each buffer is owned by the corresponding
219 // frame.
220 SharedBuffers shared_buffers_;
222 // Keeps the desired screen resolution so it can be passed to a newly attached
223 // desktop session agent.
224 ScreenResolution screen_resolution_;
226 // True if |this| has been connected to the desktop session.
227 bool is_desktop_session_connected_;
229 bool virtual_terminal_;
231 // True if touch events are supported by the desktop session.
232 bool supports_touch_events_;
234 DISALLOW_COPY_AND_ASSIGN(DesktopSessionProxy);
237 // Destroys |DesktopSessionProxy| instances on the caller's thread.
238 struct DesktopSessionProxyTraits {
239 static void Destruct(const DesktopSessionProxy* desktop_session_proxy);
242 } // namespace remoting
244 #endif // REMOTING_HOST_DESKTOP_SESSION_PROXY_H_