Make DOM geometry structs serializable (#38828)

Makes the following DOM geometry structs serializable:
- `DOMRect`
- `DOMRectReadOnly`
- `DOMQuad`
- `DOMMatrix`
- `DOMMatrixReadOnly`

Testing: Covered by WPT (`css/geometry/structured-serialization.html`).

---------

Signed-off-by: lumiscosity <averyrudelphe@gmail.com>
This commit is contained in:
lumiscosity 2025-08-22 01:19:42 +02:00 committed by GitHub
parent e00f39d827
commit 39f3ce7a2e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 529 additions and 89 deletions

View file

@ -11,7 +11,11 @@ use std::cell::RefCell;
use std::collections::HashMap;
use std::path::PathBuf;
use base::id::{BlobId, DomExceptionId, DomPointId, ImageBitmapId, QuotaExceededErrorId};
use base::id::{
BlobId, DomExceptionId, DomMatrixId, DomPointId, DomQuadId, DomRectId, ImageBitmapId,
QuotaExceededErrorId,
};
use euclid::default::Transform3D;
use malloc_size_of_derive::MallocSizeOf;
use net_traits::filemanager_thread::RelativePos;
use pixels::Snapshot;
@ -49,6 +53,16 @@ pub enum Serializable {
DomPoint,
/// The `DOMPointReadOnly` interface.
DomPointReadOnly,
/// The `DOMRect` interface.
DomRect,
/// The `DOMRectReadOnly` interface.
DomRectReadOnly,
/// The `DOMQuad` interface.
DomQuad,
/// The `DOMMatrix` interface.
DomMatrix,
/// The `DOMMatrixReadOnly` interface.
DomMatrixReadOnly,
/// The `QuotaExceededError` interface.
QuotaExceededError,
/// The `DOMException` interface.
@ -63,10 +77,17 @@ impl Serializable {
) -> fn(&StructuredSerializedData, &mut StructuredSerializedData) {
match self {
Serializable::Blob => StructuredSerializedData::clone_all_of_type::<BlobImpl>,
Serializable::DomPoint => StructuredSerializedData::clone_all_of_type::<DomPoint>,
Serializable::DomPointReadOnly => {
StructuredSerializedData::clone_all_of_type::<DomPoint>
},
Serializable::DomPoint => StructuredSerializedData::clone_all_of_type::<DomPoint>,
Serializable::DomRect => StructuredSerializedData::clone_all_of_type::<DomRect>,
Serializable::DomRectReadOnly => StructuredSerializedData::clone_all_of_type::<DomRect>,
Serializable::DomQuad => StructuredSerializedData::clone_all_of_type::<DomQuad>,
Serializable::DomMatrix => StructuredSerializedData::clone_all_of_type::<DomMatrix>,
Serializable::DomMatrixReadOnly => {
StructuredSerializedData::clone_all_of_type::<DomMatrix>
},
Serializable::DomException => {
StructuredSerializedData::clone_all_of_type::<DomException>
},
@ -300,6 +321,101 @@ impl BroadcastClone for DomPoint {
}
}
#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
/// A serializable version of the DOMRect/DOMRectReadOnly interface.
pub struct DomRect {
/// The x coordinate.
pub x: f64,
/// The y coordinate.
pub y: f64,
/// The width.
pub width: f64,
/// The height.
pub height: f64,
}
impl BroadcastClone for DomRect {
type Id = DomRectId;
fn source(
data: &StructuredSerializedData,
) -> &Option<std::collections::HashMap<Self::Id, Self>> {
&data.rects
}
fn destination(
data: &mut StructuredSerializedData,
) -> &mut Option<std::collections::HashMap<Self::Id, Self>> {
&mut data.rects
}
fn clone_for_broadcast(&self) -> Option<Self> {
Some(self.clone())
}
}
#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
/// A serializable version of the DOMQuad interface.
pub struct DomQuad {
/// The first point.
pub p1: DomPoint,
/// The second point.
pub p2: DomPoint,
/// The third point.
pub p3: DomPoint,
/// The fourth point.
pub p4: DomPoint,
}
impl BroadcastClone for DomQuad {
type Id = DomQuadId;
fn source(
data: &StructuredSerializedData,
) -> &Option<std::collections::HashMap<Self::Id, Self>> {
&data.quads
}
fn destination(
data: &mut StructuredSerializedData,
) -> &mut Option<std::collections::HashMap<Self::Id, Self>> {
&mut data.quads
}
fn clone_for_broadcast(&self) -> Option<Self> {
Some(self.clone())
}
}
#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
/// A serializable version of the DOMMatrix/DOMMatrixReadOnly interface.
pub struct DomMatrix {
/// The matrix.
pub matrix: Transform3D<f64>,
/// Whether this matrix represents a 2D transformation.
pub is_2d: bool,
}
impl BroadcastClone for DomMatrix {
type Id = DomMatrixId;
fn source(
data: &StructuredSerializedData,
) -> &Option<std::collections::HashMap<Self::Id, Self>> {
&data.matrices
}
fn destination(
data: &mut StructuredSerializedData,
) -> &mut Option<std::collections::HashMap<Self::Id, Self>> {
&mut data.matrices
}
fn clone_for_broadcast(&self) -> Option<Self> {
Some(self.clone())
}
}
#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
/// A serializable version of the DOMException interface.
pub struct DomException {