1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_Promise_h
8 #define mozilla_dom_Promise_h
10 #include "mozilla/Attributes.h"
11 #include "mozilla/ErrorResult.h"
12 #include "mozilla/dom/BindingDeclarations.h"
13 #include "nsCycleCollectionParticipant.h"
14 #include "mozilla/dom/PromiseBinding.h"
15 #include "nsWrapperCache.h"
16 #include "nsAutoPtr.h"
17 #include "nsPIDOMWindow.h"
18 #include "js/TypeDecls.h"
24 class PromiseCallback
;
26 class PromiseNativeHandler
;
28 class Promise MOZ_FINAL
: public nsISupports
,
31 friend class NativePromiseCallback
;
32 friend class PromiseResolverMixin
;
33 friend class PromiseResolverTask
;
34 friend class PromiseTask
;
35 friend class RejectPromiseCallback
;
36 friend class ResolvePromiseCallback
;
37 friend class WorkerPromiseResolverTask
;
38 friend class WorkerPromiseTask
;
39 friend class WrapperPromiseCallback
;
42 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
43 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Promise
)
45 Promise(nsPIDOMWindow
* aWindow
);
48 static bool EnabledForScope(JSContext
* aCx
, JSObject
* /* unused */);
50 void MaybeResolve(JSContext
* aCx
,
51 JS::Handle
<JS::Value
> aValue
);
52 void MaybeReject(JSContext
* aCx
,
53 JS::Handle
<JS::Value
> aValue
);
57 nsPIDOMWindow
* GetParentObject() const
63 WrapObject(JSContext
* aCx
, JS::Handle
<JSObject
*> aScope
) MOZ_OVERRIDE
;
65 static already_AddRefed
<Promise
>
66 Constructor(const GlobalObject
& aGlobal
, PromiseInit
& aInit
,
69 static already_AddRefed
<Promise
>
70 Resolve(const GlobalObject
& aGlobal
, JSContext
* aCx
,
71 const Optional
<JS::Handle
<JS::Value
>>& aValue
, ErrorResult
& aRv
);
73 static already_AddRefed
<Promise
>
74 Reject(const GlobalObject
& aGlobal
, JSContext
* aCx
,
75 const Optional
<JS::Handle
<JS::Value
>>& aValue
, ErrorResult
& aRv
);
77 already_AddRefed
<Promise
>
78 Then(const Optional
<nsRefPtr
<AnyCallback
>>& aResolveCallback
,
79 const Optional
<nsRefPtr
<AnyCallback
>>& aRejectCallback
);
82 already_AddRefed
<Promise
>
83 Catch(const Optional
<nsRefPtr
<AnyCallback
>>& aRejectCallback
);
85 void AppendNativeHandler(PromiseNativeHandler
* aRunnable
);
94 enum PromiseTaskSync
{
99 void SetState(PromiseState aState
)
101 MOZ_ASSERT(mState
== Pending
);
102 MOZ_ASSERT(aState
!= Pending
);
106 void SetResult(JS::Handle
<JS::Value
> aValue
)
111 // This method processes promise's resolve/reject callbacks with promise's
112 // result. It's executed when the resolver.resolve() or resolver.reject() is
113 // called or when the promise already has a result and new callbacks are
114 // appended by then(), catch() or done().
117 void RunResolveTask(JS::Handle
<JS::Value
> aValue
,
118 Promise::PromiseState aState
,
119 PromiseTaskSync aAsynchronous
);
121 void AppendCallbacks(PromiseCallback
* aResolveCallback
,
122 PromiseCallback
* aRejectCallback
);
124 // If we have been rejected and our mResult is a JS exception,
125 // report it to the error console.
126 void MaybeReportRejected();
128 void MaybeResolveInternal(JSContext
* aCx
,
129 JS::Handle
<JS::Value
> aValue
,
130 PromiseTaskSync aSync
= AsyncTask
);
131 void MaybeRejectInternal(JSContext
* aCx
,
132 JS::Handle
<JS::Value
> aValue
,
133 PromiseTaskSync aSync
= AsyncTask
);
135 void ResolveInternal(JSContext
* aCx
,
136 JS::Handle
<JS::Value
> aValue
,
137 PromiseTaskSync aSync
= AsyncTask
);
139 void RejectInternal(JSContext
* aCx
,
140 JS::Handle
<JS::Value
> aValue
,
141 PromiseTaskSync aSync
= AsyncTask
);
143 // Static methods for the PromiseInit functions.
145 JSCallback(JSContext
*aCx
, unsigned aArgc
, JS::Value
*aVp
);
147 CreateFunction(JSContext
* aCx
, JSObject
* aParent
, Promise
* aPromise
,
150 nsRefPtr
<nsPIDOMWindow
> mWindow
;
152 nsTArray
<nsRefPtr
<PromiseCallback
> > mResolveCallbacks
;
153 nsTArray
<nsRefPtr
<PromiseCallback
> > mRejectCallbacks
;
155 JS::Heap
<JS::Value
> mResult
;
158 bool mHadRejectCallback
;
160 bool mResolvePending
;
164 } // namespace mozilla
166 #endif // mozilla_dom_Promise_h