2024-04-26 18:20:30 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <AK/ByteString.h>
|
2026-06-04 05:40:32 -03:00
|
|
|
#include <AK/NeverDestroyed.h>
|
2024-04-26 18:20:30 -03:00
|
|
|
#include <LibCore/Process.h>
|
2026-03-20 17:47:34 -03:00
|
|
|
#include <LibCore/Socket.h>
|
2024-07-22 11:17:44 -03:00
|
|
|
#include <LibCore/System.h>
|
2024-04-26 18:20:30 -03:00
|
|
|
#include <LibIPC/ConnectionToServer.h>
|
2026-03-19 15:42:05 -03:00
|
|
|
#include <LibIPC/Transport.h>
|
2026-03-22 15:59:08 -03:00
|
|
|
#if defined(AK_OS_MACOS)
|
|
|
|
|
# include <LibIPC/TransportBootstrapMach.h>
|
|
|
|
|
#endif
|
2024-07-30 15:01:05 -03:00
|
|
|
#include <LibWebView/Application.h>
|
2025-03-15 18:00:58 -03:00
|
|
|
#include <LibWebView/BrowserProcess.h>
|
2024-07-30 15:01:05 -03:00
|
|
|
#include <LibWebView/URL.h>
|
2026-03-22 15:59:08 -03:00
|
|
|
#include <LibWebView/Utilities.h>
|
2024-04-26 18:20:30 -03:00
|
|
|
|
|
|
|
|
namespace WebView {
|
|
|
|
|
|
2026-06-04 05:40:32 -03:00
|
|
|
static HashMap<int, RefPtr<UIProcessConnectionFromClient>>& connections()
|
|
|
|
|
{
|
|
|
|
|
static NeverDestroyed<HashMap<int, RefPtr<UIProcessConnectionFromClient>>> connections;
|
|
|
|
|
return *connections;
|
|
|
|
|
}
|
2024-04-26 18:20:30 -03:00
|
|
|
|
|
|
|
|
class UIProcessClient final
|
|
|
|
|
: public IPC::ConnectionToServer<UIProcessClientEndpoint, UIProcessServerEndpoint> {
|
|
|
|
|
C_OBJECT(UIProcessClient);
|
|
|
|
|
|
|
|
|
|
private:
|
2025-04-08 17:01:46 -03:00
|
|
|
explicit UIProcessClient(NonnullOwnPtr<IPC::Transport>);
|
2024-04-26 18:20:30 -03:00
|
|
|
};
|
|
|
|
|
|
2025-03-15 18:00:58 -03:00
|
|
|
ErrorOr<BrowserProcess::ProcessDisposition> BrowserProcess::connect(Vector<ByteString> const& raw_urls, NewWindow new_window)
|
2024-04-26 18:20:30 -03:00
|
|
|
{
|
|
|
|
|
static constexpr auto process_name = "Ladybird"sv;
|
|
|
|
|
|
2024-10-22 16:27:15 -03:00
|
|
|
auto [socket_path, pid_path] = TRY(Process::paths_for_process(process_name));
|
2024-04-26 18:20:30 -03:00
|
|
|
|
2024-10-22 16:27:15 -03:00
|
|
|
if (auto pid = TRY(Process::get_process_pid(process_name, pid_path)); pid.has_value()) {
|
2026-03-22 15:59:08 -03:00
|
|
|
#if defined(AK_OS_MACOS)
|
|
|
|
|
TRY(connect_as_client(*pid, raw_urls, new_window));
|
|
|
|
|
#else
|
2024-04-26 18:20:30 -03:00
|
|
|
TRY(connect_as_client(socket_path, raw_urls, new_window));
|
2026-03-22 15:59:08 -03:00
|
|
|
#endif
|
2024-04-26 18:20:30 -03:00
|
|
|
return ProcessDisposition::ExitProcess;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-22 15:59:08 -03:00
|
|
|
#if defined(AK_OS_MACOS)
|
|
|
|
|
TRY(connect_as_server());
|
|
|
|
|
#else
|
2024-04-26 18:20:30 -03:00
|
|
|
TRY(connect_as_server(socket_path));
|
2026-03-22 15:59:08 -03:00
|
|
|
#endif
|
2024-04-26 18:20:30 -03:00
|
|
|
|
|
|
|
|
m_pid_path = pid_path;
|
|
|
|
|
m_pid_file = TRY(Core::File::open(pid_path, Core::File::OpenMode::Write));
|
2024-12-21 08:19:39 -03:00
|
|
|
TRY(m_pid_file->write_until_depleted(ByteString::number(Core::System::getpid())));
|
2024-04-26 18:20:30 -03:00
|
|
|
|
|
|
|
|
return ProcessDisposition::ContinueMainProcess;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-22 15:59:08 -03:00
|
|
|
#if defined(AK_OS_MACOS)
|
|
|
|
|
ErrorOr<void> BrowserProcess::connect_as_client(pid_t pid, Vector<ByteString> const& raw_urls, NewWindow new_window)
|
|
|
|
|
{
|
|
|
|
|
auto transport_ports = TRY(IPC::bootstrap_transport_from_mach_server(mach_server_name_for_process("Ladybird"sv, pid)));
|
|
|
|
|
auto client = UIProcessClient::construct(make<IPC::Transport>(move(transport_ports.receive_right), move(transport_ports.send_right)));
|
|
|
|
|
|
|
|
|
|
switch (new_window) {
|
|
|
|
|
case NewWindow::Yes:
|
|
|
|
|
if (!client->send_sync_but_allow_failure<Messages::UIProcessServer::CreateNewWindow>(raw_urls))
|
|
|
|
|
dbgln("Failed to send CreateNewWindow message to UIProcess");
|
|
|
|
|
return {};
|
|
|
|
|
case NewWindow::No:
|
|
|
|
|
if (!client->send_sync_but_allow_failure<Messages::UIProcessServer::CreateNewTab>(raw_urls))
|
|
|
|
|
dbgln("Failed to send CreateNewTab message to UIProcess");
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
#else
|
2025-08-24 03:47:03 -03:00
|
|
|
ErrorOr<void> BrowserProcess::connect_as_client(ByteString const& socket_path, Vector<ByteString> const& raw_urls, NewWindow new_window)
|
2024-04-26 18:20:30 -03:00
|
|
|
{
|
|
|
|
|
auto socket = TRY(Core::LocalSocket::connect(socket_path));
|
2026-03-19 15:42:05 -03:00
|
|
|
auto transport = TRY(IPC::Transport::from_socket(move(socket)));
|
|
|
|
|
auto client = UIProcessClient::construct(move(transport));
|
2024-04-26 18:20:30 -03:00
|
|
|
|
2026-03-20 17:47:34 -03:00
|
|
|
switch (new_window) {
|
|
|
|
|
case NewWindow::Yes:
|
2024-04-26 18:20:30 -03:00
|
|
|
if (!client->send_sync_but_allow_failure<Messages::UIProcessServer::CreateNewWindow>(raw_urls))
|
|
|
|
|
dbgln("Failed to send CreateNewWindow message to UIProcess");
|
2026-03-20 17:47:34 -03:00
|
|
|
return {};
|
|
|
|
|
case NewWindow::No:
|
2024-04-26 18:20:30 -03:00
|
|
|
if (!client->send_sync_but_allow_failure<Messages::UIProcessServer::CreateNewTab>(raw_urls))
|
|
|
|
|
dbgln("Failed to send CreateNewTab message to UIProcess");
|
2026-03-20 17:47:34 -03:00
|
|
|
return {};
|
2024-04-26 18:20:30 -03:00
|
|
|
}
|
|
|
|
|
|
2026-03-20 17:47:34 -03:00
|
|
|
VERIFY_NOT_REACHED();
|
2024-04-26 18:20:30 -03:00
|
|
|
}
|
2026-03-22 15:59:08 -03:00
|
|
|
#endif
|
2024-04-26 18:20:30 -03:00
|
|
|
|
2026-03-22 15:59:08 -03:00
|
|
|
#if defined(AK_OS_MACOS)
|
|
|
|
|
ErrorOr<void> BrowserProcess::connect_as_server()
|
|
|
|
|
{
|
|
|
|
|
Application::the().set_browser_process_transport_handler([this](auto transport) {
|
|
|
|
|
accept_transport(move(transport));
|
|
|
|
|
});
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
#else
|
2025-08-24 03:47:03 -03:00
|
|
|
ErrorOr<void> BrowserProcess::connect_as_server(ByteString const& socket_path)
|
2024-04-26 18:20:30 -03:00
|
|
|
{
|
2024-10-22 16:27:15 -03:00
|
|
|
auto socket_fd = TRY(Process::create_ipc_socket(socket_path));
|
2024-04-26 18:20:30 -03:00
|
|
|
m_socket_path = socket_path;
|
2026-03-20 17:47:34 -03:00
|
|
|
m_local_server = Core::LocalServer::construct();
|
|
|
|
|
TRY(m_local_server->take_over_fd(socket_fd));
|
2024-10-22 18:47:33 -03:00
|
|
|
|
2026-03-20 17:47:34 -03:00
|
|
|
m_local_server->on_accept = [this](auto client_socket) {
|
2026-03-19 15:42:05 -03:00
|
|
|
auto transport = IPC::Transport::from_socket(move(client_socket));
|
|
|
|
|
if (transport.is_error()) {
|
|
|
|
|
dbgln("Failed to create IPC transport for UIProcess client: {}", transport.error());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
accept_transport(transport.release_value());
|
2026-03-20 17:47:34 -03:00
|
|
|
};
|
2024-04-26 18:20:30 -03:00
|
|
|
|
2026-03-20 17:47:34 -03:00
|
|
|
return {};
|
|
|
|
|
}
|
2026-03-22 15:59:08 -03:00
|
|
|
#endif
|
2024-04-26 18:20:30 -03:00
|
|
|
|
2026-03-20 17:47:34 -03:00
|
|
|
void BrowserProcess::accept_transport(NonnullOwnPtr<IPC::Transport> transport)
|
|
|
|
|
{
|
|
|
|
|
auto client = UIProcessConnectionFromClient::construct(move(transport), ++m_next_client_id);
|
|
|
|
|
client->on_new_tab = [this](auto raw_urls) {
|
|
|
|
|
if (this->on_new_tab)
|
|
|
|
|
this->on_new_tab(raw_urls);
|
2024-04-26 18:20:30 -03:00
|
|
|
};
|
|
|
|
|
|
2026-03-20 17:47:34 -03:00
|
|
|
client->on_new_window = [this](auto raw_urls) {
|
|
|
|
|
if (this->on_new_window)
|
|
|
|
|
this->on_new_window(raw_urls);
|
|
|
|
|
};
|
2024-04-26 18:20:30 -03:00
|
|
|
}
|
|
|
|
|
|
2025-03-15 18:00:58 -03:00
|
|
|
BrowserProcess::~BrowserProcess()
|
2024-04-26 18:20:30 -03:00
|
|
|
{
|
2026-03-22 15:59:08 -03:00
|
|
|
#if defined(AK_OS_MACOS)
|
|
|
|
|
Application::the().set_browser_process_transport_handler({});
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-04-26 18:20:30 -03:00
|
|
|
if (m_pid_file) {
|
|
|
|
|
MUST(m_pid_file->truncate(0));
|
2025-08-24 03:47:03 -03:00
|
|
|
#if defined(AK_OS_WINDOWS)
|
|
|
|
|
// NOTE: On Windows, System::open() duplicates the underlying OS file handle,
|
|
|
|
|
// so we need to explicitly close said handle, otherwise the unlink() call fails due
|
|
|
|
|
// to permission errors and we crash on shutdown.
|
|
|
|
|
m_pid_file->close();
|
|
|
|
|
#endif
|
2024-04-26 18:20:30 -03:00
|
|
|
MUST(Core::System::unlink(m_pid_path));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!m_socket_path.is_empty())
|
|
|
|
|
MUST(Core::System::unlink(m_socket_path));
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-08 17:01:46 -03:00
|
|
|
UIProcessClient::UIProcessClient(NonnullOwnPtr<IPC::Transport> transport)
|
2024-10-22 18:47:33 -03:00
|
|
|
: IPC::ConnectionToServer<UIProcessClientEndpoint, UIProcessServerEndpoint>(*this, move(transport))
|
2024-04-26 18:20:30 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-08 17:01:46 -03:00
|
|
|
UIProcessConnectionFromClient::UIProcessConnectionFromClient(NonnullOwnPtr<IPC::Transport> transport, int client_id)
|
2024-10-22 18:47:33 -03:00
|
|
|
: IPC::ConnectionFromClient<UIProcessClientEndpoint, UIProcessServerEndpoint>(*this, move(transport), client_id)
|
2024-04-26 18:20:30 -03:00
|
|
|
{
|
2026-06-04 05:40:32 -03:00
|
|
|
connections().set(client_id, *this);
|
2024-04-26 18:20:30 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UIProcessConnectionFromClient::die()
|
|
|
|
|
{
|
2026-06-04 05:40:32 -03:00
|
|
|
connections().remove(client_id());
|
2024-04-26 18:20:30 -03:00
|
|
|
}
|
|
|
|
|
|
2025-03-08 14:22:39 -03:00
|
|
|
void UIProcessConnectionFromClient::create_new_tab(Vector<ByteString> urls)
|
2024-04-26 18:20:30 -03:00
|
|
|
{
|
|
|
|
|
if (on_new_tab)
|
2026-04-24 08:47:57 -03:00
|
|
|
on_new_tab(sanitize_urls(urls));
|
2024-04-26 18:20:30 -03:00
|
|
|
}
|
|
|
|
|
|
2025-03-08 14:22:39 -03:00
|
|
|
void UIProcessConnectionFromClient::create_new_window(Vector<ByteString> urls)
|
2024-04-26 18:20:30 -03:00
|
|
|
{
|
|
|
|
|
if (on_new_window)
|
2026-04-24 08:47:57 -03:00
|
|
|
on_new_window(sanitize_urls(urls));
|
2024-04-26 18:20:30 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|