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_PromiseCallback_h
8 #define mozilla_dom_PromiseCallback_h
10 #include "mozilla/dom/Promise.h"
11 #include "nsCycleCollectionParticipant.h"
16 // This is the base class for any PromiseCallback.
17 // It's a logical step in the promise chain of callbacks.
18 class PromiseCallback
: public nsISupports
21 virtual ~PromiseCallback();
24 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
25 NS_DECL_CYCLE_COLLECTION_CLASS(PromiseCallback
)
29 virtual void Call(JSContext
* aCx
,
30 JS::Handle
<JS::Value
> aValue
) = 0;
37 // This factory returns a PromiseCallback object with refcount of 0.
38 static PromiseCallback
*
39 Factory(Promise
* aNextPromise
, JS::Handle
<JSObject
*> aObject
,
40 AnyCallback
* aCallback
, Task aTask
);
43 // WrapperPromiseCallback execs a JS Callback with a value, and then the return
44 // value is sent to the aNextPromise->ResolveFunction() or to
45 // aNextPromise->RejectFunction() if the JS Callback throws.
46 class WrapperPromiseCallback MOZ_FINAL
: public PromiseCallback
49 NS_DECL_ISUPPORTS_INHERITED
50 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(WrapperPromiseCallback
,
53 void Call(JSContext
* aCx
,
54 JS::Handle
<JS::Value
> aValue
) MOZ_OVERRIDE
;
56 WrapperPromiseCallback(Promise
* aNextPromise
, JS::Handle
<JSObject
*> aGlobal
,
57 AnyCallback
* aCallback
);
60 ~WrapperPromiseCallback();
62 nsRefPtr
<Promise
> mNextPromise
;
63 JS::Heap
<JSObject
*> mGlobal
;
64 nsRefPtr
<AnyCallback
> mCallback
;
67 // ResolvePromiseCallback calls aPromise->ResolveFunction() with the value
68 // received by Call().
69 class ResolvePromiseCallback MOZ_FINAL
: public PromiseCallback
72 NS_DECL_ISUPPORTS_INHERITED
73 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(ResolvePromiseCallback
,
76 void Call(JSContext
* aCx
,
77 JS::Handle
<JS::Value
> aValue
) MOZ_OVERRIDE
;
79 ResolvePromiseCallback(Promise
* aPromise
, JS::Handle
<JSObject
*> aGlobal
);
82 ~ResolvePromiseCallback();
84 nsRefPtr
<Promise
> mPromise
;
85 JS::Heap
<JSObject
*> mGlobal
;
88 // RejectPromiseCallback calls aPromise->RejectFunction() with the value
89 // received by Call().
90 class RejectPromiseCallback MOZ_FINAL
: public PromiseCallback
93 NS_DECL_ISUPPORTS_INHERITED
94 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(RejectPromiseCallback
,
97 void Call(JSContext
* aCx
,
98 JS::Handle
<JS::Value
> aValue
) MOZ_OVERRIDE
;
100 RejectPromiseCallback(Promise
* aPromise
, JS::Handle
<JSObject
*> aGlobal
);
103 ~RejectPromiseCallback();
105 nsRefPtr
<Promise
> mPromise
;
106 JS::Heap
<JSObject
*> mGlobal
;
109 // NativePromiseCallback wraps a NativePromiseHandler.
110 class NativePromiseCallback MOZ_FINAL
: public PromiseCallback
113 NS_DECL_ISUPPORTS_INHERITED
114 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(NativePromiseCallback
,
117 void Call(JSContext
* aCx
,
118 JS::Handle
<JS::Value
> aValue
) MOZ_OVERRIDE
;
120 NativePromiseCallback(PromiseNativeHandler
* aHandler
,
121 Promise::PromiseState aState
);
124 ~NativePromiseCallback();
126 nsRefPtr
<PromiseNativeHandler
> mHandler
;
127 Promise::PromiseState mState
;
131 } // namespace mozilla
133 #endif // mozilla_dom_PromiseCallback_h