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::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
52type OrderingIdentifier = DOMString;
55
56#[derive(JSTraceable, MallocSizeOf)]
57struct OrderingEntry {
58 milliseconds: u64,
59 start_seq: u64,
60 handle: OneshotTimerHandle,
61}
62
63type OrderingQueues = FxHashMap<OrderingIdentifier, Vec<OrderingEntry>>;
65
66type 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 suspension_offset: Cell<Duration>,
85 #[no_trace]
92 expected_event_id: Cell<TimerEventId>,
93 map_of_active_timers: DomRefCell<RunStepsActiveMap>,
97
98 runsteps_queues: DomRefCell<OrderingQueues>,
102
103 next_runsteps_key: Cell<TimerKey>,
105
106 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#[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 RunStepsAfterTimeout {
133 timer_key: i32,
135 ordering_id: DOMString,
137 milliseconds: u64,
139 #[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 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 #[inline]
207 pub(crate) fn now_for_runsteps(&self) -> Instant {
208 self.base_time()
210 }
211
212 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 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 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 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 pub(crate) fn fire_timer(&self, id: TimerEventId, global: &GlobalScope, cx: &mut JSContext) {
325 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 if base_time < self.timers.borrow().back().unwrap().scheduled_for {
341 warn!("Unexpected timing!");
342 return;
343 }
344
345 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 if !global.can_continue_running() {
365 return;
366 }
367 match &timer.callback {
368 OneshotTimerCallback::RunStepsAfterTimeout { ordering_id, .. } => {
370 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 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 (completion)(cx, global);
409
410 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 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 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 fn schedule_timer_call(&self) {
481 if self.suspended_since.get().is_some() {
482 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 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 active_timers: DomRefCell<FxHashMap<JsTimerHandle, JsTimerEntry>>,
560 nesting_level: Cell<u32>,
562 min_duration: Cell<Option<Duration>>,
564}
565
566#[derive(JSTraceable, MallocSizeOf)]
567struct JsTimerEntry {
568 oneshot_handle: OneshotTimerHandle,
569}
570
571#[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#[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 #[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 let global_name = if global.is::<Window>() {
638 "Window"
639 } else {
640 "WorkerGlobalScope"
641 };
642 let method_name = if is_interval == IsInterval::Interval {
644 "setInterval"
645 } else {
646 "setTimeout"
647 };
648 let sink = format!("{} {}", global_name, method_name);
650 let code_str = TrustedScript::get_trusted_type_compliant_string(
653 cx,
654 global,
655 trusted_script_or_string,
656 &sink,
657 )?;
658 if global
661 .get_csp_list()
662 .is_js_evaluation_allowed(global, &code_str.str())
663 {
664 InternalTimerCallback::StringTimerCallback(code_str)
666 } else {
667 return Ok(0);
668 }
669 },
670 TimerCallback::FunctionTimerCallback(function) => {
671 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 InternalTimerCallback::FunctionTimerCallback(
683 function,
684 Rc::new(args.into_boxed_slice()),
685 )
686 },
687 };
688
689 let JsTimerHandle(new_handle) = self.next_timer_handle.get();
693 self.next_timer_handle.set(JsTimerHandle(new_handle + 1));
694
695 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 task.duration = timeout.max(Duration::ZERO);
710
711 self.initialize_and_schedule(global, task);
712
713 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 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 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 let nesting_level = self.nesting_level.get();
750
751 let duration = self.user_agent_pad(clamp_duration(nesting_level, task.duration));
752 task.nesting_level = nesting_level + 1;
755
756 let callback = OneshotTimerCallback::JsTimer(task);
759 let oneshot_handle = global.schedule_callback(callback, duration);
760
761 let entry = active_timers
763 .entry(handle)
764 .or_insert(JsTimerEntry { oneshot_handle });
765 entry.oneshot_handle = oneshot_handle;
766 }
767}
768
769fn clamp_duration(nesting_level: u32, unclamped: Duration) -> Duration {
771 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 pub(crate) fn invoke<T: DomObject>(self, this: &T, timers: &JsTimers, cx: &mut JSContext) {
780 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 let global = this.global();
792 let fetch_options = ScriptFetchOptions::default_classic_script(&global);
796
797 let base_url = global.api_base_url();
799
800 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 _ = global.run_a_classic_script(script, RethrowErrors::No, CanGc::from_cx(cx));
823 },
824 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 timers.nesting_level.set(0);
841
842 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#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, Serialize)]
861pub enum TimerSource {
862 FromWindow(PipelineId),
864 FromWorker,
866}
867
868#[derive(Clone, Copy, Debug, Deserialize, Eq, MallocSizeOf, PartialEq, Serialize)]
870pub struct TimerEventId(pub u32);
871
872#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
876pub struct TimerEvent(pub TimerSource, pub TimerEventId);
877
878#[derive(Clone)]
880struct TimerListener {
881 task_source: SendableTaskSource,
882 context: Trusted<GlobalScope>,
883 source: TimerSource,
884 id: TimerEventId,
885}
886
887impl TimerListener {
888 fn handle(&self, event: TimerEvent) {
892 let context = self.context.clone();
893 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}