1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::root::DomRoot;
use crate::dom::globalscope::GlobalScope;
use crate::dom::identityhub::Identities;
use crate::dom::paintworkletglobalscope::PaintWorkletGlobalScope;
use crate::dom::paintworkletglobalscope::PaintWorkletTask;
use crate::dom::testworkletglobalscope::TestWorkletGlobalScope;
use crate::dom::testworkletglobalscope::TestWorkletTask;
use crate::dom::worklet::WorkletExecutor;
use crate::script_module::ScriptFetchOptions;
use crate::script_runtime::JSContext;
use crate::script_thread::MainThreadScriptMsg;
use crossbeam_channel::Sender;
use devtools_traits::ScriptToDevtoolsControlMsg;
use dom_struct::dom_struct;
use ipc_channel::ipc::IpcSender;
use js::jsval::UndefinedValue;
use js::rust::Runtime;
use msg::constellation_msg::PipelineId;
use net_traits::image_cache::ImageCache;
use net_traits::ResourceThreads;
use parking_lot::Mutex;
use profile_traits::mem;
use profile_traits::time;
use script_traits::{Painter, ScriptMsg};
use script_traits::{ScriptToConstellationChan, TimerSchedulerMsg};
use servo_atoms::Atom;
use servo_url::ImmutableOrigin;
use servo_url::MutableOrigin;
use servo_url::ServoUrl;
use std::borrow::Cow;
use std::sync::Arc;
#[dom_struct]
pub struct WorkletGlobalScope {
globalscope: GlobalScope,
base_url: ServoUrl,
#[ignore_malloc_size_of = "channels are hard"]
to_script_thread_sender: Sender<MainThreadScriptMsg>,
executor: WorkletExecutor,
}
impl WorkletGlobalScope {
pub fn new_inherited(
pipeline_id: PipelineId,
base_url: ServoUrl,
executor: WorkletExecutor,
init: &WorkletGlobalScopeInit,
) -> Self {
let script_to_constellation_chan = ScriptToConstellationChan {
sender: init.to_constellation_sender.clone(),
pipeline_id,
};
Self {
globalscope: GlobalScope::new_inherited(
pipeline_id,
init.devtools_chan.clone(),
init.mem_profiler_chan.clone(),
init.time_profiler_chan.clone(),
script_to_constellation_chan,
init.scheduler_chan.clone(),
init.resource_threads.clone(),
MutableOrigin::new(ImmutableOrigin::new_opaque()),
None,
Default::default(),
init.is_headless,
init.user_agent.clone(),
init.gpu_id_hub.clone(),
init.inherited_secure_context,
),
base_url,
to_script_thread_sender: init.to_script_thread_sender.clone(),
executor,
}
}
pub fn get_cx(&self) -> JSContext {
self.globalscope.get_cx()
}
pub fn evaluate_js(&self, script: &str) -> bool {
debug!("Evaluating Dom in a worklet.");
rooted!(in (*self.globalscope.get_cx()) let mut rval = UndefinedValue());
self.globalscope.evaluate_js_on_global_with_result(
&*script,
rval.handle_mut(),
ScriptFetchOptions::default_classic_script(&self.globalscope),
self.globalscope.api_base_url(),
)
}
pub fn register_paint_worklet(
&self,
name: Atom,
properties: Vec<Atom>,
painter: Box<dyn Painter>,
) {
self.to_script_thread_sender
.send(MainThreadScriptMsg::RegisterPaintWorklet {
pipeline_id: self.globalscope.pipeline_id(),
name,
properties,
painter,
})
.expect("Worklet thread outlived script thread.");
}
pub fn base_url(&self) -> ServoUrl {
self.base_url.clone()
}
pub fn executor(&self) -> WorkletExecutor {
self.executor.clone()
}
pub fn perform_a_worklet_task(&self, task: WorkletTask) {
match task {
WorkletTask::Test(task) => match self.downcast::<TestWorkletGlobalScope>() {
Some(global) => global.perform_a_worklet_task(task),
None => warn!("This is not a test worklet."),
},
WorkletTask::Paint(task) => match self.downcast::<PaintWorkletGlobalScope>() {
Some(global) => global.perform_a_worklet_task(task),
None => warn!("This is not a paint worklet."),
},
}
}
}
#[derive(Clone)]
pub struct WorkletGlobalScopeInit {
pub to_script_thread_sender: Sender<MainThreadScriptMsg>,
pub resource_threads: ResourceThreads,
pub mem_profiler_chan: mem::ProfilerChan,
pub time_profiler_chan: time::ProfilerChan,
pub devtools_chan: Option<IpcSender<ScriptToDevtoolsControlMsg>>,
pub to_constellation_sender: IpcSender<(PipelineId, ScriptMsg)>,
pub scheduler_chan: IpcSender<TimerSchedulerMsg>,
pub image_cache: Arc<dyn ImageCache>,
pub is_headless: bool,
pub user_agent: Cow<'static, str>,
pub gpu_id_hub: Arc<Mutex<Identities>>,
pub inherited_secure_context: Option<bool>,
}
#[derive(Clone, Copy, Debug, JSTraceable, MallocSizeOf)]
pub enum WorkletGlobalScopeType {
Test,
Paint,
}
impl WorkletGlobalScopeType {
pub fn new(
&self,
runtime: &Runtime,
pipeline_id: PipelineId,
base_url: ServoUrl,
executor: WorkletExecutor,
init: &WorkletGlobalScopeInit,
) -> DomRoot<WorkletGlobalScope> {
match *self {
WorkletGlobalScopeType::Test => DomRoot::upcast(TestWorkletGlobalScope::new(
runtime,
pipeline_id,
base_url,
executor,
init,
)),
WorkletGlobalScopeType::Paint => DomRoot::upcast(PaintWorkletGlobalScope::new(
runtime,
pipeline_id,
base_url,
executor,
init,
)),
}
}
}
pub enum WorkletTask {
Test(TestWorkletTask),
Paint(PaintWorkletTask),
}