Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 | |
| 5 #include "remoting/host/security_key/remote_security_key_message_writer.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <string> | |
| 11 #include <utility> | |
| 12 | |
| 13 #include "remoting/host/security_key/security_key_message.h" | |
| 14 | |
| 15 namespace remoting { | |
| 16 | |
| 17 RemoteSecurityKeyMessageWriter::RemoteSecurityKeyMessageWriter( | |
| 18 base::File output_file) | |
| 19 : output_stream_(std::move(output_file)) {} | |
| 20 | |
| 21 RemoteSecurityKeyMessageWriter::~RemoteSecurityKeyMessageWriter() {} | |
| 22 | |
| 23 bool RemoteSecurityKeyMessageWriter::WriteMessage( | |
|
Sergey Ulanov
2016/03/28 18:20:07
Maybe call this WriteEmptyMessage()? Style guide i
joedow
2016/03/29 22:14:14
Done.
| |
| 24 RemoteSecurityKeyMessageType message_type) { | |
| 25 return WriteMessage(message_type, std::string()); | |
| 26 } | |
| 27 | |
| 28 bool RemoteSecurityKeyMessageWriter::WriteMessage( | |
| 29 RemoteSecurityKeyMessageType message_type, | |
| 30 const std::string& message_payload) { | |
| 31 if (write_failed_) { | |
| 32 return false; | |
| 33 } | |
| 34 | |
| 35 if (!output_stream_.IsValid()) { | |
|
Sergey Ulanov
2016/03/28 18:20:07
Why would it be invalid here? I think you can just
joedow
2016/03/29 22:14:14
The file representing output_stream could be valid
| |
| 36 LOG(ERROR) << "Cannot write to invalid stream."; | |
| 37 write_failed_ = true; | |
| 38 return false; | |
| 39 } | |
| 40 | |
| 41 uint32_t message_payload_size_bytes = message_payload.size(); | |
| 42 uint32_t total_message_size_bytes = | |
| 43 sizeof(message_type) + message_payload_size_bytes; | |
|
Sergey Ulanov
2016/03/28 18:20:07
In remoting/host/security_key/security_key_message
joedow
2016/03/29 22:14:14
Done.
| |
| 44 CHECK(SecurityKeyMessage::IsValidMessageSize(total_message_size_bytes)); | |
| 45 | |
| 46 // First we send the message header which is the length of the message_type | |
| 47 // and message_payload in bytes. | |
| 48 int result = output_stream_.WriteAtCurrentPos( | |
| 49 reinterpret_cast<char*>(&total_message_size_bytes), | |
| 50 SecurityKeyMessage::kHeaderSizeBytes); | |
| 51 if (result != SecurityKeyMessage::kHeaderSizeBytes) { | |
| 52 LOG(ERROR) << "Failed to send message header: " << result; | |
|
Sergey Ulanov
2016/03/28 18:20:07
result will be -1 on error. Print error_details()
joedow
2016/03/29 22:14:14
Done.
| |
| 53 write_failed_ = true; | |
| 54 return false; | |
| 55 } | |
| 56 | |
| 57 // Next we send the message_type. | |
| 58 result = output_stream_.WriteAtCurrentPos( | |
|
Sergey Ulanov
2016/03/28 18:20:08
In this function WriteAtCurrentPos is called in 3
joedow
2016/03/29 22:14:14
Done.
| |
| 59 reinterpret_cast<char*>(&message_type), sizeof(message_type)); | |
| 60 if (result != sizeof(message_type)) { | |
| 61 LOG(ERROR) << "Failed to send message type: " << result; | |
| 62 write_failed_ = true; | |
| 63 return false; | |
| 64 } | |
| 65 | |
| 66 // Lastly, send the message data if appropriate. | |
| 67 if (!message_payload.empty()) { | |
| 68 result = output_stream_.WriteAtCurrentPos(message_payload.data(), | |
| 69 message_payload_size_bytes); | |
| 70 // The static cast is safe as we know the value is smaller than max int. | |
| 71 if (result != static_cast<int>(message_payload_size_bytes)) { | |
|
Sergey Ulanov
2016/03/28 18:20:07
nit: You wouldn't need this static_cast<> if messa
joedow
2016/03/29 22:14:14
Done.
| |
| 72 LOG(ERROR) << "Failed to send message payload:" << result; | |
| 73 write_failed_ = true; | |
| 74 return false; | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 return true; | |
| 79 } | |
| 80 | |
| 81 } // namespace remoting | |
| OLD | NEW |