Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ |
michael@0 | 2 | /* vim: set ts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 5 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef mozilla_dom_PromiseCallback_h |
michael@0 | 8 | #define mozilla_dom_PromiseCallback_h |
michael@0 | 9 | |
michael@0 | 10 | #include "mozilla/dom/Promise.h" |
michael@0 | 11 | #include "nsCycleCollectionParticipant.h" |
michael@0 | 12 | |
michael@0 | 13 | namespace mozilla { |
michael@0 | 14 | namespace dom { |
michael@0 | 15 | |
michael@0 | 16 | // This is the base class for any PromiseCallback. |
michael@0 | 17 | // It's a logical step in the promise chain of callbacks. |
michael@0 | 18 | class PromiseCallback : public nsISupports |
michael@0 | 19 | { |
michael@0 | 20 | public: |
michael@0 | 21 | NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
michael@0 | 22 | NS_DECL_CYCLE_COLLECTION_CLASS(PromiseCallback) |
michael@0 | 23 | |
michael@0 | 24 | PromiseCallback(); |
michael@0 | 25 | virtual ~PromiseCallback(); |
michael@0 | 26 | |
michael@0 | 27 | virtual void Call(JS::Handle<JS::Value> aValue) = 0; |
michael@0 | 28 | |
michael@0 | 29 | enum Task { |
michael@0 | 30 | Resolve, |
michael@0 | 31 | Reject |
michael@0 | 32 | }; |
michael@0 | 33 | |
michael@0 | 34 | // This factory returns a PromiseCallback object with refcount of 0. |
michael@0 | 35 | static PromiseCallback* |
michael@0 | 36 | Factory(Promise* aNextPromise, JS::Handle<JSObject*> aObject, |
michael@0 | 37 | AnyCallback* aCallback, Task aTask); |
michael@0 | 38 | }; |
michael@0 | 39 | |
michael@0 | 40 | // WrapperPromiseCallback execs a JS Callback with a value, and then the return |
michael@0 | 41 | // value is sent to the aNextPromise->ResolveFunction() or to |
michael@0 | 42 | // aNextPromise->RejectFunction() if the JS Callback throws. |
michael@0 | 43 | class WrapperPromiseCallback MOZ_FINAL : public PromiseCallback |
michael@0 | 44 | { |
michael@0 | 45 | public: |
michael@0 | 46 | NS_DECL_ISUPPORTS_INHERITED |
michael@0 | 47 | NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(WrapperPromiseCallback, |
michael@0 | 48 | PromiseCallback) |
michael@0 | 49 | |
michael@0 | 50 | void Call(JS::Handle<JS::Value> aValue) MOZ_OVERRIDE; |
michael@0 | 51 | |
michael@0 | 52 | WrapperPromiseCallback(Promise* aNextPromise, JS::Handle<JSObject*> aGlobal, |
michael@0 | 53 | AnyCallback* aCallback); |
michael@0 | 54 | ~WrapperPromiseCallback(); |
michael@0 | 55 | |
michael@0 | 56 | private: |
michael@0 | 57 | nsRefPtr<Promise> mNextPromise; |
michael@0 | 58 | JS::Heap<JSObject*> mGlobal; |
michael@0 | 59 | nsRefPtr<AnyCallback> mCallback; |
michael@0 | 60 | }; |
michael@0 | 61 | |
michael@0 | 62 | // ResolvePromiseCallback calls aPromise->ResolveFunction() with the value |
michael@0 | 63 | // received by Call(). |
michael@0 | 64 | class ResolvePromiseCallback MOZ_FINAL : public PromiseCallback |
michael@0 | 65 | { |
michael@0 | 66 | public: |
michael@0 | 67 | NS_DECL_ISUPPORTS_INHERITED |
michael@0 | 68 | NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(ResolvePromiseCallback, |
michael@0 | 69 | PromiseCallback) |
michael@0 | 70 | |
michael@0 | 71 | void Call(JS::Handle<JS::Value> aValue) MOZ_OVERRIDE; |
michael@0 | 72 | |
michael@0 | 73 | ResolvePromiseCallback(Promise* aPromise, JS::Handle<JSObject*> aGlobal); |
michael@0 | 74 | ~ResolvePromiseCallback(); |
michael@0 | 75 | |
michael@0 | 76 | private: |
michael@0 | 77 | nsRefPtr<Promise> mPromise; |
michael@0 | 78 | JS::Heap<JSObject*> mGlobal; |
michael@0 | 79 | }; |
michael@0 | 80 | |
michael@0 | 81 | // RejectPromiseCallback calls aPromise->RejectFunction() with the value |
michael@0 | 82 | // received by Call(). |
michael@0 | 83 | class RejectPromiseCallback MOZ_FINAL : public PromiseCallback |
michael@0 | 84 | { |
michael@0 | 85 | public: |
michael@0 | 86 | NS_DECL_ISUPPORTS_INHERITED |
michael@0 | 87 | NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(RejectPromiseCallback, |
michael@0 | 88 | PromiseCallback) |
michael@0 | 89 | |
michael@0 | 90 | void Call(JS::Handle<JS::Value> aValue) MOZ_OVERRIDE; |
michael@0 | 91 | |
michael@0 | 92 | RejectPromiseCallback(Promise* aPromise, JS::Handle<JSObject*> aGlobal); |
michael@0 | 93 | ~RejectPromiseCallback(); |
michael@0 | 94 | |
michael@0 | 95 | private: |
michael@0 | 96 | nsRefPtr<Promise> mPromise; |
michael@0 | 97 | JS::Heap<JSObject*> mGlobal; |
michael@0 | 98 | }; |
michael@0 | 99 | |
michael@0 | 100 | // NativePromiseCallback wraps a NativePromiseHandler. |
michael@0 | 101 | class NativePromiseCallback MOZ_FINAL : public PromiseCallback |
michael@0 | 102 | { |
michael@0 | 103 | public: |
michael@0 | 104 | NS_DECL_ISUPPORTS_INHERITED |
michael@0 | 105 | NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(NativePromiseCallback, |
michael@0 | 106 | PromiseCallback) |
michael@0 | 107 | |
michael@0 | 108 | void Call(JS::Handle<JS::Value> aValue) MOZ_OVERRIDE; |
michael@0 | 109 | |
michael@0 | 110 | NativePromiseCallback(PromiseNativeHandler* aHandler, |
michael@0 | 111 | Promise::PromiseState aState); |
michael@0 | 112 | ~NativePromiseCallback(); |
michael@0 | 113 | |
michael@0 | 114 | private: |
michael@0 | 115 | nsRefPtr<PromiseNativeHandler> mHandler; |
michael@0 | 116 | Promise::PromiseState mState; |
michael@0 | 117 | }; |
michael@0 | 118 | |
michael@0 | 119 | } // namespace dom |
michael@0 | 120 | } // namespace mozilla |
michael@0 | 121 | |
michael@0 | 122 | #endif // mozilla_dom_PromiseCallback_h |