| 
									
										
										
										
											2021-09-08 23:09:18 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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 { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-04 09:02:00 -04:00
										 |  |  | static IDAllocator s_unique_task_source_allocator { static_cast<int>(Task::Source::UniqueTaskSourceStart) }; | 
					
						
							| 
									
										
										
										
											2023-08-31 13:25:27 -04:00
										 |  |  | static IDAllocator s_task_id_allocator; | 
					
						
							| 
									
										
										
										
											2023-04-04 09:02:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-25 10:44:51 -07:00
										 |  |  | Task::Task(Source source, DOM::Document const* document, JS::SafeFunction<void()> steps) | 
					
						
							| 
									
										
										
										
											2023-08-31 13:25:27 -04:00
										 |  |  |     : m_id(s_task_id_allocator.allocate()) | 
					
						
							|  |  |  |     , m_source(source) | 
					
						
							| 
									
										
										
										
											2021-09-08 23:53:55 +02:00
										 |  |  |     , m_steps(move(steps)) | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     , m_document(JS::make_handle(document)) | 
					
						
							| 
									
										
										
										
											2021-09-08 23:09:18 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-31 13:25:27 -04:00
										 |  |  | Task::~Task() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     s_unique_task_source_allocator.deallocate(m_id); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-09-08 23:09:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | void Task::execute() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_steps(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } |