OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/extensions/api/cast_channel/cast_message_util.h" | 5 #include "chrome/browser/extensions/api/cast_channel/cast_message_util.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
11 #include "chrome/browser/extensions/api/cast_channel/cast_channel.pb.h" | 11 #include "chrome/browser/extensions/api/cast_channel/cast_channel.pb.h" |
12 #include "chrome/common/extensions/api/cast_channel.h" | 12 #include "chrome/common/extensions/api/cast_channel.h" |
13 | 13 |
14 namespace { | 14 namespace { |
15 static const char kAuthNamespace[] = | 15 static const char kAuthNamespace[] = |
16 "urn:x-cast:com.google.cast.tp.deviceauth"; | 16 "urn:x-cast:com.google.cast.tp.deviceauth"; |
17 // Sender and receiver IDs to use for platform messages. | 17 // Sender and receiver IDs to use for platform messages. |
18 static const char kPlatformSenderId[] = "sender-0"; | 18 static const char kPlatformSenderId[] = "sender-0"; |
19 static const char kPlatformReceiverId[] = "receiver-0"; | 19 static const char kPlatformReceiverId[] = "receiver-0"; |
20 } // namespace | 20 } // namespace |
21 | 21 |
22 namespace extensions { | 22 namespace extensions { |
23 namespace api { | 23 namespace api { |
24 namespace cast_channel { | 24 namespace cast_channel { |
25 | 25 |
26 bool MessageInfoToCastMessage(const MessageInfo& message, | 26 bool MessageInfoToCastMessage(const MessageInfo& message, |
27 CastMessage* message_proto) { | 27 CastMessage* message_proto) { |
28 DCHECK(message_proto); | 28 DCHECK(message_proto); |
| 29 if (!message.data) |
| 30 return false; |
| 31 |
29 message_proto->set_protocol_version(CastMessage_ProtocolVersion_CASTV2_1_0); | 32 message_proto->set_protocol_version(CastMessage_ProtocolVersion_CASTV2_1_0); |
30 message_proto->set_source_id(message.source_id); | 33 message_proto->set_source_id(message.source_id); |
31 message_proto->set_destination_id(message.destination_id); | 34 message_proto->set_destination_id(message.destination_id); |
32 message_proto->set_namespace_(message.namespace_); | 35 message_proto->set_namespace_(message.namespace_); |
33 // Determine the type of the base::Value and set the message payload | 36 // Determine the type of the base::Value and set the message payload |
34 // appropriately. | 37 // appropriately. |
35 std::string data; | 38 std::string data; |
36 base::BinaryValue* real_value; | 39 base::BinaryValue* real_value; |
37 switch (message.data->GetType()) { | 40 switch (message.data->GetType()) { |
38 // JS string | 41 // JS string |
39 case base::Value::TYPE_STRING: | 42 case base::Value::TYPE_STRING: |
40 if (message.data->GetAsString(&data)) { | 43 if (message.data->GetAsString(&data)) { |
41 message_proto->set_payload_type(CastMessage_PayloadType_STRING); | 44 message_proto->set_payload_type(CastMessage_PayloadType_STRING); |
42 message_proto->set_payload_utf8(data); | 45 message_proto->set_payload_utf8(data); |
43 } | 46 } |
44 break; | 47 break; |
45 // JS ArrayBuffer | 48 // JS ArrayBuffer |
46 case base::Value::TYPE_BINARY: | 49 case base::Value::TYPE_BINARY: |
47 real_value = static_cast<base::BinaryValue*>(message.data.get()); | 50 real_value = static_cast<base::BinaryValue*>(message.data.get()); |
48 if (real_value->GetBuffer()) { | 51 if (real_value->GetBuffer()) { |
49 message_proto->set_payload_type(CastMessage_PayloadType_BINARY); | 52 message_proto->set_payload_type(CastMessage_PayloadType_BINARY); |
50 message_proto->set_payload_binary(real_value->GetBuffer(), | 53 message_proto->set_payload_binary(real_value->GetBuffer(), |
51 real_value->GetSize()); | 54 real_value->GetSize()); |
52 } | 55 } |
53 break; | 56 break; |
54 default: | 57 default: |
55 // Unknown value type. message_proto will remain uninitialized because | 58 // Unknown value type. message_proto will remain uninitialized because |
56 // payload_type is unset. | 59 // payload_type is unset. |
57 break; | 60 break; |
58 } | 61 } |
59 return message_proto->IsInitialized(); | 62 return message_proto->IsInitialized(); |
60 } | 63 } |
61 | 64 |
62 bool CastMessageToMessageInfo(const CastMessage& message_proto, | 65 bool CastMessageToMessageInfo(const CastMessage& message_proto, |
63 MessageInfo* message) { | 66 MessageInfo* message) { |
64 DCHECK(message); | 67 DCHECK(message); |
65 message->source_id = message_proto.source_id(); | 68 message->source_id = message_proto.source_id(); |
66 message->destination_id = message_proto.destination_id(); | 69 message->destination_id = message_proto.destination_id(); |
67 message->namespace_ = message_proto.namespace_(); | 70 message->namespace_ = message_proto.namespace_(); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 message_proto->set_payload_binary(auth_message_string); | 140 message_proto->set_payload_binary(auth_message_string); |
138 } | 141 } |
139 | 142 |
140 bool IsAuthMessage(const CastMessage& message) { | 143 bool IsAuthMessage(const CastMessage& message) { |
141 return message.namespace_() == kAuthNamespace; | 144 return message.namespace_() == kAuthNamespace; |
142 } | 145 } |
143 | 146 |
144 } // namespace cast_channel | 147 } // namespace cast_channel |
145 } // namespace api | 148 } // namespace api |
146 } // namespace extensions | 149 } // namespace extensions |
OLD | NEW |