Skip to main content

script/dom/performance/
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 dom_struct::dom_struct;
6use net_traits::{ResourceFetchTiming, ResourceTimingType};
7use script_bindings::reflector::reflect_dom_object;
8
9use super::performanceresourcetiming::{InitiatorType, PerformanceResourceTiming};
10use crate::dom::bindings::codegen::Bindings::PerformanceBinding::DOMHighResTimeStamp;
11use crate::dom::bindings::codegen::Bindings::PerformanceNavigationTimingBinding::{
12    NavigationTimingType, PerformanceNavigationTimingMethods,
13};
14use crate::dom::bindings::inheritance::Castable;
15use crate::dom::bindings::root::{Dom, DomRoot};
16use crate::dom::document::Document;
17use crate::dom::globalscope::GlobalScope;
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(document: &Document) -> PerformanceNavigationTiming {
33        PerformanceNavigationTiming {
34            performanceresourcetiming: PerformanceResourceTiming::new_inherited(
35                document.url(),
36                InitiatorType::Navigation,
37                document
38                    .resource_fetch_timing()
39                    .as_ref()
40                    .unwrap_or(&ResourceFetchTiming::new(ResourceTimingType::None)),
41            ),
42            document: Dom::from_ref(document),
43            nav_type: NavigationTimingType::Navigate,
44        }
45    }
46
47    pub(crate) fn new(
48        global: &GlobalScope,
49        document: &Document,
50        can_gc: CanGc,
51    ) -> DomRoot<PerformanceNavigationTiming> {
52        reflect_dom_object(
53            Box::new(PerformanceNavigationTiming::new_inherited(document)),
54            global,
55            can_gc,
56        )
57    }
58}
59
60// https://w3c.github.io/navigation-timing/
61impl PerformanceNavigationTimingMethods<crate::DomTypeHolder> for PerformanceNavigationTiming {
62    /// <https://w3c.github.io/navigation-timing/#dom-performancenavigationtiming-unloadeventstart>
63    fn UnloadEventStart(&self) -> DOMHighResTimeStamp {
64        self.upcast::<PerformanceResourceTiming>()
65            .to_dom_high_res_time_stamp(self.document.navigation_timing().unload_event_start.get())
66    }
67
68    /// <https://w3c.github.io/navigation-timing/#dom-performancenavigationtiming-unloadeventend>
69    fn UnloadEventEnd(&self) -> DOMHighResTimeStamp {
70        self.upcast::<PerformanceResourceTiming>()
71            .to_dom_high_res_time_stamp(self.document.navigation_timing().unload_event_end.get())
72    }
73
74    /// <https://w3c.github.io/navigation-timing/#dom-performancenavigationtiming-dominteractive>
75    fn DomInteractive(&self) -> DOMHighResTimeStamp {
76        self.upcast::<PerformanceResourceTiming>()
77            .to_dom_high_res_time_stamp(self.document.navigation_timing().dom_interactive.get())
78    }
79
80    /// <https://w3c.github.io/navigation-timing/#dom-performancenavigationtiming-domcontentloadedeventstart>
81    fn DomContentLoadedEventStart(&self) -> DOMHighResTimeStamp {
82        self.upcast::<PerformanceResourceTiming>()
83            .to_dom_high_res_time_stamp(
84                self.document
85                    .navigation_timing()
86                    .dom_content_loaded_event_start
87                    .get(),
88            )
89    }
90
91    /// <https://w3c.github.io/navigation-timing/#dom-performancenavigationtiming-domcontentloadedeventend>
92    fn DomContentLoadedEventEnd(&self) -> DOMHighResTimeStamp {
93        self.upcast::<PerformanceResourceTiming>()
94            .to_dom_high_res_time_stamp(
95                self.document
96                    .navigation_timing()
97                    .dom_content_loaded_event_end
98                    .get(),
99            )
100    }
101
102    /// <https://w3c.github.io/navigation-timing/#dom-performancenavigationtiming-domcomplete>
103    fn DomComplete(&self) -> DOMHighResTimeStamp {
104        self.upcast::<PerformanceResourceTiming>()
105            .to_dom_high_res_time_stamp(self.document.navigation_timing().dom_complete.get())
106    }
107
108    /// <https://w3c.github.io/navigation-timing/#dom-performancenavigationtiming-loadeventstart>
109    fn LoadEventStart(&self) -> DOMHighResTimeStamp {
110        self.upcast::<PerformanceResourceTiming>()
111            .to_dom_high_res_time_stamp(self.document.navigation_timing().load_event_start.get())
112    }
113
114    /// <https://w3c.github.io/navigation-timing/#dom-performancenavigationtiming-loadeventend>
115    fn LoadEventEnd(&self) -> DOMHighResTimeStamp {
116        self.upcast::<PerformanceResourceTiming>()
117            .to_dom_high_res_time_stamp(self.document.navigation_timing().load_event_end.get())
118    }
119
120    /// <https://w3c.github.io/navigation-timing/#dom-performancenavigationtiming-type>
121    fn Type(&self) -> NavigationTimingType {
122        self.nav_type
123    }
124
125    /// <https://w3c.github.io/navigation-timing/#dom-performancenavigationtiming-redirectcount>
126    fn RedirectCount(&self) -> u16 {
127        self.document.get_redirect_count()
128    }
129
130    // check-tidy: no specs after this line
131    // Servo-only timing for when top-level content (not iframes) is complete
132    fn TopLevelDomComplete(&self) -> DOMHighResTimeStamp {
133        self.upcast::<PerformanceResourceTiming>()
134            .to_dom_high_res_time_stamp(
135                self.document
136                    .navigation_timing()
137                    .top_level_dom_complete
138                    .get(),
139            )
140    }
141}