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