1use 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
51type OrderingIdentifier = DOMString;
54
55#[derive(JSTraceable, MallocSizeOf)]
56struct OrderingEntry {
57 milliseconds: u64,
58 start_seq: u64,
59 handle: OneshotTimerHandle,
60}
61
62type OrderingQueues = FxHashMap<OrderingIdentifier, Vec<OrderingEntry>>;
64
65type 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 suspension_offset: Cell<Duration>,
84 #[no_trace]
91 expected_event_id: Cell<TimerEventId>,
92 map_of_active_timers: DomRefCell<RunStepsActiveMap>,
96
97 runsteps_queues: DomRefCell<OrderingQueues>,
101
102 next_runsteps_key: Cell<TimerKey>,
104
105 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#[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 RunStepsAfterTimeout {
132 timer_key: i32,
134 ordering_id: DOMString,
136 milliseconds: u64,
138 #[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 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 #[inline]
204 pub(crate) fn now_for_runsteps(&self) -> Instant {
205 self.base_time()
207 }
208
209 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 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 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 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 pub(crate) fn fire_timer(&self, id: TimerEventId, global: &GlobalScope, can_gc: CanGc) {
322 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 if base_time < self.timers.borrow().back().unwrap().scheduled_for {
338 warn!("Unexpected timing!");
339 return;
340 }
341
342 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 if !global.can_continue_running() {
362 return;
363 }
364 match &timer.callback {
365 OneshotTimerCallback::RunStepsAfterTimeout { ordering_id, .. } => {
367 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 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 (completion)(global, can_gc);
406
407 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 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 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 fn schedule_timer_call(&self) {
478 if self.suspended_since.get().is_some() {
479 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 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 active_timers: DomRefCell<FxHashMap<JsTimerHandle, JsTimerEntry>>,
557 nesting_level: Cell<u32>,
559 min_duration: Cell<Option<Duration>>,
561}
562
563#[derive(JSTraceable, MallocSizeOf)]
564struct JsTimerEntry {
565 oneshot_handle: OneshotTimerHandle,
566}
567
568#[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#[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 #[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 let global_name = if global.is::<Window>() {
635 "Window"
636 } else {
637 "WorkerGlobalScope"
638 };
639 let method_name = if is_interval == IsInterval::Interval {
641 "setInterval"
642 } else {
643 "setTimeout"
644 };
645 let sink = format!("{} {}", global_name, method_name);
647 let code_str = TrustedScript::get_trusted_script_compliant_string(
650 global,
651 trusted_script_or_string,
652 &sink,
653 can_gc,
654 )?;
655 if global
658 .get_csp_list()
659 .is_js_evaluation_allowed(global, &code_str.str())
660 {
661 InternalTimerCallback::StringTimerCallback(code_str)
663 } else {
664 return Ok(0);
665 }
666 },
667 TimerCallback::FunctionTimerCallback(function) => {
668 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 InternalTimerCallback::FunctionTimerCallback(
680 function,
681 Rc::new(args.into_boxed_slice()),
682 )
683 },
684 };
685
686 let JsTimerHandle(new_handle) = self.next_timer_handle.get();
690 self.next_timer_handle.set(JsTimerHandle(new_handle + 1));
691
692 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 task.duration = timeout.max(Duration::ZERO);
707
708 self.initialize_and_schedule(global, task);
709
710 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 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 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 let nesting_level = self.nesting_level.get();
747
748 let duration = self.user_agent_pad(clamp_duration(nesting_level, task.duration));
749 task.nesting_level = nesting_level + 1;
752
753 let callback = OneshotTimerCallback::JsTimer(task);
756 let oneshot_handle = global.schedule_callback(callback, duration);
757
758 let entry = active_timers
760 .entry(handle)
761 .or_insert(JsTimerEntry { oneshot_handle });
762 entry.oneshot_handle = oneshot_handle;
763 }
764}
765
766fn clamp_duration(nesting_level: u32, unclamped: Duration) -> Duration {
768 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 pub(crate) fn invoke<T: DomObject>(self, this: &T, timers: &JsTimers, can_gc: CanGc) {
777 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 let global = this.global();
789 let fetch_options = ScriptFetchOptions::default_classic_script(&global);
793
794 let base_url = global.api_base_url();
796
797 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 _ = global.run_a_classic_script(script, RethrowErrors::No, can_gc);
820 },
821 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 timers.nesting_level.set(0);
832
833 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#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, Serialize)]
852pub enum TimerSource {
853 FromWindow(PipelineId),
855 FromWorker,
857}
858
859#[derive(Clone, Copy, Debug, Deserialize, Eq, MallocSizeOf, PartialEq, Serialize)]
861pub struct TimerEventId(pub u32);
862
863#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
867pub struct TimerEvent(pub TimerSource, pub TimerEventId);
868
869#[derive(Clone)]
871struct TimerListener {
872 task_source: SendableTaskSource,
873 context: Trusted<GlobalScope>,
874 source: TimerSource,
875 id: TimerEventId,
876}
877
878impl TimerListener {
879 fn handle(&self, event: TimerEvent) {
883 let context = self.context.clone();
884 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}