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