imagebitmap: Make ImageBitmap serializable and transferable (#37101)

According to specification ImageBitmap objects are serializable objects
and transferable objects.

https://html.spec.whatwg.org/multipage/#the-imagebitmap-interface:imagebitmap-11

Testing:
 - html/canvas/element/manual/imagebitmap/*
 - html/infrastructure/safe-passing-of-structured-data/*
 - html/webappapis/structured-clone/*
 - workers/semantics/structured-clone/*

Signed-off-by: Andrei Volykhin <andrei.volykhin@gmail.com>
This commit is contained in:
Andrei Volykhin 2025-05-23 23:40:25 +03:00 committed by GitHub
parent cd36c35cf2
commit 1f5087d773
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 215 additions and 103 deletions

View file

@ -10,11 +10,13 @@ use std::os::raw;
use std::ptr;
use base::id::{
BlobId, DomExceptionId, DomPointId, Index, MessagePortId, NamespaceIndex, PipelineNamespaceId,
BlobId, DomExceptionId, DomPointId, ImageBitmapId, Index, MessagePortId, NamespaceIndex,
PipelineNamespaceId,
};
use constellation_traits::{
BlobImpl, DomException, DomPoint, MessagePortImpl, Serializable as SerializableInterface,
StructuredSerializedData, Transferrable as TransferrableInterface, TransformStreamData,
SerializableImageBitmap, StructuredSerializedData, Transferrable as TransferrableInterface,
TransformStreamData,
};
use js::gc::RootedVec;
use js::glue::{
@ -42,6 +44,7 @@ use crate::dom::blob::Blob;
use crate::dom::dompoint::DOMPoint;
use crate::dom::dompointreadonly::DOMPointReadOnly;
use crate::dom::globalscope::GlobalScope;
use crate::dom::imagebitmap::ImageBitmap;
use crate::dom::messageport::MessagePort;
use crate::dom::readablestream::ReadableStream;
use crate::dom::types::{DOMException, TransformStream};
@ -66,6 +69,7 @@ pub(super) enum StructuredCloneTags {
DomException = 0xFFFF8007,
WritableStream = 0xFFFF8008,
TransformStream = 0xFFFF8009,
ImageBitmap = 0xFFFF800A,
Max = 0xFFFFFFFF,
}
@ -76,6 +80,7 @@ impl From<SerializableInterface> for StructuredCloneTags {
SerializableInterface::DomPointReadOnly => StructuredCloneTags::DomPointReadOnly,
SerializableInterface::DomPoint => StructuredCloneTags::DomPoint,
SerializableInterface::DomException => StructuredCloneTags::DomException,
SerializableInterface::ImageBitmap => StructuredCloneTags::ImageBitmap,
}
}
}
@ -83,6 +88,7 @@ impl From<SerializableInterface> for StructuredCloneTags {
impl From<TransferrableInterface> for StructuredCloneTags {
fn from(v: TransferrableInterface) -> Self {
match v {
TransferrableInterface::ImageBitmap => StructuredCloneTags::ImageBitmap,
TransferrableInterface::MessagePort => StructuredCloneTags::MessagePort,
TransferrableInterface::ReadableStream => StructuredCloneTags::ReadableStream,
TransferrableInterface::WritableStream => StructuredCloneTags::WritableStream,
@ -104,6 +110,7 @@ fn reader_for_type(
SerializableInterface::DomPointReadOnly => read_object::<DOMPointReadOnly>,
SerializableInterface::DomPoint => read_object::<DOMPoint>,
SerializableInterface::DomException => read_object::<DOMException>,
SerializableInterface::ImageBitmap => read_object::<ImageBitmap>,
}
}
@ -237,6 +244,7 @@ fn serialize_for_type(val: SerializableInterface) -> SerializeOperation {
SerializableInterface::DomPointReadOnly => try_serialize::<DOMPointReadOnly>,
SerializableInterface::DomPoint => try_serialize::<DOMPoint>,
SerializableInterface::DomException => try_serialize::<DOMException>,
SerializableInterface::ImageBitmap => try_serialize::<ImageBitmap>,
}
}
@ -264,6 +272,7 @@ fn receiver_for_type(
) -> fn(&GlobalScope, &mut StructuredDataReader<'_>, u64, RawMutableHandleObject) -> Result<(), ()>
{
match val {
TransferrableInterface::ImageBitmap => receive_object::<ImageBitmap>,
TransferrableInterface::MessagePort => receive_object::<MessagePort>,
TransferrableInterface::ReadableStream => receive_object::<ReadableStream>,
TransferrableInterface::WritableStream => receive_object::<WritableStream>,
@ -390,6 +399,7 @@ type TransferOperation = unsafe fn(
fn transfer_for_type(val: TransferrableInterface) -> TransferOperation {
match val {
TransferrableInterface::ImageBitmap => try_transfer::<ImageBitmap>,
TransferrableInterface::MessagePort => try_transfer::<MessagePort>,
TransferrableInterface::ReadableStream => try_transfer::<ReadableStream>,
TransferrableInterface::WritableStream => try_transfer::<WritableStream>,
@ -439,6 +449,7 @@ unsafe fn can_transfer_for_type(
root_from_object::<T>(*obj, cx).map(|o| Transferable::can_transfer(&*o))
}
match transferable {
TransferrableInterface::ImageBitmap => can_transfer::<ImageBitmap>(obj, cx),
TransferrableInterface::MessagePort => can_transfer::<MessagePort>(obj, cx),
TransferrableInterface::ReadableStream => can_transfer::<ReadableStream>(obj, cx),
TransferrableInterface::WritableStream => can_transfer::<WritableStream>(obj, cx),
@ -527,6 +538,10 @@ pub(crate) struct StructuredDataReader<'a> {
pub(crate) points: Option<HashMap<DomPointId, DomPoint>>,
/// A map of serialized exceptions.
pub(crate) exceptions: Option<HashMap<DomExceptionId, DomException>>,
// A map of serialized image bitmaps.
pub(crate) image_bitmaps: Option<HashMap<ImageBitmapId, SerializableImageBitmap>>,
/// A map of transferred image bitmaps.
pub(crate) transferred_image_bitmaps: Option<HashMap<ImageBitmapId, SerializableImageBitmap>>,
}
/// A data holder for transferred and serialized objects.
@ -545,6 +560,10 @@ pub(crate) struct StructuredDataWriter {
pub(crate) exceptions: Option<HashMap<DomExceptionId, DomException>>,
/// Serialized blobs.
pub(crate) blobs: Option<HashMap<BlobId, BlobImpl>>,
/// Serialized image bitmaps.
pub(crate) image_bitmaps: Option<HashMap<ImageBitmapId, SerializableImageBitmap>>,
/// Transferred image bitmaps.
pub(crate) transferred_image_bitmaps: Option<HashMap<ImageBitmapId, SerializableImageBitmap>>,
}
/// Writes a structured clone. Returns a `DataClone` error if that fails.
@ -599,6 +618,8 @@ pub(crate) fn write(
points: sc_writer.points.take(),
exceptions: sc_writer.exceptions.take(),
blobs: sc_writer.blobs.take(),
image_bitmaps: sc_writer.image_bitmaps.take(),
transferred_image_bitmaps: sc_writer.transferred_image_bitmaps.take(),
};
Ok(data)
@ -623,6 +644,8 @@ pub(crate) fn read(
points: data.points.take(),
exceptions: data.exceptions.take(),
errors: DOMErrorRecord { message: None },
image_bitmaps: data.image_bitmaps.take(),
transferred_image_bitmaps: data.transferred_image_bitmaps.take(),
};
let sc_reader_ptr = &mut sc_reader as *mut _;
unsafe {

View file

@ -3,8 +3,11 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::cell::Cell;
use std::collections::HashMap;
use std::vec::Vec;
use base::id::{ImageBitmapId, ImageBitmapIndex};
use constellation_traits::SerializableImageBitmap;
use dom_struct::dom_struct;
use crate::dom::bindings::cell::DomRefCell;
@ -12,6 +15,9 @@ use crate::dom::bindings::codegen::Bindings::ImageBitmapBinding::ImageBitmapMeth
use crate::dom::bindings::error::Fallible;
use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::serializable::Serializable;
use crate::dom::bindings::structuredclone::StructuredData;
use crate::dom::bindings::transferable::Transferable;
use crate::dom::globalscope::GlobalScope;
use crate::script_runtime::CanGc;
@ -29,11 +35,11 @@ pub(crate) struct ImageBitmap {
}
impl ImageBitmap {
fn new_inherited(width_arg: u32, height_arg: u32) -> ImageBitmap {
fn new_inherited(width: u32, height: u32) -> ImageBitmap {
ImageBitmap {
reflector_: Reflector::new(),
width: width_arg,
height: height_arg,
width,
height,
bitmap_data: DomRefCell::new(Some(vec![])),
origin_clean: Cell::new(true),
}
@ -56,6 +62,10 @@ impl ImageBitmap {
*self.bitmap_data.borrow_mut() = Some(data);
}
pub(crate) fn origin_is_clean(&self) -> bool {
self.origin_clean.get()
}
pub(crate) fn set_origin_clean(&self, origin_is_clean: bool) {
self.origin_clean.set(origin_is_clean);
}
@ -67,6 +77,118 @@ impl ImageBitmap {
}
}
impl Serializable for ImageBitmap {
type Index = ImageBitmapIndex;
type Data = SerializableImageBitmap;
/// <https://html.spec.whatwg.org/multipage/#the-imagebitmap-interface:serialization-steps>
fn serialize(&self) -> Result<(ImageBitmapId, Self::Data), ()> {
// Step 1. If value's origin-clean flag is not set, then throw a "DataCloneError" DOMException.
if !self.origin_is_clean() {
return Err(());
}
// If value has a [[Detached]] internal slot whose value is true,
// then throw a "DataCloneError" DOMException.
if self.is_detached() {
return Err(());
}
// Step 2. Set serialized.[[BitmapData]] to a copy of value's bitmap data.
let serialized = SerializableImageBitmap {
width: self.width,
height: self.height,
bitmap_data: self.bitmap_data.borrow().clone().unwrap(),
};
Ok((ImageBitmapId::new(), serialized))
}
/// <https://html.spec.whatwg.org/multipage/#the-imagebitmap-interface:deserialization-steps>
fn deserialize(
owner: &GlobalScope,
serialized: Self::Data,
can_gc: CanGc,
) -> Result<DomRoot<Self>, ()> {
let image_bitmap =
ImageBitmap::new(owner, serialized.width, serialized.height, can_gc).unwrap();
// Step 1. Set value's bitmap data to serialized.[[BitmapData]].
image_bitmap.set_bitmap_data(serialized.bitmap_data);
Ok(image_bitmap)
}
fn serialized_storage<'a>(
reader: StructuredData<'a, '_>,
) -> &'a mut Option<HashMap<ImageBitmapId, Self::Data>> {
match reader {
StructuredData::Reader(r) => &mut r.image_bitmaps,
StructuredData::Writer(w) => &mut w.image_bitmaps,
}
}
}
impl Transferable for ImageBitmap {
type Index = ImageBitmapIndex;
type Data = SerializableImageBitmap;
fn can_transfer(&self) -> bool {
if !self.origin_is_clean() || self.is_detached() {
return false;
}
true
}
/// <https://html.spec.whatwg.org/multipage/#the-imagebitmap-interface:transfer-steps>
fn transfer(&self) -> Result<(ImageBitmapId, SerializableImageBitmap), ()> {
// Step 1. If value's origin-clean flag is not set, then throw a "DataCloneError" DOMException.
if !self.origin_is_clean() {
return Err(());
}
// If value has a [[Detached]] internal slot whose value is true,
// then throw a "DataCloneError" DOMException.
if self.is_detached() {
return Err(());
}
// Step 2. Set dataHolder.[[BitmapData]] to value's bitmap data.
// Step 3. Unset value's bitmap data.
let serialized = SerializableImageBitmap {
width: self.width,
height: self.height,
bitmap_data: self.bitmap_data.borrow_mut().take().unwrap(),
};
Ok((ImageBitmapId::new(), serialized))
}
/// <https://html.spec.whatwg.org/multipage/#the-imagebitmap-interface:transfer-receiving-steps>
fn transfer_receive(
owner: &GlobalScope,
_: ImageBitmapId,
serialized: SerializableImageBitmap,
) -> Result<DomRoot<Self>, ()> {
let image_bitmap =
ImageBitmap::new(owner, serialized.width, serialized.height, CanGc::note()).unwrap();
// Step 1. Set value's bitmap data to serialized.[[BitmapData]].
image_bitmap.set_bitmap_data(serialized.bitmap_data);
Ok(image_bitmap)
}
fn serialized_storage<'a>(
data: StructuredData<'a, '_>,
) -> &'a mut Option<HashMap<ImageBitmapId, Self::Data>> {
match data {
StructuredData::Reader(r) => &mut r.transferred_image_bitmaps,
StructuredData::Writer(w) => &mut w.transferred_image_bitmaps,
}
}
}
impl ImageBitmapMethods<crate::DomTypeHolder> for ImageBitmap {
/// <https://html.spec.whatwg.org/multipage/#dom-imagebitmap-height>
fn Height(&self) -> u32 {

View file

@ -9,8 +9,7 @@
* You are granted a license to use, reproduce and create derivative works of this document.
*/
//[Exposed=(Window,Worker), Serializable, Transferable]
[Exposed=(Window,Worker), Pref="dom_imagebitmap_enabled"]
[Exposed=(Window,Worker), Serializable, Transferable, Pref="dom_imagebitmap_enabled"]
interface ImageBitmap {
readonly attribute unsigned long width;
readonly attribute unsigned long height;

View file

@ -371,6 +371,8 @@ namespace_id! {DomExceptionId, DomExceptionIndex, "DomException"}
namespace_id! {HistoryStateId, HistoryStateIndex, "HistoryState"}
namespace_id! {ImageBitmapId, ImageBitmapIndex, "ImageBitmap"}
// We provide ids just for unit testing.
pub const TEST_NAMESPACE: PipelineNamespaceId = PipelineNamespaceId(1234);
#[allow(unsafe_code)]

View file

@ -10,7 +10,7 @@ mod transferable;
use std::collections::HashMap;
use base::id::{BlobId, DomExceptionId, DomPointId, MessagePortId};
use base::id::{BlobId, DomExceptionId, DomPointId, ImageBitmapId, MessagePortId};
use log::warn;
use malloc_size_of_derive::MallocSizeOf;
use serde::{Deserialize, Serialize};
@ -34,6 +34,10 @@ pub struct StructuredSerializedData {
pub ports: Option<HashMap<MessagePortId, MessagePortImpl>>,
/// Transform streams transferred objects.
pub transform_streams: Option<HashMap<MessagePortId, TransformStreamData>>,
/// Serialized image bitmap objects.
pub image_bitmaps: Option<HashMap<ImageBitmapId, SerializableImageBitmap>>,
/// Transferred image bitmap objects.
pub transferred_image_bitmaps: Option<HashMap<ImageBitmapId, SerializableImageBitmap>>,
}
impl StructuredSerializedData {
@ -42,6 +46,7 @@ impl StructuredSerializedData {
field.as_ref().is_some_and(|h| h.is_empty())
}
match val {
Transferrable::ImageBitmap => is_field_empty(&self.transferred_image_bitmaps),
Transferrable::MessagePort => is_field_empty(&self.ports),
Transferrable::ReadableStream => is_field_empty(&self.ports),
Transferrable::WritableStream => is_field_empty(&self.ports),

View file

@ -11,7 +11,7 @@ use std::cell::RefCell;
use std::collections::HashMap;
use std::path::PathBuf;
use base::id::{BlobId, DomExceptionId, DomPointId};
use base::id::{BlobId, DomExceptionId, DomPointId, ImageBitmapId};
use malloc_size_of_derive::MallocSizeOf;
use net_traits::filemanager_thread::RelativePos;
use serde::{Deserialize, Serialize};
@ -47,6 +47,8 @@ pub enum Serializable {
DomPointReadOnly,
/// The `DOMException` interface.
DomException,
/// The `ImageBitmap` interface.
ImageBitmap,
}
impl Serializable {
@ -62,6 +64,9 @@ impl Serializable {
Serializable::DomException => {
StructuredSerializedData::clone_all_of_type::<DomException>
},
Serializable::ImageBitmap => {
StructuredSerializedData::clone_all_of_type::<SerializableImageBitmap>
},
}
}
}
@ -312,3 +317,31 @@ impl BroadcastClone for DomException {
Some(self.clone())
}
}
#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
/// A serializable version of the ImageBitmap interface.
pub struct SerializableImageBitmap {
pub width: u32,
pub height: u32,
pub bitmap_data: Vec<u8>,
}
impl BroadcastClone for SerializableImageBitmap {
type Id = ImageBitmapId;
fn source(
data: &StructuredSerializedData,
) -> &Option<std::collections::HashMap<Self::Id, Self>> {
&data.image_bitmaps
}
fn destination(
data: &mut StructuredSerializedData,
) -> &mut Option<std::collections::HashMap<Self::Id, Self>> {
&mut data.image_bitmaps
}
fn clone_for_broadcast(&self) -> Option<Self> {
Some(self.clone())
}
}

View file

@ -24,6 +24,8 @@ pub struct TransformStreamData {
/// All the DOM interfaces that can be transferred.
#[derive(Clone, Copy, Debug, EnumIter)]
pub enum Transferrable {
/// The `ImageBitmap` interface.
ImageBitmap,
/// The `MessagePort` interface.
MessagePort,
/// The `ReadableStream` interface.

View file

@ -5,15 +5,9 @@
[Serialize ImageBitmap created from an HTMLVideoElement]
expected: FAIL
[Serialize ImageBitmap created from an HTMLCanvasElement]
expected: FAIL
[Serialize ImageBitmap created from an HTMLVideoElement from a data URL]
expected: FAIL
[Serialize ImageBitmap created from an OffscreenCanvas]
expected: FAIL
[Serialize ImageBitmap created from a vector HTMLImageElement]
expected: FAIL

View file

@ -11,12 +11,6 @@
[Transfer ImageBitmap created from a Blob]
expected: FAIL
[Transfer ImageBitmap created from an HTMLCanvasElement]
expected: FAIL
[Transfer ImageBitmap created from an OffscreenCanvas]
expected: FAIL
[Transfer ImageBitmap created from a bitmap HTMLImageElement]
expected: FAIL

View file

@ -1,3 +1,4 @@
[offscreencanvas.filter.w.html]
expected: ERROR
[offscreencanvas]
expected: FAIL
expected: TIMEOUT

View file

@ -49,24 +49,6 @@
[Object ImageData object, ImageData 1x1 non-transparent non-black]
expected: FAIL
[ImageBitmap 1x1 transparent black]
expected: FAIL
[ImageBitmap 1x1 non-transparent non-black]
expected: FAIL
[Array ImageBitmap object, ImageBitmap 1x1 transparent black]
expected: FAIL
[Array ImageBitmap object, ImageBitmap 1x1 transparent non-black]
expected: FAIL
[Object ImageBitmap object, ImageBitmap 1x1 transparent black]
expected: FAIL
[Object ImageBitmap object, ImageBitmap 1x1 transparent non-black]
expected: FAIL
[A subclass instance will deserialize as its closest serializable superclass]
expected: FAIL

View file

@ -5,15 +5,9 @@
[Serialize should throw before a detached MessagePort is found]
expected: FAIL
[Serialize should make the ImageBitmap detached, so it cannot be transferred again]
expected: FAIL
[Serialize should throw before a detached ImageBitmap is found]
expected: FAIL
[Cannot transfer ImageBitmap detached while the message was serialized]
expected: FAIL
[Serialize should make the OffscreenCanvas detached, so it cannot be transferred again]
expected: FAIL

View file

@ -29,24 +29,6 @@
[Object ImageData object, ImageData 1x1 non-transparent non-black]
expected: FAIL
[ImageBitmap 1x1 transparent black]
expected: FAIL
[ImageBitmap 1x1 non-transparent non-black]
expected: FAIL
[Array ImageBitmap object, ImageBitmap 1x1 transparent black]
expected: FAIL
[Array ImageBitmap object, ImageBitmap 1x1 transparent non-black]
expected: FAIL
[Object ImageBitmap object, ImageBitmap 1x1 transparent black]
expected: FAIL
[Object ImageBitmap object, ImageBitmap 1x1 transparent non-black]
expected: FAIL
[A subclass instance will deserialize as its closest serializable superclass]
expected: FAIL

View file

@ -43,24 +43,6 @@
[Object ImageData object, ImageData 1x1 non-transparent non-black]
expected: FAIL
[ImageBitmap 1x1 transparent black]
expected: FAIL
[ImageBitmap 1x1 non-transparent non-black]
expected: FAIL
[Array ImageBitmap object, ImageBitmap 1x1 transparent black]
expected: FAIL
[Array ImageBitmap object, ImageBitmap 1x1 transparent non-black]
expected: FAIL
[Object ImageBitmap object, ImageBitmap 1x1 transparent black]
expected: FAIL
[Object ImageBitmap object, ImageBitmap 1x1 transparent non-black]
expected: FAIL
[A subclass instance will deserialize as its closest serializable superclass]
expected: FAIL

View file

@ -1,9 +1,10 @@
[postMessage_cross_domain_image_transfer_2d.sub.htm]
expected: TIMEOUT
[sending 2D canvas ImageBitmap to http://web-platform.test:8000]
expected: FAIL
expected: TIMEOUT
[sending 2D canvas ImageBitmap to http://www1.web-platform.test:8000]
expected: FAIL
expected: NOTRUN
[sending 2D canvas ImageBitmap to http://not-web-platform.test:8000]
expected: FAIL
expected: NOTRUN

View file

@ -32,24 +32,6 @@
[Object with a getter that throws]
expected: FAIL
[ImageBitmap 1x1 transparent black]
expected: FAIL
[ImageBitmap 1x1 non-transparent non-black]
expected: FAIL
[Array ImageBitmap object, ImageBitmap 1x1 transparent black]
expected: FAIL
[Array ImageBitmap object, ImageBitmap 1x1 transparent non-black]
expected: FAIL
[Object ImageBitmap object, ImageBitmap 1x1 transparent black]
expected: FAIL
[Object ImageBitmap object, ImageBitmap 1x1 transparent non-black]
expected: FAIL
[A subclass instance will deserialize as its closest serializable superclass]
expected: FAIL

View file

@ -2106,6 +2106,7 @@ class IDLInterface(IDLInterfaceOrNamespace):
or identifier == "Serializable"
or identifier == "Abstract"
or identifier == "Inline"
or identifier == "Transferable"
):
# Known extended attributes that do not take values
if not attr.noArguments():

12
third_party/WebIDL/transferable.patch vendored Normal file
View file

@ -0,0 +1,12 @@
diff --git a/third_party/WebIDL/WebIDL.py b/third_party/WebIDL/WebIDL.py
index 40e118e3781..bad5cbe54ca 100644
--- a/third_party/WebIDL/WebIDL.py
+++ b/third_party/WebIDL/WebIDL.py
@@ -2106,6 +2106,7 @@ class IDLInterface(IDLInterfaceOrNamespace):
or identifier == "Serializable"
or identifier == "Abstract"
or identifier == "Inline"
+ or identifier == "Transferable"
):
# Known extended attributes that do not take values
if not attr.noArguments():

View file

@ -7,6 +7,7 @@ patch < inline.patch
patch < like-as-iterable.patch
patch < builtin-array.patch
patch < array-type.patch
patch < transferable.patch
wget https://hg.mozilla.org/mozilla-central/archive/tip.zip/dom/bindings/parser/tests/ -O tests.zip
rm -r tests