Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(628)

Side by Side Diff: remoting/host/security_key/remote_security_key_message_reader.cc

Issue 1818233005: Adding the message reading class used for remote_security_key STDIN communication. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@writer
Patch Set: Integrating security message changes Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 <stdint.h>
8
9 #include <string>
10 #include <utility>
11
12 #include "base/bind.h"
13 #include "base/files/file.h"
14 #include "base/macros.h"
15 #include "base/sequenced_task_runner.h"
16 #include "base/single_thread_task_runner.h"
17 #include "base/stl_util.h"
18 #include "base/thread_task_runner_handle.h"
19 #include "base/threading/thread_checker.h"
20 #include "remoting/host/security_key/security_key_message.h"
21
22 namespace remoting {
23
24 class RemoteSecurityKeyMessageReader::Core {
25 public:
26 Core(base::File input_file,
27 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner,
28 base::WeakPtr<RemoteSecurityKeyMessageReader> reader_);
29 ~Core();
30
31 // Reads a message from the remote security key process and passes it to
32 // |message_callback_| on the originating thread. Called on the reader thread.
33 void ReadMessage();
34
35 private:
36 // Notify the owning class when an error occurs or EOF is reached.
37 void NotifyError();
38
39 base::File read_stream_;
40
41 base::WeakPtr<RemoteSecurityKeyMessageReader> reader_;
42
43 // Used to post callbacks on the caller thread.
44 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_;
45
46 base::ThreadChecker thread_checker_;
47
48 DISALLOW_COPY_AND_ASSIGN(Core);
49 };
50
51 RemoteSecurityKeyMessageReader::Core::Core(
52 base::File input_file,
53 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner,
54 base::WeakPtr<RemoteSecurityKeyMessageReader> reader)
55 : read_stream_(std::move(input_file)),
56 reader_(reader),
57 caller_task_runner_(caller_task_runner) {
58 thread_checker_.DetachFromThread();
59 }
60
61 RemoteSecurityKeyMessageReader::Core::~Core() {}
62
63 void RemoteSecurityKeyMessageReader::Core::ReadMessage() {
64 DCHECK(thread_checker_.CalledOnValidThread());
65
66 while (true) {
67 if (!read_stream_.IsValid()) {
68 LOG(ERROR) << "Cannot read from invalid stream.";
69 NotifyError();
70 return;
71 }
72
73 // Read the message header to retrieve the remaining message length.
74 uint32_t total_message_size_bytes;
75 int read_result = read_stream_.ReadAtCurrentPos(
76 reinterpret_cast<char*>(&total_message_size_bytes),
77 SecurityKeyMessage::kHeaderSizeBytes);
78 if (read_result != SecurityKeyMessage::kHeaderSizeBytes) {
79 // 0 means EOF which is normal and should not be logged as an error.
80 if (read_result != 0) {
81 LOG(ERROR) << "Failed to read message header, read returned "
82 << read_result;
83 }
84 NotifyError();
85 return;
86 }
87
88 if (!SecurityKeyMessage::IsValidMessageSize(total_message_size_bytes)) {
89 LOG(ERROR) << "Message size too large: " << total_message_size_bytes;
90 NotifyError();
91 return;
92 }
93
94 std::string message_data(total_message_size_bytes, '\0');
95 read_result = read_stream_.ReadAtCurrentPos(string_as_array(&message_data),
96 total_message_size_bytes);
97 // The static cast is safe as we know the value is smaller than max int.
98 if (read_result != static_cast<int>(total_message_size_bytes)) {
99 LOG(ERROR) << "Failed to read message: " << read_result;
100 NotifyError();
101 return;
102 }
103
104 scoped_ptr<SecurityKeyMessage> message(new SecurityKeyMessage());
105 if (!message->ParseMessage(message_data)) {
106 LOG(ERROR) << "Invalid message data received.";
107 NotifyError();
108 return;
109 }
110
111 // Notify callback of the new message received.
112 caller_task_runner_->PostTask(
113 FROM_HERE,
114 base::Bind(&RemoteSecurityKeyMessageReader::InvokeMessageCallback,
115 reader_, base::Passed(&message)));
116 }
117 }
118
119 void RemoteSecurityKeyMessageReader::Core::NotifyError() {
120 DCHECK(thread_checker_.CalledOnValidThread());
121 caller_task_runner_->PostTask(
122 FROM_HERE,
123 base::Bind(&RemoteSecurityKeyMessageReader::InvokeErrorCallback,
124 reader_));
125 }
126
127 RemoteSecurityKeyMessageReader::RemoteSecurityKeyMessageReader(
128 base::File input_file)
129 : reader_thread_("RemoteSecurityKeyMessageReader"), weak_factory_(this) {
130 reader_thread_.Start();
131 read_task_runner_ = reader_thread_.task_runner();
132 core_.reset(new Core(std::move(input_file),
133 base::ThreadTaskRunnerHandle::Get(),
134 weak_factory_.GetWeakPtr()));
135 }
136
137 RemoteSecurityKeyMessageReader::~RemoteSecurityKeyMessageReader() {
138 read_task_runner_->DeleteSoon(FROM_HERE, core_.release());
139 }
Sergey Ulanov 2016/03/28 18:52:45 Thread destructor will join the thread and it will
joedow 2016/03/30 19:39:00 I don't think I have the same problem here. I clo
140
141 void RemoteSecurityKeyMessageReader::Start(
142 SecurityKeyMessage::Callback message_callback,
143 base::Closure error_callback) {
144 message_callback_ = message_callback;
145 error_callback_ = error_callback;
146
147 // base::Unretained is safe since |core_| is only deleted via the
148 // DeleteSoon task which is posted from this class's dtor.
149 read_task_runner_->PostTask(
150 FROM_HERE, base::Bind(&RemoteSecurityKeyMessageReader::Core::ReadMessage,
151 base::Unretained(core_.get())));
152 }
153
154 void RemoteSecurityKeyMessageReader::InvokeMessageCallback(
155 scoped_ptr<SecurityKeyMessage> message) {
156 message_callback_.Run(std::move(message));
157 }
158
159 void RemoteSecurityKeyMessageReader::InvokeErrorCallback() {
160 error_callback_.Run();
161 }
162
163 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698