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

Side by Side Diff: extensions/browser/api/cast_channel/cast_message_util.cc

Issue 2926313002: Revert of [cast_channel] Move cast_channel related files from //extensions to //components (Closed)
Patch Set: Created 3 years, 6 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "extensions/browser/api/cast_channel/cast_message_util.h" 5 #include "extensions/browser/api/cast_channel/cast_message_util.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_number_conversions.h"
11 #include "base/values.h" 11 #include "base/values.h"
12 #include "components/cast_channel/proto/cast_channel.pb.h" 12 #include "extensions/browser/api/cast_channel/cast_auth_util.h"
13 #include "extensions/common/api/cast_channel.h" 13 #include "extensions/common/api/cast_channel.h"
14 #include "extensions/common/api/cast_channel/cast_channel.pb.h"
15
16 namespace {
17 static const char kAuthNamespace[] =
18 "urn:x-cast:com.google.cast.tp.deviceauth";
19 // Sender and receiver IDs to use for platform messages.
20 static const char kPlatformSenderId[] = "sender-0";
21 static const char kPlatformReceiverId[] = "receiver-0";
22 } // namespace
14 23
15 namespace extensions { 24 namespace extensions {
16 namespace api { 25 namespace api {
17 namespace cast_channel { 26 namespace cast_channel {
18 27
19 bool MessageInfoToCastMessage(const MessageInfo& message, 28 bool MessageInfoToCastMessage(const MessageInfo& message,
20 ::cast_channel::CastMessage* message_proto) { 29 CastMessage* message_proto) {
21 DCHECK(message_proto); 30 DCHECK(message_proto);
22 if (!message.data) 31 if (!message.data)
23 return false; 32 return false;
24 33
25 message_proto->set_protocol_version( 34 message_proto->set_protocol_version(CastMessage_ProtocolVersion_CASTV2_1_0);
26 ::cast_channel::CastMessage_ProtocolVersion_CASTV2_1_0);
27 message_proto->set_source_id(message.source_id); 35 message_proto->set_source_id(message.source_id);
28 message_proto->set_destination_id(message.destination_id); 36 message_proto->set_destination_id(message.destination_id);
29 message_proto->set_namespace_(message.namespace_); 37 message_proto->set_namespace_(message.namespace_);
30 // Determine the type of the base::Value and set the message payload 38 // Determine the type of the base::Value and set the message payload
31 // appropriately. 39 // appropriately.
32 std::string data; 40 std::string data;
33 switch (message.data->GetType()) { 41 switch (message.data->GetType()) {
34 // JS string 42 // JS string
35 case base::Value::Type::STRING: 43 case base::Value::Type::STRING:
36 if (message.data->GetAsString(&data)) { 44 if (message.data->GetAsString(&data)) {
37 message_proto->set_payload_type( 45 message_proto->set_payload_type(CastMessage_PayloadType_STRING);
38 ::cast_channel::CastMessage_PayloadType_STRING);
39 message_proto->set_payload_utf8(data); 46 message_proto->set_payload_utf8(data);
40 } 47 }
41 break; 48 break;
42 // JS ArrayBuffer 49 // JS ArrayBuffer
43 case base::Value::Type::BINARY: 50 case base::Value::Type::BINARY:
44 message_proto->set_payload_type( 51 message_proto->set_payload_type(CastMessage_PayloadType_BINARY);
45 ::cast_channel::CastMessage_PayloadType_BINARY);
46 message_proto->set_payload_binary(message.data->GetBlob().data(), 52 message_proto->set_payload_binary(message.data->GetBlob().data(),
47 message.data->GetBlob().size()); 53 message.data->GetBlob().size());
48 break; 54 break;
49 default: 55 default:
50 // Unknown value type. message_proto will remain uninitialized because 56 // Unknown value type. message_proto will remain uninitialized because
51 // payload_type is unset. 57 // payload_type is unset.
52 break; 58 break;
53 } 59 }
54 return message_proto->IsInitialized(); 60 return message_proto->IsInitialized();
55 } 61 }
56 62
57 bool CastMessageToMessageInfo(const ::cast_channel::CastMessage& message_proto, 63 bool IsCastMessageValid(const CastMessage& message_proto) {
64 if (message_proto.namespace_().empty() || message_proto.source_id().empty() ||
65 message_proto.destination_id().empty()) {
66 return false;
67 }
68 return (message_proto.payload_type() == CastMessage_PayloadType_STRING &&
69 message_proto.has_payload_utf8()) ||
70 (message_proto.payload_type() == CastMessage_PayloadType_BINARY &&
71 message_proto.has_payload_binary());
72 }
73
74 bool CastMessageToMessageInfo(const CastMessage& message_proto,
58 MessageInfo* message) { 75 MessageInfo* message) {
59 DCHECK(message); 76 DCHECK(message);
60 message->source_id = message_proto.source_id(); 77 message->source_id = message_proto.source_id();
61 message->destination_id = message_proto.destination_id(); 78 message->destination_id = message_proto.destination_id();
62 message->namespace_ = message_proto.namespace_(); 79 message->namespace_ = message_proto.namespace_();
63 // Determine the type of the payload and fill base::Value appropriately. 80 // Determine the type of the payload and fill base::Value appropriately.
64 std::unique_ptr<base::Value> value; 81 std::unique_ptr<base::Value> value;
65 switch (message_proto.payload_type()) { 82 switch (message_proto.payload_type()) {
66 case ::cast_channel::CastMessage_PayloadType_STRING: 83 case CastMessage_PayloadType_STRING:
67 if (message_proto.has_payload_utf8()) 84 if (message_proto.has_payload_utf8())
68 value.reset(new base::Value(message_proto.payload_utf8())); 85 value.reset(new base::Value(message_proto.payload_utf8()));
69 break; 86 break;
70 case ::cast_channel::CastMessage_PayloadType_BINARY: 87 case CastMessage_PayloadType_BINARY:
71 if (message_proto.has_payload_binary()) 88 if (message_proto.has_payload_binary())
72 value = base::Value::CreateWithCopiedBuffer( 89 value = base::Value::CreateWithCopiedBuffer(
73 message_proto.payload_binary().data(), 90 message_proto.payload_binary().data(),
74 message_proto.payload_binary().size()); 91 message_proto.payload_binary().size());
75 break; 92 break;
76 default: 93 default:
77 // Unknown payload type. value will remain unset. 94 // Unknown payload type. value will remain unset.
78 break; 95 break;
79 } 96 }
80 if (value.get()) { 97 if (value.get()) {
81 DCHECK(!message->data.get()); 98 DCHECK(!message->data.get());
82 message->data = std::move(value); 99 message->data = std::move(value);
83 return true; 100 return true;
84 } else { 101 } else {
85 return false; 102 return false;
86 } 103 }
87 } 104 }
88 105
106 std::string CastMessageToString(const CastMessage& message_proto) {
107 std::string out("{");
108 out += "namespace = " + message_proto.namespace_();
109 out += ", sourceId = " + message_proto.source_id();
110 out += ", destId = " + message_proto.destination_id();
111 out += ", type = " + base::IntToString(message_proto.payload_type());
112 out += ", str = \"" + message_proto.payload_utf8() + "\"}";
113 return out;
114 }
115
116 std::string AuthMessageToString(const DeviceAuthMessage& message) {
117 std::string out("{");
118 if (message.has_challenge()) {
119 out += "challenge: {}, ";
120 }
121 if (message.has_response()) {
122 out += "response: {signature: (";
123 out += base::SizeTToString(message.response().signature().length());
124 out += " bytes), certificate: (";
125 out += base::SizeTToString(
126 message.response().client_auth_certificate().length());
127 out += " bytes)}";
128 }
129 if (message.has_error()) {
130 out += ", error: {";
131 out += base::IntToString(message.error().error_type());
132 out += "}";
133 }
134 out += "}";
135 return out;
136 }
137
138 void CreateAuthChallengeMessage(CastMessage* message_proto,
139 const AuthContext& auth_context) {
140 CHECK(message_proto);
141 DeviceAuthMessage auth_message;
142 auth_message.mutable_challenge()->set_sender_nonce(auth_context.nonce());
143 std::string auth_message_string;
144 auth_message.SerializeToString(&auth_message_string);
145
146 message_proto->set_protocol_version(CastMessage_ProtocolVersion_CASTV2_1_0);
147 message_proto->set_source_id(kPlatformSenderId);
148 message_proto->set_destination_id(kPlatformReceiverId);
149 message_proto->set_namespace_(kAuthNamespace);
150 message_proto->set_payload_type(CastMessage_PayloadType_BINARY);
151 message_proto->set_payload_binary(auth_message_string);
152 }
153
154 bool IsAuthMessage(const CastMessage& message) {
155 return message.namespace_() == kAuthNamespace;
156 }
157
89 } // namespace cast_channel 158 } // namespace cast_channel
90 } // namespace api 159 } // namespace api
91 } // namespace extensions 160 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/api/cast_channel/cast_message_util.h ('k') | extensions/browser/api/cast_channel/cast_socket.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698