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"
|
2018-08-02 03:30:59 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2017-11-21 19:33:16 +03:00
|
|
|
#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
|
|
|
|
2018-08-02 03:30:59 +03:00
|
|
|
mozilla::LazyLogModule gAutoplayPermissionLog("Autoplay");
|
|
|
|
|
|
|
|
#define AUTOPLAY_LOG(msg, ...) \
|
|
|
|
MOZ_LOG(gAutoplayPermissionLog, LogLevel::Debug, (msg, ##__VA_ARGS__))
|
|
|
|
|
|
|
|
static const char*
|
|
|
|
AllowAutoplayToStr(const uint32_t state)
|
|
|
|
{
|
|
|
|
switch (state) {
|
|
|
|
case nsIAutoplay::ALLOWED:
|
|
|
|
return "allowed";
|
|
|
|
case nsIAutoplay::BLOCKED:
|
|
|
|
return "blocked";
|
|
|
|
case nsIAutoplay::PROMPT:
|
|
|
|
return "prompt";
|
|
|
|
default:
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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")) {
|
2018-08-02 03:30:59 +03:00
|
|
|
AUTOPLAY_LOG("Allow autoplay as document has autoplay permission.");
|
2018-04-30 08:40:50 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-22 02:57:24 +03:00
|
|
|
if (approver->HasBeenUserGestureActivated()) {
|
2018-08-02 03:30:59 +03:00
|
|
|
AUTOPLAY_LOG("Allow autoplay as document 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()) {
|
2018-08-02 03:30:59 +03:00
|
|
|
AUTOPLAY_LOG("Allow autoplay as in extension document.");
|
2018-07-10 20:37:33 +03:00
|
|
|
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)
|
|
|
|
{
|
2018-08-02 03:30:59 +03:00
|
|
|
if ((aElement.Volume() == 0.0 || aElement.Muted()) &&
|
|
|
|
Preferences::GetBool("media.autoplay.allow-muted", true)) {
|
|
|
|
AUTOPLAY_LOG("Allow muted media %p to autoplay.", &aElement);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsWindowAllowedToPlay(aElement.OwnerDoc()->GetInnerWindow())) {
|
|
|
|
AUTOPLAY_LOG("Autoplay allowed as activated/whitelisted window, media %p.", &aElement);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aElement.OwnerDoc()->MediaDocumentKind() == nsIDocument::MediaDocumentKind::Video) {
|
|
|
|
AUTOPLAY_LOG("Allow video document %p to autoplay\n", &aElement);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
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-08-02 03:30:59 +03:00
|
|
|
const uint32_t result = IsMediaElementAllowedToPlay(aElement) ?
|
|
|
|
nsIAutoplay::ALLOWED : autoplayDefault;
|
2018-06-26 05:16:13 +03:00
|
|
|
|
2018-08-02 03:30:59 +03:00
|
|
|
AUTOPLAY_LOG("IsAllowedToPlay, mediaElement=%p, isAllowToPlay=%s",
|
|
|
|
&aElement, AllowAutoplayToStr(result));
|
|
|
|
return result;
|
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
|