| 
									
										
										
										
											2021-09-08 23:09:18 +02:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2024-10-04 13:19:50 +02:00
										 |  |  |  * Copyright (c) 2021-2024, Andreas Kling <andreas@ladybird.org> | 
					
						
							| 
									
										
										
										
											2021-09-08 23:09:18 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-04 09:02:00 -04:00
										 |  |  | #include <AK/IDAllocator.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-08 23:09:18 +02:00
										 |  |  | #include <LibWeb/DOM/Document.h>
 | 
					
						
							|  |  |  | #include <LibWeb/HTML/EventLoop/Task.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web::HTML { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | GC_DEFINE_ALLOCATOR(Task); | 
					
						
							| 
									
										
										
										
											2024-04-04 12:06:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-04 09:02:00 -04:00
										 |  |  | static IDAllocator s_unique_task_source_allocator { static_cast<int>(Task::Source::UniqueTaskSourceStart) }; | 
					
						
							| 
									
										
										
										
											2024-08-04 17:10:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | [[nodiscard]] static TaskID allocate_task_id() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     static u64 next_task_id = 1; | 
					
						
							|  |  |  |     return next_task_id++; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-04-04 09:02:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | GC::Ref<Task> Task::create(JS::VM& vm, Source source, GC::Ptr<DOM::Document const> document, GC::Ref<GC::Function<void()>> steps) | 
					
						
							| 
									
										
										
										
											2024-04-04 12:06:50 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-11-14 06:13:46 +13:00
										 |  |  |     return vm.heap().allocate<Task>(source, document, move(steps)); | 
					
						
							| 
									
										
										
										
											2024-04-04 12:06:50 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | Task::Task(Source source, GC::Ptr<DOM::Document const> document, GC::Ref<GC::Function<void()>> steps) | 
					
						
							| 
									
										
										
										
											2024-08-04 17:10:49 +02:00
										 |  |  |     : m_id(allocate_task_id()) | 
					
						
							| 
									
										
										
										
											2023-08-31 13:25:27 -04:00
										 |  |  |     , m_source(source) | 
					
						
							| 
									
										
										
										
											2024-04-04 12:06:50 +02:00
										 |  |  |     , m_steps(steps) | 
					
						
							|  |  |  |     , m_document(document) | 
					
						
							| 
									
										
										
										
											2021-09-08 23:09:18 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-04 12:06:50 +02:00
										 |  |  | Task::~Task() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Task::visit_edges(Visitor& visitor) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Base::visit_edges(visitor); | 
					
						
							|  |  |  |     visitor.visit(m_steps); | 
					
						
							|  |  |  |     visitor.visit(m_document); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-08 23:09:18 +02:00
										 |  |  | void Task::execute() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-04-04 12:06:50 +02:00
										 |  |  |     m_steps->function()(); | 
					
						
							| 
									
										
										
										
											2021-09-08 23:09:18 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-03 15:38:11 +02:00
										 |  |  | // https://html.spec.whatwg.org/#concept-task-runnable
 | 
					
						
							|  |  |  | bool Task::is_runnable() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // A task is runnable if its document is either null or fully active.
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     return !m_document.ptr() || m_document->is_fully_active(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | DOM::Document const* Task::document() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return m_document.ptr(); | 
					
						
							| 
									
										
										
										
											2021-10-03 15:38:11 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-04 09:02:00 -04:00
										 |  |  | UniqueTaskSource::UniqueTaskSource() | 
					
						
							|  |  |  |     : source(static_cast<Task::Source>(s_unique_task_source_allocator.allocate())) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | UniqueTaskSource::~UniqueTaskSource() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     s_unique_task_source_allocator.deallocate(static_cast<int>(source)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-08 23:09:18 +02:00
										 |  |  | } |