OLD | NEW |
| (Empty) |
1 // Copyright 2013 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 "chrome/browser/extensions/api/cast_channel/cast_message_util.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "base/strings/string_number_conversions.h" | |
10 #include "base/values.h" | |
11 #include "chrome/browser/extensions/api/cast_channel/cast_channel.pb.h" | |
12 #include "chrome/common/extensions/api/cast_channel.h" | |
13 | |
14 namespace { | |
15 static const char kAuthNamespace[] = | |
16 "urn:x-cast:com.google.cast.tp.deviceauth"; | |
17 // Sender and receiver IDs to use for platform messages. | |
18 static const char kPlatformSenderId[] = "sender-0"; | |
19 static const char kPlatformReceiverId[] = "receiver-0"; | |
20 } // namespace | |
21 | |
22 namespace extensions { | |
23 namespace api { | |
24 namespace cast_channel { | |
25 | |
26 bool MessageInfoToCastMessage(const MessageInfo& message, | |
27 CastMessage* message_proto) { | |
28 DCHECK(message_proto); | |
29 if (!message.data) | |
30 return false; | |
31 | |
32 message_proto->set_protocol_version(CastMessage_ProtocolVersion_CASTV2_1_0); | |
33 message_proto->set_source_id(message.source_id); | |
34 message_proto->set_destination_id(message.destination_id); | |
35 message_proto->set_namespace_(message.namespace_); | |
36 // Determine the type of the base::Value and set the message payload | |
37 // appropriately. | |
38 std::string data; | |
39 base::BinaryValue* real_value; | |
40 switch (message.data->GetType()) { | |
41 // JS string | |
42 case base::Value::TYPE_STRING: | |
43 if (message.data->GetAsString(&data)) { | |
44 message_proto->set_payload_type(CastMessage_PayloadType_STRING); | |
45 message_proto->set_payload_utf8(data); | |
46 } | |
47 break; | |
48 // JS ArrayBuffer | |
49 case base::Value::TYPE_BINARY: | |
50 real_value = static_cast<base::BinaryValue*>(message.data.get()); | |
51 if (real_value->GetBuffer()) { | |
52 message_proto->set_payload_type(CastMessage_PayloadType_BINARY); | |
53 message_proto->set_payload_binary(real_value->GetBuffer(), | |
54 real_value->GetSize()); | |
55 } | |
56 break; | |
57 default: | |
58 // Unknown value type. message_proto will remain uninitialized because | |
59 // payload_type is unset. | |
60 break; | |
61 } | |
62 return message_proto->IsInitialized(); | |
63 } | |
64 | |
65 bool CastMessageToMessageInfo(const CastMessage& message_proto, | |
66 MessageInfo* message) { | |
67 DCHECK(message); | |
68 message->source_id = message_proto.source_id(); | |
69 message->destination_id = message_proto.destination_id(); | |
70 message->namespace_ = message_proto.namespace_(); | |
71 // Determine the type of the payload and fill base::Value appropriately. | |
72 scoped_ptr<base::Value> value; | |
73 switch (message_proto.payload_type()) { | |
74 case CastMessage_PayloadType_STRING: | |
75 if (message_proto.has_payload_utf8()) | |
76 value.reset(new base::StringValue(message_proto.payload_utf8())); | |
77 break; | |
78 case CastMessage_PayloadType_BINARY: | |
79 if (message_proto.has_payload_binary()) | |
80 value.reset(base::BinaryValue::CreateWithCopiedBuffer( | |
81 message_proto.payload_binary().data(), | |
82 message_proto.payload_binary().size())); | |
83 break; | |
84 default: | |
85 // Unknown payload type. value will remain unset. | |
86 break; | |
87 } | |
88 if (value.get()) { | |
89 DCHECK(!message->data.get()); | |
90 message->data.reset(value.release()); | |
91 return true; | |
92 } else { | |
93 return false; | |
94 } | |
95 } | |
96 | |
97 std::string CastMessageToString(const CastMessage& message_proto) { | |
98 std::string out("{"); | |
99 out += "namespace = " + message_proto.namespace_(); | |
100 out += ", sourceId = " + message_proto.source_id(); | |
101 out += ", destId = " + message_proto.destination_id(); | |
102 out += ", type = " + base::IntToString(message_proto.payload_type()); | |
103 out += ", str = \"" + message_proto.payload_utf8() + "\"}"; | |
104 return out; | |
105 } | |
106 | |
107 std::string AuthMessageToString(const DeviceAuthMessage& message) { | |
108 std::string out("{"); | |
109 if (message.has_challenge()) { | |
110 out += "challenge: {}, "; | |
111 } | |
112 if (message.has_response()) { | |
113 out += "response: {signature: ("; | |
114 out += base::UintToString(message.response().signature().length()); | |
115 out += " bytes), certificate: ("; | |
116 out += base::UintToString( | |
117 message.response().client_auth_certificate().length()); | |
118 out += " bytes)}"; | |
119 } | |
120 if (message.has_error()) { | |
121 out += ", error: {"; | |
122 out += base::IntToString(message.error().error_type()); | |
123 out += "}"; | |
124 } | |
125 out += "}"; | |
126 return out; | |
127 } | |
128 | |
129 void CreateAuthChallengeMessage(CastMessage* message_proto) { | |
130 CHECK(message_proto); | |
131 DeviceAuthMessage auth_message; | |
132 auth_message.mutable_challenge(); | |
133 std::string auth_message_string; | |
134 auth_message.SerializeToString(&auth_message_string); | |
135 | |
136 message_proto->set_protocol_version(CastMessage_ProtocolVersion_CASTV2_1_0); | |
137 message_proto->set_source_id(kPlatformSenderId); | |
138 message_proto->set_destination_id(kPlatformReceiverId); | |
139 message_proto->set_namespace_(kAuthNamespace); | |
140 message_proto->set_payload_type(CastMessage_PayloadType_BINARY); | |
141 message_proto->set_payload_binary(auth_message_string); | |
142 } | |
143 | |
144 bool IsAuthMessage(const CastMessage& message) { | |
145 return message.namespace_() == kAuthNamespace; | |
146 } | |
147 | |
148 } // namespace cast_channel | |
149 } // namespace api | |
150 } // namespace extensions | |
OLD | NEW |