servo/components/script/dom/bindings/transferable.rs
Andrei Volykhin a5b02047f9
script: Allow to throw a custom exception on structured cloning (#37948)
The structured cloning with transfer list
https://html.spec.whatwg.org/multipage/#structuredserializewithtransfer
throws a "DataCloneError" DOM expection by default if
serialization/transferral
is not possible, but a platform object can throw a custom excepton on
its serialization/transfer steps.

One example is OffscreenCanvas, which can throw
an "InvalidStateError" exception if the context mode is not none on
transfer steps.

https://html.spec.whatwg.org/multipage/#the-offscreencanvas-interface:transfer-steps

Testing: Improvements in the following tests
-
html/canvas/offscreen/manual/the-offscreen-canvas/offscreencanvas.transferrable*

Fixes: #37919

Signed-off-by: Andrei Volykhin <andrei.volykhin@gmail.com>
2025-07-09 11:43:09 +00:00

43 lines
1.3 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! Trait representing the concept of [transferable objects]
//! (<https://html.spec.whatwg.org/multipage/#transferable-objects>).
use std::collections::HashMap;
use std::hash::Hash;
use base::id::NamespaceIndex;
use crate::dom::bindings::error::Fallible;
use crate::dom::bindings::reflector::DomObject;
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::structuredclone::StructuredData;
use crate::dom::globalscope::GlobalScope;
pub(crate) trait Transferable: DomObject
where
Self: Sized,
{
type Index: Copy + Eq + Hash;
type Data;
fn can_transfer(&self) -> bool {
true
}
/// <https://html.spec.whatwg.org/multipage/#transfer-steps>
fn transfer(&self) -> Fallible<(NamespaceIndex<Self::Index>, Self::Data)>;
/// <https://html.spec.whatwg.org/multipage/#transfer-receiving-steps>
fn transfer_receive(
owner: &GlobalScope,
id: NamespaceIndex<Self::Index>,
serialized: Self::Data,
) -> Result<DomRoot<Self>, ()>;
fn serialized_storage<'a>(
data: StructuredData<'a, '_>,
) -> &'a mut Option<HashMap<NamespaceIndex<Self::Index>, Self::Data>>;
}