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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/global_media_controls/media_notification_service.h"
#include <algorithm>
#include <memory>
#include "base/callback_list.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "base/unguessable_token.h"
#include "chrome/browser/feature_engagement/tracker_factory.h"
#include "chrome/browser/media/router/media_router_feature.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/global_media_controls/cast_device_list_host.h"
#include "chrome/browser/ui/global_media_controls/media_notification_device_provider_impl.h"
#include "chrome/browser/ui/global_media_controls/presentation_request_notification_producer.h"
#include "chrome/browser/ui/media_router/cast_dialog_controller.h"
#include "chrome/browser/ui/media_router/media_router_ui.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/buildflags.h"
#include "components/feature_engagement/public/tracker.h"
#include "components/global_media_controls/public/media_dialog_delegate.h"
#include "components/global_media_controls/public/media_item_manager.h"
#include "components/global_media_controls/public/media_item_producer.h"
#include "components/media_message_center/media_notification_item.h"
#include "components/media_router/browser/media_router_factory.h"
#include "components/media_router/browser/presentation/start_presentation_context.h"
#include "components/media_router/browser/presentation/web_contents_presentation_manager.h"
#include "components/sessions/content/session_tab_helper.h"
#include "content/public/browser/audio_service.h"
#include "content/public/browser/media_session.h"
#include "content/public/browser/media_session_service.h"
#include "media/base/media_switches.h"
#include "media/remoting/device_capability_checker.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "services/media_session/public/mojom/media_session.mojom.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "services/metrics/public/cpp/ukm_recorder.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/system/media/media_tray.h"
#include "ash/system/status_area_widget.h"
#include "ash/system/unified/unified_system_tray.h"
#include "ash/system/unified/unified_system_tray_bubble.h"
#include "ash/system/unified/unified_system_tray_controller.h"
#include "chrome/browser/ash/crosapi/crosapi_ash.h"
#include "chrome/browser/ash/crosapi/crosapi_manager.h"
#include "chrome/browser/ash/crosapi/media_ui_ash.h"
#endif
#if BUILDFLAG(ENABLE_GLIC)
#include "chrome/browser/glic/glic_keyed_service.h"
#endif
namespace mojom {
using global_media_controls::mojom::DeviceListClient;
using global_media_controls::mojom::DeviceListHost;
} // namespace mojom
namespace {
// The maximum number of actions we will record to UKM for a specific source.
constexpr int kMaxActionsRecordedToUKM = 100;
void CancelRequest(
std::unique_ptr<media_router::StartPresentationContext> context,
const std::string& message) {
context->InvokeErrorCallback(blink::mojom::PresentationError(
blink::mojom::PresentationErrorType::PRESENTATION_REQUEST_CANCELLED,
message));
}
// Here we check to see if the WebContents is focused. Note that we can't just
// use |WebContentsObserver::OnWebContentsFocused()| and
// |WebContentsObserver::OnWebContentsLostFocus()| because focusing the
// MediaDialogView causes the WebContents to "lose focus", so we'd never be
// focused.
bool IsWebContentsFocused(content::WebContents* web_contents) {
DCHECK(web_contents);
Browser* browser = chrome::FindBrowserWithTab(web_contents);
if (!browser) {
return false;
}
// If the given WebContents is not in the focused window, then it's not
// focused. Note that we know a Browser is focused because otherwise the user
// could not interact with the MediaDialogView.
if (BrowserList::GetInstance()->GetLastActive() != browser) {
return false;
}
return browser->tab_strip_model()->GetActiveWebContents() == web_contents;
}
#if BUILDFLAG(IS_CHROMEOS)
crosapi::mojom::MediaUI* GetMediaUI() {
// TODO(crbug.com/373971535): Figure how to call `media_ui_ash()` once crosapi
// is gone.
if (crosapi::CrosapiManager::IsInitialized()) {
return crosapi::CrosapiManager::Get()->crosapi_ash()->media_ui_ash();
}
return nullptr;
}
#endif // BUILDFLAG(IS_CHROMEOS)
bool ShouldInitializeWithRemotePlaybackSource(
content::WebContents* web_contents,
media_session::mojom::RemotePlaybackMetadataPtr remote_playback_metadata) {
if (!base::FeatureList::IsEnabled(media::kMediaRemotingWithoutFullscreen)) {
return false;
}
// Do not initialize MediaRouterUI with RemotePlayback media source when there
// exists default presentation request.
base::WeakPtr<media_router::WebContentsPresentationManager>
presentation_manager =
media_router::WebContentsPresentationManager::Get(web_contents);
if (presentation_manager &&
presentation_manager->HasDefaultPresentationRequest()) {
return false;
}
if (!remote_playback_metadata) {
return false;
}
if (media::remoting::ParseVideoCodec(remote_playback_metadata->video_codec) ==
media::VideoCodec::kUnknown ||
media::remoting::ParseAudioCodec(remote_playback_metadata->audio_codec) ==
media::AudioCodec::kUnknown) {
return false;
}
return true;
}
} // namespace
MediaNotificationService::MediaNotificationService(Profile* profile,
bool show_from_all_profiles)
: profile_(profile), receiver_(this) {
item_manager_ = global_media_controls::MediaItemManager::Create();
std::optional<base::UnguessableToken> source_id;
if (!show_from_all_profiles) {
source_id = content::MediaSession::GetSourceId(profile);
}
mojo::Remote<media_session::mojom::AudioFocusManager> audio_focus_remote;
mojo::Remote<media_session::mojom::MediaControllerManager>
controller_manager_remote;
// Connect to receive audio focus events.
content::GetMediaSessionService().BindAudioFocusManager(
audio_focus_remote.BindNewPipeAndPassReceiver());
// Connect to the controller manager so we can create media controllers for
// media sessions.
content::GetMediaSessionService().BindMediaControllerManager(
controller_manager_remote.BindNewPipeAndPassReceiver());
media_session_item_producer_ =
std::make_unique<global_media_controls::MediaSessionItemProducer>(
std::move(audio_focus_remote), std::move(controller_manager_remote),
item_manager_.get(), source_id);
// It is safe to use `base::Unretained` here because
// `media_session_item_producer_` is owned by `this`.
media_session_item_producer_->SetIsIdBlockedCallback(base::BindRepeating(
&MediaNotificationService::IsIdBlocked, base::Unretained(this)));
media_session_item_producer_->AddObserver(this);
item_manager_->AddItemProducer(media_session_item_producer_.get());
if (!media_router::MediaRouterEnabled(profile)) {
return;
}
// CastMediaNotificationProducer is owned by
// CastMediaNotificationProducerKeyedService in Ash.
#if !BUILDFLAG(IS_CHROMEOS)
// base::Unretained() is safe here because `cast_notification_producer_` is
// deleted before `item_manager_`.
cast_notification_producer_ = std::make_unique<CastMediaNotificationProducer>(
profile, item_manager_.get());
item_manager_->AddItemProducer(cast_notification_producer_.get());
#endif // !BUILDFLAG(IS_CHROMEOS)
presentation_request_notification_producer_ =
std::make_unique<PresentationRequestNotificationProducer>(
base::BindRepeating(
&MediaNotificationService::HasActiveNotificationsForWebContents,
base::Unretained(this)),
content::MediaSession::GetSourceId(profile));
#if !BUILDFLAG(IS_CHROMEOS)
supplemental_device_picker_producer_ =
std::make_unique<SupplementalDevicePickerProducer>(item_manager_.get());
item_manager_->AddItemProducer(supplemental_device_picker_producer_.get());
// On Chrome OS, SetDevicePickerProvider() gets called by Ash via the
// crosapi.
SetDevicePickerProvider(supplemental_device_picker_producer_->PassRemote());
#endif // !BUILDFLAG(IS_CHROMEOS)
#if BUILDFLAG(IS_CHROMEOS)
// The Ash instance manages Casting from System Web Apps.
if (GetMediaUI()) {
GetMediaUI()->RegisterDeviceService(
content::MediaSession::GetSourceId(profile),
receiver_.BindNewPipeAndPassRemote());
}
#endif // BUILDFLAG(IS_CHROMEOS)
}
#if BUILDFLAG(IS_CHROMEOS)
void MediaNotificationService::ShowDialogAsh(
std::unique_ptr<media_router::StartPresentationContext> context) {
auto* web_contents = content::WebContents::FromRenderFrameHost(
content::RenderFrameHost::FromID(
context->presentation_request().render_frame_host_id));
OnStartPresentationContextCreated(std::move(context));
auto routes = media_router::WebContentsPresentationManager::Get(web_contents)
->GetMediaRoutes();
std::string item_id;
if (!routes.empty()) {
// When `routes` is not empty, we'd ideally set `item_id` to be the ID of a
// MediaRoute so that we'd only show the corresponding notification item.
item_id = routes.begin()->media_route_id();
} else {
item_id = content::MediaSession::GetRequestIdFromWebContents(web_contents)
.ToString();
}
// Keep Media Tray pinned to use a separate widget in kiosk sessions because
// the Unified System Tray bubble is not available.
if (ash::Shell::Get()->session_controller()->IsRunningInAppMode()) {
ash::MediaTray::SetPinnedToShelf(true);
}
if (ash::MediaTray::IsPinnedToShelf()) {
ash::StatusAreaWidget::ForWindow(ash::Shell::Get()->GetPrimaryRootWindow())
->media_tray()
->ShowBubbleWithItem(item_id);
} else {
ash::UnifiedSystemTray* tray =
ash::StatusAreaWidget::ForWindow(
ash::Shell::Get()->GetPrimaryRootWindow())
->unified_system_tray();
tray->ShowBubble();
tray->bubble()
->unified_system_tray_controller()
->ShowMediaControlsDetailedView(
global_media_controls::GlobalMediaControlsEntryPoint::kPresentation,
item_id);
}
}
#endif // BUILDFLAG(IS_CHROMEOS)
MediaNotificationService::~MediaNotificationService() {
media_session_item_producer_->RemoveObserver(this);
item_manager_->RemoveItemProducer(media_session_item_producer_.get());
}
void MediaNotificationService::Shutdown() {
shutdown_has_started_ = true;
// `cast_notification_producer_`,
// `presentation_request_notification_producer_` and `host_receivers_`
// depend on MediaRouter, which is another keyed service. So they must be
// destroyed here.
if (cast_notification_producer_) {
item_manager_->RemoveItemProducer(cast_notification_producer_.get());
}
cast_notification_producer_.reset();
presentation_request_notification_producer_.reset();
for (const auto& host : host_receivers_) {
if (host.second) {
host.second->Close();
}
}
}
void MediaNotificationService::OnAudioSinkChosen(const std::string& item_id,
const std::string& sink_id) {
media_session_item_producer_->SetAudioSinkId(item_id, sink_id);
}
base::CallbackListSubscription
MediaNotificationService::RegisterAudioOutputDeviceDescriptionsCallback(
MediaNotificationDeviceProvider::GetOutputDevicesCallback callback) {
if (!device_provider_) {
device_provider_ = std::make_unique<MediaNotificationDeviceProviderImpl>(
content::CreateAudioSystemForAudioService());
}
return device_provider_->RegisterOutputDeviceDescriptionsCallback(
std::move(callback));
}
base::CallbackListSubscription
MediaNotificationService::RegisterIsAudioOutputDeviceSwitchingSupportedCallback(
const std::string& id,
base::RepeatingCallback<void(bool)> callback) {
return media_session_item_producer_
->RegisterIsAudioOutputDeviceSwitchingSupportedCallback(
id, std::move(callback));
}
void MediaNotificationService::OnMediaRemotingRequested(
const std::string& item_id) {
auto item = media_session_item_producer_->GetMediaItem(item_id);
if (!item) {
return;
}
item->RequestMediaRemoting();
auto* web_contents =
content::MediaSession::GetWebContentsFromRequestId(item_id);
if (web_contents && web_contents->GetLastCommittedURL().SchemeIsFile()) {
feature_engagement::TrackerFactory::GetForBrowserContext(profile_)
->NotifyEvent("media_route_started_from_gmc");
}
}
void MediaNotificationService::OnSinksDiscovered(const std::string& item_id) {
auto item = media_session_item_producer_->GetMediaItem(item_id);
auto* web_contents =
content::MediaSession::GetWebContentsFromRequestId(item_id);
if (web_contents) {
should_show_cast_local_media_iph_ =
web_contents->GetLastCommittedURL().SchemeIsFile();
}
}
void MediaNotificationService::OnMediaSessionActionButtonPressed(
const std::string& id,
media_session::mojom::MediaSessionAction action) {
auto* web_contents = content::MediaSession::GetWebContentsFromRequestId(id);
if (!web_contents) {
return;
}
base::UmaHistogramBoolean("Media.GlobalMediaControls.UserActionFocus",
IsWebContentsFocused(web_contents));
ukm::UkmRecorder* recorder = ukm::UkmRecorder::Get();
ukm::SourceId source_id =
web_contents->GetPrimaryMainFrame()->GetPageUkmSourceId();
if (++actions_recorded_to_ukm_[source_id] > kMaxActionsRecordedToUKM) {
return;
}
ukm::builders::Media_GlobalMediaControls_ActionButtonPressed(source_id)
.SetMediaSessionAction(static_cast<int64_t>(action))
.Record(recorder);
}
void MediaNotificationService::SetDialogDelegateForWebContents(
global_media_controls::MediaDialogDelegate* delegate,
content::WebContents* contents) {
DCHECK(delegate);
DCHECK(contents);
// When the dialog is opened by a PresentationRequest, there should be only
// one notification, in the following priority order:
// 1. A cast presentation session associated with `contents`.
// 2. A local media session associated with `contents`. This media session
// might potentially be associated with a Remote Playback route.
// 3. A supplemental notification populated using the PresentationRequest.
std::string item_id;
// Find the cast presentation route associated with `contents`.
// WebContentsPresentationManager manages all presentation routes including
// Cast and Remote Playback presentations. For the sake of displaying media
// routes in the GMC dialog, Cast presentation routes should be shown as Cast
// notification items and Remote Playback presentation routes should be shown
// as media session notification items.
std::optional<std::string> cast_presentation_route_id;
for (const auto& route :
media_router::WebContentsPresentationManager::Get(contents)
->GetMediaRoutes()) {
if (route.media_source().IsCastPresentationUrl()) {
cast_presentation_route_id = route.media_route_id();
break;
}
}
if (cast_presentation_route_id.has_value()) {
// It is possible for a sender page to connect to two routes. For the
// sake of the Zenith dialog, only one notification is needed.
item_id = cast_presentation_route_id.value();
} else if (HasActiveControllableSessionForWebContents(contents)) {
item_id = GetActiveControllableSessionForWebContents(contents);
} else {
const SupplementalDevicePickerItem& supplemental_item =
supplemental_device_picker_producer_->GetOrCreateNotificationItem(
content::MediaSession::GetSourceId(profile_));
item_id = supplemental_item.id();
DCHECK(presentation_request_notification_producer_->GetWebContents() ==
contents);
}
item_manager_->SetDialogDelegateForId(delegate, item_id);
}
bool MediaNotificationService::HasActiveNotificationsForWebContents(
content::WebContents* web_contents) const {
bool has_media_session =
HasActiveControllableSessionForWebContents(web_contents);
return HasCastNotificationsForWebContents(web_contents) || has_media_session;
}
bool MediaNotificationService::HasLocalCastNotifications() const {
return cast_notification_producer_
? cast_notification_producer_->HasLocalMediaRoute()
: false;
}
void MediaNotificationService::OnStartPresentationContextCreated(
std::unique_ptr<media_router::StartPresentationContext> context) {
auto* web_contents = content::WebContents::FromRenderFrameHost(
content::RenderFrameHost::FromID(
context->presentation_request().render_frame_host_id));
if (!web_contents) {
CancelRequest(std::move(context), "The web page is closed.");
return;
}
// If there exists a cast notification associated with `web_contents`, delete
// `context` because users should not start a new presentation at this time.
if (HasCastNotificationsForWebContents(web_contents)) {
CancelRequest(std::move(context), "A presentation has already started.");
} else if (HasActiveControllableSessionForWebContents(web_contents)) {
// If there exists a media session notification and a tab mirroring session,
// both, associated with `web_contents`, delete `context` because users
// should not start a new presentation at this time.
if (HasTabMirroringSessionForWebContents(web_contents)) {
CancelRequest(std::move(context),
"A tab mirroring session has already started.");
return;
}
// If there exists a media session notification associated with
// |web_contents|, hold onto the context for later use.
context_ = std::move(context);
// When a media session item is associated with PresentationRequest, we
// must show the origin associated with the request rather than that for
// the top frame.
std::string item_id =
GetActiveControllableSessionForWebContents(web_contents);
media_session_item_producer_->UpdateMediaItemSourceOrigin(
item_id, context_->presentation_request().frame_origin);
} else if (presentation_request_notification_producer_) {
// If there do not exist active notifications, pass |context| to
// |presentation_request_notification_producer_| to create a dummy
// notification.
presentation_request_notification_producer_
->OnStartPresentationContextCreated(std::move(context));
} else {
CancelRequest(std::move(context), "Unable to start presentation.");
}
}
void MediaNotificationService::GetDeviceListHostForSession(
const std::string& session_id,
mojo::PendingReceiver<mojom::DeviceListHost> host_receiver,
mojo::PendingRemote<mojom::DeviceListClient> client_remote) {
std::optional<std::string> remoting_session_id;
// `remoting_session_id` is used to construct the MediaRemotingCallback for
// CastDeviceListHost to request Media Remoting for a MediaSession. This is
// used for Media Remoting sessions started from the GMC dialog. However, when
// the dialog is opened for RemotePlayback#prompt() (when `context_` is not
// nullptr), the Remote Playback API on the blink side handles sending Media
// Remoting request and there's no need for requesting Media Remoting from
// MNS.
if (context_ == nullptr) {
remoting_session_id = session_id;
}
CreateCastDeviceListHost(CreateCastDialogControllerForSession(session_id),
std::move(host_receiver), std::move(client_remote),
remoting_session_id);
}
void MediaNotificationService::GetDeviceListHostForPresentation(
mojo::PendingReceiver<mojom::DeviceListHost> host_receiver,
mojo::PendingRemote<mojom::DeviceListClient> client_remote) {
CreateCastDeviceListHost(CreateCastDialogControllerForPresentationRequest(),
std::move(host_receiver), std::move(client_remote),
std::nullopt);
}
void MediaNotificationService::SetDevicePickerProvider(
mojo::PendingRemote<global_media_controls::mojom::DevicePickerProvider>
provider_remote) {
presentation_request_notification_producer_->BindProvider(
std::move(provider_remote));
}
std::unique_ptr<media_router::CastDialogController>
MediaNotificationService::CreateCastDialogControllerForSession(
const std::string& id) {
auto* web_contents = content::MediaSession::GetWebContentsFromRequestId(id);
if (!web_contents) {
return nullptr;
}
if (context_) {
return media_router::MediaRouterUI::CreateWithStartPresentationContext(
web_contents, std::move(context_));
}
auto remote_playback_metadata =
media_session_item_producer_->GetRemotePlaybackMetadataFromItem(id);
if (ShouldInitializeWithRemotePlaybackSource(
web_contents, remote_playback_metadata.Clone())) {
return media_router::MediaRouterUI::CreateWithMediaSessionRemotePlayback(
web_contents,
media::remoting::ParseVideoCodec(remote_playback_metadata->video_codec),
media::remoting::ParseAudioCodec(
remote_playback_metadata->audio_codec));
}
return media_router::MediaRouterUI::CreateWithDefaultMediaSource(
web_contents);
}
std::unique_ptr<media_router::CastDialogController>
MediaNotificationService::CreateCastDialogControllerForPresentationRequest() {
auto* web_contents =
presentation_request_notification_producer_->GetWebContents();
if (!web_contents) {
return nullptr;
}
if (!presentation_request_notification_producer_->GetNotificationItem()
->is_default_presentation_request()) {
return media_router::MediaRouterUI::CreateWithStartPresentationContext(
web_contents,
presentation_request_notification_producer_->GetNotificationItem()
->PassContext());
}
return media_router::MediaRouterUI::CreateWithDefaultMediaSource(
web_contents);
}
void MediaNotificationService::CreateCastDeviceListHost(
std::unique_ptr<media_router::CastDialogController> dialog_controller,
mojo::PendingReceiver<mojom::DeviceListHost> host_pending_receiver,
mojo::PendingRemote<mojom::DeviceListClient> client_remote,
std::optional<std::string> remoting_session_id) {
if (!dialog_controller) {
// We discard the PendingReceiver/Remote here, and if they have disconnect
// handlers set, those get called.
return;
}
auto media_remoting_callback_ =
remoting_session_id.has_value()
? base::BindRepeating(
&MediaNotificationService::OnMediaRemotingRequested,
weak_ptr_factory_.GetWeakPtr(), remoting_session_id.value())
: base::DoNothing();
auto on_sinks_discovered_callback =
remoting_session_id.has_value()
? base::BindRepeating(&MediaNotificationService::OnSinksDiscovered,
weak_ptr_factory_.GetWeakPtr(),
remoting_session_id.value())
: base::DoNothing();
auto host = std::make_unique<CastDeviceListHost>(
std::move(dialog_controller), std::move(client_remote),
std::move(media_remoting_callback_),
base::BindRepeating(&global_media_controls::MediaItemManager::HideDialog,
item_manager_->GetWeakPtr()),
std::move(on_sinks_discovered_callback));
int host_id = host->id();
mojo::SelfOwnedReceiverRef<global_media_controls::mojom::DeviceListHost>
host_receiver = mojo::MakeSelfOwnedReceiver(
std::move(host), std::move(host_pending_receiver));
host_receiver->set_connection_error_handler(
base::BindOnce(&MediaNotificationService::RemoveDeviceListHost,
weak_ptr_factory_.GetWeakPtr(), host_id));
host_receivers_.emplace(host_id, std::move(host_receiver));
}
void MediaNotificationService::set_device_provider_for_testing(
std::unique_ptr<MediaNotificationDeviceProvider> device_provider) {
device_provider_ = std::move(device_provider);
}
bool MediaNotificationService::HasCastNotificationsForWebContents(
content::WebContents* web_contents) const {
return !media_router::WebContentsPresentationManager::Get(web_contents)
->GetMediaRoutes()
.empty();
}
bool MediaNotificationService::HasTabMirroringSessionForWebContents(
content::WebContents* web_contents) const {
if (!base::FeatureList::IsEnabled(
media_router::kFallbackToAudioTabMirroring)) {
return false;
}
// Return true if there exists a tab mirroring session associated with
// `web_contents`.
const int item_tab_id =
sessions::SessionTabHelper::IdForTab(web_contents).id();
for (const auto& route :
media_router::MediaRouterFactory::GetApiForBrowserContext(
web_contents->GetBrowserContext())
->GetCurrentRoutes()) {
media_router::MediaSource media_source = route.media_source();
if (media_source.IsTabMirroringSource() &&
media_source.TabId().has_value() &&
media_source.TabId().value() == item_tab_id) {
return true;
}
}
return false;
}
bool MediaNotificationService::HasActiveControllableSessionForWebContents(
content::WebContents* web_contents) const {
DCHECK(web_contents);
auto item_ids = media_session_item_producer_->GetActiveControllableItemIds();
return std::ranges::any_of(item_ids, [web_contents](const auto& item_id) {
return web_contents ==
content::MediaSession::GetWebContentsFromRequestId(item_id);
});
}
std::string
MediaNotificationService::GetActiveControllableSessionForWebContents(
content::WebContents* web_contents) const {
DCHECK(web_contents);
for (const auto& item_id :
media_session_item_producer_->GetActiveControllableItemIds()) {
if (web_contents ==
content::MediaSession::GetWebContentsFromRequestId(item_id)) {
return item_id;
}
}
return "";
}
void MediaNotificationService::RemoveDeviceListHost(int host_id) {
// If shutdown has started, then we may currently be iterating through
// `host_receivers_` so we should not erase from it. `host_receivers_` will
// get destroyed soon anyways.
if (!shutdown_has_started_) {
host_receivers_.erase(host_id);
}
}
bool MediaNotificationService::IsIdBlocked(
const std::string& request_id) const {
#if BUILDFLAG(ENABLE_GLIC)
auto* glic_keyed_service = glic::GlicKeyedService::Get(profile_);
if (!glic_keyed_service) {
return false;
}
auto* host = glic_keyed_service->host().webui_contents();
if (!host) {
return false;
}
std::vector<content::WebContents*> inner_contents =
host->GetInnerWebContents();
if (inner_contents.size() == 1ul) {
return content::MediaSession::GetRequestIdFromWebContents(inner_contents[0])
.ToString() == request_id;
}
#endif
return false;
}
|