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/. */
7 #ifndef mozilla_dom_AbortSignal_h
8 #define mozilla_dom_AbortSignal_h
10 #include "mozilla/RefPtr.h"
11 #include "mozilla/dom/AbortFollower.h"
12 #include "mozilla/DOMEventTargetHelper.h"
14 namespace mozilla::dom
{
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>.
22 // This requires that AbortSignal also inherit from AbortFollower.
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
,
29 public AbortSignalImpl
,
30 public AbortFollower
{
32 NS_DECL_ISUPPORTS_INHERITED
33 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(AbortSignal
,
36 AbortSignal(nsIGlobalObject
* aGlobalObject
, bool aAborted
,
37 JS::Handle
<JS::Value
> aReason
);
39 JSObject
* WrapObject(JSContext
* aCx
,
40 JS::Handle
<JSObject
*> aGivenProto
) override
;
42 IMPL_EVENT_HANDLER(abort
);
44 static already_AddRefed
<AbortSignal
> Abort(GlobalObject
& aGlobal
,
45 JS::Handle
<JS::Value
> aReason
);
47 static already_AddRefed
<AbortSignal
> Timeout(GlobalObject
& aGlobal
,
48 uint64_t aMilliseconds
,
51 static already_AddRefed
<AbortSignal
> Any(
52 GlobalObject
& aGlobal
,
53 const Sequence
<OwningNonNull
<AbortSignal
>>& aSignals
);
55 void ThrowIfAborted(JSContext
* aCx
, ErrorResult
& aRv
);
58 void SignalAbort(JS::Handle
<JS::Value
> aReason
) override
;
61 void RunAbortAlgorithm() override
;
63 virtual bool IsTaskSignal() const { return false; }
65 bool Dependent() const;
70 void MakeDependentOn(AbortSignal
* aSignal
);
72 nsTArray
<WeakPtr
<AbortSignal
>> mSourceSignals
;
73 nsTArray
<RefPtr
<AbortSignal
>> mDependentSignals
;
78 } // namespace mozilla::dom
80 #endif // mozilla_dom_AbortSignal_h