1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "remoting/host/security_key/security_key_message_handler.h"
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/logging.h"
#include "remoting/base/logging.h"
#include "remoting/host/security_key/security_key_ipc_client.h"
#include "remoting/host/security_key/security_key_ipc_constants.h"
#include "remoting/host/security_key/security_key_message_reader_impl.h"
#include "remoting/host/security_key/security_key_message_writer_impl.h"
namespace remoting {
SecurityKeyMessageHandler::SecurityKeyMessageHandler() = default;
SecurityKeyMessageHandler::~SecurityKeyMessageHandler() {
DCHECK(thread_checker_.CalledOnValidThread());
}
void SecurityKeyMessageHandler::Start(
base::File message_read_stream,
base::File message_write_stream,
std::unique_ptr<SecurityKeyIpcClient> ipc_client,
base::OnceClosure error_callback) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(message_read_stream.IsValid());
DCHECK(message_write_stream.IsValid());
DCHECK(ipc_client);
DCHECK(!error_callback.is_null());
DCHECK(error_callback_.is_null());
if (!reader_) {
reader_ = std::make_unique<SecurityKeyMessageReaderImpl>(
std::move(message_read_stream));
}
if (!writer_) {
writer_ = std::make_unique<SecurityKeyMessageWriterImpl>(
std::move(message_write_stream));
}
ipc_client_ = std::move(ipc_client);
error_callback_ = std::move(error_callback);
reader_->Start(
base::BindRepeating(&SecurityKeyMessageHandler::ProcessSecurityKeyMessage,
base::Unretained(this)),
base::BindOnce(&SecurityKeyMessageHandler::OnError,
base::Unretained(this)));
}
void SecurityKeyMessageHandler::SetSecurityKeyMessageReaderForTest(
std::unique_ptr<SecurityKeyMessageReader> reader) {
DCHECK(!reader_);
reader_ = std::move(reader);
}
void SecurityKeyMessageHandler::SetSecurityKeyMessageWriterForTest(
std::unique_ptr<SecurityKeyMessageWriter> writer) {
DCHECK(!writer_);
writer_ = std::move(writer);
}
void SecurityKeyMessageHandler::ProcessSecurityKeyMessage(
std::unique_ptr<SecurityKeyMessage> message) {
DCHECK(thread_checker_.CalledOnValidThread());
SecurityKeyMessageType message_type = message->type();
HOST_LOG << "Received message from pipe. type="
<< static_cast<int>(message_type);
if (message_type == SecurityKeyMessageType::CONNECT) {
HandleConnectRequest(message->payload());
} else if (message_type == SecurityKeyMessageType::REQUEST) {
HandleSecurityKeyRequest(message->payload());
} else {
// uint8_t is handled as char, so we use uint16_t to show number here.
LOG(ERROR) << "Unknown message type: "
<< static_cast<uint16_t>(message_type);
SendMessage(SecurityKeyMessageType::UNKNOWN_COMMAND);
}
}
void SecurityKeyMessageHandler::HandleIpcConnectionChange() {
DCHECK(thread_checker_.CalledOnValidThread());
SendMessageWithPayload(SecurityKeyMessageType::CONNECT_RESPONSE,
std::string(1, kConnectResponseActiveSession));
}
void SecurityKeyMessageHandler::HandleIpcConnectionError() {
DCHECK(thread_checker_.CalledOnValidThread());
SendMessageWithPayload(SecurityKeyMessageType::CONNECT_ERROR,
"Unknown error occurred during connection.");
}
void SecurityKeyMessageHandler::HandleSecurityKeyResponse(
const std::string& response_data) {
if (response_data.compare(kSecurityKeyConnectionError) == 0) {
SendMessageWithPayload(SecurityKeyMessageType::REQUEST_ERROR,
"An error occurred during the request.");
return;
}
if (response_data.empty()) {
SendMessageWithPayload(SecurityKeyMessageType::REQUEST_ERROR,
"Invalid client response received.");
return;
}
SendMessageWithPayload(SecurityKeyMessageType::REQUEST_RESPONSE,
response_data);
}
void SecurityKeyMessageHandler::HandleConnectRequest(
const std::string& message_payload) {
DCHECK(thread_checker_.CalledOnValidThread());
if (!message_payload.empty()) {
SendMessageWithPayload(SecurityKeyMessageType::CONNECT_ERROR,
"Unexpected payload data received.");
return;
}
if (ipc_client_->CheckForSecurityKeyIpcServerChannel()) {
// If we find an IPC server, then attempt to establish a connection.
ipc_client_->EstablishIpcConnection(
base::BindOnce(&SecurityKeyMessageHandler::HandleIpcConnectionChange,
base::Unretained(this)),
base::BindOnce(&SecurityKeyMessageHandler::HandleIpcConnectionError,
base::Unretained(this)));
} else {
SendMessageWithPayload(SecurityKeyMessageType::CONNECT_RESPONSE,
std::string(1, kConnectResponseNoSession));
}
}
void SecurityKeyMessageHandler::HandleSecurityKeyRequest(
const std::string& message_payload) {
DCHECK(thread_checker_.CalledOnValidThread());
if (message_payload.empty()) {
SendMessageWithPayload(SecurityKeyMessageType::REQUEST_ERROR,
"Request sent without request data.");
return;
}
if (!ipc_client_->SendSecurityKeyRequest(
message_payload,
base::BindRepeating(
&SecurityKeyMessageHandler::HandleSecurityKeyResponse,
base::Unretained(this)))) {
SendMessageWithPayload(SecurityKeyMessageType::REQUEST_ERROR,
"Failed to send request data.");
}
}
void SecurityKeyMessageHandler::SendMessage(
SecurityKeyMessageType message_type) {
if (!writer_->WriteMessage(message_type)) {
OnError();
}
}
void SecurityKeyMessageHandler::SendMessageWithPayload(
SecurityKeyMessageType message_type,
const std::string& message_payload) {
if (!writer_->WriteMessageWithPayload(message_type, message_payload)) {
HOST_LOG << "Failed to send message to pipe. type="
<< static_cast<int>(message_type);
OnError();
} else {
HOST_LOG << "Successfully sent message to pipe. type="
<< static_cast<int>(message_type);
}
}
void SecurityKeyMessageHandler::OnError() {
DCHECK(thread_checker_.CalledOnValidThread());
ipc_client_.reset();
writer_.reset();
reader_.reset();
if (!error_callback_.is_null()) {
std::move(error_callback_).Run();
}
}
} // namespace remoting
|