Skip to main content

script/dom/testing/
testworklet.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// check-tidy: no specs after this line
6use std::rc::Rc;
7
8use dom_struct::dom_struct;
9use js::context::JSContext;
10use js::realm::CurrentRealm;
11use js::rust::HandleObject;
12use script_bindings::reflector::{Reflector, reflect_dom_object_with_proto};
13
14use crate::dom::WorkletThreadPool;
15use crate::dom::bindings::codegen::Bindings::TestWorkletBinding::TestWorkletMethods;
16use crate::dom::bindings::codegen::Bindings::WorkletBinding::Worklet_Binding::WorkletMethods;
17use crate::dom::bindings::codegen::Bindings::WorkletBinding::WorkletOptions;
18use crate::dom::bindings::error::Fallible;
19use crate::dom::bindings::root::{Dom, DomRoot};
20use crate::dom::bindings::str::{DOMString, USVString};
21use crate::dom::promise::Promise;
22use crate::dom::window::Window;
23use crate::dom::worklet::Worklet;
24use crate::dom::workletglobalscope::WorkletGlobalScopeType;
25
26#[dom_struct]
27pub(crate) struct TestWorklet {
28    reflector: Reflector,
29    worklet: Dom<Worklet>,
30}
31
32impl TestWorklet {
33    fn new_inherited(worklet: &Worklet) -> TestWorklet {
34        TestWorklet {
35            reflector: Reflector::new(),
36            worklet: Dom::from_ref(worklet),
37        }
38    }
39
40    fn new(
41        cx: &mut JSContext,
42        window: &Window,
43        proto: Option<HandleObject>,
44    ) -> DomRoot<TestWorklet> {
45        let worklet_global_scope_init = window.into();
46        let worklet = Worklet::new(
47            cx,
48            window,
49            WorkletGlobalScopeType::Test,
50            Box::new(|| Rc::new(WorkletThreadPool::spawn(worklet_global_scope_init))),
51        );
52        reflect_dom_object_with_proto(
53            cx,
54            Box::new(TestWorklet::new_inherited(&worklet)),
55            window,
56            proto,
57        )
58    }
59}
60
61impl TestWorkletMethods<crate::DomTypeHolder> for TestWorklet {
62    fn Constructor(
63        cx: &mut JSContext,
64        window: &Window,
65        proto: Option<HandleObject>,
66    ) -> Fallible<DomRoot<TestWorklet>> {
67        Ok(TestWorklet::new(cx, window, proto))
68    }
69
70    fn AddModule(
71        &self,
72        realm: &mut CurrentRealm,
73        module_url: USVString,
74        options: &WorkletOptions,
75    ) -> Rc<Promise> {
76        self.worklet.AddModule(realm, module_url, options)
77    }
78
79    fn Lookup(&self, key: DOMString) -> Option<DOMString> {
80        let id = self.worklet.worklet_id();
81
82        self.worklet
83            .worklet_thread_pool()
84            .test_worklet_lookup(id, String::from(key))
85            .map(DOMString::from)
86    }
87}