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

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

Issue 2170563002: Revert of Renaming Gnubby and RemoteSecurityKey files/classes/members (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 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/security_key_message_reader.h"
6
7 #include <cstdint>
8 #include <string>
9 #include <utility>
10
11 #include "base/bind.h"
12 #include "base/files/file.h"
13 #include "base/macros.h"
14 #include "base/single_thread_task_runner.h"
15 #include "base/stl_util.h"
16 #include "base/threading/thread_task_runner_handle.h"
17 #include "remoting/host/security_key/security_key_message.h"
18
19 namespace remoting {
20
21 SecurityKeyMessageReader::SecurityKeyMessageReader(base::File input_file)
22 : read_stream_(std::move(input_file)),
23 reader_thread_("SecurityKeyMessageReader"),
24 weak_factory_(this) {
25 base::Thread::Options options;
26 options.message_loop_type = base::MessageLoop::TYPE_IO;
27 reader_thread_.StartWithOptions(options);
28
29 read_task_runner_ = reader_thread_.task_runner();
30 main_task_runner_ = base::ThreadTaskRunnerHandle::Get();
31 }
32
33 SecurityKeyMessageReader::~SecurityKeyMessageReader() {
34 DCHECK(main_task_runner_->RunsTasksOnCurrentThread());
35
36 // In order to ensure the reader thread is stopped cleanly, we close the
37 // stream it is blocking on and then wait for the thread to exit.
38 read_stream_.Close();
39 reader_thread_.Stop();
40 }
41
42 void SecurityKeyMessageReader::Start(
43 SecurityKeyMessageCallback message_callback,
44 base::Closure error_callback) {
45 DCHECK(main_task_runner_->RunsTasksOnCurrentThread());
46
47 message_callback_ = message_callback;
48 error_callback_ = error_callback;
49
50 // base::Unretained is safe since this class owns the thread running this task
51 // which will be destroyed before this instance is.
52 read_task_runner_->PostTask(FROM_HERE,
53 base::Bind(&SecurityKeyMessageReader::ReadMessage,
54 base::Unretained(this)));
55 }
56
57 void SecurityKeyMessageReader::ReadMessage() {
58 DCHECK(read_task_runner_->RunsTasksOnCurrentThread());
59
60 while (true) {
61 if (!read_stream_.IsValid()) {
62 LOG(ERROR) << "Cannot read from invalid stream.";
63 NotifyError();
64 return;
65 }
66
67 // Read the message header to retrieve the remaining message length.
68 uint32_t total_message_size_bytes;
69 int read_result = read_stream_.ReadAtCurrentPos(
70 reinterpret_cast<char*>(&total_message_size_bytes),
71 SecurityKeyMessage::kHeaderSizeBytes);
72 if (read_result != SecurityKeyMessage::kHeaderSizeBytes) {
73 // 0 means EOF which is normal and should not be logged as an error.
74 if (read_result != 0) {
75 LOG(ERROR) << "Failed to read message header, read returned "
76 << read_result;
77 }
78 NotifyError();
79 return;
80 }
81
82 if (!SecurityKeyMessage::IsValidMessageSize(total_message_size_bytes)) {
83 LOG(ERROR) << "Message size too large: " << total_message_size_bytes;
84 NotifyError();
85 return;
86 }
87
88 std::string message_data(total_message_size_bytes, '\0');
89 read_result = read_stream_.ReadAtCurrentPos(string_as_array(&message_data),
90 total_message_size_bytes);
91 // The static cast is safe as we know the value is smaller than max int.
92 if (read_result != static_cast<int>(total_message_size_bytes)) {
93 LOG(ERROR) << "Failed to read message: " << read_result;
94 NotifyError();
95 return;
96 }
97
98 std::unique_ptr<SecurityKeyMessage> message(new SecurityKeyMessage());
99 if (!message->ParseMessage(message_data)) {
100 LOG(ERROR) << "Invalid message data received.";
101 NotifyError();
102 return;
103 }
104
105 // Notify callback of the new message received.
106 main_task_runner_->PostTask(
107 FROM_HERE,
108 base::Bind(&SecurityKeyMessageReader::InvokeMessageCallback,
109 weak_factory_.GetWeakPtr(), base::Passed(&message)));
110 }
111 }
112
113 void SecurityKeyMessageReader::NotifyError() {
114 DCHECK(read_task_runner_->RunsTasksOnCurrentThread());
115
116 main_task_runner_->PostTask(
117 FROM_HERE, base::Bind(&SecurityKeyMessageReader::InvokeErrorCallback,
118 weak_factory_.GetWeakPtr()));
119 }
120
121 void SecurityKeyMessageReader::InvokeMessageCallback(
122 std::unique_ptr<SecurityKeyMessage> message) {
123 DCHECK(main_task_runner_->RunsTasksOnCurrentThread());
124 message_callback_.Run(std::move(message));
125 }
126
127 void SecurityKeyMessageReader::InvokeErrorCallback() {
128 DCHECK(main_task_runner_->RunsTasksOnCurrentThread());
129 error_callback_.Run();
130 }
131
132 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698