1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 #ifndef mozilla_PromiseNativeWrapper_h__
5 #define mozilla_PromiseNativeWrapper_h__
7 #include "mozilla/MozPromise.h"
8 #include "mozilla/dom/Promise.h"
9 #include "nsISupports.h"
10 #include "mozilla/dom/PromiseNativeHandler.h"
14 // Helper for wrapping JS promises in MozPromise so they can be handled from C++
16 class PromiseNativeWrapper
: public dom::PromiseNativeHandler
{
20 explicit PromiseNativeWrapper(
21 MozPromiseHolder
<GenericNonExclusivePromise
>&& aHolder
)
22 : mHolder(std::move(aHolder
)) {}
24 void ResolvedCallback(JSContext
* aCx
, JS::Handle
<JS::Value
> aValue
,
25 ErrorResult
& aRv
) override
{
26 mHolder
.Resolve(true, __func__
);
29 void RejectedCallback(JSContext
* aCx
, JS::Handle
<JS::Value
> aValue
,
30 ErrorResult
& aRv
) override
{
31 mHolder
.Reject(NS_ERROR_FAILURE
, __func__
);
34 static RefPtr
<GenericNonExclusivePromise
> ConvertJSPromiseToMozPromise(
35 const RefPtr
<dom::Promise
>& jsPromise
);
38 ~PromiseNativeWrapper() = default;
39 MozPromiseHolder
<GenericNonExclusivePromise
> mHolder
;
42 } // namespace mozilla