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
|
// Copyright 2020 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/presentation_request_notification_producer.h"
#include <utility>
#include "base/memory/raw_ptr.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "components/media_router/common/providers/cast/cast_media_source.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/media_session.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
namespace {
base::WeakPtr<media_router::WebContentsPresentationManager>
GetActiveWebContentsPresentationManager() {
auto* browser = chrome::FindLastActive();
if (!browser) {
return nullptr;
}
auto* tab_strip = browser->tab_strip_model();
if (!tab_strip) {
return nullptr;
}
auto* web_contents = tab_strip->GetActiveWebContents();
if (!web_contents) {
return nullptr;
}
return media_router::WebContentsPresentationManager::Get(web_contents);
}
content::WebContents* GetWebContentsFromPresentationRequest(
const content::PresentationRequest& request) {
auto* rfh = content::RenderFrameHost::FromID(request.render_frame_host_id);
return rfh ? content::WebContents::FromRenderFrameHost(rfh) : nullptr;
}
} // namespace
class PresentationRequestNotificationProducer::
PresentationRequestWebContentsObserver
: public content::WebContentsObserver {
public:
PresentationRequestWebContentsObserver(
content::WebContents* web_contents,
PresentationRequestNotificationProducer* notification_producer)
: content::WebContentsObserver(web_contents),
notification_producer_(notification_producer) {
DCHECK(notification_producer_);
}
private:
void WebContentsDestroyed() override {
notification_producer_->DeleteItemForPresentationRequest("Dialog closed.");
}
void NavigationEntryCommitted(
const content::LoadCommittedDetails& load_details) override {
notification_producer_->DeleteItemForPresentationRequest("Dialog closed.");
}
void PrimaryMainFrameRenderProcessGone(
base::TerminationStatus status) override {
notification_producer_->DeleteItemForPresentationRequest("Dialog closed.");
}
const raw_ptr<PresentationRequestNotificationProducer> notification_producer_;
};
PresentationRequestNotificationProducer::
PresentationRequestNotificationProducer(
base::RepeatingCallback<bool(content::WebContents*)>
has_active_notifications_callback,
const base::UnguessableToken& source_id)
: observer_receiver_(this),
has_active_notifications_callback_(
std::move(has_active_notifications_callback)),
source_id_(source_id) {}
PresentationRequestNotificationProducer::
~PresentationRequestNotificationProducer() = default;
void PresentationRequestNotificationProducer::OnStartPresentationContextCreated(
std::unique_ptr<media_router::StartPresentationContext> context) {
DCHECK(context);
const auto& request = context->presentation_request();
CreateItemForPresentationRequest(request, std::move(context));
}
content::WebContents*
PresentationRequestNotificationProducer::GetWebContents() {
return item_ ? GetWebContentsFromPresentationRequest(item_->request())
: nullptr;
}
base::WeakPtr<PresentationRequestNotificationItem>
PresentationRequestNotificationProducer::GetNotificationItem() {
return item_ ? item_->GetWeakPtr() : nullptr;
}
void PresentationRequestNotificationProducer::BindProvider(
mojo::PendingRemote<global_media_controls::mojom::DevicePickerProvider>
pending_provider) {
provider_.Bind(std::move(pending_provider));
provider_->AddObserver(observer_receiver_.BindNewPipeAndPassRemote());
provider_.set_disconnect_handler(base::BindOnce(
[](PresentationRequestNotificationProducer* self) {
self->item_.reset();
},
base::Unretained(this)));
}
void PresentationRequestNotificationProducer::SetTestPresentationManager(
base::WeakPtr<media_router::WebContentsPresentationManager>
presentation_manager) {
test_presentation_manager_ = presentation_manager;
}
void PresentationRequestNotificationProducer::OnMediaUIOpened() {
is_dialog_open_ = true;
presentation_manager_ = test_presentation_manager_
? test_presentation_manager_
: GetActiveWebContentsPresentationManager();
// It's possible the presentation manager was deleted since the call to
// this method was scheduled.
if (!presentation_manager_) {
return;
}
presentation_manager_->AddObserver(this);
// Handle any request that was created while we weren't watching, first
// making sure the dialog hasn't been closed since the we found out it was
// opening. This is the normal way notifications are created for a default
// presentation request.
if (presentation_manager_->HasDefaultPresentationRequest()) {
OnDefaultPresentationChanged(
&presentation_manager_->GetDefaultPresentationRequest());
}
}
void PresentationRequestNotificationProducer::OnMediaUIClosed() {
is_dialog_open_ = false;
DeleteItemForPresentationRequest("Dialog closed.");
if (presentation_manager_) {
presentation_manager_->RemoveObserver(this);
}
presentation_manager_ = nullptr;
}
void PresentationRequestNotificationProducer::OnMediaUIUpdated() {
ShowOrHideItem();
}
void PresentationRequestNotificationProducer::OnPickerDismissed() {
DeleteItemForPresentationRequest("Dialog closed.");
}
void PresentationRequestNotificationProducer::OnPresentationsChanged(
bool has_presentation) {
// If there is a presentation, there would already be an item associated with
// that, so `this` doesn't have to provide another item.
if (has_presentation && provider_.is_bound()) {
#if !BUILDFLAG(IS_CHROMEOS)
provider_->HideMediaUI();
#endif
provider_->HideItem();
}
}
void PresentationRequestNotificationProducer::OnDefaultPresentationChanged(
const content::PresentationRequest* presentation_request) {
// NOTE: We only observe the presentation manager while the media control
// dialog is open, so this method is only handling the unusual case where
// the default presentation request is changed while the dialog is open.
// In the even more unusual case where the dialog is already open with a
// notification for a non-default request, we ignored changes in the
// default request.
if (!HasItemForNonDefaultRequest()) {
DeleteItemForPresentationRequest("Default presentation changed.");
if (presentation_request) {
CreateItemForPresentationRequest(*presentation_request, nullptr);
}
}
}
void PresentationRequestNotificationProducer::CreateItemForPresentationRequest(
const content::PresentationRequest& request,
std::unique_ptr<media_router::StartPresentationContext> context) {
presentation_request_observer_ =
std::make_unique<PresentationRequestWebContentsObserver>(
GetWebContentsFromPresentationRequest(request), this);
// This may replace an existing item, which is the right thing to do if
// we've reached this point.
item_.emplace(request, std::move(context), provider_);
if (provider_.is_bound()) {
provider_->CreateItem(source_id_);
}
ShowOrHideItem();
}
void PresentationRequestNotificationProducer::DeleteItemForPresentationRequest(
const std::string& message) {
if (!item_) {
return;
}
if (item_->context()) {
item_->context()->InvokeErrorCallback(blink::mojom::PresentationError(
blink::mojom::PresentationErrorType::PRESENTATION_REQUEST_CANCELLED,
message));
}
item_.reset();
presentation_request_observer_.reset();
if (provider_.is_bound()) {
provider_->DeleteItem();
}
}
void PresentationRequestNotificationProducer::ShowOrHideItem() {
if (!item_ || !provider_.is_bound()) {
should_show_ = false;
return;
}
// If the dialog is open and the item is currently hidden, do not show it.
// Otherwise, the item might be shown when the user has dismissed a media
// session notification or a cast notification from the same WebContents.
if (!should_show_ && is_dialog_open_) {
return;
}
auto* web_contents = GetWebContentsFromPresentationRequest(item_->request());
bool new_visibility =
web_contents && !has_active_notifications_callback_.Run(web_contents);
if (should_show_ == new_visibility) {
return;
}
should_show_ = new_visibility;
if (should_show_) {
provider_->ShowItem();
} else {
provider_->HideItem();
}
}
|