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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use std::cell::RefCell;
use std::option::Option;
use std::result::Result;

use base::id::PipelineId;
use bluetooth_traits::BluetoothRequest;
use crossbeam_channel::{select, Receiver, Sender};
use devtools_traits::{DevtoolScriptControlMsg, ScriptToDevtoolsControlMsg};
use ipc_channel::ipc::IpcSender;
use net_traits::image_cache::PendingImageResponse;
use profile_traits::mem::{self as profile_mem, OpaqueSender};
use profile_traits::time::{self as profile_time};
use script_traits::{ConstellationControlMsg, LayoutMsg, Painter, ScriptMsg};
use servo_atoms::Atom;
use timers::TimerScheduler;
#[cfg(feature = "webgpu")]
use webgpu::WebGPUMsg;

use crate::dom::serviceworker::TrustedServiceWorkerAddress;
use crate::dom::worker::TrustedWorkerAddress;
use crate::script_runtime::{CommonScriptMsg, ScriptChan, ScriptPort};
use crate::task_queue::{QueuedTask, QueuedTaskConversion, TaskQueue};
use crate::task_source::TaskSourceName;

pub(crate) type ImageCacheMsg = (PipelineId, PendingImageResponse);

#[derive(Debug)]
pub(crate) enum MixedMessage {
    FromConstellation(ConstellationControlMsg),
    FromScript(MainThreadScriptMsg),
    FromDevtools(DevtoolScriptControlMsg),
    FromImageCache((PipelineId, PendingImageResponse)),
    #[cfg(feature = "webgpu")]
    FromWebGPUServer(WebGPUMsg),
    TimerFired,
}

impl MixedMessage {
    pub(crate) fn pipeline_id(&self) -> Option<PipelineId> {
        match self {
            MixedMessage::FromConstellation(ref inner_msg) => match *inner_msg {
                ConstellationControlMsg::StopDelayingLoadEventsMode(id) => Some(id),
                ConstellationControlMsg::NavigationResponse(id, _) => Some(id),
                ConstellationControlMsg::AttachLayout(ref new_layout_info) => new_layout_info
                    .parent_info
                    .or(Some(new_layout_info.new_pipeline_id)),
                ConstellationControlMsg::Resize(id, ..) => Some(id),
                ConstellationControlMsg::ThemeChange(id, ..) => Some(id),
                ConstellationControlMsg::ResizeInactive(id, ..) => Some(id),
                ConstellationControlMsg::UnloadDocument(id) => Some(id),
                ConstellationControlMsg::ExitPipeline(id, ..) => Some(id),
                ConstellationControlMsg::ExitScriptThread => None,
                ConstellationControlMsg::SendEvent(id, ..) => Some(id),
                ConstellationControlMsg::Viewport(id, ..) => Some(id),
                ConstellationControlMsg::GetTitle(id) => Some(id),
                ConstellationControlMsg::SetDocumentActivity(id, ..) => Some(id),
                ConstellationControlMsg::SetThrottled(id, ..) => Some(id),
                ConstellationControlMsg::SetThrottledInContainingIframe(id, ..) => Some(id),
                ConstellationControlMsg::NavigateIframe(id, ..) => Some(id),
                ConstellationControlMsg::PostMessage { target: id, .. } => Some(id),
                ConstellationControlMsg::UpdatePipelineId(_, _, _, id, _) => Some(id),
                ConstellationControlMsg::UpdateHistoryState(id, ..) => Some(id),
                ConstellationControlMsg::RemoveHistoryStates(id, ..) => Some(id),
                ConstellationControlMsg::FocusIFrame(id, ..) => Some(id),
                ConstellationControlMsg::WebDriverScriptCommand(id, ..) => Some(id),
                ConstellationControlMsg::TickAllAnimations(id, ..) => Some(id),
                ConstellationControlMsg::WebFontLoaded(id, ..) => Some(id),
                ConstellationControlMsg::DispatchIFrameLoadEvent {
                    target: _,
                    parent: id,
                    child: _,
                } => Some(id),
                ConstellationControlMsg::DispatchStorageEvent(id, ..) => Some(id),
                ConstellationControlMsg::ReportCSSError(id, ..) => Some(id),
                ConstellationControlMsg::Reload(id, ..) => Some(id),
                ConstellationControlMsg::PaintMetric(id, ..) => Some(id),
                ConstellationControlMsg::ExitFullScreen(id, ..) => Some(id),
                ConstellationControlMsg::MediaSessionAction(..) => None,
                #[cfg(feature = "webgpu")]
                ConstellationControlMsg::SetWebGPUPort(..) => None,
                ConstellationControlMsg::SetScrollStates(id, ..) => Some(id),
                ConstellationControlMsg::SetEpochPaintTime(id, ..) => Some(id),
            },
            MixedMessage::FromScript(ref inner_msg) => match *inner_msg {
                MainThreadScriptMsg::Common(CommonScriptMsg::Task(_, _, pipeline_id, _)) => {
                    pipeline_id
                },
                MainThreadScriptMsg::Common(CommonScriptMsg::CollectReports(_)) => None,
                MainThreadScriptMsg::WorkletLoaded(pipeline_id) => Some(pipeline_id),
                MainThreadScriptMsg::RegisterPaintWorklet { pipeline_id, .. } => Some(pipeline_id),
                MainThreadScriptMsg::Inactive => None,
                MainThreadScriptMsg::WakeUp => None,
            },
            MixedMessage::FromImageCache((pipeline_id, _)) => Some(*pipeline_id),
            MixedMessage::FromDevtools(_) | MixedMessage::TimerFired => None,
            #[cfg(feature = "webgpu")]
            MixedMessage::FromWebGPUServer(..) => None,
        }
    }
}

/// Messages used to control the script event loop.
#[derive(Debug)]
pub(crate) enum MainThreadScriptMsg {
    /// Common variants associated with the script messages
    Common(CommonScriptMsg),
    /// Notifies the script thread that a new worklet has been loaded, and thus the page should be
    /// reflowed.
    WorkletLoaded(PipelineId),
    /// Notifies the script thread that a new paint worklet has been registered.
    RegisterPaintWorklet {
        pipeline_id: PipelineId,
        name: Atom,
        properties: Vec<Atom>,
        painter: Box<dyn Painter>,
    },
    /// A task related to a not fully-active document has been throttled.
    Inactive,
    /// Wake-up call from the task queue.
    WakeUp,
}

impl QueuedTaskConversion for MainThreadScriptMsg {
    fn task_source_name(&self) -> Option<&TaskSourceName> {
        let script_msg = match self {
            MainThreadScriptMsg::Common(script_msg) => script_msg,
            _ => return None,
        };
        match script_msg {
            CommonScriptMsg::Task(_category, _boxed, _pipeline_id, task_source) => {
                Some(task_source)
            },
            _ => None,
        }
    }

    fn pipeline_id(&self) -> Option<PipelineId> {
        let script_msg = match self {
            MainThreadScriptMsg::Common(script_msg) => script_msg,
            _ => return None,
        };
        match script_msg {
            CommonScriptMsg::Task(_category, _boxed, pipeline_id, _task_source) => *pipeline_id,
            _ => None,
        }
    }

    fn into_queued_task(self) -> Option<QueuedTask> {
        let script_msg = match self {
            MainThreadScriptMsg::Common(script_msg) => script_msg,
            _ => return None,
        };
        let (category, boxed, pipeline_id, task_source) = match script_msg {
            CommonScriptMsg::Task(category, boxed, pipeline_id, task_source) => {
                (category, boxed, pipeline_id, task_source)
            },
            _ => return None,
        };
        Some((None, category, boxed, pipeline_id, task_source))
    }

    fn from_queued_task(queued_task: QueuedTask) -> Self {
        let (_worker, category, boxed, pipeline_id, task_source) = queued_task;
        let script_msg = CommonScriptMsg::Task(category, boxed, pipeline_id, task_source);
        MainThreadScriptMsg::Common(script_msg)
    }

    fn inactive_msg() -> Self {
        MainThreadScriptMsg::Inactive
    }

    fn wake_up_msg() -> Self {
        MainThreadScriptMsg::WakeUp
    }

    fn is_wake_up(&self) -> bool {
        matches!(self, MainThreadScriptMsg::WakeUp)
    }
}

impl OpaqueSender<CommonScriptMsg> for Box<dyn ScriptChan + Send> {
    fn send(&self, msg: CommonScriptMsg) {
        ScriptChan::send(&**self, msg).unwrap();
    }
}

impl ScriptPort for Receiver<CommonScriptMsg> {
    fn recv(&self) -> Result<CommonScriptMsg, ()> {
        self.recv().map_err(|_| ())
    }
}

impl ScriptPort for Receiver<MainThreadScriptMsg> {
    fn recv(&self) -> Result<CommonScriptMsg, ()> {
        match self.recv() {
            Ok(MainThreadScriptMsg::Common(script_msg)) => Ok(script_msg),
            Ok(_) => panic!("unexpected main thread event message!"),
            Err(_) => Err(()),
        }
    }
}

impl ScriptPort for Receiver<(TrustedWorkerAddress, CommonScriptMsg)> {
    fn recv(&self) -> Result<CommonScriptMsg, ()> {
        self.recv().map(|(_, msg)| msg).map_err(|_| ())
    }
}

impl ScriptPort for Receiver<(TrustedWorkerAddress, MainThreadScriptMsg)> {
    fn recv(&self) -> Result<CommonScriptMsg, ()> {
        match self.recv().map(|(_, msg)| msg) {
            Ok(MainThreadScriptMsg::Common(script_msg)) => Ok(script_msg),
            Ok(_) => panic!("unexpected main thread event message!"),
            Err(_) => Err(()),
        }
    }
}

impl ScriptPort for Receiver<(TrustedServiceWorkerAddress, CommonScriptMsg)> {
    fn recv(&self) -> Result<CommonScriptMsg, ()> {
        self.recv().map(|(_, msg)| msg).map_err(|_| ())
    }
}

/// Encapsulates internal communication of shared messages within the script thread.
#[derive(Clone, JSTraceable)]
pub(crate) struct SendableMainThreadScriptChan(#[no_trace] pub Sender<CommonScriptMsg>);

impl ScriptChan for SendableMainThreadScriptChan {
    fn send(&self, msg: CommonScriptMsg) -> Result<(), ()> {
        self.0.send(msg).map_err(|_| ())
    }

    fn as_boxed(&self) -> Box<dyn ScriptChan + Send> {
        Box::new(SendableMainThreadScriptChan((self.0).clone()))
    }
}

/// Encapsulates internal communication of main thread messages within the script thread.
#[derive(Clone, JSTraceable)]
pub(crate) struct MainThreadScriptChan(#[no_trace] pub Sender<MainThreadScriptMsg>);

impl ScriptChan for MainThreadScriptChan {
    fn send(&self, msg: CommonScriptMsg) -> Result<(), ()> {
        self.0
            .send(MainThreadScriptMsg::Common(msg))
            .map_err(|_| ())
    }

    fn as_boxed(&self) -> Box<dyn ScriptChan + Send> {
        Box::new(MainThreadScriptChan((self.0).clone()))
    }
}

impl OpaqueSender<CommonScriptMsg> for Sender<MainThreadScriptMsg> {
    fn send(&self, msg: CommonScriptMsg) {
        self.send(MainThreadScriptMsg::Common(msg)).unwrap()
    }
}

#[derive(Clone, JSTraceable)]
pub(crate) struct ScriptThreadSenders {
    /// A channel to hand out to script thread-based entities that need to be able to enqueue
    /// events in the event queue.
    pub self_sender: MainThreadScriptChan,

    /// A handle to the bluetooth thread.
    #[no_trace]
    pub bluetooth_sender: IpcSender<BluetoothRequest>,

    /// A [`Sender`] that sends messages to the `Constellation`.
    #[no_trace]
    pub constellation_sender: IpcSender<ConstellationControlMsg>,

    /// A [`Sender`] that sends messages to the `Constellation` associated with
    /// particular pipelines.
    #[no_trace]
    pub pipeline_to_constellation_sender: IpcSender<(PipelineId, ScriptMsg)>,

    /// A sender for layout to communicate to the constellation.
    #[no_trace]
    pub layout_to_constellation_ipc_sender: IpcSender<LayoutMsg>,

    /// The [`Sender`] on which messages can be sent to the `ImageCache`.
    #[no_trace]
    pub image_cache_sender: Sender<ImageCacheMsg>,

    /// For providing contact with the time profiler.
    #[no_trace]
    pub time_profiler_sender: profile_time::ProfilerChan,

    /// For providing contact with the memory profiler.
    #[no_trace]
    pub memory_profiler_sender: profile_mem::ProfilerChan,

    /// For providing instructions to an optional devtools server.
    #[no_trace]
    pub devtools_server_sender: Option<IpcSender<ScriptToDevtoolsControlMsg>>,

    #[no_trace]
    pub devtools_client_to_script_thread_sender: IpcSender<DevtoolScriptControlMsg>,

    #[no_trace]
    pub content_process_shutdown_sender: Sender<()>,
}

#[derive(JSTraceable)]
pub(crate) struct ScriptThreadReceivers {
    /// A [`Receiver`] that receives messages from the constellation.
    #[no_trace]
    pub constellation_receiver: Receiver<ConstellationControlMsg>,

    /// The [`Receiver`] which receives incoming messages from the `ImageCache`.
    #[no_trace]
    pub image_cache_receiver: Receiver<ImageCacheMsg>,

    /// For receiving commands from an optional devtools server. Will be ignored if
    /// no such server exists.
    #[no_trace]
    pub devtools_server_receiver: Receiver<DevtoolScriptControlMsg>,

    /// Receiver to receive commands from optional WebGPU server.
    #[no_trace]
    #[cfg(feature = "webgpu")]
    pub webgpu_receiver: RefCell<Receiver<WebGPUMsg>>,
}

impl ScriptThreadReceivers {
    /// Block until a message is received by any of the receivers of this [`ScriptThreadReceivers`]
    /// or the given [`TaskQueue`] or [`TimerScheduler`]. Return the first message received.
    pub(crate) fn recv(
        &self,
        task_queue: &TaskQueue<MainThreadScriptMsg>,
        timer_scheduler: &TimerScheduler,
    ) -> MixedMessage {
        select! {
            recv(task_queue.select()) -> msg => {
                task_queue.take_tasks(msg.unwrap());
                let event = task_queue
                    .recv()
                    .expect("Spurious wake-up of the event-loop, task-queue has no tasks available");
                MixedMessage::FromScript(event)
            },
            recv(self.constellation_receiver) -> msg => MixedMessage::FromConstellation(msg.unwrap()),
            recv(self.devtools_server_receiver) -> msg => MixedMessage::FromDevtools(msg.unwrap()),
            recv(self.image_cache_receiver) -> msg => MixedMessage::FromImageCache(msg.unwrap()),
            recv(timer_scheduler.wait_channel()) -> _ => MixedMessage::TimerFired,
            recv({
                #[cfg(feature = "webgpu")]
                {
                    self.webgpu_receiver.borrow()
                }
                #[cfg(not(feature = "webgpu"))]
                {
                    &crossbeam_channel::never::<()>()
                }
            }) -> msg => {
                #[cfg(feature = "webgpu")]
                {
                    MixedMessage::FromWebGPUServer(msg.unwrap())
                }
                #[cfg(not(feature = "webgpu"))]
                {
                    unreachable!("This should never be hit when webgpu is disabled");
                }
            }
        }
    }

    /// Try to receive a from any of the receivers of this [`ScriptThreadReceivers`] or the given
    /// [`TaskQueue`]. Return `None` if no messages are ready to be received.
    pub(crate) fn try_recv(
        &self,
        task_queue: &TaskQueue<MainThreadScriptMsg>,
    ) -> Option<MixedMessage> {
        if let Ok(message) = self.constellation_receiver.try_recv() {
            return MixedMessage::FromConstellation(message).into();
        }
        if let Ok(message) = task_queue.take_tasks_and_recv() {
            return MixedMessage::FromScript(message).into();
        }
        if let Ok(message) = self.devtools_server_receiver.try_recv() {
            return MixedMessage::FromDevtools(message).into();
        }
        if let Ok(message) = self.image_cache_receiver.try_recv() {
            return MixedMessage::FromImageCache(message).into();
        }
        #[cfg(feature = "webgpu")]
        if let Ok(message) = self.webgpu_receiver.borrow().try_recv() {
            return MixedMessage::FromWebGPUServer(message).into();
        }
        None
    }
}