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 base::id::{PipelineId, WebViewId};
8use crossbeam_channel::Sender;
9use dom_struct::dom_struct;
10use js::context::JSContext;
11use servo_url::ServoUrl;
12
13use crate::dom::bindings::cell::DomRefCell;
14use crate::dom::bindings::codegen::Bindings::TestWorkletGlobalScopeBinding;
15use crate::dom::bindings::codegen::Bindings::TestWorkletGlobalScopeBinding::TestWorkletGlobalScopeMethods;
16use crate::dom::bindings::root::DomRoot;
17use crate::dom::bindings::str::DOMString;
18use crate::dom::worklet::WorkletExecutor;
19use crate::dom::workletglobalscope::{WorkletGlobalScope, WorkletGlobalScopeInit};
20
21// check-tidy: no specs after this line
22
23#[dom_struct]
24pub(crate) struct TestWorkletGlobalScope {
25    // The worklet global for this object
26    worklet_global: WorkletGlobalScope,
27    // The key/value pairs
28    lookup_table: DomRefCell<HashMap<String, String>>,
29}
30
31impl TestWorkletGlobalScope {
32    pub(crate) fn new(
33        webview_id: WebViewId,
34        pipeline_id: PipelineId,
35        base_url: ServoUrl,
36        inherited_secure_context: Option<bool>,
37        executor: WorkletExecutor,
38        init: &WorkletGlobalScopeInit,
39        cx: &mut JSContext,
40    ) -> DomRoot<TestWorkletGlobalScope> {
41        debug!(
42            "Creating test worklet global scope for pipeline {}.",
43            pipeline_id
44        );
45        let global = Box::new(TestWorkletGlobalScope {
46            worklet_global: WorkletGlobalScope::new_inherited(
47                webview_id,
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)
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}