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 servo_url::ServoUrl;
11
12use crate::dom::bindings::cell::DomRefCell;
13use crate::dom::bindings::codegen::Bindings::TestWorkletGlobalScopeBinding;
14use crate::dom::bindings::codegen::Bindings::TestWorkletGlobalScopeBinding::TestWorkletGlobalScopeMethods;
15use crate::dom::bindings::root::DomRoot;
16use crate::dom::bindings::str::DOMString;
17use crate::dom::globalscope::GlobalScope;
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    ) -> DomRoot<TestWorkletGlobalScope> {
40        debug!(
41            "Creating test worklet global scope for pipeline {}.",
42            pipeline_id
43        );
44        let global = Box::new(TestWorkletGlobalScope {
45            worklet_global: WorkletGlobalScope::new_inherited(
46                webview_id,
47                pipeline_id,
48                base_url,
49                inherited_secure_context,
50                executor,
51                init,
52            ),
53            lookup_table: Default::default(),
54        });
55        TestWorkletGlobalScopeBinding::Wrap::<crate::DomTypeHolder>(GlobalScope::get_cx(), global)
56    }
57
58    pub(crate) fn perform_a_worklet_task(&self, task: TestWorkletTask) {
59        match task {
60            TestWorkletTask::Lookup(key, sender) => {
61                debug!("Looking up key {}.", key);
62                let result = self.lookup_table.borrow().get(&key).cloned();
63                let _ = sender.send(result);
64            },
65        }
66    }
67}
68
69impl TestWorkletGlobalScopeMethods<crate::DomTypeHolder> for TestWorkletGlobalScope {
70    fn RegisterKeyValue(&self, key: DOMString, value: DOMString) {
71        debug!("Registering test worklet key/value {}/{}.", key, value);
72        self.lookup_table
73            .borrow_mut()
74            .insert(String::from(key), String::from(value));
75    }
76}
77
78/// Tasks which can be performed by test worklets.
79pub(crate) enum TestWorkletTask {
80    Lookup(String, Sender<Option<String>>),
81}