Skip to main content

script/dom/bindings/
structuredclone.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! This module implements structured cloning, as defined by [HTML](https://html.spec.whatwg.org/multipage/#safe-passing-of-structured-data).
6
7use std::ffi::CStr;
8use std::os::raw;
9use std::ptr::{self, NonNull};
10
11use js::context::JSContext;
12use js::gc::RootedVec;
13use js::glue::{
14    CopyJSStructuredCloneData, GetLengthOfJSStructuredCloneData, WriteBytesToJSStructuredCloneData,
15};
16use js::jsapi::{
17    CloneDataPolicy, HandleObject as RawHandleObject, Heap, JS_ReadUint32Pair,
18    JS_STRUCTURED_CLONE_VERSION, JS_WriteUint32Pair, JSContext as RawJSContext, JSObject,
19    JSStructuredCloneCallbacks, JSStructuredCloneReader, JSStructuredCloneWriter,
20    MutableHandleObject as RawMutableHandleObject, StructuredCloneScope, TransferableOwnership,
21};
22use js::jsval::UndefinedValue;
23use js::realm::CurrentRealm;
24use js::rust::wrappers2::{JS_IsExceptionPending, JS_ReadStructuredClone, JS_WriteStructuredClone};
25use js::rust::{
26    CustomAutoRooterGuard, HandleValue, JSAutoStructuredCloneBufferWrapper, MutableHandleValue,
27};
28use rustc_hash::FxHashMap;
29use script_bindings::conversions::{IDLInterface, SafeToJSValConvertible};
30use servo_base::id::{
31    BlobId, CryptoKeyId, DomExceptionId, DomMatrixId, DomPointId, DomQuadId, DomRectId, FileId,
32    FileListId, ImageBitmapId, ImageDataId, Index, MessagePortId, NamespaceIndex,
33    OffscreenCanvasId, PipelineNamespaceId, QuotaExceededErrorId,
34};
35use servo_constellation_traits::{
36    BlobImpl, DomException, DomMatrix, DomPoint, DomQuad, DomRect, MessagePortImpl,
37    Serializable as SerializableInterface, SerializableCryptoKey, SerializableFile,
38    SerializableFileList, SerializableImageBitmap, SerializableImageData,
39    SerializableQuotaExceededError, StructuredSerializedData, TransferableOffscreenCanvas,
40    Transferrable as TransferrableInterface, TransformStreamData,
41};
42use strum::IntoEnumIterator;
43
44use crate::dom::bindings::conversions::root_from_object;
45use crate::dom::bindings::error::{Error, Fallible};
46use crate::dom::bindings::root::DomRoot;
47use crate::dom::bindings::serializable::{Serializable, StorageKey};
48use crate::dom::bindings::transferable::Transferable;
49use crate::dom::blob::Blob;
50use crate::dom::cryptokey::CryptoKey;
51use crate::dom::dompoint::DOMPoint;
52use crate::dom::dompointreadonly::DOMPointReadOnly;
53use crate::dom::file::File;
54use crate::dom::filelist::FileList;
55use crate::dom::globalscope::GlobalScope;
56use crate::dom::imagebitmap::ImageBitmap;
57use crate::dom::imagedata::ImageData;
58use crate::dom::messageport::MessagePort;
59use crate::dom::offscreencanvas::OffscreenCanvas;
60use crate::dom::stream::readablestream::ReadableStream;
61use crate::dom::stream::writablestream::WritableStream;
62use crate::dom::types::{
63    DOMException, DOMMatrix, DOMMatrixReadOnly, DOMQuad, DOMRect, DOMRectReadOnly,
64    QuotaExceededError, TransformStream,
65};
66use crate::realms::enter_auto_realm;
67use crate::script_runtime::CanGc;
68
69// TODO: Should we add Min and Max const to https://github.com/servo/rust-mozjs/blob/master/src/consts.rs?
70// TODO: Determine for sure which value Min and Max should have.
71// NOTE: Current values found at https://dxr.mozilla.org/mozilla-central/
72// rev/ff04d410e74b69acfab17ef7e73e7397602d5a68/js/public/StructuredClone.h#323
73#[repr(u32)]
74pub(super) enum StructuredCloneTags {
75    /// To support additional types, add new tags with values incremented from the last one before Max.
76    Min = 0xFFFF8000,
77    DomBlob = 0xFFFF8001,
78    DomFile = 0xFFFF8002,
79    DomFileList = 0xFFFF8003,
80    MessagePort = 0xFFFF8004,
81    Principals = 0xFFFF8005,
82    DomPointReadOnly = 0xFFFF8006,
83    DomPoint = 0xFFFF8007,
84    ReadableStream = 0xFFFF8008,
85    DomException = 0xFFFF8009,
86    WritableStream = 0xFFFF800A,
87    TransformStream = 0xFFFF800B,
88    ImageBitmap = 0xFFFF800C,
89    OffscreenCanvas = 0xFFFF800D,
90    QuotaExceededError = 0xFFFF800E,
91    DomRect = 0xFFFF800F,
92    DomRectReadOnly = 0xFFFF8010,
93    DomQuad = 0xFFFF8011,
94    DomMatrix = 0xFFFF8012,
95    DomMatrixReadOnly = 0xFFFF8013,
96    ImageData = 0xFFFF8014,
97    CryptoKey = 0xFFFF8015,
98    Max = 0xFFFFFFFF,
99}
100
101impl From<SerializableInterface> for StructuredCloneTags {
102    fn from(v: SerializableInterface) -> Self {
103        match v {
104            SerializableInterface::File => StructuredCloneTags::DomFile,
105            SerializableInterface::FileList => StructuredCloneTags::DomFileList,
106            SerializableInterface::Blob => StructuredCloneTags::DomBlob,
107            SerializableInterface::DomPoint => StructuredCloneTags::DomPoint,
108            SerializableInterface::DomPointReadOnly => StructuredCloneTags::DomPointReadOnly,
109            SerializableInterface::DomRect => StructuredCloneTags::DomRect,
110            SerializableInterface::DomRectReadOnly => StructuredCloneTags::DomRectReadOnly,
111            SerializableInterface::DomQuad => StructuredCloneTags::DomQuad,
112            SerializableInterface::DomMatrix => StructuredCloneTags::DomMatrix,
113            SerializableInterface::DomMatrixReadOnly => StructuredCloneTags::DomMatrixReadOnly,
114            SerializableInterface::DomException => StructuredCloneTags::DomException,
115            SerializableInterface::ImageBitmap => StructuredCloneTags::ImageBitmap,
116            SerializableInterface::QuotaExceededError => StructuredCloneTags::QuotaExceededError,
117            SerializableInterface::ImageData => StructuredCloneTags::ImageData,
118            SerializableInterface::CryptoKey => StructuredCloneTags::CryptoKey,
119        }
120    }
121}
122
123impl From<TransferrableInterface> for StructuredCloneTags {
124    fn from(v: TransferrableInterface) -> Self {
125        match v {
126            TransferrableInterface::ImageBitmap => StructuredCloneTags::ImageBitmap,
127            TransferrableInterface::MessagePort => StructuredCloneTags::MessagePort,
128            TransferrableInterface::OffscreenCanvas => StructuredCloneTags::OffscreenCanvas,
129            TransferrableInterface::ReadableStream => StructuredCloneTags::ReadableStream,
130            TransferrableInterface::WritableStream => StructuredCloneTags::WritableStream,
131            TransferrableInterface::TransformStream => StructuredCloneTags::TransformStream,
132        }
133    }
134}
135
136fn reader_for_type(
137    val: SerializableInterface,
138) -> unsafe fn(
139    cx: &mut JSContext,
140    &GlobalScope,
141    *mut JSStructuredCloneReader,
142    &mut StructuredDataReader<'_>,
143) -> *mut JSObject {
144    match val {
145        SerializableInterface::File => read_object::<File>,
146        SerializableInterface::FileList => read_object::<FileList>,
147        SerializableInterface::Blob => read_object::<Blob>,
148        SerializableInterface::DomPoint => read_object::<DOMPoint>,
149        SerializableInterface::DomPointReadOnly => read_object::<DOMPointReadOnly>,
150        SerializableInterface::DomRect => read_object::<DOMRect>,
151        SerializableInterface::DomRectReadOnly => read_object::<DOMRectReadOnly>,
152        SerializableInterface::DomQuad => read_object::<DOMQuad>,
153        SerializableInterface::DomMatrix => read_object::<DOMMatrix>,
154        SerializableInterface::DomMatrixReadOnly => read_object::<DOMMatrixReadOnly>,
155        SerializableInterface::DomException => read_object::<DOMException>,
156        SerializableInterface::ImageBitmap => read_object::<ImageBitmap>,
157        SerializableInterface::QuotaExceededError => read_object::<QuotaExceededError>,
158        SerializableInterface::ImageData => read_object::<ImageData>,
159        SerializableInterface::CryptoKey => read_object::<CryptoKey>,
160    }
161}
162
163unsafe fn read_object<T: Serializable>(
164    cx: &mut JSContext,
165    owner: &GlobalScope,
166    r: *mut JSStructuredCloneReader,
167    sc_reader: &mut StructuredDataReader<'_>,
168) -> *mut JSObject {
169    let mut name_space: u32 = 0;
170    let mut index: u32 = 0;
171    unsafe {
172        assert!(JS_ReadUint32Pair(
173            r,
174            &mut name_space as *mut u32,
175            &mut index as *mut u32
176        ));
177    }
178    let storage_key = StorageKey { index, name_space };
179
180    // 1. Re-build the key for the storage location
181    // of the serialized object.
182    let id: NamespaceIndex<T::Index> = storage_key.into();
183
184    // 2. Get the transferred object from its storage, using the key.
185    let objects = T::serialized_storage(StructuredData::Reader(sc_reader));
186    let objects_map = objects
187        .as_mut()
188        .expect("The SC holder does not have any relevant objects");
189    let serialized = objects_map
190        .remove(&id)
191        .expect("No object to be deserialized found.");
192    if objects_map.is_empty() {
193        *objects = None;
194    }
195
196    if let Ok(obj) = T::deserialize(cx, owner, serialized) {
197        let reflector = obj.reflector().get_jsobject().get();
198        sc_reader.roots.push(Heap::boxed(reflector));
199        return reflector;
200    }
201    warn!("Reading structured data failed in {:?}.", owner.get_url());
202    ptr::null_mut()
203}
204
205unsafe fn write_object<T: Serializable>(
206    interface: SerializableInterface,
207    owner: &GlobalScope,
208    object: &T,
209    w: *mut JSStructuredCloneWriter,
210    sc_writer: &mut StructuredDataWriter,
211) -> bool {
212    if let Ok((new_id, serialized)) = object.serialize() {
213        let objects = T::serialized_storage(StructuredData::Writer(sc_writer))
214            .get_or_insert(FxHashMap::default());
215        objects.insert(new_id, serialized);
216        let storage_key = StorageKey::new(new_id);
217
218        unsafe {
219            assert!(JS_WriteUint32Pair(
220                w,
221                StructuredCloneTags::from(interface) as u32,
222                0
223            ));
224            assert!(JS_WriteUint32Pair(
225                w,
226                storage_key.name_space,
227                storage_key.index
228            ));
229        }
230        return true;
231    }
232    warn!("Writing structured data failed in {:?}.", owner.get_url());
233    false
234}
235
236unsafe extern "C" fn read_callback(
237    cx: *mut RawJSContext,
238    r: *mut JSStructuredCloneReader,
239    _policy: *const CloneDataPolicy,
240    tag: u32,
241    _data: u32,
242    closure: *mut raw::c_void,
243) -> *mut JSObject {
244    assert!(
245        tag < StructuredCloneTags::Max as u32,
246        "tag should be lower than StructuredCloneTags::Max"
247    );
248    assert!(
249        tag > StructuredCloneTags::Min as u32,
250        "tag should be higher than StructuredCloneTags::Min"
251    );
252
253    // SAFETY: it is safe to construct a JSContext from engine hook.
254    let mut cx = unsafe { JSContext::from_ptr(NonNull::new(cx).unwrap()) };
255    let cx = &mut cx;
256
257    let sc_reader = unsafe { &mut *(closure as *mut StructuredDataReader<'_>) };
258
259    let realm = CurrentRealm::assert(cx);
260    let global = GlobalScope::from_current_realm(&realm);
261
262    for serializable in SerializableInterface::iter() {
263        if tag == StructuredCloneTags::from(serializable) as u32 {
264            let reader = reader_for_type(serializable);
265            return unsafe { reader(cx, &global, r, sc_reader) };
266        }
267    }
268
269    ptr::null_mut()
270}
271
272enum OperationError {
273    InterfaceDoesNotMatch,
274    Exception(Error),
275}
276
277unsafe fn try_serialize<T: Serializable + IDLInterface>(
278    cx: &mut JSContext,
279    val: SerializableInterface,
280    object: RawHandleObject,
281    global: &GlobalScope,
282    w: *mut JSStructuredCloneWriter,
283    writer: &mut StructuredDataWriter,
284) -> Result<bool, OperationError> {
285    let object = unsafe { root_from_object::<T>(*object, cx.raw_cx()) };
286    if let Ok(obj) = object {
287        return unsafe { Ok(write_object(val, global, &*obj, w, writer)) };
288    }
289    Err(OperationError::InterfaceDoesNotMatch)
290}
291
292type SerializeOperation = unsafe fn(
293    &mut JSContext,
294    SerializableInterface,
295    RawHandleObject,
296    &GlobalScope,
297    *mut JSStructuredCloneWriter,
298    &mut StructuredDataWriter,
299) -> Result<bool, OperationError>;
300
301fn serialize_for_type(val: SerializableInterface) -> SerializeOperation {
302    match val {
303        SerializableInterface::File => try_serialize::<File>,
304        SerializableInterface::FileList => try_serialize::<FileList>,
305        SerializableInterface::Blob => try_serialize::<Blob>,
306        SerializableInterface::DomPoint => try_serialize::<DOMPoint>,
307        SerializableInterface::DomPointReadOnly => try_serialize::<DOMPointReadOnly>,
308        SerializableInterface::DomRect => try_serialize::<DOMRect>,
309        SerializableInterface::DomRectReadOnly => try_serialize::<DOMRectReadOnly>,
310        SerializableInterface::DomQuad => try_serialize::<DOMQuad>,
311        SerializableInterface::DomMatrix => try_serialize::<DOMMatrix>,
312        SerializableInterface::DomMatrixReadOnly => try_serialize::<DOMMatrixReadOnly>,
313        SerializableInterface::DomException => try_serialize::<DOMException>,
314        SerializableInterface::ImageBitmap => try_serialize::<ImageBitmap>,
315        SerializableInterface::QuotaExceededError => try_serialize::<QuotaExceededError>,
316        SerializableInterface::ImageData => try_serialize::<ImageData>,
317        SerializableInterface::CryptoKey => try_serialize::<CryptoKey>,
318    }
319}
320
321unsafe extern "C" fn write_callback(
322    cx: *mut RawJSContext,
323    w: *mut JSStructuredCloneWriter,
324    obj: RawHandleObject,
325    _same_process_scope_required: *mut bool,
326    closure: *mut raw::c_void,
327) -> bool {
328    // SAFETY: it is safe to construct a JSContext from engine hook.
329    let mut cx = unsafe { JSContext::from_ptr(NonNull::new(cx).unwrap()) };
330    let cx = &mut cx;
331
332    let sc_writer = unsafe { &mut *(closure as *mut StructuredDataWriter) };
333
334    let realm = CurrentRealm::assert(cx);
335    let global = GlobalScope::from_current_realm(&realm);
336
337    for serializable in SerializableInterface::iter() {
338        let serializer = serialize_for_type(serializable);
339        if let Ok(result) = unsafe { serializer(cx, serializable, obj, &global, w, sc_writer) } {
340            return result;
341        }
342    }
343    false
344}
345
346fn receiver_for_type(
347    val: TransferrableInterface,
348) -> fn(
349    &mut JSContext,
350    &GlobalScope,
351    &mut StructuredDataReader<'_>,
352    u64,
353    RawMutableHandleObject,
354) -> Result<(), ()> {
355    match val {
356        TransferrableInterface::ImageBitmap => receive_object::<ImageBitmap>,
357        TransferrableInterface::MessagePort => receive_object::<MessagePort>,
358        TransferrableInterface::OffscreenCanvas => receive_object::<OffscreenCanvas>,
359        TransferrableInterface::ReadableStream => receive_object::<ReadableStream>,
360        TransferrableInterface::WritableStream => receive_object::<WritableStream>,
361        TransferrableInterface::TransformStream => receive_object::<TransformStream>,
362    }
363}
364
365fn receive_object<T: Transferable>(
366    cx: &mut JSContext,
367    owner: &GlobalScope,
368    sc_reader: &mut StructuredDataReader<'_>,
369    extra_data: u64,
370    return_object: RawMutableHandleObject,
371) -> Result<(), ()> {
372    // 1. Re-build the key for the storage location
373    // of the transferred object.
374    let big: [u8; 8] = extra_data.to_ne_bytes();
375    let (name_space, index) = big.split_at(4);
376
377    let namespace_id = PipelineNamespaceId(u32::from_ne_bytes(
378        name_space
379            .try_into()
380            .expect("name_space to be a slice of four."),
381    ));
382    let id: NamespaceIndex<T::Index> = NamespaceIndex {
383        namespace_id,
384        index: Index::new(u32::from_ne_bytes(
385            index.try_into().expect("index to be a slice of four."),
386        ))
387        .expect("Index to be non-zero"),
388    };
389
390    // 2. Get the transferred object from its storage, using the key.
391    let storage = T::serialized_storage(StructuredData::Reader(sc_reader));
392    let serialized = if let Some(objects) = storage.as_mut() {
393        let object = objects.remove(&id).expect("Transferred port to be stored");
394        if objects.is_empty() {
395            *storage = None;
396        }
397        object
398    } else {
399        panic!(
400            "An interface was transfer-received, yet the SC holder does not have any serialized objects"
401        );
402    };
403
404    let Ok(received) = T::transfer_receive(cx, owner, id, serialized) else {
405        return Err(());
406    };
407    return_object.set(received.reflector().rootable().get());
408    sc_reader.roots.push(Heap::boxed(return_object.get()));
409    Ok(())
410}
411
412unsafe extern "C" fn read_transfer_callback(
413    cx: *mut RawJSContext,
414    _r: *mut JSStructuredCloneReader,
415    _policy: *const CloneDataPolicy,
416    tag: u32,
417    _content: *mut raw::c_void,
418    extra_data: u64,
419    closure: *mut raw::c_void,
420    return_object: RawMutableHandleObject,
421) -> bool {
422    let sc_reader = unsafe { &mut *(closure as *mut StructuredDataReader<'_>) };
423    let mut cx = unsafe {
424        // This is safe because we are in SM hook
425        JSContext::from_ptr(
426            NonNull::new(cx).expect("JSContext pointer should not be null in SM hook"),
427        )
428    };
429    let mut cx = CurrentRealm::assert(&mut cx);
430    let owner = GlobalScope::from_current_realm(&cx);
431
432    for transferrable in TransferrableInterface::iter() {
433        if tag == StructuredCloneTags::from(transferrable) as u32 {
434            let transfer_receiver = receiver_for_type(transferrable);
435            if transfer_receiver(&mut cx, &owner, sc_reader, extra_data, return_object).is_ok() {
436                return true;
437            }
438        }
439    }
440    false
441}
442
443unsafe fn try_transfer<T: Transferable + IDLInterface>(
444    interface: TransferrableInterface,
445    obj: RawHandleObject,
446    cx: &mut JSContext,
447    sc_writer: &mut StructuredDataWriter,
448    tag: *mut u32,
449    ownership: *mut TransferableOwnership,
450    extra_data: *mut u64,
451) -> Result<(), OperationError> {
452    let object = unsafe { root_from_object::<T>(*obj, cx.raw_cx()) };
453    let Ok(object) = object else {
454        return Err(OperationError::InterfaceDoesNotMatch);
455    };
456
457    unsafe { *tag = StructuredCloneTags::from(interface) as u32 };
458    unsafe { *ownership = TransferableOwnership::SCTAG_TMO_CUSTOM };
459
460    let (id, object) = object.transfer(cx).map_err(OperationError::Exception)?;
461
462    // 2. Store the transferred object at a given key.
463    let objects = T::serialized_storage(StructuredData::Writer(sc_writer))
464        .get_or_insert(FxHashMap::default());
465    objects.insert(id, object);
466
467    let index = id.index.0.get();
468
469    let mut big: [u8; 8] = [0; 8];
470    let name_space = id.namespace_id.0.to_ne_bytes();
471    let index = index.to_ne_bytes();
472
473    let (left, right) = big.split_at_mut(4);
474    left.copy_from_slice(&name_space);
475    right.copy_from_slice(&index);
476
477    // 3. Return a u64 representation of the key where the object is stored.
478    unsafe { *extra_data = u64::from_ne_bytes(big) };
479    Ok(())
480}
481
482type TransferOperation = unsafe fn(
483    TransferrableInterface,
484    RawHandleObject,
485    &mut JSContext,
486    &mut StructuredDataWriter,
487    *mut u32,
488    *mut TransferableOwnership,
489    *mut u64,
490) -> Result<(), OperationError>;
491
492fn transfer_for_type(val: TransferrableInterface) -> TransferOperation {
493    match val {
494        TransferrableInterface::ImageBitmap => try_transfer::<ImageBitmap>,
495        TransferrableInterface::MessagePort => try_transfer::<MessagePort>,
496        TransferrableInterface::OffscreenCanvas => try_transfer::<OffscreenCanvas>,
497        TransferrableInterface::ReadableStream => try_transfer::<ReadableStream>,
498        TransferrableInterface::WritableStream => try_transfer::<WritableStream>,
499        TransferrableInterface::TransformStream => try_transfer::<TransformStream>,
500    }
501}
502
503/// <https://html.spec.whatwg.org/multipage/#structuredserializewithtransfer>
504unsafe extern "C" fn write_transfer_callback(
505    cx: *mut RawJSContext,
506    obj: RawHandleObject,
507    closure: *mut raw::c_void,
508    tag: *mut u32,
509    ownership: *mut TransferableOwnership,
510    _content: *mut *mut raw::c_void,
511    extra_data: *mut u64,
512) -> bool {
513    let sc_writer = unsafe { &mut *(closure as *mut StructuredDataWriter) };
514    let mut cx = unsafe {
515        // This is safe because we are in SM hook
516        JSContext::from_ptr(
517            NonNull::new(cx).expect("JSContext pointer should not be null in SM hook"),
518        )
519    };
520    for transferable in TransferrableInterface::iter() {
521        let try_transfer = transfer_for_type(transferable);
522
523        let transfer_result = unsafe {
524            try_transfer(
525                transferable,
526                obj,
527                &mut cx,
528                sc_writer,
529                tag,
530                ownership,
531                extra_data,
532            )
533        };
534        match transfer_result {
535            Err(error) => match error {
536                OperationError::InterfaceDoesNotMatch => {},
537                OperationError::Exception(error) => {
538                    sc_writer.error = Some(error);
539                    return false;
540                },
541            },
542            Ok(..) => return true,
543        }
544    }
545
546    false
547}
548
549unsafe extern "C" fn free_transfer_callback(
550    _tag: u32,
551    _ownership: TransferableOwnership,
552    _content: *mut raw::c_void,
553    _extra_data: u64,
554    _closure: *mut raw::c_void,
555) {
556}
557
558unsafe fn can_transfer_for_type(
559    cx: &mut JSContext,
560    transferable: TransferrableInterface,
561    obj: RawHandleObject,
562) -> Result<bool, ()> {
563    unsafe fn can_transfer<T: Transferable + IDLInterface>(
564        cx: &mut JSContext,
565        obj: RawHandleObject,
566    ) -> Result<bool, ()> {
567        unsafe { root_from_object::<T>(*obj, cx.raw_cx()).map(|o| Transferable::can_transfer(&*o)) }
568    }
569
570    unsafe {
571        match transferable {
572            TransferrableInterface::ImageBitmap => can_transfer::<ImageBitmap>(cx, obj),
573            TransferrableInterface::MessagePort => can_transfer::<MessagePort>(cx, obj),
574            TransferrableInterface::OffscreenCanvas => can_transfer::<OffscreenCanvas>(cx, obj),
575            TransferrableInterface::ReadableStream => can_transfer::<ReadableStream>(cx, obj),
576            TransferrableInterface::WritableStream => can_transfer::<WritableStream>(cx, obj),
577            TransferrableInterface::TransformStream => can_transfer::<TransformStream>(cx, obj),
578        }
579    }
580}
581
582unsafe extern "C" fn can_transfer_callback(
583    cx: *mut RawJSContext,
584    obj: RawHandleObject,
585    _same_process_scope_required: *mut bool,
586    _closure: *mut raw::c_void,
587) -> bool {
588    // SAFETY: it is safe to construct a JSContext from engine hook.
589    let mut cx = unsafe { JSContext::from_ptr(NonNull::new(cx).unwrap()) };
590    let cx = &mut cx;
591
592    for transferable in TransferrableInterface::iter() {
593        let can_transfer = unsafe { can_transfer_for_type(cx, transferable, obj) };
594        if let Ok(can_transfer) = can_transfer {
595            return can_transfer;
596        }
597    }
598    false
599}
600
601unsafe extern "C" fn report_error_callback(
602    _cx: *mut RawJSContext,
603    _errorid: u32,
604    closure: *mut raw::c_void,
605    error_message: *const ::std::os::raw::c_char,
606) {
607    let msg_result = unsafe { CStr::from_ptr(error_message).to_str().map(str::to_string) };
608
609    if let Ok(msg) = msg_result {
610        let error = unsafe { &mut *(closure as *mut Option<Error>) };
611
612        if error.is_none() {
613            *error = Some(Error::DataClone(Some(msg)));
614        }
615    }
616}
617
618unsafe extern "C" fn sab_cloned_callback(
619    _cx: *mut RawJSContext,
620    _receiving: bool,
621    _closure: *mut ::std::os::raw::c_void,
622) -> bool {
623    false
624}
625
626static STRUCTURED_CLONE_CALLBACKS: JSStructuredCloneCallbacks = JSStructuredCloneCallbacks {
627    read: Some(read_callback),
628    write: Some(write_callback),
629    reportError: Some(report_error_callback),
630    readTransfer: Some(read_transfer_callback),
631    writeTransfer: Some(write_transfer_callback),
632    freeTransfer: Some(free_transfer_callback),
633    canTransfer: Some(can_transfer_callback),
634    sabCloned: Some(sab_cloned_callback),
635};
636
637pub(crate) enum StructuredData<'a, 'b> {
638    Reader(&'a mut StructuredDataReader<'b>),
639    Writer(&'a mut StructuredDataWriter),
640}
641
642/// Reader and writer structs for results from, and inputs to, structured-data read/write operations.
643/// <https://html.spec.whatwg.org/multipage/#safe-passing-of-structured-data>
644#[repr(C)]
645pub(crate) struct StructuredDataReader<'a> {
646    /// A error record.
647    error: Option<Error>,
648    /// Rooted copies of every deserialized object to ensure they are not garbage collected.
649    roots: RootedVec<'a, Box<Heap<*mut JSObject>>>,
650    /// A map of port implementations,
651    /// used as part of the "transfer-receiving" steps of ports,
652    /// to produce the DOM ports stored in `message_ports` above.
653    pub(crate) port_impls: Option<FxHashMap<MessagePortId, MessagePortImpl>>,
654    /// A map of transform stream implementations,
655    pub(crate) transform_streams_port_impls: Option<FxHashMap<MessagePortId, TransformStreamData>>,
656    /// A map of blob implementations,
657    /// used as part of the "deserialize" steps of blobs,
658    /// to produce the DOM blobs stored in `blobs` above.
659    pub(crate) blob_impls: Option<FxHashMap<BlobId, BlobImpl>>,
660    /// A map of serialized files.
661    pub(crate) files: Option<FxHashMap<FileId, SerializableFile>>,
662    /// A map of serialized file lists.
663    pub(crate) file_lists: Option<FxHashMap<FileListId, SerializableFileList>>,
664    /// A map of serialized points.
665    pub(crate) points: Option<FxHashMap<DomPointId, DomPoint>>,
666    /// A map of serialized rects.
667    pub(crate) rects: Option<FxHashMap<DomRectId, DomRect>>,
668    /// A map of serialized quads.
669    pub(crate) quads: Option<FxHashMap<DomQuadId, DomQuad>>,
670    /// A map of serialized matrices.
671    pub(crate) matrices: Option<FxHashMap<DomMatrixId, DomMatrix>>,
672    /// A map of serialized exceptions.
673    pub(crate) exceptions: Option<FxHashMap<DomExceptionId, DomException>>,
674    /// A map of serialized quota exceeded errors.
675    pub(crate) quota_exceeded_errors:
676        Option<FxHashMap<QuotaExceededErrorId, SerializableQuotaExceededError>>,
677    // A map of serialized image bitmaps.
678    pub(crate) image_bitmaps: Option<FxHashMap<ImageBitmapId, SerializableImageBitmap>>,
679    /// A map of transferred image bitmaps.
680    pub(crate) transferred_image_bitmaps: Option<FxHashMap<ImageBitmapId, SerializableImageBitmap>>,
681    /// A map of transferred offscreen canvases.
682    pub(crate) offscreen_canvases:
683        Option<FxHashMap<OffscreenCanvasId, TransferableOffscreenCanvas>>,
684    // A map of serialized image data.
685    pub(crate) image_data: Option<FxHashMap<ImageDataId, SerializableImageData>>,
686    // A map of serialized crypto keys.
687    pub(crate) crypto_keys: Option<FxHashMap<CryptoKeyId, SerializableCryptoKey>>,
688}
689
690/// A data holder for transferred and serialized objects.
691#[derive(Default)]
692#[repr(C)]
693pub(crate) struct StructuredDataWriter {
694    /// Error record.
695    pub(crate) error: Option<Error>,
696    /// Transferred ports.
697    pub(crate) ports: Option<FxHashMap<MessagePortId, MessagePortImpl>>,
698    /// Transferred transform streams.
699    pub(crate) transform_streams_port: Option<FxHashMap<MessagePortId, TransformStreamData>>,
700    /// Serialized points.
701    pub(crate) points: Option<FxHashMap<DomPointId, DomPoint>>,
702    /// Serialized rects.
703    pub(crate) rects: Option<FxHashMap<DomRectId, DomRect>>,
704    /// Serialized quads.
705    pub(crate) quads: Option<FxHashMap<DomQuadId, DomQuad>>,
706    /// Serialized matrices.
707    pub(crate) matrices: Option<FxHashMap<DomMatrixId, DomMatrix>>,
708    /// Serialized exceptions.
709    pub(crate) exceptions: Option<FxHashMap<DomExceptionId, DomException>>,
710    /// Serialized quota exceeded errors.
711    pub(crate) quota_exceeded_errors:
712        Option<FxHashMap<QuotaExceededErrorId, SerializableQuotaExceededError>>,
713    /// Serialized blobs.
714    pub(crate) blobs: Option<FxHashMap<BlobId, BlobImpl>>,
715    /// Serialized files.
716    pub(crate) files: Option<FxHashMap<FileId, SerializableFile>>,
717    /// Serialized file lists.
718    pub(crate) file_lists: Option<FxHashMap<FileListId, SerializableFileList>>,
719    /// Serialized image bitmaps.
720    pub(crate) image_bitmaps: Option<FxHashMap<ImageBitmapId, SerializableImageBitmap>>,
721    /// Transferred image bitmaps.
722    pub(crate) transferred_image_bitmaps: Option<FxHashMap<ImageBitmapId, SerializableImageBitmap>>,
723    /// Transferred offscreen canvases.
724    pub(crate) offscreen_canvases:
725        Option<FxHashMap<OffscreenCanvasId, TransferableOffscreenCanvas>>,
726    // A map of serialized image data.
727    pub(crate) image_data: Option<FxHashMap<ImageDataId, SerializableImageData>>,
728    // A map of serialized crypto keys.
729    pub(crate) crypto_keys: Option<FxHashMap<CryptoKeyId, SerializableCryptoKey>>,
730}
731
732/// Writes a structured clone. Returns a `DataClone` error if that fails.
733pub(crate) fn write(
734    cx: &mut JSContext,
735    message: HandleValue,
736    transfer: Option<CustomAutoRooterGuard<Vec<*mut JSObject>>>,
737) -> Fallible<StructuredSerializedData> {
738    unsafe {
739        rooted!(&in(cx) let mut val = UndefinedValue());
740        if let Some(transfer) = transfer {
741            transfer.safe_to_jsval(cx.into(), val.handle_mut(), CanGc::from_cx(cx));
742        }
743        let mut sc_writer = StructuredDataWriter::default();
744        let sc_writer_ptr = &mut sc_writer as *mut _;
745
746        let scbuf = JSAutoStructuredCloneBufferWrapper::new(
747            StructuredCloneScope::DifferentProcess,
748            &STRUCTURED_CLONE_CALLBACKS,
749        );
750        let scdata = &mut ((*scbuf.as_raw_ptr()).data_);
751        let policy = CloneDataPolicy {
752            allowIntraClusterClonableSharedObjects_: false,
753            allowSharedMemoryObjects_: false,
754        };
755        let result = JS_WriteStructuredClone(
756            cx,
757            message,
758            scdata,
759            StructuredCloneScope::DifferentProcess,
760            &policy,
761            &STRUCTURED_CLONE_CALLBACKS,
762            sc_writer_ptr as *mut raw::c_void,
763            val.handle(),
764        );
765        if !result {
766            let error = if JS_IsExceptionPending(cx) {
767                Error::JSFailed
768            } else {
769                sc_writer.error.unwrap_or(Error::DataClone(None))
770            };
771
772            return Err(error);
773        }
774
775        let nbytes = GetLengthOfJSStructuredCloneData(scdata);
776        let mut data = Vec::with_capacity(nbytes);
777        CopyJSStructuredCloneData(scdata, data.as_mut_ptr());
778        data.set_len(nbytes);
779
780        let data = StructuredSerializedData {
781            serialized: data,
782            ports: sc_writer.ports.take(),
783            transform_streams: sc_writer.transform_streams_port.take(),
784            points: sc_writer.points.take(),
785            rects: sc_writer.rects.take(),
786            quads: sc_writer.quads.take(),
787            matrices: sc_writer.matrices.take(),
788            exceptions: sc_writer.exceptions.take(),
789            quota_exceeded_errors: sc_writer.quota_exceeded_errors.take(),
790            blobs: sc_writer.blobs.take(),
791            files: sc_writer.files.take(),
792            file_lists: sc_writer.file_lists.take(),
793            image_bitmaps: sc_writer.image_bitmaps.take(),
794            transferred_image_bitmaps: sc_writer.transferred_image_bitmaps.take(),
795            offscreen_canvases: sc_writer.offscreen_canvases.take(),
796            image_data: sc_writer.image_data.take(),
797            crypto_keys: sc_writer.crypto_keys.take(),
798        };
799
800        Ok(data)
801    }
802}
803
804/// Read structured serialized data, possibly containing transferred objects.
805/// Returns a vec of rooted transfer-received ports, or an error.
806pub(crate) fn read(
807    cx: &mut JSContext,
808    global: &GlobalScope,
809    mut data: StructuredSerializedData,
810    rval: MutableHandleValue,
811) -> Fallible<Vec<DomRoot<MessagePort>>> {
812    let mut realm = enter_auto_realm(cx, global);
813    let cx = &mut realm.current_realm();
814
815    rooted_vec!(let mut roots);
816    let mut sc_reader = StructuredDataReader {
817        error: None,
818        roots,
819        port_impls: data.ports.take(),
820        transform_streams_port_impls: data.transform_streams.take(),
821        blob_impls: data.blobs.take(),
822        files: data.files.take(),
823        file_lists: data.file_lists.take(),
824        points: data.points.take(),
825        rects: data.rects.take(),
826        quads: data.quads.take(),
827        matrices: data.matrices.take(),
828        exceptions: data.exceptions.take(),
829        quota_exceeded_errors: data.quota_exceeded_errors.take(),
830        image_bitmaps: data.image_bitmaps.take(),
831        transferred_image_bitmaps: data.transferred_image_bitmaps.take(),
832        offscreen_canvases: data.offscreen_canvases.take(),
833        image_data: data.image_data.take(),
834        crypto_keys: data.crypto_keys.take(),
835    };
836    let sc_reader_ptr = &mut sc_reader as *mut _;
837    unsafe {
838        let scbuf = JSAutoStructuredCloneBufferWrapper::new(
839            StructuredCloneScope::DifferentProcess,
840            &STRUCTURED_CLONE_CALLBACKS,
841        );
842        let scdata = &mut ((*scbuf.as_raw_ptr()).data_);
843
844        WriteBytesToJSStructuredCloneData(
845            data.serialized.as_mut_ptr() as *const u8,
846            data.serialized.len(),
847            scdata,
848        );
849
850        let result = JS_ReadStructuredClone(
851            cx,
852            scdata,
853            JS_STRUCTURED_CLONE_VERSION,
854            StructuredCloneScope::DifferentProcess,
855            rval,
856            &CloneDataPolicy {
857                allowIntraClusterClonableSharedObjects_: false,
858                allowSharedMemoryObjects_: false,
859            },
860            &STRUCTURED_CLONE_CALLBACKS,
861            sc_reader_ptr as *mut raw::c_void,
862        );
863        if !result {
864            let error = if JS_IsExceptionPending(cx) {
865                Error::JSFailed
866            } else {
867                sc_reader.error.unwrap_or(Error::DataClone(None))
868            };
869
870            return Err(error);
871        }
872
873        let mut message_ports = vec![];
874        for reflector in sc_reader.roots.iter() {
875            let Ok(message_port) = root_from_object::<MessagePort>(reflector.get(), cx.raw_cx())
876            else {
877                continue;
878            };
879            message_ports.push(message_port);
880        }
881        // Any transfer-received port-impls should have been taken out.
882        assert!(sc_reader.port_impls.is_none());
883        Ok(message_ports)
884    }
885}