File: media_route_starter.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (177 lines) | stat: -rw-r--r-- 7,429 bytes parent folder | download | duplicates (5)
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
176
177
// Copyright 2022 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_UI_MEDIA_ROUTER_MEDIA_ROUTE_STARTER_H_
#define CHROME_BROWSER_UI_MEDIA_ROUTER_MEDIA_ROUTE_STARTER_H_

#include <string>

#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/media_router/media_cast_mode.h"
#include "chrome/browser/ui/media_router/media_router_ui_helper.h"
#include "chrome/browser/ui/media_router/media_sink_with_cast_modes_observer.h"
#include "chrome/browser/ui/media_router/presentation_request_source_observer.h"
#include "chrome/browser/ui/media_router/query_result_manager.h"
#include "components/media_router/browser/presentation/start_presentation_context.h"
#include "components/media_router/browser/presentation/web_contents_presentation_manager.h"
#include "components/media_router/common/issue.h"
#include "content/public/browser/presentation_observer.h"
#include "url/origin.h"

namespace content {
class BrowserContext;
struct PresentationRequest;
class WebContents;
}  // namespace content

namespace media_router {
class MediaRouter;
class QueryResultManager;
struct MediaRouterUIParameters;

// Provides cast services (lists of sinks, routes, start & terminate route) to
// UI controllers
class MediaRouteStarter : public content::PresentationObserver {
 public:
  explicit MediaRouteStarter(MediaRouterUIParameters params);
  MediaRouteStarter(const MediaRouteStarter&) = delete;
  MediaRouteStarter& operator=(const MediaRouteStarter&) = delete;

  ~MediaRouteStarter() override;

  // Adds/removes an observer that is notified when the presentation request
  // source is updated.
  void AddPresentationRequestSourceObserver(
      PresentationRequestSourceObserver* observer);
  void RemovePresentationRequestSourceObserver(
      PresentationRequestSourceObserver* observer);

  void AddMediaSinkWithCastModesObserver(
      MediaSinkWithCastModesObserver* observer);
  void RemoveMediaSinkWithCastModesObserver(
      MediaSinkWithCastModesObserver* observer);

  content::WebContents* GetWebContents() const { return web_contents_; }

  // Returns the profile associated with this casting attempt. Will either be
  // the profile associated with the browser containing the content being
  // cast, or the active user profile in the case of desktop mirroring.
  virtual Profile* GetProfile() const;

  // Returns the MediaRouter for this instance's BrowserContext.
  virtual MediaRouter* GetMediaRouter() const;

  // Sets the component used for media router logging statements made by the
  // MediaRouteStarter
  void SetLoggerComponent(const std::string& component) {
    component_ = component;
  }

  // Constructs |RouteParameters| with the necessary information for
  // |StartRoute| to construct a route between |sink_id| and an available source
  // that supports |cast_mode|. The returned |RouteParameters| can be modified
  // by the caller as desired, notably any callbacks that the caller wishes to
  // have called on route creation shuold be added to
  // |RouteParameters.route_result_callbacks|.
  std::unique_ptr<RouteParameters> CreateRouteParameters(
      const MediaSink::Id& sink_id,
      MediaCastMode cast_mode);

  void StartRoute(std::unique_ptr<RouteParameters> params);

  // Returns a PresentationRequest source name that can be shown in the dialog.
  std::u16string GetPresentationRequestSourceName() const;

  // Returns true if the specified sink supports the specified cast mode.
  bool SinkSupportsCastMode(const MediaSink::Id& sink_id,
                            MediaCastMode cast_mode) const;

  // Determines if the given cast mode requires user permission, and if so,
  // obtains it. Will only be false if permission is required and user does not
  // provide.
  static bool GetScreenCapturePermission(MediaCastMode cast_mode);

  QueryResultManager* GetQueryResultManagerForTesting() const {
    return query_result_manager_.get();
  }

 private:
  friend class MediaRouteStarterTest;
  friend class MediaRouterViewsUITest;
  FRIEND_TEST_ALL_PREFIXES(MediaRouteStarterTest,
                           OnPresentationRequestSourceRemoved);
  FRIEND_TEST_ALL_PREFIXES(MediaRouteStarterTest,
                           OnPresentationRequestSourceUpdated);
  FRIEND_TEST_ALL_PREFIXES(MediaRouteStarterTest, GetScreenCapturePermission);
  FRIEND_TEST_ALL_PREFIXES(MediaRouterViewsUITest, SetDialogHeader);
  FRIEND_TEST_ALL_PREFIXES(MediaRouterViewsUITest, RouteCreationTimeout);

  void InitPresentationSources(const CastModeSet& initial_modes);
  void InitMirroringSources(const CastModeSet& initial_modes);
  void InitRemotePlaybackSources(const CastModeSet& initial_modes,
                                 media::VideoCodec video_codec,
                                 media::AudioCodec audio_codec);

  content::BrowserContext* GetBrowserContext() const;

  // Returns the default PresentationRequest's frame origin if there is one.
  // Otherwise returns an opaque origin.
  url::Origin GetFrameOrigin() const;

  // Returns the QueryResultManager for this instance's |GetMediaRouter|
  QueryResultManager* GetQueryResultManager() const {
    return query_result_manager_.get();
  }

  // Returns true if the specified cast mode is among the cast modes specified
  // as supported for the service when initialized.
  static bool IsCastModeAvailable(const CastModeSet& modes, MediaCastMode mode);

  // content::PresentationObserver:
  void OnDefaultPresentationChanged(
      const content::PresentationRequest* presentation_request) override;

  void set_start_presentation_context_for_test(
      std::unique_ptr<StartPresentationContext> start_presentation_context) {
    start_presentation_context_ = std::move(start_presentation_context);
  }

  // Component name used for media router logging.
  std::string component_ = "MediaRouteStarter";

  // If set, this is the tab for which this casting request was initiated. May
  // be null in the case of desktop tab casting.
  const raw_ptr<content::WebContents, AcrossTasksDanglingUntriaged>
      web_contents_;

  // If set, then the result of the next presentation route request will
  // be handled by this object instead of |presentation_manager_|
  std::unique_ptr<StartPresentationContext> start_presentation_context_;

  // |presentation_manager_| notifies |this| whenever there is an update to the
  // default PresentationRequest or MediaRoutes associated with |web_contents_|.
  base::WeakPtr<WebContentsPresentationManager> presentation_manager_;

  // Monitors and reports sink availability.
  std::unique_ptr<QueryResultManager> query_result_manager_;

  // Set to the presentation request corresponding to the presentation cast
  // mode, if supported. Otherwise set to nullopt.
  std::optional<content::PresentationRequest> presentation_request_;

  // Registered observers.
  base::ObserverList<PresentationRequestSourceObserver>::Unchecked observers_;

  // NOTE: Weak pointers must be invalidated before all other member variables.
  // Therefore |weak_factory_| must be placed at the end.
  base::WeakPtrFactory<MediaRouteStarter> weak_factory_{this};
};

}  // namespace media_router

#endif  // CHROME_BROWSER_UI_MEDIA_ROUTER_MEDIA_ROUTE_STARTER_H_