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
|
// Copyright 2017 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/tether/message_wrapper.h"
#include <memory>
#include "base/base64url.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/memory/ptr_util.h"
#include "base/values.h"
namespace ash {
namespace tether {
namespace {
const char kJsonTypeKey[] = "type";
const char kJsonDataKey[] = "data";
std::unique_ptr<google::protobuf::MessageLite> DecodedMessageToProto(
const MessageType& type,
const std::string& decoded_message) {
switch (type) {
case MessageType::CONNECT_TETHERING_REQUEST: {
std::unique_ptr<ConnectTetheringRequest> connect_request =
std::make_unique<ConnectTetheringRequest>();
connect_request->ParseFromString(decoded_message);
return connect_request;
}
case MessageType::CONNECT_TETHERING_RESPONSE: {
std::unique_ptr<ConnectTetheringResponse> connect_response =
std::make_unique<ConnectTetheringResponse>();
connect_response->ParseFromString(decoded_message);
return connect_response;
}
case MessageType::DISCONNECT_TETHERING_REQUEST: {
std::unique_ptr<DisconnectTetheringRequest> disconnect_request =
std::make_unique<DisconnectTetheringRequest>();
disconnect_request->ParseFromString(decoded_message);
return disconnect_request;
}
case MessageType::KEEP_ALIVE_TICKLE: {
std::unique_ptr<KeepAliveTickle> keep_alive_tickle =
std::make_unique<KeepAliveTickle>();
keep_alive_tickle->ParseFromString(decoded_message);
return keep_alive_tickle;
}
case MessageType::KEEP_ALIVE_TICKLE_RESPONSE: {
std::unique_ptr<KeepAliveTickleResponse> keep_alive_tickle_response =
std::make_unique<KeepAliveTickleResponse>();
keep_alive_tickle_response->ParseFromString(decoded_message);
return keep_alive_tickle_response;
}
case MessageType::TETHER_AVAILABILITY_REQUEST: {
std::unique_ptr<TetherAvailabilityRequest> tether_request =
std::make_unique<TetherAvailabilityRequest>();
tether_request->ParseFromString(decoded_message);
return tether_request;
}
case MessageType::TETHER_AVAILABILITY_RESPONSE: {
std::unique_ptr<TetherAvailabilityResponse> tether_response =
std::make_unique<TetherAvailabilityResponse>();
tether_response->ParseFromString(decoded_message);
return tether_response;
}
default:
return nullptr;
}
}
} // namespace
// static
std::unique_ptr<MessageWrapper> MessageWrapper::FromRawMessage(
const std::string& message) {
std::optional<base::Value> json_value = base::JSONReader::Read(message);
if (!json_value) {
return nullptr;
}
const base::Value::Dict* json_dictionary = json_value->GetIfDict();
if (!json_dictionary) {
return nullptr;
}
std::optional<int> message_type = json_dictionary->FindInt(kJsonTypeKey);
if (!message_type)
return nullptr;
const std::string* encoded_message =
json_dictionary->FindString(kJsonDataKey);
if (!encoded_message)
return nullptr;
std::string decoded_message;
if (!base::Base64UrlDecode(*encoded_message,
base::Base64UrlDecodePolicy::REQUIRE_PADDING,
&decoded_message)) {
return nullptr;
}
std::unique_ptr<google::protobuf::MessageLite> proto = DecodedMessageToProto(
static_cast<MessageType>(*message_type), decoded_message);
if (!proto) {
return nullptr;
}
return base::WrapUnique(new MessageWrapper(
static_cast<MessageType>(*message_type), std::move(proto)));
}
MessageWrapper::MessageWrapper(const ConnectTetheringRequest& request)
: type_(MessageType::CONNECT_TETHERING_REQUEST),
proto_(std::make_unique<ConnectTetheringRequest>(request)) {}
MessageWrapper::MessageWrapper(const ConnectTetheringResponse& response)
: type_(MessageType::CONNECT_TETHERING_RESPONSE),
proto_(std::make_unique<ConnectTetheringResponse>(response)) {}
MessageWrapper::MessageWrapper(const DisconnectTetheringRequest& request)
: type_(MessageType::DISCONNECT_TETHERING_REQUEST),
proto_(std::make_unique<DisconnectTetheringRequest>(request)) {}
MessageWrapper::MessageWrapper(const KeepAliveTickle& tickle)
: type_(MessageType::KEEP_ALIVE_TICKLE),
proto_(std::make_unique<KeepAliveTickle>(tickle)) {}
MessageWrapper::MessageWrapper(const KeepAliveTickleResponse& response)
: type_(MessageType::KEEP_ALIVE_TICKLE_RESPONSE),
proto_(std::make_unique<KeepAliveTickleResponse>(response)) {}
MessageWrapper::MessageWrapper(const TetherAvailabilityRequest& request)
: type_(MessageType::TETHER_AVAILABILITY_REQUEST),
proto_(std::make_unique<TetherAvailabilityRequest>(request)) {}
MessageWrapper::MessageWrapper(const TetherAvailabilityResponse& response)
: type_(MessageType::TETHER_AVAILABILITY_RESPONSE),
proto_(std::make_unique<TetherAvailabilityResponse>(response)) {}
MessageWrapper::MessageWrapper(
const MessageType& type,
std::unique_ptr<google::protobuf::MessageLite> proto)
: type_(type), proto_(std::move(proto)) {}
MessageWrapper::~MessageWrapper() = default;
google::protobuf::MessageLite* MessageWrapper::GetProto() const {
return proto_.get();
}
MessageType MessageWrapper::GetMessageType() const {
return type_;
}
std::string MessageWrapper::ToRawMessage() const {
std::string encoded_message;
base::Base64UrlEncode(proto_->SerializeAsString(),
base::Base64UrlEncodePolicy::INCLUDE_PADDING,
&encoded_message);
base::Value::Dict json_dictionary;
json_dictionary.Set(kJsonTypeKey, static_cast<int>(type_));
json_dictionary.Set(kJsonDataKey, encoded_message);
std::string raw_message;
base::JSONWriter::Write(json_dictionary, &raw_message);
return raw_message;
}
} // namespace tether
} // namespace ash
|