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_impl.h" | |
6 | |
7 #include <cstdint> | |
8 #include <string> | |
9 #include <utility> | |
10 | |
11 #include "remoting/host/security_key/security_key_message.h" | |
12 | |
13 namespace remoting { | |
14 | |
15 RemoteSecurityKeyMessageWriterImpl::RemoteSecurityKeyMessageWriterImpl( | |
16 base::File output_file) | |
17 : output_stream_(std::move(output_file)) {} | |
18 | |
19 RemoteSecurityKeyMessageWriterImpl::~RemoteSecurityKeyMessageWriterImpl() {} | |
20 | |
21 bool RemoteSecurityKeyMessageWriterImpl::WriteMessage( | |
22 RemoteSecurityKeyMessageType message_type) { | |
23 return WriteMessageWithPayload(message_type, std::string()); | |
24 } | |
25 | |
26 bool RemoteSecurityKeyMessageWriterImpl::WriteMessageWithPayload( | |
27 RemoteSecurityKeyMessageType message_type, | |
28 const std::string& message_payload) { | |
29 if (write_failed_ || !output_stream_.IsValid()) { | |
30 return false; | |
31 } | |
32 | |
33 int message_payload_size_bytes = message_payload.size(); | |
34 uint32_t total_message_size_bytes = | |
35 SecurityKeyMessage::kMessageTypeSizeBytes + message_payload_size_bytes; | |
36 CHECK(SecurityKeyMessage::IsValidMessageSize(total_message_size_bytes)); | |
37 | |
38 // First we send the message header which is the length of the message_type | |
39 // and message_payload in bytes. | |
40 if (!WriteBytesToOutput(reinterpret_cast<char*>(&total_message_size_bytes), | |
41 SecurityKeyMessage::kHeaderSizeBytes)) { | |
42 LOG(ERROR) << "Failed to send message header."; | |
43 return false; | |
44 } | |
45 | |
46 // Next we send the message_type. | |
47 if (!WriteBytesToOutput(reinterpret_cast<char*>(&message_type), | |
48 SecurityKeyMessage::kMessageTypeSizeBytes)) { | |
49 LOG(ERROR) << "Failed to send message type."; | |
50 return false; | |
51 } | |
52 | |
53 // Lastly, send the message data if appropriate. | |
54 if (!message_payload.empty()) { | |
55 if (!WriteBytesToOutput(message_payload.data(), | |
56 message_payload_size_bytes)) { | |
57 LOG(ERROR) << "Failed to send message payload."; | |
58 return false; | |
59 } | |
60 } | |
61 | |
62 return true; | |
63 } | |
64 | |
65 bool RemoteSecurityKeyMessageWriterImpl::WriteBytesToOutput( | |
66 const char* message, | |
67 int bytes_to_write) { | |
68 DCHECK(message); | |
69 DCHECK_GT(bytes_to_write, 0); | |
70 | |
71 int result = output_stream_.WriteAtCurrentPos(message, bytes_to_write); | |
72 if (result != bytes_to_write) { | |
73 LOG(ERROR) << "Failed to write all bytes to output stream. bytes written: " | |
74 << result << ", file error: " | |
75 << base::File::ErrorToString(output_stream_.error_details()); | |
76 write_failed_ = true; | |
77 return false; | |
78 } | |
79 | |
80 return true; | |
81 } | |
82 | |
83 } // namespace remoting | |
OLD | NEW |