Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / remoting / protocol / client_control_dispatcher.cc
blobf8cdc514b8b3203ad8bb0679accd779ac98d0d3e
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 "net/socket/stream_socket.h"
11 #include "remoting/base/constants.h"
12 #include "remoting/proto/control.pb.h"
13 #include "remoting/proto/internal.pb.h"
14 #include "remoting/protocol/client_stub.h"
15 #include "remoting/protocol/message_serialization.h"
17 namespace remoting {
18 namespace protocol {
20 namespace {
22 // 32-bit BGRA is 4 bytes per pixel.
23 const int kBytesPerPixel = 4;
25 bool CursorShapeIsValid(const CursorShapeInfo& cursor_shape) {
26 if (!cursor_shape.has_data() ||
27 !cursor_shape.has_width() ||
28 !cursor_shape.has_height() ||
29 !cursor_shape.has_hotspot_x() ||
30 !cursor_shape.has_hotspot_y()) {
31 LOG(ERROR) << "Cursor shape is missing required fields.";
32 return false;
35 int width = cursor_shape.width();
36 int height = cursor_shape.height();
38 // Verify that |width| and |height| are within sane limits. Otherwise integer
39 // overflow can occur while calculating |cursor_total_bytes| below.
40 if (width < 0 || width > (SHRT_MAX / 2) ||
41 height < 0 || height > (SHRT_MAX / 2)) {
42 LOG(ERROR) << "Cursor dimensions are out of bounds for SetCursor: "
43 << width << "x" << height;
44 return false;
47 uint32 cursor_total_bytes = width * height * kBytesPerPixel;
48 if (cursor_shape.data().size() < cursor_total_bytes) {
49 LOG(ERROR) << "Expected " << cursor_total_bytes << " bytes for a "
50 << width << "x" << height << " cursor. Only received "
51 << cursor_shape.data().size() << " bytes";
52 return false;
55 return true;
58 } // namespace
60 ClientControlDispatcher::ClientControlDispatcher()
61 : ChannelDispatcherBase(kControlChannelName),
62 client_stub_(nullptr),
63 clipboard_stub_(nullptr),
64 parser_(base::Bind(&ClientControlDispatcher::OnMessageReceived,
65 base::Unretained(this)),
66 reader()) {
69 ClientControlDispatcher::~ClientControlDispatcher() {
72 void ClientControlDispatcher::InjectClipboardEvent(
73 const ClipboardEvent& event) {
74 ControlMessage message;
75 message.mutable_clipboard_event()->CopyFrom(event);
76 writer()->Write(SerializeAndFrameMessage(message), base::Closure());
79 void ClientControlDispatcher::NotifyClientResolution(
80 const ClientResolution& resolution) {
81 ControlMessage message;
82 message.mutable_client_resolution()->CopyFrom(resolution);
83 writer()->Write(SerializeAndFrameMessage(message), base::Closure());
86 void ClientControlDispatcher::ControlVideo(const VideoControl& video_control) {
87 ControlMessage message;
88 message.mutable_video_control()->CopyFrom(video_control);
89 writer()->Write(SerializeAndFrameMessage(message), base::Closure());
92 void ClientControlDispatcher::ControlAudio(const AudioControl& audio_control) {
93 ControlMessage message;
94 message.mutable_audio_control()->CopyFrom(audio_control);
95 writer()->Write(SerializeAndFrameMessage(message), base::Closure());
98 void ClientControlDispatcher::SetCapabilities(
99 const Capabilities& capabilities) {
100 ControlMessage message;
101 message.mutable_capabilities()->CopyFrom(capabilities);
102 writer()->Write(SerializeAndFrameMessage(message), base::Closure());
105 void ClientControlDispatcher::RequestPairing(
106 const PairingRequest& pairing_request) {
107 ControlMessage message;
108 message.mutable_pairing_request()->CopyFrom(pairing_request);
109 writer()->Write(SerializeAndFrameMessage(message), base::Closure());
112 void ClientControlDispatcher::DeliverClientMessage(
113 const ExtensionMessage& message) {
114 ControlMessage control_message;
115 control_message.mutable_extension_message()->CopyFrom(message);
116 writer()->Write(SerializeAndFrameMessage(control_message), base::Closure());
119 void ClientControlDispatcher::OnMessageReceived(
120 scoped_ptr<ControlMessage> message,
121 const base::Closure& done_task) {
122 DCHECK(client_stub_);
123 DCHECK(clipboard_stub_);
124 base::ScopedClosureRunner done_runner(done_task);
126 if (message->has_clipboard_event()) {
127 clipboard_stub_->InjectClipboardEvent(message->clipboard_event());
128 } else if (message->has_capabilities()) {
129 client_stub_->SetCapabilities(message->capabilities());
130 } else if (message->has_cursor_shape()) {
131 if (CursorShapeIsValid(message->cursor_shape()))
132 client_stub_->SetCursorShape(message->cursor_shape());
133 } else if (message->has_pairing_response()) {
134 client_stub_->SetPairingResponse(message->pairing_response());
135 } else if (message->has_extension_message()) {
136 client_stub_->DeliverHostMessage(message->extension_message());
137 } else {
138 LOG(WARNING) << "Unknown control message received.";
142 } // namespace protocol
143 } // namespace remoting