Skip to main content

script_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
5use js::gc::RootedVec;
6use js::jsapi::{Heap, JSObject};
7use rustc_hash::FxHashMap;
8use servo_base::id::{
9    BlobId, CryptoKeyId, DomExceptionId, DomMatrixId, DomPointId, DomQuadId, DomRectId, FileId,
10    FileListId, ImageBitmapId, ImageDataId, MessagePortId, OffscreenCanvasId, QuotaExceededErrorId,
11};
12use servo_constellation_traits::{
13    BlobImpl, DomException, DomMatrix, DomPoint, DomQuad, DomRect, MessagePortImpl,
14    SerializableCryptoKey, SerializableFile, SerializableFileList, SerializableImageBitmap,
15    SerializableImageData, SerializableQuotaExceededError, TransferableOffscreenCanvas,
16    TransformStreamData,
17};
18
19use crate::error::Error;
20
21pub enum StructuredData<'a, 'b> {
22    Reader(&'a mut StructuredDataReader<'b>),
23    Writer(&'a mut StructuredDataWriter),
24}
25
26/// Reader and writer structs for results from, and inputs to, structured-data read/write operations.
27/// <https://html.spec.whatwg.org/multipage/#safe-passing-of-structured-data>
28#[repr(C)]
29pub struct StructuredDataReader<'a> {
30    /// A error record.
31    pub error: Option<Error>,
32    /// Rooted copies of every deserialized object to ensure they are not garbage collected.
33    pub roots: RootedVec<'a, Box<Heap<*mut JSObject>>>,
34    /// A map of port implementations,
35    /// used as part of the "transfer-receiving" steps of ports,
36    /// to produce the DOM ports stored in `message_ports` above.
37    pub port_impls: Option<FxHashMap<MessagePortId, MessagePortImpl>>,
38    /// A map of transform stream implementations,
39    pub transform_streams_port_impls: Option<FxHashMap<MessagePortId, TransformStreamData>>,
40    /// A map of blob implementations,
41    /// used as part of the "deserialize" steps of blobs,
42    /// to produce the DOM blobs stored in `blobs` above.
43    pub blob_impls: Option<FxHashMap<BlobId, BlobImpl>>,
44    /// A map of serialized files.
45    pub files: Option<FxHashMap<FileId, SerializableFile>>,
46    /// A map of serialized file lists.
47    pub file_lists: Option<FxHashMap<FileListId, SerializableFileList>>,
48    /// A map of serialized points.
49    pub points: Option<FxHashMap<DomPointId, DomPoint>>,
50    /// A map of serialized rects.
51    pub rects: Option<FxHashMap<DomRectId, DomRect>>,
52    /// A map of serialized quads.
53    pub quads: Option<FxHashMap<DomQuadId, DomQuad>>,
54    /// A map of serialized matrices.
55    pub matrices: Option<FxHashMap<DomMatrixId, DomMatrix>>,
56    /// A map of serialized exceptions.
57    pub exceptions: Option<FxHashMap<DomExceptionId, DomException>>,
58    /// A map of serialized quota exceeded errors.
59    pub quota_exceeded_errors:
60        Option<FxHashMap<QuotaExceededErrorId, SerializableQuotaExceededError>>,
61    // A map of serialized image bitmaps.
62    pub image_bitmaps: Option<FxHashMap<ImageBitmapId, SerializableImageBitmap>>,
63    /// A map of transferred image bitmaps.
64    pub transferred_image_bitmaps: Option<FxHashMap<ImageBitmapId, SerializableImageBitmap>>,
65    /// A map of transferred offscreen canvases.
66    pub offscreen_canvases: Option<FxHashMap<OffscreenCanvasId, TransferableOffscreenCanvas>>,
67    // A map of serialized image data.
68    pub image_data: Option<FxHashMap<ImageDataId, SerializableImageData>>,
69    // A map of serialized crypto keys.
70    pub crypto_keys: Option<FxHashMap<CryptoKeyId, SerializableCryptoKey>>,
71}
72
73/// A data holder for transferred and serialized objects.
74#[derive(Default)]
75#[repr(C)]
76pub struct StructuredDataWriter {
77    /// Error record.
78    pub error: Option<Error>,
79    /// Transferred ports.
80    pub ports: Option<FxHashMap<MessagePortId, MessagePortImpl>>,
81    /// Transferred transform streams.
82    pub transform_streams_port: Option<FxHashMap<MessagePortId, TransformStreamData>>,
83    /// Serialized points.
84    pub points: Option<FxHashMap<DomPointId, DomPoint>>,
85    /// Serialized rects.
86    pub rects: Option<FxHashMap<DomRectId, DomRect>>,
87    /// Serialized quads.
88    pub quads: Option<FxHashMap<DomQuadId, DomQuad>>,
89    /// Serialized matrices.
90    pub matrices: Option<FxHashMap<DomMatrixId, DomMatrix>>,
91    /// Serialized exceptions.
92    pub exceptions: Option<FxHashMap<DomExceptionId, DomException>>,
93    /// Serialized quota exceeded errors.
94    pub quota_exceeded_errors:
95        Option<FxHashMap<QuotaExceededErrorId, SerializableQuotaExceededError>>,
96    /// Serialized blobs.
97    pub blobs: Option<FxHashMap<BlobId, BlobImpl>>,
98    /// Serialized files.
99    pub files: Option<FxHashMap<FileId, SerializableFile>>,
100    /// Serialized file lists.
101    pub file_lists: Option<FxHashMap<FileListId, SerializableFileList>>,
102    /// Serialized image bitmaps.
103    pub image_bitmaps: Option<FxHashMap<ImageBitmapId, SerializableImageBitmap>>,
104    /// Transferred image bitmaps.
105    pub transferred_image_bitmaps: Option<FxHashMap<ImageBitmapId, SerializableImageBitmap>>,
106    /// Transferred offscreen canvases.
107    pub offscreen_canvases: Option<FxHashMap<OffscreenCanvasId, TransferableOffscreenCanvas>>,
108    // A map of serialized image data.
109    pub image_data: Option<FxHashMap<ImageDataId, SerializableImageData>>,
110    // A map of serialized crypto keys.
111    pub crypto_keys: Option<FxHashMap<CryptoKeyId, SerializableCryptoKey>>,
112}
113
114/// A marker to ensure that the `[Serializable]` attribute is present on
115/// types that can be serialized. This trait should not be implemented manually.
116pub trait MarkedAsSerializableInIdl {
117    /// Used to define compile-time assertions about the type implementing this trait.
118    fn assert_serializable();
119}
120
121/// A marker to ensure that the `[Transferable]` attribute is present on
122/// types that can be transferred. This trait should not be implemented manually.
123pub trait MarkedAsTransferableInIdl {
124    /// Used to define compile-time assertions about the type implementing this trait.
125    fn assert_transferable();
126}