File: dial_internal_message_util.h

package info (click to toggle)
chromium 139.0.7258.127-1~deb13u1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,096 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (175 lines) | stat: -rw-r--r-- 6,552 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_MEDIA_ROUTER_PROVIDERS_DIAL_DIAL_INTERNAL_MESSAGE_UTIL_H_
#define CHROME_BROWSER_MEDIA_ROUTER_PROVIDERS_DIAL_DIAL_INTERNAL_MESSAGE_UTIL_H_

#include <memory>
#include <string>

#include "base/values.h"
#include "chrome/browser/media/router/discovery/dial/dial_app_discovery_service.h"
#include "chrome/browser/media/router/discovery/dial/parsed_dial_app_info.h"
#include "components/media_router/common/mojom/media_router.mojom.h"

namespace media_router {

class MediaSinkInternal;

// Types of internal messages that are used in a custom DIAL launch workflow.
enum class DialInternalMessageType {
  // Cast SDK -> MR
  kClientConnect,
  kV2Message,

  // MR -> Cast SDK
  kNewSession,
  kReceiverAction,
  kError,

  // MR <-> Cast SDK
  kCustomDialLaunch,
  kDialAppInfo,

  kOther
};

// Possible types of ReceiverAction taken by the user on a receiver.
enum class DialReceiverAction {
  // The user selected a receiver with the intent of casting to it with the
  // sender application.
  kCast,

  // The user requested to stop the session running on a receiver.
  kStop
};

// Parsed custom DIAL launch internal message coming from a Cast SDK client.
struct DialInternalMessage {
  // Returns a DialInternalMessage for |message|. If |message| is not a valid
  // custom DIAL launch internal message, returns nullptr and sets |error| with
  // an error reason.
  static std::unique_ptr<DialInternalMessage> From(base::Value::Dict message,
                                                   std::string* error);

  DialInternalMessage(DialInternalMessageType type,
                      std::optional<base::Value> body,
                      const std::string& client_id,
                      int sequence_number);

  DialInternalMessage(const DialInternalMessage&) = delete;
  DialInternalMessage& operator=(const DialInternalMessage&) = delete;

  ~DialInternalMessage();

  DialInternalMessageType type;
  std::optional<base::Value> body;
  std::string client_id;
  int sequence_number;
};

// Parsed CUSTOM_DIAL_LAUNCH response from the Cast SDK client.
struct CustomDialLaunchMessageBody {
  // Returns a CustomDialLaunchMessageBody for |message|.
  // This method is only valid to call if |message.type| == |kCustomDialLaunch|.
  static CustomDialLaunchMessageBody From(const DialInternalMessage& message);

  CustomDialLaunchMessageBody();
  CustomDialLaunchMessageBody(
      bool do_launch,
      const std::optional<std::string>& launch_parameter);
  CustomDialLaunchMessageBody(const CustomDialLaunchMessageBody& other);
  ~CustomDialLaunchMessageBody();

  // If |true|, the DialMediaRouteProvider should handle the app launch.
  bool do_launch = true;

  // If |do_launch| is |true|, optional launch parameter to include with the
  // launch (POST) request. This overrides the launch parameter that was
  // specified in the MediaSource (if any).
  std::optional<std::string> launch_parameter;
};

class DialInternalMessageUtil final {
 public:
  // |hash_token|: A per-profile value used to hash sink IDs.
  explicit DialInternalMessageUtil(const std::string& hash_token);

  DialInternalMessageUtil(const DialInternalMessageUtil&) = delete;
  DialInternalMessageUtil& operator=(const DialInternalMessageUtil&) = delete;

  ~DialInternalMessageUtil();

  // Returns |true| if |message| is a valid STOP_SESSION message.
  static bool IsStopSessionMessage(const DialInternalMessage& message);

  // Returns a NEW_SESSION message to be sent to the page when the user requests
  // an app launch.
  mojom::RouteMessagePtr CreateNewSessionMessage(
      const std::string& app_name,
      const std::string& client_id,
      const MediaSinkInternal& sink) const;

  // Returns a RECEIVER_ACTION / CAST message to be sent to the page when the
  // user requests an app launch.
  mojom::RouteMessagePtr CreateReceiverActionCastMessage(
      const std::string& client_id,
      const MediaSinkInternal& sink) const;

  // Returns a RECEIVER_ACTION / STOP message to be sent to the page when an app
  // is stopped by DialMediaRouteProvider.
  mojom::RouteMessagePtr CreateReceiverActionStopMessage(
      const std::string& client_id,
      const MediaSinkInternal& sink) const;

  // Returns a CUSTOM_DIAL_LAUNCH request message to be sent to the page.
  // Generates and returns the next number to associate a DIAL launch sequence
  // with.
  std::pair<mojom::RouteMessagePtr, int> CreateCustomDialLaunchMessage(
      const std::string& client_id,
      const MediaSinkInternal& sink,
      const ParsedDialAppInfo& app_info) const;

  // Creates an app info message used in a DIAL_APP_INFO response or a
  // CUSTOM_DIAL_LAUNCH (called via CreateCustomDialLaunchMessage() above)
  // message.
  mojom::RouteMessagePtr CreateDialAppInfoMessage(
      const std::string& client_id,
      const MediaSinkInternal& sink,
      const ParsedDialAppInfo& app_info,
      int sequence_number,
      DialInternalMessageType type) const;

  mojom::RouteMessagePtr CreateDialAppInfoErrorMessage(
      DialAppInfoResultCode result_code,
      const std::string& client_id,
      int sequence_number,
      const std::string& error_message,
      std::optional<int> http_error_code = std::nullopt) const;

 private:
  base::Value::Dict CreateReceiver(const MediaSinkInternal& sink) const;
  base::Value::Dict CreateReceiverActionBody(const MediaSinkInternal& sink,
                                             DialReceiverAction action) const;
  base::Value::Dict CreateNewSessionBody(const std::string& app_name,
                                         const MediaSinkInternal& sink) const;
  base::Value::Dict CreateDialAppInfoBody(
      const MediaSinkInternal& sink,
      const ParsedDialAppInfo& app_info) const;

  // |sequence_number| is used by the Cast SDK to match up requests from the SDK
  // to Chrome with responses from Chrome. If a message from Chrome has no
  // corresponding request, then its |sequence_number| is an invalid value of
  // -1.
  base::Value CreateDialMessageCommon(DialInternalMessageType type,
                                      base::Value::Dict body,
                                      const std::string& client_id,
                                      int sequence_number = -1) const;

  std::string hash_token_;
};

}  // namespace media_router

#endif  // CHROME_BROWSER_MEDIA_ROUTER_PROVIDERS_DIAL_DIAL_INTERNAL_MESSAGE_UTIL_H_