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"
|
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;
|
2013-11-19 10:43:51 -08:00
|
|
|
class PromiseCallback;
|
|
|
|
class PromiseInit;
|
|
|
|
class PromiseNativeHandler;
|
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:
|
|
|
|
PromiseReportRejectFeature(Promise* aPromise)
|
|
|
|
: mPromise(aPromise)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mPromise);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool
|
|
|
|
Notify(JSContext* aCx, workers::Status aStatus) MOZ_OVERRIDE;
|
|
|
|
};
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
class Promise MOZ_FINAL : public nsISupports,
|
|
|
|
public nsWrapperCache
|
2013-06-11 18:41:21 -07:00
|
|
|
{
|
2013-11-19 10:43:51 -08:00
|
|
|
friend class NativePromiseCallback;
|
2013-11-24 11:26:07 -08:00
|
|
|
friend class PromiseResolverMixin;
|
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;
|
2013-09-11 09:03:04 -07:00
|
|
|
friend class RejectPromiseCallback;
|
2013-11-24 11:26:07 -08:00
|
|
|
friend class ResolvePromiseCallback;
|
|
|
|
friend class WorkerPromiseResolverTask;
|
|
|
|
friend class WorkerPromiseTask;
|
2013-09-11 09:03:04 -07:00
|
|
|
friend class WrapperPromiseCallback;
|
2013-06-11 18:41:21 -07:00
|
|
|
|
2014-02-25 13:34:55 -08:00
|
|
|
~Promise();
|
|
|
|
|
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)
|
2013-06-11 18:41:21 -07:00
|
|
|
|
2014-02-25 13:34:55 -08:00
|
|
|
Promise(nsIGlobalObject* aGlobal);
|
|
|
|
|
|
|
|
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-04-16 12:13:41 -07:00
|
|
|
// aArg is a const reference so we can pass rvalues like NS_ERROR_*
|
2014-02-25 13:34:55 -08:00
|
|
|
template <typename T>
|
2014-04-16 12:13:41 -07:00
|
|
|
void MaybeReject(const T& aArg) {
|
2014-02-25 13:34:55 -08:00
|
|
|
MaybeSomething(aArg, &Promise::MaybeReject);
|
|
|
|
}
|
|
|
|
|
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>
|
2013-06-11 18:41:22 -07:00
|
|
|
Resolve(const GlobalObject& aGlobal, JSContext* aCx,
|
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>
|
2013-06-11 18:41:22 -07:00
|
|
|
Reject(const GlobalObject& aGlobal, JSContext* aCx,
|
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,
|
|
|
|
AnyCallback* aRejectCallback);
|
2013-06-20 03:20:01 -07:00
|
|
|
|
2013-07-16 06:56:24 -07:00
|
|
|
already_AddRefed<Promise>
|
2014-04-09 01:30:24 -07:00
|
|
|
Catch(JSContext* aCx, AnyCallback* aRejectCallback);
|
2013-06-11 18:41:21 -07:00
|
|
|
|
2013-11-19 13:53:00 -08:00
|
|
|
static already_AddRefed<Promise>
|
|
|
|
All(const GlobalObject& aGlobal, JSContext* aCx,
|
|
|
|
const Sequence<JS::Value>& aIterable, ErrorResult& aRv);
|
|
|
|
|
|
|
|
static already_AddRefed<Promise>
|
|
|
|
Race(const GlobalObject& aGlobal, JSContext* aCx,
|
|
|
|
const Sequence<JS::Value>& aIterable, ErrorResult& aRv);
|
|
|
|
|
2013-11-19 10:43:51 -08:00
|
|
|
void AppendNativeHandler(PromiseNativeHandler* aRunnable);
|
|
|
|
|
2013-06-11 18:41:21 -07:00
|
|
|
private:
|
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
|
|
|
// Helper methods for using Promises from C++
|
|
|
|
JSObject* GetOrCreateWrapper(JSContext* aCx);
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void MaybeSomething(T& aArgument, MaybeFunc aFunc) {
|
|
|
|
ThreadsafeAutoJSContext cx;
|
|
|
|
|
|
|
|
JSObject* wrapper = GetOrCreateWrapper(cx);
|
|
|
|
if (!wrapper) {
|
|
|
|
HandleException(cx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|