AbortSignal.h (3258B)
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 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_AbortSignal_h 8 #define mozilla_dom_AbortSignal_h 9 10 #include "mozilla/DOMEventTargetHelper.h" 11 #include "mozilla/RefPtr.h" 12 #include "mozilla/dom/AbortFollower.h" 13 14 namespace mozilla::dom { 15 16 // AbortSignal the spec concept includes the concept of a child signal 17 // "following" a parent signal -- internally, adding abort steps to the parent 18 // signal that will then signal abort on the child signal -- to propagate 19 // signaling abort from one signal to another. See 20 // <https://dom.spec.whatwg.org/#abortsignal-follow>. 21 // 22 // This requires that AbortSignal also inherit from AbortFollower. 23 // 24 // This ability to follow isn't directly exposed in the DOM; as of this writing 25 // it appears only to be used internally in the Fetch API. It might be a good 26 // idea to split AbortSignal into an implementation that can follow, and an 27 // implementation that can't, to provide this complexity only when it's needed. 28 class AbortSignal : public DOMEventTargetHelper, public AbortSignalImpl { 29 public: 30 NS_DECL_ISUPPORTS_INHERITED 31 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(AbortSignal, 32 DOMEventTargetHelper) 33 34 static already_AddRefed<AbortSignal> Create(nsIGlobalObject* aGlobalObject, 35 SignalAborted aAborted, 36 JS::Handle<JS::Value> aReason); 37 38 virtual JSObject* WrapObject(JSContext* aCx, 39 JS::Handle<JSObject*> aGivenProto) override; 40 41 IMPL_EVENT_HANDLER(abort); 42 43 static already_AddRefed<AbortSignal> Abort(GlobalObject& aGlobal, 44 JS::Handle<JS::Value> aReason); 45 46 static already_AddRefed<AbortSignal> Timeout(GlobalObject& aGlobal, 47 uint64_t aMilliseconds, 48 ErrorResult& aRv); 49 50 static already_AddRefed<AbortSignal> Any( 51 GlobalObject& aGlobal, 52 const Sequence<OwningNonNull<AbortSignal>>& aSignals); 53 static already_AddRefed<AbortSignal> Any( 54 nsIGlobalObject* aGlobal, 55 const Span<const OwningNonNull<AbortSignal>>& aSignals, 56 FunctionRef<already_AddRefed<AbortSignal>(nsIGlobalObject* aGlobal)> 57 aCreateResultSignal); 58 59 void ThrowIfAborted(JSContext* aCx, ErrorResult& aRv); 60 61 virtual bool IsTaskSignal() const { return false; } 62 63 bool Dependent() const; 64 65 protected: 66 AbortSignal(nsIGlobalObject* aGlobalObject, SignalAborted aAborted, 67 JS::Handle<JS::Value> aReason); 68 69 void Init(); 70 71 virtual ~AbortSignal(); 72 73 void MakeDependentOn(AbortSignal* aSignal); 74 75 void SignalAbortWithDependents() override; 76 77 void RunAbortSteps() override; 78 79 nsTArray<WeakPtr<AbortSignal>> mSourceSignals; 80 nsTArray<RefPtr<AbortSignal>> mDependentSignals; 81 82 bool mDependent; 83 }; 84 85 } // namespace mozilla::dom 86 87 #endif // mozilla_dom_AbortSignal_h