2021-02-03 10:41:07 +01:00
/*
2024-10-04 13:19:50 +02:00
* Copyright ( c ) 2021 - 2022 , Andreas Kling < andreas @ ladybird . org >
2023-03-29 23:46:18 +01:00
* Copyright ( c ) 2021 - 2023 , Luke Wilde < lukew @ serenityos . org >
2021-02-03 10:41:07 +01:00
*
2021-04-22 01:24:48 -07:00
* SPDX - License - Identifier : BSD - 2 - Clause
2021-02-03 10:41:07 +01:00
*/
# pragma once
# include <LibJS/Forward.h>
2022-02-06 03:32:26 +00:00
# include <LibJS/Runtime/JobCallback.h>
2021-09-19 22:10:49 +02:00
# include <LibJS/Runtime/VM.h>
2023-03-29 23:46:18 +01:00
# include <LibWeb/DOM/Element.h>
2022-07-11 16:37:51 +01:00
# include <LibWeb/DOM/MutationObserver.h>
2021-09-08 23:09:18 +02:00
# include <LibWeb/HTML/EventLoop/EventLoop.h>
2021-02-03 10:41:07 +01:00
namespace Web : : Bindings {
2023-03-29 23:46:18 +01:00
// https://html.spec.whatwg.org/multipage/custom-elements.html#custom-element-reactions-stack
struct CustomElementReactionsStack {
CustomElementReactionsStack ( ) = default ;
~ CustomElementReactionsStack ( ) = default ;
// https://html.spec.whatwg.org/multipage/custom-elements.html#element-queue
// Each item in the stack is an element queue, which is initially empty as well. Each item in an element queue is an element.
// (The elements are not necessarily custom yet, since this queue is used for upgrades as well.)
Vector < Vector < JS : : Handle < DOM : : Element > > > element_queue_stack ;
// https://html.spec.whatwg.org/multipage/custom-elements.html#backup-element-queue
// Each custom element reactions stack has an associated backup element queue, which an initially-empty element queue.
Vector < JS : : Handle < DOM : : Element > > backup_element_queue ;
// https://html.spec.whatwg.org/multipage/custom-elements.html#processing-the-backup-element-queue
// To prevent reentrancy when processing the backup element queue, each custom element reactions stack also has a processing the backup element queue flag, initially unset.
bool processing_the_backup_element_queue { false } ;
} ;
2021-09-08 23:09:18 +02:00
struct WebEngineCustomData final : public JS : : VM : : CustomData {
2022-03-14 13:21:51 -06:00
virtual ~ WebEngineCustomData ( ) override = default ;
2021-09-08 23:09:18 +02:00
2024-10-31 01:06:56 +13:00
virtual void spin_event_loop_until ( JS : : Handle < JS : : HeapFunction < bool ( ) > > goal_condition ) override ;
2022-09-08 00:13:39 +02:00
2024-04-04 12:06:50 +02:00
JS : : Handle < HTML : : EventLoop > event_loop ;
2022-07-11 16:37:51 +01:00
// FIXME: These should only be on similar-origin window agents, but we don't currently differentiate agent types.
// https://dom.spec.whatwg.org/#mutation-observer-compound-microtask-queued-flag
bool mutation_observer_microtask_queued { false } ;
// https://dom.spec.whatwg.org/#mutation-observer-list
// FIXME: This should be a set.
2023-09-27 14:58:14 +02:00
Vector < JS : : NonnullGCPtr < DOM : : MutationObserver > > mutation_observers ;
2022-08-04 20:16:40 +02:00
2022-10-17 10:55:16 +02:00
JS : : Handle < JS : : Realm > internal_realm ;
2022-08-04 20:16:40 +02:00
OwnPtr < JS : : ExecutionContext > root_execution_context ;
2023-03-29 23:46:18 +01:00
// https://html.spec.whatwg.org/multipage/custom-elements.html#custom-element-reactions-stack
// Each similar-origin window agent has a custom element reactions stack, which is initially empty.
CustomElementReactionsStack custom_element_reactions_stack { } ;
// https://html.spec.whatwg.org/multipage/custom-elements.html#current-element-queue
// A similar-origin window agent's current element queue is the element queue at the top of its custom element reactions stack.
Vector < JS : : Handle < DOM : : Element > > & current_element_queue ( ) { return custom_element_reactions_stack . element_queue_stack . last ( ) ; }
Vector < JS : : Handle < DOM : : Element > > const & current_element_queue ( ) const { return custom_element_reactions_stack . element_queue_stack . last ( ) ; }
2021-09-08 23:09:18 +02:00
} ;
2022-02-06 03:32:26 +00:00
struct WebEngineCustomJobCallbackData final : public JS : : JobCallback : : CustomData {
WebEngineCustomJobCallbackData ( HTML : : EnvironmentSettingsObject & incumbent_settings , OwnPtr < JS : : ExecutionContext > active_script_context )
: incumbent_settings ( incumbent_settings )
, active_script_context ( move ( active_script_context ) )
{
}
2022-03-14 13:21:51 -06:00
virtual ~ WebEngineCustomJobCallbackData ( ) override = default ;
2022-02-06 03:32:26 +00:00
2023-02-26 16:09:02 -07:00
JS : : NonnullGCPtr < HTML : : EnvironmentSettingsObject > incumbent_settings ;
2022-02-06 03:32:26 +00:00
OwnPtr < JS : : ExecutionContext > active_script_context ;
} ;
2022-10-24 19:39:35 +01:00
HTML : : Script * active_script ( ) ;
2023-03-17 10:56:59 -04:00
2024-07-09 02:59:25 -06:00
ErrorOr < void > initialize_main_thread_vm ( HTML : : EventLoop : : Type ) ;
2021-02-03 10:41:07 +01:00
JS : : VM & main_thread_vm ( ) ;
2023-03-17 10:56:59 -04:00
2023-02-25 10:44:51 -07:00
void queue_mutation_observer_microtask ( DOM : : Document const & ) ;
2024-05-17 17:09:20 -07:00
NonnullOwnPtr < JS : : ExecutionContext > create_a_new_javascript_realm ( JS : : VM & , Function < JS : : Object * ( JS : : Realm & ) > create_global_object , Function < JS : : Object * ( JS : : Realm & ) > create_global_this_value ) ;
2023-03-29 23:46:18 +01:00
void invoke_custom_element_reactions ( Vector < JS : : Handle < DOM : : Element > > & element_queue ) ;
2021-02-03 10:41:07 +01:00
}