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

Side by Side Diff: components/cast_channel/cast_message_util.cc

Issue 2913033003: [cast_channel] Move cast_channel related files from //extensions to //components (Closed)
Patch Set: fix buildbot compile errors 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
« no previous file with comments | « components/cast_channel/cast_message_util.h ('k') | components/cast_channel/cast_socket.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "components/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/cast_auth_util.h"
13 #include "extensions/common/api/cast_channel.h" 13 #include "components/cast_channel/proto/cast_channel.pb.h"
14 #include "extensions/common/api/cast_channel/cast_channel.pb.h"
15 14
16 namespace { 15 namespace {
17 static const char kAuthNamespace[] = 16 static const char kAuthNamespace[] = "urn:x-cast:com.google.cast.tp.deviceauth";
18 "urn:x-cast:com.google.cast.tp.deviceauth";
19 // Sender and receiver IDs to use for platform messages. 17 // Sender and receiver IDs to use for platform messages.
20 static const char kPlatformSenderId[] = "sender-0"; 18 static const char kPlatformSenderId[] = "sender-0";
21 static const char kPlatformReceiverId[] = "receiver-0"; 19 static const char kPlatformReceiverId[] = "receiver-0";
22 } // namespace 20 } // namespace
23 21
24 namespace extensions {
25 namespace api {
26 namespace cast_channel { 22 namespace cast_channel {
27 23
28 bool MessageInfoToCastMessage(const MessageInfo& message,
29 CastMessage* message_proto) {
30 DCHECK(message_proto);
31 if (!message.data)
32 return false;
33
34 message_proto->set_protocol_version(CastMessage_ProtocolVersion_CASTV2_1_0);
35 message_proto->set_source_id(message.source_id);
36 message_proto->set_destination_id(message.destination_id);
37 message_proto->set_namespace_(message.namespace_);
38 // Determine the type of the base::Value and set the message payload
39 // appropriately.
40 std::string data;
41 switch (message.data->GetType()) {
42 // JS string
43 case base::Value::Type::STRING:
44 if (message.data->GetAsString(&data)) {
45 message_proto->set_payload_type(CastMessage_PayloadType_STRING);
46 message_proto->set_payload_utf8(data);
47 }
48 break;
49 // JS ArrayBuffer
50 case base::Value::Type::BINARY:
51 message_proto->set_payload_type(CastMessage_PayloadType_BINARY);
52 message_proto->set_payload_binary(message.data->GetBlob().data(),
53 message.data->GetBlob().size());
54 break;
55 default:
56 // Unknown value type. message_proto will remain uninitialized because
57 // payload_type is unset.
58 break;
59 }
60 return message_proto->IsInitialized();
61 }
62
63 bool IsCastMessageValid(const CastMessage& message_proto) { 24 bool IsCastMessageValid(const CastMessage& message_proto) {
64 if (message_proto.namespace_().empty() || message_proto.source_id().empty() || 25 if (message_proto.namespace_().empty() || message_proto.source_id().empty() ||
65 message_proto.destination_id().empty()) { 26 message_proto.destination_id().empty()) {
66 return false; 27 return false;
67 } 28 }
68 return (message_proto.payload_type() == CastMessage_PayloadType_STRING && 29 return (message_proto.payload_type() == CastMessage_PayloadType_STRING &&
69 message_proto.has_payload_utf8()) || 30 message_proto.has_payload_utf8()) ||
70 (message_proto.payload_type() == CastMessage_PayloadType_BINARY && 31 (message_proto.payload_type() == CastMessage_PayloadType_BINARY &&
71 message_proto.has_payload_binary()); 32 message_proto.has_payload_binary());
72 } 33 }
73 34
74 bool CastMessageToMessageInfo(const CastMessage& message_proto,
75 MessageInfo* message) {
76 DCHECK(message);
77 message->source_id = message_proto.source_id();
78 message->destination_id = message_proto.destination_id();
79 message->namespace_ = message_proto.namespace_();
80 // Determine the type of the payload and fill base::Value appropriately.
81 std::unique_ptr<base::Value> value;
82 switch (message_proto.payload_type()) {
83 case CastMessage_PayloadType_STRING:
84 if (message_proto.has_payload_utf8())
85 value.reset(new base::Value(message_proto.payload_utf8()));
86 break;
87 case CastMessage_PayloadType_BINARY:
88 if (message_proto.has_payload_binary())
89 value = base::Value::CreateWithCopiedBuffer(
90 message_proto.payload_binary().data(),
91 message_proto.payload_binary().size());
92 break;
93 default:
94 // Unknown payload type. value will remain unset.
95 break;
96 }
97 if (value.get()) {
98 DCHECK(!message->data.get());
99 message->data = std::move(value);
100 return true;
101 } else {
102 return false;
103 }
104 }
105
106 std::string CastMessageToString(const CastMessage& message_proto) { 35 std::string CastMessageToString(const CastMessage& message_proto) {
107 std::string out("{"); 36 std::string out("{");
108 out += "namespace = " + message_proto.namespace_(); 37 out += "namespace = " + message_proto.namespace_();
109 out += ", sourceId = " + message_proto.source_id(); 38 out += ", sourceId = " + message_proto.source_id();
110 out += ", destId = " + message_proto.destination_id(); 39 out += ", destId = " + message_proto.destination_id();
111 out += ", type = " + base::IntToString(message_proto.payload_type()); 40 out += ", type = " + base::IntToString(message_proto.payload_type());
112 out += ", str = \"" + message_proto.payload_utf8() + "\"}"; 41 out += ", str = \"" + message_proto.payload_utf8() + "\"}";
113 return out; 42 return out;
114 } 43 }
115 44
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 message_proto->set_namespace_(kAuthNamespace); 78 message_proto->set_namespace_(kAuthNamespace);
150 message_proto->set_payload_type(CastMessage_PayloadType_BINARY); 79 message_proto->set_payload_type(CastMessage_PayloadType_BINARY);
151 message_proto->set_payload_binary(auth_message_string); 80 message_proto->set_payload_binary(auth_message_string);
152 } 81 }
153 82
154 bool IsAuthMessage(const CastMessage& message) { 83 bool IsAuthMessage(const CastMessage& message) {
155 return message.namespace_() == kAuthNamespace; 84 return message.namespace_() == kAuthNamespace;
156 } 85 }
157 86
158 } // namespace cast_channel 87 } // namespace cast_channel
159 } // namespace api
160 } // namespace extensions
OLDNEW
« no previous file with comments | « components/cast_channel/cast_message_util.h ('k') | components/cast_channel/cast_socket.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698