Truly independent web browser
LibWeb: Destroy a worker's event loop once its closing flag is set
Andreas Kling08023fd9338d
diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cppindex 58a355f0a55..157ee27766c 100644--- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp+++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp@@ -121,6 +121,22 @@ void EventLoop::schedule_for_queued_task()m_system_event_loop_timer->restart();}+void EventLoop::destroy_once_script_has_finished()+{+ m_destruction_requested = true;+ schedule();+}++bool EventLoop::destroy_if_requested()+{+ // NB: A paused event loop has script waiting further up the stack, as a spinning one has.+ if (!m_destruction_requested || m_spin_depth > 0 || execution_paused())+ return false;+ if (auto on_destroyed = move(m_on_destroyed))+ on_destroyed();+ return true;+}+EventLoop& main_thread_event_loop(){return *static_cast<HTML::Agent*>(Bindings::main_thread_vm().agent())->event_loop;@@ -155,7 +171,11 @@ void EventLoop::spin_until(GC::Ref<GC::Function<bool()>> goal_condition)// NOTE: This is achieved by returning from the function.++m_spin_depth;- ScopeGuard leave_spin = [this] { --m_spin_depth; };+ ScopeGuard leave_spin = [this] {+ --m_spin_depth;+ if (m_destruction_requested)+ schedule();+ };Platform::EventLoopPlugin::the().spin_until(GC::create_function(GC::Heap::the(), [this, goal_condition] {if (goal_condition->function()())return true;@@ -177,7 +197,7 @@ void EventLoop::process(){m_turn_requested_without_a_task = false;- if (execution_paused())+ if (destroy_if_requested() || execution_paused())return;// OPTIMIZATION: A trip through the platform event loop costs more than a small task does, so run the tasks that are@@ -196,6 +216,9 @@ void EventLoop::process()break;}+ if (destroy_if_requested())+ return;+// If there are eligible tasks in the queue, schedule a new round of processing. :^)if (has_runnable_tasks || (!m_microtask_queue.is_empty() && !m_performing_a_microtask_checkpoint)) {schedule();diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.h b/Libraries/LibWeb/HTML/EventLoop/EventLoop.hindex 3c92ba14bb5..6f5da53d909 100644--- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.h+++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.h@@ -144,6 +144,12 @@ public:void schedule();void schedule_for_queued_task();+ // A worker event loop is destroyed once its worker's closing flag is set and the script on it has finished, which+ // is what resumes the run a worker steps.+ void set_on_destroyed(Function<void()> on_destroyed) { m_on_destroyed = move(on_destroyed); }+ void destroy_once_script_has_finished();+ bool is_spinning() const { return m_spin_depth > 0; }+void perform_a_microtask_checkpoint();// NB: Style and layout run no script. A temporary execution context they use ends with an empty stack, which would@@ -342,6 +348,11 @@ private:u64 m_rendering_update_serial { 0 };// How deep the event loop is spun inside a task.size_t m_spin_depth { 0 };++ bool destroy_if_requested();++ bool m_destruction_requested { false };+ Function<void()> m_on_destroyed;};WEB_API EventLoop& main_thread_event_loop();diff --git a/Libraries/LibWeb/HTML/WorkerGlobalScope.cpp b/Libraries/LibWeb/HTML/WorkerGlobalScope.cppindex c34c1f4536f..504d6c78fec 100644--- a/Libraries/LibWeb/HTML/WorkerGlobalScope.cpp+++ b/Libraries/LibWeb/HTML/WorkerGlobalScope.cpp@@ -87,6 +87,23 @@ void WorkerGlobalScope::close_a_worker()// 2. Set workerGlobal's closing flag to true. (This prevents any further tasks from being queued.)m_closing = true;relevant_settings_object(*this).responsible_event_loop().task_queue().discard_further_tasks();++ // NB: The event loop is destroyed once it has no more to do after the closing flag is set.+ relevant_settings_object(*this).responsible_event_loop().destroy_once_script_has_finished();+}++// https://html.spec.whatwg.org/multipage/workers.html#terminate-a-worker+void WorkerGlobalScope::terminate_a_worker()+{+ // 1. Set the worker's WorkerGlobalScope object's closing flag to true.+ // 2. If there are any tasks queued in the WorkerGlobalScope object's relevant agent's event loop's task queues,+ // discard them without processing them.+ close_a_worker();++ // FIXME: 3. Abort the script currently running in the worker.+ // FIXME: 4. If the worker's WorkerGlobalScope object is actually a DedicatedWorkerGlobalScope object (i.e. the+ // worker is a dedicated worker), then empty the port message queue of the port that the worker's implicit+ // port is entangled with.}// https://html.spec.whatwg.org/multipage/workers.html#importing-scripts-and-librariesdiff --git a/Libraries/LibWeb/HTML/WorkerGlobalScope.h b/Libraries/LibWeb/HTML/WorkerGlobalScope.hindex e783d20c9c4..a218807ce91 100644--- a/Libraries/LibWeb/HTML/WorkerGlobalScope.h+++ b/Libraries/LibWeb/HTML/WorkerGlobalScope.h@@ -135,6 +135,8 @@ public:bool is_closing() const { return m_closing; }+ void terminate_a_worker();+void initialize_policy_container(GC::Ref<Fetch::Infrastructure::ResponseView const> response, GC::Ref<EnvironmentSettingsObject> environment);[[nodiscard]] bool check_embedder_policy(EmbedderPolicy const& owner_policy) const;[[nodiscard]] ContentSecurityPolicy::Directives::Directive::Result run_csp_initialization() const;