Backout a74bd5095902, Bug 959405 - Please update the Buri Moz-central, 1.3, 1.2 with...
[gecko.git] / dom / promise / Promise.h
blob368a2f4ae4f4a88cdbd3f01e73fae2d8412766a5
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"
20 namespace mozilla {
21 namespace dom {
23 class AnyCallback;
24 class PromiseCallback;
25 class PromiseInit;
26 class PromiseNativeHandler;
28 class Promise MOZ_FINAL : public nsISupports,
29 public nsWrapperCache
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;
41 public:
42 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
43 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Promise)
45 Promise(nsPIDOMWindow* aWindow);
46 ~Promise();
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);
55 // WebIDL
57 nsPIDOMWindow* GetParentObject() const
59 return mWindow;
62 virtual JSObject*
63 WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope) MOZ_OVERRIDE;
65 static already_AddRefed<Promise>
66 Constructor(const GlobalObject& aGlobal, PromiseInit& aInit,
67 ErrorResult& aRv);
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);
87 private:
88 enum PromiseState {
89 Pending,
90 Resolved,
91 Rejected
94 enum PromiseTaskSync {
95 SyncTask,
96 AsyncTask
99 void SetState(PromiseState aState)
101 MOZ_ASSERT(mState == Pending);
102 MOZ_ASSERT(aState != Pending);
103 mState = aState;
106 void SetResult(JS::Handle<JS::Value> aValue)
108 mResult = 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().
115 void RunTask();
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.
144 static bool
145 JSCallback(JSContext *aCx, unsigned aArgc, JS::Value *aVp);
146 static JSObject*
147 CreateFunction(JSContext* aCx, JSObject* aParent, Promise* aPromise,
148 int32_t aTask);
150 nsRefPtr<nsPIDOMWindow> mWindow;
152 nsTArray<nsRefPtr<PromiseCallback> > mResolveCallbacks;
153 nsTArray<nsRefPtr<PromiseCallback> > mRejectCallbacks;
155 JS::Heap<JS::Value> mResult;
156 PromiseState mState;
157 bool mTaskPending;
158 bool mHadRejectCallback;
160 bool mResolvePending;
163 } // namespace dom
164 } // namespace mozilla
166 #endif // mozilla_dom_Promise_h