Chromium Code Reviews| 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_reader_impl.h" | 5 #include "remoting/host/security_key/remote_security_key_message_reader_impl.h" |
| 6 | 6 |
| 7 #include <cstdint> | 7 #include <cstdint> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 57 void RemoteSecurityKeyMessageReaderImpl::ReadMessage() { | 57 void RemoteSecurityKeyMessageReaderImpl::ReadMessage() { |
| 58 DCHECK(read_task_runner_->RunsTasksOnCurrentThread()); | 58 DCHECK(read_task_runner_->RunsTasksOnCurrentThread()); |
| 59 | 59 |
| 60 while (true) { | 60 while (true) { |
| 61 if (!read_stream_.IsValid()) { | 61 if (!read_stream_.IsValid()) { |
| 62 LOG(ERROR) << "Cannot read from invalid stream."; | 62 LOG(ERROR) << "Cannot read from invalid stream."; |
| 63 NotifyError(); | 63 NotifyError(); |
| 64 return; | 64 return; |
| 65 } | 65 } |
| 66 | 66 |
| 67 // Read the message header to retrieve the remaining message length. | 67 uint32_t message_length_bytes = 0; |
| 68 uint32_t total_message_size_bytes; | 68 if (!ReadFromStream(reinterpret_cast<char*>(&message_length_bytes), 4)) { |
| 69 int read_result = read_stream_.ReadAtCurrentPos( | |
| 70 reinterpret_cast<char*>(&total_message_size_bytes), | |
| 71 SecurityKeyMessage::kHeaderSizeBytes); | |
| 72 if (read_result != SecurityKeyMessage::kHeaderSizeBytes) { | |
| 73 // 0 means EOF which is normal and should not be logged as an error. | |
| 74 if (read_result != 0) { | |
| 75 LOG(ERROR) << "Failed to read message header, read returned " | |
| 76 << read_result; | |
| 77 } | |
| 78 NotifyError(); | 69 NotifyError(); |
| 79 return; | 70 return; |
| 80 } | 71 } |
| 81 | 72 |
| 82 if (!SecurityKeyMessage::IsValidMessageSize(total_message_size_bytes)) { | 73 if (!SecurityKeyMessage::IsValidMessageSize(message_length_bytes)) { |
| 83 LOG(ERROR) << "Message size too large: " << total_message_size_bytes; | 74 LOG(ERROR) << "Message size is invalid: " << message_length_bytes; |
| 84 NotifyError(); | 75 NotifyError(); |
| 85 return; | 76 return; |
| 86 } | 77 } |
| 87 | 78 |
| 88 std::string message_data(total_message_size_bytes, '\0'); | 79 std::string message_data(message_length_bytes, '\0'); |
| 89 read_result = read_stream_.ReadAtCurrentPos(string_as_array(&message_data), | 80 if (!ReadFromStream(string_as_array(&message_data), message_data.size())) { |
| 90 total_message_size_bytes); | |
| 91 // The static cast is safe as we know the value is smaller than max int. | |
| 92 if (read_result != static_cast<int>(total_message_size_bytes)) { | |
| 93 LOG(ERROR) << "Failed to read message: " << read_result; | |
| 94 NotifyError(); | 81 NotifyError(); |
| 95 return; | 82 return; |
| 96 } | 83 } |
| 97 | 84 |
| 98 std::unique_ptr<SecurityKeyMessage> message(new SecurityKeyMessage()); | 85 std::unique_ptr<SecurityKeyMessage> message(new SecurityKeyMessage()); |
| 99 if (!message->ParseMessage(message_data)) { | 86 if (!message->ParseMessage(message_data)) { |
| 100 LOG(ERROR) << "Invalid message data received."; | 87 LOG(ERROR) << "Invalid message data received."; |
| 101 NotifyError(); | 88 NotifyError(); |
| 102 return; | 89 return; |
| 103 } | 90 } |
| 104 | 91 |
| 105 // Notify callback of the new message received. | 92 // Notify callback of the new message received. |
| 106 main_task_runner_->PostTask( | 93 main_task_runner_->PostTask( |
| 107 FROM_HERE, | 94 FROM_HERE, |
| 108 base::Bind(&RemoteSecurityKeyMessageReaderImpl::InvokeMessageCallback, | 95 base::Bind(&RemoteSecurityKeyMessageReaderImpl::InvokeMessageCallback, |
| 109 weak_factory_.GetWeakPtr(), base::Passed(&message))); | 96 weak_factory_.GetWeakPtr(), base::Passed(&message))); |
| 110 } | 97 } |
| 111 } | 98 } |
| 112 | 99 |
| 100 bool RemoteSecurityKeyMessageReaderImpl::ReadFromStream( | |
| 101 char* buffer, | |
| 102 uint32_t bytes_to_read) { | |
|
Sergey Ulanov
2016/04/19 17:36:39
size_t
joedow
2016/04/19 21:07:56
Done.
| |
| 103 DCHECK(buffer); | |
| 104 DCHECK_GT(bytes_to_read, 0u); | |
| 105 | |
| 106 uint32_t bytes_read = 0; | |
|
Sergey Ulanov
2016/04/19 17:36:40
size_t
joedow
2016/04/19 21:07:56
Done.
| |
| 107 do { | |
| 108 int read_result = read_stream_.ReadAtCurrentPos(buffer + bytes_read, | |
|
Sergey Ulanov
2016/04/19 17:36:40
Use ReadAtCurrentPosNoBestEffort() here?
joedow
2016/04/19 21:07:56
Done.
| |
| 109 bytes_to_read - bytes_read); | |
| 110 if (read_result < 1) { | |
| 111 // 0 means EOF which is normal and should not be logged as an error. | |
| 112 if (read_result != 0) { | |
| 113 LOG(ERROR) << "Failed to read from stream, ReadAtCurrentPos returned " | |
| 114 << read_result; | |
| 115 } | |
| 116 return false; | |
| 117 } | |
| 118 bytes_read += read_result; | |
| 119 } while (bytes_read < bytes_to_read); | |
| 120 DCHECK_EQ(bytes_read, bytes_to_read); | |
| 121 | |
| 122 return true; | |
| 123 } | |
| 124 | |
| 113 void RemoteSecurityKeyMessageReaderImpl::NotifyError() { | 125 void RemoteSecurityKeyMessageReaderImpl::NotifyError() { |
| 114 DCHECK(read_task_runner_->RunsTasksOnCurrentThread()); | 126 DCHECK(read_task_runner_->RunsTasksOnCurrentThread()); |
| 115 | 127 |
| 116 main_task_runner_->PostTask( | 128 main_task_runner_->PostTask( |
| 117 FROM_HERE, | 129 FROM_HERE, |
| 118 base::Bind(&RemoteSecurityKeyMessageReaderImpl::InvokeErrorCallback, | 130 base::Bind(&RemoteSecurityKeyMessageReaderImpl::InvokeErrorCallback, |
| 119 weak_factory_.GetWeakPtr())); | 131 weak_factory_.GetWeakPtr())); |
| 120 } | 132 } |
| 121 | 133 |
| 122 void RemoteSecurityKeyMessageReaderImpl::InvokeMessageCallback( | 134 void RemoteSecurityKeyMessageReaderImpl::InvokeMessageCallback( |
| 123 std::unique_ptr<SecurityKeyMessage> message) { | 135 std::unique_ptr<SecurityKeyMessage> message) { |
| 124 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | 136 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); |
| 125 message_callback_.Run(std::move(message)); | 137 message_callback_.Run(std::move(message)); |
| 126 } | 138 } |
| 127 | 139 |
| 128 void RemoteSecurityKeyMessageReaderImpl::InvokeErrorCallback() { | 140 void RemoteSecurityKeyMessageReaderImpl::InvokeErrorCallback() { |
| 129 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | 141 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); |
| 130 error_callback_.Run(); | 142 error_callback_.Run(); |
| 131 } | 143 } |
| 132 | 144 |
| 133 } // namespace remoting | 145 } // namespace remoting |
| OLD | NEW |