diff options
author | Luke Wilde <lukew@serenityos.org> | 2021-10-14 16:12:53 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-02-08 17:47:44 +0000 |
commit | f71f404e0c56497bdf1b65a4a69b45de51e8e42c (patch) | |
tree | b02b0d04c3b34c3a167f1ed95ab6d22431d35519 /Userland/Libraries/LibWeb/HTML/EventLoop | |
parent | 4db5406d6234bad17d34fe8a0b461dea766271e6 (diff) | |
download | serenity-f71f404e0c56497bdf1b65a4a69b45de51e8e42c.zip |
LibWeb: Introduce the Environment Settings Object
The environment settings object is effectively the context a piece of
script is running under, for example, it contains the origin,
responsible document, realm, global object and event loop for the
current context. This effectively replaces ScriptExecutionContext, but
it cannot be removed in this commit as EventTarget still depends on it.
https://html.spec.whatwg.org/multipage/webappapis.html#environment-settings-object
Diffstat (limited to 'Userland/Libraries/LibWeb/HTML/EventLoop')
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp | 59 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.h | 19 |
2 files changed, 70 insertions, 8 deletions
diff --git a/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp index 4bdd50c33b..b4d8e57c78 100644 --- a/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp +++ b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp @@ -12,6 +12,7 @@ #include <LibWeb/DOM/Window.h> #include <LibWeb/HTML/BrowsingContext.h> #include <LibWeb/HTML/EventLoop/EventLoop.h> +#include <LibWeb/HTML/Scripting/Environments.h> #include <LibWeb/HighResolutionTime/Performance.h> namespace Web::HTML { @@ -207,15 +208,33 @@ void EventLoop::process() schedule(); } -// https://html.spec.whatwg.org/multipage/webappapis.html#queue-a-global-task -void queue_global_task(HTML::Task::Source source, DOM::Document& document, Function<void()> steps) +// FIXME: This is here to paper over an issue in the HTML parser where it'll create new interpreters (and thus ESOs) on temporary documents created for innerHTML if it uses Document::realm() to get the global object. +// Use queue_global_task instead. +void old_queue_global_task_with_document(HTML::Task::Source source, DOM::Document& document, Function<void()> steps) { - // FIXME: This should take a global object as input and find the relevant document for it. main_thread_event_loop().task_queue().add(HTML::Task::create(source, &document, move(steps))); } +// https://html.spec.whatwg.org/multipage/webappapis.html#queue-a-global-task +void queue_global_task(HTML::Task::Source source, JS::GlobalObject& global_object, Function<void()> steps) +{ + // 1. Let event loop be global's relevant agent's event loop. + auto& global_custom_data = verify_cast<Bindings::WebEngineCustomData>(*global_object.vm().custom_data()); + auto& event_loop = global_custom_data.event_loop; + + // 2. Let document be global's associated Document, if global is a Window object; otherwise null. + DOM::Document* document { nullptr }; + if (is<Bindings::WindowObject>(global_object)) { + auto& window_object = verify_cast<Bindings::WindowObject>(global_object); + document = &window_object.impl().associated_document(); + } + + // 3. Queue a task given source, event loop, document, and steps. + event_loop.task_queue().add(HTML::Task::create(source, document, move(steps))); +} + // https://html.spec.whatwg.org/#queue-a-microtask -void queue_a_microtask(DOM::Document& document, Function<void()> steps) +void queue_a_microtask(DOM::Document* document, Function<void()> steps) { // 1. If event loop was not given, set event loop to the implied event loop. auto& event_loop = HTML::main_thread_event_loop(); @@ -226,7 +245,7 @@ void queue_a_microtask(DOM::Document& document, Function<void()> steps) // 4. Set microtask's steps to steps. // 5. Set microtask's source to the microtask task source. // 6. Set microtask's document to document. - auto microtask = HTML::Task::create(HTML::Task::Source::Microtask, &document, move(steps)); + auto microtask = HTML::Task::create(HTML::Task::Source::Microtask, document, move(steps)); // FIXME: 7. Set microtask's script evaluation environment settings object set to an empty set. @@ -259,7 +278,9 @@ void EventLoop::perform_a_microtask_checkpoint() m_currently_running_task = nullptr; } - // FIXME: 4. For each environment settings object whose responsible event loop is this event loop, notify about rejected promises on that environment settings object. + // 4. For each environment settings object whose responsible event loop is this event loop, notify about rejected promises on that environment settings object. + for (auto& environment_settings_object : m_related_environment_settings_objects) + environment_settings_object.notify_about_rejected_promises({}); // FIXME: 5. Cleanup Indexed Database transactions. @@ -290,4 +311,30 @@ void EventLoop::unregister_document(Badge<DOM::Document>, DOM::Document& documen VERIFY(did_remove); } +void EventLoop::push_onto_backup_incumbent_settings_object_stack(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject& environment_settings_object) +{ + m_backup_incumbent_settings_object_stack.append(environment_settings_object); +} + +void EventLoop::pop_backup_incumbent_settings_object_stack(Badge<EnvironmentSettingsObject>) +{ + m_backup_incumbent_settings_object_stack.take_last(); +} + +EnvironmentSettingsObject& EventLoop::top_of_backup_incumbent_settings_object_stack() +{ + return m_backup_incumbent_settings_object_stack.last(); +} + +void EventLoop::register_environment_settings_object(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject& environment_settings_object) +{ + m_related_environment_settings_objects.append(environment_settings_object); +} + +void EventLoop::unregister_environment_settings_object(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject& environment_settings_object) +{ + bool did_remove = m_related_environment_settings_objects.remove_first_matching([&](auto& entry) { return &entry == &environment_settings_object; }); + VERIFY(did_remove); +} + } diff --git a/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.h b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.h index b3ebdd5e07..658ff67dae 100644 --- a/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.h +++ b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.h @@ -55,6 +55,14 @@ public: NonnullRefPtrVector<DOM::Document> documents_in_this_event_loop() const; + 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&); + private: Type m_type { Type::Window }; @@ -72,11 +80,18 @@ private: bool m_performing_a_microtask_checkpoint { false }; Vector<WeakPtr<DOM::Document>> m_documents; + + // Used to implement step 4 of "perform a microtask checkpoint". + Vector<EnvironmentSettingsObject&> m_related_environment_settings_objects; + + // https://html.spec.whatwg.org/multipage/webappapis.html#backup-incumbent-settings-object-stack + Vector<EnvironmentSettingsObject&> m_backup_incumbent_settings_object_stack; }; EventLoop& main_thread_event_loop(); -void queue_global_task(HTML::Task::Source, DOM::Document&, Function<void()> steps); -void queue_a_microtask(DOM::Document&, Function<void()> steps); +void old_queue_global_task_with_document(HTML::Task::Source, DOM::Document&, Function<void()> steps); +void queue_global_task(HTML::Task::Source, JS::GlobalObject&, Function<void()> steps); +void queue_a_microtask(DOM::Document*, Function<void()> steps); void perform_a_microtask_checkpoint(); } |