[M73 Dev][EFL] Disable VizDisplayCompositor for EFL port
[platform/framework/web/chromium-efl.git] / components / cast_channel / cast_message_util.h
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.
4
5 #ifndef COMPONENTS_CAST_CHANNEL_CAST_MESSAGE_UTIL_H_
6 #define COMPONENTS_CAST_CHANNEL_CAST_MESSAGE_UTIL_H_
7
8 #include <string>
9
10 #include "base/values.h"
11
12 namespace cast_channel {
13
14 class AuthContext;
15 class CastMessage;
16 class DeviceAuthMessage;
17
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";
32
33 // Sender and receiver IDs to use for platform messages.
34 static constexpr char kPlatformSenderId[] = "sender-0";
35 static constexpr char kPlatformReceiverId[] = "receiver-0";
36
37 // Cast application protocol message types.
38 enum class CastMessageType {
39   kPing,
40   kPong,
41   kGetAppAvailability,
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
48   kReceiverStatus,
49   kMediaStatus,
50   kLaunchError,
51   kOther  // Add new types above |kOther|.
52 };
53
54 enum class V2MessageType {
55   kEditTracksInfo,
56   kGetStatus,
57   kLoad,
58   kMediaGetStatus,
59   kMediaSetVolume,
60   kPause,
61   kPlay,
62   kPrecache,
63   kQueueInsert,
64   kQueueLoad,
65   kQueueRemove,
66   kQueueReorder,
67   kQueueUpdate,
68   kSeek,
69   kSetVolume,
70   kStop,
71   kStopMedia,
72   kOther  // Add new types above |kOther|.
73 };
74
75 // Checks if the contents of |message_proto| are valid.
76 bool IsCastMessageValid(const CastMessage& message_proto);
77
78 // Returns true if |message_namespace| is a namespace reserved for internal
79 // messages.
80 bool IsCastInternalNamespace(const std::string& message_namespace);
81
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);
85
86 // Returns a human readable string for |message_type|.
87 const char* ToString(CastMessageType message_type);
88 const char* ToString(V2MessageType message_type);
89
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);
93
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);
97
98 // Returns a human readable string for |message_proto|.
99 std::string CastMessageToString(const CastMessage& message_proto);
100
101 // Returns a human readable string for |message|.
102 std::string AuthMessageToString(const DeviceAuthMessage& message);
103
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);
108
109 // Returns whether the given message is an auth handshake message.
110 bool IsAuthMessage(const CastMessage& message);
111
112 // Returns whether |message| is a Cast receiver message.
113 bool IsReceiverMessage(const CastMessage& message);
114
115 // Returns whether |message| is destined for the platform sender.
116 bool IsPlatformSenderMessage(const CastMessage& message);
117
118 // Creates a keep-alive message of either type PING or PONG.
119 CastMessage CreateKeepAlivePingMessage();
120 CastMessage CreateKeepAlivePongMessage();
121
122 enum VirtualConnectionType {
123   kStrong = 0,
124   // kWeak = 1 not used.
125   kInvisible = 2
126 };
127
128 // Creates a virtual connection request message for |source_id| and
129 // |destination_id|. |user_agent| and |browser_version| will be included with
130 // the request.
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);
139
140 // Creates an app availability request for |app_id| from |source_id| with
141 // ID |request_id|.
142 // TODO(imcheng): May not need |source_id|, just use sender-0?
143 CastMessage CreateGetAppAvailabilityRequest(const std::string& source_id,
144                                             int request_id,
145                                             const std::string& app_id);
146
147 CastMessage CreateReceiverStatusRequest(const std::string& source_id,
148                                         int request_id);
149
150 // Represents a broadcast request. Currently it is used for precaching data
151 // on a receiver.
152 struct BroadcastRequest {
153   BroadcastRequest(const std::string& broadcast_namespace,
154                    const std::string& message);
155   ~BroadcastRequest();
156   bool operator==(const BroadcastRequest& other) const;
157
158   std::string broadcast_namespace;
159   std::string message;
160 };
161
162 // Creates a broadcast request with the given parameters.
163 CastMessage CreateBroadcastRequest(const std::string& source_id,
164                                    int request_id,
165                                    const std::vector<std::string>& app_ids,
166                                    const BroadcastRequest& request);
167
168 // Creates a session launch request with the given parameters.
169 CastMessage CreateLaunchRequest(const std::string& source_id,
170                                 int request_id,
171                                 const std::string& app_id,
172                                 const std::string& locale);
173
174 CastMessage CreateStopRequest(const std::string& source_id,
175                               int request_id,
176                               const std::string& session_id);
177
178 // Creates a generic CastMessage with |message| as the string payload. Used for
179 // app messages.
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);
184
185 CastMessage CreateMediaRequest(const base::Value& body,
186                                int request_id,
187                                const std::string& source_id,
188                                const std::string& destination_id);
189
190 CastMessage CreateSetVolumeRequest(const base::Value& body,
191                                    int request_id,
192                                    const std::string& source_id);
193
194 bool IsMediaRequestMessageType(V2MessageType v2_message_type);
195
196 // Possible results of a GET_APP_AVAILABILITY request.
197 enum class GetAppAvailabilityResult {
198   kAvailable,
199   kUnavailable,
200   kUnknown,
201 };
202
203 const char* ToString(GetAppAvailabilityResult result);
204
205 // Extracts request ID from |payload| corresponding to a Cast message response.
206 base::Optional<int> GetRequestIdFromResponse(const base::Value& payload);
207
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);
213
214 // Result of a session launch.
215 struct LaunchSessionResponse {
216   enum Result { kOk, kError, kTimedOut, kUnknown };
217
218   LaunchSessionResponse();
219   LaunchSessionResponse(LaunchSessionResponse&& other);
220   ~LaunchSessionResponse();
221
222   Result result = Result::kUnknown;
223   // Populated if |result| is |kOk|.
224   base::Optional<base::Value> receiver_status;
225 };
226
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
230 // CastMessage.
231 LaunchSessionResponse GetLaunchSessionResponse(const base::Value& payload);
232
233 }  // namespace cast_channel
234
235 #endif  // COMPONENTS_CAST_CHANNEL_CAST_MESSAGE_UTIL_H_