script/dom/
performancenavigationtiming.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 base::cross_process_instant::CrossProcessInstant;
6use dom_struct::dom_struct;
7
8use crate::dom::bindings::codegen::Bindings::PerformanceBinding::DOMHighResTimeStamp;
9use crate::dom::bindings::codegen::Bindings::PerformanceNavigationTimingBinding::{
10    NavigationTimingType, PerformanceNavigationTimingMethods,
11};
12use crate::dom::bindings::inheritance::Castable;
13use crate::dom::bindings::reflector::reflect_dom_object;
14use crate::dom::bindings::root::{Dom, DomRoot};
15use crate::dom::document::Document;
16use crate::dom::globalscope::GlobalScope;
17use crate::dom::performanceresourcetiming::{InitiatorType, PerformanceResourceTiming};
18use crate::script_runtime::CanGc;
19
20#[dom_struct]
21// https://w3c.github.io/navigation-timing/#dom-performancenavigationtiming
22/// Only the current document resource is included in the performance timeline;
23/// there is only one PerformanceNavigationTiming object in the performance timeline.
24pub(crate) struct PerformanceNavigationTiming {
25    // https://w3c.github.io/navigation-timing/#PerformanceResourceTiming
26    performanceresourcetiming: PerformanceResourceTiming,
27    document: Dom<Document>,
28    nav_type: NavigationTimingType,
29}
30
31impl PerformanceNavigationTiming {
32    fn new_inherited(
33        navigation_start: CrossProcessInstant,
34        document: &Document,
35    ) -> PerformanceNavigationTiming {
36        PerformanceNavigationTiming {
37            performanceresourcetiming: PerformanceResourceTiming::new_inherited(
38                document.url(),
39                InitiatorType::Navigation,
40                None,
41                Some(navigation_start),
42            ),
43            document: Dom::from_ref(document),
44            nav_type: NavigationTimingType::Navigate,
45        }
46    }
47
48    pub(crate) fn new(
49        global: &GlobalScope,
50        fetch_start: CrossProcessInstant,
51        document: &Document,
52        can_gc: CanGc,
53    ) -> DomRoot<PerformanceNavigationTiming> {
54        reflect_dom_object(
55            Box::new(PerformanceNavigationTiming::new_inherited(
56                fetch_start,
57                document,
58            )),
59            global,
60            can_gc,
61        )
62    }
63}
64
65// https://w3c.github.io/navigation-timing/
66impl PerformanceNavigationTimingMethods<crate::DomTypeHolder> for PerformanceNavigationTiming {
67    // https://w3c.github.io/navigation-timing/#dom-performancenavigationtiming-unloadeventstart
68    fn UnloadEventStart(&self) -> DOMHighResTimeStamp {
69        self.upcast::<PerformanceResourceTiming>()
70            .to_dom_high_res_time_stamp(self.document.get_unload_event_start())
71    }
72
73    // https://w3c.github.io/navigation-timing/#dom-performancenavigationtiming-unloadeventend
74    fn UnloadEventEnd(&self) -> DOMHighResTimeStamp {
75        self.upcast::<PerformanceResourceTiming>()
76            .to_dom_high_res_time_stamp(self.document.get_unload_event_end())
77    }
78
79    // https://w3c.github.io/navigation-timing/#dom-performancenavigationtiming-dominteractive
80    fn DomInteractive(&self) -> DOMHighResTimeStamp {
81        self.upcast::<PerformanceResourceTiming>()
82            .to_dom_high_res_time_stamp(self.document.get_dom_interactive())
83    }
84
85    // https://w3c.github.io/navigation-timing/#dom-performancenavigationtiming-domcontentloadedeventstart
86    fn DomContentLoadedEventStart(&self) -> DOMHighResTimeStamp {
87        self.upcast::<PerformanceResourceTiming>()
88            .to_dom_high_res_time_stamp(self.document.get_dom_content_loaded_event_start())
89    }
90
91    // https://w3c.github.io/navigation-timing/#dom-performancenavigationtiming-domcontentloadedeventstart
92    fn DomContentLoadedEventEnd(&self) -> DOMHighResTimeStamp {
93        self.upcast::<PerformanceResourceTiming>()
94            .to_dom_high_res_time_stamp(self.document.get_dom_content_loaded_event_end())
95    }
96
97    // https://w3c.github.io/navigation-timing/#dom-performancenavigationtiming-domcomplete
98    fn DomComplete(&self) -> DOMHighResTimeStamp {
99        self.upcast::<PerformanceResourceTiming>()
100            .to_dom_high_res_time_stamp(self.document.get_dom_complete())
101    }
102
103    // https://w3c.github.io/navigation-timing/#dom-performancenavigationtiming-loadeventstart
104    fn LoadEventStart(&self) -> DOMHighResTimeStamp {
105        self.upcast::<PerformanceResourceTiming>()
106            .to_dom_high_res_time_stamp(self.document.get_load_event_start())
107    }
108
109    // https://w3c.github.io/navigation-timing/#dom-performancenavigationtiming-loadeventend
110    fn LoadEventEnd(&self) -> DOMHighResTimeStamp {
111        self.upcast::<PerformanceResourceTiming>()
112            .to_dom_high_res_time_stamp(self.document.get_load_event_end())
113    }
114
115    // https://w3c.github.io/navigation-timing/#dom-performancenavigationtiming-type
116    fn Type(&self) -> NavigationTimingType {
117        self.nav_type
118    }
119
120    // https://w3c.github.io/navigation-timing/#dom-performancenavigationtiming-redirectcount
121    fn RedirectCount(&self) -> u16 {
122        self.document.get_redirect_count()
123    }
124
125    // check-tidy: no specs after this line
126    // Servo-only timing for when top-level content (not iframes) is complete
127    fn TopLevelDomComplete(&self) -> DOMHighResTimeStamp {
128        self.upcast::<PerformanceResourceTiming>()
129            .to_dom_high_res_time_stamp(self.document.get_top_level_dom_complete())
130    }
131}