From fa8b64d59a35aa1f1480426a985b9698a4b7bb4b Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Wed, 27 Mar 2024 11:53:07 -0600 Subject: [PATCH] LibWebView+WebContent: Notify UI process about WebContent PID explicitly On Serenity, it's not trivial to extract the peer pid from a socket that is created by SystemServer and then passed to a forked service process. This patch adds an API to let the WebContent process notify the UI directly, which makes the WebContent process show up in the Serenity port's TaskManagerWidget. It seems that we will need to do something of this sort in order to properly gather metrics on macOS as well, due to the way that self mach ports work. --- Ladybird/HelperProcess.cpp | 2 -- .../Userland/Libraries/LibWebView/BUILD.gn | 1 + Userland/Libraries/LibWebView/CMakeLists.txt | 1 + Userland/Libraries/LibWebView/Forward.h | 1 + .../Libraries/LibWebView/ProcessHandle.cpp | 23 +++++++++++++++++ Userland/Libraries/LibWebView/ProcessHandle.h | 25 +++++++++++++++++++ .../Libraries/LibWebView/WebContentClient.cpp | 7 ++++++ .../Libraries/LibWebView/WebContentClient.h | 1 + .../WebContent/ConnectionFromClient.cpp | 1 + .../Services/WebContent/WebContentClient.ipc | 3 +++ 10 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 Userland/Libraries/LibWebView/ProcessHandle.cpp create mode 100644 Userland/Libraries/LibWebView/ProcessHandle.h diff --git a/Ladybird/HelperProcess.cpp b/Ladybird/HelperProcess.cpp index 7933fbebbb..2a37e0c3c0 100644 --- a/Ladybird/HelperProcess.cpp +++ b/Ladybird/HelperProcess.cpp @@ -96,8 +96,6 @@ ErrorOr> launch_web_content_process( dbgln(); } - WebView::ProcessManager::the().add_process(WebView::ProcessType::WebContent, child_pid); - return new_client; } diff --git a/Meta/gn/secondary/Userland/Libraries/LibWebView/BUILD.gn b/Meta/gn/secondary/Userland/Libraries/LibWebView/BUILD.gn index f680473f7c..c9bd3f3a39 100644 --- a/Meta/gn/secondary/Userland/Libraries/LibWebView/BUILD.gn +++ b/Meta/gn/secondary/Userland/Libraries/LibWebView/BUILD.gn @@ -122,6 +122,7 @@ shared_library("LibWebView") { "Database.cpp", "History.cpp", "InspectorClient.cpp", + "ProcessHandle.cpp", "ProcessManager.cpp", "RequestServerAdapter.cpp", "SearchEngine.cpp", diff --git a/Userland/Libraries/LibWebView/CMakeLists.txt b/Userland/Libraries/LibWebView/CMakeLists.txt index 8cbaa4bea4..f1974442a3 100644 --- a/Userland/Libraries/LibWebView/CMakeLists.txt +++ b/Userland/Libraries/LibWebView/CMakeLists.txt @@ -6,6 +6,7 @@ set(SOURCES Database.cpp History.cpp InspectorClient.cpp + ProcessHandle.cpp ProcessManager.cpp RequestServerAdapter.cpp SearchEngine.cpp diff --git a/Userland/Libraries/LibWebView/Forward.h b/Userland/Libraries/LibWebView/Forward.h index 83415c91a4..64332eec92 100644 --- a/Userland/Libraries/LibWebView/Forward.h +++ b/Userland/Libraries/LibWebView/Forward.h @@ -21,6 +21,7 @@ class WebContentClient; struct Attribute; struct CookieStorageKey; +struct ProcessHandle; struct SearchEngine; struct SocketPair; diff --git a/Userland/Libraries/LibWebView/ProcessHandle.cpp b/Userland/Libraries/LibWebView/ProcessHandle.cpp new file mode 100644 index 0000000000..f94a19b0a1 --- /dev/null +++ b/Userland/Libraries/LibWebView/ProcessHandle.cpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2024, Andrew Kaster + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include + +template<> +ErrorOr IPC::encode(IPC::Encoder& encoder, WebView::ProcessHandle const& handle) +{ + TRY(encoder.encode(handle.pid)); + return {}; +} + +template<> +ErrorOr IPC::decode(IPC::Decoder& decoder) +{ + auto pid = TRY(decoder.decode()); + return WebView::ProcessHandle { pid }; +} diff --git a/Userland/Libraries/LibWebView/ProcessHandle.h b/Userland/Libraries/LibWebView/ProcessHandle.h new file mode 100644 index 0000000000..320306248b --- /dev/null +++ b/Userland/Libraries/LibWebView/ProcessHandle.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2024, Andrew Kaster + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include + +namespace WebView { + +struct ProcessHandle { + // FIXME: Use mach_port_t on macOS/Hurd and HANDLE on Windows. + pid_t pid; +}; + +} + +template<> +ErrorOr IPC::encode(IPC::Encoder&, WebView::ProcessHandle const&); + +template<> +ErrorOr IPC::decode(IPC::Decoder&); diff --git a/Userland/Libraries/LibWebView/WebContentClient.cpp b/Userland/Libraries/LibWebView/WebContentClient.cpp index 08b43a6427..e4bb524e03 100644 --- a/Userland/Libraries/LibWebView/WebContentClient.cpp +++ b/Userland/Libraries/LibWebView/WebContentClient.cpp @@ -5,6 +5,7 @@ */ #include "WebContentClient.h" +#include "ProcessManager.h" #include "ViewImplementation.h" #include @@ -33,6 +34,12 @@ void WebContentClient::unregister_view(u64 page_id) m_views.remove(page_id); } +void WebContentClient::notify_process_information(WebView::ProcessHandle const& handle) +{ + dbgln_if(SPAM_DEBUG, "handle: WebContentClient::NotifyProcessInformation! pid={}", handle.pid); + ProcessManager::the().add_process(ProcessType::WebContent, handle.pid); +} + void WebContentClient::did_paint(u64 page_id, Gfx::IntRect const& rect, i32 bitmap_id) { if (auto view = view_for_page_id(page_id); view.has_value()) diff --git a/Userland/Libraries/LibWebView/WebContentClient.h b/Userland/Libraries/LibWebView/WebContentClient.h index fe231fb694..0275277763 100644 --- a/Userland/Libraries/LibWebView/WebContentClient.h +++ b/Userland/Libraries/LibWebView/WebContentClient.h @@ -37,6 +37,7 @@ public: private: virtual void die() override; + virtual void notify_process_information(WebView::ProcessHandle const&) override; virtual void did_paint(u64 page_id, Gfx::IntRect const&, i32) override; virtual void did_finish_loading(u64 page_id, URL::URL const&) override; virtual void did_update_url(u64 page_id, URL::URL const& url, Web::HTML::HistoryHandlingBehavior history_behavior) override; diff --git a/Userland/Services/WebContent/ConnectionFromClient.cpp b/Userland/Services/WebContent/ConnectionFromClient.cpp index b14a5518fa..419fec8540 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.cpp +++ b/Userland/Services/WebContent/ConnectionFromClient.cpp @@ -59,6 +59,7 @@ ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr sock , m_page_host(PageHost::create(*this)) { m_input_event_queue_timer = Web::Platform::Timer::create_single_shot(0, [this] { process_next_input_event(); }); + async_notify_process_information({ ::getpid() }); } void ConnectionFromClient::die() diff --git a/Userland/Services/WebContent/WebContentClient.ipc b/Userland/Services/WebContent/WebContentClient.ipc index c6c691a40f..776c5098f9 100644 --- a/Userland/Services/WebContent/WebContentClient.ipc +++ b/Userland/Services/WebContent/WebContentClient.ipc @@ -15,9 +15,12 @@ #include #include #include +#include endpoint WebContentClient { + notify_process_information(WebView::ProcessHandle handle) =| + did_start_loading(u64 page_id, URL::URL url, bool is_redirect) =| did_finish_loading(u64 page_id, URL::URL url) =| did_update_url(u64 page_id, URL::URL url, Web::HTML::HistoryHandlingBehavior history_behavior) =|