2017-11-21 19:33:16 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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 "AutoplayPolicy.h"
|
|
|
|
|
|
|
|
#include "mozilla/EventStateManager.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
2017-12-13 23:05:35 +03:00
|
|
|
#include "mozilla/dom/AudioContext.h"
|
2018-07-06 12:15:20 +03:00
|
|
|
#include "mozilla/AutoplayPermissionManager.h"
|
2017-11-21 19:33:16 +03:00
|
|
|
#include "mozilla/dom/HTMLMediaElement.h"
|
2018-02-15 06:23:32 +03:00
|
|
|
#include "mozilla/dom/HTMLMediaElementBinding.h"
|
2018-06-29 16:14:33 +03:00
|
|
|
#include "nsIAutoplay.h"
|
2018-04-30 08:40:50 +03:00
|
|
|
#include "nsContentUtils.h"
|
2017-11-21 19:33:16 +03:00
|
|
|
#include "nsIDocument.h"
|
2018-02-08 06:05:46 +03:00
|
|
|
#include "MediaManager.h"
|
2018-06-22 02:57:24 +03:00
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsIDocShellTreeItem.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
2017-11-21 19:33:16 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2018-06-22 02:57:24 +03:00
|
|
|
static nsIDocument*
|
|
|
|
ApproverDocOf(const nsIDocument& aDocument)
|
2017-11-21 19:33:16 +03:00
|
|
|
{
|
2018-06-22 02:57:24 +03:00
|
|
|
nsCOMPtr<nsIDocShell> ds = aDocument.GetDocShell();
|
|
|
|
if (!ds) {
|
|
|
|
return nullptr;
|
2017-11-21 19:33:16 +03:00
|
|
|
}
|
|
|
|
|
2018-06-22 02:57:24 +03:00
|
|
|
nsCOMPtr<nsIDocShellTreeItem> rootTreeItem;
|
|
|
|
ds->GetSameTypeRootTreeItem(getter_AddRefs(rootTreeItem));
|
|
|
|
if (!rootTreeItem) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rootTreeItem->GetDocument();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2018-06-26 05:16:13 +03:00
|
|
|
IsWindowAllowedToPlay(nsPIDOMWindowInner* aWindow)
|
2018-06-22 02:57:24 +03:00
|
|
|
{
|
|
|
|
if (!aWindow) {
|
|
|
|
return false;
|
2018-05-18 04:43:00 +03:00
|
|
|
}
|
|
|
|
|
2018-02-08 06:05:46 +03:00
|
|
|
// Pages which have been granted permission to capture WebRTC camera or
|
|
|
|
// microphone are assumed to be trusted, and are allowed to autoplay.
|
|
|
|
MediaManager* manager = MediaManager::GetIfExists();
|
2018-06-22 02:57:24 +03:00
|
|
|
if (manager &&
|
|
|
|
manager->IsActivelyCapturingOrHasAPermission(aWindow->WindowID())) {
|
|
|
|
return true;
|
2018-02-08 06:05:46 +03:00
|
|
|
}
|
|
|
|
|
2018-06-22 02:57:24 +03:00
|
|
|
if (!aWindow->GetExtantDoc()) {
|
|
|
|
return false;
|
2017-11-30 05:50:21 +03:00
|
|
|
}
|
|
|
|
|
2018-06-22 02:57:24 +03:00
|
|
|
nsIDocument* approver = ApproverDocOf(*aWindow->GetExtantDoc());
|
|
|
|
if (nsContentUtils::IsExactSitePermAllow(approver->NodePrincipal(),
|
|
|
|
"autoplay-media")) {
|
|
|
|
// Autoplay permission has been granted already.
|
2018-04-30 08:40:50 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-22 02:57:24 +03:00
|
|
|
if (approver->HasBeenUserGestureActivated()) {
|
|
|
|
// Document has been activated by user gesture.
|
2018-04-30 08:40:50 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-10 20:37:33 +03:00
|
|
|
if (approver->IsExtensionPage()) {
|
|
|
|
// Always allow extension page to autoplay.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-30 08:40:50 +03:00
|
|
|
return false;
|
2017-11-21 19:33:16 +03:00
|
|
|
}
|
|
|
|
|
2018-06-22 02:57:24 +03:00
|
|
|
/* static */
|
2018-07-06 12:15:20 +03:00
|
|
|
already_AddRefed<AutoplayPermissionManager>
|
2018-06-22 02:57:24 +03:00
|
|
|
AutoplayPolicy::RequestFor(const nsIDocument& aDocument)
|
|
|
|
{
|
|
|
|
nsIDocument* document = ApproverDocOf(aDocument);
|
|
|
|
if (!document) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
nsPIDOMWindowInner* window = document->GetInnerWindow();
|
|
|
|
if (!window) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-07-06 12:15:20 +03:00
|
|
|
return window->GetAutoplayPermissionManager();
|
2018-06-22 02:57:24 +03:00
|
|
|
}
|
|
|
|
|
2018-06-29 16:14:33 +03:00
|
|
|
static uint32_t
|
|
|
|
DefaultAutoplayBehaviour()
|
2017-12-13 23:05:35 +03:00
|
|
|
{
|
2018-06-29 16:14:33 +03:00
|
|
|
int prefValue = Preferences::GetInt("media.autoplay.default", nsIAutoplay::ALLOWED);
|
|
|
|
if (prefValue < nsIAutoplay::ALLOWED || prefValue > nsIAutoplay::PROMPT) {
|
|
|
|
// Invalid pref values are just converted to ALLOWED.
|
|
|
|
return nsIAutoplay::ALLOWED;
|
2017-12-13 23:05:35 +03:00
|
|
|
}
|
2018-06-29 16:14:33 +03:00
|
|
|
return prefValue;
|
|
|
|
}
|
2017-12-13 23:05:35 +03:00
|
|
|
|
2018-07-18 06:34:04 +03:00
|
|
|
static bool
|
|
|
|
IsMediaElementAllowedToPlay(const HTMLMediaElement& aElement)
|
|
|
|
{
|
|
|
|
return ((aElement.Volume() == 0.0 || aElement.Muted()) &&
|
2018-07-23 21:17:00 +03:00
|
|
|
Preferences::GetBool("media.autoplay.allow-muted", true)) ||
|
|
|
|
IsWindowAllowedToPlay(aElement.OwnerDoc()->GetInnerWindow()) ||
|
|
|
|
(aElement.OwnerDoc()->MediaDocumentKind() == nsIDocument::MediaDocumentKind::Video);
|
2018-07-18 06:34:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ bool
|
|
|
|
AutoplayPolicy::WouldBeAllowedToPlayIfAutoplayDisabled(const HTMLMediaElement& aElement)
|
|
|
|
{
|
|
|
|
return IsMediaElementAllowedToPlay(aElement);
|
|
|
|
}
|
|
|
|
|
2018-06-29 16:14:33 +03:00
|
|
|
/* static */ uint32_t
|
|
|
|
AutoplayPolicy::IsAllowedToPlay(const HTMLMediaElement& aElement)
|
|
|
|
{
|
|
|
|
const uint32_t autoplayDefault = DefaultAutoplayBehaviour();
|
2018-06-22 02:57:24 +03:00
|
|
|
// TODO : this old way would be removed when user-gestures-needed becomes
|
|
|
|
// as a default option to block autoplay.
|
2017-12-13 23:05:35 +03:00
|
|
|
if (!Preferences::GetBool("media.autoplay.enabled.user-gestures-needed", false)) {
|
2018-06-22 02:57:24 +03:00
|
|
|
// If element is blessed, it would always be allowed to play().
|
2018-06-29 16:14:33 +03:00
|
|
|
return (autoplayDefault == nsIAutoplay::ALLOWED ||
|
|
|
|
aElement.IsBlessed() ||
|
|
|
|
EventStateManager::IsHandlingUserInput())
|
|
|
|
? nsIAutoplay::ALLOWED : nsIAutoplay::BLOCKED;
|
2017-12-13 23:05:35 +03:00
|
|
|
}
|
|
|
|
|
2018-07-18 06:34:04 +03:00
|
|
|
if (IsMediaElementAllowedToPlay(aElement)) {
|
2018-06-29 16:14:33 +03:00
|
|
|
return nsIAutoplay::ALLOWED;
|
2018-06-26 05:16:13 +03:00
|
|
|
}
|
|
|
|
|
2018-06-29 16:14:33 +03:00
|
|
|
return autoplayDefault;
|
2018-06-22 02:57:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ bool
|
|
|
|
AutoplayPolicy::IsAudioContextAllowedToPlay(NotNull<AudioContext*> aContext)
|
|
|
|
{
|
2018-07-16 07:08:49 +03:00
|
|
|
if (!Preferences::GetBool("media.autoplay.block-webaudio", false)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-29 16:14:33 +03:00
|
|
|
if (DefaultAutoplayBehaviour() == nsIAutoplay::ALLOWED) {
|
2018-06-22 02:57:24 +03:00
|
|
|
return true;
|
2018-04-27 20:13:40 +03:00
|
|
|
}
|
|
|
|
|
2018-06-22 02:57:24 +03:00
|
|
|
if (!Preferences::GetBool("media.autoplay.enabled.user-gestures-needed", false)) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-12-13 23:05:35 +03:00
|
|
|
|
2018-06-22 02:57:24 +03:00
|
|
|
// Offline context won't directly output sound to audio devices.
|
|
|
|
if (aContext->IsOffline()) {
|
2017-12-13 23:05:35 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-26 05:16:13 +03:00
|
|
|
if (IsWindowAllowedToPlay(aContext->GetOwner())) {
|
2017-12-13 23:05:35 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-21 19:33:16 +03:00
|
|
|
} // namespace dom
|
2018-02-15 06:23:32 +03:00
|
|
|
} // namespace mozilla
|