| 
									
										
										
										
											2021-09-08 23:09:18 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/Function.h>
 | 
					
						
							| 
									
										
										
										
											2021-10-03 16:07:21 +02:00
										 |  |  | #include <AK/WeakPtr.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-09 00:38:43 +02:00
										 |  |  | #include <LibCore/Forward.h>
 | 
					
						
							|  |  |  | #include <LibJS/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2022-09-24 12:04:06 +02:00
										 |  |  | #include <LibJS/SafeFunction.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-08 23:09:18 +02:00
										 |  |  | #include <LibWeb/HTML/EventLoop/TaskQueue.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web::HTML { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class EventLoop { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2021-09-09 00:21:57 +02:00
										 |  |  |     enum class Type { | 
					
						
							|  |  |  |         // https://html.spec.whatwg.org/multipage/webappapis.html#window-event-loop
 | 
					
						
							|  |  |  |         Window, | 
					
						
							|  |  |  |         // https://html.spec.whatwg.org/multipage/webappapis.html#worker-event-loop
 | 
					
						
							|  |  |  |         Worker, | 
					
						
							|  |  |  |         // https://html.spec.whatwg.org/multipage/webappapis.html#worklet-event-loop
 | 
					
						
							|  |  |  |         Worklet, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-08 23:09:18 +02:00
										 |  |  |     EventLoop(); | 
					
						
							|  |  |  |     ~EventLoop(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-09 00:21:57 +02:00
										 |  |  |     Type type() const { return m_type; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-08 23:09:18 +02:00
										 |  |  |     TaskQueue& task_queue() { return m_task_queue; } | 
					
						
							|  |  |  |     TaskQueue const& task_queue() const { return m_task_queue; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-26 14:36:20 +02:00
										 |  |  |     TaskQueue& microtask_queue() { return m_microtask_queue; } | 
					
						
							|  |  |  |     TaskQueue const& microtask_queue() const { return m_microtask_queue; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-21 12:13:04 +01:00
										 |  |  |     void spin_until(JS::SafeFunction<bool()> goal_condition); | 
					
						
							| 
									
										
										
										
											2021-09-09 00:18:04 +02:00
										 |  |  |     void process(); | 
					
						
							| 
									
										
										
										
											2021-09-08 23:50:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-21 01:06:47 +02:00
										 |  |  |     // https://html.spec.whatwg.org/multipage/browsing-the-web.html#termination-nesting-level
 | 
					
						
							|  |  |  |     size_t termination_nesting_level() const { return m_termination_nesting_level; } | 
					
						
							|  |  |  |     void increment_termination_nesting_level() { ++m_termination_nesting_level; } | 
					
						
							|  |  |  |     void decrement_termination_nesting_level() { --m_termination_nesting_level; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-08 23:56:50 +02:00
										 |  |  |     Task const* currently_running_task() const { return m_currently_running_task; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-09 00:27:06 +02:00
										 |  |  |     JS::VM& vm() { return *m_vm; } | 
					
						
							|  |  |  |     JS::VM const& vm() const { return *m_vm; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void set_vm(JS::VM&); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-09 00:38:43 +02:00
										 |  |  |     void schedule(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-26 14:36:20 +02:00
										 |  |  |     void perform_a_microtask_checkpoint(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-03 16:07:21 +02:00
										 |  |  |     void register_document(Badge<DOM::Document>, DOM::Document&); | 
					
						
							|  |  |  |     void unregister_document(Badge<DOM::Document>, DOM::Document&); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     Vector<JS::Handle<DOM::Document>> documents_in_this_event_loop() const; | 
					
						
							| 
									
										
										
										
											2021-10-03 16:07:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     Vector<JS::Handle<HTML::Window>> same_loop_windows() const; | 
					
						
							| 
									
										
										
										
											2022-03-31 21:55:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-14 16:12:53 +01:00
										 |  |  |     void push_onto_backup_incumbent_settings_object_stack(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject& environment_settings_object); | 
					
						
							|  |  |  |     void pop_backup_incumbent_settings_object_stack(Badge<EnvironmentSettingsObject>); | 
					
						
							|  |  |  |     EnvironmentSettingsObject& top_of_backup_incumbent_settings_object_stack(); | 
					
						
							|  |  |  |     bool is_backup_incumbent_settings_object_stack_empty() const { return m_backup_incumbent_settings_object_stack.is_empty(); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void register_environment_settings_object(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject&); | 
					
						
							|  |  |  |     void unregister_environment_settings_object(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject&); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-31 21:55:01 +02:00
										 |  |  |     double compute_deadline() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-15 15:27:58 -05:00
										 |  |  |     // https://html.spec.whatwg.org/multipage/webappapis.html#pause
 | 
					
						
							|  |  |  |     void set_execution_paused(bool execution_paused) { m_execution_paused = execution_paused; } | 
					
						
							|  |  |  |     bool execution_paused() const { return m_execution_paused; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-08 23:09:18 +02:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2021-09-09 00:21:57 +02:00
										 |  |  |     Type m_type { Type::Window }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-08 23:09:18 +02:00
										 |  |  |     TaskQueue m_task_queue; | 
					
						
							| 
									
										
										
										
											2021-09-26 14:36:20 +02:00
										 |  |  |     TaskQueue m_microtask_queue; | 
					
						
							| 
									
										
										
										
											2021-09-08 23:56:50 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // https://html.spec.whatwg.org/multipage/webappapis.html#currently-running-task
 | 
					
						
							|  |  |  |     Task* m_currently_running_task { nullptr }; | 
					
						
							| 
									
										
										
										
											2021-09-09 00:27:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-31 21:55:01 +02:00
										 |  |  |     // https://html.spec.whatwg.org/multipage/webappapis.html#last-render-opportunity-time
 | 
					
						
							|  |  |  |     double m_last_render_opportunity_time { 0 }; | 
					
						
							|  |  |  |     // https://html.spec.whatwg.org/multipage/webappapis.html#last-idle-period-start-time
 | 
					
						
							|  |  |  |     double m_last_idle_period_start_time { 0 }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-09 00:27:06 +02:00
										 |  |  |     JS::VM* m_vm { nullptr }; | 
					
						
							| 
									
										
										
										
											2021-09-09 00:38:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-07 20:30:31 +02:00
										 |  |  |     RefPtr<Platform::Timer> m_system_event_loop_timer; | 
					
						
							| 
									
										
										
										
											2021-09-26 14:36:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // https://html.spec.whatwg.org/#performing-a-microtask-checkpoint
 | 
					
						
							|  |  |  |     bool m_performing_a_microtask_checkpoint { false }; | 
					
						
							| 
									
										
										
										
											2021-10-03 16:07:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Vector<WeakPtr<DOM::Document>> m_documents; | 
					
						
							| 
									
										
										
										
											2021-10-14 16:12:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Used to implement step 4 of "perform a microtask checkpoint".
 | 
					
						
							| 
									
										
										
										
											2023-02-26 16:09:02 -07:00
										 |  |  |     Vector<JS::NonnullGCPtr<EnvironmentSettingsObject>> m_related_environment_settings_objects; | 
					
						
							| 
									
										
										
										
											2021-10-14 16:12:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // https://html.spec.whatwg.org/multipage/webappapis.html#backup-incumbent-settings-object-stack
 | 
					
						
							| 
									
										
										
										
											2023-02-26 16:09:02 -07:00
										 |  |  |     Vector<JS::NonnullGCPtr<EnvironmentSettingsObject>> m_backup_incumbent_settings_object_stack; | 
					
						
							| 
									
										
										
										
											2022-09-21 01:06:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // https://html.spec.whatwg.org/multipage/browsing-the-web.html#termination-nesting-level
 | 
					
						
							|  |  |  |     size_t m_termination_nesting_level { 0 }; | 
					
						
							| 
									
										
										
										
											2022-11-15 15:27:58 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool m_execution_paused { false }; | 
					
						
							| 
									
										
										
										
											2021-09-08 23:09:18 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | EventLoop& main_thread_event_loop(); | 
					
						
							| 
									
										
										
										
											2022-09-24 12:04:06 +02:00
										 |  |  | void old_queue_global_task_with_document(HTML::Task::Source, DOM::Document&, JS::SafeFunction<void()> steps); | 
					
						
							|  |  |  | void queue_global_task(HTML::Task::Source, JS::Object&, JS::SafeFunction<void()> steps); | 
					
						
							| 
									
										
										
										
											2023-02-25 10:44:51 -07:00
										 |  |  | void queue_a_microtask(DOM::Document const*, JS::SafeFunction<void()> steps); | 
					
						
							| 
									
										
										
										
											2021-09-26 14:36:20 +02:00
										 |  |  | void perform_a_microtask_checkpoint(); | 
					
						
							| 
									
										
										
										
											2021-09-08 23:09:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | } |