Bug 1593843 - part8 : move helper functions out from 'AutoplayPolicy'. r=bryce

Move helper functions out from `AutoplayPolicy`, makes us clearly know which one could really be used to determine the blocking autoplay result. It also give other classes an ability to use those helper functions if they have a need.

Differential Revision: https://phabricator.services.mozilla.com/D52432

--HG--
extra : moz-landing-system : lando
This commit is contained in:
alwu 2019-11-25 11:00:13 +00:00
Родитель b3fa60b9f3
Коммит 54b8cc7893
4 изменённых файлов: 27 добавлений и 16 удалений

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

@ -4143,7 +4143,8 @@ void HTMLMediaElement::UpdateHadAudibleAutoplayState() {
if ((Volume() > 0.0 && !Muted()) &&
(!OwnerDoc()->HasBeenUserGestureActivated() || Autoplay())) {
OwnerDoc()->SetDocTreeHadAudibleMedia();
if (AutoplayPolicy::WouldBeAllowedToPlayIfAutoplayDisabled(*this)) {
if (AutoplayPolicyTelemetryUtils::WouldBeAllowedToPlayIfAutoplayDisabled(
*this)) {
ScalarAdd(Telemetry::ScalarID::MEDIA_AUTOPLAY_WOULD_BE_ALLOWED_COUNT, 1);
} else {
ScalarAdd(Telemetry::ScalarID::MEDIA_AUTOPLAY_WOULD_NOT_BE_ALLOWED_COUNT,

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

@ -165,19 +165,6 @@ static bool IsEnableBlockingWebAudioByUserGesturePolicy() {
StaticPrefs::media_autoplay_enabled_user_gestures_needed();
}
/* static */
bool AutoplayPolicy::WouldBeAllowedToPlayIfAutoplayDisabled(
const HTMLMediaElement& aElement) {
return IsMediaElementInaudible(aElement) ||
IsWindowAllowedToPlay(aElement.OwnerDoc()->GetInnerWindow());
}
/* static */
bool AutoplayPolicy::WouldBeAllowedToPlayIfAutoplayDisabled(
const AudioContext& aContext) {
return IsAudioContextAllowedToPlay(aContext);
}
static bool IsAllowedToPlayByBlockingModel(const HTMLMediaElement& aElement) {
if (!StaticPrefs::media_autoplay_enabled_user_gestures_needed()) {
// If element is blessed, it would always be allowed to play().
@ -292,5 +279,18 @@ DocumentAutoplayPolicy AutoplayPolicy::IsAllowedToPlay(
return DocumentAutoplayPolicy::Disallowed;
}
/* static */
bool AutoplayPolicyTelemetryUtils::WouldBeAllowedToPlayIfAutoplayDisabled(
const HTMLMediaElement& aElement) {
return IsMediaElementInaudible(aElement) ||
IsWindowAllowedToPlay(aElement.OwnerDoc()->GetInnerWindow());
}
/* static */
bool AutoplayPolicyTelemetryUtils::WouldBeAllowedToPlayIfAutoplayDisabled(
const AudioContext& aContext) {
return IsAudioContextAllowedToPlay(aContext);
}
} // namespace dom
} // namespace mozilla

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

@ -38,7 +38,15 @@ class AutoplayPolicy {
// Returns whether a given AudioContext is allowed to play.
static bool IsAllowedToPlay(const AudioContext& aContext);
};
/**
* This class contains helper funtions which could be used in AutoplayPolicy
* for determing Telemetry use-only result. They shouldn't represent the final
* result of blocking autoplay.
*/
class AutoplayPolicyTelemetryUtils {
public:
// Returns true if a given media element would be allowed to play
// if block autoplay was enabled. If this returns false, it means we would
// either block or ask for permission.

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

@ -1072,7 +1072,8 @@ void AudioContext::ResumeInternal(AudioContextOperationFlags aFlags) {
}
void AudioContext::UpdateAutoplayAssumptionStatus() {
if (AutoplayPolicy::WouldBeAllowedToPlayIfAutoplayDisabled(*this)) {
if (AutoplayPolicyTelemetryUtils::WouldBeAllowedToPlayIfAutoplayDisabled(
*this)) {
mWasEverAllowedToStart |= true;
mWouldBeAllowedToStart = true;
} else {
@ -1087,7 +1088,8 @@ void AudioContext::MaybeUpdateAutoplayTelemetry() {
return;
}
if (AutoplayPolicy::WouldBeAllowedToPlayIfAutoplayDisabled(*this) &&
if (AutoplayPolicyTelemetryUtils::WouldBeAllowedToPlayIfAutoplayDisabled(
*this) &&
!mWouldBeAllowedToStart) {
AccumulateCategorical(
mozilla::Telemetry::LABELS_WEB_AUDIO_AUTOPLAY::AllowedAfterBlocked);