Skip to content

Truly independent web browser

LibWeb: Write transfer data holders in Rust

Shannon Booth522c5725bdbd
diff --git a/Cargo.lock b/Cargo.lock
index 96a6abee2fb..c5fb5d97a9b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1963,6 +1963,7 @@ dependencies = [
"libcompress_rust",
"libgfx_rust",
"libhttp_rust",
+ "libipc_rust",
"libjs_rust",
"libtextcodec_rust",
"libunicode_rust",
diff --git a/Libraries/LibWeb/Bindings/Rust/Cargo.toml b/Libraries/LibWeb/Bindings/Rust/Cargo.toml
index 4ed944215c3..3406fb394a9 100644
--- a/Libraries/LibWeb/Bindings/Rust/Cargo.toml
+++ b/Libraries/LibWeb/Bindings/Rust/Cargo.toml
@@ -25,6 +25,7 @@ libcompositing_rust = { path = "../../../LibCompositing/Rust" }
libcompress_rust = { path = "../../../LibCompress/Rust" }
libgfx_rust = { path = "../../../LibGfx/Rust", default-features = false }
libhttp_rust = { path = "../../../LibHTTP/Rust" }
+libipc_rust = { path = "../../../LibIPC/Rust" }
libjs_rust = { path = "../../../LibJS/Rust", default-features = false }
libtextcodec_rust = { path = "../../../LibTextCodec/Rust" }
libunicode_rust = { path = "../../../LibUnicode/Rust", default-features = false }
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/structured_data.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/structured_data.rs
index aa41af78432..27931db052c 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/structured_data.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/structured_data.rs
@@ -22,6 +22,7 @@ use core::marker::PhantomData;
use core::ptr::NonNull;
use ak::Utf16String;
+use libipc_rust::Encode;
use libjs_rust::api::{Gc, RawNativeFunctionResult, Realm, ThrowCompletionOr, Value};
use libwebcommon_rust::html::serialization_records::SerializationType;
@@ -29,10 +30,11 @@ use crate::bindings::interface::{ConstructedObject, Interface, Ref, cell_from_ab
use crate::bindings::transport::TransportHandle;
use crate::bindings::{CallContext, WebIdlResult};
use crate::html::structured_serialize::cpp::{
- ImageBitmapSharedPixels, OwnedSerializedTransferRecord, TransferDataDecoder, TransferDataHolder,
+ ImageBitmapSharedPixels, OwnedSerializedTransferRecord, TransferDataDecoder,
data_clone_error_from_serialization_error,
};
use crate::html::structured_serialize::reader::StructuredSerializeReader;
+use crate::html::structured_serialize::transfer_data_holder::TransferDataHolder;
use crate::html::structured_serialize::writer::StructuredSerializeWriter;
use crate::html::structured_serialize::{
Deserializer, Serializer, decode_serializable_object_header, encode_serializable_object_header,
@@ -40,7 +42,6 @@ use crate::html::structured_serialize::{
};
unsafe extern "C" {
- fn web_bindings_transfer_data_encode_bool(data_holder: *mut c_void, value: bool);
fn web_bindings_transfer_data_encode_records(data_holder: *mut c_void, records: *const *mut c_void, count: usize);
fn web_bindings_transfer_data_encode_transport_handle(data_holder: *mut c_void, handle: *mut c_void);
fn web_bindings_transfer_data_extend(data_holder: *mut c_void, record: *mut c_void);
@@ -244,39 +245,25 @@ pub(crate) trait Serializable: Interface {
) -> WebIdlResult<impl ConstructedObject<Self> + use<Self>>;
}
-/// The data holder of a transferred object, which its transfer steps write to: an HTML::TransferDataEncoder, in the
-/// realm of the transfer, which creates what the steps throw.
+/// The data holder of a transferred object, which its transfer steps write to, in the realm of the transfer, which
+/// creates what the steps throw.
pub(crate) struct TransferDataWriter<'a> {
- raw: NonNull<c_void>,
+ data_holder: &'a TransferDataHolder,
realm: Gc<Realm>,
/// AD-HOC: The shared memory that the transfer gathers the pixels of its ImageBitmaps in, if it has some.
- image_bitmap_pixels: Option<NonNull<c_void>>,
- _lent: PhantomData<&'a mut c_void>,
+ image_bitmap_pixels: Option<&'a ImageBitmapSharedPixels>,
}
impl<'a> TransferDataWriter<'a> {
pub(crate) fn new(
data_holder: &'a TransferDataHolder,
realm: Gc<Realm>,
- image_bitmap_pixels: &'a ImageBitmapSharedPixels,
+ image_bitmap_pixels: Option<&'a ImageBitmapSharedPixels>,
) -> Self {
Self {
- raw: data_holder.as_raw(),
+ data_holder,
realm,
- image_bitmap_pixels: image_bitmap_pixels.as_raw(),
- _lent: PhantomData,
- }
- }
-
- /// # Safety
- ///
- /// `data_holder` must be an HTML::TransferDataEncoder that C++ lends for `'a`.
- pub(crate) unsafe fn from_lent(data_holder: *mut c_void, realm: Gc<Realm>) -> Self {
- Self {
- raw: NonNull::new(data_holder).expect("C++ lends a data holder"),
- realm,
- image_bitmap_pixels: None,
- _lent: PhantomData,
+ image_bitmap_pixels,
}
}
@@ -286,38 +273,46 @@ impl<'a> TransferDataWriter<'a> {
/// The shared pixels as the C ABI of ImageBitmap's C++ code takes them, or null.
pub(crate) fn image_bitmap_pixels_as_raw(&self) -> *mut c_void {
- self.image_bitmap_pixels.map_or(core::ptr::null_mut(), NonNull::as_ptr)
+ self.image_bitmap_pixels
+ .and_then(ImageBitmapSharedPixels::as_raw)
+ .map_or(core::ptr::null_mut(), NonNull::as_ptr)
}
- /// The data holder as the C ABI of an interface's C++ class takes it, which encodes what the steps write.
- pub(crate) fn as_raw(&mut self) -> *mut c_void {
- self.raw.as_ptr()
+ pub(crate) fn write<T: Encode + ?Sized>(&mut self, value: &T) {
+ self.data_holder.encode(value);
}
pub(crate) fn write_bool(&mut self, value: bool) {
- // SAFETY: The data holder is lent for the steps.
- unsafe { web_bindings_transfer_data_encode_bool(self.raw.as_ptr(), value) };
+ self.write(&value);
}
/// Writes the records of structured serializations with transfer, as a list.
pub(crate) fn write_records(&mut self, records: &[OwnedSerializedTransferRecord]) {
let records: Vec<*mut c_void> = records.iter().map(|record| record.record().as_raw()).collect();
- // SAFETY: The data holder is lent for the steps, and the call borrows the records.
- unsafe { web_bindings_transfer_data_encode_records(self.raw.as_ptr(), records.as_ptr(), records.len()) };
+ // SAFETY: The carrier is live, and the call borrows the records.
+ unsafe {
+ web_bindings_transfer_data_encode_records(
+ self.data_holder.carrier().as_raw(),
+ records.as_ptr(),
+ records.len(),
+ );
+ }
}
/// Writes the handle of one end of an IPC transport, whose rights the data holder takes.
pub(crate) fn write_transport_handle(&mut self, handle: TransportHandle) {
- // SAFETY: The data holder is lent for the steps, and the call borrows the handle.
- unsafe { web_bindings_transfer_data_encode_transport_handle(self.raw.as_ptr(), handle.as_raw()) };
+ // SAFETY: The carrier is live, and the call borrows the handle.
+ unsafe {
+ web_bindings_transfer_data_encode_transport_handle(self.data_holder.carrier().as_raw(), handle.as_raw());
+ }
}
/// Writes ! StructuredSerializeWithTransfer(value, « value ») as the data holder of value, which is all that the
/// record holds: its [[Serialized]] is a reference to the transferred value. read_transferred_value() reads it.
pub(crate) fn write_transferred_value(&mut self, cx: &CallContext, value: Value) -> WebIdlResult<()> {
let record = structured_serialize_with_transfer(cx, value, &[value.as_object()])?;
- // SAFETY: The data holder is lent for the steps, and the call borrows the record.
- unsafe { web_bindings_transfer_data_extend(self.raw.as_ptr(), record.record().as_raw()) };
+ // SAFETY: The carrier is live, and the call borrows the record.
+ unsafe { web_bindings_transfer_data_extend(self.data_holder.carrier().as_raw(), record.record().as_raw()) };
Ok(())
}
}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/exports.rs b/Libraries/LibWeb/Bindings/Rust/src/html/exports.rs
index 553ff4b2772..b2ae6cad700 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/exports.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/exports.rs
@@ -33,6 +33,7 @@ use crate::bindings::structured_data::{TransferDataReader, TransferDataWriter, T
use crate::dom::event::EventInit;
use crate::html::structured_serialize::cpp::TransferDataDecoder;
use crate::html::structured_serialize::message_record::MessageRecord;
+use crate::html::structured_serialize::transfer_data_holder::TransferDataHolder;
use crate::storage_api::storage_area_mirror::StorageEndpointType;
use crate::storage_api::storage_bottle::{obtain_a_local_storage_bottle_map, obtain_a_session_storage_bottle_map};
use crate::storage_api::storage_key::StorageKey;
@@ -341,26 +342,27 @@ pub unsafe extern "C" fn web_rust_message_port_fail_next_transfer_for_testing(po
unsafe { cell_from_abi::<MessagePort>(port) }.fail_next_transfer_for_testing();
}
-/// The transfer steps of the MessagePort, which C++ runs outside StructuredSerializeWithTransfer, writing to a data
-/// holder that it lends, which holds no type.
+/// The transfer steps of the MessagePort, which C++ runs outside StructuredSerializeWithTransfer, into a data holder
+/// that holds no type, whose carrier it hands over.
///
/// # Safety
///
-/// `vm` must be the live VM, `realm` a live realm, `port` a live cell of a MessagePort, and `data_holder` an
-/// HTML::TransferDataEncoder that C++ lends for the call.
+/// `vm` must be the live VM, `realm` a live realm, and `port` a live cell of a MessagePort.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn web_rust_message_port_transfer_steps(
vm: *mut c_void,
realm: *mut c_void,
port: *mut c_void,
- data_holder: *mut c_void,
+ out_data_holder: &mut *mut c_void,
) -> RawNativeFunctionResult {
// SAFETY: The caller passes the live VM, a live realm and a live cell of a MessagePort.
let ((vm, realm), port) = unsafe { (realm_arguments(vm, realm), cell_from_abi::<MessagePort>(port)) };
let mut cx = CallContext::new(vm, realm);
- // SAFETY: The caller lends the data holder for the call.
- let mut data_holder = unsafe { TransferDataWriter::from_lent(data_holder, realm) };
- let result = MessagePort::transfer_steps(&port, &mut cx, &mut data_holder);
+ let data_holder = TransferDataHolder::new();
+ let result = MessagePort::transfer_steps(&port, &mut cx, &mut TransferDataWriter::new(&data_holder, realm, None));
+ if result.is_ok() {
+ *out_data_holder = data_holder.into_carrier().into_raw();
+ }
RawNativeFunctionResult::from_completion(
result
.map(|()| Value::UNDEFINED)
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/image_bitmap.rs b/Libraries/LibWeb/Bindings/Rust/src/html/image_bitmap.rs
index ce9820aa836..a074a2cdc94 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/image_bitmap.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/image_bitmap.rs
@@ -48,12 +48,12 @@ unsafe extern "C" {
pixels: &FramePixels,
out_frame: &mut *mut c_void,
) -> RawNativeFunctionResult;
- fn web_image_bitmap_transfer_bitmap(
- realm: *mut c_void,
- data_holder: *mut c_void,
+ fn web_image_bitmap_copy_to_shared_pixels(
image_bitmap_pixels: *mut c_void,
frame: *const c_void,
- ) -> RawNativeFunctionResult;
+ buffer_index: &mut u32,
+ offset: &mut u64,
+ ) -> bool;
fn web_image_bitmap_receive_bitmap(
realm: *mut c_void,
data_holder: *mut c_void,
@@ -140,12 +140,8 @@ fn deserialize_enumeration(
.ok_or_else(|| data_clone_error(unknown_message))
}
-/// Writes a copy of the bitmap data of the frame, or that there is none.
-fn serialize_bitmap_data(serialized: &mut SerializationWriter, frame: *const c_void, for_storage: bool) {
- if frame.is_null() {
- serialized.write_u8(BITMAP_KIND_NONE);
- return;
- }
+/// The values of the bitmap and the color space of the frame, with bytes that borrow from it.
+fn frame_pixels(frame: *const c_void) -> FramePixels {
let mut pixels = FramePixels {
width: 0,
height: 0,
@@ -162,6 +158,16 @@ fn serialize_bitmap_data(serialized: &mut SerializationWriter, frame: *const c_v
};
// SAFETY: The frame is live.
unsafe { web_image_bitmap_frame_pixels(frame, &mut pixels) };
+ pixels
+}
+
+/// Writes a copy of the bitmap data of the frame, or that there is none.
+fn serialize_bitmap_data(serialized: &mut SerializationWriter, frame: *const c_void, for_storage: bool) {
+ if frame.is_null() {
+ serialized.write_u8(BITMAP_KIND_NONE);
+ return;
+ }
+ let pixels = frame_pixels(frame);
let has_color_space = pixels.color_space_kind != COLOR_SPACE_KIND_SRGB;
serialized.write_u8(if has_color_space {
@@ -189,6 +195,65 @@ fn serialize_bitmap_data(serialized: &mut SerializationWriter, frame: *const c_v
}
}
+fn write_transferred_color_space(data_holder: &mut TransferDataWriter, pixels: &FramePixels) {
+ data_holder.write(&pixels.color_space_kind);
+ if pixels.color_space_kind == COLOR_SPACE_KIND_CICP {
+ data_holder.write(&pixels.color_primaries);
+ data_holder.write(&pixels.transfer_characteristics);
+ } else if pixels.color_space_kind == COLOR_SPACE_KIND_ICC_PROFILE {
+ // SAFETY: The bytes borrow from the frame, which lives for the call.
+ data_holder.write(unsafe { ak::slice_from_raw_parts(pixels.icc_profile, pixels.icc_profile_size) });
+ }
+}
+
+/// Writes the bitmap data of the frame, or that there is none, to the data holder of a transfer: its pixels go to the
+/// shared memory of the transfer if there is room for them there, and inline otherwise.
+fn write_transferred_bitmap_data(data_holder: &mut TransferDataWriter, frame: *const c_void) {
+ let (mut buffer_index, mut offset) = (0, 0);
+ // SAFETY: The pixels are lent for the steps, and the frame is live.
+ let in_shared_pixels = !frame.is_null()
+ && unsafe {
+ web_image_bitmap_copy_to_shared_pixels(
+ data_holder.image_bitmap_pixels_as_raw(),
+ frame,
+ &mut buffer_index,
+ &mut offset,
+ )
+ };
+ data_holder.write_bool(in_shared_pixels);
+ if frame.is_null() {
+ data_holder.write(&BITMAP_KIND_NONE);
+ return;
+ }
+ let pixels = frame_pixels(frame);
+ if in_shared_pixels {
+ data_holder.write(&buffer_index);
+ data_holder.write(&offset);
+ data_holder.write(&pixels.width);
+ data_holder.write(&pixels.height);
+ data_holder.write(&pixels.format);
+ data_holder.write(&pixels.alpha_type);
+ write_transferred_color_space(data_holder, &pixels);
+ return;
+ }
+ let has_color_space = pixels.color_space_kind != COLOR_SPACE_KIND_SRGB;
+ data_holder.write(&if has_color_space {
+ BITMAP_KIND_WITH_COLOR_SPACE
+ } else {
+ BITMAP_KIND_SRGB
+ });
+ data_holder.write(&pixels.width);
+ data_holder.write(&pixels.height);
+ data_holder.write(&pixels.pitch);
+ data_holder.write(&pixels.format);
+ data_holder.write(&pixels.alpha_type);
+ // SAFETY: The bytes borrow from the frame, which lives for the call.
+ data_holder.write(unsafe { ak::slice_from_raw_parts(pixels.data, pixels.data_size) });
+ if has_color_space {
+ write_transferred_color_space(data_holder, &pixels);
+ }
+}
+
/// Reads what serialize_bitmap_data() writes, and creates the bitmap data in the realm of the call context.
fn deserialize_bitmap_data(cx: &CallContext, serialized: &mut SerializationReader) -> WebIdlResult<Option<BitmapData>> {
let kind = serialized.read_u8()?;
@@ -420,18 +485,7 @@ impl Transferable for ImageBitmap {
}
// 2. Set dataHolder.[[BitmapData]] to value's bitmap data.
- // NB: The pixels go to the shared memory of the transfer if there is room for them there, and inline otherwise.
- // SAFETY: The realm is live, the data holder and the pixels are lent for the steps, and the frame lives while
- // the bitmap data is set.
- unsafe {
- web_image_bitmap_transfer_bitmap(
- data_holder.realm().as_ptr().cast(),
- data_holder.as_raw(),
- data_holder.image_bitmap_pixels_as_raw(),
- this.raw_frame(),
- )
- }
- .into_completion()?;
+ write_transferred_bitmap_data(data_holder, this.raw_frame());
// 3. Unset value's bitmap data.
this.bitmap.replace(None);
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/offscreen_canvas.rs b/Libraries/LibWeb/Bindings/Rust/src/html/offscreen_canvas.rs
index 84de0eb3429..7246109743d 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/offscreen_canvas.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/offscreen_canvas.rs
@@ -21,7 +21,7 @@ unsafe extern "C" {
fn web_offscreen_canvas_transfer_steps(
realm: *mut c_void,
object: *mut c_void,
- data_holder: *mut c_void,
+ fields: &mut TransferredFields,
) -> RawNativeFunctionResult;
fn web_offscreen_canvas_create_from_transfer(
realm: *mut c_void,
@@ -29,6 +29,17 @@ unsafe extern "C" {
) -> RawNativeFunctionResult;
}
+/// What the transfer steps of Libraries/LibWeb/HTML/OffscreenCanvas.cpp set the fields of the data holder to.
+#[repr(C)]
+#[derive(Default)]
+struct TransferredFields {
+ width: u64,
+ height: u64,
+ has_placeholder_canvas: bool,
+ placeholder_canvas_id: u64,
+ placeholder_secret: u64,
+}
+
/// Web::Bindings::InterfaceName of OffscreenCanvas.
pub(crate) fn interface_name() -> u16 {
// SAFETY: The call has no arguments.
@@ -47,15 +58,26 @@ pub(crate) fn is_detached(object: Gc<Object>) -> bool {
/// https://html.spec.whatwg.org/multipage/canvas.html#the-offscreencanvas-interface:transfer-steps
pub(crate) fn transfer_steps(object: Gc<Object>, data_holder: &mut TransferDataWriter) -> WebIdlResult<()> {
- // SAFETY: The realm and the object are live, and the data holder is lent for the steps.
+ let mut fields = TransferredFields::default();
+ // SAFETY: The realm and the object are live, and the call writes to the fields.
unsafe {
- web_offscreen_canvas_transfer_steps(
- data_holder.realm().as_ptr().cast(),
- object.as_ptr().cast(),
- data_holder.as_raw(),
- )
+ web_offscreen_canvas_transfer_steps(data_holder.realm().as_ptr().cast(), object.as_ptr().cast(), &mut fields)
}
.into_completion()?;
+
+ // 6. Set dataHolder.[[Width]] to width and dataHolder.[[Height]] to height.
+ data_holder.write(&fields.width);
+ data_holder.write(&fields.height);
+
+ // FIXME: 7. Set dataHolder.[[Language]] to language and dataHolder.[[Direction]] to direction.
+
+ // 8. Set dataHolder.[[PlaceholderCanvas]] to be a weak reference to value's placeholder canvas element, if value
+ // has one, or null if it does not.
+ data_holder.write_bool(fields.has_placeholder_canvas);
+ if fields.has_placeholder_canvas {
+ data_holder.write(&fields.placeholder_canvas_id);
+ data_holder.write(&fields.placeholder_secret);
+ }
Ok(())
}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/structured_serialize/cpp.rs b/Libraries/LibWeb/Bindings/Rust/src/html/structured_serialize/cpp.rs
index acdd1ee9f18..ee1b3ab1e58 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/structured_serialize/cpp.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/structured_serialize/cpp.rs
@@ -18,6 +18,7 @@ use libjs_rust::api::{ArrayBuffer, ArrayBufferShared, Gc, Object, RawNativeFunct
use crate::bindings::structured_data::{SerializationReader, SerializationWriter};
use crate::bindings::{CallContext, Exception, WebIdlResult};
+use crate::html::structured_serialize::transfer_data_holder::TransferDataHolder;
unsafe extern "C" {
fn web_structured_serialize_set_ipc_record(
@@ -95,9 +96,7 @@ unsafe extern "C" {
fn web_structured_serialize_image_bitmap_pixels_destroy(image_bitmap_pixels: *mut c_void);
fn web_structured_serialize_transfer_data_holder_create() -> *mut c_void;
- fn web_structured_serialize_transfer_data_holder_encode_u8(data_holder: *mut c_void, value: u8);
- fn web_structured_serialize_transfer_data_holder_encode_bytes(data_holder: *mut c_void, bytes: ak::Span<'_, u8>);
- fn web_structured_serialize_transfer_data_holder_encode_size(data_holder: *mut c_void, value: usize);
+ fn web_structured_serialize_transfer_data_holder_append(data_holder: *mut c_void, bytes: ak::Span<'_, u8>);
fn web_structured_serialize_transfer_data_holder_destroy(data_holder: *mut c_void);
fn web_structured_serialize_transfer_record_create() -> *mut c_void;
fn web_structured_serialize_transfer_record_destroy(record: *mut c_void);
@@ -508,40 +507,37 @@ impl Drop for ImageBitmapSharedPixels {
}
}
-/// A data holder of a transfer that this owns: an HTML::TransferDataEncoder.
-pub(crate) struct TransferDataHolder(NonNull<c_void>);
+/// The C++ carrier of a data holder that this owns: an HTML::TransferDataEncoder, which the record of a transfer and
+/// IPC carry, and which C++ encodes the values that have attachments into.
+pub(crate) struct TransferDataCarrier(NonNull<c_void>);
-impl TransferDataHolder {
+impl TransferDataCarrier {
pub(crate) fn new() -> Self {
- // SAFETY: LibWeb returns a new data holder, which this owns.
+ // SAFETY: LibWeb returns a new carrier, which this owns.
Self(
NonNull::new(unsafe { web_structured_serialize_transfer_data_holder_create() }).expect("LibWeb creates it"),
)
}
- pub(crate) fn as_raw(&self) -> NonNull<c_void> {
- self.0
- }
-
- pub(crate) fn encode_u8(&self, value: u8) {
- // SAFETY: This owns the data holder.
- unsafe { web_structured_serialize_transfer_data_holder_encode_u8(self.0.as_ptr(), value) };
+ pub(crate) fn as_raw(&self) -> *mut c_void {
+ self.0.as_ptr()
}
- pub(crate) fn encode_bytes(&self, bytes: &[u8]) {
- // SAFETY: This owns the data holder, and the call borrows the bytes.
- unsafe { web_structured_serialize_transfer_data_holder_encode_bytes(self.0.as_ptr(), ak::Span::new(bytes)) };
+ /// Appends bytes that are already in IPC's encoding.
+ pub(crate) fn append(&self, bytes: &[u8]) {
+ // SAFETY: This owns the carrier, and the call borrows the bytes.
+ unsafe { web_structured_serialize_transfer_data_holder_append(self.0.as_ptr(), ak::Span::new(bytes)) };
}
- pub(crate) fn encode_size(&self, value: usize) {
- // SAFETY: This owns the data holder.
- unsafe { web_structured_serialize_transfer_data_holder_encode_size(self.0.as_ptr(), value) };
+ /// Hands the carrier over to LibWeb's C++ code, which owns it from then on.
+ pub(crate) fn into_raw(self) -> *mut c_void {
+ ManuallyDrop::new(self).0.as_ptr()
}
}
-impl Drop for TransferDataHolder {
+impl Drop for TransferDataCarrier {
fn drop(&mut self) {
- // SAFETY: This owns the data holder.
+ // SAFETY: This owns the carrier.
unsafe { web_structured_serialize_transfer_data_holder_destroy(self.0.as_ptr()) };
}
}
@@ -585,9 +581,13 @@ impl SerializedTransferRecord {
/// Appends the data holder to the record's [[TransferDataHolders]].
pub(crate) fn append_data_holder(self, data_holder: TransferDataHolder) {
- let data_holder = core::mem::ManuallyDrop::new(data_holder);
- // SAFETY: The record is lent, and LibWeb takes the data holder.
- unsafe { web_structured_serialize_transfer_record_append_data_holder(self.0.as_ptr(), data_holder.0.as_ptr()) };
+ // SAFETY: The record is lent, and LibWeb takes the carrier.
+ unsafe {
+ web_structured_serialize_transfer_record_append_data_holder(
+ self.0.as_ptr(),
+ data_holder.into_carrier().into_raw(),
+ );
+ }
}
pub(crate) fn data_holder_count(self) -> usize {
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/structured_serialize/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/html/structured_serialize/mod.rs
index 5fb9f43f2f3..00fcd407441 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/structured_serialize/mod.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/structured_serialize/mod.rs
@@ -17,6 +17,7 @@ pub(crate) mod exports;
pub(crate) mod memory;
pub(crate) mod message_record;
pub(crate) mod reader;
+pub(crate) mod transfer_data_holder;
pub(crate) mod writer;
use ak::Utf16String;
@@ -41,14 +42,15 @@ use crate::html::message_port::MessagePort;
use crate::html::offscreen_canvas;
use crate::html::structured_serialize::cpp::{
OwnedSerializedTransferRecord, SerializableSteps, SerializedTransferRecord, TransferDataDecoder,
- TransferDataHolder, current_settings_object_agent_cluster,
- current_settings_object_cross_origin_isolated_capability, data_clone_error_from_serialization_error,
- deserialize_rust_platform_object, is_exposed, is_platform_object, realm_agent_cluster, serializable_interface_name,
- serializable_interface_storage_identifier, serializable_payload_version, serializable_steps, serialization_steps,
+ current_settings_object_agent_cluster, current_settings_object_cross_origin_isolated_capability,
+ data_clone_error_from_serialization_error, deserialize_rust_platform_object, is_exposed, is_platform_object,
+ realm_agent_cluster, serializable_interface_name, serializable_interface_storage_identifier,
+ serializable_payload_version, serializable_steps, serialization_steps,
};
use crate::html::structured_serialize::memory::{DeserializationMemory, SerializationMemory};
use crate::html::structured_serialize::message_record::MessageRecord;
use crate::html::structured_serialize::reader::StructuredSerializeReader;
+use crate::html::structured_serialize::transfer_data_holder::TransferDataHolder;
use crate::html::structured_serialize::writer::StructuredSerializeWriter;
use crate::streams::readable_stream::ReadableStream;
use crate::streams::transform_stream::TransformStream;
@@ -1773,23 +1775,23 @@ pub(crate) fn structured_serialize_with_transfer(
if !array_buffer.is_fixed_length() {
// 1. Set dataHolder.[[Type]] to "ResizableArrayBuffer".
- data_holder.encode_u8(TransferType::ResizableArrayBuffer as u8);
+ data_holder.encode(&(TransferType::ResizableArrayBuffer as u8));
// 2. Set dataHolder.[[ArrayBufferData]] to transferable.[[ArrayBufferData]].
// 3. Set dataHolder.[[ArrayBufferByteLength]] to transferable.[[ArrayBufferByteLength]].
- data_holder.encode_bytes(&buffer_data);
+ data_holder.encode(buffer_data.as_slice());
// 4. Set dataHolder.[[ArrayBufferMaxByteLength]] to transferable.[[ArrayBufferMaxByteLength]].
- data_holder.encode_size(array_buffer.max_byte_length());
+ data_holder.encode(&array_buffer.max_byte_length());
}
// 2. Otherwise:
else {
// 1. Set dataHolder.[[Type]] to "ArrayBuffer".
- data_holder.encode_u8(TransferType::ArrayBuffer as u8);
+ data_holder.encode(&(TransferType::ArrayBuffer as u8));
// 2. Set dataHolder.[[ArrayBufferData]] to transferable.[[ArrayBufferData]].
// 3. Set dataHolder.[[ArrayBufferByteLength]] to transferable.[[ArrayBufferByteLength]].
- data_holder.encode_bytes(&buffer_data);
+ data_holder.encode(buffer_data.as_slice());
}
// 3. Perform ? DetachArrayBuffer(transferable).
@@ -1806,13 +1808,13 @@ pub(crate) fn structured_serialize_with_transfer(
let interface_name = platform_object.primary_interface();
// 3. Set dataHolder.[[Type]] to interfaceName.
- data_holder.encode_u8(interface_name as u8);
+ data_holder.encode(&(interface_name as u8));
// FIXME: 4. If transferable's primary interface is decorated with [SameAgentCluster], then restrict dataHolder to the same agent cluster.
// 5. Perform the appropriate transfer steps for the interface identified by interfaceName, given transferable and dataHolder.
let mut steps_cx = CallContext::new(vm, cx.realm());
- let mut writer = TransferDataWriter::new(&data_holder, cx.realm(), &image_bitmap_pixels);
+ let mut writer = TransferDataWriter::new(&data_holder, cx.realm(), Some(&image_bitmap_pixels));
platform_object.transfer_steps(&mut steps_cx, &mut writer)?;
// 6. Set transferable.[[Detached]] to true.
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/structured_serialize/transfer_data_holder.rs b/Libraries/LibWeb/Bindings/Rust/src/html/structured_serialize/transfer_data_holder.rs
new file mode 100644
index 00000000000..6ae23a4293d
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/structured_serialize/transfer_data_holder.rs
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! The data holder of a transferred value, in the encoding of IPC, which only IPC carries. Rust writes its bytes. The
+//! values that have attachments, like the end of a transport or the records of messages, C++ encodes into the holder's
+//! carrier, after the bytes before them.
+
+use core::cell::RefCell;
+
+use libipc_rust::{Encode, Encoder};
+
+use crate::html::structured_serialize::cpp::TransferDataCarrier;
+
+pub(crate) struct TransferDataHolder {
+ /// The bytes since the carrier last took them.
+ bytes: RefCell<Encoder>,
+ carrier: TransferDataCarrier,
+}
+
+impl TransferDataHolder {
+ pub(crate) fn new() -> Self {
+ Self {
+ bytes: RefCell::new(Encoder::new()),
+ carrier: TransferDataCarrier::new(),
+ }
+ }
+
+ pub(crate) fn encode<T: Encode + ?Sized>(&self, value: &T) {
+ self.bytes
+ .borrow_mut()
+ .encode(value)
+ .expect("a data holder encodes what it is given");
+ }
+
+ /// The carrier, with the bytes so far, for C++ to encode a value that has attachments into.
+ pub(crate) fn carrier(&self) -> &TransferDataCarrier {
+ let (bytes, attachments) = core::mem::take(&mut *self.bytes.borrow_mut()).finish();
+ debug_assert!(attachments.is_empty(), "C++ encodes the values that have attachments");
+ self.carrier.append(&bytes);
+ &self.carrier
+ }
+
+ /// The carrier, with every byte of the holder, which C++ takes over.
+ pub(crate) fn into_carrier(self) -> TransferDataCarrier {
+ self.carrier();
+ self.carrier
+ }
+}
diff --git a/Libraries/LibWeb/Bindings/RustBindingsServices.cpp b/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
index 14dbfc949f0..a6af33c8b99 100644
--- a/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
+++ b/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
@@ -132,7 +132,6 @@ void web_bindings_with_temporary_execution_context(void* realm, bool callbacks_e
bool web_bindings_object_implements_interface(JSObject* object, u16 interface_name);
JSObject* web_bindings_create_dom_exception(void* realm, ReadonlyBytes name, FlatPtr message);
bool web_bindings_extract_an_origin(JSObject* object, void* ctx, void (*on_origin)(void*, URL::FFI::FfiOrigin const*));
-void web_bindings_transfer_data_encode_bool(HTML::TransferDataEncoder*, bool);
void web_bindings_transfer_data_encode_records(HTML::TransferDataEncoder*, HTML::SerializedTransferRecord const* const*, size_t count);
void web_bindings_transfer_data_encode_transport_handle(HTML::TransferDataEncoder*, IPC::TransportHandle const*);
void web_bindings_transfer_data_extend(HTML::TransferDataEncoder*, HTML::SerializedTransferRecord*);
@@ -317,11 +316,6 @@ bool web_bindings_extract_an_origin(JSObject* object, void* ctx, void (*on_origi
return true;
}
-void web_bindings_transfer_data_encode_bool(HTML::TransferDataEncoder* data_holder, bool value)
-{
- MUST(data_holder->encode(value));
-}
-
// Writes the records as a Vector of them is encoded, which web_bindings_transfer_data_decode_records() and C++ read.
void web_bindings_transfer_data_encode_records(HTML::TransferDataEncoder* data_holder, HTML::SerializedTransferRecord const* const* records, size_t count)
{
diff --git a/Libraries/LibWeb/HTML/ImageBitmap.cpp b/Libraries/LibWeb/HTML/ImageBitmap.cpp
index c87f17d3c88..55c3c261940 100644
--- a/Libraries/LibWeb/HTML/ImageBitmap.cpp
+++ b/Libraries/LibWeb/HTML/ImageBitmap.cpp
@@ -48,39 +48,12 @@ static constexpr size_t shared_pixels_alignment = 64;
return TRY_OR_THROW_OOM(vm, Gfx::Bitmap::create_wrapper(format, alpha_type, Gfx::IntSize(width, height), pitch, pixels, [bitmap_data = move(bitmap_data)] { }));
}
-template<typename T>
-static WebIDL::ExceptionOr<void> encode_bitmap_value(HTML::TransferDataEncoder& encoder, T const& value)
-{
- if (encoder.encode(value).is_error())
- return WebIDL::DataCloneError::create("Unable to transfer ImageBitmap"_utf16);
- return {};
-}
-
enum class SerializedColorSpaceKind : u8 {
SRGB,
CICP,
ICCProfile,
};
-static WebIDL::ExceptionOr<void> serialize_color_space(HTML::TransferDataEncoder& encoder, Gfx::ColorSpace const& color_space)
-{
- return color_space.data().visit(
- [&](Empty) -> WebIDL::ExceptionOr<void> {
- return encode_bitmap_value(encoder, to_underlying(SerializedColorSpaceKind::SRGB));
- },
- [&](Gfx::ColorSpace::CICP const& cicp) -> WebIDL::ExceptionOr<void> {
- TRY(encode_bitmap_value(encoder, to_underlying(SerializedColorSpaceKind::CICP)));
- TRY(encode_bitmap_value(encoder, to_underlying(cicp.color_primaries)));
- TRY(encode_bitmap_value(encoder, to_underlying(cicp.transfer_characteristics)));
- return {};
- },
- [&](NonnullRefPtr<Gfx::ColorSpace::ICCProfile const> const& icc_profile) -> WebIDL::ExceptionOr<void> {
- TRY(encode_bitmap_value(encoder, to_underlying(SerializedColorSpaceKind::ICCProfile)));
- TRY(encode_bitmap_value(encoder, icc_profile->bytes()));
- return {};
- });
-}
-
[[nodiscard]] static ErrorOr<Gfx::ColorSpace> deserialize_color_space(HTML::TransferDataDecoder& decoder)
{
auto kind = TRY(decoder.decode<u8>());
@@ -108,25 +81,6 @@ enum class SerializedBitmapKind : u8 {
WithColorSpace,
};
-static WebIDL::ExceptionOr<void> serialize_bitmap(HTML::TransferDataEncoder& encoder, Gfx::DecodedImageFrame const* frame)
-{
- if (!frame)
- return encode_bitmap_value(encoder, to_underlying(SerializedBitmapKind::None));
-
- auto const& bitmap = frame->bitmap();
- auto const has_color_space = !frame->color_space().data().has<Empty>();
- TRY(encode_bitmap_value(encoder, to_underlying(has_color_space ? SerializedBitmapKind::WithColorSpace : SerializedBitmapKind::SRGB)));
- TRY(encode_bitmap_value(encoder, bitmap.width()));
- TRY(encode_bitmap_value(encoder, bitmap.height()));
- TRY(encode_bitmap_value(encoder, static_cast<u64>(bitmap.pitch())));
- TRY(encode_bitmap_value(encoder, bitmap.format()));
- TRY(encode_bitmap_value(encoder, bitmap.alpha_type()));
- TRY(encode_bitmap_value(encoder, ReadonlyBytes { bitmap.scanline_u8(0), bitmap.data_size() }));
- if (has_color_space)
- TRY(serialize_color_space(encoder, frame->color_space()));
- return {};
-}
-
[[nodiscard]] static WebIDL::ExceptionOr<Optional<Gfx::DecodedImageFrame>> deserialize_bitmap(JS::Realm& realm, HTML::TransferDataDecoder& decoder)
{
auto const kind = TRY(decode_or_throw_data_clone_error<u8>(realm, decoder));
@@ -216,34 +170,6 @@ template<typename T>
return TRY_OR_THROW_OOM(vm, Gfx::Bitmap::create_wrapper(format, alpha_type, size, pitch, pixels, [buffer = move(buffer)] { }));
}
-// 2. Set dataHolder.[[BitmapData]] to value's bitmap data, of
-// https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#the-imagebitmap-interface:transfer-steps
-static WebIDL::ExceptionOr<void> transfer_bitmap(HTML::TransferDataEncoder& data_holder, ImageBitmapSharedPixels* shared_pixels, Gfx::DecodedImageFrame const* frame)
-{
- auto const* bitmap = frame ? &frame->bitmap() : nullptr;
- // NB: The pixels go to the shared memory if there is room for them there, and inline otherwise.
- if (shared_pixels && bitmap && ImageBitmap::shared_pixels_size(*bitmap) <= shared_pixels->buffer.size() - shared_pixels->used_size) {
- auto const pitch = Gfx::Bitmap::minimum_pitch(bitmap->width(), bitmap->format());
- auto* pixels = shared_pixels->buffer.data<u8>() + shared_pixels->used_size;
- for (int y = 0; y < bitmap->height(); ++y)
- memcpy(pixels + y * pitch, bitmap->scanline_u8(y), pitch);
-
- TRY(encode_bitmap_value(data_holder, true));
- TRY(encode_bitmap_value(data_holder, shared_pixels->buffer_index));
- TRY(encode_bitmap_value(data_holder, static_cast<u64>(shared_pixels->used_size)));
- TRY(encode_bitmap_value(data_holder, bitmap->width()));
- TRY(encode_bitmap_value(data_holder, bitmap->height()));
- TRY(encode_bitmap_value(data_holder, bitmap->format()));
- TRY(encode_bitmap_value(data_holder, bitmap->alpha_type()));
- TRY(serialize_color_space(data_holder, frame->color_space()));
- shared_pixels->used_size += ImageBitmap::shared_pixels_size(*bitmap);
- } else {
- TRY(encode_bitmap_value(data_holder, false));
- TRY(serialize_bitmap(data_holder, frame));
- }
- return {};
-}
-
// 1. Set value's bitmap data to dataHolder.[[BitmapData]], of
// https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#the-imagebitmap-interface:transfer-receiving-steps
static WebIDL::ExceptionOr<Optional<Gfx::DecodedImageFrame>> receive_bitmap(JS::Realm& realm, HTML::TransferDataDecoder& data_holder)
@@ -306,7 +232,7 @@ struct FramePixels {
size_t web_image_bitmap_frame_size_in_bytes(Gfx::DecodedImageFrame const*);
void web_image_bitmap_frame_pixels(Gfx::DecodedImageFrame const*, FramePixels*);
JSCompletion web_image_bitmap_create_frame(void* realm, FramePixels const*, Gfx::DecodedImageFrame** out_frame);
-JSCompletion web_image_bitmap_transfer_bitmap(void* realm, HTML::TransferDataEncoder*, ImageBitmapSharedPixels*, Gfx::DecodedImageFrame const*);
+bool web_image_bitmap_copy_to_shared_pixels(ImageBitmapSharedPixels*, Gfx::DecodedImageFrame const*, u32* buffer_index, u64* offset);
JSCompletion web_image_bitmap_receive_bitmap(void* realm, HTML::TransferDataDecoder*, Gfx::DecodedImageFrame** out_frame);
size_t web_image_bitmap_frame_size_in_bytes(Gfx::DecodedImageFrame const* frame)
@@ -386,13 +312,21 @@ JSCompletion web_image_bitmap_create_frame(void* realm, FramePixels const* pixel
return Bindings::completion_to_abi(js_realm, JS::js_undefined());
}
-JSCompletion web_image_bitmap_transfer_bitmap(void* realm, HTML::TransferDataEncoder* data_holder, ImageBitmapSharedPixels* image_bitmap_pixels, Gfx::DecodedImageFrame const* frame)
+// AD-HOC: Copies the pixels of the frame to the shared memory of a transfer, if there is room for them there, and
+// returns where they are.
+bool web_image_bitmap_copy_to_shared_pixels(ImageBitmapSharedPixels* shared_pixels, Gfx::DecodedImageFrame const* frame, u32* buffer_index, u64* offset)
{
- auto& js_realm = *static_cast<JS::Realm*>(realm);
- auto result = transfer_bitmap(*data_holder, image_bitmap_pixels, frame);
- if (result.is_exception())
- return Bindings::completion_to_abi(js_realm, result.release_error());
- return Bindings::completion_to_abi(js_realm, JS::js_undefined());
+ auto const& bitmap = frame->bitmap();
+ if (!shared_pixels || ImageBitmap::shared_pixels_size(bitmap) > shared_pixels->buffer.size() - shared_pixels->used_size)
+ return false;
+ auto const pitch = Gfx::Bitmap::minimum_pitch(bitmap.width(), bitmap.format());
+ auto* pixels = shared_pixels->buffer.data<u8>() + shared_pixels->used_size;
+ for (int y = 0; y < bitmap.height(); ++y)
+ memcpy(pixels + y * pitch, bitmap.scanline_u8(y), pitch);
+ *buffer_index = shared_pixels->buffer_index;
+ *offset = shared_pixels->used_size;
+ shared_pixels->used_size += ImageBitmap::shared_pixels_size(bitmap);
+ return true;
}
JSCompletion web_image_bitmap_receive_bitmap(void* realm, HTML::TransferDataDecoder* data_holder, Gfx::DecodedImageFrame** out_frame)
diff --git a/Libraries/LibWeb/HTML/MessagePort.cpp b/Libraries/LibWeb/HTML/MessagePort.cpp
index 9ae733b7b5a..0dae7b265b3 100644
--- a/Libraries/LibWeb/HTML/MessagePort.cpp
+++ b/Libraries/LibWeb/HTML/MessagePort.cpp
@@ -29,7 +29,7 @@ void web_rust_message_port_discard_pending_messages(void* port);
size_t web_rust_message_port_pending_outgoing_message_count(void* port);
void web_rust_message_port_fail_next_transfer_for_testing(void* port);
void web_rust_message_port_disentangle_ports_of_realm(JSVM*, void* realm);
-JSCompletion web_rust_message_port_transfer_steps(JSVM*, void* realm, void* port, TransferDataEncoder*);
+JSCompletion web_rust_message_port_transfer_steps(JSVM*, void* realm, void* port, TransferDataEncoder** out_data_holder);
JSCompletion web_rust_message_port_transfer_receiving_steps(JSVM*, void* realm, TransferDataDecoder*);
}
@@ -94,10 +94,13 @@ void MessagePort::discard_pending_messages()
web_rust_message_port_discard_pending_messages(this);
}
-WebIDL::ExceptionOr<void> MessagePort::transfer_steps(JS::Realm& realm, TransferDataEncoder& data_holder)
+WebIDL::ExceptionOr<TransferDataEncoder> MessagePort::transfer_steps(JS::Realm& realm)
{
+ TransferDataEncoder* data_holder = nullptr;
TRY(JS::HostABI::completion_from_abi<void>(web_rust_message_port_transfer_steps(vm_abi(), &realm, this, &data_holder)));
- return {};
+ auto result = move(*data_holder);
+ delete data_holder;
+ return result;
}
size_t MessagePort::pending_outgoing_message_count() const
diff --git a/Libraries/LibWeb/HTML/MessagePort.h b/Libraries/LibWeb/HTML/MessagePort.h
index b57f85dd218..9b5e18df219 100644
--- a/Libraries/LibWeb/HTML/MessagePort.h
+++ b/Libraries/LibWeb/HTML/MessagePort.h
@@ -49,7 +49,7 @@ public:
void discard_pending_messages();
// https://html.spec.whatwg.org/multipage/web-messaging.html#message-ports:transfer-steps
- WebIDL::ExceptionOr<void> transfer_steps(JS::Realm&, TransferDataEncoder&);
+ WebIDL::ExceptionOr<TransferDataEncoder> transfer_steps(JS::Realm&);
size_t pending_outgoing_message_count() const;
void fail_next_transfer_for_testing();
diff --git a/Libraries/LibWeb/HTML/OffscreenCanvas.cpp b/Libraries/LibWeb/HTML/OffscreenCanvas.cpp
index 45accd931a7..04fd74a6234 100644
--- a/Libraries/LibWeb/HTML/OffscreenCanvas.cpp
+++ b/Libraries/LibWeb/HTML/OffscreenCanvas.cpp
@@ -91,7 +91,7 @@ JS::Object& OffscreenCanvas::relevant_global_object() const
}
// https://html.spec.whatwg.org/multipage/canvas.html#the-offscreencanvas-interface:transfer-steps
-WebIDL::ExceptionOr<void> OffscreenCanvas::transfer_steps(JS::Realm& realm, HTML::TransferDataEncoder& data_holder)
+WebIDL::ExceptionOr<OffscreenCanvas::TransferredFields> OffscreenCanvas::transfer_steps()
{
// 1. If value's context mode is not equal to none, then throw an "InvalidStateError" DOMException.
if (!m_context.has<Empty>())
@@ -113,22 +113,13 @@ WebIDL::ExceptionOr<void> OffscreenCanvas::transfer_steps(JS::Realm& realm, HTML
m_height = 0;
// 6. Set dataHolder.[[Width]] to width and dataHolder.[[Height]] to height.
- TRY(encode_or_throw_data_clone_error(realm, data_holder, width));
- TRY(encode_or_throw_data_clone_error(realm, data_holder, height));
-
// FIXME: 7. Set dataHolder.[[Language]] to language and dataHolder.[[Direction]] to direction.
-
// 8. Set dataHolder.[[PlaceholderCanvas]] to be a weak reference to value's placeholder canvas element, if value
// has one, or null if it does not.
+ // NB: The transfer in Libraries/LibWeb/Bindings/Rust/src/html/offscreen_canvas.rs writes these fields.
auto placeholder_link = m_placeholder_link;
m_placeholder_link.clear();
- TRY(encode_or_throw_data_clone_error(realm, data_holder, placeholder_link.has_value()));
- if (placeholder_link.has_value()) {
- TRY(encode_or_throw_data_clone_error(realm, data_holder, placeholder_link->canvas_id.value()));
- TRY(encode_or_throw_data_clone_error(realm, data_holder, placeholder_link->secret));
- }
-
- return {};
+ return TransferredFields { .width = width, .height = height, .placeholder_link = placeholder_link };
}
// https://html.spec.whatwg.org/multipage/canvas.html#the-offscreencanvas-interface:transfer-receiving-steps
@@ -517,7 +508,16 @@ extern "C" {
u16 web_offscreen_canvas_interface_name();
bool web_offscreen_canvas_is_detached(JSObject*);
-JSCompletion web_offscreen_canvas_transfer_steps(void* realm, JSObject*, Web::HTML::TransferDataEncoder*);
+// The fields that the transfer steps set the data holder's fields to.
+struct OffscreenCanvasTransferredFields {
+ u64 width;
+ u64 height;
+ bool has_placeholder_canvas;
+ u64 placeholder_canvas_id;
+ u64 placeholder_secret;
+};
+
+JSCompletion web_offscreen_canvas_transfer_steps(void* realm, JSObject*, OffscreenCanvasTransferredFields*);
JSCompletion web_offscreen_canvas_create_from_transfer(void* realm, Web::HTML::TransferDataDecoder*);
u16 web_offscreen_canvas_interface_name()
@@ -530,13 +530,21 @@ bool web_offscreen_canvas_is_detached(JSObject* object)
return Web::Bindings::impl_from<Web::HTML::OffscreenCanvas>(JS::HostABI::object_from_abi(object))->is_detached();
}
-JSCompletion web_offscreen_canvas_transfer_steps(void* realm, JSObject* object, Web::HTML::TransferDataEncoder* data_holder)
+JSCompletion web_offscreen_canvas_transfer_steps(void* realm, JSObject* object, OffscreenCanvasTransferredFields* fields)
{
auto& js_realm = *static_cast<JS::Realm*>(realm);
auto* offscreen_canvas = Web::Bindings::impl_from<Web::HTML::OffscreenCanvas>(JS::HostABI::object_from_abi(object));
- auto result = offscreen_canvas->transfer_steps(js_realm, *data_holder);
+ auto result = offscreen_canvas->transfer_steps();
if (result.is_exception())
return Web::Bindings::completion_to_abi(js_realm, result.release_error());
+ auto transferred = result.release_value();
+ *fields = {
+ .width = transferred.width,
+ .height = transferred.height,
+ .has_placeholder_canvas = transferred.placeholder_link.has_value(),
+ .placeholder_canvas_id = transferred.placeholder_link.has_value() ? transferred.placeholder_link->canvas_id.value() : 0,
+ .placeholder_secret = transferred.placeholder_link.has_value() ? transferred.placeholder_link->secret : 0,
+ };
return Web::Bindings::completion_to_abi(js_realm, JS::js_undefined());
}
diff --git a/Libraries/LibWeb/HTML/OffscreenCanvas.h b/Libraries/LibWeb/HTML/OffscreenCanvas.h
index 050ae448e98..2f7ad04fca3 100644
--- a/Libraries/LibWeb/HTML/OffscreenCanvas.h
+++ b/Libraries/LibWeb/HTML/OffscreenCanvas.h
@@ -41,7 +41,13 @@ public:
virtual GC::Ptr<Bindings::Wrappable> relevant_global_impl() const override { return m_global_object; }
- WebIDL::ExceptionOr<void> transfer_steps(JS::Realm&, HTML::TransferDataEncoder&);
+ struct TransferredFields {
+ WebIDL::UnsignedLongLong width { 0 };
+ WebIDL::UnsignedLongLong height { 0 };
+ Optional<Compositor::PlaceholderCanvasLink> placeholder_link;
+ };
+
+ WebIDL::ExceptionOr<TransferredFields> transfer_steps();
WebIDL::ExceptionOr<void> transfer_receiving_steps(JS::Realm&, HTML::TransferDataDecoder&);
bool is_detached() const { return m_detached; }
diff --git a/Libraries/LibWeb/HTML/StructuredSerializeRustIntegration.cpp b/Libraries/LibWeb/HTML/StructuredSerializeRustIntegration.cpp
index 5ed7efddbe6..ddfda6c0fbd 100644
--- a/Libraries/LibWeb/HTML/StructuredSerializeRustIntegration.cpp
+++ b/Libraries/LibWeb/HTML/StructuredSerializeRustIntegration.cpp
@@ -73,9 +73,7 @@ ImageBitmapSharedPixels* web_structured_serialize_gather_image_bitmap_pixels(Vec
void web_structured_serialize_image_bitmap_pixels_destroy(ImageBitmapSharedPixels*);
TransferDataEncoder* web_structured_serialize_transfer_data_holder_create();
-void web_structured_serialize_transfer_data_holder_encode_u8(TransferDataEncoder*, u8);
-void web_structured_serialize_transfer_data_holder_encode_bytes(TransferDataEncoder*, ReadonlyBytes);
-void web_structured_serialize_transfer_data_holder_encode_size(TransferDataEncoder*, size_t);
+void web_structured_serialize_transfer_data_holder_append(TransferDataEncoder*, ReadonlyBytes);
void web_structured_serialize_transfer_data_holder_destroy(TransferDataEncoder*);
SerializedTransferRecord* web_structured_serialize_transfer_record_create();
void web_structured_serialize_transfer_record_destroy(SerializedTransferRecord*);
@@ -338,19 +336,9 @@ TransferDataEncoder* web_structured_serialize_transfer_data_holder_create()
return new TransferDataEncoder;
}
-void web_structured_serialize_transfer_data_holder_encode_u8(TransferDataEncoder* data_holder, u8 value)
+void web_structured_serialize_transfer_data_holder_append(TransferDataEncoder* data_holder, ReadonlyBytes bytes)
{
- MUST(data_holder->encode(value));
-}
-
-void web_structured_serialize_transfer_data_holder_encode_bytes(TransferDataEncoder* data_holder, ReadonlyBytes bytes)
-{
- MUST(data_holder->encode(bytes));
-}
-
-void web_structured_serialize_transfer_data_holder_encode_size(TransferDataEncoder* data_holder, size_t value)
-{
- MUST(data_holder->encode(value));
+ MUST(data_holder->append(bytes));
}
void web_structured_serialize_transfer_data_holder_destroy(TransferDataEncoder* data_holder)
diff --git a/Libraries/LibWeb/HTML/WorkerAgentParent.cpp b/Libraries/LibWeb/HTML/WorkerAgentParent.cpp
index 7e3201dd28c..a590ce664f5 100644
--- a/Libraries/LibWeb/HTML/WorkerAgentParent.cpp
+++ b/Libraries/LibWeb/HTML/WorkerAgentParent.cpp
@@ -83,8 +83,7 @@ void WorkerAgentParent::start()
m_message_port = MessagePort::create(realm);
m_message_port->entangle_with(*m_outside_port);
- TransferDataEncoder data_holder;
- MUST(m_message_port->transfer_steps(realm, data_holder));
+ auto data_holder = MUST(m_message_port->transfer_steps(realm));
// FIXME: Specification says this supposed to happen in step 11 of onComplete handler defined in https://html.spec.whatwg.org/multipage/workers.html#run-a-worker
// but that would require introducing a new IPC message type to communicate this from WebWorker to WebContent process,
diff --git a/Libraries/LibWebCommon/HTML/SerializationRecords.h b/Libraries/LibWebCommon/HTML/SerializationRecords.h
index 5c3693716b8..e600217e7b8 100644
--- a/Libraries/LibWebCommon/HTML/SerializationRecords.h
+++ b/Libraries/LibWebCommon/HTML/SerializationRecords.h
@@ -134,6 +134,13 @@ public:
return m_encoder.encode_size(size);
}
+ // Bytes that are already in the encoding, which the Rust data holders write.
+ ErrorOr<void> append(ReadonlyBytes bytes)
+ {
+ VERIFY(!m_buffer_has_been_taken);
+ return m_encoder.append(bytes.data(), bytes.size());
+ }
+
void extend(Vector<TransferDataEncoder>);
IPC::MessageBuffer const& buffer() const;
diff --git a/Meta/Linters/check_libweb_realm_mentions.py b/Meta/Linters/check_libweb_realm_mentions.py
index 02b9a4b8d0d..e2c99ca5189 100755
--- a/Meta/Linters/check_libweb_realm_mentions.py
+++ b/Meta/Linters/check_libweb_realm_mentions.py
@@ -61,7 +61,7 @@ ALLOWED_REALM_MENTIONS = {
"FileAPI": (2, 9, "Blob glue still creates streams and buffers in selected realms"),
"HTML": (
64,
- 185,
+ 182,
"HTML algorithms still contain structured serialization, including posted-message preparation, canvas, navigation transfer/reconstruction, worker/worklet, and event realm use, and cross-origin Window/Location property functions are created in the current realm; RemoteWindow mirrors Window's postMessage; "
"the C++ glue of the Rust structured serialization creates platform objects and SharedArrayBuffers in a given realm; "
"the C++ glue of the Rust Storage and StorageEvent creates them in a given realm",