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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/ash/components/proximity_auth/messenger_impl.h"
#include <memory>
#include <utility>
#include "base/functional/bind.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/location.h"
#include "chromeos/ash/components/multidevice/logging/logging.h"
#include "chromeos/ash/components/proximity_auth/messenger_observer.h"
#include "chromeos/ash/components/proximity_auth/remote_status_update.h"
namespace proximity_auth {
namespace {
// The key names of JSON fields for messages sent between the devices.
const char kTypeKey[] = "type";
const char kNameKey[] = "name";
// The types of messages that can be sent and received.
const char kMessageTypeLocalEvent[] = "event";
const char kMessageTypeRemoteStatusUpdate[] = "status_update";
const char kMessageTypeUnlockRequest[] = "unlock_request";
const char kMessageTypeUnlockResponse[] = "unlock_response";
// The name for an unlock event originating from the local device.
const char kUnlockEventName[] = "easy_unlock";
// Serializes the |value| to a JSON string and returns the result.
std::string SerializeValueToJson(const base::Value::Dict& value) {
std::string json;
base::JSONWriter::Write(value, &json);
return json;
}
// Returns the message type represented by the |message|. This is a convenience
// wrapper that should only be called when the |message| is known to specify its
// message type, i.e. this should not be called for untrusted input.
std::string GetMessageType(const base::Value::Dict& message) {
const std::string* type = message.FindString(kTypeKey);
return type ? *type : std::string();
}
} // namespace
MessengerImpl::MessengerImpl(
std::unique_ptr<ash::secure_channel::ClientChannel> channel)
: channel_(std::move(channel)) {
DCHECK(!channel_->is_disconnected());
channel_->AddObserver(this);
}
MessengerImpl::~MessengerImpl() {
channel_->RemoveObserver(this);
}
void MessengerImpl::AddObserver(MessengerObserver* observer) {
observers_.AddObserver(observer);
}
void MessengerImpl::RemoveObserver(MessengerObserver* observer) {
observers_.RemoveObserver(observer);
}
void MessengerImpl::DispatchUnlockEvent() {
base::Value::Dict message;
message.Set(kTypeKey, kMessageTypeLocalEvent);
message.Set(kNameKey, kUnlockEventName);
queued_messages_.push_back(PendingMessage(message));
ProcessMessageQueue();
}
void MessengerImpl::RequestUnlock() {
base::Value::Dict message;
message.Set(kTypeKey, kMessageTypeUnlockRequest);
queued_messages_.push_back(PendingMessage(message));
ProcessMessageQueue();
}
ash::secure_channel::ClientChannel* MessengerImpl::GetChannel() const {
if (channel_->is_disconnected())
return nullptr;
return channel_.get();
}
MessengerImpl::PendingMessage::PendingMessage() = default;
MessengerImpl::PendingMessage::~PendingMessage() = default;
MessengerImpl::PendingMessage::PendingMessage(const base::Value::Dict& message)
: json_message(SerializeValueToJson(message)),
type(GetMessageType(message)) {}
MessengerImpl::PendingMessage::PendingMessage(const std::string& message)
: json_message(message), type(std::string()) {}
void MessengerImpl::ProcessMessageQueue() {
if (pending_message_ || queued_messages_.empty())
return;
if (channel_->is_disconnected())
return;
pending_message_ = std::make_unique<PendingMessage>(queued_messages_.front());
queued_messages_.pop_front();
channel_->SendMessage(
pending_message_->json_message,
base::BindOnce(&MessengerImpl::OnSendMessageResult,
weak_ptr_factory_.GetWeakPtr(), true /* success */));
}
void MessengerImpl::HandleRemoteStatusUpdateMessage(
const base::Value::Dict& message) {
std::unique_ptr<RemoteStatusUpdate> status_update =
RemoteStatusUpdate::Deserialize(message);
if (!status_update) {
PA_LOG(ERROR) << "Unexpected remote status update: " << message;
return;
}
for (auto& observer : observers_)
observer.OnRemoteStatusUpdate(*status_update);
}
void MessengerImpl::HandleUnlockResponseMessage(
const base::Value::Dict& message) {
for (auto& observer : observers_)
observer.OnUnlockResponse(true);
}
void MessengerImpl::OnDisconnected() {
for (auto& observer : observers_)
observer.OnDisconnected();
}
void MessengerImpl::OnMessageReceived(const std::string& payload) {
HandleMessage(payload);
}
void MessengerImpl::HandleMessage(const std::string& message) {
// The decoded message should be a JSON string.
std::optional<base::Value::Dict> message_value =
base::JSONReader::ReadDict(message);
if (!message_value) {
PA_LOG(ERROR) << "Unable to parse message as JSON:\n" << message;
return;
}
const std::string* type = message_value->FindString(kTypeKey);
if (!type) {
PA_LOG(ERROR) << "Missing '" << kTypeKey << "' key in message:\n "
<< message;
return;
}
// Remote status updates can be received out of the blue.
if (*type == kMessageTypeRemoteStatusUpdate) {
HandleRemoteStatusUpdateMessage(*message_value);
return;
}
// All other messages should only be received in response to a message that
// the messenger sent.
if (!pending_message_) {
PA_LOG(WARNING) << "Unexpected message received: " << message;
return;
}
std::string expected_type;
if (pending_message_->type == kMessageTypeUnlockRequest) {
expected_type = kMessageTypeUnlockResponse;
} else {
// (crbug.com/286944516): Unexpected path occurring.
PA_LOG(ERROR) << "Response received from unexpected message type: "
<< pending_message_->type;
DUMP_WILL_BE_NOTREACHED(); // There are no other message types
// that expect a response.
}
if (*type != expected_type) {
PA_LOG(ERROR) << "Unexpected '" << kTypeKey << "' value in message. "
<< "Expected '" << expected_type << "' but received '"
<< *type << "'.";
return;
}
if (*type == kMessageTypeUnlockResponse) {
HandleUnlockResponseMessage(*message_value);
} else {
NOTREACHED(); // There are no other message types that expect a response.
}
pending_message_.reset();
ProcessMessageQueue();
}
void MessengerImpl::OnSendMessageResult(bool success) {
if (!pending_message_) {
PA_LOG(ERROR) << "Unexpected message sent.";
return;
}
// In the common case, wait for a response from the remote device.
// Don't wait if the message could not be sent, as there won't ever be a
// response in that case. Likewise, don't wait for a response to local
// event messages, as there is no response for such messages.
if (success && pending_message_->type != kMessageTypeLocalEvent)
return;
// Notify observer of failure if sending the message fails.
// For local events, we don't expect a response, so on success, we
// notify observers right away.
if (pending_message_->type == kMessageTypeUnlockRequest) {
for (auto& observer : observers_)
observer.OnUnlockResponse(false);
} else if (pending_message_->type == kMessageTypeLocalEvent) {
for (auto& observer : observers_)
observer.OnUnlockEventSent(success);
} else {
PA_LOG(ERROR) << "Message of unknown type '" << pending_message_->type
<< "' sent.";
}
pending_message_.reset();
ProcessMessageQueue();
}
} // namespace proximity_auth
|