2021-09-02 02:12:49 +01:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2022-09-25 16:15:49 -06:00
|
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2021-09-02 02:12:49 +01:00
|
|
|
|
#include <LibWeb/DOM/AbortSignal.h>
|
|
|
|
|
#include <LibWeb/DOM/Document.h>
|
|
|
|
|
#include <LibWeb/DOM/EventDispatcher.h>
|
2021-10-01 01:09:11 +01:00
|
|
|
|
#include <LibWeb/HTML/EventHandler.h>
|
2021-09-02 02:12:49 +01:00
|
|
|
|
|
|
|
|
|
namespace Web::DOM {
|
|
|
|
|
|
2023-11-19 19:47:52 +01:00
|
|
|
|
JS_DEFINE_ALLOCATOR(AbortSignal);
|
|
|
|
|
|
2023-02-14 21:02:46 +01:00
|
|
|
|
WebIDL::ExceptionOr<JS::NonnullGCPtr<AbortSignal>> AbortSignal::construct_impl(JS::Realm& realm)
|
2021-09-02 02:12:49 +01:00
|
|
|
|
{
|
2023-08-13 13:05:26 +02:00
|
|
|
|
return realm.heap().allocate<AbortSignal>(realm, realm);
|
2022-09-25 16:15:49 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AbortSignal::AbortSignal(JS::Realm& realm)
|
|
|
|
|
: EventTarget(realm)
|
|
|
|
|
{
|
2023-01-10 06:28:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
|
void AbortSignal::initialize(JS::Realm& realm)
|
2023-01-10 06:28:20 -05:00
|
|
|
|
{
|
2023-08-07 08:41:28 +02:00
|
|
|
|
Base::initialize(realm);
|
2023-11-22 12:55:21 +13:00
|
|
|
|
set_prototype(&Bindings::ensure_web_prototype<Bindings::AbortSignalPrototype>(realm, "AbortSignal"_fly_string));
|
2021-09-02 02:12:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://dom.spec.whatwg.org/#abortsignal-add
|
2023-09-25 18:45:20 +02:00
|
|
|
|
void AbortSignal::add_abort_algorithm(Function<void()> abort_algorithm)
|
2021-09-02 02:12:49 +01:00
|
|
|
|
{
|
2021-12-10 20:05:12 +00:00
|
|
|
|
// 1. If signal is aborted, then return.
|
|
|
|
|
if (aborted())
|
2021-09-02 02:12:49 +01:00
|
|
|
|
return;
|
|
|
|
|
|
2021-12-10 20:05:12 +00:00
|
|
|
|
// 2. Append algorithm to signal’s abort algorithms.
|
2023-09-25 18:45:20 +02:00
|
|
|
|
m_abort_algorithms.append(JS::create_heap_function(vm().heap(), move(abort_algorithm)));
|
2021-09-02 02:12:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://dom.spec.whatwg.org/#abortsignal-signal-abort
|
2021-12-10 20:05:12 +00:00
|
|
|
|
void AbortSignal::signal_abort(JS::Value reason)
|
2021-09-02 02:12:49 +01:00
|
|
|
|
{
|
2021-12-10 20:05:12 +00:00
|
|
|
|
// 1. If signal is aborted, then return.
|
|
|
|
|
if (aborted())
|
2021-09-02 02:12:49 +01:00
|
|
|
|
return;
|
|
|
|
|
|
2021-12-10 20:05:12 +00:00
|
|
|
|
// 2. Set signal’s abort reason to reason if it is given; otherwise to a new "AbortError" DOMException.
|
|
|
|
|
if (!reason.is_undefined())
|
|
|
|
|
m_abort_reason = reason;
|
|
|
|
|
else
|
2023-09-06 16:03:01 +12:00
|
|
|
|
m_abort_reason = WebIDL::AbortError::create(realm(), "Aborted without reason"_fly_string).ptr();
|
2021-09-02 02:12:49 +01:00
|
|
|
|
|
2021-12-10 20:05:12 +00:00
|
|
|
|
// 3. For each algorithm in signal’s abort algorithms: run algorithm.
|
2021-09-02 02:12:49 +01:00
|
|
|
|
for (auto& algorithm : m_abort_algorithms)
|
2023-09-25 18:45:20 +02:00
|
|
|
|
algorithm->function()();
|
2021-09-02 02:12:49 +01:00
|
|
|
|
|
2021-12-10 20:05:12 +00:00
|
|
|
|
// 4. Empty signal’s abort algorithms.
|
2021-09-02 02:12:49 +01:00
|
|
|
|
m_abort_algorithms.clear();
|
|
|
|
|
|
2021-12-10 20:05:12 +00:00
|
|
|
|
// 5. Fire an event named abort at signal.
|
2023-08-13 13:05:26 +02:00
|
|
|
|
dispatch_event(Event::create(realm(), HTML::EventNames::abort));
|
2021-09-02 02:12:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-24 16:02:41 +01:00
|
|
|
|
void AbortSignal::set_onabort(WebIDL::CallbackType* event_handler)
|
2021-10-01 01:09:11 +01:00
|
|
|
|
{
|
2022-08-08 14:12:01 +02:00
|
|
|
|
set_event_handler_attribute(HTML::EventNames::abort, event_handler);
|
2021-10-01 01:09:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-24 16:02:41 +01:00
|
|
|
|
WebIDL::CallbackType* AbortSignal::onabort()
|
2021-10-01 01:09:11 +01:00
|
|
|
|
{
|
|
|
|
|
return event_handler_attribute(HTML::EventNames::abort);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-10 19:48:51 +00:00
|
|
|
|
// https://dom.spec.whatwg.org/#dom-abortsignal-throwifaborted
|
|
|
|
|
JS::ThrowCompletionOr<void> AbortSignal::throw_if_aborted() const
|
|
|
|
|
{
|
|
|
|
|
// The throwIfAborted() method steps are to throw this’s abort reason, if this is aborted.
|
|
|
|
|
if (!aborted())
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
return JS::throw_completion(m_abort_reason);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-10 20:05:12 +00:00
|
|
|
|
void AbortSignal::visit_edges(JS::Cell::Visitor& visitor)
|
|
|
|
|
{
|
2022-08-28 13:42:07 +02:00
|
|
|
|
Base::visit_edges(visitor);
|
2021-12-10 20:05:12 +00:00
|
|
|
|
visitor.visit(m_abort_reason);
|
2023-09-25 18:45:20 +02:00
|
|
|
|
for (auto& algorithm : m_abort_algorithms)
|
|
|
|
|
visitor.visit(algorithm);
|
2021-12-10 20:05:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-26 18:15:46 +01:00
|
|
|
|
// https://dom.spec.whatwg.org/#abortsignal-follow
|
|
|
|
|
void AbortSignal::follow(JS::NonnullGCPtr<AbortSignal> parent_signal)
|
|
|
|
|
{
|
|
|
|
|
// A followingSignal (an AbortSignal) is made to follow a parentSignal (an AbortSignal) by running these steps:
|
|
|
|
|
|
|
|
|
|
// 1. If followingSignal is aborted, then return.
|
|
|
|
|
if (aborted())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// 2. If parentSignal is aborted, then signal abort on followingSignal with parentSignal’s abort reason.
|
|
|
|
|
if (parent_signal->aborted()) {
|
|
|
|
|
signal_abort(parent_signal->reason());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3. Otherwise, add the following abort steps to parentSignal:
|
|
|
|
|
// NOTE: `this` and `parent_signal` are protected by AbortSignal using JS::SafeFunction.
|
2022-11-19 01:09:53 +00:00
|
|
|
|
parent_signal->add_abort_algorithm([this, parent_signal] {
|
2022-10-26 18:15:46 +01:00
|
|
|
|
// 1. Signal abort on followingSignal with parentSignal’s abort reason.
|
|
|
|
|
signal_abort(parent_signal->reason());
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-20 20:51:29 +00:00
|
|
|
|
// https://dom.spec.whatwg.org/#dom-abortsignal-abort
|
|
|
|
|
WebIDL::ExceptionOr<JS::NonnullGCPtr<AbortSignal>> AbortSignal::abort(JS::VM& vm, JS::Value reason)
|
|
|
|
|
{
|
|
|
|
|
// 1. Let signal be a new AbortSignal object.
|
|
|
|
|
auto signal = TRY(construct_impl(*vm.current_realm()));
|
|
|
|
|
|
|
|
|
|
// 2. Set signal’s abort reason to reason if it is given; otherwise to a new "AbortError" DOMException.
|
|
|
|
|
if (reason.is_undefined())
|
|
|
|
|
reason = WebIDL::AbortError::create(*vm.current_realm(), "Aborted without reason"_fly_string).ptr();
|
|
|
|
|
|
|
|
|
|
signal->set_reason(reason);
|
|
|
|
|
|
|
|
|
|
// 3. Return signal.
|
|
|
|
|
return signal;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-02 02:12:49 +01:00
|
|
|
|
}
|