Bug 1336400 - part3 : move all autoplay checks to CanActivateAutoplay(). draft
authorAlastor Wu <alwu@mozilla.com>
Mon, 13 Nov 2017 18:43:55 +0800
changeset 697108 cc5008062cfa9320e95f7a4cc74d2bcb6cb52b93
parent 697107 0875dbcbaa432afd6eaf1d5e450539672b27303f
child 740021 b1fe68dce179179cffc2de36d350da9bfda846d4
push id88894
push useralwu@mozilla.com
push dateMon, 13 Nov 2017 10:44:33 +0000
bugs1336400
milestone58.0a1
Bug 1336400 - part3 : move all autoplay checks to CanActivateAutoplay(). 1. move all checks to CanActivateAutoplay() 2. don't cache the pref's value in advance, it might cause wrong result if user changes pref after media was binded to tree. MozReview-Commit-ID: 3BeOeaq9wGa
dom/html/HTMLMediaElement.cpp
dom/html/HTMLMediaElement.h
--- a/dom/html/HTMLMediaElement.cpp
+++ b/dom/html/HTMLMediaElement.cpp
@@ -3966,17 +3966,16 @@ HTMLMediaElement::HTMLMediaElement(alrea
     mFragmentEnd(-1.0),
     mDefaultPlaybackRate(1.0),
     mPlaybackRate(1.0),
     mPreservesPitch(true),
     mPlayed(new TimeRanges(ToSupports(OwnerDoc()))),
     mCurrentPlayRangeStart(-1.0),
     mLoadedDataFired(false),
     mAutoplaying(true),
-    mAutoplayEnabled(true),
     mPaused(true, *this),
     mStatsShowing(false),
     mAllowCasting(false),
     mIsCasting(false),
     mAudioCaptured(false),
     mPlayingBeforeSeek(false),
     mPausedForInactiveDocumentOrChannel(false),
     mEventDeliveryPaused(false),
@@ -4514,19 +4513,16 @@ nsresult HTMLMediaElement::BindToTree(ns
   nsresult rv = nsGenericHTMLElement::BindToTree(aDocument,
                                                  aParent,
                                                  aBindingParent,
                                                  aCompileEventHandlers);
 
   mUnboundFromTree = false;
 
   if (aDocument) {
-    mAutoplayEnabled =
-      IsAutoplayEnabled() && (!aDocument || !aDocument->IsStaticDocument()) &&
-      !IsEditable();
     // The preload action depends on the value of the autoplay attribute.
     // It's value may have changed, so update it.
     UpdatePreloadAction();
   }
 
   NotifyDecoderActivityChanges();
 
   return rv;
@@ -6101,17 +6097,21 @@ bool HTMLMediaElement::CanActivateAutopl
 {
   // For stream inputs, we activate autoplay on HAVE_NOTHING because
   // this element itself might be blocking the stream from making progress by
   // being paused. We only check that it has data by checking its active state.
   // We also activate autoplay when playing a media source since the data
   // download is controlled by the script and there is no way to evaluate
   // MediaDecoder::CanPlayThrough().
 
-  if (!HasAttr(kNameSpaceID_None, nsGkAtoms::autoplay) || !mAutoplayEnabled) {
+  if (!IsAutoplayEnabled()) {
+    return false;
+  }
+
+  if (!HasAttr(kNameSpaceID_None, nsGkAtoms::autoplay)) {
     return false;
   }
 
   if (!mAutoplaying) {
     return false;
   }
 
   if (IsEditable()) {
@@ -6121,16 +6121,21 @@ bool HTMLMediaElement::CanActivateAutopl
   if (!mPaused) {
     return false;
   }
 
   if (mPausedForInactiveDocumentOrChannel) {
     return false;
   }
 
+  // Static document is used for print preview and printing, should not be autoplay
+  if (OwnerDoc()->IsStaticDocument()) {
+    return false;
+  }
+
   if (mAudioChannelWrapper) {
     // Note: SUSPENDED_PAUSE and SUSPENDED_BLOCK will be merged into one single state.
     if (mAudioChannelWrapper->GetSuspendType() == nsISuspendedTypes::SUSPENDED_PAUSE ||
         mAudioChannelWrapper->GetSuspendType() == nsISuspendedTypes::SUSPENDED_BLOCK ||
         mAudioChannelWrapper->IsPlaybackBlocked()) {
       return false;
     }
   }
--- a/dom/html/HTMLMediaElement.h
+++ b/dom/html/HTMLMediaElement.h
@@ -1542,20 +1542,16 @@ protected:
   // latter case.
   // The 'autoplay' HTML attribute indicates that the video should
   // start playing when loaded. The 'autoplay' attribute of the object
   // is a mirror of the HTML attribute. These are different from this
   // 'mAutoplaying' flag, which indicates whether the current playback
   // is a result of the autoplay attribute.
   bool mAutoplaying;
 
-  // Indicates whether |autoplay| will actually autoplay based on the pref
-  // media.autoplay.enabled
-  bool mAutoplayEnabled;
-
   // Playback of the video is paused either due to calling the
   // 'Pause' method, or playback not yet having started.
   WakeLockBoolWrapper mPaused;
 
   // True if the media statistics are currently being shown by the builtin
   // video controls
   bool mStatsShowing;