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.h" | 5 #include "remoting/host/security_key/security_key_message_reader.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 RemoteSecurityKeyMessageReader::RemoteSecurityKeyMessageReader( | 21 SecurityKeyMessageReader::SecurityKeyMessageReader(base::File input_file) |
22 base::File input_file) | |
23 : read_stream_(std::move(input_file)), | 22 : read_stream_(std::move(input_file)), |
24 reader_thread_("RemoteSecurityKeyMessageReader"), | 23 reader_thread_("SecurityKeyMessageReader"), |
25 weak_factory_(this) { | 24 weak_factory_(this) { |
26 base::Thread::Options options; | 25 base::Thread::Options options; |
27 options.message_loop_type = base::MessageLoop::TYPE_IO; | 26 options.message_loop_type = base::MessageLoop::TYPE_IO; |
28 reader_thread_.StartWithOptions(options); | 27 reader_thread_.StartWithOptions(options); |
29 | 28 |
30 read_task_runner_ = reader_thread_.task_runner(); | 29 read_task_runner_ = reader_thread_.task_runner(); |
31 main_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 30 main_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
32 } | 31 } |
33 | 32 |
34 RemoteSecurityKeyMessageReader::~RemoteSecurityKeyMessageReader() { | 33 SecurityKeyMessageReader::~SecurityKeyMessageReader() { |
35 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | 34 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); |
36 | 35 |
37 // In order to ensure the reader thread is stopped cleanly, we close the | 36 // 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. | 37 // stream it is blocking on and then wait for the thread to exit. |
39 read_stream_.Close(); | 38 read_stream_.Close(); |
40 reader_thread_.Stop(); | 39 reader_thread_.Stop(); |
41 } | 40 } |
42 | 41 |
43 void RemoteSecurityKeyMessageReader::Start( | 42 void SecurityKeyMessageReader::Start( |
44 SecurityKeyMessageCallback message_callback, | 43 SecurityKeyMessageCallback message_callback, |
45 base::Closure error_callback) { | 44 base::Closure error_callback) { |
46 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | 45 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); |
47 | 46 |
48 message_callback_ = message_callback; | 47 message_callback_ = message_callback; |
49 error_callback_ = error_callback; | 48 error_callback_ = error_callback; |
50 | 49 |
51 // 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 |
52 // which will be destroyed before this instance is. | 51 // which will be destroyed before this instance is. |
53 read_task_runner_->PostTask( | 52 read_task_runner_->PostTask(FROM_HERE, |
54 FROM_HERE, base::Bind(&RemoteSecurityKeyMessageReader::ReadMessage, | 53 base::Bind(&SecurityKeyMessageReader::ReadMessage, |
55 base::Unretained(this))); | 54 base::Unretained(this))); |
56 } | 55 } |
57 | 56 |
58 void RemoteSecurityKeyMessageReader::ReadMessage() { | 57 void SecurityKeyMessageReader::ReadMessage() { |
59 DCHECK(read_task_runner_->RunsTasksOnCurrentThread()); | 58 DCHECK(read_task_runner_->RunsTasksOnCurrentThread()); |
60 | 59 |
61 while (true) { | 60 while (true) { |
62 if (!read_stream_.IsValid()) { | 61 if (!read_stream_.IsValid()) { |
63 LOG(ERROR) << "Cannot read from invalid stream."; | 62 LOG(ERROR) << "Cannot read from invalid stream."; |
64 NotifyError(); | 63 NotifyError(); |
65 return; | 64 return; |
66 } | 65 } |
67 | 66 |
68 // Read the message header to retrieve the remaining message length. | 67 // Read the message header to retrieve the remaining message length. |
(...skipping 30 matching lines...) Expand all Loading... |
99 std::unique_ptr<SecurityKeyMessage> message(new SecurityKeyMessage()); | 98 std::unique_ptr<SecurityKeyMessage> message(new SecurityKeyMessage()); |
100 if (!message->ParseMessage(message_data)) { | 99 if (!message->ParseMessage(message_data)) { |
101 LOG(ERROR) << "Invalid message data received."; | 100 LOG(ERROR) << "Invalid message data received."; |
102 NotifyError(); | 101 NotifyError(); |
103 return; | 102 return; |
104 } | 103 } |
105 | 104 |
106 // Notify callback of the new message received. | 105 // Notify callback of the new message received. |
107 main_task_runner_->PostTask( | 106 main_task_runner_->PostTask( |
108 FROM_HERE, | 107 FROM_HERE, |
109 base::Bind(&RemoteSecurityKeyMessageReader::InvokeMessageCallback, | 108 base::Bind(&SecurityKeyMessageReader::InvokeMessageCallback, |
110 weak_factory_.GetWeakPtr(), base::Passed(&message))); | 109 weak_factory_.GetWeakPtr(), base::Passed(&message))); |
111 } | 110 } |
112 } | 111 } |
113 | 112 |
114 void RemoteSecurityKeyMessageReader::NotifyError() { | 113 void SecurityKeyMessageReader::NotifyError() { |
115 DCHECK(read_task_runner_->RunsTasksOnCurrentThread()); | 114 DCHECK(read_task_runner_->RunsTasksOnCurrentThread()); |
116 | 115 |
117 main_task_runner_->PostTask( | 116 main_task_runner_->PostTask( |
118 FROM_HERE, | 117 FROM_HERE, base::Bind(&SecurityKeyMessageReader::InvokeErrorCallback, |
119 base::Bind(&RemoteSecurityKeyMessageReader::InvokeErrorCallback, | 118 weak_factory_.GetWeakPtr())); |
120 weak_factory_.GetWeakPtr())); | |
121 } | 119 } |
122 | 120 |
123 void RemoteSecurityKeyMessageReader::InvokeMessageCallback( | 121 void SecurityKeyMessageReader::InvokeMessageCallback( |
124 std::unique_ptr<SecurityKeyMessage> message) { | 122 std::unique_ptr<SecurityKeyMessage> message) { |
125 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | 123 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); |
126 message_callback_.Run(std::move(message)); | 124 message_callback_.Run(std::move(message)); |
127 } | 125 } |
128 | 126 |
129 void RemoteSecurityKeyMessageReader::InvokeErrorCallback() { | 127 void SecurityKeyMessageReader::InvokeErrorCallback() { |
130 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | 128 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); |
131 error_callback_.Run(); | 129 error_callback_.Run(); |
132 } | 130 } |
133 | 131 |
134 } // namespace remoting | 132 } // namespace remoting |
OLD | NEW |