Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=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/. */
6 :
7 : #ifndef mozilla_dom_Promise_h
8 : #define mozilla_dom_Promise_h
9 :
10 : #include "mozilla/Attributes.h"
11 : #include "mozilla/ErrorResult.h"
12 : #include "mozilla/TimeStamp.h"
13 : #include "mozilla/TypeTraits.h"
14 : #include "mozilla/dom/BindingDeclarations.h"
15 : #include "nsCycleCollectionParticipant.h"
16 : #include "mozilla/dom/PromiseBinding.h"
17 : #include "mozilla/dom/ToJSValue.h"
18 : #include "mozilla/WeakPtr.h"
19 : #include "nsWrapperCache.h"
20 : #include "nsAutoPtr.h"
21 : #include "js/TypeDecls.h"
22 : #include "jspubtd.h"
23 :
24 : class nsIGlobalObject;
25 :
26 : namespace mozilla {
27 : namespace dom {
28 :
29 : class AnyCallback;
30 : class DOMError;
31 : class MediaStreamError;
32 : class PromiseInit;
33 : class PromiseNativeHandler;
34 : class PromiseDebugging;
35 :
36 : #define NS_PROMISE_IID \
37 : { 0x1b8d6215, 0x3e67, 0x43ba, \
38 : { 0x8a, 0xf9, 0x31, 0x5e, 0x8f, 0xce, 0x75, 0x65 } }
39 :
40 : class Promise : public nsISupports,
41 : public SupportsWeakPtr<Promise>
42 : {
43 : friend class PromiseTask;
44 : friend class PromiseWorkerProxy;
45 : friend class PromiseWorkerProxyRunnable;
46 :
47 : public:
48 : NS_DECLARE_STATIC_IID_ACCESSOR(NS_PROMISE_IID)
49 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
50 75 : NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Promise)
51 4 : MOZ_DECLARE_WEAKREFERENCE_TYPENAME(Promise)
52 :
53 : // Promise creation tries to create a JS reflector for the Promise, so is
54 : // fallible. Furthermore, we don't want to do JS-wrapping on a 0-refcount
55 : // object, so we addref before doing that and return the addrefed pointer
56 : // here.
57 : static already_AddRefed<Promise>
58 : Create(nsIGlobalObject* aGlobal, ErrorResult& aRv);
59 :
60 : // Reports a rejected Promise by sending an error report.
61 : static void ReportRejectedPromise(JSContext* aCx, JS::HandleObject aPromise);
62 :
63 : typedef void (Promise::*MaybeFunc)(JSContext* aCx,
64 : JS::Handle<JS::Value> aValue);
65 :
66 : void MaybeResolve(JSContext* aCx,
67 : JS::Handle<JS::Value> aValue);
68 : void MaybeReject(JSContext* aCx,
69 : JS::Handle<JS::Value> aValue);
70 :
71 : // Helpers for using Promise from C++.
72 : // Most DOM objects are handled already. To add a new type T, add a
73 : // ToJSValue overload in ToJSValue.h.
74 : // aArg is a const reference so we can pass rvalues like integer constants
75 : template <typename T>
76 1 : void MaybeResolve(const T& aArg) {
77 1 : MaybeSomething(aArg, &Promise::MaybeResolve);
78 1 : }
79 :
80 : void MaybeResolveWithUndefined();
81 :
82 0 : inline void MaybeReject(nsresult aArg) {
83 0 : MOZ_ASSERT(NS_FAILED(aArg));
84 0 : MaybeSomething(aArg, &Promise::MaybeReject);
85 0 : }
86 :
87 0 : inline void MaybeReject(ErrorResult& aArg) {
88 0 : MOZ_ASSERT(aArg.Failed());
89 0 : MaybeSomething(aArg, &Promise::MaybeReject);
90 0 : }
91 :
92 : void MaybeReject(const RefPtr<MediaStreamError>& aArg);
93 :
94 : void MaybeRejectWithUndefined();
95 :
96 : // DO NOT USE MaybeRejectBrokenly with in new code. Promises should be
97 : // rejected with Error instances.
98 : // Note: MaybeRejectBrokenly is a template so we can use it with DOMError
99 : // without instantiating the DOMError specialization of MaybeSomething in
100 : // every translation unit that includes this header, because that would
101 : // require use to include DOMError.h either here or in all those translation
102 : // units.
103 : template<typename T>
104 : void MaybeRejectBrokenly(const T& aArg); // Not implemented by default; see
105 : // specializations in the .cpp for
106 : // the T values we support.
107 :
108 : // Called by DOM to let us execute our callbacks. May be called recursively.
109 : // Returns true if at least one microtask was processed.
110 : static bool PerformMicroTaskCheckpoint();
111 :
112 : static void PerformWorkerMicroTaskCheckpoint();
113 :
114 : static void PerformWorkerDebuggerMicroTaskCheckpoint();
115 :
116 : // WebIDL
117 :
118 1 : nsIGlobalObject* GetParentObject() const
119 : {
120 1 : return mGlobal;
121 : }
122 :
123 : // Do the equivalent of Promise.resolve in the current compartment of aCx.
124 : // Errorrs are reported on the ErrorResult; if aRv comes back !Failed(), this
125 : // function MUST return a non-null value.
126 : static already_AddRefed<Promise>
127 : Resolve(nsIGlobalObject* aGlobal, JSContext* aCx,
128 : JS::Handle<JS::Value> aValue, ErrorResult& aRv);
129 :
130 : // Do the equivalent of Promise.reject in the current compartment of aCx.
131 : // Errorrs are reported on the ErrorResult; if aRv comes back !Failed(), this
132 : // function MUST return a non-null value.
133 : static already_AddRefed<Promise>
134 : Reject(nsIGlobalObject* aGlobal, JSContext* aCx,
135 : JS::Handle<JS::Value> aValue, ErrorResult& aRv);
136 :
137 : static already_AddRefed<Promise>
138 : All(const GlobalObject& aGlobal,
139 : const nsTArray<RefPtr<Promise>>& aPromiseList, ErrorResult& aRv);
140 :
141 : void
142 : Then(JSContext* aCx,
143 : // aCalleeGlobal may not be in the compartment of aCx, when called over
144 : // Xrays.
145 : JS::Handle<JSObject*> aCalleeGlobal,
146 : AnyCallback* aResolveCallback, AnyCallback* aRejectCallback,
147 : JS::MutableHandle<JS::Value> aRetval,
148 : ErrorResult& aRv);
149 :
150 5 : JSObject* PromiseObj() const
151 : {
152 5 : return mPromiseObj;
153 : }
154 :
155 : void AppendNativeHandler(PromiseNativeHandler* aRunnable);
156 :
157 : JSObject* GlobalJSObject() const;
158 :
159 : JSCompartment* Compartment() const;
160 :
161 : // Create a dom::Promise from a given SpiderMonkey Promise object.
162 : // aPromiseObj MUST be in the compartment of aGlobal's global JS object.
163 : static already_AddRefed<Promise>
164 : CreateFromExisting(nsIGlobalObject* aGlobal,
165 : JS::Handle<JSObject*> aPromiseObj);
166 :
167 : enum class PromiseState {
168 : Pending,
169 : Resolved,
170 : Rejected
171 : };
172 :
173 : PromiseState State() const;
174 :
175 : protected:
176 : struct PromiseCapability;
177 :
178 : // Do NOT call this unless you're Promise::Create or
179 : // Promise::CreateFromExisting. I wish we could enforce that from inside this
180 : // class too, somehow.
181 : explicit Promise(nsIGlobalObject* aGlobal);
182 :
183 : virtual ~Promise();
184 :
185 : // Do JS-wrapping after Promise creation. Passing null for aDesiredProto will
186 : // use the default prototype for the sort of Promise we have.
187 : void CreateWrapper(JS::Handle<JSObject*> aDesiredProto, ErrorResult& aRv);
188 :
189 : private:
190 : template <typename T>
191 1 : void MaybeSomething(T& aArgument, MaybeFunc aFunc) {
192 1 : MOZ_ASSERT(PromiseObj()); // It was preserved!
193 :
194 2 : AutoEntryScript aes(mGlobal, "Promise resolution or rejection");
195 1 : JSContext* cx = aes.cx();
196 :
197 2 : JS::Rooted<JS::Value> val(cx);
198 1 : if (!ToJSValue(cx, aArgument, &val)) {
199 0 : HandleException(cx);
200 0 : return;
201 : }
202 :
203 1 : (this->*aFunc)(cx, val);
204 : }
205 :
206 : void HandleException(JSContext* aCx);
207 :
208 : RefPtr<nsIGlobalObject> mGlobal;
209 :
210 : JS::Heap<JSObject*> mPromiseObj;
211 : };
212 :
213 : NS_DEFINE_STATIC_IID_ACCESSOR(Promise, NS_PROMISE_IID)
214 :
215 : } // namespace dom
216 : } // namespace mozilla
217 :
218 : #endif // mozilla_dom_Promise_h
|