1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "remoting/host/desktop_session_proxy.h"
#include <stddef.h>
#include <utility>
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/memory/shared_memory.h"
#include "base/process/process_handle.h"
#include "base/single_thread_task_runner.h"
#include "build/build_config.h"
#include "ipc/ipc_channel_proxy.h"
#include "ipc/ipc_message_macros.h"
#include "remoting/base/capabilities.h"
#include "remoting/host/chromoting_messages.h"
#include "remoting/host/client_session.h"
#include "remoting/host/client_session_control.h"
#include "remoting/host/desktop_session_connector.h"
#include "remoting/host/ipc_audio_capturer.h"
#include "remoting/host/ipc_input_injector.h"
#include "remoting/host/ipc_mouse_cursor_monitor.h"
#include "remoting/host/ipc_screen_controls.h"
#include "remoting/host/ipc_video_frame_capturer.h"
#include "remoting/proto/audio.pb.h"
#include "remoting/proto/control.pb.h"
#include "remoting/proto/event.pb.h"
#include "remoting/protocol/capability_names.h"
#include "third_party/webrtc/modules/desktop_capture/desktop_frame.h"
#include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h"
#include "third_party/webrtc/modules/desktop_capture/mouse_cursor.h"
#include "third_party/webrtc/modules/desktop_capture/shared_memory.h"
#if defined(OS_WIN)
#include "base/win/scoped_handle.h"
#endif // defined(OS_WIN)
const bool kReadOnly = true;
namespace remoting {
class DesktopSessionProxy::IpcSharedBufferCore
: public base::RefCountedThreadSafe<IpcSharedBufferCore> {
public:
IpcSharedBufferCore(int id,
base::SharedMemoryHandle handle,
size_t size)
: id_(id),
shared_memory_(handle, kReadOnly),
size_(size) {
if (!shared_memory_.Map(size)) {
LOG(ERROR) << "Failed to map a shared buffer: id=" << id
<< ", size=" << size;
}
}
int id() { return id_; }
size_t size() { return size_; }
void* memory() { return shared_memory_.memory(); }
private:
virtual ~IpcSharedBufferCore() {}
friend class base::RefCountedThreadSafe<IpcSharedBufferCore>;
int id_;
base::SharedMemory shared_memory_;
size_t size_;
DISALLOW_COPY_AND_ASSIGN(IpcSharedBufferCore);
};
class DesktopSessionProxy::IpcSharedBuffer : public webrtc::SharedMemory {
public:
IpcSharedBuffer(scoped_refptr<IpcSharedBufferCore> core)
: SharedMemory(core->memory(), core->size(), 0, core->id()),
core_(core) {}
private:
scoped_refptr<IpcSharedBufferCore> core_;
DISALLOW_COPY_AND_ASSIGN(IpcSharedBuffer);
};
DesktopSessionProxy::DesktopSessionProxy(
scoped_refptr<base::SingleThreadTaskRunner> audio_capture_task_runner,
scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner,
scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
base::WeakPtr<ClientSessionControl> client_session_control,
base::WeakPtr<DesktopSessionConnector> desktop_session_connector,
const DesktopEnvironmentOptions& options)
: audio_capture_task_runner_(audio_capture_task_runner),
caller_task_runner_(caller_task_runner),
io_task_runner_(io_task_runner),
client_session_control_(client_session_control),
desktop_session_connector_(desktop_session_connector),
pending_capture_frame_requests_(0),
is_desktop_session_connected_(false),
options_(options) {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
}
std::unique_ptr<AudioCapturer> DesktopSessionProxy::CreateAudioCapturer() {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
return base::MakeUnique<IpcAudioCapturer>(this);
}
std::unique_ptr<InputInjector> DesktopSessionProxy::CreateInputInjector() {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
return base::MakeUnique<IpcInputInjector>(this);
}
std::unique_ptr<ScreenControls> DesktopSessionProxy::CreateScreenControls() {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
return base::MakeUnique<IpcScreenControls>(this);
}
std::unique_ptr<webrtc::DesktopCapturer>
DesktopSessionProxy::CreateVideoCapturer() {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
return base::MakeUnique<IpcVideoFrameCapturer>(this);
}
std::unique_ptr<webrtc::MouseCursorMonitor>
DesktopSessionProxy::CreateMouseCursorMonitor() {
return base::MakeUnique<IpcMouseCursorMonitor>(this);
}
std::string DesktopSessionProxy::GetCapabilities() const {
std::string result = protocol::kRateLimitResizeRequests;
// Ask the client to send its resolution unconditionally.
if (options_.enable_curtaining()) {
result += " ";
result += protocol::kSendInitialResolution;
}
if (InputInjector::SupportsTouchEvents()) {
result += " ";
result += protocol::kTouchEventsCapability;
}
return result;
}
void DesktopSessionProxy::SetCapabilities(const std::string& capabilities) {
// Delay creation of the desktop session until the client screen resolution is
// received if the desktop session requires the initial screen resolution
// (when enable_curtaining() is true) and the client is expected to
// sent its screen resolution (the 'sendInitialResolution' capability is
// supported).
if (options_.enable_curtaining() &&
HasCapability(capabilities, protocol::kSendInitialResolution)) {
VLOG(1) << "Waiting for the client screen resolution.";
return;
}
// Connect to the desktop session.
if (!is_desktop_session_connected_) {
is_desktop_session_connected_ = true;
if (desktop_session_connector_.get()) {
desktop_session_connector_->ConnectTerminal(this, screen_resolution_,
options_.enable_curtaining());
}
}
}
bool DesktopSessionProxy::OnMessageReceived(const IPC::Message& message) {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(DesktopSessionProxy, message)
IPC_MESSAGE_HANDLER(ChromotingDesktopNetworkMsg_AudioPacket,
OnAudioPacket)
IPC_MESSAGE_HANDLER(ChromotingDesktopNetworkMsg_CaptureResult,
OnCaptureResult)
IPC_MESSAGE_HANDLER(ChromotingDesktopNetworkMsg_MouseCursor,
OnMouseCursor)
IPC_MESSAGE_HANDLER(ChromotingDesktopNetworkMsg_CreateSharedBuffer,
OnCreateSharedBuffer)
IPC_MESSAGE_HANDLER(ChromotingDesktopNetworkMsg_ReleaseSharedBuffer,
OnReleaseSharedBuffer)
IPC_MESSAGE_HANDLER(ChromotingDesktopNetworkMsg_InjectClipboardEvent,
OnInjectClipboardEvent)
IPC_MESSAGE_HANDLER(ChromotingDesktopNetworkMsg_DisconnectSession,
DisconnectSession);
IPC_END_MESSAGE_MAP()
CHECK(handled) << "Received unexpected IPC type: " << message.type();
return handled;
}
void DesktopSessionProxy::OnChannelConnected(int32_t peer_pid) {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
VLOG(1) << "IPC: network <- desktop (" << peer_pid << ")";
}
void DesktopSessionProxy::OnChannelError() {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
DetachFromDesktop();
}
bool DesktopSessionProxy::AttachToDesktop(
const IPC::ChannelHandle& desktop_pipe,
int session_id) {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
DCHECK(!desktop_channel_);
// Ignore the attach notification if the client session has been disconnected
// already.
if (!client_session_control_.get())
return false;
// Connect to the desktop process.
desktop_channel_ = IPC::ChannelProxy::Create(desktop_pipe,
IPC::Channel::MODE_CLIENT, this,
io_task_runner_.get());
// Pass ID of the client (which is authenticated at this point) to the desktop
// session agent and start the agent.
SendToDesktop(new ChromotingNetworkDesktopMsg_StartSessionAgent(
client_session_control_->client_jid(), screen_resolution_, options_));
desktop_session_id_ = session_id;
return true;
}
void DesktopSessionProxy::DetachFromDesktop() {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
desktop_channel_.reset();
desktop_session_id_ = UINT32_MAX;
shared_buffers_.clear();
// Generate fake responses to keep the video capturer in sync.
while (pending_capture_frame_requests_) {
--pending_capture_frame_requests_;
video_capturer_->OnCaptureResult(
webrtc::DesktopCapturer::Result::ERROR_TEMPORARY, nullptr);
}
}
void DesktopSessionProxy::SetAudioCapturer(
const base::WeakPtr<IpcAudioCapturer>& audio_capturer) {
DCHECK(audio_capture_task_runner_->BelongsToCurrentThread());
audio_capturer_ = audio_capturer;
}
void DesktopSessionProxy::CaptureFrame() {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
if (desktop_channel_) {
++pending_capture_frame_requests_;
SendToDesktop(new ChromotingNetworkDesktopMsg_CaptureFrame());
} else {
video_capturer_->OnCaptureResult(
webrtc::DesktopCapturer::Result::ERROR_TEMPORARY, nullptr);
}
}
void DesktopSessionProxy::SetVideoCapturer(
const base::WeakPtr<IpcVideoFrameCapturer> video_capturer) {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
video_capturer_ = video_capturer;
}
void DesktopSessionProxy::SetMouseCursorMonitor(
const base::WeakPtr<IpcMouseCursorMonitor>& mouse_cursor_monitor) {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
mouse_cursor_monitor_ = mouse_cursor_monitor;
}
void DesktopSessionProxy::DisconnectSession(protocol::ErrorCode error) {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
// Disconnect the client session if it hasn't been disconnected yet.
if (client_session_control_.get())
client_session_control_->DisconnectSession(error);
}
void DesktopSessionProxy::InjectClipboardEvent(
const protocol::ClipboardEvent& event) {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
std::string serialized_event;
if (!event.SerializeToString(&serialized_event)) {
LOG(ERROR) << "Failed to serialize protocol::ClipboardEvent.";
return;
}
SendToDesktop(
new ChromotingNetworkDesktopMsg_InjectClipboardEvent(serialized_event));
}
void DesktopSessionProxy::InjectKeyEvent(const protocol::KeyEvent& event) {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
std::string serialized_event;
if (!event.SerializeToString(&serialized_event)) {
LOG(ERROR) << "Failed to serialize protocol::KeyEvent.";
return;
}
SendToDesktop(
new ChromotingNetworkDesktopMsg_InjectKeyEvent(serialized_event));
}
void DesktopSessionProxy::InjectTextEvent(const protocol::TextEvent& event) {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
std::string serialized_event;
if (!event.SerializeToString(&serialized_event)) {
LOG(ERROR) << "Failed to serialize protocol::TextEvent.";
return;
}
SendToDesktop(
new ChromotingNetworkDesktopMsg_InjectTextEvent(serialized_event));
}
void DesktopSessionProxy::InjectMouseEvent(const protocol::MouseEvent& event) {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
std::string serialized_event;
if (!event.SerializeToString(&serialized_event)) {
LOG(ERROR) << "Failed to serialize protocol::MouseEvent.";
return;
}
SendToDesktop(
new ChromotingNetworkDesktopMsg_InjectMouseEvent(serialized_event));
}
void DesktopSessionProxy::InjectTouchEvent(const protocol::TouchEvent& event) {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
std::string serialized_event;
if (!event.SerializeToString(&serialized_event)) {
LOG(ERROR) << "Failed to serialize protocol::TouchEvent.";
return;
}
SendToDesktop(
new ChromotingNetworkDesktopMsg_InjectTouchEvent(serialized_event));
}
void DesktopSessionProxy::StartInputInjector(
std::unique_ptr<protocol::ClipboardStub> client_clipboard) {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
client_clipboard_ = std::move(client_clipboard);
}
void DesktopSessionProxy::SetScreenResolution(
const ScreenResolution& resolution) {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
if (resolution.IsEmpty())
return;
screen_resolution_ = resolution;
// Connect to the desktop session if it is not done yet.
if (!is_desktop_session_connected_) {
is_desktop_session_connected_ = true;
if (desktop_session_connector_.get()) {
desktop_session_connector_->ConnectTerminal(this, screen_resolution_,
options_.enable_curtaining());
}
return;
}
// Pass the client's resolution to both daemon and desktop session agent.
// Depending on the session kind the screen resolution can be set by either
// the daemon (for example RDP sessions on Windows) or by the desktop session
// agent (when sharing the physical console).
if (desktop_session_connector_.get())
desktop_session_connector_->SetScreenResolution(this, screen_resolution_);
SendToDesktop(
new ChromotingNetworkDesktopMsg_SetScreenResolution(screen_resolution_));
}
DesktopSessionProxy::~DesktopSessionProxy() {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
if (desktop_session_connector_.get() && is_desktop_session_connected_)
desktop_session_connector_->DisconnectTerminal(this);
}
scoped_refptr<DesktopSessionProxy::IpcSharedBufferCore>
DesktopSessionProxy::GetSharedBufferCore(int id) {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
SharedBuffers::const_iterator i = shared_buffers_.find(id);
if (i != shared_buffers_.end()) {
return i->second;
} else {
LOG(ERROR) << "Failed to find the shared buffer " << id;
return nullptr;
}
}
void DesktopSessionProxy::OnAudioPacket(const std::string& serialized_packet) {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
// Parse a serialized audio packet. No further validation is done since
// the message was sent by more privileged process.
std::unique_ptr<AudioPacket> packet(new AudioPacket());
if (!packet->ParseFromString(serialized_packet)) {
LOG(ERROR) << "Failed to parse AudioPacket.";
return;
}
// Pass a captured audio packet to |audio_capturer_|.
audio_capture_task_runner_->PostTask(
FROM_HERE, base::Bind(&IpcAudioCapturer::OnAudioPacket, audio_capturer_,
base::Passed(&packet)));
}
void DesktopSessionProxy::OnCreateSharedBuffer(
int id,
base::SharedMemoryHandle handle,
uint32_t size) {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
scoped_refptr<IpcSharedBufferCore> shared_buffer =
new IpcSharedBufferCore(id, handle, size);
if (shared_buffer->memory() != nullptr &&
!shared_buffers_.insert(std::make_pair(id, shared_buffer)).second) {
LOG(ERROR) << "Duplicate shared buffer id " << id << " encountered";
}
}
void DesktopSessionProxy::OnReleaseSharedBuffer(int id) {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
// Drop the cached reference to the buffer.
shared_buffers_.erase(id);
}
void DesktopSessionProxy::OnCaptureResult(
webrtc::DesktopCapturer::Result result,
const SerializedDesktopFrame& serialized_frame) {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
--pending_capture_frame_requests_;
if (!video_capturer_) {
return;
}
if (result != webrtc::DesktopCapturer::Result::SUCCESS) {
video_capturer_->OnCaptureResult(result, nullptr);
return;
}
// Assume that |serialized_frame| is well-formed because it was received from
// a more privileged process.
scoped_refptr<IpcSharedBufferCore> shared_buffer_core =
GetSharedBufferCore(serialized_frame.shared_buffer_id);
CHECK(shared_buffer_core.get());
std::unique_ptr<webrtc::DesktopFrame> frame(
new webrtc::SharedMemoryDesktopFrame(
serialized_frame.dimensions, serialized_frame.bytes_per_row,
new IpcSharedBuffer(shared_buffer_core)));
frame->set_capture_time_ms(serialized_frame.capture_time_ms);
frame->set_dpi(serialized_frame.dpi);
for (const auto& rect : serialized_frame.dirty_region) {
frame->mutable_updated_region()->AddRect(rect);
}
video_capturer_->OnCaptureResult(webrtc::DesktopCapturer::Result::SUCCESS,
std::move(frame));
}
void DesktopSessionProxy::OnMouseCursor(
const webrtc::MouseCursor& mouse_cursor) {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
if (mouse_cursor_monitor_) {
mouse_cursor_monitor_->OnMouseCursor(
base::WrapUnique(webrtc::MouseCursor::CopyOf(mouse_cursor)));
}
}
void DesktopSessionProxy::OnInjectClipboardEvent(
const std::string& serialized_event) {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
if (client_clipboard_) {
protocol::ClipboardEvent event;
if (!event.ParseFromString(serialized_event)) {
LOG(ERROR) << "Failed to parse protocol::ClipboardEvent.";
return;
}
client_clipboard_->InjectClipboardEvent(event);
}
}
void DesktopSessionProxy::SendToDesktop(IPC::Message* message) {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
if (desktop_channel_) {
desktop_channel_->Send(message);
} else {
delete message;
}
}
// static
void DesktopSessionProxyTraits::Destruct(
const DesktopSessionProxy* desktop_session_proxy) {
desktop_session_proxy->caller_task_runner_->DeleteSoon(FROM_HERE,
desktop_session_proxy);
}
} // namespace remoting
|