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
5#![cfg_attr(crown, allow(crown::jscontext_first_arg))]
6
7use std::collections::HashMap;
8use std::sync::Arc;
9use std::sync::atomic::AtomicBool;
10
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};
25
26// check-tidy: no specs after this line
27
28#[dom_struct]
29pub(crate) struct TestWorkletGlobalScope {
30    // The worklet global for this object
31    worklet_global: WorkletGlobalScope,
32    // The key/value pairs
33    lookup_table: DomRefCell<HashMap<String, String>>,
34}
35
36impl TestWorkletGlobalScope {
37    #[allow(clippy::too_many_arguments)]
38    pub(crate) fn new(
39        pipeline_id: PipelineId,
40        base_url: ServoUrl,
41        inherited_secure_context: Option<bool>,
42        executor: WorkletExecutor,
43        init: &WorkletGlobalScopeInit,
44        cx: &mut JSContext,
45        closing: Arc<AtomicBool>,
46    ) -> DomRoot<TestWorkletGlobalScope> {
47        debug!(
48            "Creating test worklet global scope for pipeline {}.",
49            pipeline_id
50        );
51
52        let global = Box::new(TestWorkletGlobalScope {
53            worklet_global: WorkletGlobalScope::new_inherited(
54                pipeline_id,
55                base_url,
56                inherited_secure_context,
57                executor,
58                init,
59                closing,
60            ),
61            lookup_table: Default::default(),
62        });
63        TestWorkletGlobalScopeBinding::Wrap::<crate::DomTypeHolder>(cx, &global.origin(), global)
64    }
65
66    /// Get the value associated with the given key.
67    pub fn lookup_value(&self, key: &str) -> Option<String> {
68        self.lookup_table.borrow().get(key).cloned()
69    }
70}
71
72impl TestWorkletGlobalScopeMethods<crate::DomTypeHolder> for TestWorkletGlobalScope {
73    fn RegisterKeyValue(&self, key: DOMString, value: DOMString) {
74        debug!("Registering test worklet key/value {}/{}.", key, value);
75        self.lookup_table
76            .borrow_mut()
77            .insert(String::from(key), String::from(value));
78    }
79}
80
81impl HasOrigin for TestWorkletGlobalScope {
82    fn origin(&self) -> MutableOrigin {
83        self.upcast::<WorkletGlobalScope>().origin()
84    }
85}