script/
timers.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::cell::Cell;
6use std::cmp::{Ord, Ordering};
7use std::collections::VecDeque;
8use std::default::Default;
9use std::rc::Rc;
10use std::time::{Duration, Instant};
11
12use base::id::PipelineId;
13use deny_public_fields::DenyPublicFields;
14use js::jsapi::Heap;
15use js::jsval::JSVal;
16use js::rust::HandleValue;
17use rustc_hash::FxHashMap;
18use serde::{Deserialize, Serialize};
19use servo_config::pref;
20use timers::{BoxedTimerCallback, TimerEventRequest};
21
22use crate::dom::bindings::callback::ExceptionHandling::Report;
23use crate::dom::bindings::cell::DomRefCell;
24use crate::dom::bindings::codegen::Bindings::FunctionBinding::Function;
25use crate::dom::bindings::codegen::UnionTypes::TrustedScriptOrString;
26use crate::dom::bindings::error::Fallible;
27use crate::dom::bindings::inheritance::Castable;
28use crate::dom::bindings::refcounted::Trusted;
29use crate::dom::bindings::reflector::{DomGlobal, DomObject};
30use crate::dom::bindings::root::{AsHandleValue, Dom};
31use crate::dom::bindings::str::DOMString;
32use crate::dom::csp::CspReporting;
33use crate::dom::document::RefreshRedirectDue;
34use crate::dom::eventsource::EventSourceTimeoutCallback;
35use crate::dom::global_scope_script_execution::{ErrorReporting, RethrowErrors};
36use crate::dom::globalscope::GlobalScope;
37#[cfg(feature = "testbinding")]
38use crate::dom::testbinding::TestBindingCallback;
39use crate::dom::trustedscript::TrustedScript;
40use crate::dom::types::{Window, WorkerGlobalScope};
41use crate::dom::xmlhttprequest::XHRTimeoutCallback;
42use crate::script_module::ScriptFetchOptions;
43use crate::script_runtime::{CanGc, IntroductionType};
44use crate::script_thread::ScriptThread;
45use crate::task_source::SendableTaskSource;
46
47type TimerKey = i32;
48type RunStepsDeadline = Instant;
49type CompletionStep = Box<dyn FnOnce(&GlobalScope, CanGc) + 'static>;
50
51/// <https://html.spec.whatwg.org/multipage/#run-steps-after-a-timeout>
52/// OrderingIdentifier per spec ("orderingIdentifier")
53type OrderingIdentifier = DOMString;
54
55#[derive(JSTraceable, MallocSizeOf)]
56struct OrderingEntry {
57    milliseconds: u64,
58    start_seq: u64,
59    handle: OneshotTimerHandle,
60}
61
62// Per-ordering queues map
63type OrderingQueues = FxHashMap<OrderingIdentifier, Vec<OrderingEntry>>;
64
65// Active timers map for Run Steps After A Timeout
66type RunStepsActiveMap = FxHashMap<TimerKey, RunStepsDeadline>;
67
68#[derive(Clone, Copy, Debug, Eq, Hash, JSTraceable, MallocSizeOf, Ord, PartialEq, PartialOrd)]
69pub(crate) struct OneshotTimerHandle(i32);
70
71#[derive(DenyPublicFields, JSTraceable, MallocSizeOf)]
72#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
73pub(crate) struct OneshotTimers {
74    global_scope: Dom<GlobalScope>,
75    js_timers: JsTimers,
76    next_timer_handle: Cell<OneshotTimerHandle>,
77    timers: DomRefCell<VecDeque<OneshotTimer>>,
78    suspended_since: Cell<Option<Instant>>,
79    /// Initially 0, increased whenever the associated document is reactivated
80    /// by the amount of ms the document was inactive. The current time can be
81    /// offset back by this amount for a coherent time across document
82    /// activations.
83    suspension_offset: Cell<Duration>,
84    /// Calls to `fire_timer` with a different argument than this get ignored.
85    /// They were previously scheduled and got invalidated when
86    ///  - timers were suspended,
87    ///  - the timer it was scheduled for got canceled or
88    ///  - a timer was added with an earlier callback time. In this case the
89    ///    original timer is rescheduled when it is the next one to get called.
90    #[no_trace]
91    expected_event_id: Cell<TimerEventId>,
92    /// <https://html.spec.whatwg.org/multipage/#map-of-active-timers>
93    /// TODO this should also be used for the other timers
94    /// as per <html.spec.whatwg.org/multipage/#map-of-settimeout-and-setinterval-ids>Z.
95    map_of_active_timers: DomRefCell<RunStepsActiveMap>,
96
97    /// <https://html.spec.whatwg.org/multipage/#run-steps-after-a-timeout>
98    /// Step 4.2 Wait until any invocations of this algorithm that had the same global and orderingIdentifier,
99    /// that started before this one, and whose milliseconds is less than or equal to this one's, have completed.
100    runsteps_queues: DomRefCell<OrderingQueues>,
101
102    /// <html.spec.whatwg.org/multipage/#timers:unique-internal-value-5>
103    next_runsteps_key: Cell<TimerKey>,
104
105    /// <https://html.spec.whatwg.org/multipage/#run-steps-after-a-timeout>
106    /// Start order sequence to break ties for Step 4.2.
107    runsteps_start_seq: Cell<u64>,
108}
109
110#[derive(DenyPublicFields, JSTraceable, MallocSizeOf)]
111struct OneshotTimer {
112    handle: OneshotTimerHandle,
113    #[no_trace]
114    source: TimerSource,
115    callback: OneshotTimerCallback,
116    scheduled_for: Instant,
117}
118
119// This enum is required to work around the fact that trait objects do not support generic methods.
120// A replacement trait would have a method such as
121//     `invoke<T: DomObject>(self: Box<Self>, this: &T, js_timers: &JsTimers);`.
122#[derive(JSTraceable, MallocSizeOf)]
123pub(crate) enum OneshotTimerCallback {
124    XhrTimeout(XHRTimeoutCallback),
125    EventSourceTimeout(EventSourceTimeoutCallback),
126    JsTimer(JsTimerTask),
127    #[cfg(feature = "testbinding")]
128    TestBindingCallback(TestBindingCallback),
129    RefreshRedirectDue(RefreshRedirectDue),
130    /// <https://html.spec.whatwg.org/multipage/#run-steps-after-a-timeout>
131    RunStepsAfterTimeout {
132        /// Step 1. timerKey
133        timer_key: i32,
134        /// Step 4. orderingIdentifier
135        ordering_id: DOMString,
136        /// Spec: milliseconds (the algorithm input)
137        milliseconds: u64,
138        /// Perform completionSteps.
139        #[no_trace]
140        #[ignore_malloc_size_of = "Closure"]
141        completion: CompletionStep,
142    },
143}
144
145impl OneshotTimerCallback {
146    fn invoke<T: DomObject>(self, this: &T, js_timers: &JsTimers, can_gc: CanGc) {
147        match self {
148            OneshotTimerCallback::XhrTimeout(callback) => callback.invoke(can_gc),
149            OneshotTimerCallback::EventSourceTimeout(callback) => callback.invoke(),
150            OneshotTimerCallback::JsTimer(task) => task.invoke(this, js_timers, can_gc),
151            #[cfg(feature = "testbinding")]
152            OneshotTimerCallback::TestBindingCallback(callback) => callback.invoke(),
153            OneshotTimerCallback::RefreshRedirectDue(callback) => callback.invoke(can_gc),
154            OneshotTimerCallback::RunStepsAfterTimeout { completion, .. } => {
155                // <https://html.spec.whatwg.org/multipage/#run-steps-after-a-timeout>
156                // Step 4.4 Perform completionSteps.
157                completion(&this.global(), can_gc);
158            },
159        }
160    }
161}
162
163impl Ord for OneshotTimer {
164    fn cmp(&self, other: &OneshotTimer) -> Ordering {
165        match self.scheduled_for.cmp(&other.scheduled_for).reverse() {
166            Ordering::Equal => self.handle.cmp(&other.handle).reverse(),
167            res => res,
168        }
169    }
170}
171
172impl PartialOrd for OneshotTimer {
173    fn partial_cmp(&self, other: &OneshotTimer) -> Option<Ordering> {
174        Some(self.cmp(other))
175    }
176}
177
178impl Eq for OneshotTimer {}
179impl PartialEq for OneshotTimer {
180    fn eq(&self, other: &OneshotTimer) -> bool {
181        std::ptr::eq(self, other)
182    }
183}
184
185impl OneshotTimers {
186    pub(crate) fn new(global_scope: &GlobalScope) -> OneshotTimers {
187        OneshotTimers {
188            global_scope: Dom::from_ref(global_scope),
189            js_timers: JsTimers::default(),
190            next_timer_handle: Cell::new(OneshotTimerHandle(1)),
191            timers: DomRefCell::new(VecDeque::new()),
192            suspended_since: Cell::new(None),
193            suspension_offset: Cell::new(Duration::ZERO),
194            expected_event_id: Cell::new(TimerEventId(0)),
195            map_of_active_timers: Default::default(),
196            runsteps_queues: Default::default(),
197            next_runsteps_key: Cell::new(1),
198            runsteps_start_seq: Cell::new(0),
199        }
200    }
201
202    /// <https://html.spec.whatwg.org/multipage/#run-steps-after-a-timeout>
203    #[inline]
204    pub(crate) fn now_for_runsteps(&self) -> Instant {
205        // Step 2. Let startTime be the current high resolution time given global.
206        self.base_time()
207    }
208
209    /// <https://html.spec.whatwg.org/multipage/#run-steps-after-a-timeout>
210    /// Step 1. Let timerKey be a new unique internal value.
211    pub(crate) fn fresh_runsteps_key(&self) -> TimerKey {
212        let k = self.next_runsteps_key.get();
213        self.next_runsteps_key.set(k + 1);
214        k
215    }
216
217    /// <https://html.spec.whatwg.org/multipage/#run-steps-after-a-timeout>
218    /// Step 3. Set global's map of active timers[timerKey] to startTime plus milliseconds.
219    pub(crate) fn runsteps_set_active(&self, timer_key: TimerKey, deadline: RunStepsDeadline) {
220        self.map_of_active_timers
221            .borrow_mut()
222            .insert(timer_key, deadline);
223    }
224
225    /// <https://html.spec.whatwg.org/multipage/#run-steps-after-a-timeout>
226    /// Helper for Step 4.2: maintain per-ordering sorted queue by (milliseconds, startSeq, handle).
227    fn runsteps_enqueue_sorted(
228        &self,
229        ordering_id: &DOMString,
230        handle: OneshotTimerHandle,
231        milliseconds: u64,
232    ) {
233        let mut map = self.runsteps_queues.borrow_mut();
234        let q = map.entry(ordering_id.clone()).or_default();
235
236        let seq = {
237            let cur = self.runsteps_start_seq.get();
238            self.runsteps_start_seq.set(cur + 1);
239            cur
240        };
241
242        let key = OrderingEntry {
243            milliseconds,
244            start_seq: seq,
245            handle,
246        };
247
248        let idx = q
249            .binary_search_by(|ordering_entry| {
250                match ordering_entry.milliseconds.cmp(&milliseconds) {
251                    Ordering::Less => Ordering::Less,
252                    Ordering::Greater => Ordering::Greater,
253                    Ordering::Equal => ordering_entry.start_seq.cmp(&seq),
254                }
255            })
256            .unwrap_or_else(|i| i);
257
258        q.insert(idx, key);
259    }
260
261    pub(crate) fn schedule_callback(
262        &self,
263        callback: OneshotTimerCallback,
264        duration: Duration,
265        source: TimerSource,
266    ) -> OneshotTimerHandle {
267        let new_handle = self.next_timer_handle.get();
268        self.next_timer_handle
269            .set(OneshotTimerHandle(new_handle.0 + 1));
270
271        let timer = OneshotTimer {
272            handle: new_handle,
273            source,
274            callback,
275            scheduled_for: self.base_time() + duration,
276        };
277
278        // https://html.spec.whatwg.org/multipage/#run-steps-after-a-timeout
279        // Step 4.2: maintain per-orderingIdentifier order by milliseconds (and start order for ties).
280        if let OneshotTimerCallback::RunStepsAfterTimeout {
281            ordering_id,
282            milliseconds,
283            ..
284        } = &timer.callback
285        {
286            self.runsteps_enqueue_sorted(ordering_id, new_handle, *milliseconds);
287        }
288
289        {
290            let mut timers = self.timers.borrow_mut();
291            let insertion_index = timers.binary_search(&timer).err().unwrap();
292            timers.insert(insertion_index, timer);
293        }
294
295        if self.is_next_timer(new_handle) {
296            self.schedule_timer_call();
297        }
298
299        new_handle
300    }
301
302    pub(crate) fn unschedule_callback(&self, handle: OneshotTimerHandle) {
303        let was_next = self.is_next_timer(handle);
304
305        self.timers.borrow_mut().retain(|t| t.handle != handle);
306
307        if was_next {
308            self.invalidate_expected_event_id();
309            self.schedule_timer_call();
310        }
311    }
312
313    fn is_next_timer(&self, handle: OneshotTimerHandle) -> bool {
314        match self.timers.borrow().back() {
315            None => false,
316            Some(max_timer) => max_timer.handle == handle,
317        }
318    }
319
320    /// <https://html.spec.whatwg.org/multipage/#timer-initialisation-steps>
321    pub(crate) fn fire_timer(&self, id: TimerEventId, global: &GlobalScope, can_gc: CanGc) {
322        // Step 9.2. If id does not exist in global's map of setTimeout and setInterval IDs, then abort these steps.
323        let expected_id = self.expected_event_id.get();
324        if expected_id != id {
325            debug!(
326                "ignoring timer fire event {:?} (expected {:?})",
327                id, expected_id
328            );
329            return;
330        }
331
332        assert!(self.suspended_since.get().is_none());
333
334        let base_time = self.base_time();
335
336        // Since the event id was the expected one, at least one timer should be due.
337        if base_time < self.timers.borrow().back().unwrap().scheduled_for {
338            warn!("Unexpected timing!");
339            return;
340        }
341
342        // select timers to run to prevent firing timers
343        // that were installed during fire of another timer
344        let mut timers_to_run = Vec::new();
345
346        loop {
347            let mut timers = self.timers.borrow_mut();
348
349            if timers.is_empty() || timers.back().unwrap().scheduled_for > base_time {
350                break;
351            }
352
353            timers_to_run.push(timers.pop_back().unwrap());
354        }
355
356        for timer in timers_to_run {
357            // Since timers can be coalesced together inside a task,
358            // this loop can keep running, including after an interrupt of the JS,
359            // and prevent a clean-shutdown of a JS-running thread.
360            // This check prevents such a situation.
361            if !global.can_continue_running() {
362                return;
363            }
364            match &timer.callback {
365                // TODO: https://github.com/servo/servo/issues/40060
366                OneshotTimerCallback::RunStepsAfterTimeout { ordering_id, .. } => {
367                    // Step 4.2 Wait until any invocations of this algorithm that had the same global and orderingIdentifier,
368                    // that started before this one, and whose milliseconds is less than or equal to this one's, have completed.
369                    let head_handle_opt = {
370                        let queues_ref = self.runsteps_queues.borrow();
371                        queues_ref
372                            .get(ordering_id)
373                            .and_then(|v| v.first().map(|t| t.handle))
374                    };
375                    let is_head = head_handle_opt.is_none_or(|head| head == timer.handle);
376
377                    if !is_head {
378                        // TODO: this re queuing would go away when we revisit timers implementation.
379                        let rein = OneshotTimer {
380                            handle: timer.handle,
381                            source: timer.source,
382                            callback: timer.callback,
383                            scheduled_for: self.base_time(),
384                        };
385                        let mut timers = self.timers.borrow_mut();
386                        let idx = timers.binary_search(&rein).err().unwrap();
387                        timers.insert(idx, rein);
388                        continue;
389                    }
390
391                    let (timer_key, ordering_id_owned, completion) = match timer.callback {
392                        OneshotTimerCallback::RunStepsAfterTimeout {
393                            timer_key,
394                            ordering_id,
395                            milliseconds: _,
396                            completion,
397                        } => (timer_key, ordering_id, completion),
398                        _ => unreachable!(),
399                    };
400
401                    // Step 4.3 Optionally, wait a further implementation-defined length of time.
402                    // (No additional delay applied.)
403
404                    // Step 4.4 Perform completionSteps.
405                    (completion)(global, can_gc);
406
407                    // Step 4.5 Remove global's map of active timers[timerKey].
408                    self.map_of_active_timers.borrow_mut().remove(&timer_key);
409
410                    {
411                        let mut queues_mut = self.runsteps_queues.borrow_mut();
412                        if let Some(q) = queues_mut.get_mut(&ordering_id_owned) {
413                            if !q.is_empty() {
414                                q.remove(0);
415                            }
416                            if q.is_empty() {
417                                queues_mut.remove(&ordering_id_owned);
418                            }
419                        }
420                    }
421                },
422                _ => {
423                    let cb = timer.callback;
424                    cb.invoke(global, &self.js_timers, can_gc);
425                },
426            }
427        }
428
429        self.schedule_timer_call();
430    }
431
432    fn base_time(&self) -> Instant {
433        let offset = self.suspension_offset.get();
434        match self.suspended_since.get() {
435            Some(suspend_time) => suspend_time - offset,
436            None => Instant::now() - offset,
437        }
438    }
439
440    pub(crate) fn slow_down(&self) {
441        let min_duration_ms = pref!(js_timers_minimum_duration) as u64;
442        self.js_timers
443            .set_min_duration(Duration::from_millis(min_duration_ms));
444    }
445
446    pub(crate) fn speed_up(&self) {
447        self.js_timers.remove_min_duration();
448    }
449
450    pub(crate) fn suspend(&self) {
451        // Suspend is idempotent: do nothing if the timers are already suspended.
452        if self.suspended_since.get().is_some() {
453            return warn!("Suspending an already suspended timer.");
454        }
455
456        debug!("Suspending timers.");
457        self.suspended_since.set(Some(Instant::now()));
458        self.invalidate_expected_event_id();
459    }
460
461    pub(crate) fn resume(&self) {
462        // Resume is idempotent: do nothing if the timers are already resumed.
463        let additional_offset = match self.suspended_since.get() {
464            Some(suspended_since) => Instant::now() - suspended_since,
465            None => return warn!("Resuming an already resumed timer."),
466        };
467
468        debug!("Resuming timers.");
469        self.suspension_offset
470            .set(self.suspension_offset.get() + additional_offset);
471        self.suspended_since.set(None);
472
473        self.schedule_timer_call();
474    }
475
476    /// <https://html.spec.whatwg.org/multipage/#timer-initialisation-steps>
477    fn schedule_timer_call(&self) {
478        if self.suspended_since.get().is_some() {
479            // The timer will be scheduled when the pipeline is fully activated.
480            return;
481        }
482
483        let timers = self.timers.borrow();
484        let Some(timer) = timers.back() else {
485            return;
486        };
487
488        let expected_event_id = self.invalidate_expected_event_id();
489        // Step 12. Let completionStep be an algorithm step which queues a global
490        // task on the timer task source given global to run task.
491        let callback = TimerListener {
492            context: Trusted::new(&*self.global_scope),
493            task_source: self
494                .global_scope
495                .task_manager()
496                .timer_task_source()
497                .to_sendable(),
498            source: timer.source,
499            id: expected_event_id,
500        }
501        .into_callback();
502
503        let event_request = TimerEventRequest {
504            callback,
505            duration: timer.scheduled_for - self.base_time(),
506        };
507
508        self.global_scope.schedule_timer(event_request);
509    }
510
511    fn invalidate_expected_event_id(&self) -> TimerEventId {
512        let TimerEventId(currently_expected) = self.expected_event_id.get();
513        let next_id = TimerEventId(currently_expected + 1);
514        debug!(
515            "invalidating expected timer (was {:?}, now {:?}",
516            currently_expected, next_id
517        );
518        self.expected_event_id.set(next_id);
519        next_id
520    }
521
522    #[allow(clippy::too_many_arguments)]
523    pub(crate) fn set_timeout_or_interval(
524        &self,
525        global: &GlobalScope,
526        callback: TimerCallback,
527        arguments: Vec<HandleValue>,
528        timeout: Duration,
529        is_interval: IsInterval,
530        source: TimerSource,
531        can_gc: CanGc,
532    ) -> Fallible<i32> {
533        self.js_timers.set_timeout_or_interval(
534            global,
535            callback,
536            arguments,
537            timeout,
538            is_interval,
539            source,
540            can_gc,
541        )
542    }
543
544    pub(crate) fn clear_timeout_or_interval(&self, global: &GlobalScope, handle: i32) {
545        self.js_timers.clear_timeout_or_interval(global, handle)
546    }
547}
548
549#[derive(Clone, Copy, Eq, Hash, JSTraceable, MallocSizeOf, Ord, PartialEq, PartialOrd)]
550pub(crate) struct JsTimerHandle(i32);
551
552#[derive(DenyPublicFields, JSTraceable, MallocSizeOf)]
553pub(crate) struct JsTimers {
554    next_timer_handle: Cell<JsTimerHandle>,
555    /// <https://html.spec.whatwg.org/multipage/#list-of-active-timers>
556    active_timers: DomRefCell<FxHashMap<JsTimerHandle, JsTimerEntry>>,
557    /// The nesting level of the currently executing timer task or 0.
558    nesting_level: Cell<u32>,
559    /// Used to introduce a minimum delay in event intervals
560    min_duration: Cell<Option<Duration>>,
561}
562
563#[derive(JSTraceable, MallocSizeOf)]
564struct JsTimerEntry {
565    oneshot_handle: OneshotTimerHandle,
566}
567
568// Holder for the various JS values associated with setTimeout
569// (ie. function value to invoke and all arguments to pass
570//      to the function when calling it)
571// TODO: Handle rooting during invocation when movable GC is turned on
572#[derive(JSTraceable, MallocSizeOf)]
573pub(crate) struct JsTimerTask {
574    handle: JsTimerHandle,
575    #[no_trace]
576    source: TimerSource,
577    callback: InternalTimerCallback,
578    is_interval: IsInterval,
579    nesting_level: u32,
580    duration: Duration,
581    is_user_interacting: bool,
582}
583
584// Enum allowing more descriptive values for the is_interval field
585#[derive(Clone, Copy, JSTraceable, MallocSizeOf, PartialEq)]
586pub(crate) enum IsInterval {
587    Interval,
588    NonInterval,
589}
590
591pub(crate) enum TimerCallback {
592    StringTimerCallback(TrustedScriptOrString),
593    FunctionTimerCallback(Rc<Function>),
594}
595
596#[derive(Clone, JSTraceable, MallocSizeOf)]
597#[cfg_attr(crown, expect(crown::unrooted_must_root))]
598enum InternalTimerCallback {
599    StringTimerCallback(DOMString),
600    FunctionTimerCallback(
601        #[conditional_malloc_size_of] Rc<Function>,
602        #[ignore_malloc_size_of = "mozjs"] Rc<Box<[Heap<JSVal>]>>,
603    ),
604}
605
606impl Default for JsTimers {
607    fn default() -> Self {
608        JsTimers {
609            next_timer_handle: Cell::new(JsTimerHandle(1)),
610            active_timers: DomRefCell::new(FxHashMap::default()),
611            nesting_level: Cell::new(0),
612            min_duration: Cell::new(None),
613        }
614    }
615}
616
617impl JsTimers {
618    /// <https://html.spec.whatwg.org/multipage/#timer-initialisation-steps>
619    #[allow(clippy::too_many_arguments)]
620    #[cfg_attr(crown, expect(crown::unrooted_must_root))]
621    pub(crate) fn set_timeout_or_interval(
622        &self,
623        global: &GlobalScope,
624        callback: TimerCallback,
625        arguments: Vec<HandleValue>,
626        timeout: Duration,
627        is_interval: IsInterval,
628        source: TimerSource,
629        can_gc: CanGc,
630    ) -> Fallible<i32> {
631        let callback = match callback {
632            TimerCallback::StringTimerCallback(trusted_script_or_string) => {
633                // Step 9.6.1.1. Let globalName be "Window" if global is a Window object; "WorkerGlobalScope" otherwise.
634                let global_name = if global.is::<Window>() {
635                    "Window"
636                } else {
637                    "WorkerGlobalScope"
638                };
639                // Step 9.6.1.2. Let methodName be "setInterval" if repeat is true; "setTimeout" otherwise.
640                let method_name = if is_interval == IsInterval::Interval {
641                    "setInterval"
642                } else {
643                    "setTimeout"
644                };
645                // Step 9.6.1.3. Let sink be a concatenation of globalName, U+0020 SPACE, and methodName.
646                let sink = format!("{} {}", global_name, method_name);
647                // Step 9.6.1.4. Set handler to the result of invoking the
648                // Get Trusted Type compliant string algorithm with TrustedScript, global, handler, sink, and "script".
649                let code_str = TrustedScript::get_trusted_script_compliant_string(
650                    global,
651                    trusted_script_or_string,
652                    &sink,
653                    can_gc,
654                )?;
655                // Step 9.6.3. Perform EnsureCSPDoesNotBlockStringCompilation(realm, « », handler, handler, timer, « », handler).
656                // If this throws an exception, catch it, report it for global, and abort these steps.
657                if global
658                    .get_csp_list()
659                    .is_js_evaluation_allowed(global, &code_str.str())
660                {
661                    // Step 9.6.2. Assert: handler is a string.
662                    InternalTimerCallback::StringTimerCallback(code_str)
663                } else {
664                    return Ok(0);
665                }
666            },
667            TimerCallback::FunctionTimerCallback(function) => {
668                // This is a bit complicated, but this ensures that the vector's
669                // buffer isn't reallocated (and moved) after setting the Heap values
670                let mut args = Vec::with_capacity(arguments.len());
671                for _ in 0..arguments.len() {
672                    args.push(Heap::default());
673                }
674                for (i, item) in arguments.iter().enumerate() {
675                    args.get_mut(i).unwrap().set(item.get());
676                }
677                // Step 9.5. If handler is a Function, then invoke handler given arguments and "report",
678                // and with callback this value set to thisArg.
679                InternalTimerCallback::FunctionTimerCallback(
680                    function,
681                    Rc::new(args.into_boxed_slice()),
682                )
683            },
684        };
685
686        // Step 2. If previousId was given, let id be previousId; otherwise,
687        // let id be an implementation-defined integer that is greater than zero
688        // and does not already exist in global's map of setTimeout and setInterval IDs.
689        let JsTimerHandle(new_handle) = self.next_timer_handle.get();
690        self.next_timer_handle.set(JsTimerHandle(new_handle + 1));
691
692        // Step 3. If the surrounding agent's event loop's currently running task
693        // is a task that was created by this algorithm, then let nesting level
694        // be the task's timer nesting level. Otherwise, let nesting level be 0.
695        let mut task = JsTimerTask {
696            handle: JsTimerHandle(new_handle),
697            source,
698            callback,
699            is_interval,
700            is_user_interacting: ScriptThread::is_user_interacting(),
701            nesting_level: 0,
702            duration: Duration::ZERO,
703        };
704
705        // Step 4. If timeout is less than 0, then set timeout to 0.
706        task.duration = timeout.max(Duration::ZERO);
707
708        self.initialize_and_schedule(global, task);
709
710        // Step 15. Return id.
711        Ok(new_handle)
712    }
713
714    pub(crate) fn clear_timeout_or_interval(&self, global: &GlobalScope, handle: i32) {
715        let mut active_timers = self.active_timers.borrow_mut();
716
717        if let Some(entry) = active_timers.remove(&JsTimerHandle(handle)) {
718            global.unschedule_callback(entry.oneshot_handle);
719        }
720    }
721
722    pub(crate) fn set_min_duration(&self, duration: Duration) {
723        self.min_duration.set(Some(duration));
724    }
725
726    pub(crate) fn remove_min_duration(&self) {
727        self.min_duration.set(None);
728    }
729
730    // see step 13 of https://html.spec.whatwg.org/multipage/#timer-initialisation-steps
731    fn user_agent_pad(&self, current_duration: Duration) -> Duration {
732        match self.min_duration.get() {
733            Some(min_duration) => min_duration.max(current_duration),
734            None => current_duration,
735        }
736    }
737
738    /// <https://html.spec.whatwg.org/multipage/#timer-initialisation-steps>
739    fn initialize_and_schedule(&self, global: &GlobalScope, mut task: JsTimerTask) {
740        let handle = task.handle;
741        let mut active_timers = self.active_timers.borrow_mut();
742
743        // Step 3. If the surrounding agent's event loop's currently running task
744        // is a task that was created by this algorithm, then let nesting level be
745        // the task's timer nesting level. Otherwise, let nesting level be 0.
746        let nesting_level = self.nesting_level.get();
747
748        let duration = self.user_agent_pad(clamp_duration(nesting_level, task.duration));
749        // Step 10. Increment nesting level by one.
750        // Step 11. Set task's timer nesting level to nesting level.
751        task.nesting_level = nesting_level + 1;
752
753        // Step 13. Set uniqueHandle to the result of running steps after a timeout given global,
754        // "setTimeout/setInterval", timeout, and completionStep.
755        let callback = OneshotTimerCallback::JsTimer(task);
756        let oneshot_handle = global.schedule_callback(callback, duration);
757
758        // Step 14. Set global's map of setTimeout and setInterval IDs[id] to uniqueHandle.
759        let entry = active_timers
760            .entry(handle)
761            .or_insert(JsTimerEntry { oneshot_handle });
762        entry.oneshot_handle = oneshot_handle;
763    }
764}
765
766/// Step 5 of <https://html.spec.whatwg.org/multipage/#timer-initialisation-steps>
767fn clamp_duration(nesting_level: u32, unclamped: Duration) -> Duration {
768    // Step 5. If nesting level is greater than 5, and timeout is less than 4, then set timeout to 4.
769    let lower_bound_ms = if nesting_level > 5 { 4 } else { 0 };
770    let lower_bound = Duration::from_millis(lower_bound_ms);
771    lower_bound.max(unclamped)
772}
773
774impl JsTimerTask {
775    // see https://html.spec.whatwg.org/multipage/#timer-initialisation-steps
776    pub(crate) fn invoke<T: DomObject>(self, this: &T, timers: &JsTimers, can_gc: CanGc) {
777        // step 9.2 can be ignored, because we proactively prevent execution
778        // of this task when its scheduled execution is canceled.
779
780        // prep for step ? in nested set_timeout_or_interval calls
781        timers.nesting_level.set(self.nesting_level);
782
783        let _guard = ScriptThread::user_interacting_guard();
784        match self.callback {
785            InternalTimerCallback::StringTimerCallback(ref code_str) => {
786                // Step 6.4. Let settings object be global's relevant settings object.
787                // Step 6. Let realm be global's relevant realm.
788                let global = this.global();
789                // TODO Step 7. Let initiating script be the active script.
790
791                // Step 9.6.5. Let fetch options be the default script fetch options.
792                let fetch_options = ScriptFetchOptions::default_classic_script(&global);
793
794                // Step 9.6.6. Let base URL be settings object's API base URL.
795                let base_url = global.api_base_url();
796
797                // TODO Step 9.6.7. If initiating script is not null, then:
798                // Step 9.6.7.1. Set fetch options to a script fetch options whose cryptographic nonce
799                // is initiating script's fetch options's cryptographic nonce,
800                // integrity metadata is the empty string, parser metadata is "not-parser-inserted",
801                // credentials mode is initiating script's fetch options's credentials mode,
802                // referrer policy is initiating script's fetch options's referrer policy,
803                // and fetch priority is "auto".
804                // Step 9.6.7.2. Set base URL to initiating script's base URL.
805
806                // Step 9.6.8. Let script be the result of creating a classic script given handler,
807                // settings object, base URL, and fetch options.
808                let script = global.create_a_classic_script(
809                    (*code_str.str()).into(),
810                    base_url,
811                    fetch_options,
812                    ErrorReporting::Unmuted,
813                    Some(IntroductionType::DOM_TIMER),
814                    1,
815                    false,
816                );
817
818                // Step 9.6.9. Run the classic script script.
819                _ = global.run_a_classic_script(script, RethrowErrors::No, can_gc);
820            },
821            // Step 9.5. If handler is a Function, then invoke handler given arguments and
822            // "report", and with callback this value set to thisArg.
823            InternalTimerCallback::FunctionTimerCallback(ref function, ref arguments) => {
824                let arguments = self.collect_heap_args(arguments);
825                rooted!(in(*GlobalScope::get_cx()) let mut value: JSVal);
826                let _ = function.Call_(this, arguments, value.handle_mut(), Report, can_gc);
827            },
828        };
829
830        // reset nesting level (see above)
831        timers.nesting_level.set(0);
832
833        // Step 9.9. If repeat is true, then perform the timer initialization steps again,
834        // given global, handler, timeout, arguments, true, and id.
835        //
836        // Since we choose proactively prevent execution (see 4.1 above), we must only
837        // reschedule repeating timers when they were not canceled as part of step 4.2.
838        if self.is_interval == IsInterval::Interval &&
839            timers.active_timers.borrow().contains_key(&self.handle)
840        {
841            timers.initialize_and_schedule(&this.global(), self);
842        }
843    }
844
845    fn collect_heap_args<'b>(&self, args: &'b [Heap<JSVal>]) -> Vec<HandleValue<'b>> {
846        args.iter().map(|arg| arg.as_handle_value()).collect()
847    }
848}
849
850/// Describes the source that requested the [`TimerEvent`].
851#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, Serialize)]
852pub enum TimerSource {
853    /// The event was requested from a window (`ScriptThread`).
854    FromWindow(PipelineId),
855    /// The event was requested from a worker (`DedicatedGlobalWorkerScope`).
856    FromWorker,
857}
858
859/// The id to be used for a [`TimerEvent`] is defined by the corresponding [`TimerEventRequest`].
860#[derive(Clone, Copy, Debug, Deserialize, Eq, MallocSizeOf, PartialEq, Serialize)]
861pub struct TimerEventId(pub u32);
862
863/// A notification that a timer has fired. [`TimerSource`] must be `FromWindow` when
864/// dispatched to `ScriptThread` and must be `FromWorker` when dispatched to a
865/// `DedicatedGlobalWorkerScope`
866#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
867pub struct TimerEvent(pub TimerSource, pub TimerEventId);
868
869/// A wrapper between timer events coming in over IPC, and the event-loop.
870#[derive(Clone)]
871struct TimerListener {
872    task_source: SendableTaskSource,
873    context: Trusted<GlobalScope>,
874    source: TimerSource,
875    id: TimerEventId,
876}
877
878impl TimerListener {
879    /// Handle a timer-event coming from the [`timers::TimerScheduler`]
880    /// by queuing the appropriate task on the relevant event-loop.
881    /// <https://html.spec.whatwg.org/multipage/#timer-initialisation-steps>
882    fn handle(&self, event: TimerEvent) {
883        let context = self.context.clone();
884        // Step 9. Let task be a task that runs the following substeps:
885        self.task_source.queue(task!(timer_event: move || {
886                let global = context.root();
887                let TimerEvent(source, id) = event;
888                match source {
889                    TimerSource::FromWorker => {
890                        global.downcast::<WorkerGlobalScope>().expect("Window timer delivered to worker");
891                    },
892                    TimerSource::FromWindow(pipeline) => {
893                        assert_eq!(pipeline, global.pipeline_id());
894                        global.downcast::<Window>().expect("Worker timer delivered to window");
895                    },
896                };
897                global.fire_timer(id, CanGc::note());
898            })
899        );
900    }
901
902    fn into_callback(self) -> BoxedTimerCallback {
903        let timer_event = TimerEvent(self.source, self.id);
904        Box::new(move || self.handle(timer_event))
905    }
906}