Truly independent web browser
LibWeb: Count the worker agents of a process
Andreas Klingfb7490333bc0
diff --git a/Libraries/LibWeb/Bindings/MainThreadVM.cpp b/Libraries/LibWeb/Bindings/MainThreadVM.cppindex 99995b50237..b99b8c07299 100644--- a/Libraries/LibWeb/Bindings/MainThreadVM.cpp+++ b/Libraries/LibWeb/Bindings/MainThreadVM.cpp@@ -107,6 +107,7 @@ static auto& main_thread_vm_ptr()}static Atomic<bool, AK::MemoryOrder::memory_order_relaxed> s_collect_garbage_on_every_allocation { false };+static Atomic<size_t, AK::MemoryOrder::memory_order_relaxed> s_worker_agent_count { 0 };static HTML::SimilarOriginWindowAgent*& main_thread_similar_origin_window_agent_ptr(){@@ -173,6 +174,8 @@ void initialize_agent(HTML::AgentType type)if (type == HTML::AgentType::SimilarOriginWindow) {VERIFY(!main_thread_vm_ptr());main_thread_vm_ptr() = new_vm;+ } else {+ ++s_worker_agent_count;}vm.set_agent(create_agent(vm.heap(), type));@@ -818,6 +821,13 @@ void shut_down_agent()// NB: The VM stays reachable while it is destroyed, as the finalizers of the cells in its heap use it.vm.unref();++ --s_worker_agent_count;+}++size_t worker_agent_count()+{+ return s_worker_agent_count;}JS::VM& main_thread_vm()diff --git a/Libraries/LibWeb/Bindings/MainThreadVM.h b/Libraries/LibWeb/Bindings/MainThreadVM.hindex 0fc3b3ad374..924c8681b0d 100644--- a/Libraries/LibWeb/Bindings/MainThreadVM.h+++ b/Libraries/LibWeb/Bindings/MainThreadVM.h@@ -57,6 +57,9 @@ WEB_API JS::VM& main_thread_vm();// thread or the process, and the agent's thread has to be the one that calls it. Other agents carry on.WEB_API void shut_down_agent();+// How many agents other than the window agent are alive in this process, on any thread.+WEB_API size_t worker_agent_count();+WEB_API NonnullOwnPtr<JS::ExecutionContext> create_a_new_javascript_realm(JS::VM&, HTML::CrossOriginIsolationMode agent_cluster_cross_origin_isolation_mode, Function<GC::Ref<JS::Object>(JS::Realm&)> create_global_object, Function<GC::Ref<JS::Object>(JS::Realm&)> create_global_this_value);// Creates a bare Window-backed realm for tests and tools that do not need a full Document.diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cppindex e61baf167dd..1bb910e0cc4 100644--- a/Libraries/LibWeb/Internals/Internals.cpp+++ b/Libraries/LibWeb/Internals/Internals.cpp@@ -422,6 +422,11 @@ void Internals::gc()window().principal_realm().vm().heap().collect_garbage();}+u32 Internals::worker_agent_count() const+{+ return Bindings::worker_agent_count();+}+void Internals::gc_async(GC::Ref<WebIDL::Promise> promise){// Queue a LibCore event to run the collection, so that it runs outside the JS execution context with as small adiff --git a/Libraries/LibWeb/Internals/Internals.h b/Libraries/LibWeb/Internals/Internals.hindex 90f7078a62a..e4f758bd853 100644--- a/Libraries/LibWeb/Internals/Internals.h+++ b/Libraries/LibWeb/Internals/Internals.h@@ -73,6 +73,7 @@ public:WebIDL::ExceptionOr<Utf16String> set_time_zone(Utf16String const& time_zone);void gc();+ u32 worker_agent_count() const;void gc_async(GC::Ref<WebIDL::Promise>);WebIDL::UnsignedLongLong discard_decoded_image_frames();WebIDL::ExceptionOr<void> mark_as_garbage(Utf16String const& variable_name);diff --git a/Libraries/LibWeb/Internals/Internals.idl b/Libraries/LibWeb/Internals/Internals.idlindex 8b12e05c87f..90e3dc16017 100644--- a/Libraries/LibWeb/Internals/Internals.idl+++ b/Libraries/LibWeb/Internals/Internals.idl@@ -29,6 +29,10 @@ interface Internals {DOMString setTimeZone(DOMString timeZone);undefined gc();++ // How many worker agents are alive in this process. One that a worker's owner let go of still counts until its+ // thread has shut it down.+ unsigned long workerAgentCount();[CreatesPromise] Promise<undefined> gcAsync();// Discards every decoded image frame that can be decoded again, and returns how many were discarded.unsigned long long discardDecodedImageFrames();