AutoplayPolicy.h (2368B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #if !defined(AutoplayPolicy_h_) 8 # define AutoplayPolicy_h_ 9 10 # include <cstdint> 11 12 class nsIPrincipal; 13 14 namespace mozilla::dom { 15 16 class HTMLMediaElement; 17 class AudioContext; 18 class Document; 19 enum class AutoplayPolicy : uint8_t; 20 enum class AutoplayPolicyMediaType : uint8_t; 21 22 } // namespace mozilla::dom 23 24 namespace mozilla::media { 25 /** 26 * AutoplayPolicy is used to manage autoplay logic for all kinds of media, 27 * including MediaElement, Web Audio and Web Speech. 28 * 29 * Autoplay could be disable by setting the pref "media.autoplay.default" 30 * to anything but nsIAutoplay::Allowed. Once user disables autoplay, media 31 * could only be played if one of following conditions is true. 32 * 1) Owner document is activated by user gestures 33 * We restrict user gestures to "mouse click", "keyboard press" and "touch". 34 * 2) Muted media content or video without audio content. 35 * 3) Document's origin has the "autoplay-media" permission. 36 */ 37 class AutoplayPolicy { 38 public: 39 // Returns whether a given media element is allowed to play. 40 static bool IsAllowedToPlay(const dom::HTMLMediaElement& aElement); 41 42 // Returns whether a given AudioContext is allowed to play. 43 static bool IsAllowedToPlay(const dom::AudioContext& aContext); 44 45 // Return the value of the autoplay permission for given principal. The return 46 // value can be 0=unknown, 1=allow, 2=block audio, 5=block audio and video. 47 static uint32_t GetSiteAutoplayPermission(nsIPrincipal* aPrincipal); 48 49 // Following methods are used for the internal implementation for the Autoplay 50 // Policy Detection API, the public JS interfaces are in exposed on Navigator. 51 // https://w3c.github.io/autoplay/#autoplay-detection-methods 52 static dom::AutoplayPolicy GetAutoplayPolicy( 53 const dom::HTMLMediaElement& aElement); 54 55 static dom::AutoplayPolicy GetAutoplayPolicy( 56 const dom::AudioContext& aContext); 57 58 static dom::AutoplayPolicy GetAutoplayPolicy( 59 const dom::AutoplayPolicyMediaType& aType, const dom::Document& aDoc); 60 }; 61 62 } // namespace mozilla::media 63 64 #endif