Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(406)

Side by Side Diff: remoting/host/security_key/remote_security_key_message_writer.cc

Issue 1830433002: Adding the message writing class used for remote_security_key STDOUT communication. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@messages
Patch Set: Integrating security message changes Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698