Skip to main content

script/dom/testing/
testworkletglobalscope.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 std::collections::HashMap;
6
7use crossbeam_channel::Sender;
8use dom_struct::dom_struct;
9use js::context::JSContext;
10use script_bindings::cell::DomRefCell;
11use script_bindings::inheritance::Castable;
12use script_bindings::interfaces::HasOrigin;
13use servo_base::id::PipelineId;
14use servo_url::{MutableOrigin, ServoUrl};
15
16use crate::dom::bindings::codegen::Bindings::TestWorkletGlobalScopeBinding;
17use crate::dom::bindings::codegen::Bindings::TestWorkletGlobalScopeBinding::TestWorkletGlobalScopeMethods;
18use crate::dom::bindings::root::DomRoot;
19use crate::dom::bindings::str::DOMString;
20use crate::dom::worklet::WorkletExecutor;
21use crate::dom::workletglobalscope::{WorkletGlobalScope, WorkletGlobalScopeInit};
22
23// check-tidy: no specs after this line
24
25#[dom_struct]
26pub(crate) struct TestWorkletGlobalScope {
27    // The worklet global for this object
28    worklet_global: WorkletGlobalScope,
29    // The key/value pairs
30    lookup_table: DomRefCell<HashMap<String, String>>,
31}
32
33impl TestWorkletGlobalScope {
34    pub(crate) fn new(
35        pipeline_id: PipelineId,
36        base_url: ServoUrl,
37        inherited_secure_context: Option<bool>,
38        executor: WorkletExecutor,
39        init: &WorkletGlobalScopeInit,
40        cx: &mut JSContext,
41    ) -> DomRoot<TestWorkletGlobalScope> {
42        debug!(
43            "Creating test worklet global scope for pipeline {}.",
44            pipeline_id
45        );
46        let global = Box::new(TestWorkletGlobalScope {
47            worklet_global: WorkletGlobalScope::new_inherited(
48                pipeline_id,
49                base_url,
50                inherited_secure_context,
51                executor,
52                init,
53            ),
54            lookup_table: Default::default(),
55        });
56        TestWorkletGlobalScopeBinding::Wrap::<crate::DomTypeHolder>(cx, &global.origin(), global)
57    }
58
59    pub(crate) fn perform_a_worklet_task(&self, task: TestWorkletTask) {
60        match task {
61            TestWorkletTask::Lookup(key, sender) => {
62                debug!("Looking up key {}.", key);
63                let result = self.lookup_table.borrow().get(&key).cloned();
64                let _ = sender.send(result);
65            },
66        }
67    }
68}
69
70impl TestWorkletGlobalScopeMethods<crate::DomTypeHolder> for TestWorkletGlobalScope {
71    fn RegisterKeyValue(&self, key: DOMString, value: DOMString) {
72        debug!("Registering test worklet key/value {}/{}.", key, value);
73        self.lookup_table
74            .borrow_mut()
75            .insert(String::from(key), String::from(value));
76    }
77}
78
79/// Tasks which can be performed by test worklets.
80pub(crate) enum TestWorkletTask {
81    Lookup(String, Sender<Option<String>>),
82}
83
84impl HasOrigin for TestWorkletGlobalScope {
85    fn origin(&self) -> MutableOrigin {
86        self.upcast::<WorkletGlobalScope>().origin()
87    }
88}