| 
									
										
										
										
											2021-09-02 02:12:49 +01:00
										 |  |  |  | /*
 | 
					
						
							|  |  |  |  |  * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org> | 
					
						
							|  |  |  |  |  * | 
					
						
							|  |  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  |  */ | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-10 20:05:12 +00:00
										 |  |  |  | #include <LibWeb/Bindings/DOMExceptionWrapper.h>
 | 
					
						
							|  |  |  |  | #include <LibWeb/Bindings/Wrapper.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 { | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |  | JS::NonnullGCPtr<AbortSignal> AbortSignal::create_with_global_object(HTML::Window& window) | 
					
						
							| 
									
										
										
										
											2021-09-02 02:12:49 +01:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |  |     return *window.heap().allocate<AbortSignal>(window.realm(), window); | 
					
						
							| 
									
										
										
										
											2021-09-02 02:12:49 +01:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |  | AbortSignal::AbortSignal(HTML::Window& window) | 
					
						
							|  |  |  |  |     : EventTarget(window.realm()) | 
					
						
							| 
									
										
										
										
											2021-09-02 02:12:49 +01:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // https://dom.spec.whatwg.org/#abortsignal-add
 | 
					
						
							|  |  |  |  | void AbortSignal::add_abort_algorithm(Function<void()> abort_algorithm) | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											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.
 | 
					
						
							| 
									
										
										
										
											2021-09-02 02:12:49 +01:00
										 |  |  |  |     m_abort_algorithms.append(move(abort_algorithm)); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // 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 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |  |         m_abort_reason = wrap(realm(), AbortError::create("Aborted without reason")); | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  |  |         algorithm(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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.
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |  |     dispatch_event(*Event::create(global_object(), HTML::EventNames::abort)); | 
					
						
							| 
									
										
										
										
											2021-09-02 02:12:49 +01:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 14:12:01 +02:00
										 |  |  |  | void AbortSignal::set_onabort(Bindings::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
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-14 18:03:08 +01:00
										 |  |  |  | Bindings::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); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-02 02:12:49 +01:00
										 |  |  |  | } |