[Sync] Test Android passphrase creation UI
[chromium-blink-merge.git] / remoting / host / desktop_session_proxy.cc
blob7d1fc7e4f0ecd63a1802e427d059152c768306e6
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 #include "remoting/host/desktop_session_proxy.h"
7 #include "base/compiler_specific.h"
8 #include "base/logging.h"
9 #include "base/process/process_handle.h"
10 #include "base/memory/shared_memory.h"
11 #include "base/single_thread_task_runner.h"
12 #include "ipc/ipc_channel_proxy.h"
13 #include "ipc/ipc_message_macros.h"
14 #include "remoting/base/capabilities.h"
15 #include "remoting/host/chromoting_messages.h"
16 #include "remoting/host/client_session.h"
17 #include "remoting/host/client_session_control.h"
18 #include "remoting/host/desktop_session_connector.h"
19 #include "remoting/host/ipc_audio_capturer.h"
20 #include "remoting/host/ipc_input_injector.h"
21 #include "remoting/host/ipc_mouse_cursor_monitor.h"
22 #include "remoting/host/ipc_screen_controls.h"
23 #include "remoting/host/ipc_video_frame_capturer.h"
24 #include "remoting/proto/audio.pb.h"
25 #include "remoting/proto/control.pb.h"
26 #include "remoting/proto/event.pb.h"
27 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h"
28 #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h"
29 #include "third_party/webrtc/modules/desktop_capture/mouse_cursor.h"
30 #include "third_party/webrtc/modules/desktop_capture/shared_memory.h"
32 #if defined(OS_WIN)
33 #include "base/win/scoped_handle.h"
34 #endif // defined(OS_WIN)
36 const bool kReadOnly = true;
37 const char kSendInitialResolution[] = "sendInitialResolution";
38 const char kRateLimitResizeRequests[] = "rateLimitResizeRequests";
40 namespace remoting {
42 class DesktopSessionProxy::IpcSharedBufferCore
43 : public base::RefCountedThreadSafe<IpcSharedBufferCore> {
44 public:
45 IpcSharedBufferCore(int id,
46 base::SharedMemoryHandle handle,
47 base::ProcessHandle process,
48 size_t size)
49 : id_(id),
50 #if defined(OS_WIN)
51 shared_memory_(handle, kReadOnly, process),
52 #else // !defined(OS_WIN)
53 shared_memory_(handle, kReadOnly),
54 #endif // !defined(OS_WIN)
55 size_(size) {
56 if (!shared_memory_.Map(size)) {
57 LOG(ERROR) << "Failed to map a shared buffer: id=" << id
58 #if defined(OS_WIN)
59 << ", handle=" << handle
60 #else
61 << ", handle.fd=" << handle.fd
62 #endif
63 << ", size=" << size;
67 int id() { return id_; }
68 size_t size() { return size_; }
69 void* memory() { return shared_memory_.memory(); }
70 webrtc::SharedMemory::Handle handle() {
71 #if defined(OS_WIN)
72 return shared_memory_.handle();
73 #else
74 return shared_memory_.handle().fd;
75 #endif
78 private:
79 virtual ~IpcSharedBufferCore() {}
80 friend class base::RefCountedThreadSafe<IpcSharedBufferCore>;
82 int id_;
83 base::SharedMemory shared_memory_;
84 size_t size_;
86 DISALLOW_COPY_AND_ASSIGN(IpcSharedBufferCore);
89 class DesktopSessionProxy::IpcSharedBuffer : public webrtc::SharedMemory {
90 public:
91 IpcSharedBuffer(scoped_refptr<IpcSharedBufferCore> core)
92 : SharedMemory(core->memory(), core->size(),
93 core->handle(), core->id()),
94 core_(core) {
97 private:
98 scoped_refptr<IpcSharedBufferCore> core_;
100 DISALLOW_COPY_AND_ASSIGN(IpcSharedBuffer);
103 DesktopSessionProxy::DesktopSessionProxy(
104 scoped_refptr<base::SingleThreadTaskRunner> audio_capture_task_runner,
105 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner,
106 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
107 scoped_refptr<base::SingleThreadTaskRunner> video_capture_task_runner,
108 base::WeakPtr<ClientSessionControl> client_session_control,
109 base::WeakPtr<DesktopSessionConnector> desktop_session_connector,
110 bool virtual_terminal)
111 : audio_capture_task_runner_(audio_capture_task_runner),
112 caller_task_runner_(caller_task_runner),
113 io_task_runner_(io_task_runner),
114 video_capture_task_runner_(video_capture_task_runner),
115 client_session_control_(client_session_control),
116 desktop_session_connector_(desktop_session_connector),
117 pending_capture_frame_requests_(0),
118 is_desktop_session_connected_(false),
119 virtual_terminal_(virtual_terminal) {
120 DCHECK(caller_task_runner_->BelongsToCurrentThread());
123 scoped_ptr<AudioCapturer> DesktopSessionProxy::CreateAudioCapturer() {
124 DCHECK(caller_task_runner_->BelongsToCurrentThread());
126 return make_scoped_ptr(new IpcAudioCapturer(this));
129 scoped_ptr<InputInjector> DesktopSessionProxy::CreateInputInjector() {
130 DCHECK(caller_task_runner_->BelongsToCurrentThread());
132 return make_scoped_ptr(new IpcInputInjector(this));
135 scoped_ptr<ScreenControls> DesktopSessionProxy::CreateScreenControls() {
136 DCHECK(caller_task_runner_->BelongsToCurrentThread());
138 return make_scoped_ptr(new IpcScreenControls(this));
141 scoped_ptr<webrtc::DesktopCapturer> DesktopSessionProxy::CreateVideoCapturer() {
142 DCHECK(caller_task_runner_->BelongsToCurrentThread());
144 return make_scoped_ptr(new IpcVideoFrameCapturer(this));
147 scoped_ptr<webrtc::MouseCursorMonitor>
148 DesktopSessionProxy::CreateMouseCursorMonitor() {
149 return make_scoped_ptr(new IpcMouseCursorMonitor(this));
152 std::string DesktopSessionProxy::GetCapabilities() const {
153 std::string result = kRateLimitResizeRequests;
154 // Ask the client to send its resolution unconditionally.
155 if (virtual_terminal_)
156 result = result + " " + kSendInitialResolution;
157 return result;
160 void DesktopSessionProxy::SetCapabilities(const std::string& capabilities) {
161 // Delay creation of the desktop session until the client screen resolution is
162 // received if the desktop session requires the initial screen resolution
163 // (when |virtual_terminal_| is true) and the client is expected to
164 // sent its screen resolution (the 'sendInitialResolution' capability is
165 // supported).
166 if (virtual_terminal_ &&
167 HasCapability(capabilities, kSendInitialResolution)) {
168 VLOG(1) << "Waiting for the client screen resolution.";
169 return;
172 // Connect to the desktop session.
173 if (!is_desktop_session_connected_) {
174 is_desktop_session_connected_ = true;
175 if (desktop_session_connector_.get()) {
176 desktop_session_connector_->ConnectTerminal(
177 this, screen_resolution_, virtual_terminal_);
182 bool DesktopSessionProxy::OnMessageReceived(const IPC::Message& message) {
183 DCHECK(caller_task_runner_->BelongsToCurrentThread());
185 bool handled = true;
186 IPC_BEGIN_MESSAGE_MAP(DesktopSessionProxy, message)
187 IPC_MESSAGE_HANDLER(ChromotingDesktopNetworkMsg_AudioPacket,
188 OnAudioPacket)
189 IPC_MESSAGE_HANDLER(ChromotingDesktopNetworkMsg_CaptureCompleted,
190 OnCaptureCompleted)
191 IPC_MESSAGE_HANDLER(ChromotingDesktopNetworkMsg_MouseCursor,
192 OnMouseCursor)
193 IPC_MESSAGE_HANDLER(ChromotingDesktopNetworkMsg_CreateSharedBuffer,
194 OnCreateSharedBuffer)
195 IPC_MESSAGE_HANDLER(ChromotingDesktopNetworkMsg_ReleaseSharedBuffer,
196 OnReleaseSharedBuffer)
197 IPC_MESSAGE_HANDLER(ChromotingDesktopNetworkMsg_InjectClipboardEvent,
198 OnInjectClipboardEvent)
199 IPC_MESSAGE_HANDLER(ChromotingDesktopNetworkMsg_DisconnectSession,
200 DisconnectSession);
201 IPC_END_MESSAGE_MAP()
203 CHECK(handled) << "Received unexpected IPC type: " << message.type();
204 return handled;
207 void DesktopSessionProxy::OnChannelConnected(int32 peer_pid) {
208 DCHECK(caller_task_runner_->BelongsToCurrentThread());
210 VLOG(1) << "IPC: network <- desktop (" << peer_pid << ")";
213 void DesktopSessionProxy::OnChannelError() {
214 DCHECK(caller_task_runner_->BelongsToCurrentThread());
216 DetachFromDesktop();
219 bool DesktopSessionProxy::AttachToDesktop(
220 base::Process desktop_process,
221 IPC::PlatformFileForTransit desktop_pipe) {
222 DCHECK(caller_task_runner_->BelongsToCurrentThread());
223 DCHECK(!desktop_channel_);
224 DCHECK(!desktop_process_.IsValid());
226 // Ignore the attach notification if the client session has been disconnected
227 // already.
228 if (!client_session_control_.get())
229 return false;
231 desktop_process_ = desktop_process.Pass();
233 #if defined(OS_WIN)
234 // On Windows: |desktop_process| is a valid handle, but |desktop_pipe| needs
235 // to be duplicated from the desktop process.
236 HANDLE temp_handle;
237 if (!DuplicateHandle(desktop_process_.Handle(), desktop_pipe,
238 GetCurrentProcess(), &temp_handle, 0,
239 FALSE, DUPLICATE_SAME_ACCESS)) {
240 PLOG(ERROR) << "Failed to duplicate the desktop-to-network pipe handle";
242 desktop_process_.Close();
243 return false;
245 base::win::ScopedHandle pipe(temp_handle);
247 IPC::ChannelHandle desktop_channel_handle(pipe.Get());
249 #elif defined(OS_POSIX)
250 // On posix: |desktop_pipe| is a valid file descriptor.
251 DCHECK(desktop_pipe.auto_close);
253 IPC::ChannelHandle desktop_channel_handle(std::string(), desktop_pipe);
255 #else
256 #error Unsupported platform.
257 #endif
259 // Connect to the desktop process.
260 desktop_channel_ = IPC::ChannelProxy::Create(desktop_channel_handle,
261 IPC::Channel::MODE_CLIENT,
262 this,
263 io_task_runner_.get());
265 // Pass ID of the client (which is authenticated at this point) to the desktop
266 // session agent and start the agent.
267 SendToDesktop(new ChromotingNetworkDesktopMsg_StartSessionAgent(
268 client_session_control_->client_jid(),
269 screen_resolution_,
270 virtual_terminal_));
272 return true;
275 void DesktopSessionProxy::DetachFromDesktop() {
276 DCHECK(caller_task_runner_->BelongsToCurrentThread());
278 desktop_channel_.reset();
280 if (desktop_process_.IsValid())
281 desktop_process_.Close();
283 shared_buffers_.clear();
285 // Generate fake responses to keep the video capturer in sync.
286 while (pending_capture_frame_requests_) {
287 --pending_capture_frame_requests_;
288 PostCaptureCompleted(nullptr);
292 void DesktopSessionProxy::SetAudioCapturer(
293 const base::WeakPtr<IpcAudioCapturer>& audio_capturer) {
294 DCHECK(audio_capture_task_runner_->BelongsToCurrentThread());
296 audio_capturer_ = audio_capturer;
299 void DesktopSessionProxy::CaptureFrame() {
300 if (!caller_task_runner_->BelongsToCurrentThread()) {
301 caller_task_runner_->PostTask(
302 FROM_HERE, base::Bind(&DesktopSessionProxy::CaptureFrame, this));
303 return;
306 if (desktop_channel_) {
307 ++pending_capture_frame_requests_;
308 SendToDesktop(new ChromotingNetworkDesktopMsg_CaptureFrame());
309 } else {
310 PostCaptureCompleted(nullptr);
314 void DesktopSessionProxy::SetVideoCapturer(
315 const base::WeakPtr<IpcVideoFrameCapturer> video_capturer) {
316 DCHECK(video_capture_task_runner_->BelongsToCurrentThread());
318 video_capturer_ = video_capturer;
321 void DesktopSessionProxy::SetMouseCursorMonitor(
322 const base::WeakPtr<IpcMouseCursorMonitor>& mouse_cursor_monitor) {
323 DCHECK(video_capture_task_runner_->BelongsToCurrentThread());
325 mouse_cursor_monitor_ = mouse_cursor_monitor;
328 void DesktopSessionProxy::DisconnectSession() {
329 DCHECK(caller_task_runner_->BelongsToCurrentThread());
331 // Disconnect the client session if it hasn't been disconnected yet.
332 if (client_session_control_.get())
333 client_session_control_->DisconnectSession();
336 void DesktopSessionProxy::InjectClipboardEvent(
337 const protocol::ClipboardEvent& event) {
338 DCHECK(caller_task_runner_->BelongsToCurrentThread());
340 std::string serialized_event;
341 if (!event.SerializeToString(&serialized_event)) {
342 LOG(ERROR) << "Failed to serialize protocol::ClipboardEvent.";
343 return;
346 SendToDesktop(
347 new ChromotingNetworkDesktopMsg_InjectClipboardEvent(serialized_event));
350 void DesktopSessionProxy::InjectKeyEvent(const protocol::KeyEvent& event) {
351 DCHECK(caller_task_runner_->BelongsToCurrentThread());
353 std::string serialized_event;
354 if (!event.SerializeToString(&serialized_event)) {
355 LOG(ERROR) << "Failed to serialize protocol::KeyEvent.";
356 return;
359 SendToDesktop(
360 new ChromotingNetworkDesktopMsg_InjectKeyEvent(serialized_event));
363 void DesktopSessionProxy::InjectTextEvent(const protocol::TextEvent& event) {
364 DCHECK(caller_task_runner_->BelongsToCurrentThread());
366 std::string serialized_event;
367 if (!event.SerializeToString(&serialized_event)) {
368 LOG(ERROR) << "Failed to serialize protocol::TextEvent.";
369 return;
372 SendToDesktop(
373 new ChromotingNetworkDesktopMsg_InjectTextEvent(serialized_event));
376 void DesktopSessionProxy::InjectMouseEvent(const protocol::MouseEvent& event) {
377 DCHECK(caller_task_runner_->BelongsToCurrentThread());
379 std::string serialized_event;
380 if (!event.SerializeToString(&serialized_event)) {
381 LOG(ERROR) << "Failed to serialize protocol::MouseEvent.";
382 return;
385 SendToDesktop(
386 new ChromotingNetworkDesktopMsg_InjectMouseEvent(serialized_event));
389 void DesktopSessionProxy::InjectTouchEvent(const protocol::TouchEvent& event) {
390 DCHECK(caller_task_runner_->BelongsToCurrentThread());
392 std::string serialized_event;
393 if (!event.SerializeToString(&serialized_event)) {
394 LOG(ERROR) << "Failed to serialize protocol::TouchEvent.";
395 return;
398 SendToDesktop(
399 new ChromotingNetworkDesktopMsg_InjectTouchEvent(serialized_event));
402 void DesktopSessionProxy::StartInputInjector(
403 scoped_ptr<protocol::ClipboardStub> client_clipboard) {
404 DCHECK(caller_task_runner_->BelongsToCurrentThread());
406 client_clipboard_ = client_clipboard.Pass();
409 void DesktopSessionProxy::SetScreenResolution(
410 const ScreenResolution& resolution) {
411 DCHECK(caller_task_runner_->BelongsToCurrentThread());
413 if (resolution.IsEmpty())
414 return;
416 screen_resolution_ = resolution;
418 // Connect to the desktop session if it is not done yet.
419 if (!is_desktop_session_connected_) {
420 is_desktop_session_connected_ = true;
421 if (desktop_session_connector_.get()) {
422 desktop_session_connector_->ConnectTerminal(
423 this, screen_resolution_, virtual_terminal_);
425 return;
428 // Pass the client's resolution to both daemon and desktop session agent.
429 // Depending on the session kind the screen resolution can be set by either
430 // the daemon (for example RDP sessions on Windows) or by the desktop session
431 // agent (when sharing the physical console).
432 if (desktop_session_connector_.get())
433 desktop_session_connector_->SetScreenResolution(this, screen_resolution_);
434 SendToDesktop(
435 new ChromotingNetworkDesktopMsg_SetScreenResolution(screen_resolution_));
438 DesktopSessionProxy::~DesktopSessionProxy() {
439 DCHECK(caller_task_runner_->BelongsToCurrentThread());
441 if (desktop_session_connector_.get() && is_desktop_session_connected_)
442 desktop_session_connector_->DisconnectTerminal(this);
445 scoped_refptr<DesktopSessionProxy::IpcSharedBufferCore>
446 DesktopSessionProxy::GetSharedBufferCore(int id) {
447 DCHECK(caller_task_runner_->BelongsToCurrentThread());
449 SharedBuffers::const_iterator i = shared_buffers_.find(id);
450 if (i != shared_buffers_.end()) {
451 return i->second;
452 } else {
453 LOG(ERROR) << "Failed to find the shared buffer " << id;
454 return nullptr;
458 void DesktopSessionProxy::OnAudioPacket(const std::string& serialized_packet) {
459 DCHECK(caller_task_runner_->BelongsToCurrentThread());
461 // Parse a serialized audio packet. No further validation is done since
462 // the message was sent by more privileged process.
463 scoped_ptr<AudioPacket> packet(new AudioPacket());
464 if (!packet->ParseFromString(serialized_packet)) {
465 LOG(ERROR) << "Failed to parse AudioPacket.";
466 return;
469 // Pass a captured audio packet to |audio_capturer_|.
470 audio_capture_task_runner_->PostTask(
471 FROM_HERE, base::Bind(&IpcAudioCapturer::OnAudioPacket, audio_capturer_,
472 base::Passed(&packet)));
475 void DesktopSessionProxy::OnCreateSharedBuffer(
476 int id,
477 IPC::PlatformFileForTransit handle,
478 uint32 size) {
479 DCHECK(caller_task_runner_->BelongsToCurrentThread());
481 scoped_refptr<IpcSharedBufferCore> shared_buffer =
482 new IpcSharedBufferCore(id, handle, desktop_process_.Handle(), size);
484 if (shared_buffer->memory() != nullptr &&
485 !shared_buffers_.insert(std::make_pair(id, shared_buffer)).second) {
486 LOG(ERROR) << "Duplicate shared buffer id " << id << " encountered";
490 void DesktopSessionProxy::OnReleaseSharedBuffer(int id) {
491 DCHECK(caller_task_runner_->BelongsToCurrentThread());
493 // Drop the cached reference to the buffer.
494 shared_buffers_.erase(id);
497 void DesktopSessionProxy::OnCaptureCompleted(
498 const SerializedDesktopFrame& serialized_frame) {
499 DCHECK(caller_task_runner_->BelongsToCurrentThread());
501 // Assume that |serialized_frame| is well-formed because it was received from
502 // a more privileged process.
503 scoped_refptr<IpcSharedBufferCore> shared_buffer_core =
504 GetSharedBufferCore(serialized_frame.shared_buffer_id);
505 CHECK(shared_buffer_core.get());
507 scoped_ptr<webrtc::DesktopFrame> frame(
508 new webrtc::SharedMemoryDesktopFrame(
509 serialized_frame.dimensions, serialized_frame.bytes_per_row,
510 new IpcSharedBuffer(shared_buffer_core)));
511 frame->set_capture_time_ms(serialized_frame.capture_time_ms);
512 frame->set_dpi(serialized_frame.dpi);
514 for (size_t i = 0; i < serialized_frame.dirty_region.size(); ++i) {
515 frame->mutable_updated_region()->AddRect(serialized_frame.dirty_region[i]);
518 --pending_capture_frame_requests_;
519 PostCaptureCompleted(frame.Pass());
522 void DesktopSessionProxy::OnMouseCursor(
523 const webrtc::MouseCursor& mouse_cursor) {
524 DCHECK(caller_task_runner_->BelongsToCurrentThread());
525 PostMouseCursor(make_scoped_ptr(webrtc::MouseCursor::CopyOf(mouse_cursor)));
528 void DesktopSessionProxy::OnInjectClipboardEvent(
529 const std::string& serialized_event) {
530 DCHECK(caller_task_runner_->BelongsToCurrentThread());
532 if (client_clipboard_) {
533 protocol::ClipboardEvent event;
534 if (!event.ParseFromString(serialized_event)) {
535 LOG(ERROR) << "Failed to parse protocol::ClipboardEvent.";
536 return;
539 client_clipboard_->InjectClipboardEvent(event);
543 void DesktopSessionProxy::PostCaptureCompleted(
544 scoped_ptr<webrtc::DesktopFrame> frame) {
545 DCHECK(caller_task_runner_->BelongsToCurrentThread());
547 video_capture_task_runner_->PostTask(
548 FROM_HERE,
549 base::Bind(&IpcVideoFrameCapturer::OnCaptureCompleted, video_capturer_,
550 base::Passed(&frame)));
553 void DesktopSessionProxy::PostMouseCursor(
554 scoped_ptr<webrtc::MouseCursor> mouse_cursor) {
555 DCHECK(caller_task_runner_->BelongsToCurrentThread());
557 video_capture_task_runner_->PostTask(
558 FROM_HERE,
559 base::Bind(&IpcMouseCursorMonitor::OnMouseCursor, mouse_cursor_monitor_,
560 base::Passed(&mouse_cursor)));
563 void DesktopSessionProxy::SendToDesktop(IPC::Message* message) {
564 DCHECK(caller_task_runner_->BelongsToCurrentThread());
566 if (desktop_channel_) {
567 desktop_channel_->Send(message);
568 } else {
569 delete message;
573 // static
574 void DesktopSessionProxyTraits::Destruct(
575 const DesktopSessionProxy* desktop_session_proxy) {
576 desktop_session_proxy->caller_task_runner_->DeleteSoon(FROM_HERE,
577 desktop_session_proxy);
580 } // namespace remoting