Bug 1639230: Remove vendor prefix for printing with webdriver r=jgraham,webdriver...
[gecko.git] / dom / promise / PromiseNativeHandler.h
blobf29ec403fa5e3bff44a5a3c3f8fa04c369e9d689
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/. */
7 #ifndef mozilla_dom_PromiseNativeHandler_h
8 #define mozilla_dom_PromiseNativeHandler_h
10 #include <functional>
11 #include "js/TypeDecls.h"
12 #include "nsISupports.h"
14 namespace mozilla {
15 namespace dom {
17 class Promise;
20 * PromiseNativeHandler allows C++ to react to a Promise being
21 * rejected/resolved. A PromiseNativeHandler can be appended to a Promise using
22 * Promise::AppendNativeHandler().
24 class PromiseNativeHandler : public nsISupports {
25 protected:
26 virtual ~PromiseNativeHandler() = default;
28 public:
29 MOZ_CAN_RUN_SCRIPT
30 virtual void ResolvedCallback(JSContext* aCx,
31 JS::Handle<JS::Value> aValue) = 0;
33 MOZ_CAN_RUN_SCRIPT
34 virtual void RejectedCallback(JSContext* aCx,
35 JS::Handle<JS::Value> aValue) = 0;
38 // This class is used to set C++ callbacks once a dom Promise a resolved or
39 // rejected.
40 class DomPromiseListener final : public PromiseNativeHandler {
41 NS_DECL_ISUPPORTS
43 public:
44 using CallbackTypeResolved =
45 std::function<void(JSContext*, JS::Handle<JS::Value>)>;
46 using CallbackTypeRejected = std::function<void(nsresult)>;
48 explicit DomPromiseListener(Promise* aDOMPromise);
49 DomPromiseListener(Promise* aDOMPromise, CallbackTypeResolved&& aResolve,
50 CallbackTypeRejected&& aReject);
51 void SetResolvers(CallbackTypeResolved&& aResolve,
52 CallbackTypeRejected&& aReject);
53 void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
54 void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
56 private:
57 ~DomPromiseListener();
58 Maybe<CallbackTypeResolved> mResolve;
59 Maybe<CallbackTypeRejected> mReject;
62 } // namespace dom
63 } // namespace mozilla
65 #endif // mozilla_dom_PromiseNativeHandler_h