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_PROXY_H_ 6 #define REMOTING_HOST_DESKTOP_SESSION_PROXY_H_ 7 8 #include <map> 9 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" 24 25 namespace base { 26 class SingleThreadTaskRunner; 27 } // namespace base 28 29 namespace IPC { 30 class ChannelProxy; 31 class Message; 32 } // namespace IPC 33 34 struct SerializedDesktopFrame; 35 36 namespace remoting { 37 38 class AudioPacket; 39 class ClientSession; 40 class ClientSessionControl; 41 class DesktopSessionConnector; 42 struct DesktopSessionProxyTraits; 43 class IpcAudioCapturer; 44 class IpcVideoFrameCapturer; 45 class ScreenControls; 46 47 // DesktopSessionProxy is created by an owning DesktopEnvironment to route 48 // requests from stubs to the DesktopSessionAgent instance through 49 // the IPC channel. DesktopSessionProxy is owned both by the DesktopEnvironment 50 // and the stubs, since stubs can out-live their DesktopEnvironment. 51 // 52 // DesktopSessionProxy objects are ref-counted but are always deleted on 53 // the |caller_tast_runner_| thread. This makes it possible to continue 54 // to receive IPC messages after the ref-count has dropped to zero, until 55 // the proxy is deleted. DesktopSessionProxy must therefore avoid creating new 56 // references to the itself while handling IPC messages and desktop 57 // attach/detach notifications. 58 // 59 // All public methods of DesktopSessionProxy are called on 60 // the |caller_task_runner_| thread unless it is specified otherwise. 61 class DesktopSessionProxy 62 : public base::RefCountedThreadSafe<DesktopSessionProxy, 63 DesktopSessionProxyTraits>, 64 public IPC::Listener { 65 public: 66 DesktopSessionProxy( 67 scoped_refptr<base::SingleThreadTaskRunner> audio_capture_task_runner, 68 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, 69 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, 70 scoped_refptr<base::SingleThreadTaskRunner> video_capture_task_runner, 71 base::WeakPtr<ClientSessionControl> client_session_control, 72 base::WeakPtr<DesktopSessionConnector> desktop_session_connector, 73 bool virtual_terminal); 74 75 // Mirrors DesktopEnvironment. 76 scoped_ptr<AudioCapturer> CreateAudioCapturer(); 77 scoped_ptr<InputInjector> CreateInputInjector(); 78 scoped_ptr<ScreenControls> CreateScreenControls(); 79 scoped_ptr<webrtc::ScreenCapturer> CreateVideoCapturer(); 80 std::string GetCapabilities() const; 81 void SetCapabilities(const std::string& capabilities); 82 83 // IPC::Listener implementation. 84 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 85 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE; 86 virtual void OnChannelError() OVERRIDE; 87 88 // Connects to the desktop session agent. 89 bool AttachToDesktop(base::ProcessHandle desktop_process, 90 IPC::PlatformFileForTransit desktop_pipe); 91 92 // Closes the connection to the desktop session agent and cleans up 93 // the associated resources. 94 void DetachFromDesktop(); 95 96 // Disconnects the client session that owns |this|. 97 void DisconnectSession(); 98 99 // Stores |audio_capturer| to be used to post captured audio packets. Called 100 // on the |audio_capture_task_runner_| thread. 101 void SetAudioCapturer(const base::WeakPtr<IpcAudioCapturer>& audio_capturer); 102 103 // APIs used to implement the webrtc::ScreenCapturer interface. These must be 104 // called on the |video_capture_task_runner_| thread. 105 void CaptureFrame(); 106 107 // Stores |video_capturer| to be used to post captured video frames. Called on 108 // the |video_capture_task_runner_| thread. 109 void SetVideoCapturer( 110 const base::WeakPtr<IpcVideoFrameCapturer> video_capturer); 111 112 // APIs used to implement the InputInjector interface. 113 void InjectClipboardEvent(const protocol::ClipboardEvent& event); 114 void InjectKeyEvent(const protocol::KeyEvent& event); 115 void InjectMouseEvent(const protocol::MouseEvent& event); 116 void StartInputInjector(scoped_ptr<protocol::ClipboardStub> client_clipboard); 117 118 // API used to implement the SessionController interface. 119 void SetScreenResolution(const ScreenResolution& resolution); 120 121 private: 122 friend class base::DeleteHelper<DesktopSessionProxy>; 123 friend struct DesktopSessionProxyTraits; 124 125 class IpcSharedBufferCore; 126 class IpcSharedBuffer; 127 typedef std::map<int, scoped_refptr<IpcSharedBufferCore> > SharedBuffers; 128 129 virtual ~DesktopSessionProxy(); 130 131 // Returns a shared buffer from the list of known buffers. 132 scoped_refptr<IpcSharedBufferCore> GetSharedBufferCore(int id); 133 134 // Handles AudioPacket notification from the desktop session agent. 135 void OnAudioPacket(const std::string& serialized_packet); 136 137 // Registers a new shared buffer created by the desktop process. 138 void OnCreateSharedBuffer(int id, 139 IPC::PlatformFileForTransit handle, 140 uint32 size); 141 142 // Drops a cached reference to the shared buffer. 143 void OnReleaseSharedBuffer(int id); 144 145 // Handles CaptureCompleted notification from the desktop session agent. 146 void OnCaptureCompleted(const SerializedDesktopFrame& serialized_frame); 147 148 // Handles CursorShapeChanged notification from the desktop session agent. 149 void OnCursorShapeChanged(const webrtc::MouseCursorShape& cursor_shape); 150 151 // Handles InjectClipboardEvent request from the desktop integration process. 152 void OnInjectClipboardEvent(const std::string& serialized_event); 153 154 // Posts OnCaptureCompleted() to |video_capturer_| on the video thread, 155 // passing |frame|. 156 void PostCaptureCompleted(scoped_ptr<webrtc::DesktopFrame> frame); 157 158 // Posts OnCursorShapeChanged() to |video_capturer_| on the video thread, 159 // passing |cursor_shape|. 160 void PostCursorShape(scoped_ptr<webrtc::MouseCursorShape> cursor_shape); 161 162 // Sends a message to the desktop session agent. The message is silently 163 // deleted if the channel is broken. 164 void SendToDesktop(IPC::Message* message); 165 166 // Task runners: 167 // - |audio_capturer_| is called back on |audio_capture_task_runner_|. 168 // - public methods of this class (with some exceptions) are called on 169 // |caller_task_runner_|. 170 // - background I/O is served on |io_task_runner_|. 171 // - |video_capturer_| is called back on |video_capture_task_runner_|. 172 scoped_refptr<base::SingleThreadTaskRunner> audio_capture_task_runner_; 173 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_; 174 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; 175 scoped_refptr<base::SingleThreadTaskRunner> video_capture_task_runner_; 176 177 // Points to the audio capturer receiving captured audio packets. 178 base::WeakPtr<IpcAudioCapturer> audio_capturer_; 179 180 // Points to the client stub passed to StartInputInjector(). 181 scoped_ptr<protocol::ClipboardStub> client_clipboard_; 182 183 // Used to disconnect the client session. 184 base::WeakPtr<ClientSessionControl> client_session_control_; 185 186 // Used to create a desktop session and receive notifications every time 187 // the desktop process is replaced. 188 base::WeakPtr<DesktopSessionConnector> desktop_session_connector_; 189 190 // Points to the video capturer receiving captured video frames. 191 base::WeakPtr<IpcVideoFrameCapturer> video_capturer_; 192 193 // IPC channel to the desktop session agent. 194 scoped_ptr<IPC::ChannelProxy> desktop_channel_; 195 196 // Handle of the desktop process. 197 base::ProcessHandle desktop_process_; 198 199 int pending_capture_frame_requests_; 200 201 // Shared memory buffers by Id. Each buffer is owned by the corresponding 202 // frame. 203 SharedBuffers shared_buffers_; 204 205 // Keeps the desired screen resolution so it can be passed to a newly attached 206 // desktop session agent. 207 ScreenResolution screen_resolution_; 208 209 // True if |this| has been connected to the desktop session. 210 bool is_desktop_session_connected_; 211 212 bool virtual_terminal_; 213 214 DISALLOW_COPY_AND_ASSIGN(DesktopSessionProxy); 215 }; 216 217 // Destroys |DesktopSessionProxy| instances on the caller's thread. 218 struct DesktopSessionProxyTraits { 219 static void Destruct(const DesktopSessionProxy* desktop_session_proxy); 220 }; 221 222 } // namespace remoting 223 224 #endif // REMOTING_HOST_DESKTOP_SESSION_PROXY_H_ 225