OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 "chromeos/components/tether/message_wrapper.h" |
| 6 |
| 7 #include "base/base64url.h" |
| 8 #include "base/json/json_reader.h" |
| 9 #include "base/json/json_writer.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/values.h" |
| 13 |
| 14 namespace chromeos { |
| 15 |
| 16 namespace tether { |
| 17 |
| 18 namespace { |
| 19 |
| 20 const char kJsonTypeKey[] = "type"; |
| 21 const char kJsonDataKey[] = "data"; |
| 22 |
| 23 std::unique_ptr<google::protobuf::MessageLite> DecodedMessageToProto( |
| 24 const MessageType& type, |
| 25 const std::string& decoded_message) { |
| 26 switch (type) { |
| 27 case MessageType::CONNECT_TETHERING_REQUEST: { |
| 28 std::unique_ptr<ConnectTetheringRequest> connect_request = |
| 29 base::MakeUnique<ConnectTetheringRequest>(); |
| 30 connect_request->ParseFromString(decoded_message); |
| 31 return connect_request; |
| 32 } |
| 33 case MessageType::CONNECT_TETHERING_RESPONSE: { |
| 34 std::unique_ptr<ConnectTetheringResponse> connect_response = |
| 35 base::MakeUnique<ConnectTetheringResponse>(); |
| 36 connect_response->ParseFromString(decoded_message); |
| 37 return connect_response; |
| 38 } |
| 39 case MessageType::DISCONNECT_TETHERING_REQUEST: { |
| 40 std::unique_ptr<DisconnectTetheringRequest> disconnect_request = |
| 41 base::MakeUnique<DisconnectTetheringRequest>(); |
| 42 disconnect_request->ParseFromString(decoded_message); |
| 43 return disconnect_request; |
| 44 } |
| 45 case MessageType::KEEP_ALIVE_TICKLE: { |
| 46 std::unique_ptr<KeepAliveTickle> keep_alive_tickle = |
| 47 base::MakeUnique<KeepAliveTickle>(); |
| 48 keep_alive_tickle->ParseFromString(decoded_message); |
| 49 return keep_alive_tickle; |
| 50 } |
| 51 case MessageType::TETHER_AVAILABILITY_REQUEST: { |
| 52 std::unique_ptr<TetherAvailabilityRequest> tether_request = |
| 53 base::MakeUnique<TetherAvailabilityRequest>(); |
| 54 tether_request->ParseFromString(decoded_message); |
| 55 return tether_request; |
| 56 } |
| 57 case MessageType::TETHER_AVAILABILITY_RESPONSE: { |
| 58 std::unique_ptr<TetherAvailabilityResponse> tether_response = |
| 59 base::MakeUnique<TetherAvailabilityResponse>(); |
| 60 tether_response->ParseFromString(decoded_message); |
| 61 return tether_response; |
| 62 } |
| 63 default: |
| 64 return nullptr; |
| 65 } |
| 66 } |
| 67 |
| 68 } // namespace |
| 69 |
| 70 // static |
| 71 std::unique_ptr<MessageWrapper> MessageWrapper::FromRawMessage( |
| 72 const std::string& message) { |
| 73 std::unique_ptr<base::Value> json_value = base::JSONReader::Read(message); |
| 74 if (!json_value) { |
| 75 return nullptr; |
| 76 } |
| 77 |
| 78 std::unique_ptr<base::DictionaryValue> json_dictionary = |
| 79 base::DictionaryValue::From(std::move(json_value)); |
| 80 if (!json_dictionary) { |
| 81 return nullptr; |
| 82 } |
| 83 |
| 84 int message_type; |
| 85 if (!json_dictionary->GetInteger(kJsonTypeKey, &message_type)) { |
| 86 return nullptr; |
| 87 } |
| 88 |
| 89 std::string encoded_message; |
| 90 if (!json_dictionary->GetString(kJsonDataKey, &encoded_message)) { |
| 91 return nullptr; |
| 92 } |
| 93 |
| 94 std::string decoded_message; |
| 95 if (!base::Base64UrlDecode(encoded_message, |
| 96 base::Base64UrlDecodePolicy::REQUIRE_PADDING, |
| 97 &decoded_message)) { |
| 98 return nullptr; |
| 99 } |
| 100 |
| 101 std::unique_ptr<google::protobuf::MessageLite> proto = DecodedMessageToProto( |
| 102 static_cast<MessageType>(message_type), decoded_message); |
| 103 if (!proto) { |
| 104 return nullptr; |
| 105 } |
| 106 |
| 107 return base::WrapUnique(new MessageWrapper( |
| 108 static_cast<MessageType>(message_type), std::move(proto))); |
| 109 } |
| 110 |
| 111 MessageWrapper::MessageWrapper(const ConnectTetheringRequest& request) |
| 112 : type_(MessageType::CONNECT_TETHERING_REQUEST), |
| 113 proto_(new ConnectTetheringRequest(request)) {} |
| 114 |
| 115 MessageWrapper::MessageWrapper(const ConnectTetheringResponse& response) |
| 116 : type_(MessageType::CONNECT_TETHERING_RESPONSE), |
| 117 proto_(new ConnectTetheringResponse(response)) {} |
| 118 |
| 119 MessageWrapper::MessageWrapper(const DisconnectTetheringRequest& request) |
| 120 : type_(MessageType::DISCONNECT_TETHERING_REQUEST), |
| 121 proto_(new DisconnectTetheringRequest(request)) {} |
| 122 |
| 123 MessageWrapper::MessageWrapper(const KeepAliveTickle& tickle) |
| 124 : type_(MessageType::KEEP_ALIVE_TICKLE), |
| 125 proto_(new KeepAliveTickle(tickle)) {} |
| 126 |
| 127 MessageWrapper::MessageWrapper(const TetherAvailabilityRequest& request) |
| 128 : type_(MessageType::TETHER_AVAILABILITY_REQUEST), |
| 129 proto_(new TetherAvailabilityRequest(request)) {} |
| 130 |
| 131 MessageWrapper::MessageWrapper(const TetherAvailabilityResponse& response) |
| 132 : type_(MessageType::TETHER_AVAILABILITY_RESPONSE), |
| 133 proto_(new TetherAvailabilityResponse(response)) {} |
| 134 |
| 135 MessageWrapper::MessageWrapper( |
| 136 const MessageType& type, |
| 137 std::shared_ptr<google::protobuf::MessageLite> proto) |
| 138 : type_(type), proto_(proto) {} |
| 139 |
| 140 MessageWrapper::~MessageWrapper() {} |
| 141 |
| 142 std::shared_ptr<google::protobuf::MessageLite> MessageWrapper::GetProto() |
| 143 const { |
| 144 return proto_; |
| 145 } |
| 146 |
| 147 MessageType MessageWrapper::GetMessageType() const { |
| 148 return type_; |
| 149 } |
| 150 |
| 151 std::string MessageWrapper::ToRawMessage() const { |
| 152 std::string encoded_message; |
| 153 base::Base64UrlEncode(proto_->SerializeAsString(), |
| 154 base::Base64UrlEncodePolicy::INCLUDE_PADDING, |
| 155 &encoded_message); |
| 156 |
| 157 base::DictionaryValue json_dictionary; |
| 158 json_dictionary.SetInteger(kJsonTypeKey, static_cast<int>(type_)); |
| 159 json_dictionary.SetString(kJsonDataKey, encoded_message); |
| 160 |
| 161 std::string raw_message; |
| 162 base::JSONWriter::Write(json_dictionary, &raw_message); |
| 163 return raw_message; |
| 164 } |
| 165 |
| 166 } // namespace tether |
| 167 |
| 168 } // namespace chromeos |
OLD | NEW |