Bug 1480281 - part1 : add autoplay debug log module.
Add new log module which allow us to debug by using "MOZ_LOG=Autoplay:5".
MozReview-Commit-ID: 9CG5JyCw21G
--- a/dom/html/HTMLMediaElement.cpp
+++ b/dom/html/HTMLMediaElement.cpp
@@ -122,16 +122,20 @@
#include "xpcpublic.h"
#include <algorithm>
#include <cmath>
#include <limits>
mozilla::LazyLogModule gMediaElementLog("nsMediaElement");
static mozilla::LazyLogModule gMediaElementEventsLog("nsMediaElementEvents");
+extern mozilla::LazyLogModule gAutoplayPermissionLog;
+#define AUTOPLAY_LOG(msg, ...) \
+ MOZ_LOG(gAutoplayPermissionLog, LogLevel::Debug, (msg, ##__VA_ARGS__))
+
#define LOG(type, msg) MOZ_LOG(gMediaElementLog, type, msg)
#define LOG_EVENT(type, msg) MOZ_LOG(gMediaElementEventsLog, type, msg)
using namespace mozilla::layers;
using mozilla::net::nsMediaFragmentURIParser;
using namespace mozilla::dom::HTMLMediaElement_Binding;
namespace mozilla {
@@ -3057,16 +3061,17 @@ HTMLMediaElement::SetMutedInternal(uint3
void
HTMLMediaElement::PauseIfShouldNotBePlaying()
{
if (GetPaused()) {
return;
}
if (AutoplayPolicy::IsAllowedToPlay(*this) != nsIAutoplay::ALLOWED) {
+ AUTOPLAY_LOG("pause because not allowed to play, element=%p", this);
ErrorResult rv;
Pause(rv);
OwnerDoc()->SetDocTreeHadPlayRevoked();
}
}
void
HTMLMediaElement::SetVolumeInternal()
--- a/dom/media/AutoplayPolicy.cpp
+++ b/dom/media/AutoplayPolicy.cpp
@@ -2,29 +2,50 @@
/* 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/Logging.h"
#include "mozilla/Preferences.h"
#include "mozilla/dom/AudioContext.h"
#include "mozilla/AutoplayPermissionManager.h"
#include "mozilla/dom/HTMLMediaElement.h"
#include "mozilla/dom/HTMLMediaElementBinding.h"
#include "nsIAutoplay.h"
#include "nsContentUtils.h"
#include "nsIDocument.h"
#include "MediaManager.h"
#include "nsIDocShell.h"
#include "nsIDocShellTreeItem.h"
#include "nsPIDOMWindow.h"
+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";
+ }
+}
+
namespace mozilla {
namespace dom {
static nsIDocument*
ApproverDocOf(const nsIDocument& aDocument)
{
nsCOMPtr<nsIDocShell> ds = aDocument.GetDocShell();
if (!ds) {
@@ -57,27 +78,27 @@ IsWindowAllowedToPlay(nsPIDOMWindowInner
if (!aWindow->GetExtantDoc()) {
return false;
}
nsIDocument* approver = ApproverDocOf(*aWindow->GetExtantDoc());
if (nsContentUtils::IsExactSitePermAllow(approver->NodePrincipal(),
"autoplay-media")) {
- // Autoplay permission has been granted already.
+ AUTOPLAY_LOG("Allow autoplay as document has autoplay permission.");
return true;
}
if (approver->HasBeenUserGestureActivated()) {
- // Document has been activated by user gesture.
+ AUTOPLAY_LOG("Allow autoplay as document activated by user gesture.");
return true;
}
if (approver->IsExtensionPage()) {
- // Always allow extension page to autoplay.
+ AUTOPLAY_LOG("Allow autoplay as in extension document.");
return true;
}
return false;
}
/* static */
already_AddRefed<AutoplayPermissionManager>
@@ -103,20 +124,33 @@ DefaultAutoplayBehaviour()
return nsIAutoplay::ALLOWED;
}
return prefValue;
}
static bool
IsMediaElementAllowedToPlay(const HTMLMediaElement& aElement)
{
- return ((aElement.Volume() == 0.0 || aElement.Muted()) &&
- Preferences::GetBool("media.autoplay.allow-muted", true)) ||
- IsWindowAllowedToPlay(aElement.OwnerDoc()->GetInnerWindow()) ||
- (aElement.OwnerDoc()->MediaDocumentKind() == nsIDocument::MediaDocumentKind::Video);
+ 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;
}
/* static */ bool
AutoplayPolicy::WouldBeAllowedToPlayIfAutoplayDisabled(const HTMLMediaElement& aElement)
{
return IsMediaElementAllowedToPlay(aElement);
}
@@ -129,21 +163,22 @@ AutoplayPolicy::IsAllowedToPlay(const HT
if (!Preferences::GetBool("media.autoplay.enabled.user-gestures-needed", false)) {
// If element is blessed, it would always be allowed to play().
return (autoplayDefault == nsIAutoplay::ALLOWED ||
aElement.IsBlessed() ||
EventStateManager::IsHandlingUserInput())
? nsIAutoplay::ALLOWED : nsIAutoplay::BLOCKED;
}
- if (IsMediaElementAllowedToPlay(aElement)) {
- return nsIAutoplay::ALLOWED;
- }
+ const uint32_t result = IsMediaElementAllowedToPlay(aElement) ?
+ nsIAutoplay::ALLOWED : autoplayDefault;
- return autoplayDefault;
+ AUTOPLAY_LOG("IsAllowedToPlay, mediaElement=%p, isAllowToPlay=%s",
+ &aElement, AllowAutoplayToStr(result));
+ return result;
}
/* static */ bool
AutoplayPolicy::IsAudioContextAllowedToPlay(NotNull<AudioContext*> aContext)
{
if (!Preferences::GetBool("media.autoplay.block-webaudio", false)) {
return true;
}