[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 | |
[email protected] | c694427 | 2012-01-06 22:12:28 | [diff] [blame] | 7 | #include "base/bind_helpers.h" |
| 8 | #include "base/callback.h" |
[email protected] | 1c232c2 | 2013-08-30 02:04:04 | [diff] [blame] | 9 | #include "base/callback_helpers.h" |
[email protected] | 7ccb707 | 2013-06-10 20:56:28 | [diff] [blame] | 10 | #include "base/message_loop/message_loop_proxy.h" |
[email protected] | 2e8b52c | 2011-11-22 00:07:13 | [diff] [blame] | 11 | #include "net/socket/stream_socket.h" |
| 12 | #include "remoting/base/constants.h" |
[email protected] | 409ac61 | 2011-11-18 04:05:57 | [diff] [blame] | 13 | #include "remoting/proto/control.pb.h" |
[email protected] | 409ac61 | 2011-11-18 04:05:57 | [diff] [blame] | 14 | #include "remoting/proto/internal.pb.h" |
| 15 | #include "remoting/protocol/client_stub.h" |
[email protected] | b76a274 | 2014-04-10 05:38:26 | [diff] [blame] | 16 | #include "remoting/protocol/message_serialization.h" |
[email protected] | 409ac61 | 2011-11-18 04:05:57 | [diff] [blame] | 17 | |
| 18 | namespace remoting { |
| 19 | namespace protocol { |
| 20 | |
[email protected] | cb95934 | 2013-09-12 05:39:02 | [diff] [blame] | 21 | namespace { |
| 22 | |
| 23 | // 32-bit BGRA is 4 bytes per pixel. |
| 24 | const int kBytesPerPixel = 4; |
| 25 | |
| 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; |
| 34 | } |
| 35 | |
| 36 | int width = cursor_shape.width(); |
| 37 | int height = cursor_shape.height(); |
| 38 | |
| 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; |
| 46 | } |
| 47 | |
| 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; |
| 54 | } |
| 55 | |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | } // namespace |
| 60 | |
[email protected] | 409ac61 | 2011-11-18 04:05:57 | [diff] [blame] | 61 | ClientControlDispatcher::ClientControlDispatcher() |
[email protected] | 2e8b52c | 2011-11-22 00:07:13 | [diff] [blame] | 62 | : ChannelDispatcherBase(kControlChannelName), |
| 63 | client_stub_(NULL), |
[email protected] | a3464dca | 2012-05-24 01:27:09 | [diff] [blame] | 64 | clipboard_stub_(NULL) { |
[email protected] | 409ac61 | 2011-11-18 04:05:57 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | ClientControlDispatcher::~ClientControlDispatcher() { |
[email protected] | a3464dca | 2012-05-24 01:27:09 | [diff] [blame] | 68 | writer_.Close(); |
[email protected] | 409ac61 | 2011-11-18 04:05:57 | [diff] [blame] | 69 | } |
| 70 | |
[email protected] | 2e8b52c | 2011-11-22 00:07:13 | [diff] [blame] | 71 | void ClientControlDispatcher::OnInitialized() { |
[email protected] | 409ac61 | 2011-11-18 04:05:57 | [diff] [blame] | 72 | // TODO(garykac): Set write failed callback. |
[email protected] | a3464dca | 2012-05-24 01:27:09 | [diff] [blame] | 73 | writer_.Init(channel(), BufferedSocketWriter::WriteFailedCallback()); |
[email protected] | 2e8b52c | 2011-11-22 00:07:13 | [diff] [blame] | 74 | reader_.Init(channel(), base::Bind( |
[email protected] | 409ac61 | 2011-11-18 04:05:57 | [diff] [blame] | 75 | &ClientControlDispatcher::OnMessageReceived, base::Unretained(this))); |
| 76 | } |
| 77 | |
[email protected] | e265ad7 | 2012-03-16 17:28:03 | [diff] [blame] | 78 | void ClientControlDispatcher::InjectClipboardEvent( |
| 79 | const ClipboardEvent& event) { |
| 80 | ControlMessage message; |
| 81 | message.mutable_clipboard_event()->CopyFrom(event); |
[email protected] | a3464dca | 2012-05-24 01:27:09 | [diff] [blame] | 82 | writer_.Write(SerializeAndFrameMessage(message), base::Closure()); |
[email protected] | e265ad7 | 2012-03-16 17:28:03 | [diff] [blame] | 83 | } |
| 84 | |
[email protected] | 48a8ca3 | 2013-02-13 04:31:01 | [diff] [blame] | 85 | void ClientControlDispatcher::NotifyClientResolution( |
| 86 | const ClientResolution& resolution) { |
[email protected] | f2b9cf3 | 2012-04-27 00:13:43 | [diff] [blame] | 87 | ControlMessage message; |
[email protected] | 48a8ca3 | 2013-02-13 04:31:01 | [diff] [blame] | 88 | message.mutable_client_resolution()->CopyFrom(resolution); |
[email protected] | a3464dca | 2012-05-24 01:27:09 | [diff] [blame] | 89 | writer_.Write(SerializeAndFrameMessage(message), base::Closure()); |
[email protected] | f2b9cf3 | 2012-04-27 00:13:43 | [diff] [blame] | 90 | } |
| 91 | |
[email protected] | 50d71c7 | 2012-05-03 01:28:55 | [diff] [blame] | 92 | void ClientControlDispatcher::ControlVideo(const VideoControl& video_control) { |
| 93 | ControlMessage message; |
| 94 | message.mutable_video_control()->CopyFrom(video_control); |
[email protected] | a3464dca | 2012-05-24 01:27:09 | [diff] [blame] | 95 | writer_.Write(SerializeAndFrameMessage(message), base::Closure()); |
[email protected] | 50d71c7 | 2012-05-03 01:28:55 | [diff] [blame] | 96 | } |
| 97 | |
[email protected] | f458bed | 2012-10-18 03:27:59 | [diff] [blame] | 98 | void ClientControlDispatcher::ControlAudio(const AudioControl& audio_control) { |
| 99 | ControlMessage message; |
| 100 | message.mutable_audio_control()->CopyFrom(audio_control); |
| 101 | writer_.Write(SerializeAndFrameMessage(message), base::Closure()); |
| 102 | } |
| 103 | |
[email protected] | a5d181f | 2013-04-19 14:55:37 | [diff] [blame] | 104 | void ClientControlDispatcher::SetCapabilities( |
| 105 | const Capabilities& capabilities) { |
| 106 | ControlMessage message; |
| 107 | message.mutable_capabilities()->CopyFrom(capabilities); |
| 108 | writer_.Write(SerializeAndFrameMessage(message), base::Closure()); |
| 109 | } |
| 110 | |
[email protected] | 9ffa78a2 | 2013-05-10 04:35:10 | [diff] [blame] | 111 | void ClientControlDispatcher::RequestPairing( |
| 112 | const PairingRequest& pairing_request) { |
| 113 | ControlMessage message; |
| 114 | message.mutable_pairing_request()->CopyFrom(pairing_request); |
| 115 | writer_.Write(SerializeAndFrameMessage(message), base::Closure()); |
| 116 | } |
| 117 | |
[email protected] | 09eabd65c | 2013-08-13 00:13:48 | [diff] [blame] | 118 | void ClientControlDispatcher::DeliverClientMessage( |
| 119 | const ExtensionMessage& message) { |
| 120 | ControlMessage control_message; |
| 121 | control_message.mutable_extension_message()->CopyFrom(message); |
| 122 | writer_.Write(SerializeAndFrameMessage(control_message), base::Closure()); |
| 123 | } |
| 124 | |
[email protected] | 409ac61 | 2011-11-18 04:05:57 | [diff] [blame] | 125 | void ClientControlDispatcher::OnMessageReceived( |
[email protected] | 9302fce | 2012-03-28 03:57:57 | [diff] [blame] | 126 | scoped_ptr<ControlMessage> message, const base::Closure& done_task) { |
[email protected] | 409ac61 | 2011-11-18 04:05:57 | [diff] [blame] | 127 | DCHECK(client_stub_); |
[email protected] | ba6d1c2d | 2012-03-31 01:28:38 | [diff] [blame] | 128 | DCHECK(clipboard_stub_); |
[email protected] | 409ac61 | 2011-11-18 04:05:57 | [diff] [blame] | 129 | base::ScopedClosureRunner done_runner(done_task); |
[email protected] | ba6d1c2d | 2012-03-31 01:28:38 | [diff] [blame] | 130 | |
| 131 | if (message->has_clipboard_event()) { |
| 132 | clipboard_stub_->InjectClipboardEvent(message->clipboard_event()); |
[email protected] | a5d181f | 2013-04-19 14:55:37 | [diff] [blame] | 133 | } else if (message->has_capabilities()) { |
| 134 | client_stub_->SetCapabilities(message->capabilities()); |
[email protected] | d65e15bd | 2012-06-02 22:16:41 | [diff] [blame] | 135 | } else if (message->has_cursor_shape()) { |
[email protected] | cb95934 | 2013-09-12 05:39:02 | [diff] [blame] | 136 | if (CursorShapeIsValid(message->cursor_shape())) |
| 137 | client_stub_->SetCursorShape(message->cursor_shape()); |
[email protected] | 9ffa78a2 | 2013-05-10 04:35:10 | [diff] [blame] | 138 | } else if (message->has_pairing_response()) { |
| 139 | client_stub_->SetPairingResponse(message->pairing_response()); |
[email protected] | 09eabd65c | 2013-08-13 00:13:48 | [diff] [blame] | 140 | } else if (message->has_extension_message()) { |
| 141 | client_stub_->DeliverHostMessage(message->extension_message()); |
[email protected] | ba6d1c2d | 2012-03-31 01:28:38 | [diff] [blame] | 142 | } else { |
| 143 | LOG(WARNING) << "Unknown control message received."; |
| 144 | } |
[email protected] | 409ac61 | 2011-11-18 04:05:57 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | } // namespace protocol |
| 148 | } // namespace remoting |