mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2026-04-18 01:40:33 +00:00
On macOS, use Mach port messaging instead of Unix domain sockets for all IPC transport. This makes the transport capable of carrying Mach port rights as message attachments, which is a prerequisite for sending IOSurface handles over the main IPC channel (currently sent via a separate out-of-band path). It also avoids the need for the FD acknowledgement protocol that TransportSocket requires, since Mach port right transfers are atomic in the kernel. Three connection establishment patterns: - Spawned helper processes (WebContent, RequestServer, etc.) use the existing MachPortServer: the child sends its task port with a reply port, and the parent responds with a pre-created port pair. - Socket-bootstrapped connections (WebDriver, BrowserProcess) exchange Mach port names over the socket, then drop the socket. - Pre-created pairs for IPC tests and in-message transport transfer. Attachment on macOS now wraps a MachPort instead of a file descriptor, converting between the two via fileport_makeport()/fileport_makefd(). The LibIPC socket transport tests are disabled on macOS since they are socket-specific.
86 lines
2.5 KiB
C++
86 lines
2.5 KiB
C++
/*
|
|
* Copyright (c) 2026, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <LibCore/Socket.h>
|
|
#include <LibIPC/Attachment.h>
|
|
#include <LibIPC/Decoder.h>
|
|
#include <LibIPC/Encoder.h>
|
|
#include <LibIPC/Transport.h>
|
|
#include <LibIPC/TransportHandle.h>
|
|
|
|
namespace IPC {
|
|
|
|
#if defined(AK_OS_MACOS)
|
|
|
|
TransportHandle::TransportHandle(Core::MachPort receive_right, Core::MachPort send_right)
|
|
: m_receive_right(move(receive_right))
|
|
, m_send_right(move(send_right))
|
|
{
|
|
VERIFY(MACH_PORT_VALID(m_receive_right.port()));
|
|
VERIFY(MACH_PORT_VALID(m_send_right.port()));
|
|
}
|
|
|
|
ErrorOr<NonnullOwnPtr<Transport>> TransportHandle::create_transport() const
|
|
{
|
|
VERIFY(MACH_PORT_VALID(m_receive_right.port()));
|
|
VERIFY(MACH_PORT_VALID(m_send_right.port()));
|
|
return make<Transport>(move(m_receive_right), move(m_send_right));
|
|
}
|
|
|
|
template<>
|
|
ErrorOr<void> encode(Encoder& encoder, TransportHandle const& handle)
|
|
{
|
|
VERIFY(MACH_PORT_VALID(handle.m_receive_right.port()));
|
|
VERIFY(MACH_PORT_VALID(handle.m_send_right.port()));
|
|
TRY(encoder.append_attachment(Attachment::from_mach_port(move(handle.m_receive_right), Core::MachPort::MessageRight::MoveReceive)));
|
|
TRY(encoder.append_attachment(Attachment::from_mach_port(move(handle.m_send_right), Core::MachPort::MessageRight::MoveSend)));
|
|
return {};
|
|
}
|
|
|
|
template<>
|
|
ErrorOr<TransportHandle> decode(Decoder& decoder)
|
|
{
|
|
auto& attachments = decoder.attachments();
|
|
VERIFY(attachments.size() >= 2);
|
|
auto recv_attachment = attachments.dequeue();
|
|
auto send_attachment = attachments.dequeue();
|
|
VERIFY(recv_attachment.message_right() == Core::MachPort::MessageRight::MoveReceive);
|
|
VERIFY(send_attachment.message_right() == Core::MachPort::MessageRight::MoveSend);
|
|
auto receive_right = recv_attachment.release_mach_port();
|
|
auto send_right = send_attachment.release_mach_port();
|
|
return TransportHandle { move(receive_right), move(send_right) };
|
|
}
|
|
|
|
#else
|
|
|
|
TransportHandle::TransportHandle(File file)
|
|
: m_file(move(file))
|
|
{
|
|
}
|
|
|
|
ErrorOr<NonnullOwnPtr<Transport>> TransportHandle::create_transport() const
|
|
{
|
|
auto socket = TRY(Core::LocalSocket::adopt_fd(m_file.take_fd()));
|
|
TRY(socket->set_blocking(false));
|
|
return make<Transport>(move(socket));
|
|
}
|
|
|
|
template<>
|
|
ErrorOr<void> encode(Encoder& encoder, TransportHandle const& handle)
|
|
{
|
|
return encoder.encode(handle.m_file);
|
|
}
|
|
|
|
template<>
|
|
ErrorOr<TransportHandle> decode(Decoder& decoder)
|
|
{
|
|
auto file = TRY(decoder.decode<File>());
|
|
return TransportHandle { move(file) };
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|