| 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/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 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/files/file.h" | 12 #include "base/files/file.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
| 15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
| 17 #include "remoting/host/security_key/security_key_message.h" | 17 #include "remoting/host/security_key/security_key_message.h" |
| 18 | 18 |
| 19 namespace remoting { | 19 namespace remoting { |
| 20 | 20 |
| 21 RemoteSecurityKeyMessageReaderImpl::RemoteSecurityKeyMessageReaderImpl( | 21 SecurityKeyMessageReaderImpl::SecurityKeyMessageReaderImpl( |
| 22 base::File input_file) | 22 base::File input_file) |
| 23 : read_stream_(std::move(input_file)), | 23 : read_stream_(std::move(input_file)), |
| 24 reader_thread_("RemoteSecurityKeyMessageReaderImpl"), | 24 reader_thread_("SecurityKeyMessageReaderImpl"), |
| 25 weak_factory_(this) { | 25 weak_factory_(this) { |
| 26 base::Thread::Options options; | 26 base::Thread::Options options; |
| 27 options.message_loop_type = base::MessageLoop::TYPE_IO; | 27 options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 28 reader_thread_.StartWithOptions(options); | 28 reader_thread_.StartWithOptions(options); |
| 29 | 29 |
| 30 read_task_runner_ = reader_thread_.task_runner(); | 30 read_task_runner_ = reader_thread_.task_runner(); |
| 31 main_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 31 main_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 32 } | 32 } |
| 33 | 33 |
| 34 RemoteSecurityKeyMessageReaderImpl::~RemoteSecurityKeyMessageReaderImpl() { | 34 SecurityKeyMessageReaderImpl::~SecurityKeyMessageReaderImpl() { |
| 35 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | 35 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); |
| 36 | 36 |
| 37 // In order to ensure the reader thread is stopped cleanly, we want to stop | 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. | 38 // the thread before the task runners and weak pointers are invalidated. |
| 39 reader_thread_.Stop(); | 39 reader_thread_.Stop(); |
| 40 } | 40 } |
| 41 | 41 |
| 42 void RemoteSecurityKeyMessageReaderImpl::Start( | 42 void SecurityKeyMessageReaderImpl::Start( |
| 43 const SecurityKeyMessageCallback& message_callback, | 43 const SecurityKeyMessageCallback& message_callback, |
| 44 const base::Closure& error_callback) { | 44 const base::Closure& error_callback) { |
| 45 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | 45 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); |
| 46 | 46 |
| 47 message_callback_ = message_callback; | 47 message_callback_ = message_callback; |
| 48 error_callback_ = error_callback; | 48 error_callback_ = error_callback; |
| 49 | 49 |
| 50 // base::Unretained is safe since this class owns the thread running this task | 50 // base::Unretained is safe since this class owns the thread running this task |
| 51 // which will be destroyed before this instance is. | 51 // which will be destroyed before this instance is. |
| 52 read_task_runner_->PostTask( | 52 read_task_runner_->PostTask( |
| 53 FROM_HERE, base::Bind(&RemoteSecurityKeyMessageReaderImpl::ReadMessage, | 53 FROM_HERE, base::Bind(&SecurityKeyMessageReaderImpl::ReadMessage, |
| 54 base::Unretained(this))); | 54 base::Unretained(this))); |
| 55 } | 55 } |
| 56 | 56 |
| 57 void RemoteSecurityKeyMessageReaderImpl::ReadMessage() { | 57 void SecurityKeyMessageReaderImpl::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 uint32_t message_length_bytes = 0; | 67 uint32_t message_length_bytes = 0; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 85 std::unique_ptr<SecurityKeyMessage> message(new SecurityKeyMessage()); | 85 std::unique_ptr<SecurityKeyMessage> message(new SecurityKeyMessage()); |
| 86 if (!message->ParseMessage(message_data)) { | 86 if (!message->ParseMessage(message_data)) { |
| 87 LOG(ERROR) << "Invalid message data received."; | 87 LOG(ERROR) << "Invalid message data received."; |
| 88 NotifyError(); | 88 NotifyError(); |
| 89 return; | 89 return; |
| 90 } | 90 } |
| 91 | 91 |
| 92 // Notify callback of the new message received. | 92 // Notify callback of the new message received. |
| 93 main_task_runner_->PostTask( | 93 main_task_runner_->PostTask( |
| 94 FROM_HERE, | 94 FROM_HERE, |
| 95 base::Bind(&RemoteSecurityKeyMessageReaderImpl::InvokeMessageCallback, | 95 base::Bind(&SecurityKeyMessageReaderImpl::InvokeMessageCallback, |
| 96 weak_factory_.GetWeakPtr(), base::Passed(&message))); | 96 weak_factory_.GetWeakPtr(), base::Passed(&message))); |
| 97 } | 97 } |
| 98 } | 98 } |
| 99 | 99 |
| 100 bool RemoteSecurityKeyMessageReaderImpl::ReadFromStream(char* buffer, | 100 bool SecurityKeyMessageReaderImpl::ReadFromStream(char* buffer, |
| 101 size_t bytes_to_read) { | 101 size_t bytes_to_read) { |
| 102 DCHECK(buffer); | 102 DCHECK(buffer); |
| 103 DCHECK_GT(bytes_to_read, 0u); | 103 DCHECK_GT(bytes_to_read, 0u); |
| 104 | 104 |
| 105 size_t bytes_read = 0; | 105 size_t bytes_read = 0; |
| 106 do { | 106 do { |
| 107 int read_result = read_stream_.ReadAtCurrentPosNoBestEffort( | 107 int read_result = read_stream_.ReadAtCurrentPosNoBestEffort( |
| 108 buffer + bytes_read, bytes_to_read - bytes_read); | 108 buffer + bytes_read, bytes_to_read - bytes_read); |
| 109 if (read_result < 1) { | 109 if (read_result < 1) { |
| 110 // 0 means EOF which is normal and should not be logged as an error. | 110 // 0 means EOF which is normal and should not be logged as an error. |
| 111 if (read_result != 0) { | 111 if (read_result != 0) { |
| 112 LOG(ERROR) << "Failed to read from stream, ReadAtCurrentPos returned " | 112 LOG(ERROR) << "Failed to read from stream, ReadAtCurrentPos returned " |
| 113 << read_result; | 113 << read_result; |
| 114 } | 114 } |
| 115 return false; | 115 return false; |
| 116 } | 116 } |
| 117 bytes_read += read_result; | 117 bytes_read += read_result; |
| 118 } while (bytes_read < bytes_to_read); | 118 } while (bytes_read < bytes_to_read); |
| 119 DCHECK_EQ(bytes_read, bytes_to_read); | 119 DCHECK_EQ(bytes_read, bytes_to_read); |
| 120 | 120 |
| 121 return true; | 121 return true; |
| 122 } | 122 } |
| 123 | 123 |
| 124 void RemoteSecurityKeyMessageReaderImpl::NotifyError() { | 124 void SecurityKeyMessageReaderImpl::NotifyError() { |
| 125 DCHECK(read_task_runner_->RunsTasksOnCurrentThread()); | 125 DCHECK(read_task_runner_->RunsTasksOnCurrentThread()); |
| 126 | 126 |
| 127 main_task_runner_->PostTask( | 127 main_task_runner_->PostTask( |
| 128 FROM_HERE, | 128 FROM_HERE, base::Bind(&SecurityKeyMessageReaderImpl::InvokeErrorCallback, |
| 129 base::Bind(&RemoteSecurityKeyMessageReaderImpl::InvokeErrorCallback, | 129 weak_factory_.GetWeakPtr())); |
| 130 weak_factory_.GetWeakPtr())); | |
| 131 } | 130 } |
| 132 | 131 |
| 133 void RemoteSecurityKeyMessageReaderImpl::InvokeMessageCallback( | 132 void SecurityKeyMessageReaderImpl::InvokeMessageCallback( |
| 134 std::unique_ptr<SecurityKeyMessage> message) { | 133 std::unique_ptr<SecurityKeyMessage> message) { |
| 135 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | 134 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); |
| 136 message_callback_.Run(std::move(message)); | 135 message_callback_.Run(std::move(message)); |
| 137 } | 136 } |
| 138 | 137 |
| 139 void RemoteSecurityKeyMessageReaderImpl::InvokeErrorCallback() { | 138 void SecurityKeyMessageReaderImpl::InvokeErrorCallback() { |
| 140 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | 139 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); |
| 141 error_callback_.Run(); | 140 error_callback_.Run(); |
| 142 } | 141 } |
| 143 | 142 |
| 144 } // namespace remoting | 143 } // namespace remoting |
| OLD | NEW |