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 dom_struct::dom_struct;
11use js::context::JSContext;
12use script_bindings::cell::DomRefCell;
13use script_bindings::inheritance::Castable;
14use script_bindings::interfaces::HasOrigin;
15use servo_base::id::PipelineId;
16use servo_url::{MutableOrigin, ServoUrl};
17
18use crate::dom::bindings::codegen::Bindings::TestWorkletGlobalScopeBinding;
19use crate::dom::bindings::codegen::Bindings::TestWorkletGlobalScopeBinding::TestWorkletGlobalScopeMethods;
20use crate::dom::bindings::root::DomRoot;
21use crate::dom::bindings::str::DOMString;
22use crate::dom::worklet::WorkletExecutor;
23use crate::dom::workletglobalscope::{WorkletGlobalScope, WorkletGlobalScopeInit};
24use crate::runtime::microtask::MicrotaskQueue;
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        microtask_queue: Rc<MicrotaskQueue>,
47    ) -> DomRoot<TestWorkletGlobalScope> {
48        debug!(
49            "Creating test worklet global scope for pipeline {}.",
50            pipeline_id
51        );
52
53        let global = Box::new(TestWorkletGlobalScope {
54            worklet_global: WorkletGlobalScope::new_inherited(
55                pipeline_id,
56                base_url,
57                inherited_secure_context,
58                executor,
59                init,
60                closing,
61                microtask_queue,
62            ),
63            lookup_table: Default::default(),
64        });
65        TestWorkletGlobalScopeBinding::Wrap::<crate::DomTypeHolder>(cx, &global.origin(), global)
66    }
67
68    /// Get the value associated with the given key.
69    pub fn lookup_value(&self, key: &str) -> Option<String> {
70        self.lookup_table.borrow().get(key).cloned()
71    }
72}
73
74impl TestWorkletGlobalScopeMethods<crate::DomTypeHolder> for TestWorkletGlobalScope {
75    fn RegisterKeyValue(&self, key: DOMString, value: DOMString) {
76        debug!("Registering test worklet key/value {}/{}.", key, value);
77        self.lookup_table
78            .borrow_mut()
79            .insert(String::from(key), String::from(value));
80    }
81}
82
83impl HasOrigin for TestWorkletGlobalScope {
84    fn origin(&self) -> MutableOrigin {
85        self.upcast::<WorkletGlobalScope>().origin()
86    }
87}