2013-06-11 18:41:21 -07:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* 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-11 13:40:36 -07:00
|
|
|
#ifndef mozilla_dom_Promise_h
|
|
|
|
#define mozilla_dom_Promise_h
|
2013-06-11 18:41:21 -07:00
|
|
|
|
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/ErrorResult.h"
|
2014-02-25 13:34:55 -08:00
|
|
|
#include "mozilla/TypeTraits.h"
|
2013-06-11 18:41:21 -07:00
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2013-07-11 13:40:36 -07:00
|
|
|
#include "mozilla/dom/PromiseBinding.h"
|
2014-04-10 11:57:07 -07:00
|
|
|
#include "mozilla/dom/ToJSValue.h"
|
2014-08-01 09:42:19 -07:00
|
|
|
#include "mozilla/WeakPtr.h"
|
2013-06-11 18:41:21 -07:00
|
|
|
#include "nsWrapperCache.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2013-08-27 19:59:14 -07:00
|
|
|
#include "js/TypeDecls.h"
|
2013-06-11 18:41:21 -07:00
|
|
|
|
2014-03-12 07:31:03 -07:00
|
|
|
#include "mozilla/dom/workers/bindings/WorkerFeature.h"
|
|
|
|
|
2014-02-25 13:34:55 -08:00
|
|
|
class nsIGlobalObject;
|
|
|
|
|
2013-06-11 18:41:21 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
class AnyCallback;
|
2014-06-03 08:38:38 -07:00
|
|
|
class DOMError;
|
2013-11-19 10:43:51 -08:00
|
|
|
class PromiseCallback;
|
|
|
|
class PromiseInit;
|
|
|
|
class PromiseNativeHandler;
|
2014-07-01 21:19:48 -07:00
|
|
|
class PromiseDebugging;
|
2013-06-11 18:41:21 -07:00
|
|
|
|
2014-03-12 07:31:03 -07:00
|
|
|
class Promise;
|
|
|
|
class PromiseReportRejectFeature : public workers::WorkerFeature
|
|
|
|
{
|
|
|
|
// The Promise that owns this feature.
|
|
|
|
Promise* mPromise;
|
|
|
|
|
|
|
|
public:
|
2014-09-01 17:49:25 -07:00
|
|
|
explicit PromiseReportRejectFeature(Promise* aPromise)
|
2014-03-12 07:31:03 -07:00
|
|
|
: mPromise(aPromise)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mPromise);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool
|
|
|
|
Notify(JSContext* aCx, workers::Status aStatus) MOZ_OVERRIDE;
|
|
|
|
};
|
|
|
|
|
2014-09-11 19:18:49 -07:00
|
|
|
class Promise : public nsISupports,
|
|
|
|
public nsWrapperCache,
|
|
|
|
public SupportsWeakPtr<Promise>
|
2013-06-11 18:41:21 -07:00
|
|
|
{
|
2013-11-19 10:43:51 -08:00
|
|
|
friend class NativePromiseCallback;
|
2013-07-11 13:40:36 -07:00
|
|
|
friend class PromiseResolverTask;
|
2013-11-24 11:26:07 -08:00
|
|
|
friend class PromiseTask;
|
2014-03-12 07:31:03 -07:00
|
|
|
friend class PromiseReportRejectFeature;
|
2014-02-24 05:56:54 -08:00
|
|
|
friend class PromiseWorkerProxy;
|
|
|
|
friend class PromiseWorkerProxyRunnable;
|
2013-09-11 09:03:04 -07:00
|
|
|
friend class RejectPromiseCallback;
|
2013-11-24 11:26:07 -08:00
|
|
|
friend class ResolvePromiseCallback;
|
2014-08-19 03:39:56 -07:00
|
|
|
friend class ThenableResolverTask;
|
2013-09-11 09:03:04 -07:00
|
|
|
friend class WrapperPromiseCallback;
|
2013-06-11 18:41:21 -07:00
|
|
|
|
|
|
|
public:
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2013-07-11 13:40:36 -07:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Promise)
|
2014-08-01 09:42:19 -07:00
|
|
|
MOZ_DECLARE_REFCOUNTED_TYPENAME(Promise)
|
2013-06-11 18:41:21 -07:00
|
|
|
|
2014-07-18 18:31:11 -07: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.
|
|
|
|
static already_AddRefed<Promise>
|
|
|
|
Create(nsIGlobalObject* aGlobal, ErrorResult& aRv);
|
2014-02-25 13:34:55 -08:00
|
|
|
|
|
|
|
typedef void (Promise::*MaybeFunc)(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue);
|
2013-06-11 18:41:21 -07:00
|
|
|
|
2013-09-26 11:09:16 -07:00
|
|
|
void MaybeResolve(JSContext* aCx,
|
2013-11-19 10:39:51 -08:00
|
|
|
JS::Handle<JS::Value> aValue);
|
2013-09-26 11:09:16 -07:00
|
|
|
void MaybeReject(JSContext* aCx,
|
2013-11-19 10:39:51 -08:00
|
|
|
JS::Handle<JS::Value> aValue);
|
2013-09-26 11:09:16 -07:00
|
|
|
|
2014-02-25 13:34:55 -08:00
|
|
|
// Helpers for using Promise from C++.
|
2014-04-16 12:13:41 -07: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-25 13:34:55 -08:00
|
|
|
template <typename T>
|
2014-04-16 12:13:41 -07:00
|
|
|
void MaybeResolve(const T& aArg) {
|
2014-02-25 13:34:55 -08:00
|
|
|
MaybeSomething(aArg, &Promise::MaybeResolve);
|
|
|
|
}
|
|
|
|
|
2014-06-03 08:38:38 -07:00
|
|
|
inline void MaybeReject(nsresult aArg) {
|
|
|
|
MOZ_ASSERT(NS_FAILED(aArg));
|
2014-02-25 13:34:55 -08:00
|
|
|
MaybeSomething(aArg, &Promise::MaybeReject);
|
|
|
|
}
|
2014-06-03 08:38:38 -07:00
|
|
|
// DO NOT USE MaybeRejectBrokenly with in new code. Promises should be
|
|
|
|
// rejected with Error instances.
|
|
|
|
// Note: MaybeRejectBrokenly is a template so we can use it with DOMError
|
|
|
|
// without instantiating the DOMError specialization of MaybeSomething in
|
|
|
|
// every translation unit that includes this header, because that would
|
|
|
|
// require use to include DOMError.h either here or in all those translation
|
|
|
|
// units.
|
|
|
|
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-25 13:34:55 -08:00
|
|
|
|
2013-06-11 18:41:21 -07:00
|
|
|
// WebIDL
|
|
|
|
|
2014-02-25 13:34:55 -08:00
|
|
|
nsIGlobalObject* GetParentObject() const
|
2013-06-11 18:41:21 -07:00
|
|
|
{
|
2014-02-25 13:34:55 -08:00
|
|
|
return mGlobal;
|
2013-06-11 18:41:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual JSObject*
|
2014-04-08 15:27:18 -07:00
|
|
|
WrapObject(JSContext* aCx) MOZ_OVERRIDE;
|
2013-06-11 18:41:21 -07:00
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
static already_AddRefed<Promise>
|
2013-08-22 22:17:08 -07:00
|
|
|
Constructor(const GlobalObject& aGlobal, PromiseInit& aInit,
|
2013-06-11 18:41:21 -07:00
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
static already_AddRefed<Promise>
|
2014-06-16 09:52:00 -07:00
|
|
|
Resolve(const GlobalObject& aGlobal,
|
2014-02-19 07:13:38 -08:00
|
|
|
JS::Handle<JS::Value> aValue, ErrorResult& aRv);
|
2013-06-11 18:41:22 -07:00
|
|
|
|
2014-01-13 14:36:03 -08:00
|
|
|
static already_AddRefed<Promise>
|
2014-02-25 13:34:55 -08:00
|
|
|
Resolve(nsIGlobalObject* aGlobal, JSContext* aCx,
|
2014-01-13 14:36:03 -08:00
|
|
|
JS::Handle<JS::Value> aValue, ErrorResult& aRv);
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
static already_AddRefed<Promise>
|
2014-06-16 09:52:00 -07:00
|
|
|
Reject(const GlobalObject& aGlobal,
|
2014-02-19 07:13:38 -08:00
|
|
|
JS::Handle<JS::Value> aValue, ErrorResult& aRv);
|
2013-06-11 18:41:22 -07:00
|
|
|
|
2014-01-13 14:36:03 -08:00
|
|
|
static already_AddRefed<Promise>
|
2014-02-25 13:34:55 -08:00
|
|
|
Reject(nsIGlobalObject* aGlobal, JSContext* aCx,
|
2014-01-13 14:36:03 -08:00
|
|
|
JS::Handle<JS::Value> aValue, ErrorResult& aRv);
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
already_AddRefed<Promise>
|
2014-04-09 01:30:24 -07:00
|
|
|
Then(JSContext* aCx, AnyCallback* aResolveCallback,
|
2014-07-18 18:31:11 -07:00
|
|
|
AnyCallback* aRejectCallback, ErrorResult& aRv);
|
2013-06-20 03:20:01 -07:00
|
|
|
|
2013-07-16 06:56:24 -07:00
|
|
|
already_AddRefed<Promise>
|
2014-07-18 18:31:11 -07:00
|
|
|
Catch(JSContext* aCx, AnyCallback* aRejectCallback, ErrorResult& aRv);
|
2013-06-11 18:41:21 -07:00
|
|
|
|
2013-11-19 13:53:00 -08:00
|
|
|
static already_AddRefed<Promise>
|
2014-06-16 09:52:00 -07:00
|
|
|
All(const GlobalObject& aGlobal,
|
2013-11-19 13:53:00 -08:00
|
|
|
const Sequence<JS::Value>& aIterable, ErrorResult& aRv);
|
|
|
|
|
|
|
|
static already_AddRefed<Promise>
|
2014-06-16 09:52:00 -07:00
|
|
|
Race(const GlobalObject& aGlobal,
|
2013-11-19 13:53:00 -08:00
|
|
|
const Sequence<JS::Value>& aIterable, ErrorResult& aRv);
|
|
|
|
|
2013-11-19 10:43:51 -08:00
|
|
|
void AppendNativeHandler(PromiseNativeHandler* aRunnable);
|
|
|
|
|
2014-09-11 19:18:49 -07:00
|
|
|
protected:
|
2014-07-18 18:31:11 -07:00
|
|
|
// Do NOT call this unless you're Promise::Create. I wish we could enforce
|
|
|
|
// that from inside this class too, somehow.
|
2014-09-01 17:49:25 -07:00
|
|
|
explicit Promise(nsIGlobalObject* aGlobal);
|
2014-07-18 18:31:11 -07:00
|
|
|
|
2014-09-11 19:18:49 -07:00
|
|
|
virtual ~Promise();
|
|
|
|
|
|
|
|
// Queue an async task to current main or worker thread.
|
|
|
|
static void
|
|
|
|
DispatchToMainOrWorkerThread(nsIRunnable* aRunnable);
|
|
|
|
|
|
|
|
// Do JS-wrapping after Promise creation.
|
|
|
|
void CreateWrapper(ErrorResult& aRv);
|
|
|
|
|
|
|
|
// Create the JS resolving functions of resolve() and reject(). And provide
|
|
|
|
// references to the two functions by calling PromiseInit passed from Promise
|
|
|
|
// constructor.
|
|
|
|
void CallInitFunction(const GlobalObject& aGlobal, PromiseInit& aInit,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
|
|
|
bool IsPending()
|
|
|
|
{
|
|
|
|
return mResolvePending;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-07-01 21:19:48 -07:00
|
|
|
friend class PromiseDebugging;
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
enum PromiseState {
|
2013-06-11 18:41:21 -07:00
|
|
|
Pending,
|
|
|
|
Resolved,
|
|
|
|
Rejected
|
|
|
|
};
|
|
|
|
|
2013-09-11 09:03:04 -07:00
|
|
|
enum PromiseTaskSync {
|
|
|
|
SyncTask,
|
|
|
|
AsyncTask
|
|
|
|
};
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
void SetState(PromiseState aState)
|
2013-06-11 18:41:21 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mState == Pending);
|
|
|
|
MOZ_ASSERT(aState != Pending);
|
|
|
|
mState = aState;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetResult(JS::Handle<JS::Value> aValue)
|
|
|
|
{
|
|
|
|
mResult = aValue;
|
|
|
|
}
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
// This method processes promise's resolve/reject callbacks with promise's
|
2013-06-11 18:41:21 -07:00
|
|
|
// result. It's executed when the resolver.resolve() or resolver.reject() is
|
2013-07-11 13:40:36 -07:00
|
|
|
// called or when the promise already has a result and new callbacks are
|
2013-06-11 18:41:21 -07:00
|
|
|
// appended by then(), catch() or done().
|
|
|
|
void RunTask();
|
|
|
|
|
2013-09-11 09:03:04 -07:00
|
|
|
void RunResolveTask(JS::Handle<JS::Value> aValue,
|
|
|
|
Promise::PromiseState aState,
|
|
|
|
PromiseTaskSync aAsynchronous);
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
void AppendCallbacks(PromiseCallback* aResolveCallback,
|
|
|
|
PromiseCallback* aRejectCallback);
|
2013-06-11 18:41:21 -07:00
|
|
|
|
2013-08-28 21:30:06 -07:00
|
|
|
// If we have been rejected and our mResult is a JS exception,
|
|
|
|
// report it to the error console.
|
2014-03-12 07:31:03 -07:00
|
|
|
// Use MaybeReportRejectedOnce() for actual calls.
|
2013-08-28 21:30:06 -07:00
|
|
|
void MaybeReportRejected();
|
|
|
|
|
2014-03-12 07:31:03 -07:00
|
|
|
void MaybeReportRejectedOnce() {
|
|
|
|
MaybeReportRejected();
|
|
|
|
RemoveFeature();
|
|
|
|
mResult = JS::UndefinedValue();
|
|
|
|
}
|
|
|
|
|
2013-09-26 11:09:16 -07:00
|
|
|
void MaybeResolveInternal(JSContext* aCx,
|
2013-11-19 10:39:51 -08:00
|
|
|
JS::Handle<JS::Value> aValue,
|
2013-09-26 11:09:16 -07:00
|
|
|
PromiseTaskSync aSync = AsyncTask);
|
|
|
|
void MaybeRejectInternal(JSContext* aCx,
|
2013-11-19 10:39:51 -08:00
|
|
|
JS::Handle<JS::Value> aValue,
|
2013-09-26 11:09:16 -07:00
|
|
|
PromiseTaskSync aSync = AsyncTask);
|
2013-09-11 09:03:04 -07:00
|
|
|
|
|
|
|
void ResolveInternal(JSContext* aCx,
|
2013-11-19 10:39:51 -08:00
|
|
|
JS::Handle<JS::Value> aValue,
|
2013-09-11 09:03:04 -07:00
|
|
|
PromiseTaskSync aSync = AsyncTask);
|
2013-06-11 18:41:21 -07:00
|
|
|
|
2013-09-11 09:03:04 -07:00
|
|
|
void RejectInternal(JSContext* aCx,
|
2013-11-19 10:39:51 -08:00
|
|
|
JS::Handle<JS::Value> aValue,
|
2013-09-11 09:03:04 -07:00
|
|
|
PromiseTaskSync aSync = AsyncTask);
|
|
|
|
|
2014-02-25 13:34:55 -08:00
|
|
|
template <typename T>
|
|
|
|
void MaybeSomething(T& aArgument, MaybeFunc aFunc) {
|
|
|
|
ThreadsafeAutoJSContext cx;
|
2014-07-18 18:31:11 -07:00
|
|
|
JSObject* wrapper = GetWrapper();
|
|
|
|
MOZ_ASSERT(wrapper); // We preserved it!
|
2014-02-25 13:34:55 -08:00
|
|
|
|
|
|
|
JSAutoCompartment ac(cx, wrapper);
|
|
|
|
JS::Rooted<JS::Value> val(cx);
|
2014-04-10 11:57:07 -07:00
|
|
|
if (!ToJSValue(cx, aArgument, &val)) {
|
2014-02-25 13:34:55 -08:00
|
|
|
HandleException(cx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
(this->*aFunc)(cx, val);
|
|
|
|
}
|
|
|
|
|
2013-09-11 09:03:04 -07:00
|
|
|
// Static methods for the PromiseInit functions.
|
|
|
|
static bool
|
|
|
|
JSCallback(JSContext *aCx, unsigned aArgc, JS::Value *aVp);
|
2014-01-23 10:47:29 -08:00
|
|
|
|
|
|
|
static bool
|
|
|
|
ThenableResolverCommon(JSContext* aCx, uint32_t /* PromiseCallback::Task */ aTask,
|
|
|
|
unsigned aArgc, JS::Value* aVp);
|
|
|
|
static bool
|
|
|
|
JSCallbackThenableResolver(JSContext *aCx, unsigned aArgc, JS::Value *aVp);
|
|
|
|
static bool
|
|
|
|
JSCallbackThenableRejecter(JSContext *aCx, unsigned aArgc, JS::Value *aVp);
|
|
|
|
|
2013-09-11 09:03:04 -07:00
|
|
|
static JSObject*
|
|
|
|
CreateFunction(JSContext* aCx, JSObject* aParent, Promise* aPromise,
|
|
|
|
int32_t aTask);
|
|
|
|
|
2014-01-23 10:47:29 -08:00
|
|
|
static JSObject*
|
|
|
|
CreateThenableFunction(JSContext* aCx, Promise* aPromise, uint32_t aTask);
|
|
|
|
|
|
|
|
void HandleException(JSContext* aCx);
|
|
|
|
|
2014-03-12 07:31:03 -07:00
|
|
|
void RemoveFeature();
|
|
|
|
|
2014-02-25 13:34:55 -08:00
|
|
|
nsRefPtr<nsIGlobalObject> mGlobal;
|
2013-06-11 18:41:21 -07:00
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
nsTArray<nsRefPtr<PromiseCallback> > mResolveCallbacks;
|
|
|
|
nsTArray<nsRefPtr<PromiseCallback> > mRejectCallbacks;
|
2013-06-11 18:41:21 -07:00
|
|
|
|
2013-06-18 03:00:37 -07:00
|
|
|
JS::Heap<JS::Value> mResult;
|
2013-07-11 13:40:36 -07:00
|
|
|
PromiseState mState;
|
2013-06-11 18:41:21 -07:00
|
|
|
bool mTaskPending;
|
2013-08-28 21:30:06 -07:00
|
|
|
bool mHadRejectCallback;
|
2013-09-11 09:03:04 -07:00
|
|
|
|
|
|
|
bool mResolvePending;
|
2014-03-12 07:31:03 -07:00
|
|
|
|
|
|
|
// If a rejected promise on a worker has no reject callbacks attached, it
|
|
|
|
// needs to know when the worker is shutting down, to report the error on the
|
|
|
|
// console before the worker's context is deleted. This feature is used for
|
|
|
|
// that purpose.
|
|
|
|
nsAutoPtr<PromiseReportRejectFeature> mFeature;
|
2013-06-11 18:41:21 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
#endif // mozilla_dom_Promise_h
|