Commit ed8a27f0 authored by Chris Pearce's avatar Chris Pearce
Browse files

Bug 1457048 - Ensure origins with autoplay-media exact permission can autoplay. r=bryce,johannh

Sites which are whitelisted should be allowed to autoplay audible media.
So check whether a HTMLMediaElement's owner doc's principal has an exact
"autoplay-media" permission. This ensures whitelisted origins can autoplay,
but sub-origins of whitelisted origins need their own permission.

MozReview-Commit-ID: 2IO5KIyplEa

--HG--
extra : rebase_source : 4a974aba0533bfbd5e9bb4c4c11d77d17a81db6d
parent 70ec3d2f
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -600,6 +600,16 @@ var gPermissionObject = {
   *    don't want to expose a "Hide Prompt" button to the user through pageinfo.
   */

  "autoplay-media": {
    exactHostMatch: true,
    getDefault() {
      if (Services.prefs.getBoolPref("media.autoplay.enabled")) {
        return SitePermissions.ALLOW;
      }
      return SitePermissions.BLOCK;
    }
  },

  "image": {
    states: [ SitePermissions.ALLOW, SitePermissions.BLOCK ],
  },
+14 −8
Original line number Diff line number Diff line
@@ -10,18 +10,13 @@
#include "mozilla/Preferences.h"
#include "mozilla/dom/HTMLMediaElement.h"
#include "mozilla/dom/HTMLMediaElementBinding.h"
#include "nsContentUtils.h"
#include "nsIDocument.h"
#include "MediaManager.h"

namespace mozilla {
namespace dom {

/* static */ bool
AutoplayPolicy::IsDocumentAllowedToPlay(nsIDocument* aDoc)
{
  return aDoc ? aDoc->HasBeenUserActivated() : false;
}

/* static */ bool
AutoplayPolicy::IsMediaElementAllowedToPlay(NotNull<HTMLMediaElement*> aElement)
{
@@ -59,7 +54,18 @@ AutoplayPolicy::IsMediaElementAllowedToPlay(NotNull<HTMLMediaElement*> aElement)
    return true;
  }

  return AutoplayPolicy::IsDocumentAllowedToPlay(aElement->OwnerDoc());
  // Whitelisted.
  if (nsContentUtils::IsExactSitePermAllow(
        aElement->NodePrincipal(), "autoplay-media")) {
    return true;
  }

  // Activated by user gesture.
  if (aElement->OwnerDoc()->HasBeenUserActivated()) {
    return true;
  }

  return false;
}

} // namespace dom
+2 −3
Original line number Diff line number Diff line
@@ -25,14 +25,13 @@ class HTMLMediaElement;
 * conditions is true.
 * 1) Owner document is activated by user gestures
 *    We restrict user gestures to "mouse click", "keyboard press" and "touch".
 * 2) Muted media content or video without audio content
 * 2) Muted media content or video without audio content.
 * 3) Document's origin has the "autoplay-media" permission.
 */
class AutoplayPolicy
{
public:
  static bool IsMediaElementAllowedToPlay(NotNull<HTMLMediaElement*> aElement);
private:
  static bool IsDocumentAllowedToPlay(nsIDocument* aDoc);
};

} // namespace dom