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

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: Fixing a sign mismatch (non-windows) build issue. Created 4 years, 8 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/single_thread_task_runner.h"
16 #include "base/stl_util.h"
Sergey Ulanov 2016/03/30 21:34:28 Don't need this
joedow 2016/03/31 01:45:12 The 'string_as_array' function I use below is defi
17 #include "base/thread_task_runner_handle.h"
18 #include "base/threading/thread_checker.h"
Sergey Ulanov 2016/03/30 21:34:28 Don't need this include.
joedow 2016/03/31 01:45:12 Done.
19 #include "remoting/host/security_key/security_key_message.h"
20
21 namespace remoting {
22
23 RemoteSecurityKeyMessageReader::RemoteSecurityKeyMessageReader(
24 base::File input_file)
25 : read_stream_(std::move(input_file)),
26 reader_thread_("RemoteSecurityKeyMessageReader"),
27 weak_factory_(this) {
28 base::Thread::Options options;
29 options.message_loop_type = base::MessageLoop::TYPE_IO;
30 reader_thread_.StartWithOptions(options);
31
32 read_task_runner_ = reader_thread_.task_runner();
33 main_task_runner_ = base::ThreadTaskRunnerHandle::Get();
34 reader_ = weak_factory_.GetWeakPtr();
Sergey Ulanov 2016/03/30 21:34:28 I don't think you really need reader_. Just use we
joedow 2016/03/31 01:45:12 Done.
35 }
36
37 RemoteSecurityKeyMessageReader::~RemoteSecurityKeyMessageReader() {
38 DCHECK(main_task_runner_->RunsTasksOnCurrentThread());
39
40 // In order to ensure the reader thread is stopped cleanly, we close the
41 // stream it is blocking on and then wait for the thread to exit.
42 read_stream_.Close();
43 reader_thread_.Stop();
Sergey Ulanov 2016/03/30 21:34:28 nit: Thread destructor calls stop, so you don't ne
joedow 2016/03/31 01:45:12 Acknowledged.
44 }
45
46 void RemoteSecurityKeyMessageReader::Start(
47 SecurityKeyMessageCallback message_callback,
48 base::Closure error_callback) {
49 DCHECK(main_task_runner_->RunsTasksOnCurrentThread());
50
51 message_callback_ = message_callback;
52 error_callback_ = error_callback;
53
54 // base::Unretained is safe since this class owns the thread running this task
55 // which will be destroyed before this instance is.
56 read_task_runner_->PostTask(
57 FROM_HERE, base::Bind(&RemoteSecurityKeyMessageReader::ReadMessage,
58 base::Unretained(this)));
59 }
60
61 void RemoteSecurityKeyMessageReader::ReadMessage() {
62 DCHECK(read_task_runner_->RunsTasksOnCurrentThread());
63
64 while (true) {
65 if (!read_stream_.IsValid()) {
66 LOG(ERROR) << "Cannot read from invalid stream.";
67 NotifyError();
68 return;
69 }
70
71 // Read the message header to retrieve the remaining message length.
72 uint32_t total_message_size_bytes;
73 int read_result = read_stream_.ReadAtCurrentPos(
74 reinterpret_cast<char*>(&total_message_size_bytes),
75 SecurityKeyMessage::kHeaderSizeBytes);
76 if (read_result != SecurityKeyMessage::kHeaderSizeBytes) {
77 // 0 means EOF which is normal and should not be logged as an error.
78 if (read_result != 0) {
79 LOG(ERROR) << "Failed to read message header, read returned "
80 << read_result;
81 }
82 NotifyError();
83 return;
84 }
85
86 if (!SecurityKeyMessage::IsValidMessageSize(total_message_size_bytes)) {
87 LOG(ERROR) << "Message size too large: " << total_message_size_bytes;
88 NotifyError();
89 return;
90 }
91
92 std::string message_data(total_message_size_bytes, '\0');
93 read_result = read_stream_.ReadAtCurrentPos(string_as_array(&message_data),
94 total_message_size_bytes);
95 // The static cast is safe as we know the value is smaller than max int.
96 if (read_result != static_cast<int>(total_message_size_bytes)) {
97 LOG(ERROR) << "Failed to read message: " << read_result;
98 NotifyError();
99 return;
100 }
101
102 scoped_ptr<SecurityKeyMessage> message(new SecurityKeyMessage());
103 if (!message->ParseMessage(message_data)) {
104 LOG(ERROR) << "Invalid message data received.";
105 NotifyError();
106 return;
107 }
108
109 // Notify callback of the new message received.
110 main_task_runner_->PostTask(
111 FROM_HERE,
112 base::Bind(&RemoteSecurityKeyMessageReader::InvokeMessageCallback,
113 reader_, base::Passed(&message)));
114 }
115 }
116
117 void RemoteSecurityKeyMessageReader::NotifyError() {
118 DCHECK(read_task_runner_->RunsTasksOnCurrentThread());
119
120 main_task_runner_->PostTask(
121 FROM_HERE,
122 base::Bind(&RemoteSecurityKeyMessageReader::InvokeErrorCallback,
123 reader_));
124 }
125
126 void RemoteSecurityKeyMessageReader::InvokeMessageCallback(
127 scoped_ptr<SecurityKeyMessage> message) {
128 DCHECK(main_task_runner_->RunsTasksOnCurrentThread());
129 message_callback_.Run(std::move(message));
130 }
131
132 void RemoteSecurityKeyMessageReader::InvokeErrorCallback() {
133 DCHECK(main_task_runner_->RunsTasksOnCurrentThread());
134 error_callback_.Run();
135 }
136
137 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698