2017-12-06 01:22:36 +03:00
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "BackgroundVideoDecodingPermissionObserver.h"
|
|
|
|
|
|
|
|
#include "mozilla/AsyncEventDispatcher.h"
|
2020-08-19 13:31:54 +03:00
|
|
|
#include "mozilla/dom/BrowsingContext.h"
|
2019-07-26 04:10:23 +03:00
|
|
|
#include "mozilla/StaticPrefs_media.h"
|
2017-12-06 01:22:36 +03:00
|
|
|
#include "MediaDecoder.h"
|
|
|
|
#include "nsContentUtils.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2017-12-06 01:22:36 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
BackgroundVideoDecodingPermissionObserver::
|
|
|
|
BackgroundVideoDecodingPermissionObserver(MediaDecoder* aDecoder)
|
|
|
|
: mDecoder(aDecoder), mIsRegisteredForEvent(false) {
|
|
|
|
MOZ_ASSERT(mDecoder);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BackgroundVideoDecodingPermissionObserver::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const char16_t* aData) {
|
2019-06-28 07:09:05 +03:00
|
|
|
if (!StaticPrefs::media_resume_bkgnd_video_on_tabhover()) {
|
2017-12-06 01:22:36 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsValidEventSender(aSubject)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(aTopic, "unselected-tab-hover") == 0) {
|
|
|
|
bool allowed = !NS_strcmp(aData, u"true");
|
|
|
|
mDecoder->SetIsBackgroundVideoDecodingAllowed(allowed);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundVideoDecodingPermissionObserver::RegisterEvent() {
|
|
|
|
MOZ_ASSERT(!mIsRegisteredForEvent);
|
|
|
|
nsCOMPtr<nsIObserverService> observerService = services::GetObserverService();
|
|
|
|
if (observerService) {
|
|
|
|
observerService->AddObserver(this, "unselected-tab-hover", false);
|
|
|
|
mIsRegisteredForEvent = true;
|
|
|
|
if (nsContentUtils::IsInStableOrMetaStableState()) {
|
|
|
|
// Events shall not be fired synchronously to prevent anything visible
|
|
|
|
// from the scripts while we are in stable state.
|
2019-01-02 16:05:23 +03:00
|
|
|
if (nsCOMPtr<dom::Document> doc = GetOwnerDoc()) {
|
2017-12-06 01:22:36 +03:00
|
|
|
doc->Dispatch(
|
|
|
|
TaskCategory::Other,
|
|
|
|
NewRunnableMethod(
|
|
|
|
"BackgroundVideoDecodingPermissionObserver::"
|
|
|
|
"EnableEvent",
|
|
|
|
this, &BackgroundVideoDecodingPermissionObserver::EnableEvent));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
EnableEvent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundVideoDecodingPermissionObserver::UnregisterEvent() {
|
|
|
|
MOZ_ASSERT(mIsRegisteredForEvent);
|
|
|
|
nsCOMPtr<nsIObserverService> observerService = services::GetObserverService();
|
|
|
|
if (observerService) {
|
|
|
|
observerService->RemoveObserver(this, "unselected-tab-hover");
|
|
|
|
mIsRegisteredForEvent = false;
|
|
|
|
mDecoder->SetIsBackgroundVideoDecodingAllowed(false);
|
|
|
|
if (nsContentUtils::IsInStableOrMetaStableState()) {
|
|
|
|
// Events shall not be fired synchronously to prevent anything visible
|
|
|
|
// from the scripts while we are in stable state.
|
2019-01-02 16:05:23 +03:00
|
|
|
if (nsCOMPtr<dom::Document> doc = GetOwnerDoc()) {
|
2017-12-06 01:22:36 +03:00
|
|
|
doc->Dispatch(
|
|
|
|
TaskCategory::Other,
|
|
|
|
NewRunnableMethod(
|
|
|
|
"BackgroundVideoDecodingPermissionObserver::"
|
|
|
|
"DisableEvent",
|
|
|
|
this,
|
|
|
|
&BackgroundVideoDecodingPermissionObserver::DisableEvent));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DisableEvent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BackgroundVideoDecodingPermissionObserver::
|
|
|
|
~BackgroundVideoDecodingPermissionObserver() {
|
|
|
|
MOZ_ASSERT(!mIsRegisteredForEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundVideoDecodingPermissionObserver::EnableEvent() const {
|
2020-08-19 13:31:54 +03:00
|
|
|
// If we can't get document or outer window, then you can't reach the chrome
|
|
|
|
// <browser> either, so we don't need want to dispatch the event.
|
2019-01-02 16:05:23 +03:00
|
|
|
dom::Document* doc = GetOwnerDoc();
|
2020-08-19 13:31:54 +03:00
|
|
|
if (!doc || !doc->GetWindow()) {
|
2018-08-08 22:00:03 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-06 01:22:36 +03:00
|
|
|
RefPtr<AsyncEventDispatcher> asyncDispatcher =
|
|
|
|
new AsyncEventDispatcher(doc, u"UnselectedTabHover:Enable"_ns,
|
2018-06-25 19:23:50 +03:00
|
|
|
CanBubble::eYes, ChromeOnlyDispatch::eYes);
|
2017-12-06 01:22:36 +03:00
|
|
|
asyncDispatcher->PostDOMEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackgroundVideoDecodingPermissionObserver::DisableEvent() const {
|
2020-08-19 13:31:54 +03:00
|
|
|
// If we can't get document or outer window, then you can't reach the chrome
|
|
|
|
// <browser> either, so we don't need want to dispatch the event.
|
2019-01-02 16:05:23 +03:00
|
|
|
dom::Document* doc = GetOwnerDoc();
|
2020-08-19 13:31:54 +03:00
|
|
|
if (!doc || !doc->GetWindow()) {
|
2018-08-08 22:00:03 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-06 01:22:36 +03:00
|
|
|
RefPtr<AsyncEventDispatcher> asyncDispatcher =
|
|
|
|
new AsyncEventDispatcher(doc, u"UnselectedTabHover:Disable"_ns,
|
2018-06-25 19:23:50 +03:00
|
|
|
CanBubble::eYes, ChromeOnlyDispatch::eYes);
|
2017-12-06 01:22:36 +03:00
|
|
|
asyncDispatcher->PostDOMEvent();
|
|
|
|
}
|
|
|
|
|
2020-08-19 13:31:54 +03:00
|
|
|
dom::BrowsingContext* BackgroundVideoDecodingPermissionObserver::GetOwnerBC()
|
|
|
|
const {
|
2019-01-02 16:05:23 +03:00
|
|
|
dom::Document* doc = GetOwnerDoc();
|
2020-08-19 13:31:54 +03:00
|
|
|
return doc ? doc->GetBrowsingContext() : nullptr;
|
2017-12-06 01:22:36 +03:00
|
|
|
}
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
dom::Document* BackgroundVideoDecodingPermissionObserver::GetOwnerDoc() const {
|
2017-12-06 01:22:36 +03:00
|
|
|
if (!mDecoder->GetOwner()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mDecoder->GetOwner()->GetDocument();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BackgroundVideoDecodingPermissionObserver::IsValidEventSender(
|
|
|
|
nsISupports* aSubject) const {
|
|
|
|
nsCOMPtr<nsPIDOMWindowInner> senderInner(do_QueryInterface(aSubject));
|
|
|
|
if (!senderInner) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-19 13:31:54 +03:00
|
|
|
RefPtr<dom::BrowsingContext> senderBC = senderInner->GetBrowsingContext();
|
|
|
|
if (!senderBC) {
|
2017-12-06 01:22:36 +03:00
|
|
|
return false;
|
|
|
|
}
|
2020-08-19 13:31:54 +03:00
|
|
|
// Valid sender should be in the same browsing context tree as where owner is.
|
|
|
|
return GetOwnerBC() ? GetOwnerBC()->Top() == senderBC->Top() : false;
|
2017-12-06 01:22:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(BackgroundVideoDecodingPermissionObserver, nsIObserver)
|
|
|
|
|
|
|
|
} // namespace mozilla
|