Bug 1415478 - part2 : allow autoplay for non-audible media content and video without audio content. r=jwwang

Per UX spec, we would allow non-audible media (volume 0, muted, video without audio track)
to autoplay.

MozReview-Commit-ID: HKUyt5Jt4sH

--HG--
extra : rebase_source : fa8d1bfd2fb667e974dbe499d7f8215273d4fa10
This commit is contained in:
Alastor Wu 2017-11-24 11:14:26 +08:00
Родитель d5341011c9
Коммит 35a99cd8d3
3 изменённых файлов: 23 добавлений и 19 удалений

Просмотреть файл

@ -670,11 +670,6 @@ void HTMLMediaElement::ReportLoadError(const char* aMsg,
aParamCount);
}
static bool IsAutoplayEnabled()
{
return Preferences::GetBool("media.autoplay.enabled");
}
class HTMLMediaElement::AudioChannelAgentCallback final :
public nsIAudioChannelAgentCallback
{
@ -2434,7 +2429,8 @@ void HTMLMediaElement::UpdatePreloadAction()
PreloadAction nextAction = PRELOAD_UNDEFINED;
// If autoplay is set, or we're playing, we should always preload data,
// as we'll need it to play.
if ((IsAutoplayEnabled() && HasAttr(kNameSpaceID_None, nsGkAtoms::autoplay)) ||
if ((AutoplayPolicy::IsMediaElementAllowedToPlay(WrapNotNull(this)) &&
HasAttr(kNameSpaceID_None, nsGkAtoms::autoplay)) ||
!mPaused)
{
nextAction = HTMLMediaElement::PRELOAD_ENOUGH;
@ -6178,7 +6174,7 @@ bool HTMLMediaElement::CanActivateAutoplay()
// download is controlled by the script and there is no way to evaluate
// MediaDecoder::CanPlayThrough().
if (!IsAutoplayEnabled()) {
if (!AutoplayPolicy::IsMediaElementAllowedToPlay(WrapNotNull(this))) {
return false;
}

Просмотреть файл

@ -7,7 +7,6 @@
#include "AutoplayPolicy.h"
#include "mozilla/EventStateManager.h"
#include "mozilla/NotNull.h"
#include "mozilla/Preferences.h"
#include "mozilla/dom/HTMLMediaElement.h"
#include "nsIDocument.h"
@ -18,10 +17,6 @@ namespace dom {
/* static */ bool
AutoplayPolicy::IsDocumentAllowedToPlay(nsIDocument* aDoc)
{
if (!Preferences::GetBool("media.autoplay.enabled.user-gestures-needed")) {
return true;
}
return aDoc ? aDoc->HasBeenUserActivated() : false;
}
@ -32,13 +27,25 @@ AutoplayPolicy::IsMediaElementAllowedToPlay(NotNull<HTMLMediaElement*> aElement)
return true;
}
if (Preferences::GetBool("media.autoplay.enabled.user-gestures-needed", false)) {
return AutoplayPolicy::IsDocumentAllowedToPlay(aElement->OwnerDoc());
}
// TODO : this old way would be removed when user-gestures-needed becomes
// as a default option to block autoplay.
if (!Preferences::GetBool("media.autoplay.enabled.user-gestures-needed", false)) {
return EventStateManager::IsHandlingUserInput();
}
// Muted content
if (aElement->Volume() == 0.0 || aElement->Muted()) {
return true;
}
// Media has already loaded metadata and doesn't contain audio track
if (aElement->IsVideo() &&
aElement->ReadyState() >= nsIDOMHTMLMediaElement::HAVE_METADATA &&
!aElement->HasAudio()) {
return true;
}
return AutoplayPolicy::IsDocumentAllowedToPlay(aElement->OwnerDoc());
}
} // namespace dom

Просмотреть файл

@ -25,13 +25,14 @@ 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) TODO...
* 2) Muted media content or video without audio content
*/
class AutoplayPolicy
{
public:
static bool IsDocumentAllowedToPlay(nsIDocument* aDoc);
static bool IsMediaElementAllowedToPlay(NotNull<HTMLMediaElement*> aElement);
private:
static bool IsDocumentAllowedToPlay(nsIDocument* aDoc);
};
} // namespace dom