[email protected] | 3f118f7 | 2013-05-03 19:12:01 | [diff] [blame] | 1 | // Copyright 2013 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. |
| 4 | |
[email protected] | b4b68f8 | 2013-11-02 01:27:03 | [diff] [blame] | 5 | #include "remoting/host/native_messaging/native_messaging_writer.h" |
[email protected] | 3f118f7 | 2013-05-03 19:12:01 | [diff] [blame] | 6 | |
avi | c5960f3 | 2015-12-22 22:49:48 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
[email protected] | 3f118f7 | 2013-05-03 19:12:01 | [diff] [blame] | 10 | #include <string> |
sergeyu | 1417e013 | 2015-12-23 19:01:22 | [diff] [blame] | 11 | #include <utility> |
[email protected] | 3f118f7 | 2013-05-03 19:12:01 | [diff] [blame] | 12 | |
[email protected] | 3f118f7 | 2013-05-03 19:12:01 | [diff] [blame] | 13 | #include "base/json/json_writer.h" |
Hans Wennborg | 1a0cd30 | 2020-04-22 15:45:24 | [diff] [blame] | 14 | #include "base/logging.h" |
[email protected] | 3f118f7 | 2013-05-03 19:12:01 | [diff] [blame] | 15 | |
| 16 | namespace { |
| 17 | |
| 18 | // 4-byte type used for the message header. |
avi | c5960f3 | 2015-12-22 22:49:48 | [diff] [blame] | 19 | typedef uint32_t MessageLengthType; |
[email protected] | 3f118f7 | 2013-05-03 19:12:01 | [diff] [blame] | 20 | |
| 21 | // Defined as an int, for passing to APIs that take an int, to avoid |
| 22 | // signed/unsigned warnings about implicit cast. |
| 23 | const int kMessageHeaderSize = sizeof(MessageLengthType); |
| 24 | |
| 25 | // Limit the size of sent messages, since Chrome will not accept messages |
| 26 | // larger than 1MB, and this helps deal with the problem of integer overflow |
| 27 | // when passing sizes to net::FileStream APIs that take |int| parameters. |
| 28 | // This is defined as size_t (unsigned type) so it can be compared with the |
| 29 | // result of std::string::length() without compiler warnings. |
| 30 | const size_t kMaximumMessageSize = 1024 * 1024; |
| 31 | |
[email protected] | 3f118f7 | 2013-05-03 19:12:01 | [diff] [blame] | 32 | } // namespace |
| 33 | |
| 34 | namespace remoting { |
| 35 | |
[email protected] | 0fa0bbf | 2014-05-02 04:28:00 | [diff] [blame] | 36 | NativeMessagingWriter::NativeMessagingWriter(base::File file) |
sergeyu | 1417e013 | 2015-12-23 19:01:22 | [diff] [blame] | 37 | : write_stream_(std::move(file)), fail_(false) {} |
[email protected] | 3f118f7 | 2013-05-03 19:12:01 | [diff] [blame] | 38 | |
Chris Watkins | 6fe52aa | 2017-11-28 03:24:05 | [diff] [blame] | 39 | NativeMessagingWriter::~NativeMessagingWriter() = default; |
[email protected] | 3f118f7 | 2013-05-03 19:12:01 | [diff] [blame] | 40 | |
| 41 | bool NativeMessagingWriter::WriteMessage(const base::Value& message) { |
| 42 | if (fail_) { |
| 43 | LOG(ERROR) << "Stream marked as corrupt."; |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | std::string message_json; |
estade | 8d04646 | 2015-05-16 01:02:34 | [diff] [blame] | 48 | base::JSONWriter::Write(message, &message_json); |
[email protected] | 3f118f7 | 2013-05-03 19:12:01 | [diff] [blame] | 49 | |
| 50 | CHECK_LE(message_json.length(), kMaximumMessageSize); |
| 51 | |
| 52 | // Cast from size_t to the proper header type. The check above ensures this |
| 53 | // won't overflow. |
| 54 | MessageLengthType message_length = |
| 55 | static_cast<MessageLengthType>(message_json.length()); |
| 56 | |
[email protected] | 86993fd | 2014-04-07 05:55:24 | [diff] [blame] | 57 | int result = write_stream_.WriteAtCurrentPos( |
| 58 | reinterpret_cast<char*>(&message_length), kMessageHeaderSize); |
[email protected] | 3f118f7 | 2013-05-03 19:12:01 | [diff] [blame] | 59 | if (result != kMessageHeaderSize) { |
| 60 | LOG(ERROR) << "Failed to send message header, write returned " << result; |
| 61 | fail_ = true; |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | // The length check above ensures that the cast won't overflow a signed |
| 66 | // 32-bit int. |
| 67 | int message_length_as_int = message_length; |
| 68 | |
| 69 | // CHECK needed since data() is undefined on an empty std::string. |
| 70 | CHECK(!message_json.empty()); |
[email protected] | 86993fd | 2014-04-07 05:55:24 | [diff] [blame] | 71 | result = write_stream_.WriteAtCurrentPos(message_json.data(), |
| 72 | message_length_as_int); |
[email protected] | 3f118f7 | 2013-05-03 19:12:01 | [diff] [blame] | 73 | if (result != message_length_as_int) { |
| 74 | LOG(ERROR) << "Failed to send message body, write returned " << result; |
| 75 | fail_ = true; |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | } // namespace remoting |