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

Side by Side Diff: chromeos/components/tether/message_wrapper.cc

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

Powered by Google App Engine
This is Rietveld 408576698