PlayPromise.cpp (2412B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 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 #include "mozilla/dom/PlayPromise.h" 8 9 #include "mozilla/Logging.h" 10 11 extern mozilla::LazyLogModule gMediaElementLog; 12 13 #define PLAY_PROMISE_LOG(msg, ...) \ 14 MOZ_LOG(gMediaElementLog, LogLevel::Debug, (msg, ##__VA_ARGS__)) 15 16 namespace mozilla::dom { 17 18 PlayPromise::PlayPromise(nsIGlobalObject* aGlobal) : Promise(aGlobal) {} 19 20 PlayPromise::~PlayPromise() { 21 if (!mFulfilled && PromiseObj()) { 22 MaybeReject(NS_ERROR_DOM_ABORT_ERR); 23 } 24 } 25 26 /* static */ 27 already_AddRefed<PlayPromise> PlayPromise::Create(nsIGlobalObject* aGlobal, 28 ErrorResult& aRv) { 29 RefPtr<PlayPromise> promise = new PlayPromise(aGlobal); 30 promise->CreateWrapper(aRv); 31 return aRv.Failed() ? nullptr : promise.forget(); 32 } 33 34 /* static */ 35 void PlayPromise::ResolvePromisesWithUndefined( 36 const PlayPromiseArr& aPromises) { 37 for (const auto& promise : aPromises) { 38 promise->MaybeResolveWithUndefined(); 39 } 40 } 41 42 /* static */ 43 void PlayPromise::RejectPromises(const PlayPromiseArr& aPromises, 44 nsresult aError) { 45 for (const auto& promise : aPromises) { 46 promise->MaybeReject(aError); 47 } 48 } 49 50 void PlayPromise::MaybeResolveWithUndefined() { 51 if (mFulfilled) { 52 return; 53 } 54 mFulfilled = true; 55 PLAY_PROMISE_LOG("PlayPromise %p resolved with undefined", this); 56 Promise::MaybeResolveWithUndefined(); 57 } 58 59 static const char* ToPlayResultStr(nsresult aReason) { 60 switch (aReason) { 61 case NS_ERROR_DOM_MEDIA_NOT_ALLOWED_ERR: 62 return "NotAllowedErr"; 63 case NS_ERROR_DOM_MEDIA_NOT_SUPPORTED_ERR: 64 return "SrcNotSupportedErr"; 65 case NS_ERROR_DOM_MEDIA_ABORT_ERR: 66 return "PauseAbortErr"; 67 case NS_ERROR_DOM_ABORT_ERR: 68 return "AbortErr"; 69 default: 70 return "UnknownErr"; 71 } 72 } 73 74 void PlayPromise::MaybeReject(nsresult aReason) { 75 if (mFulfilled) { 76 return; 77 } 78 mFulfilled = true; 79 PLAY_PROMISE_LOG("PlayPromise %p rejected with 0x%x (%s)", this, 80 static_cast<uint32_t>(aReason), ToPlayResultStr(aReason)); 81 Promise::MaybeReject(aReason); 82 } 83 84 } // namespace mozilla::dom