Bug 1520088 - part1 : let 'IsMediaElementAllowedToPlay()' only check media element's attributes r=cpearce
authorAlastor Wu <alwu@mozilla.com>
Wed, 16 Jan 2019 01:04:27 +0000
changeset 514131 eb086e6c019f
parent 514130 8d7abfb509e9
child 514132 1d2fcf021b32
push id1953
push userffxbld-merge
push dateMon, 11 Mar 2019 12:10:20 +0000
treeherdermozilla-release@9c35dcbaa899 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerscpearce
bugs1520088
milestone66.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1520088 - part1 : let 'IsMediaElementAllowedToPlay()' only check media element's attributes r=cpearce We would like to have different phases checking for autoplay, the first phase is to check media element itself, so we need to move other non-related checkings out from 'IsMediaElementAllowedToPlay()'. Differential Revision: https://phabricator.services.mozilla.com/D16523
dom/media/AutoplayPolicy.cpp
--- a/dom/media/AutoplayPolicy.cpp
+++ b/dom/media/AutoplayPolicy.cpp
@@ -103,16 +103,21 @@ static bool IsWindowAllowedToPlay(nsPIDO
     return true;
   }
 
   if (approver->IsExtensionPage()) {
     AUTOPLAY_LOG("Allow autoplay as in extension document.");
     return true;
   }
 
+  if (approver->MediaDocumentKind() == Document::MediaDocumentKind::Video) {
+    AUTOPLAY_LOG("Allow video document to autoplay.");
+    return true;
+  }
+
   return false;
 }
 
 static uint32_t DefaultAutoplayBehaviour() {
   int prefValue =
       Preferences::GetInt("media.autoplay.default", nsIAutoplay::ALLOWED);
   if (prefValue < nsIAutoplay::ALLOWED || prefValue > nsIAutoplay::BLOCKED) {
     // Invalid pref values are just converted to BLOCKED.
@@ -124,29 +129,16 @@ static uint32_t DefaultAutoplayBehaviour
 static bool IsMediaElementAllowedToPlay(const HTMLMediaElement& aElement) {
   const bool isAllowedMuted =
       Preferences::GetBool("media.autoplay.allow-muted", true);
   if ((aElement.Volume() == 0.0 || aElement.Muted()) && isAllowedMuted) {
     AUTOPLAY_LOG("Allow muted media %p to autoplay.", &aElement);
     return true;
   }
 
-  if (IsWindowAllowedToPlay(aElement.OwnerDoc()->GetInnerWindow())) {
-    AUTOPLAY_LOG("Autoplay allowed as window is allowed to play, media %p.",
-                 &aElement);
-    return true;
-  }
-
-  Document* topDocument = ApproverDocOf(*aElement.OwnerDoc());
-  if (topDocument &&
-      topDocument->MediaDocumentKind() == Document::MediaDocumentKind::Video) {
-    AUTOPLAY_LOG("Allow video document %p to autoplay", &aElement);
-    return true;
-  }
-
   if (!aElement.HasAudio() &&
       aElement.ReadyState() >= HTMLMediaElement_Binding::HAVE_METADATA &&
       isAllowedMuted) {
     AUTOPLAY_LOG("Allow media %p without audio track to autoplay", &aElement);
     return true;
   }
 
   return false;
@@ -162,17 +154,18 @@ static bool IsEnableBlockingWebAudioByUs
   return DefaultAutoplayBehaviour() != nsIAutoplay::ALLOWED &&
          Preferences::GetBool("media.autoplay.block-webaudio", false) &&
          Preferences::GetBool("media.autoplay.enabled.user-gestures-needed",
                               false);
 }
 
 /* static */ bool AutoplayPolicy::WouldBeAllowedToPlayIfAutoplayDisabled(
     const HTMLMediaElement& aElement) {
-  return IsMediaElementAllowedToPlay(aElement);
+  return IsMediaElementAllowedToPlay(aElement) ||
+         IsWindowAllowedToPlay(aElement.OwnerDoc()->GetInnerWindow());
 }
 
 /* static */ bool AutoplayPolicy::WouldBeAllowedToPlayIfAutoplayDisabled(
     const AudioContext& aContext) {
   return IsAudioContextAllowedToPlay(aContext);
 }
 
 /* static */ bool AutoplayPolicy::IsAllowedToPlay(
@@ -186,16 +179,22 @@ static bool IsEnableBlockingWebAudioByUs
     return (autoplayDefault == nsIAutoplay::ALLOWED || aElement.IsBlessed() ||
             EventStateManager::IsHandlingUserInput());
   }
 
   if (IsMediaElementAllowedToPlay(aElement)) {
     return true;
   }
 
+  if (IsWindowAllowedToPlay(aElement.OwnerDoc()->GetInnerWindow())) {
+    AUTOPLAY_LOG("Autoplay allowed as window is allowed to play, media %p.",
+                 &aElement);
+    return true;
+  }
+
   const bool result = IsMediaElementAllowedToPlay(aElement) ||
                       autoplayDefault == nsIAutoplay::ALLOWED;
 
   AUTOPLAY_LOG("IsAllowedToPlay, mediaElement=%p, isAllowToPlay=%s", &aElement,
                result ? "allowed" : "blocked");
 
   return result;
 }