/src/serenity/Userland/Libraries/LibWeb/DOM/AbortSignal.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org> |
3 | | * Copyright (c) 2024, Tim Ledbetter <timledbetter@gmail.com> |
4 | | * |
5 | | * SPDX-License-Identifier: BSD-2-Clause |
6 | | */ |
7 | | |
8 | | #pragma once |
9 | | |
10 | | #include <AK/RefCounted.h> |
11 | | #include <AK/Weakable.h> |
12 | | #include <LibJS/Heap/HeapFunction.h> |
13 | | #include <LibWeb/DOM/EventTarget.h> |
14 | | #include <LibWeb/Forward.h> |
15 | | #include <LibWeb/WebIDL/Types.h> |
16 | | |
17 | | namespace Web::DOM { |
18 | | |
19 | | // https://dom.spec.whatwg.org/#abortsignal |
20 | | class AbortSignal final : public EventTarget { |
21 | | WEB_PLATFORM_OBJECT(AbortSignal, EventTarget); |
22 | | JS_DECLARE_ALLOCATOR(AbortSignal); |
23 | | |
24 | | public: |
25 | | static WebIDL::ExceptionOr<JS::NonnullGCPtr<AbortSignal>> construct_impl(JS::Realm&); |
26 | | |
27 | 0 | virtual ~AbortSignal() override = default; |
28 | | |
29 | | void add_abort_algorithm(ESCAPING Function<void()>); |
30 | | |
31 | | // https://dom.spec.whatwg.org/#dom-abortsignal-aborted |
32 | | // An AbortSignal object is aborted when its abort reason is not undefined. |
33 | 0 | bool aborted() const { return !m_abort_reason.is_undefined(); } |
34 | | |
35 | | void signal_abort(JS::Value reason); |
36 | | |
37 | | void set_onabort(WebIDL::CallbackType*); |
38 | | WebIDL::CallbackType* onabort(); |
39 | | |
40 | | // https://dom.spec.whatwg.org/#dom-abortsignal-reason |
41 | 0 | JS::Value reason() const { return m_abort_reason; } |
42 | 0 | void set_reason(JS::Value reason) { m_abort_reason = reason; } |
43 | | |
44 | | JS::ThrowCompletionOr<void> throw_if_aborted() const; |
45 | | |
46 | | static WebIDL::ExceptionOr<JS::NonnullGCPtr<AbortSignal>> abort(JS::VM&, JS::Value reason); |
47 | | static WebIDL::ExceptionOr<JS::NonnullGCPtr<AbortSignal>> timeout(JS::VM&, Web::WebIDL::UnsignedLongLong milliseconds); |
48 | | static WebIDL::ExceptionOr<JS::NonnullGCPtr<AbortSignal>> any(JS::VM&, Vector<JS::Handle<AbortSignal>> const&); |
49 | | |
50 | | static WebIDL::ExceptionOr<JS::NonnullGCPtr<AbortSignal>> create_dependent_abort_signal(JS::Realm&, Vector<JS::Handle<AbortSignal>> const&); |
51 | | |
52 | | private: |
53 | | explicit AbortSignal(JS::Realm&); |
54 | | |
55 | | virtual void initialize(JS::Realm&) override; |
56 | | virtual void visit_edges(JS::Cell::Visitor&) override; |
57 | | |
58 | 0 | bool dependent() const { return m_dependent; } |
59 | 0 | void set_dependent(bool dependent) { m_dependent = dependent; } |
60 | | |
61 | 0 | Vector<JS::GCPtr<AbortSignal>> source_signals() const { return m_source_signals; } |
62 | | |
63 | 0 | void append_source_signal(JS::GCPtr<AbortSignal> source_signal) { m_source_signals.append(source_signal); } |
64 | 0 | void append_dependent_signal(JS::GCPtr<AbortSignal> dependent_signal) { m_dependent_signals.append(dependent_signal); } |
65 | | |
66 | | // https://dom.spec.whatwg.org/#abortsignal-abort-reason |
67 | | // An AbortSignal object has an associated abort reason, which is a JavaScript value. It is undefined unless specified otherwise. |
68 | | JS::Value m_abort_reason { JS::js_undefined() }; |
69 | | |
70 | | // https://dom.spec.whatwg.org/#abortsignal-abort-algorithms |
71 | | // FIXME: This should be a set. |
72 | | Vector<JS::NonnullGCPtr<JS::HeapFunction<void()>>> m_abort_algorithms; |
73 | | |
74 | | // https://dom.spec.whatwg.org/#abortsignal-source-signals |
75 | | // An AbortSignal object has associated source signals (a weak set of AbortSignal objects that the object is dependent on for its aborted state), which is initially empty. |
76 | | Vector<JS::GCPtr<AbortSignal>> m_source_signals; |
77 | | |
78 | | // https://dom.spec.whatwg.org/#abortsignal-dependent-signals |
79 | | // An AbortSignal object has associated dependent signals (a weak set of AbortSignal objects that are dependent on the object for their aborted state), which is initially empty. |
80 | | Vector<JS::GCPtr<AbortSignal>> m_dependent_signals; |
81 | | |
82 | | // https://dom.spec.whatwg.org/#abortsignal-dependent |
83 | | // An AbortSignal object has a dependent (a boolean), which is initially false. |
84 | | bool m_dependent { false }; |
85 | | }; |
86 | | |
87 | | } |