blob: 2005342c5542f17225229b97aa4ee6e12c244b86 [file] [log] [blame]
[email protected]e265ad72012-03-16 17:28:031// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]409ac612011-11-18 04:05:572// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "remoting/protocol/client_control_dispatcher.h"
6
avi5a080f012015-12-22 23:15:437#include <stdint.h>
8
[email protected]c6944272012-01-06 22:12:289#include "base/bind_helpers.h"
10#include "base/callback.h"
[email protected]1c232c22013-08-30 02:04:0411#include "base/callback_helpers.h"
[email protected]2e8b52c2011-11-22 00:07:1312#include "net/socket/stream_socket.h"
13#include "remoting/base/constants.h"
[email protected]409ac612011-11-18 04:05:5714#include "remoting/proto/control.pb.h"
[email protected]409ac612011-11-18 04:05:5715#include "remoting/proto/internal.pb.h"
16#include "remoting/protocol/client_stub.h"
[email protected]b76a2742014-04-10 05:38:2617#include "remoting/protocol/message_serialization.h"
[email protected]409ac612011-11-18 04:05:5718
19namespace remoting {
20namespace protocol {
21
[email protected]cb959342013-09-12 05:39:0222namespace {
23
24// 32-bit BGRA is 4 bytes per pixel.
25const int kBytesPerPixel = 4;
26
27bool CursorShapeIsValid(const CursorShapeInfo& cursor_shape) {
28 if (!cursor_shape.has_data() ||
29 !cursor_shape.has_width() ||
30 !cursor_shape.has_height() ||
31 !cursor_shape.has_hotspot_x() ||
32 !cursor_shape.has_hotspot_y()) {
33 LOG(ERROR) << "Cursor shape is missing required fields.";
34 return false;
35 }
36
37 int width = cursor_shape.width();
38 int height = cursor_shape.height();
39
40 // Verify that |width| and |height| are within sane limits. Otherwise integer
41 // overflow can occur while calculating |cursor_total_bytes| below.
kelvinpefa63e02015-01-06 23:50:0742 if (width < 0 || width > (SHRT_MAX / 2) ||
43 height < 0 || height > (SHRT_MAX / 2)) {
[email protected]cb959342013-09-12 05:39:0244 LOG(ERROR) << "Cursor dimensions are out of bounds for SetCursor: "
45 << width << "x" << height;
46 return false;
47 }
48
avi5a080f012015-12-22 23:15:4349 uint32_t cursor_total_bytes = width * height * kBytesPerPixel;
[email protected]cb959342013-09-12 05:39:0250 if (cursor_shape.data().size() < cursor_total_bytes) {
51 LOG(ERROR) << "Expected " << cursor_total_bytes << " bytes for a "
52 << width << "x" << height << " cursor. Only received "
53 << cursor_shape.data().size() << " bytes";
54 return false;
55 }
56
57 return true;
58}
59
60} // namespace
61
[email protected]409ac612011-11-18 04:05:5762ClientControlDispatcher::ClientControlDispatcher()
[email protected]2e8b52c2011-11-22 00:07:1363 : ChannelDispatcherBase(kControlChannelName),
sergeyuc5f104b2015-01-09 19:33:2464 client_stub_(nullptr),
sergeyu203832d2015-01-10 21:44:0965 clipboard_stub_(nullptr),
66 parser_(base::Bind(&ClientControlDispatcher::OnMessageReceived,
67 base::Unretained(this)),
68 reader()) {
[email protected]409ac612011-11-18 04:05:5769}
70
71ClientControlDispatcher::~ClientControlDispatcher() {
[email protected]409ac612011-11-18 04:05:5772}
73
[email protected]e265ad72012-03-16 17:28:0374void ClientControlDispatcher::InjectClipboardEvent(
75 const ClipboardEvent& event) {
76 ControlMessage message;
77 message.mutable_clipboard_event()->CopyFrom(event);
sergeyu203832d2015-01-10 21:44:0978 writer()->Write(SerializeAndFrameMessage(message), base::Closure());
[email protected]e265ad72012-03-16 17:28:0379}
80
[email protected]48a8ca32013-02-13 04:31:0181void ClientControlDispatcher::NotifyClientResolution(
82 const ClientResolution& resolution) {
[email protected]f2b9cf32012-04-27 00:13:4383 ControlMessage message;
[email protected]48a8ca32013-02-13 04:31:0184 message.mutable_client_resolution()->CopyFrom(resolution);
sergeyu203832d2015-01-10 21:44:0985 writer()->Write(SerializeAndFrameMessage(message), base::Closure());
[email protected]f2b9cf32012-04-27 00:13:4386}
87
[email protected]50d71c72012-05-03 01:28:5588void ClientControlDispatcher::ControlVideo(const VideoControl& video_control) {
89 ControlMessage message;
90 message.mutable_video_control()->CopyFrom(video_control);
sergeyu203832d2015-01-10 21:44:0991 writer()->Write(SerializeAndFrameMessage(message), base::Closure());
[email protected]50d71c72012-05-03 01:28:5592}
93
[email protected]f458bed2012-10-18 03:27:5994void ClientControlDispatcher::ControlAudio(const AudioControl& audio_control) {
95 ControlMessage message;
96 message.mutable_audio_control()->CopyFrom(audio_control);
sergeyu203832d2015-01-10 21:44:0997 writer()->Write(SerializeAndFrameMessage(message), base::Closure());
[email protected]f458bed2012-10-18 03:27:5998}
99
[email protected]a5d181f2013-04-19 14:55:37100void ClientControlDispatcher::SetCapabilities(
101 const Capabilities& capabilities) {
102 ControlMessage message;
103 message.mutable_capabilities()->CopyFrom(capabilities);
sergeyu203832d2015-01-10 21:44:09104 writer()->Write(SerializeAndFrameMessage(message), base::Closure());
[email protected]a5d181f2013-04-19 14:55:37105}
106
[email protected]9ffa78a22013-05-10 04:35:10107void ClientControlDispatcher::RequestPairing(
108 const PairingRequest& pairing_request) {
109 ControlMessage message;
110 message.mutable_pairing_request()->CopyFrom(pairing_request);
sergeyu203832d2015-01-10 21:44:09111 writer()->Write(SerializeAndFrameMessage(message), base::Closure());
[email protected]9ffa78a22013-05-10 04:35:10112}
113
[email protected]09eabd65c2013-08-13 00:13:48114void ClientControlDispatcher::DeliverClientMessage(
115 const ExtensionMessage& message) {
116 ControlMessage control_message;
117 control_message.mutable_extension_message()->CopyFrom(message);
sergeyu203832d2015-01-10 21:44:09118 writer()->Write(SerializeAndFrameMessage(control_message), base::Closure());
[email protected]09eabd65c2013-08-13 00:13:48119}
120
[email protected]409ac612011-11-18 04:05:57121void ClientControlDispatcher::OnMessageReceived(
sergeyu203832d2015-01-10 21:44:09122 scoped_ptr<ControlMessage> message,
123 const base::Closure& done_task) {
[email protected]409ac612011-11-18 04:05:57124 DCHECK(client_stub_);
[email protected]ba6d1c2d2012-03-31 01:28:38125 DCHECK(clipboard_stub_);
[email protected]409ac612011-11-18 04:05:57126 base::ScopedClosureRunner done_runner(done_task);
[email protected]ba6d1c2d2012-03-31 01:28:38127
128 if (message->has_clipboard_event()) {
129 clipboard_stub_->InjectClipboardEvent(message->clipboard_event());
[email protected]a5d181f2013-04-19 14:55:37130 } else if (message->has_capabilities()) {
131 client_stub_->SetCapabilities(message->capabilities());
[email protected]d65e15bd2012-06-02 22:16:41132 } else if (message->has_cursor_shape()) {
[email protected]cb959342013-09-12 05:39:02133 if (CursorShapeIsValid(message->cursor_shape()))
134 client_stub_->SetCursorShape(message->cursor_shape());
[email protected]9ffa78a22013-05-10 04:35:10135 } else if (message->has_pairing_response()) {
136 client_stub_->SetPairingResponse(message->pairing_response());
[email protected]09eabd65c2013-08-13 00:13:48137 } else if (message->has_extension_message()) {
138 client_stub_->DeliverHostMessage(message->extension_message());
[email protected]ba6d1c2d2012-03-31 01:28:38139 } else {
140 LOG(WARNING) << "Unknown control message received.";
141 }
[email protected]409ac612011-11-18 04:05:57142}
143
144} // namespace protocol
145} // namespace remoting