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"
|
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2013-07-11 13:40:36 -07:00
|
|
|
#include "mozilla/dom/PromiseBinding.h"
|
2013-06-11 18:41:21 -07:00
|
|
|
#include "nsWrapperCache.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2013-08-22 22:17:11 -07:00
|
|
|
#include "nsPIDOMWindow.h"
|
2013-08-27 19:59:14 -07:00
|
|
|
#include "js/TypeDecls.h"
|
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
|
|
|
|
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-07-11 13:40:36 -07:00
|
|
|
friend class PromiseTask;
|
|
|
|
friend class PromiseResolverTask;
|
2013-09-11 09:03:04 -07:00
|
|
|
friend class ResolvePromiseCallback;
|
|
|
|
friend class RejectPromiseCallback;
|
|
|
|
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)
|
2013-06-11 18:41:21 -07:00
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
Promise(nsPIDOMWindow* aWindow);
|
|
|
|
~Promise();
|
2013-06-11 18:41:21 -07:00
|
|
|
|
2013-06-12 06:44:56 -07:00
|
|
|
static bool PrefEnabled();
|
2013-08-07 14:40:20 -07:00
|
|
|
static bool EnabledForScope(JSContext* aCx, JSObject* /* unused */);
|
2013-06-12 06:44:56 -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
|
|
|
|
2013-06-11 18:41:21 -07:00
|
|
|
// WebIDL
|
|
|
|
|
|
|
|
nsPIDOMWindow* GetParentObject() const
|
|
|
|
{
|
|
|
|
return mWindow;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual JSObject*
|
|
|
|
WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope) MOZ_OVERRIDE;
|
|
|
|
|
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,
|
2013-11-19 13:29:47 -08:00
|
|
|
const Optional<JS::Handle<JS::Value>>& aValue, ErrorResult& aRv);
|
2013-06-11 18:41:22 -07:00
|
|
|
|
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,
|
2013-11-19 13:29:47 -08:00
|
|
|
const Optional<JS::Handle<JS::Value>>& aValue, ErrorResult& aRv);
|
2013-06-11 18:41:22 -07:00
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
already_AddRefed<Promise>
|
2013-11-19 13:29:47 -08:00
|
|
|
Then(const Optional<nsRefPtr<AnyCallback>>& aResolveCallback,
|
|
|
|
const Optional<nsRefPtr<AnyCallback>>& aRejectCallback);
|
2013-06-11 18:41:22 -07:00
|
|
|
|
2013-06-20 03:20:01 -07:00
|
|
|
|
2013-07-16 06:56:24 -07:00
|
|
|
already_AddRefed<Promise>
|
2013-11-19 13:29:47 -08:00
|
|
|
Catch(const Optional<nsRefPtr<AnyCallback>>& aRejectCallback);
|
2013-06-11 18:41:21 -07:00
|
|
|
|
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.
|
|
|
|
void MaybeReportRejected();
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
// Static methods for the PromiseInit functions.
|
|
|
|
static bool
|
|
|
|
JSCallback(JSContext *aCx, unsigned aArgc, JS::Value *aVp);
|
|
|
|
static JSObject*
|
|
|
|
CreateFunction(JSContext* aCx, JSObject* aParent, Promise* aPromise,
|
|
|
|
int32_t aTask);
|
|
|
|
|
|
|
|
nsRefPtr<nsPIDOMWindow> mWindow;
|
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;
|
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
|