Truly independent web browser
/*
* Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
//! The WebRTCClient endpoints, generated by the IPC compiler from Services/WebRTCClient/*.ipc.
use anyhow::{Result, anyhow};
use libipc_rust::EndpointMessage;
use libipc_rust::wire::{Encoder, Utf8String};
#[cfg(target_vendor = "apple")]
pub use libipc_rust::transport::{ReceivedMessage, TransportMachPort as Transport};
#[cfg(all(unix, not(target_vendor = "apple")))]
pub use libipc_rust::transport_socket::{ReceivedMessage, TransportSocket as Transport};
#[cfg(windows)]
pub use libipc_rust::transport_windows::{ReceivedMessage, TransportWindows as Transport};
/// Both ends of a new connection within this process.
pub fn create_paired_transports() -> std::io::Result<(Transport, Transport)> {
let (local, handle) = Transport::create_paired()?;
#[cfg(target_vendor = "apple")]
let remote = Transport::from_handle(handle)?;
#[cfg(not(target_vendor = "apple"))]
let remote = Transport::new(handle)?;
Ok((local, remote))
}
// NB: The generated code names the types an .ipc file declares as crate::ipc::ipc_types::Namespace_Name.
#[allow(clippy::all, unused_imports)]
mod ipc_types {
include!(concat!(env!("OUT_DIR"), "/WebRTCClientServerEndpoint.rs"));
include!(concat!(env!("OUT_DIR"), "/WebRTCClientClientEndpoint.rs"));
}
pub use ipc_types::web_rtcclient_client::Message as ClientMessage;
pub use ipc_types::web_rtcclient_client::messages as client;
pub use ipc_types::web_rtcclient_server::Message as ServerMessage;
pub use ipc_types::web_rtcclient_server::messages as server;
/// Converts a C++ `String` to a Rust one, replacing any lone surrogates it carries.
pub fn to_string(string: Utf8String) -> String {
String::from_utf8(string.into_bytes())
.unwrap_or_else(|error| String::from_utf8_lossy(error.as_bytes()).into_owned())
}
pub fn to_optional_string(string: Option<Utf8String>) -> Option<String> {
string.map(to_string)
}
pub fn encode<M: EndpointMessage>(message: &M) -> Result<(Vec<u8>, Vec<libipc_rust::Attachment>)> {
let mut encoder = Encoder::new();
encoder
.encode(&M::ENDPOINT_MAGIC)
.map_err(|error| anyhow!("{error:?}"))?;
encoder.encode(&M::MESSAGE_ID).map_err(|error| anyhow!("{error:?}"))?;
encoder.encode(message).map_err(|error| anyhow!("{error:?}"))?;
Ok(encoder.finish())
}
pub fn send<M: EndpointMessage>(transport: &Transport, message: &M) -> Result<()> {
let (bytes, attachments) = encode(message)?;
transport.post_message(bytes, attachments)?;
Ok(())
}