Bug 1466926 - part1 : always allow extension background script to autoplay. draft
authoralwu <alwu@mozilla.com>
Tue, 10 Jul 2018 10:37:33 -0700
changeset 816205 1bd66591c88ce825dfba281a35617e2b40b93e56
parent 813581 a0e47ebc4c06e652b919dabee711fdbd6bfd31b5
push id115775
push userbmo:alwu@mozilla.com
push dateTue, 10 Jul 2018 18:16:53 +0000
bugs1466926
milestone63.0a1
Bug 1466926 - part1 : always allow extension background script to autoplay. MozReview-Commit-ID: 9BfWgll7PNx
dom/base/nsDocument.cpp
dom/base/nsIDocument.h
dom/media/AutoplayPolicy.cpp
--- a/dom/base/nsDocument.cpp
+++ b/dom/base/nsDocument.cpp
@@ -12464,16 +12464,22 @@ nsIDocument::HasBeenUserGestureActivated
       break;
     }
     doc = doc->GetSameTypeParentDocument();
   }
 
   return mUserGestureActivated;
 }
 
+bool
+nsIDocument::IsDocumentFromWebExtension() const
+{
+  return !!(BasePrincipal::Cast(NodePrincipal())->AddonPolicy());
+}
+
 nsIDocument*
 nsIDocument::GetSameTypeParentDocument()
 {
   nsCOMPtr<nsIDocShellTreeItem> current = GetDocShell();
   if (!current) {
     return nullptr;
   }
 
--- a/dom/base/nsIDocument.h
+++ b/dom/base/nsIDocument.h
@@ -3382,16 +3382,20 @@ public:
   // permit autoplay audible media. This also gesture activates all other
   // content documents in this tab.
   void NotifyUserGestureActivation();
 
   // Return true if NotifyUserGestureActivation() has been called on any
   // document in the document tree.
   bool HasBeenUserGestureActivated();
 
+  // This document is running on the background page which is used specifically
+  // for the extension.
+  bool IsDocumentFromWebExtension() const;
+
   bool HasScriptsBlockedBySandbox();
 
   bool InlineScriptAllowedByCSP();
 
   void ReportHasScrollLinkedEffect();
   bool HasScrollLinkedEffect() const
   {
     return mHasScrollLinkedEffect;
--- a/dom/media/AutoplayPolicy.cpp
+++ b/dom/media/AutoplayPolicy.cpp
@@ -65,16 +65,21 @@ IsWindowAllowedToPlay(nsPIDOMWindowInner
     return true;
   }
 
   if (approver->HasBeenUserGestureActivated()) {
     // Document has been activated by user gesture.
     return true;
   }
 
+  if (approver->IsDocumentFromWebExtension()) {
+    // Always allow extension background script to autoplay.
+    return true;
+  }
+
   return false;
 }
 
 /* static */
 already_AddRefed<AutoplayRequest>
 AutoplayPolicy::RequestFor(const nsIDocument& aDocument)
 {
   nsIDocument* document = ApproverDocOf(aDocument);