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_impl.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 RemoteSecurityKeyMessageReaderImpl::RemoteSecurityKeyMessageReaderImpl( |
| 22 base::File input_file) |
| 23 : read_stream_(std::move(input_file)), |
| 24 reader_thread_("RemoteSecurityKeyMessageReaderImpl"), |
| 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 RemoteSecurityKeyMessageReaderImpl::~RemoteSecurityKeyMessageReaderImpl() { |
| 35 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); |
| 36 |
| 37 // In order to ensure the reader thread is stopped cleanly, we want to stop |
| 38 // the thread before the task runners and weak pointers are invalidated. |
| 39 reader_thread_.Stop(); |
| 40 } |
| 41 |
| 42 void RemoteSecurityKeyMessageReaderImpl::Start( |
| 43 const SecurityKeyMessageCallback& message_callback, |
| 44 const base::Closure& error_callback) { |
| 45 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); |
| 46 |
| 47 message_callback_ = message_callback; |
| 48 error_callback_ = error_callback; |
| 49 |
| 50 // base::Unretained is safe since this class owns the thread running this task |
| 51 // which will be destroyed before this instance is. |
| 52 read_task_runner_->PostTask( |
| 53 FROM_HERE, base::Bind(&RemoteSecurityKeyMessageReaderImpl::ReadMessage, |
| 54 base::Unretained(this))); |
| 55 } |
| 56 |
| 57 void RemoteSecurityKeyMessageReaderImpl::ReadMessage() { |
| 58 DCHECK(read_task_runner_->RunsTasksOnCurrentThread()); |
| 59 |
| 60 while (true) { |
| 61 if (!read_stream_.IsValid()) { |
| 62 LOG(ERROR) << "Cannot read from invalid stream."; |
| 63 NotifyError(); |
| 64 return; |
| 65 } |
| 66 |
| 67 uint32_t message_length_bytes = 0; |
| 68 if (!ReadFromStream(reinterpret_cast<char*>(&message_length_bytes), 4)) { |
| 69 NotifyError(); |
| 70 return; |
| 71 } |
| 72 |
| 73 if (!SecurityKeyMessage::IsValidMessageSize(message_length_bytes)) { |
| 74 LOG(ERROR) << "Message size is invalid: " << message_length_bytes; |
| 75 NotifyError(); |
| 76 return; |
| 77 } |
| 78 |
| 79 std::string message_data(message_length_bytes, '\0'); |
| 80 if (!ReadFromStream(string_as_array(&message_data), message_data.size())) { |
| 81 NotifyError(); |
| 82 return; |
| 83 } |
| 84 |
| 85 std::unique_ptr<SecurityKeyMessage> message(new SecurityKeyMessage()); |
| 86 if (!message->ParseMessage(message_data)) { |
| 87 LOG(ERROR) << "Invalid message data received."; |
| 88 NotifyError(); |
| 89 return; |
| 90 } |
| 91 |
| 92 // Notify callback of the new message received. |
| 93 main_task_runner_->PostTask( |
| 94 FROM_HERE, |
| 95 base::Bind(&RemoteSecurityKeyMessageReaderImpl::InvokeMessageCallback, |
| 96 weak_factory_.GetWeakPtr(), base::Passed(&message))); |
| 97 } |
| 98 } |
| 99 |
| 100 bool RemoteSecurityKeyMessageReaderImpl::ReadFromStream(char* buffer, |
| 101 size_t bytes_to_read) { |
| 102 DCHECK(buffer); |
| 103 DCHECK_GT(bytes_to_read, 0u); |
| 104 |
| 105 size_t bytes_read = 0; |
| 106 do { |
| 107 int read_result = read_stream_.ReadAtCurrentPosNoBestEffort( |
| 108 buffer + bytes_read, bytes_to_read - bytes_read); |
| 109 if (read_result < 1) { |
| 110 // 0 means EOF which is normal and should not be logged as an error. |
| 111 if (read_result != 0) { |
| 112 LOG(ERROR) << "Failed to read from stream, ReadAtCurrentPos returned " |
| 113 << read_result; |
| 114 } |
| 115 return false; |
| 116 } |
| 117 bytes_read += read_result; |
| 118 } while (bytes_read < bytes_to_read); |
| 119 DCHECK_EQ(bytes_read, bytes_to_read); |
| 120 |
| 121 return true; |
| 122 } |
| 123 |
| 124 void RemoteSecurityKeyMessageReaderImpl::NotifyError() { |
| 125 DCHECK(read_task_runner_->RunsTasksOnCurrentThread()); |
| 126 |
| 127 main_task_runner_->PostTask( |
| 128 FROM_HERE, |
| 129 base::Bind(&RemoteSecurityKeyMessageReaderImpl::InvokeErrorCallback, |
| 130 weak_factory_.GetWeakPtr())); |
| 131 } |
| 132 |
| 133 void RemoteSecurityKeyMessageReaderImpl::InvokeMessageCallback( |
| 134 std::unique_ptr<SecurityKeyMessage> message) { |
| 135 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); |
| 136 message_callback_.Run(std::move(message)); |
| 137 } |
| 138 |
| 139 void RemoteSecurityKeyMessageReaderImpl::InvokeErrorCallback() { |
| 140 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); |
| 141 error_callback_.Run(); |
| 142 } |
| 143 |
| 144 } // namespace remoting |
OLD | NEW |