2015-05-03 19:32:37 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-11-19 18:43:51 +00:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_PromiseNativeHandler_h
|
|
|
|
#define mozilla_dom_PromiseNativeHandler_h
|
|
|
|
|
2019-09-20 04:09:46 +00:00
|
|
|
#include <functional>
|
2019-09-20 03:58:44 +00:00
|
|
|
#include "js/TypeDecls.h"
|
2021-03-25 19:47:04 +00:00
|
|
|
#include "js/Value.h"
|
2022-01-20 19:11:39 +00:00
|
|
|
#include "mozilla/ErrorResult.h"
|
2020-11-23 16:21:38 +00:00
|
|
|
#include "mozilla/Maybe.h"
|
2024-04-24 17:02:49 +00:00
|
|
|
#include "mozilla/StaticString.h"
|
2019-09-20 04:09:46 +00:00
|
|
|
#include "nsISupports.h"
|
2013-11-19 18:43:51 +00:00
|
|
|
|
2022-05-09 20:41:12 +00:00
|
|
|
namespace mozilla::dom {
|
2013-11-19 18:43:51 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* PromiseNativeHandler allows C++ to react to a Promise being
|
|
|
|
* rejected/resolved. A PromiseNativeHandler can be appended to a Promise using
|
|
|
|
* Promise::AppendNativeHandler().
|
|
|
|
*/
|
|
|
|
class PromiseNativeHandler : public nsISupports {
|
2014-06-23 19:56:07 +00:00
|
|
|
protected:
|
2019-09-20 04:09:46 +00:00
|
|
|
virtual ~PromiseNativeHandler() = default;
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2013-11-19 18:43:51 +00:00
|
|
|
public:
|
2019-03-19 15:53:43 +00:00
|
|
|
MOZ_CAN_RUN_SCRIPT
|
2022-01-20 19:11:39 +00:00
|
|
|
virtual void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv) = 0;
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2019-03-19 15:53:43 +00:00
|
|
|
MOZ_CAN_RUN_SCRIPT
|
2022-01-20 19:11:39 +00:00
|
|
|
virtual void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv) = 0;
|
2013-11-19 18:43:51 +00:00
|
|
|
};
|
|
|
|
|
2023-07-27 22:37:59 +00:00
|
|
|
// This base class exists solely to use NS_IMPL_ISUPPORTS because it doesn't
|
|
|
|
// support template classes.
|
|
|
|
class MozPromiseRejectOnDestructionBase : public PromiseNativeHandler {
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv) override {}
|
|
|
|
void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv) override {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
~MozPromiseRejectOnDestructionBase() override = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Use this when you subscribe to a JS promise to settle a MozPromise that is
|
|
|
|
// not guaranteed to be settled by anyone else.
|
|
|
|
template <typename T>
|
|
|
|
class MozPromiseRejectOnDestruction final
|
|
|
|
: public MozPromiseRejectOnDestructionBase {
|
|
|
|
public:
|
|
|
|
// (Accepting RefPtr<T> instead of T* because compiler fails to implicitly
|
|
|
|
// convert it at call sites)
|
|
|
|
MozPromiseRejectOnDestruction(const RefPtr<T>& aMozPromise,
|
2024-04-24 17:02:49 +00:00
|
|
|
StaticString aCallSite)
|
2023-07-27 22:37:59 +00:00
|
|
|
: mMozPromise(aMozPromise), mCallSite(aCallSite) {
|
|
|
|
MOZ_ASSERT(aMozPromise);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
~MozPromiseRejectOnDestruction() override {
|
|
|
|
// Rejecting will be no-op if the promise is already settled
|
|
|
|
mMozPromise->Reject(NS_BINDING_ABORTED, mCallSite);
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<T> mMozPromise;
|
2024-04-24 17:02:49 +00:00
|
|
|
StaticString mCallSite;
|
2023-07-27 22:37:59 +00:00
|
|
|
};
|
|
|
|
|
2022-05-09 20:41:12 +00:00
|
|
|
} // namespace mozilla::dom
|
2013-11-19 18:43:51 +00:00
|
|
|
|
|
|
|
#endif // mozilla_dom_PromiseNativeHandler_h
|