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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_CAST_CHANNEL_CAST_MESSAGE_UTIL_H_
6 #define COMPONENTS_CAST_CHANNEL_CAST_MESSAGE_UTIL_H_
10 #include "base/values.h"
12 namespace cast_channel {
16 class DeviceAuthMessage;
18 // Reserved message namespaces for internal messages.
19 static constexpr char kCastInternalNamespacePrefix[] =
20 "urn:x-cast:com.google.cast.";
21 static constexpr char kAuthNamespace[] =
22 "urn:x-cast:com.google.cast.tp.deviceauth";
23 static constexpr char kHeartbeatNamespace[] =
24 "urn:x-cast:com.google.cast.tp.heartbeat";
25 static constexpr char kConnectionNamespace[] =
26 "urn:x-cast:com.google.cast.tp.connection";
27 static constexpr char kReceiverNamespace[] =
28 "urn:x-cast:com.google.cast.receiver";
29 static constexpr char kBroadcastNamespace[] =
30 "urn:x-cast:com.google.cast.broadcast";
31 static constexpr char kMediaNamespace[] = "urn:x-cast:com.google.cast.media";
33 // Sender and receiver IDs to use for platform messages.
34 static constexpr char kPlatformSenderId[] = "sender-0";
35 static constexpr char kPlatformReceiverId[] = "receiver-0";
37 // Cast application protocol message types.
38 enum class CastMessageType {
42 kReceiverStatusRequest,
43 kConnect, // Virtual connection request
44 kCloseConnection, // Close virtual connection
45 kBroadcast, // Application broadcast / precache
46 kLaunch, // Session launch request
47 kStop, // Session stop request
51 kOther // Add new types above |kOther|.
54 enum class V2MessageType {
72 kOther // Add new types above |kOther|.
75 // Checks if the contents of |message_proto| are valid.
76 bool IsCastMessageValid(const CastMessage& message_proto);
78 // Returns true if |message_namespace| is a namespace reserved for internal
80 bool IsCastInternalNamespace(const std::string& message_namespace);
82 // Returns the value in the "type" field or |kOther| if the field is not found.
83 // The result is only valid if |payload| is a Cast application protocol message.
84 CastMessageType ParseMessageTypeFromPayload(const base::Value& payload);
86 // Returns a human readable string for |message_type|.
87 const char* ToString(CastMessageType message_type);
88 const char* ToString(V2MessageType message_type);
90 // Returns the CastMessageType for |type|, or |kOther| if it does not
91 // correspond to a known type.
92 CastMessageType CastMessageTypeFromString(const std::string& type);
94 // Returns the V2MessageType for |type|, or |kOther| if it does not
95 // correspond to a known type.
96 V2MessageType V2MessageTypeFromString(const std::string& type);
98 // Returns a human readable string for |message_proto|.
99 std::string CastMessageToString(const CastMessage& message_proto);
101 // Returns a human readable string for |message|.
102 std::string AuthMessageToString(const DeviceAuthMessage& message);
104 // Fills |message_proto| appropriately for an auth challenge request message.
105 // Uses the nonce challenge in |auth_context|.
106 void CreateAuthChallengeMessage(CastMessage* message_proto,
107 const AuthContext& auth_context);
109 // Returns whether the given message is an auth handshake message.
110 bool IsAuthMessage(const CastMessage& message);
112 // Returns whether |message| is a Cast receiver message.
113 bool IsReceiverMessage(const CastMessage& message);
115 // Returns whether |message| is destined for the platform sender.
116 bool IsPlatformSenderMessage(const CastMessage& message);
118 // Creates a keep-alive message of either type PING or PONG.
119 CastMessage CreateKeepAlivePingMessage();
120 CastMessage CreateKeepAlivePongMessage();
122 enum VirtualConnectionType {
124 // kWeak = 1 not used.
128 // Creates a virtual connection request message for |source_id| and
129 // |destination_id|. |user_agent| and |browser_version| will be included with
131 // If |destination_id| is kPlatformReceiverId, then |connection_type| must be
132 // kStrong. Otherwise |connection_type| can be either kStrong or kInvisible.
133 CastMessage CreateVirtualConnectionRequest(
134 const std::string& source_id,
135 const std::string& destination_id,
136 VirtualConnectionType connection_type,
137 const std::string& user_agent,
138 const std::string& browser_version);
140 // Creates an app availability request for |app_id| from |source_id| with
142 // TODO(imcheng): May not need |source_id|, just use sender-0?
143 CastMessage CreateGetAppAvailabilityRequest(const std::string& source_id,
145 const std::string& app_id);
147 CastMessage CreateReceiverStatusRequest(const std::string& source_id,
150 // Represents a broadcast request. Currently it is used for precaching data
152 struct BroadcastRequest {
153 BroadcastRequest(const std::string& broadcast_namespace,
154 const std::string& message);
156 bool operator==(const BroadcastRequest& other) const;
158 std::string broadcast_namespace;
162 // Creates a broadcast request with the given parameters.
163 CastMessage CreateBroadcastRequest(const std::string& source_id,
165 const std::vector<std::string>& app_ids,
166 const BroadcastRequest& request);
168 // Creates a session launch request with the given parameters.
169 CastMessage CreateLaunchRequest(const std::string& source_id,
171 const std::string& app_id,
172 const std::string& locale);
174 CastMessage CreateStopRequest(const std::string& source_id,
176 const std::string& session_id);
178 // Creates a generic CastMessage with |message| as the string payload. Used for
180 CastMessage CreateCastMessage(const std::string& message_namespace,
181 const base::Value& message,
182 const std::string& source_id,
183 const std::string& destination_id);
185 CastMessage CreateMediaRequest(const base::Value& body,
187 const std::string& source_id,
188 const std::string& destination_id);
190 CastMessage CreateSetVolumeRequest(const base::Value& body,
192 const std::string& source_id);
194 bool IsMediaRequestMessageType(V2MessageType v2_message_type);
196 // Possible results of a GET_APP_AVAILABILITY request.
197 enum class GetAppAvailabilityResult {
203 const char* ToString(GetAppAvailabilityResult result);
205 // Extracts request ID from |payload| corresponding to a Cast message response.
206 base::Optional<int> GetRequestIdFromResponse(const base::Value& payload);
208 // Returns the GetAppAvailabilityResult corresponding to |app_id| in |payload|.
209 // Returns kUnknown if result is not found.
210 GetAppAvailabilityResult GetAppAvailabilityResultFromResponse(
211 const base::Value& payload,
212 const std::string& app_id);
214 // Result of a session launch.
215 struct LaunchSessionResponse {
216 enum Result { kOk, kError, kTimedOut, kUnknown };
218 LaunchSessionResponse();
219 LaunchSessionResponse(LaunchSessionResponse&& other);
220 ~LaunchSessionResponse();
222 Result result = Result::kUnknown;
223 // Populated if |result| is |kOk|.
224 base::Optional<base::Value> receiver_status;
227 // Parses |payload| into a LaunchSessionResponse. Returns an empty
228 // LaunchSessionResponse if |payload| is not a properly formatted launch
229 // response. |payload| must be a dictionary from the string payload of a
231 LaunchSessionResponse GetLaunchSessionResponse(const base::Value& payload);
233 } // namespace cast_channel
235 #endif // COMPONENTS_CAST_CHANNEL_CAST_MESSAGE_UTIL_H_