Skip to main content

script/dom/bindings/
transferable.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 http://mozilla.org/MPL/2.0/. */
4
5//! Trait representing the concept of [transferable objects]
6//! (<https://html.spec.whatwg.org/multipage/#transferable-objects>).
7
8use std::hash::Hash;
9
10use rustc_hash::FxHashMap;
11use script_bindings::reflector::DomObject;
12use script_bindings::structuredclone::{MarkedAsTransferableInIdl, StructuredData};
13use servo_base::id::NamespaceIndex;
14
15use crate::dom::bindings::error::Fallible;
16use crate::dom::bindings::root::DomRoot;
17use crate::dom::globalscope::GlobalScope;
18
19pub(crate) trait Transferable: DomObject + MarkedAsTransferableInIdl
20where
21    Self: Sized,
22{
23    type Index: Copy + Eq + Hash;
24    type Data;
25
26    fn can_transfer(&self) -> bool {
27        true
28    }
29
30    /// <https://html.spec.whatwg.org/multipage/#transfer-steps>
31    fn transfer(
32        &self,
33        cx: &mut js::context::JSContext,
34    ) -> Fallible<(NamespaceIndex<Self::Index>, Self::Data)>;
35
36    /// <https://html.spec.whatwg.org/multipage/#transfer-receiving-steps>
37    fn transfer_receive(
38        cx: &mut js::context::JSContext,
39        owner: &GlobalScope,
40        id: NamespaceIndex<Self::Index>,
41        serialized: Self::Data,
42    ) -> Result<DomRoot<Self>, ()>;
43
44    fn serialized_storage<'a>(
45        data: StructuredData<'a, '_>,
46    ) -> &'a mut Option<FxHashMap<NamespaceIndex<Self::Index>, Self::Data>>;
47}
48
49pub(crate) fn assert_transferable<T: Transferable>() {}