[email protected] | e265ad7 | 2012-03-16 17:28:03 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 409ac61 | 2011-11-18 04:05:57 | [diff] [blame] | 2 | // 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 | |||||
avi | 5a080f01 | 2015-12-22 23:15:43 | [diff] [blame] | 7 | #include <stdint.h> |
8 | |||||
[email protected] | c694427 | 2012-01-06 22:12:28 | [diff] [blame] | 9 | #include "base/bind_helpers.h" |
10 | #include "base/callback.h" | ||||
[email protected] | 2e8b52c | 2011-11-22 00:07:13 | [diff] [blame] | 11 | #include "net/socket/stream_socket.h" |
sergeyu | f1005f6 | 2016-02-03 21:11:30 | [diff] [blame] | 12 | #include "remoting/base/compound_buffer.h" |
[email protected] | 2e8b52c | 2011-11-22 00:07:13 | [diff] [blame] | 13 | #include "remoting/base/constants.h" |
[email protected] | 409ac61 | 2011-11-18 04:05:57 | [diff] [blame] | 14 | #include "remoting/proto/control.pb.h" |
[email protected] | 409ac61 | 2011-11-18 04:05:57 | [diff] [blame] | 15 | #include "remoting/proto/internal.pb.h" |
16 | #include "remoting/protocol/client_stub.h" | ||||
sergeyu | f1005f6 | 2016-02-03 21:11:30 | [diff] [blame] | 17 | #include "remoting/protocol/message_pipe.h" |
[email protected] | b76a274 | 2014-04-10 05:38:26 | [diff] [blame] | 18 | #include "remoting/protocol/message_serialization.h" |
[email protected] | 409ac61 | 2011-11-18 04:05:57 | [diff] [blame] | 19 | |
20 | namespace remoting { | ||||
21 | namespace protocol { | ||||
22 | |||||
[email protected] | cb95934 | 2013-09-12 05:39:02 | [diff] [blame] | 23 | namespace { |
24 | |||||
25 | // 32-bit BGRA is 4 bytes per pixel. | ||||
26 | const int kBytesPerPixel = 4; | ||||
27 | |||||
28 | bool CursorShapeIsValid(const CursorShapeInfo& cursor_shape) { | ||||
29 | if (!cursor_shape.has_data() || | ||||
30 | !cursor_shape.has_width() || | ||||
31 | !cursor_shape.has_height() || | ||||
32 | !cursor_shape.has_hotspot_x() || | ||||
33 | !cursor_shape.has_hotspot_y()) { | ||||
34 | LOG(ERROR) << "Cursor shape is missing required fields."; | ||||
35 | return false; | ||||
36 | } | ||||
37 | |||||
38 | int width = cursor_shape.width(); | ||||
39 | int height = cursor_shape.height(); | ||||
40 | |||||
41 | // Verify that |width| and |height| are within sane limits. Otherwise integer | ||||
42 | // overflow can occur while calculating |cursor_total_bytes| below. | ||||
kelvinp | efa63e0 | 2015-01-06 23:50:07 | [diff] [blame] | 43 | if (width < 0 || width > (SHRT_MAX / 2) || |
44 | height < 0 || height > (SHRT_MAX / 2)) { | ||||
[email protected] | cb95934 | 2013-09-12 05:39:02 | [diff] [blame] | 45 | LOG(ERROR) << "Cursor dimensions are out of bounds for SetCursor: " |
46 | << width << "x" << height; | ||||
47 | return false; | ||||
48 | } | ||||
49 | |||||
avi | 5a080f01 | 2015-12-22 23:15:43 | [diff] [blame] | 50 | uint32_t cursor_total_bytes = width * height * kBytesPerPixel; |
[email protected] | cb95934 | 2013-09-12 05:39:02 | [diff] [blame] | 51 | if (cursor_shape.data().size() < cursor_total_bytes) { |
52 | LOG(ERROR) << "Expected " << cursor_total_bytes << " bytes for a " | ||||
53 | << width << "x" << height << " cursor. Only received " | ||||
54 | << cursor_shape.data().size() << " bytes"; | ||||
55 | return false; | ||||
56 | } | ||||
57 | |||||
58 | return true; | ||||
59 | } | ||||
60 | |||||
61 | } // namespace | ||||
62 | |||||
[email protected] | 409ac61 | 2011-11-18 04:05:57 | [diff] [blame] | 63 | ClientControlDispatcher::ClientControlDispatcher() |
sergeyu | d8af2ca | 2016-01-30 03:04:36 | [diff] [blame] | 64 | : ChannelDispatcherBase(kControlChannelName) {} |
Chris Watkins | 6fe52aa | 2017-11-28 03:24:05 | [diff] [blame] | 65 | ClientControlDispatcher::~ClientControlDispatcher() = default; |
[email protected] | 409ac61 | 2011-11-18 04:05:57 | [diff] [blame] | 66 | |
[email protected] | e265ad7 | 2012-03-16 17:28:03 | [diff] [blame] | 67 | void ClientControlDispatcher::InjectClipboardEvent( |
68 | const ClipboardEvent& event) { | ||||
69 | ControlMessage message; | ||||
70 | message.mutable_clipboard_event()->CopyFrom(event); | ||||
sergeyu | f1005f6 | 2016-02-03 21:11:30 | [diff] [blame] | 71 | message_pipe()->Send(&message, base::Closure()); |
[email protected] | e265ad7 | 2012-03-16 17:28:03 | [diff] [blame] | 72 | } |
73 | |||||
[email protected] | 48a8ca3 | 2013-02-13 04:31:01 | [diff] [blame] | 74 | void ClientControlDispatcher::NotifyClientResolution( |
75 | const ClientResolution& resolution) { | ||||
[email protected] | f2b9cf3 | 2012-04-27 00:13:43 | [diff] [blame] | 76 | ControlMessage message; |
[email protected] | 48a8ca3 | 2013-02-13 04:31:01 | [diff] [blame] | 77 | message.mutable_client_resolution()->CopyFrom(resolution); |
sergeyu | f1005f6 | 2016-02-03 21:11:30 | [diff] [blame] | 78 | message_pipe()->Send(&message, base::Closure()); |
[email protected] | f2b9cf3 | 2012-04-27 00:13:43 | [diff] [blame] | 79 | } |
80 | |||||
[email protected] | 50d71c7 | 2012-05-03 01:28:55 | [diff] [blame] | 81 | void ClientControlDispatcher::ControlVideo(const VideoControl& video_control) { |
82 | ControlMessage message; | ||||
83 | message.mutable_video_control()->CopyFrom(video_control); | ||||
sergeyu | f1005f6 | 2016-02-03 21:11:30 | [diff] [blame] | 84 | message_pipe()->Send(&message, base::Closure()); |
[email protected] | 50d71c7 | 2012-05-03 01:28:55 | [diff] [blame] | 85 | } |
86 | |||||
[email protected] | f458bed | 2012-10-18 03:27:59 | [diff] [blame] | 87 | void ClientControlDispatcher::ControlAudio(const AudioControl& audio_control) { |
88 | ControlMessage message; | ||||
89 | message.mutable_audio_control()->CopyFrom(audio_control); | ||||
sergeyu | f1005f6 | 2016-02-03 21:11:30 | [diff] [blame] | 90 | message_pipe()->Send(&message, base::Closure()); |
[email protected] | f458bed | 2012-10-18 03:27:59 | [diff] [blame] | 91 | } |
92 | |||||
[email protected] | a5d181f | 2013-04-19 14:55:37 | [diff] [blame] | 93 | void ClientControlDispatcher::SetCapabilities( |
94 | const Capabilities& capabilities) { | ||||
95 | ControlMessage message; | ||||
96 | message.mutable_capabilities()->CopyFrom(capabilities); | ||||
sergeyu | f1005f6 | 2016-02-03 21:11:30 | [diff] [blame] | 97 | message_pipe()->Send(&message, base::Closure()); |
[email protected] | a5d181f | 2013-04-19 14:55:37 | [diff] [blame] | 98 | } |
99 | |||||
[email protected] | 9ffa78a2 | 2013-05-10 04:35:10 | [diff] [blame] | 100 | void ClientControlDispatcher::RequestPairing( |
101 | const PairingRequest& pairing_request) { | ||||
102 | ControlMessage message; | ||||
103 | message.mutable_pairing_request()->CopyFrom(pairing_request); | ||||
sergeyu | f1005f6 | 2016-02-03 21:11:30 | [diff] [blame] | 104 | message_pipe()->Send(&message, base::Closure()); |
[email protected] | 9ffa78a2 | 2013-05-10 04:35:10 | [diff] [blame] | 105 | } |
106 | |||||
[email protected] | 09eabd65c | 2013-08-13 00:13:48 | [diff] [blame] | 107 | void ClientControlDispatcher::DeliverClientMessage( |
108 | const ExtensionMessage& message) { | ||||
109 | ControlMessage control_message; | ||||
110 | control_message.mutable_extension_message()->CopyFrom(message); | ||||
sergeyu | f1005f6 | 2016-02-03 21:11:30 | [diff] [blame] | 111 | message_pipe()->Send(&control_message, base::Closure()); |
[email protected] | 09eabd65c | 2013-08-13 00:13:48 | [diff] [blame] | 112 | } |
113 | |||||
sergeyu | d8af2ca | 2016-01-30 03:04:36 | [diff] [blame] | 114 | void ClientControlDispatcher::OnIncomingMessage( |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 115 | std::unique_ptr<CompoundBuffer> buffer) { |
[email protected] | 409ac61 | 2011-11-18 04:05:57 | [diff] [blame] | 116 | DCHECK(client_stub_); |
[email protected] | ba6d1c2d | 2012-03-31 01:28:38 | [diff] [blame] | 117 | DCHECK(clipboard_stub_); |
[email protected] | ba6d1c2d | 2012-03-31 01:28:38 | [diff] [blame] | 118 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 119 | std::unique_ptr<ControlMessage> message = |
sergeyu | d8af2ca | 2016-01-30 03:04:36 | [diff] [blame] | 120 | ParseMessage<ControlMessage>(buffer.get()); |
121 | if (!message) | ||||
122 | return; | ||||
123 | |||||
[email protected] | ba6d1c2d | 2012-03-31 01:28:38 | [diff] [blame] | 124 | if (message->has_clipboard_event()) { |
125 | clipboard_stub_->InjectClipboardEvent(message->clipboard_event()); | ||||
[email protected] | a5d181f | 2013-04-19 14:55:37 | [diff] [blame] | 126 | } else if (message->has_capabilities()) { |
127 | client_stub_->SetCapabilities(message->capabilities()); | ||||
[email protected] | d65e15bd | 2012-06-02 22:16:41 | [diff] [blame] | 128 | } else if (message->has_cursor_shape()) { |
[email protected] | cb95934 | 2013-09-12 05:39:02 | [diff] [blame] | 129 | if (CursorShapeIsValid(message->cursor_shape())) |
130 | client_stub_->SetCursorShape(message->cursor_shape()); | ||||
[email protected] | 9ffa78a2 | 2013-05-10 04:35:10 | [diff] [blame] | 131 | } else if (message->has_pairing_response()) { |
132 | client_stub_->SetPairingResponse(message->pairing_response()); | ||||
[email protected] | 09eabd65c | 2013-08-13 00:13:48 | [diff] [blame] | 133 | } else if (message->has_extension_message()) { |
134 | client_stub_->DeliverHostMessage(message->extension_message()); | ||||
sergeyu | 00a67b1 | 2016-04-01 00:07:00 | [diff] [blame] | 135 | } else if (message->has_video_layout()) { |
136 | client_stub_->SetVideoLayout(message->video_layout()); | ||||
[email protected] | ba6d1c2d | 2012-03-31 01:28:38 | [diff] [blame] | 137 | } else { |
138 | LOG(WARNING) << "Unknown control message received."; | ||||
139 | } | ||||
[email protected] | 409ac61 | 2011-11-18 04:05:57 | [diff] [blame] | 140 | } |
141 | |||||
142 | } // namespace protocol | ||||
143 | } // namespace remoting |