| 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_reader.h" | |
| 6 | |
| 7 #include <cstdint> | |
| 8 #include <string> | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/files/file.h" | |
| 13 #include "base/macros.h" | |
| 14 #include "base/single_thread_task_runner.h" | |
| 15 #include "base/stl_util.h" | |
| 16 #include "base/threading/thread_task_runner_handle.h" | |
| 17 #include "remoting/host/security_key/security_key_message.h" | |
| 18 | |
| 19 namespace remoting { | |
| 20 | |
| 21 RemoteSecurityKeyMessageReader::RemoteSecurityKeyMessageReader( | |
| 22 base::File input_file) | |
| 23 : read_stream_(std::move(input_file)), | |
| 24 reader_thread_("RemoteSecurityKeyMessageReader"), | |
| 25 weak_factory_(this) { | |
| 26 base::Thread::Options options; | |
| 27 options.message_loop_type = base::MessageLoop::TYPE_IO; | |
| 28 reader_thread_.StartWithOptions(options); | |
| 29 | |
| 30 read_task_runner_ = reader_thread_.task_runner(); | |
| 31 main_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | |
| 32 } | |
| 33 | |
| 34 RemoteSecurityKeyMessageReader::~RemoteSecurityKeyMessageReader() { | |
| 35 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | |
| 36 | |
| 37 // In order to ensure the reader thread is stopped cleanly, we close the | |
| 38 // stream it is blocking on and then wait for the thread to exit. | |
| 39 read_stream_.Close(); | |
| 40 reader_thread_.Stop(); | |
| 41 } | |
| 42 | |
| 43 void RemoteSecurityKeyMessageReader::Start( | |
| 44 SecurityKeyMessageCallback message_callback, | |
| 45 base::Closure error_callback) { | |
| 46 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | |
| 47 | |
| 48 message_callback_ = message_callback; | |
| 49 error_callback_ = error_callback; | |
| 50 | |
| 51 // base::Unretained is safe since this class owns the thread running this task | |
| 52 // which will be destroyed before this instance is. | |
| 53 read_task_runner_->PostTask( | |
| 54 FROM_HERE, base::Bind(&RemoteSecurityKeyMessageReader::ReadMessage, | |
| 55 base::Unretained(this))); | |
| 56 } | |
| 57 | |
| 58 void RemoteSecurityKeyMessageReader::ReadMessage() { | |
| 59 DCHECK(read_task_runner_->RunsTasksOnCurrentThread()); | |
| 60 | |
| 61 while (true) { | |
| 62 if (!read_stream_.IsValid()) { | |
| 63 LOG(ERROR) << "Cannot read from invalid stream."; | |
| 64 NotifyError(); | |
| 65 return; | |
| 66 } | |
| 67 | |
| 68 // Read the message header to retrieve the remaining message length. | |
| 69 uint32_t total_message_size_bytes; | |
| 70 int read_result = read_stream_.ReadAtCurrentPos( | |
| 71 reinterpret_cast<char*>(&total_message_size_bytes), | |
| 72 SecurityKeyMessage::kHeaderSizeBytes); | |
| 73 if (read_result != SecurityKeyMessage::kHeaderSizeBytes) { | |
| 74 // 0 means EOF which is normal and should not be logged as an error. | |
| 75 if (read_result != 0) { | |
| 76 LOG(ERROR) << "Failed to read message header, read returned " | |
| 77 << read_result; | |
| 78 } | |
| 79 NotifyError(); | |
| 80 return; | |
| 81 } | |
| 82 | |
| 83 if (!SecurityKeyMessage::IsValidMessageSize(total_message_size_bytes)) { | |
| 84 LOG(ERROR) << "Message size too large: " << total_message_size_bytes; | |
| 85 NotifyError(); | |
| 86 return; | |
| 87 } | |
| 88 | |
| 89 std::string message_data(total_message_size_bytes, '\0'); | |
| 90 read_result = read_stream_.ReadAtCurrentPos(string_as_array(&message_data), | |
| 91 total_message_size_bytes); | |
| 92 // The static cast is safe as we know the value is smaller than max int. | |
| 93 if (read_result != static_cast<int>(total_message_size_bytes)) { | |
| 94 LOG(ERROR) << "Failed to read message: " << read_result; | |
| 95 NotifyError(); | |
| 96 return; | |
| 97 } | |
| 98 | |
| 99 std::unique_ptr<SecurityKeyMessage> message(new SecurityKeyMessage()); | |
| 100 if (!message->ParseMessage(message_data)) { | |
| 101 LOG(ERROR) << "Invalid message data received."; | |
| 102 NotifyError(); | |
| 103 return; | |
| 104 } | |
| 105 | |
| 106 // Notify callback of the new message received. | |
| 107 main_task_runner_->PostTask( | |
| 108 FROM_HERE, | |
| 109 base::Bind(&RemoteSecurityKeyMessageReader::InvokeMessageCallback, | |
| 110 weak_factory_.GetWeakPtr(), base::Passed(&message))); | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 void RemoteSecurityKeyMessageReader::NotifyError() { | |
| 115 DCHECK(read_task_runner_->RunsTasksOnCurrentThread()); | |
| 116 | |
| 117 main_task_runner_->PostTask( | |
| 118 FROM_HERE, | |
| 119 base::Bind(&RemoteSecurityKeyMessageReader::InvokeErrorCallback, | |
| 120 weak_factory_.GetWeakPtr())); | |
| 121 } | |
| 122 | |
| 123 void RemoteSecurityKeyMessageReader::InvokeMessageCallback( | |
| 124 std::unique_ptr<SecurityKeyMessage> message) { | |
| 125 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | |
| 126 message_callback_.Run(std::move(message)); | |
| 127 } | |
| 128 | |
| 129 void RemoteSecurityKeyMessageReader::InvokeErrorCallback() { | |
| 130 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | |
| 131 error_callback_.Run(); | |
| 132 } | |
| 133 | |
| 134 } // namespace remoting | |
| OLD | NEW |