LibWebView: Port to Windows

This commit is contained in:
stasoid 2024-12-21 16:19:39 +05:00 committed by Andrew Kaster
parent 2abc792938
commit 2dd657f530
Notes: github-actions[bot] 2025-03-20 02:26:23 +00:00
5 changed files with 33 additions and 13 deletions

View file

@ -59,9 +59,11 @@ Application::~Application()
void Application::initialize(Main::Arguments const& arguments, URL::URL new_tab_page_url)
{
#ifndef AK_OS_WINDOWS
// Increase the open file limit, as the default limits on Linux cause us to run out of file descriptors with around 15 tabs open.
if (auto result = Core::System::set_resource_limits(RLIMIT_NOFILE, 8192); result.is_error())
warnln("Unable to increase open file limit: {}", result.error());
#endif
Vector<ByteString> raw_urls;
Vector<ByteString> certificates;

View file

@ -40,15 +40,15 @@ ErrorOr<BrowserProcess::ProcessDisposition> BrowserProcess::connect(Vector<ByteS
m_pid_path = pid_path;
m_pid_file = TRY(Core::File::open(pid_path, Core::File::OpenMode::Write));
TRY(m_pid_file->write_until_depleted(ByteString::number(::getpid())));
TRY(m_pid_file->write_until_depleted(ByteString::number(Core::System::getpid())));
return ProcessDisposition::ContinueMainProcess;
}
ErrorOr<void> BrowserProcess::connect_as_client(ByteString const& socket_path, Vector<ByteString> const& raw_urls, NewWindow new_window)
{
// TODO: Mach IPC
auto socket = TRY(Core::LocalSocket::connect(socket_path));
static_assert(IsSame<IPC::Transport, IPC::TransportSocket>, "Need to handle other IPC transports here");
auto client = UIProcessClient::construct(IPC::Transport(move(socket)));
if (new_window == NewWindow::Yes) {
@ -64,8 +64,7 @@ ErrorOr<void> BrowserProcess::connect_as_client(ByteString const& socket_path, V
ErrorOr<void> BrowserProcess::connect_as_server(ByteString const& socket_path)
{
static_assert(IsSame<IPC::Transport, IPC::TransportSocket>, "Need to handle other IPC transports here");
// TODO: Mach IPC
auto socket_fd = TRY(Process::create_ipc_socket(socket_path));
m_socket_path = socket_path;
auto local_server = TRY(Core::LocalServer::try_create());

View file

@ -27,7 +27,7 @@ Process::~Process()
ErrorOr<Process::ProcessAndIPCTransport> Process::spawn_and_connect_to_process(Core::ProcessSpawnOptions const& options)
{
static_assert(IsSame<IPC::Transport, IPC::TransportSocket>, "Need to handle other IPC transports here");
// TODO: Mach IPC
int socket_fds[2] {};
TRY(Core::System::socketpair(AF_LOCAL, SOCK_STREAM, 0, socket_fds));
@ -35,8 +35,8 @@ ErrorOr<Process::ProcessAndIPCTransport> Process::spawn_and_connect_to_process(C
ArmedScopeGuard guard_fd_0 { [&] { MUST(Core::System::close(socket_fds[0])); } };
ArmedScopeGuard guard_fd_1 { [&] { MUST(Core::System::close(socket_fds[1])); } };
auto& file_actions = const_cast<Core::ProcessSpawnOptions&>(options).file_actions;
file_actions.append(Core::FileAction::CloseFile { socket_fds[0] });
// Note: Core::System::socketpair creates inheritable sockets both on Linux and Windows unless SOCK_CLOEXEC is specified.
TRY(Core::System::set_close_on_exec(socket_fds[0], true));
auto takeover_string = MUST(String::formatted("{}:{}", options.name, socket_fds[1]));
TRY(Core::Environment::set("SOCKET_TAKEOVER"sv, takeover_string, Core::Environment::Overwrite::Yes));
@ -50,6 +50,18 @@ ErrorOr<Process::ProcessAndIPCTransport> Process::spawn_and_connect_to_process(C
return ProcessAndIPCTransport { move(process), IPC::Transport(move(ipc_socket)) };
}
#ifdef AK_OS_WINDOWS
// FIXME: Implement WebView::Process::get_process_pid on Windows
ErrorOr<Optional<pid_t>> Process::get_process_pid(StringView, StringView)
{
VERIFY(0 && "WebView::Process::get_process_pid is not implemented");
}
// FIXME: Implement WebView::Process::create_ipc_socket on Windows
ErrorOr<int> Process::create_ipc_socket(ByteString const&)
{
VERIFY(0 && "WebView::Process::create_ipc_socket is not implemented");
}
#else
ErrorOr<Optional<pid_t>> Process::get_process_pid(StringView process_name, StringView pid_path)
{
if (Core::System::stat(pid_path).is_error())
@ -92,19 +104,19 @@ ErrorOr<int> Process::create_ipc_socket(ByteString const& socket_path)
if (!Core::System::stat(socket_path).is_error())
TRY(Core::System::unlink(socket_path));
#ifdef SOCK_NONBLOCK
# ifdef SOCK_NONBLOCK
auto socket_fd = TRY(Core::System::socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0));
#else
# else
auto socket_fd = TRY(Core::System::socket(AF_LOCAL, SOCK_STREAM, 0));
int option = 1;
TRY(Core::System::ioctl(socket_fd, FIONBIO, &option));
TRY(Core::System::fcntl(socket_fd, F_SETFD, FD_CLOEXEC));
#endif
# endif
#if !defined(AK_OS_BSD_GENERIC) && !defined(AK_OS_GNU_HURD)
# if !defined(AK_OS_BSD_GENERIC) && !defined(AK_OS_GNU_HURD)
TRY(Core::System::fchmod(socket_fd, 0600));
#endif
# endif
auto socket_address = Core::SocketAddress::local(socket_path);
auto socket_address_un = socket_address.to_sockaddr_un().release_value();
@ -114,6 +126,7 @@ ErrorOr<int> Process::create_ipc_socket(ByteString const& socket_path)
return socket_fd;
}
#endif
ErrorOr<Process::ProcessPaths> Process::paths_for_process(StringView process_name)
{

View file

@ -50,6 +50,8 @@ StringView process_name_from_type(ProcessType type)
ProcessManager::ProcessManager()
: on_process_exited([](Process&&) { })
{
// FIXME: Handle exiting child processes on Windows
#ifndef AK_OS_WINDOWS
m_signal_handle = Core::EventLoop::register_signal(SIGCHLD, [this](int) {
auto result = Core::System::waitpid(-1, WNOHANG);
while (!result.is_error() && result.value().pid > 0) {
@ -61,6 +63,7 @@ ProcessManager::ProcessManager()
result = Core::System::waitpid(-1, WNOHANG);
}
});
#endif
add_process(Process(WebView::ProcessType::Browser, nullptr, Core::Process::current()));
@ -74,7 +77,10 @@ ProcessManager::ProcessManager()
ProcessManager::~ProcessManager()
{
// FIXME: Handle exiting child processes on Windows
#ifndef AK_OS_WINDOWS
Core::EventLoop::unregister_signal(m_signal_handle);
#endif
}
Optional<Process&> ProcessManager::find_process(pid_t pid)

View file

@ -104,7 +104,7 @@ ErrorOr<Vector<ByteString>> get_paths_for_helper_process(StringView process_name
auto application_path = TRY(application_directory());
Vector<ByteString> paths;
#if !defined(AK_OS_MACOS)
#if !defined(AK_OS_MACOS) && !defined(AK_OS_WINDOWS)
auto prefix = find_prefix(LexicalPath(application_path));
TRY(paths.try_append(LexicalPath::join(prefix.string(), libexec_path, process_name).string()));
TRY(paths.try_append(LexicalPath::join(prefix.string(), "bin"sv, process_name).string()));