diff --git a/dom/promise/PromiseNativeHandler.cpp b/dom/promise/PromiseNativeHandler.cpp index 3edddcb6e766..5bbdaf2f8229 100644 --- a/dom/promise/PromiseNativeHandler.cpp +++ b/dom/promise/PromiseNativeHandler.cpp @@ -13,31 +13,23 @@ namespace dom { NS_IMPL_ISUPPORTS0(DomPromiseListener) -DomPromiseListener::DomPromiseListener(dom::Promise* aDOMPromise) { - aDOMPromise->AppendNativeHandler(this); -} - -DomPromiseListener::DomPromiseListener(dom::Promise* aDOMPromise, - CallbackTypeResolved&& aResolve, +DomPromiseListener::DomPromiseListener(CallbackTypeResolved&& aResolve, CallbackTypeRejected&& aReject) - : mResolve(Some(std::move(aResolve))), mReject(Some(std::move(aReject))) { - aDOMPromise->AppendNativeHandler(this); -} + : mResolve(std::move(aResolve)), mReject(std::move(aReject)) {} DomPromiseListener::~DomPromiseListener() { if (mReject) { - (*mReject)(NS_BINDING_ABORTED); + mReject(NS_BINDING_ABORTED); } } void DomPromiseListener::ResolvedCallback(JSContext* aCx, JS::Handle aValue) { if (mResolve) { - (*mResolve)(aCx, aValue); + mResolve(aCx, aValue); } // Let's clear the lambdas in case we have a cycle to ourselves. - mResolve.reset(); - mReject.reset(); + Clear(); } void DomPromiseListener::RejectedCallback(JSContext* aCx, @@ -49,17 +41,15 @@ void DomPromiseListener::RejectedCallback(JSContext* aCx, } else { errorCode = nsresult(aValue.toInt32()); } - (*mReject)(errorCode); + mReject(errorCode); } // Let's clear the lambdas in case we have a cycle to ourselves. - mResolve.reset(); - mReject.reset(); + Clear(); } -void DomPromiseListener::SetResolvers(CallbackTypeResolved&& aResolve, - CallbackTypeRejected&& aReject) { - mResolve = Some(std::move(aResolve)); - mReject = Some(std::move(aReject)); +void DomPromiseListener::Clear() { + mResolve = nullptr; + mReject = nullptr; } } // namespace dom diff --git a/dom/promise/PromiseNativeHandler.h b/dom/promise/PromiseNativeHandler.h index f8d955133fa1..9a26332a6f51 100644 --- a/dom/promise/PromiseNativeHandler.h +++ b/dom/promise/PromiseNativeHandler.h @@ -16,8 +16,6 @@ namespace mozilla { namespace dom { -class Promise; - /* * PromiseNativeHandler allows C++ to react to a Promise being * rejected/resolved. A PromiseNativeHandler can be appended to a Promise using @@ -47,18 +45,18 @@ class DomPromiseListener final : public PromiseNativeHandler { std::function)>; using CallbackTypeRejected = std::function; - explicit DomPromiseListener(Promise* aDOMPromise); - DomPromiseListener(Promise* aDOMPromise, CallbackTypeResolved&& aResolve, + DomPromiseListener(CallbackTypeResolved&& aResolve, CallbackTypeRejected&& aReject); - void SetResolvers(CallbackTypeResolved&& aResolve, - CallbackTypeRejected&& aReject); + + void Clear(); + void ResolvedCallback(JSContext* aCx, JS::Handle aValue) override; void RejectedCallback(JSContext* aCx, JS::Handle aValue) override; private: ~DomPromiseListener(); - Maybe mResolve; - Maybe mReject; + CallbackTypeResolved mResolve; + CallbackTypeRejected mReject; }; } // namespace dom diff --git a/toolkit/components/extensions/webidl-api/ExtensionEventListener.cpp b/toolkit/components/extensions/webidl-api/ExtensionEventListener.cpp index 6f835826d91d..79ab03d7840a 100644 --- a/toolkit/components/extensions/webidl-api/ExtensionEventListener.cpp +++ b/toolkit/components/extensions/webidl-api/ExtensionEventListener.cpp @@ -59,11 +59,11 @@ class SendResponseCallback final : public nsISupports { // Create a promise monitor that invalidates the sendResponse // callback if the promise has been already resolved or rejected. mPromiseListener = new dom::DomPromiseListener( - mPromise, [self = RefPtr{this}](JSContext* aCx, JS::Handle aValue) { self->Cleanup(); }, [self = RefPtr{this}](nsresult aError) { self->Cleanup(); }); + mPromise->AppendNativeHandler(mPromiseListener); } void Cleanup(bool aIsDestroying = false) { @@ -73,11 +73,9 @@ class SendResponseCallback final : public nsISupports { } NS_WARNING("SendResponseCallback::Cleanup"); - // Override the promise listener's resolvers to release the + // Clear the promise listener's resolvers to release the // RefPtr captured by the ones initially set. - mPromiseListener->SetResolvers( - [](JSContext* aCx, JS::Handle aValue) {}, - [](nsresult aError) {}); + mPromiseListener->Clear(); mPromiseListener = nullptr; if (mPromise) { diff --git a/xpcom/threads/MozPromiseInlines.h b/xpcom/threads/MozPromiseInlines.h index 28903917baed..def7e90461b9 100644 --- a/xpcom/threads/MozPromiseInlines.h +++ b/xpcom/threads/MozPromiseInlines.h @@ -4,14 +4,15 @@ * 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/. */ -#if !defined(MozPromiseInlines_h_) -# define MozPromiseInlines_h_ +#ifndef MozPromiseInlines_h_ +#define MozPromiseInlines_h_ -# include +#include -# include "mozilla/MozPromise.h" -# include "mozilla/dom/PrimitiveConversions.h" -# include "mozilla/dom/PromiseNativeHandler.h" +#include "mozilla/MozPromise.h" +#include "mozilla/dom/PrimitiveConversions.h" +#include "mozilla/dom/Promise.h" +#include "mozilla/dom/PromiseNativeHandler.h" namespace mozilla { @@ -26,7 +27,6 @@ MozPromise::FromDomPromise( "Reject type must be nsresult"); RefPtr p = new Private(__func__); RefPtr listener = new dom::DomPromiseListener( - aDOMPromise, [p](JSContext* aCx, JS::Handle aValue) { ResolveValueT value; bool ok = dom::ValueToPrimitive::FromDomPromise( p->Resolve(value, __func__); }, [p](nsresult aError) { p->Reject(aError, __func__); }); + aDOMPromise->AppendNativeHandler(listener); return p; }