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 InjectTextEvent(const protocol::TextEvent& event); 116 void InjectMouseEvent(const protocol::MouseEvent& event); 117 void StartInputInjector(scoped_ptr<protocol::ClipboardStub> client_clipboard); 118 119 // API used to implement the SessionController interface. 120 void SetScreenResolution(const ScreenResolution& resolution); 121 122 private: 123 friend class base::DeleteHelper<DesktopSessionProxy>; 124 friend struct DesktopSessionProxyTraits; 125 126 class IpcSharedBufferCore; 127 class IpcSharedBuffer; 128 typedef std::map<int, scoped_refptr<IpcSharedBufferCore> > SharedBuffers; 129 130 virtual ~DesktopSessionProxy(); 131 132 // Returns a shared buffer from the list of known buffers. 133 scoped_refptr<IpcSharedBufferCore> GetSharedBufferCore(int id); 134 135 // Handles AudioPacket notification from the desktop session agent. 136 void OnAudioPacket(const std::string& serialized_packet); 137 138 // Registers a new shared buffer created by the desktop process. 139 void OnCreateSharedBuffer(int id, 140 IPC::PlatformFileForTransit handle, 141 uint32 size); 142 143 // Drops a cached reference to the shared buffer. 144 void OnReleaseSharedBuffer(int id); 145 146 // Handles CaptureCompleted notification from the desktop session agent. 147 void OnCaptureCompleted(const SerializedDesktopFrame& serialized_frame); 148 149 // Handles CursorShapeChanged notification from the desktop session agent. 150 void OnCursorShapeChanged(const webrtc::MouseCursorShape& cursor_shape); 151 152 // Handles InjectClipboardEvent request from the desktop integration process. 153 void OnInjectClipboardEvent(const std::string& serialized_event); 154 155 // Posts OnCaptureCompleted() to |video_capturer_| on the video thread, 156 // passing |frame|. 157 void PostCaptureCompleted(scoped_ptr<webrtc::DesktopFrame> frame); 158 159 // Posts OnCursorShapeChanged() to |video_capturer_| on the video thread, 160 // passing |cursor_shape|. 161 void PostCursorShape(scoped_ptr<webrtc::MouseCursorShape> cursor_shape); 162 163 // Sends a message to the desktop session agent. The message is silently 164 // deleted if the channel is broken. 165 void SendToDesktop(IPC::Message* message); 166 167 // Task runners: 168 // - |audio_capturer_| is called back on |audio_capture_task_runner_|. 169 // - public methods of this class (with some exceptions) are called on 170 // |caller_task_runner_|. 171 // - background I/O is served on |io_task_runner_|. 172 // - |video_capturer_| is called back on |video_capture_task_runner_|. 173 scoped_refptr<base::SingleThreadTaskRunner> audio_capture_task_runner_; 174 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_; 175 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; 176 scoped_refptr<base::SingleThreadTaskRunner> video_capture_task_runner_; 177 178 // Points to the audio capturer receiving captured audio packets. 179 base::WeakPtr<IpcAudioCapturer> audio_capturer_; 180 181 // Points to the client stub passed to StartInputInjector(). 182 scoped_ptr<protocol::ClipboardStub> client_clipboard_; 183 184 // Used to disconnect the client session. 185 base::WeakPtr<ClientSessionControl> client_session_control_; 186 187 // Used to create a desktop session and receive notifications every time 188 // the desktop process is replaced. 189 base::WeakPtr<DesktopSessionConnector> desktop_session_connector_; 190 191 // Points to the video capturer receiving captured video frames. 192 base::WeakPtr<IpcVideoFrameCapturer> video_capturer_; 193 194 // IPC channel to the desktop session agent. 195 scoped_ptr<IPC::ChannelProxy> desktop_channel_; 196 197 // Handle of the desktop process. 198 base::ProcessHandle desktop_process_; 199 200 int pending_capture_frame_requests_; 201 202 // Shared memory buffers by Id. Each buffer is owned by the corresponding 203 // frame. 204 SharedBuffers shared_buffers_; 205 206 // Keeps the desired screen resolution so it can be passed to a newly attached 207 // desktop session agent. 208 ScreenResolution screen_resolution_; 209 210 // True if |this| has been connected to the desktop session. 211 bool is_desktop_session_connected_; 212 213 bool virtual_terminal_; 214 215 DISALLOW_COPY_AND_ASSIGN(DesktopSessionProxy); 216 }; 217 218 // Destroys |DesktopSessionProxy| instances on the caller's thread. 219 struct DesktopSessionProxyTraits { 220 static void Destruct(const DesktopSessionProxy* desktop_session_proxy); 221 }; 222 223 } // namespace remoting 224 225 #endif // REMOTING_HOST_DESKTOP_SESSION_PROXY_H_ 226