2017-11-21 19:33:16 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "AutoplayPolicy.h"
|
|
|
|
|
|
|
|
#include "mozilla/EventStateManager.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "mozilla/dom/HTMLMediaElement.h"
|
2018-02-15 06:23:32 +03:00
|
|
|
#include "mozilla/dom/HTMLMediaElementBinding.h"
|
2017-11-21 19:33:16 +03:00
|
|
|
#include "nsIDocument.h"
|
2018-02-08 06:05:46 +03:00
|
|
|
#include "MediaManager.h"
|
2017-11-21 19:33:16 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2018-05-03 08:39:36 +03:00
|
|
|
/* static */ bool
|
|
|
|
AutoplayPolicy::IsDocumentAllowedToPlay(nsIDocument* aDoc)
|
|
|
|
{
|
|
|
|
return aDoc ? aDoc->HasBeenUserActivated() : false;
|
|
|
|
}
|
|
|
|
|
2017-11-21 19:33:16 +03:00
|
|
|
/* static */ bool
|
|
|
|
AutoplayPolicy::IsMediaElementAllowedToPlay(NotNull<HTMLMediaElement*> aElement)
|
|
|
|
{
|
|
|
|
if (Preferences::GetBool("media.autoplay.enabled")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-08 06:05:46 +03:00
|
|
|
// Pages which have been granted permission to capture WebRTC camera or
|
|
|
|
// microphone are assumed to be trusted, and are allowed to autoplay.
|
|
|
|
MediaManager* manager = MediaManager::GetIfExists();
|
|
|
|
if (manager) {
|
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = aElement->OwnerDoc()->GetInnerWindow();
|
|
|
|
if (window && manager->IsActivelyCapturingOrHasAPermission(window->WindowID())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-27 20:57:34 +03:00
|
|
|
// TODO : this old way would be removed when user-gestures-needed becomes
|
|
|
|
// as a default option to block autoplay.
|
2017-11-30 05:50:21 +03:00
|
|
|
if (!Preferences::GetBool("media.autoplay.enabled.user-gestures-needed", false)) {
|
|
|
|
// If elelement is blessed, it would always be allowed to play().
|
|
|
|
return aElement->IsBlessed() ||
|
|
|
|
EventStateManager::IsHandlingUserInput();
|
2018-05-03 08:39:36 +03:00
|
|
|
}
|
2017-11-30 05:50:21 +03:00
|
|
|
|
|
|
|
// 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() &&
|
2018-02-15 06:23:32 +03:00
|
|
|
aElement->ReadyState() >= HTMLMediaElementBinding::HAVE_METADATA &&
|
2017-11-30 05:50:21 +03:00
|
|
|
!aElement->HasAudio()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-03 08:39:36 +03:00
|
|
|
return AutoplayPolicy::IsDocumentAllowedToPlay(aElement->OwnerDoc());
|
2017-11-21 19:33:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
2018-02-15 06:23:32 +03:00
|
|
|
} // namespace mozilla
|