| 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/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 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 weak_factory_(this) { | 26 weak_factory_(this) { |
| 27 base::Thread::Options options; | 27 base::Thread::Options options; |
| 28 options.message_loop_type = base::MessageLoop::TYPE_IO; | 28 options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 29 reader_thread_.StartWithOptions(options); | 29 reader_thread_.StartWithOptions(options); |
| 30 | 30 |
| 31 read_task_runner_ = reader_thread_.task_runner(); | 31 read_task_runner_ = reader_thread_.task_runner(); |
| 32 main_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 32 main_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 33 } | 33 } |
| 34 | 34 |
| 35 SecurityKeyMessageReaderImpl::~SecurityKeyMessageReaderImpl() { | 35 SecurityKeyMessageReaderImpl::~SecurityKeyMessageReaderImpl() { |
| 36 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | 36 DCHECK(main_task_runner_->RunsTasksInCurrentSequence()); |
| 37 | 37 |
| 38 // In order to ensure the reader thread is stopped cleanly, we want to stop | 38 // In order to ensure the reader thread is stopped cleanly, we want to stop |
| 39 // the thread before the task runners and weak pointers are invalidated. | 39 // the thread before the task runners and weak pointers are invalidated. |
| 40 reader_thread_.Stop(); | 40 reader_thread_.Stop(); |
| 41 } | 41 } |
| 42 | 42 |
| 43 void SecurityKeyMessageReaderImpl::Start( | 43 void SecurityKeyMessageReaderImpl::Start( |
| 44 const SecurityKeyMessageCallback& message_callback, | 44 const SecurityKeyMessageCallback& message_callback, |
| 45 const base::Closure& error_callback) { | 45 const base::Closure& error_callback) { |
| 46 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | 46 DCHECK(main_task_runner_->RunsTasksInCurrentSequence()); |
| 47 | 47 |
| 48 message_callback_ = message_callback; | 48 message_callback_ = message_callback; |
| 49 error_callback_ = error_callback; | 49 error_callback_ = error_callback; |
| 50 | 50 |
| 51 // base::Unretained is safe since this class owns the thread running this task | 51 // base::Unretained is safe since this class owns the thread running this task |
| 52 // which will be destroyed before this instance is. | 52 // which will be destroyed before this instance is. |
| 53 read_task_runner_->PostTask( | 53 read_task_runner_->PostTask( |
| 54 FROM_HERE, base::Bind(&SecurityKeyMessageReaderImpl::ReadMessage, | 54 FROM_HERE, base::Bind(&SecurityKeyMessageReaderImpl::ReadMessage, |
| 55 base::Unretained(this))); | 55 base::Unretained(this))); |
| 56 } | 56 } |
| 57 | 57 |
| 58 void SecurityKeyMessageReaderImpl::ReadMessage() { | 58 void SecurityKeyMessageReaderImpl::ReadMessage() { |
| 59 DCHECK(read_task_runner_->RunsTasksOnCurrentThread()); | 59 DCHECK(read_task_runner_->RunsTasksInCurrentSequence()); |
| 60 | 60 |
| 61 while (true) { | 61 while (true) { |
| 62 if (!read_stream_.IsValid()) { | 62 if (!read_stream_.IsValid()) { |
| 63 LOG(ERROR) << "Cannot read from invalid stream."; | 63 LOG(ERROR) << "Cannot read from invalid stream."; |
| 64 NotifyError(); | 64 NotifyError(); |
| 65 return; | 65 return; |
| 66 } | 66 } |
| 67 | 67 |
| 68 uint32_t message_length_bytes = 0; | 68 uint32_t message_length_bytes = 0; |
| 69 if (!ReadFromStream(reinterpret_cast<char*>(&message_length_bytes), 4)) { | 69 if (!ReadFromStream(reinterpret_cast<char*>(&message_length_bytes), 4)) { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 return false; | 117 return false; |
| 118 } | 118 } |
| 119 bytes_read += read_result; | 119 bytes_read += read_result; |
| 120 } while (bytes_read < bytes_to_read); | 120 } while (bytes_read < bytes_to_read); |
| 121 DCHECK_EQ(bytes_read, bytes_to_read); | 121 DCHECK_EQ(bytes_read, bytes_to_read); |
| 122 | 122 |
| 123 return true; | 123 return true; |
| 124 } | 124 } |
| 125 | 125 |
| 126 void SecurityKeyMessageReaderImpl::NotifyError() { | 126 void SecurityKeyMessageReaderImpl::NotifyError() { |
| 127 DCHECK(read_task_runner_->RunsTasksOnCurrentThread()); | 127 DCHECK(read_task_runner_->RunsTasksInCurrentSequence()); |
| 128 | 128 |
| 129 main_task_runner_->PostTask( | 129 main_task_runner_->PostTask( |
| 130 FROM_HERE, base::Bind(&SecurityKeyMessageReaderImpl::InvokeErrorCallback, | 130 FROM_HERE, base::Bind(&SecurityKeyMessageReaderImpl::InvokeErrorCallback, |
| 131 weak_factory_.GetWeakPtr())); | 131 weak_factory_.GetWeakPtr())); |
| 132 } | 132 } |
| 133 | 133 |
| 134 void SecurityKeyMessageReaderImpl::InvokeMessageCallback( | 134 void SecurityKeyMessageReaderImpl::InvokeMessageCallback( |
| 135 std::unique_ptr<SecurityKeyMessage> message) { | 135 std::unique_ptr<SecurityKeyMessage> message) { |
| 136 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | 136 DCHECK(main_task_runner_->RunsTasksInCurrentSequence()); |
| 137 message_callback_.Run(std::move(message)); | 137 message_callback_.Run(std::move(message)); |
| 138 } | 138 } |
| 139 | 139 |
| 140 void SecurityKeyMessageReaderImpl::InvokeErrorCallback() { | 140 void SecurityKeyMessageReaderImpl::InvokeErrorCallback() { |
| 141 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | 141 DCHECK(main_task_runner_->RunsTasksInCurrentSequence()); |
| 142 error_callback_.Run(); | 142 error_callback_.Run(); |
| 143 } | 143 } |
| 144 | 144 |
| 145 } // namespace remoting | 145 } // namespace remoting |
| OLD | NEW |