gecko-dev/dom/html/PlayPromise.cpp
alwu 976a59a484 Bug 1686696 - part1 : store pending event runner instead of saving event name. r=bryce,mccr8
In this patch, we move all event runners to a separate file and implement an event blocker.

In order to allow us to do a special check for `timeupdate` event, we have to know that event is periodic or mandatory.

We have to save extra information for `timeupdate, however, current design for storing pending events is not flexible enough to achieve that.

Therefore, instead of storing the event name, we store the event runners which can all have different implementation and know when they should dispatch their own event.

Differential Revision: https://phabricator.services.mozilla.com/D102673
2021-02-11 20:19:23 +00:00

85 lines
2.4 KiB
C++

/* -*- 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 "mozilla/dom/PlayPromise.h"
#include "mozilla/Logging.h"
#include "mozilla/Telemetry.h"
extern mozilla::LazyLogModule gMediaElementLog;
#define PLAY_PROMISE_LOG(msg, ...) \
MOZ_LOG(gMediaElementLog, LogLevel::Debug, (msg, ##__VA_ARGS__))
namespace mozilla::dom {
PlayPromise::PlayPromise(nsIGlobalObject* aGlobal) : Promise(aGlobal) {}
PlayPromise::~PlayPromise() {
if (!mFulfilled && PromiseObj()) {
MaybeReject(NS_ERROR_DOM_ABORT_ERR);
}
}
/* static */
already_AddRefed<PlayPromise> PlayPromise::Create(nsIGlobalObject* aGlobal,
ErrorResult& aRv) {
RefPtr<PlayPromise> promise = new PlayPromise(aGlobal);
promise->CreateWrapper(aRv);
return aRv.Failed() ? nullptr : promise.forget();
}
/* static */
void PlayPromise::ResolvePromisesWithUndefined(
const PlayPromiseArr& aPromises) {
for (const auto& promise : aPromises) {
promise->MaybeResolveWithUndefined();
}
}
/* static */
void PlayPromise::RejectPromises(const PlayPromiseArr& aPromises,
nsresult aError) {
for (const auto& promise : aPromises) {
promise->MaybeReject(aError);
}
}
void PlayPromise::MaybeResolveWithUndefined() {
if (mFulfilled) {
return;
}
mFulfilled = true;
PLAY_PROMISE_LOG("PlayPromise %p resolved with undefined", this);
Promise::MaybeResolveWithUndefined();
}
static const char* ToPlayResultStr(nsresult aReason) {
switch (aReason) {
case NS_ERROR_DOM_MEDIA_NOT_ALLOWED_ERR:
return "NotAllowedErr";
case NS_ERROR_DOM_MEDIA_NOT_SUPPORTED_ERR:
return "SrcNotSupportedErr";
case NS_ERROR_DOM_MEDIA_ABORT_ERR:
return "PauseAbortErr";
case NS_ERROR_DOM_ABORT_ERR:
return "AbortErr";
default:
return "UnknownErr";
}
}
void PlayPromise::MaybeReject(nsresult aReason) {
if (mFulfilled) {
return;
}
mFulfilled = true;
PLAY_PROMISE_LOG("PlayPromise %p rejected with 0x%x (%s)", this,
static_cast<uint32_t>(aReason), ToPlayResultStr(aReason));
Promise::MaybeReject(aReason);
}
} // namespace mozilla::dom