OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/host/security_key/remote_security_key_message_writer_impl.h" | 5 #include "remoting/host/security_key/security_key_message_writer_impl.h" |
6 | 6 |
7 #include <cstdint> | 7 #include <cstdint> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "remoting/host/security_key/security_key_message.h" | 11 #include "remoting/host/security_key/security_key_message.h" |
12 | 12 |
13 namespace remoting { | 13 namespace remoting { |
14 | 14 |
15 RemoteSecurityKeyMessageWriterImpl::RemoteSecurityKeyMessageWriterImpl( | 15 SecurityKeyMessageWriterImpl::SecurityKeyMessageWriterImpl( |
16 base::File output_file) | 16 base::File output_file) |
17 : output_stream_(std::move(output_file)) {} | 17 : output_stream_(std::move(output_file)) {} |
18 | 18 |
19 RemoteSecurityKeyMessageWriterImpl::~RemoteSecurityKeyMessageWriterImpl() {} | 19 SecurityKeyMessageWriterImpl::~SecurityKeyMessageWriterImpl() {} |
20 | 20 |
21 bool RemoteSecurityKeyMessageWriterImpl::WriteMessage( | 21 bool SecurityKeyMessageWriterImpl::WriteMessage( |
22 RemoteSecurityKeyMessageType message_type) { | 22 SecurityKeyMessageType message_type) { |
23 return WriteMessageWithPayload(message_type, std::string()); | 23 return WriteMessageWithPayload(message_type, std::string()); |
24 } | 24 } |
25 | 25 |
26 bool RemoteSecurityKeyMessageWriterImpl::WriteMessageWithPayload( | 26 bool SecurityKeyMessageWriterImpl::WriteMessageWithPayload( |
27 RemoteSecurityKeyMessageType message_type, | 27 SecurityKeyMessageType message_type, |
28 const std::string& message_payload) { | 28 const std::string& message_payload) { |
29 if (write_failed_ || !output_stream_.IsValid()) { | 29 if (write_failed_ || !output_stream_.IsValid()) { |
30 return false; | 30 return false; |
31 } | 31 } |
32 | 32 |
33 int message_payload_size_bytes = message_payload.size(); | 33 int message_payload_size_bytes = message_payload.size(); |
34 uint32_t total_message_size_bytes = | 34 uint32_t total_message_size_bytes = |
35 SecurityKeyMessage::kMessageTypeSizeBytes + message_payload_size_bytes; | 35 SecurityKeyMessage::kMessageTypeSizeBytes + message_payload_size_bytes; |
36 CHECK(SecurityKeyMessage::IsValidMessageSize(total_message_size_bytes)); | 36 CHECK(SecurityKeyMessage::IsValidMessageSize(total_message_size_bytes)); |
37 | 37 |
(...skipping 17 matching lines...) Expand all Loading... |
55 if (!WriteBytesToOutput(message_payload.data(), | 55 if (!WriteBytesToOutput(message_payload.data(), |
56 message_payload_size_bytes)) { | 56 message_payload_size_bytes)) { |
57 LOG(ERROR) << "Failed to send message payload."; | 57 LOG(ERROR) << "Failed to send message payload."; |
58 return false; | 58 return false; |
59 } | 59 } |
60 } | 60 } |
61 | 61 |
62 return true; | 62 return true; |
63 } | 63 } |
64 | 64 |
65 bool RemoteSecurityKeyMessageWriterImpl::WriteBytesToOutput( | 65 bool SecurityKeyMessageWriterImpl::WriteBytesToOutput(const char* message, |
66 const char* message, | 66 int bytes_to_write) { |
67 int bytes_to_write) { | |
68 DCHECK(message); | 67 DCHECK(message); |
69 DCHECK_GT(bytes_to_write, 0); | 68 DCHECK_GT(bytes_to_write, 0); |
70 | 69 |
71 int result = output_stream_.WriteAtCurrentPos(message, bytes_to_write); | 70 int result = output_stream_.WriteAtCurrentPos(message, bytes_to_write); |
72 if (result != bytes_to_write) { | 71 if (result != bytes_to_write) { |
73 LOG(ERROR) << "Failed to write all bytes to output stream. bytes written: " | 72 LOG(ERROR) << "Failed to write all bytes to output stream. bytes written: " |
74 << result << ", file error: " | 73 << result << ", file error: " |
75 << base::File::ErrorToString(output_stream_.error_details()); | 74 << base::File::ErrorToString(output_stream_.error_details()); |
76 write_failed_ = true; | 75 write_failed_ = true; |
77 return false; | 76 return false; |
78 } | 77 } |
79 | 78 |
80 return true; | 79 return true; |
81 } | 80 } |
82 | 81 |
83 } // namespace remoting | 82 } // namespace remoting |
OLD | NEW |