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

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

Issue 1855613002: Adding the RemoteSecurityKeyMessageHandler class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressing feedback 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_handler.h"
6
7 #include <cstdint>
8 #include <string>
9 #include <utility>
10
11 #include "base/bind.h"
12 #include "base/callback.h"
13 #include "base/callback_helpers.h"
14 #include "remoting/host/security_key/remote_security_key_ipc_client.h"
15 #include "remoting/host/security_key/remote_security_key_ipc_constants.h"
16 #include "remoting/host/security_key/remote_security_key_message_reader_impl.h"
17 #include "remoting/host/security_key/remote_security_key_message_writer_impl.h"
18
19 namespace remoting {
20
21 RemoteSecurityKeyMessageHandler::RemoteSecurityKeyMessageHandler() {}
22
23 RemoteSecurityKeyMessageHandler::~RemoteSecurityKeyMessageHandler() {}
24
25 void RemoteSecurityKeyMessageHandler::Start(
26 base::File message_read_stream,
27 base::File message_write_stream,
28 scoped_ptr<RemoteSecurityKeyIpcClient> ipc_client,
29 const base::Closure& error_callback) {
30 DCHECK(thread_checker_.CalledOnValidThread());
31 DCHECK(message_read_stream.IsValid());
32 DCHECK(message_write_stream.IsValid());
33 DCHECK(ipc_client);
34 DCHECK(!error_callback.is_null());
35 DCHECK(error_callback_.is_null());
36
37 if (!reader_) {
38 reader_.reset(
39 new RemoteSecurityKeyMessageReaderImpl(std::move(message_read_stream)));
40 }
41
42 if (!writer_) {
43 writer_.reset(new RemoteSecurityKeyMessageWriterImpl(
44 std::move(message_write_stream)));
45 }
46
47 ipc_client_ = std::move(ipc_client);
48 error_callback_ = error_callback;
49
50 reader_->Start(
51 base::Bind(
52 &RemoteSecurityKeyMessageHandler::ProcessRemoteSecurityKeyMessage,
53 base::Unretained(this)),
54 base::Bind(&RemoteSecurityKeyMessageHandler::OnError,
55 base::Unretained(this)));
56 }
57
58 void RemoteSecurityKeyMessageHandler::SetRemoteSecurityKeyMessageReaderForTest(
59 scoped_ptr<RemoteSecurityKeyMessageReader> reader) {
60 DCHECK(!reader_);
61 reader_ = std::move(reader);
62 }
63
64 void RemoteSecurityKeyMessageHandler::SetRemoteSecurityKeyMessageWriterForTest(
65 scoped_ptr<RemoteSecurityKeyMessageWriter> writer) {
66 DCHECK(!writer_);
67 writer_ = std::move(writer);
68 }
69
70 void RemoteSecurityKeyMessageHandler::ProcessRemoteSecurityKeyMessage(
71 scoped_ptr<SecurityKeyMessage> message) {
72 DCHECK(thread_checker_.CalledOnValidThread());
73
74 RemoteSecurityKeyMessageType message_type = message->type();
75 if (message_type == RemoteSecurityKeyMessageType::CONNECT) {
76 HandleConnectRequest(message->payload());
77 } else if (message_type == RemoteSecurityKeyMessageType::REQUEST) {
78 HandleSecurityKeyRequest(message->payload());
79 } else {
80 LOG(ERROR) << "Unknown message type: "
81 << static_cast<uint8_t>(message_type);
82 SendMessage(RemoteSecurityKeyMessageType::UNKNOWN_COMMAND);
83 }
84 }
85
86 void RemoteSecurityKeyMessageHandler::HandleIpcConnectionChange(
87 bool connection_established) {
88 DCHECK(thread_checker_.CalledOnValidThread());
89 if (connection_established) {
90 SendMessageWithPayload(RemoteSecurityKeyMessageType::CONNECT_RESPONSE, "1");
91 } else {
92 SendMessageWithPayload(
93 RemoteSecurityKeyMessageType::CONNECT_ERROR,
94 "Unknown error occurred while establishing connection.");
95 }
96 }
97
98 void RemoteSecurityKeyMessageHandler::HandleSecurityKeyResponse(
99 const std::string& response_data) {
100 if (response_data.compare(kRemoteSecurityKeyConnectionError) == 0) {
101 SendMessageWithPayload(RemoteSecurityKeyMessageType::REQUEST_ERROR,
102 "An error occurred during the request.");
103 return;
104 }
105
106 if (response_data.empty()) {
107 SendMessageWithPayload(RemoteSecurityKeyMessageType::REQUEST_ERROR,
108 "Invalid client response received.");
109 return;
110 }
111
112 SendMessageWithPayload(RemoteSecurityKeyMessageType::REQUEST_RESPONSE,
113 response_data);
114 }
115
116 void RemoteSecurityKeyMessageHandler::HandleConnectRequest(
117 const std::string& message_payload) {
118 DCHECK(thread_checker_.CalledOnValidThread());
119 if (!message_payload.empty()) {
120 SendMessageWithPayload(RemoteSecurityKeyMessageType::CONNECT_ERROR,
121 "Unexpected payload data received.");
122 return;
123 }
124
125 if (ipc_client_->WaitForSecurityKeyIpcServerChannel()) {
126 // If we find an IPC server, then attempt to establish a connection.
127 ipc_client_->EstablishIpcConnection(
128 base::Bind(&RemoteSecurityKeyMessageHandler::HandleIpcConnectionChange,
129 base::Unretained(this), true),
130 base::Bind(&RemoteSecurityKeyMessageHandler::HandleIpcConnectionChange,
131 base::Unretained(this), false));
132 } else {
133 SendMessageWithPayload(RemoteSecurityKeyMessageType::CONNECT_RESPONSE, "0");
134 }
135 }
136
137 void RemoteSecurityKeyMessageHandler::HandleSecurityKeyRequest(
138 const std::string& message_payload) {
139 DCHECK(thread_checker_.CalledOnValidThread());
140 if (message_payload.empty()) {
141 SendMessageWithPayload(RemoteSecurityKeyMessageType::REQUEST_ERROR,
142 "Request sent without request data.");
143 return;
144 }
145
146 if (!ipc_client_->SendSecurityKeyRequest(
147 message_payload,
148 base::Bind(
149 &RemoteSecurityKeyMessageHandler::HandleSecurityKeyResponse,
150 base::Unretained(this)))) {
151 SendMessageWithPayload(RemoteSecurityKeyMessageType::REQUEST_ERROR,
152 "Failed to send request data.");
153 }
154 }
155
156 void RemoteSecurityKeyMessageHandler::SendMessage(
157 RemoteSecurityKeyMessageType message_type) {
158 if (!writer_->WriteMessage(message_type)) {
159 OnError();
160 }
161 }
162
163 void RemoteSecurityKeyMessageHandler::SendMessageWithPayload(
164 RemoteSecurityKeyMessageType message_type,
165 const std::string& message_payload) {
166 if (!writer_->WriteMessageWithPayload(message_type, message_payload)) {
167 OnError();
168 }
169 }
170
171 void RemoteSecurityKeyMessageHandler::OnError() {
172 DCHECK(thread_checker_.CalledOnValidThread());
173 ipc_client_.reset();
174 writer_.reset();
175 reader_.reset();
176
177 if (!error_callback_.is_null()) {
178 base::ResetAndReturn(&error_callback_).Run();
179 }
180 }
181
182 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698