Revert of Revert of Roll android_tools f6e2370:2a860d8 (patchset #1 id:1 of https...
[chromium-blink-merge.git] / remoting / protocol / client_control_dispatcher.cc
blobd13e53fe008d2870e39f4a4a4b1e4552f88f1bc9
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/protocol/client_control_dispatcher.h"
7 #include "base/bind_helpers.h"
8 #include "base/callback.h"
9 #include "base/callback_helpers.h"
10 #include "base/message_loop/message_loop_proxy.h"
11 #include "net/socket/stream_socket.h"
12 #include "remoting/base/constants.h"
13 #include "remoting/proto/control.pb.h"
14 #include "remoting/proto/internal.pb.h"
15 #include "remoting/protocol/client_stub.h"
16 #include "remoting/protocol/message_serialization.h"
18 namespace remoting {
19 namespace protocol {
21 namespace {
23 // 32-bit BGRA is 4 bytes per pixel.
24 const int kBytesPerPixel = 4;
26 bool CursorShapeIsValid(const CursorShapeInfo& cursor_shape) {
27 if (!cursor_shape.has_data() ||
28 !cursor_shape.has_width() ||
29 !cursor_shape.has_height() ||
30 !cursor_shape.has_hotspot_x() ||
31 !cursor_shape.has_hotspot_y()) {
32 LOG(ERROR) << "Cursor shape is missing required fields.";
33 return false;
36 int width = cursor_shape.width();
37 int height = cursor_shape.height();
39 // Verify that |width| and |height| are within sane limits. Otherwise integer
40 // overflow can occur while calculating |cursor_total_bytes| below.
41 if (width < 0 || width > (SHRT_MAX / 2) ||
42 height < 0 || height > (SHRT_MAX / 2)) {
43 LOG(ERROR) << "Cursor dimensions are out of bounds for SetCursor: "
44 << width << "x" << height;
45 return false;
48 uint32 cursor_total_bytes = width * height * kBytesPerPixel;
49 if (cursor_shape.data().size() < cursor_total_bytes) {
50 LOG(ERROR) << "Expected " << cursor_total_bytes << " bytes for a "
51 << width << "x" << height << " cursor. Only received "
52 << cursor_shape.data().size() << " bytes";
53 return false;
56 return true;
59 } // namespace
61 ClientControlDispatcher::ClientControlDispatcher()
62 : ChannelDispatcherBase(kControlChannelName),
63 client_stub_(nullptr),
64 clipboard_stub_(nullptr),
65 parser_(base::Bind(&ClientControlDispatcher::OnMessageReceived,
66 base::Unretained(this)),
67 reader()) {
70 ClientControlDispatcher::~ClientControlDispatcher() {
73 void ClientControlDispatcher::InjectClipboardEvent(
74 const ClipboardEvent& event) {
75 ControlMessage message;
76 message.mutable_clipboard_event()->CopyFrom(event);
77 writer()->Write(SerializeAndFrameMessage(message), base::Closure());
80 void ClientControlDispatcher::NotifyClientResolution(
81 const ClientResolution& resolution) {
82 ControlMessage message;
83 message.mutable_client_resolution()->CopyFrom(resolution);
84 writer()->Write(SerializeAndFrameMessage(message), base::Closure());
87 void ClientControlDispatcher::ControlVideo(const VideoControl& video_control) {
88 ControlMessage message;
89 message.mutable_video_control()->CopyFrom(video_control);
90 writer()->Write(SerializeAndFrameMessage(message), base::Closure());
93 void ClientControlDispatcher::ControlAudio(const AudioControl& audio_control) {
94 ControlMessage message;
95 message.mutable_audio_control()->CopyFrom(audio_control);
96 writer()->Write(SerializeAndFrameMessage(message), base::Closure());
99 void ClientControlDispatcher::SetCapabilities(
100 const Capabilities& capabilities) {
101 ControlMessage message;
102 message.mutable_capabilities()->CopyFrom(capabilities);
103 writer()->Write(SerializeAndFrameMessage(message), base::Closure());
106 void ClientControlDispatcher::RequestPairing(
107 const PairingRequest& pairing_request) {
108 ControlMessage message;
109 message.mutable_pairing_request()->CopyFrom(pairing_request);
110 writer()->Write(SerializeAndFrameMessage(message), base::Closure());
113 void ClientControlDispatcher::DeliverClientMessage(
114 const ExtensionMessage& message) {
115 ControlMessage control_message;
116 control_message.mutable_extension_message()->CopyFrom(message);
117 writer()->Write(SerializeAndFrameMessage(control_message), base::Closure());
120 void ClientControlDispatcher::OnMessageReceived(
121 scoped_ptr<ControlMessage> message,
122 const base::Closure& done_task) {
123 DCHECK(client_stub_);
124 DCHECK(clipboard_stub_);
125 base::ScopedClosureRunner done_runner(done_task);
127 if (message->has_clipboard_event()) {
128 clipboard_stub_->InjectClipboardEvent(message->clipboard_event());
129 } else if (message->has_capabilities()) {
130 client_stub_->SetCapabilities(message->capabilities());
131 } else if (message->has_cursor_shape()) {
132 if (CursorShapeIsValid(message->cursor_shape()))
133 client_stub_->SetCursorShape(message->cursor_shape());
134 } else if (message->has_pairing_response()) {
135 client_stub_->SetPairingResponse(message->pairing_response());
136 } else if (message->has_extension_message()) {
137 client_stub_->DeliverHostMessage(message->extension_message());
138 } else {
139 LOG(WARNING) << "Unknown control message received.";
143 } // namespace protocol
144 } // namespace remoting