| OLD | NEW |
| 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 "extensions/browser/api/cast_channel/cast_auth_util.h" | 12 #include "components/cast_channel/proto/cast_channel.pb.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 | |
| 23 | 14 |
| 24 namespace extensions { | 15 namespace extensions { |
| 25 namespace api { | 16 namespace api { |
| 26 namespace cast_channel { | 17 namespace cast_channel { |
| 27 | 18 |
| 28 bool MessageInfoToCastMessage(const MessageInfo& message, | 19 bool MessageInfoToCastMessage(const MessageInfo& message, |
| 29 CastMessage* message_proto) { | 20 ::cast_channel::CastMessage* message_proto) { |
| 30 DCHECK(message_proto); | 21 DCHECK(message_proto); |
| 31 if (!message.data) | 22 if (!message.data) |
| 32 return false; | 23 return false; |
| 33 | 24 |
| 34 message_proto->set_protocol_version(CastMessage_ProtocolVersion_CASTV2_1_0); | 25 message_proto->set_protocol_version( |
| 26 ::cast_channel::CastMessage_ProtocolVersion_CASTV2_1_0); |
| 35 message_proto->set_source_id(message.source_id); | 27 message_proto->set_source_id(message.source_id); |
| 36 message_proto->set_destination_id(message.destination_id); | 28 message_proto->set_destination_id(message.destination_id); |
| 37 message_proto->set_namespace_(message.namespace_); | 29 message_proto->set_namespace_(message.namespace_); |
| 38 // Determine the type of the base::Value and set the message payload | 30 // Determine the type of the base::Value and set the message payload |
| 39 // appropriately. | 31 // appropriately. |
| 40 std::string data; | 32 std::string data; |
| 41 switch (message.data->GetType()) { | 33 switch (message.data->GetType()) { |
| 42 // JS string | 34 // JS string |
| 43 case base::Value::Type::STRING: | 35 case base::Value::Type::STRING: |
| 44 if (message.data->GetAsString(&data)) { | 36 if (message.data->GetAsString(&data)) { |
| 45 message_proto->set_payload_type(CastMessage_PayloadType_STRING); | 37 message_proto->set_payload_type( |
| 38 ::cast_channel::CastMessage_PayloadType_STRING); |
| 46 message_proto->set_payload_utf8(data); | 39 message_proto->set_payload_utf8(data); |
| 47 } | 40 } |
| 48 break; | 41 break; |
| 49 // JS ArrayBuffer | 42 // JS ArrayBuffer |
| 50 case base::Value::Type::BINARY: | 43 case base::Value::Type::BINARY: |
| 51 message_proto->set_payload_type(CastMessage_PayloadType_BINARY); | 44 message_proto->set_payload_type( |
| 45 ::cast_channel::CastMessage_PayloadType_BINARY); |
| 52 message_proto->set_payload_binary(message.data->GetBlob().data(), | 46 message_proto->set_payload_binary(message.data->GetBlob().data(), |
| 53 message.data->GetBlob().size()); | 47 message.data->GetBlob().size()); |
| 54 break; | 48 break; |
| 55 default: | 49 default: |
| 56 // Unknown value type. message_proto will remain uninitialized because | 50 // Unknown value type. message_proto will remain uninitialized because |
| 57 // payload_type is unset. | 51 // payload_type is unset. |
| 58 break; | 52 break; |
| 59 } | 53 } |
| 60 return message_proto->IsInitialized(); | 54 return message_proto->IsInitialized(); |
| 61 } | 55 } |
| 62 | 56 |
| 63 bool IsCastMessageValid(const CastMessage& message_proto) { | 57 bool CastMessageToMessageInfo(const ::cast_channel::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, | |
| 75 MessageInfo* message) { | 58 MessageInfo* message) { |
| 76 DCHECK(message); | 59 DCHECK(message); |
| 77 message->source_id = message_proto.source_id(); | 60 message->source_id = message_proto.source_id(); |
| 78 message->destination_id = message_proto.destination_id(); | 61 message->destination_id = message_proto.destination_id(); |
| 79 message->namespace_ = message_proto.namespace_(); | 62 message->namespace_ = message_proto.namespace_(); |
| 80 // Determine the type of the payload and fill base::Value appropriately. | 63 // Determine the type of the payload and fill base::Value appropriately. |
| 81 std::unique_ptr<base::Value> value; | 64 std::unique_ptr<base::Value> value; |
| 82 switch (message_proto.payload_type()) { | 65 switch (message_proto.payload_type()) { |
| 83 case CastMessage_PayloadType_STRING: | 66 case ::cast_channel::CastMessage_PayloadType_STRING: |
| 84 if (message_proto.has_payload_utf8()) | 67 if (message_proto.has_payload_utf8()) |
| 85 value.reset(new base::Value(message_proto.payload_utf8())); | 68 value.reset(new base::Value(message_proto.payload_utf8())); |
| 86 break; | 69 break; |
| 87 case CastMessage_PayloadType_BINARY: | 70 case ::cast_channel::CastMessage_PayloadType_BINARY: |
| 88 if (message_proto.has_payload_binary()) | 71 if (message_proto.has_payload_binary()) |
| 89 value = base::Value::CreateWithCopiedBuffer( | 72 value = base::Value::CreateWithCopiedBuffer( |
| 90 message_proto.payload_binary().data(), | 73 message_proto.payload_binary().data(), |
| 91 message_proto.payload_binary().size()); | 74 message_proto.payload_binary().size()); |
| 92 break; | 75 break; |
| 93 default: | 76 default: |
| 94 // Unknown payload type. value will remain unset. | 77 // Unknown payload type. value will remain unset. |
| 95 break; | 78 break; |
| 96 } | 79 } |
| 97 if (value.get()) { | 80 if (value.get()) { |
| 98 DCHECK(!message->data.get()); | 81 DCHECK(!message->data.get()); |
| 99 message->data = std::move(value); | 82 message->data = std::move(value); |
| 100 return true; | 83 return true; |
| 101 } else { | 84 } else { |
| 102 return false; | 85 return false; |
| 103 } | 86 } |
| 104 } | 87 } |
| 105 | 88 |
| 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 | |
| 158 } // namespace cast_channel | 89 } // namespace cast_channel |
| 159 } // namespace api | 90 } // namespace api |
| 160 } // namespace extensions | 91 } // namespace extensions |
| OLD | NEW |