blob: 8e2b9d8acd6cf1abc9af5226156881d5133b8dee [file] [log] [blame]
[email protected]3f118f72013-05-03 19:12:011// 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]b4b68f82013-11-02 01:27:035#include "remoting/host/native_messaging/native_messaging_writer.h"
[email protected]3f118f72013-05-03 19:12:016
avic5960f32015-12-22 22:49:487#include <stddef.h>
8#include <stdint.h>
9
[email protected]3f118f72013-05-03 19:12:0110#include <string>
sergeyu1417e0132015-12-23 19:01:2211#include <utility>
[email protected]3f118f72013-05-03 19:12:0112
[email protected]3f118f72013-05-03 19:12:0113#include "base/json/json_writer.h"
Hans Wennborg1a0cd302020-04-22 15:45:2414#include "base/logging.h"
[email protected]3f118f72013-05-03 19:12:0115
16namespace {
17
18// 4-byte type used for the message header.
avic5960f32015-12-22 22:49:4819typedef uint32_t MessageLengthType;
[email protected]3f118f72013-05-03 19:12:0120
21// Defined as an int, for passing to APIs that take an int, to avoid
22// signed/unsigned warnings about implicit cast.
23const 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.
30const size_t kMaximumMessageSize = 1024 * 1024;
31
[email protected]3f118f72013-05-03 19:12:0132} // namespace
33
34namespace remoting {
35
[email protected]0fa0bbf2014-05-02 04:28:0036NativeMessagingWriter::NativeMessagingWriter(base::File file)
sergeyu1417e0132015-12-23 19:01:2237 : write_stream_(std::move(file)), fail_(false) {}
[email protected]3f118f72013-05-03 19:12:0138
Chris Watkins6fe52aa2017-11-28 03:24:0539NativeMessagingWriter::~NativeMessagingWriter() = default;
[email protected]3f118f72013-05-03 19:12:0140
41bool 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;
estade8d046462015-05-16 01:02:3448 base::JSONWriter::Write(message, &message_json);
[email protected]3f118f72013-05-03 19:12:0149
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]86993fd2014-04-07 05:55:2457 int result = write_stream_.WriteAtCurrentPos(
58 reinterpret_cast<char*>(&message_length), kMessageHeaderSize);
[email protected]3f118f72013-05-03 19:12:0159 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]86993fd2014-04-07 05:55:2471 result = write_stream_.WriteAtCurrentPos(message_json.data(),
72 message_length_as_int);
[email protected]3f118f72013-05-03 19:12:0173 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