2015-05-03 22:32:37 +03: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-06-12 05:41:21 +04: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/. */
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
#ifndef mozilla_dom_Promise_h
|
|
|
|
#define mozilla_dom_Promise_h
|
2013-06-12 05:41:21 +04:00
|
|
|
|
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/ErrorResult.h"
|
2014-10-20 21:02:21 +04:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2014-02-26 01:34:55 +04:00
|
|
|
#include "mozilla/TypeTraits.h"
|
2013-06-12 05:41:21 +04:00
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2013-07-12 00:40:36 +04:00
|
|
|
#include "mozilla/dom/PromiseBinding.h"
|
2014-04-10 22:57:07 +04:00
|
|
|
#include "mozilla/dom/ToJSValue.h"
|
2014-08-01 20:42:19 +04:00
|
|
|
#include "mozilla/WeakPtr.h"
|
2013-06-12 05:41:21 +04:00
|
|
|
#include "nsWrapperCache.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2013-08-28 06:59:14 +04:00
|
|
|
#include "js/TypeDecls.h"
|
2015-01-16 01:39:02 +03:00
|
|
|
#include "jspubtd.h"
|
2013-06-12 05:41:21 +04:00
|
|
|
|
2014-02-26 01:34:55 +04:00
|
|
|
class nsIGlobalObject;
|
|
|
|
|
2013-06-12 05:41:21 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
class AnyCallback;
|
2014-09-20 10:20:41 +04:00
|
|
|
class MediaStreamError;
|
2013-11-19 22:43:51 +04:00
|
|
|
class PromiseInit;
|
|
|
|
class PromiseNativeHandler;
|
2014-07-02 08:19:48 +04:00
|
|
|
class PromiseDebugging;
|
2013-06-12 05:41:21 +04:00
|
|
|
|
2014-12-26 05:11:20 +03:00
|
|
|
#define NS_PROMISE_IID \
|
|
|
|
{ 0x1b8d6215, 0x3e67, 0x43ba, \
|
|
|
|
{ 0x8a, 0xf9, 0x31, 0x5e, 0x8f, 0xce, 0x75, 0x65 } }
|
|
|
|
|
2014-09-12 06:18:49 +04:00
|
|
|
class Promise : public nsISupports,
|
|
|
|
public SupportsWeakPtr<Promise>
|
2013-06-12 05:41:21 +04:00
|
|
|
{
|
2013-11-24 23:26:07 +04:00
|
|
|
friend class PromiseTask;
|
2014-02-24 17:56:54 +04:00
|
|
|
friend class PromiseWorkerProxy;
|
|
|
|
friend class PromiseWorkerProxyRunnable;
|
2013-06-12 05:41:21 +04:00
|
|
|
|
|
|
|
public:
|
2014-12-26 05:11:20 +03:00
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(NS_PROMISE_IID)
|
2013-06-12 05:41:21 +04:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2016-02-10 01:40:31 +03:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Promise)
|
2015-04-24 19:43:01 +03:00
|
|
|
MOZ_DECLARE_WEAKREFERENCE_TYPENAME(Promise)
|
2013-06-12 05:41:21 +04:00
|
|
|
|
2014-07-19 05:31:11 +04:00
|
|
|
// Promise creation tries to create a JS reflector for the Promise, so is
|
|
|
|
// fallible. Furthermore, we don't want to do JS-wrapping on a 0-refcount
|
|
|
|
// object, so we addref before doing that and return the addrefed pointer
|
|
|
|
// here.
|
2016-02-10 01:40:31 +03:00
|
|
|
static already_AddRefed<Promise>
|
|
|
|
Create(nsIGlobalObject* aGlobal, ErrorResult& aRv);
|
2016-03-22 18:22:24 +03:00
|
|
|
|
|
|
|
// Reports a rejected Promise by sending an error report.
|
|
|
|
static void ReportRejectedPromise(JSContext* aCx, JS::HandleObject aPromise);
|
2014-02-26 01:34:55 +04:00
|
|
|
|
|
|
|
typedef void (Promise::*MaybeFunc)(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue);
|
2013-06-12 05:41:21 +04:00
|
|
|
|
2013-09-26 22:09:16 +04:00
|
|
|
void MaybeResolve(JSContext* aCx,
|
2013-11-19 22:39:51 +04:00
|
|
|
JS::Handle<JS::Value> aValue);
|
2013-09-26 22:09:16 +04:00
|
|
|
void MaybeReject(JSContext* aCx,
|
2013-11-19 22:39:51 +04:00
|
|
|
JS::Handle<JS::Value> aValue);
|
2013-09-26 22:09:16 +04:00
|
|
|
|
2014-02-26 01:34:55 +04:00
|
|
|
// Helpers for using Promise from C++.
|
2014-04-16 23:13:41 +04:00
|
|
|
// Most DOM objects are handled already. To add a new type T, add a
|
|
|
|
// ToJSValue overload in ToJSValue.h.
|
|
|
|
// aArg is a const reference so we can pass rvalues like integer constants
|
2014-02-26 01:34:55 +04:00
|
|
|
template <typename T>
|
2014-04-16 23:13:41 +04:00
|
|
|
void MaybeResolve(const T& aArg) {
|
2014-02-26 01:34:55 +04:00
|
|
|
MaybeSomething(aArg, &Promise::MaybeResolve);
|
|
|
|
}
|
|
|
|
|
2016-08-10 05:07:42 +03:00
|
|
|
void MaybeResolveWithUndefined();
|
|
|
|
|
2014-06-03 19:38:38 +04:00
|
|
|
inline void MaybeReject(nsresult aArg) {
|
|
|
|
MOZ_ASSERT(NS_FAILED(aArg));
|
2014-02-26 01:34:55 +04:00
|
|
|
MaybeSomething(aArg, &Promise::MaybeReject);
|
|
|
|
}
|
2014-09-20 10:20:41 +04:00
|
|
|
|
2014-10-30 18:12:09 +03:00
|
|
|
inline void MaybeReject(ErrorResult& aArg) {
|
|
|
|
MOZ_ASSERT(aArg.Failed());
|
|
|
|
MaybeSomething(aArg, &Promise::MaybeReject);
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
void MaybeReject(const RefPtr<MediaStreamError>& aArg);
|
2014-09-20 10:20:41 +04:00
|
|
|
|
2016-06-09 22:26:47 +03:00
|
|
|
void MaybeRejectWithUndefined();
|
|
|
|
|
2014-06-03 19:38:38 +04:00
|
|
|
// DO NOT USE MaybeRejectBrokenly with in new code. Promises should be
|
|
|
|
// rejected with Error instances.
|
2017-08-06 20:47:00 +03:00
|
|
|
// Note: MaybeRejectBrokenly is a template so we can use it with DOMException
|
|
|
|
// without instantiating the DOMException specialization of MaybeSomething in
|
2014-06-03 19:38:38 +04:00
|
|
|
// every translation unit that includes this header, because that would
|
2017-08-06 20:47:00 +03:00
|
|
|
// require use to include DOMException.h either here or in all those
|
|
|
|
// translation units.
|
2014-06-03 19:38:38 +04:00
|
|
|
template<typename T>
|
|
|
|
void MaybeRejectBrokenly(const T& aArg); // Not implemented by default; see
|
|
|
|
// specializations in the .cpp for
|
|
|
|
// the T values we support.
|
2014-02-26 01:34:55 +04:00
|
|
|
|
2013-06-12 05:41:21 +04:00
|
|
|
// WebIDL
|
|
|
|
|
2014-02-26 01:34:55 +04:00
|
|
|
nsIGlobalObject* GetParentObject() const
|
2013-06-12 05:41:21 +04:00
|
|
|
{
|
2014-02-26 01:34:55 +04:00
|
|
|
return mGlobal;
|
2013-06-12 05:41:21 +04:00
|
|
|
}
|
|
|
|
|
2018-04-14 17:25:15 +03:00
|
|
|
// Do the equivalent of Promise.resolve in the current compartment of aCx.
|
|
|
|
// Errorrs are reported on the ErrorResult; if aRv comes back !Failed(), this
|
|
|
|
// function MUST return a non-null value.
|
2016-02-10 01:40:31 +03:00
|
|
|
static already_AddRefed<Promise>
|
|
|
|
Resolve(nsIGlobalObject* aGlobal, JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue, ErrorResult& aRv);
|
|
|
|
|
2018-04-14 17:25:15 +03:00
|
|
|
// Do the equivalent of Promise.reject in the current compartment of aCx.
|
|
|
|
// Errorrs are reported on the ErrorResult; if aRv comes back !Failed(), this
|
|
|
|
// function MUST return a non-null value.
|
2016-02-10 01:40:31 +03:00
|
|
|
static already_AddRefed<Promise>
|
|
|
|
Reject(nsIGlobalObject* aGlobal, JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue, ErrorResult& aRv);
|
|
|
|
|
|
|
|
static already_AddRefed<Promise>
|
2018-04-14 17:25:15 +03:00
|
|
|
All(const GlobalObject& aGlobal,
|
|
|
|
const nsTArray<RefPtr<Promise>>& aPromiseList, ErrorResult& aRv);
|
2016-02-10 01:40:31 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
Then(JSContext* aCx,
|
|
|
|
// aCalleeGlobal may not be in the compartment of aCx, when called over
|
|
|
|
// Xrays.
|
|
|
|
JS::Handle<JSObject*> aCalleeGlobal,
|
|
|
|
AnyCallback* aResolveCallback, AnyCallback* aRejectCallback,
|
|
|
|
JS::MutableHandle<JS::Value> aRetval,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2016-02-10 01:40:31 +03:00
|
|
|
JSObject* PromiseObj() const
|
|
|
|
{
|
|
|
|
return mPromiseObj;
|
|
|
|
}
|
|
|
|
|
2013-11-19 22:43:51 +04:00
|
|
|
void AppendNativeHandler(PromiseNativeHandler* aRunnable);
|
|
|
|
|
2015-01-16 01:39:02 +03:00
|
|
|
JSObject* GlobalJSObject() const;
|
|
|
|
|
|
|
|
JSCompartment* Compartment() const;
|
|
|
|
|
2016-02-10 01:40:31 +03:00
|
|
|
// Create a dom::Promise from a given SpiderMonkey Promise object.
|
|
|
|
// aPromiseObj MUST be in the compartment of aGlobal's global JS object.
|
|
|
|
static already_AddRefed<Promise>
|
|
|
|
CreateFromExisting(nsIGlobalObject* aGlobal,
|
|
|
|
JS::Handle<JSObject*> aPromiseObj);
|
2015-11-25 23:48:09 +03:00
|
|
|
|
2016-09-07 05:51:41 +03:00
|
|
|
enum class PromiseState {
|
|
|
|
Pending,
|
|
|
|
Resolved,
|
|
|
|
Rejected
|
|
|
|
};
|
|
|
|
|
|
|
|
PromiseState State() const;
|
|
|
|
|
2014-09-12 06:18:49 +04:00
|
|
|
protected:
|
2015-11-25 23:48:08 +03:00
|
|
|
struct PromiseCapability;
|
|
|
|
|
2016-02-10 01:40:31 +03:00
|
|
|
// Do NOT call this unless you're Promise::Create or
|
|
|
|
// Promise::CreateFromExisting. I wish we could enforce that from inside this
|
|
|
|
// class too, somehow.
|
2014-09-02 04:49:25 +04:00
|
|
|
explicit Promise(nsIGlobalObject* aGlobal);
|
2014-07-19 05:31:11 +04:00
|
|
|
|
2014-09-12 06:18:49 +04:00
|
|
|
virtual ~Promise();
|
|
|
|
|
2015-07-31 20:30:55 +03:00
|
|
|
// Do JS-wrapping after Promise creation. Passing null for aDesiredProto will
|
|
|
|
// use the default prototype for the sort of Promise we have.
|
|
|
|
void CreateWrapper(JS::Handle<JSObject*> aDesiredProto, ErrorResult& aRv);
|
2014-09-12 06:18:49 +04:00
|
|
|
|
|
|
|
private:
|
2014-02-26 01:34:55 +04:00
|
|
|
template <typename T>
|
|
|
|
void MaybeSomething(T& aArgument, MaybeFunc aFunc) {
|
2016-03-23 18:02:57 +03:00
|
|
|
MOZ_ASSERT(PromiseObj()); // It was preserved!
|
|
|
|
|
2016-07-08 03:08:25 +03:00
|
|
|
AutoEntryScript aes(mGlobal, "Promise resolution or rejection");
|
|
|
|
JSContext* cx = aes.cx();
|
2014-02-26 01:34:55 +04:00
|
|
|
|
|
|
|
JS::Rooted<JS::Value> val(cx);
|
2014-04-10 22:57:07 +04:00
|
|
|
if (!ToJSValue(cx, aArgument, &val)) {
|
2014-02-26 01:34:55 +04:00
|
|
|
HandleException(cx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
(this->*aFunc)(cx, val);
|
|
|
|
}
|
|
|
|
|
2016-02-10 01:40:31 +03:00
|
|
|
void HandleException(JSContext* aCx);
|
2014-10-20 06:27:12 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsIGlobalObject> mGlobal;
|
2013-06-12 05:41:21 +04:00
|
|
|
|
2016-02-10 01:40:31 +03:00
|
|
|
JS::Heap<JSObject*> mPromiseObj;
|
2013-06-12 05:41:21 +04:00
|
|
|
};
|
|
|
|
|
2014-12-26 05:11:20 +03:00
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(Promise, NS_PROMISE_IID)
|
|
|
|
|
2013-06-12 05:41:21 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
#endif // mozilla_dom_Promise_h
|