Skip to main content

script/dom/performance/
performance.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::Ordering;
7use std::collections::VecDeque;
8
9use dom_struct::dom_struct;
10use js::context::JSContext;
11use js::jsval::NullValue;
12use script_bindings::cell::DomRefCell;
13use script_bindings::cformat;
14use script_bindings::codegen::GenericBindings::PerformanceBinding::PerformanceMarkOptions;
15use script_bindings::codegen::GenericBindings::PerformanceMarkBinding::PerformanceMarkMethods;
16use script_bindings::codegen::GenericBindings::WindowBinding::WindowMethods;
17use script_bindings::codegen::GenericUnionTypes::StringOrPerformanceMeasureOptions;
18use script_bindings::reflector::reflect_dom_object;
19use servo_base::cross_process_instant::CrossProcessInstant;
20use time::Duration;
21
22use super::performanceentry::{EntryType, PerformanceEntry};
23use super::performancemark::PerformanceMark;
24use super::performancemeasure::PerformanceMeasure;
25use super::performancenavigation::PerformanceNavigation;
26use super::performanceobserver::PerformanceObserver as DOMPerformanceObserver;
27use crate::dom::PERFORMANCE_TIMING_ATTRIBUTES;
28use crate::dom::bindings::codegen::Bindings::PerformanceBinding::{
29    DOMHighResTimeStamp, PerformanceMethods,
30};
31use crate::dom::bindings::codegen::UnionTypes::StringOrDouble;
32use crate::dom::bindings::error::{Error, Fallible};
33use crate::dom::bindings::inheritance::Castable;
34use crate::dom::bindings::num::Finite;
35use crate::dom::bindings::refcounted::Trusted;
36use crate::dom::bindings::reflector::DomGlobal;
37use crate::dom::bindings::root::{AsHandleValue, Dom, DomRoot};
38use crate::dom::bindings::str::DOMString;
39use crate::dom::bindings::structuredclone;
40use crate::dom::eventtarget::EventTarget;
41use crate::dom::globalscope::GlobalScope;
42use crate::dom::performance::performancetiming::PerformanceTiming;
43use crate::dom::window::Window;
44
45/// Implementation of a list of PerformanceEntry items shared by the
46/// Performance and PerformanceObserverEntryList interfaces implementations.
47#[derive(JSTraceable, MallocSizeOf)]
48#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
49pub(crate) struct PerformanceEntryList {
50    /// <https://w3c.github.io/performance-timeline/#dfn-performance-entry-buffer>
51    entries: Vec<Dom<PerformanceEntry>>,
52}
53
54impl PerformanceEntryList {
55    pub(crate) fn new(entries: Vec<DomRoot<PerformanceEntry>>) -> Self {
56        PerformanceEntryList {
57            entries: entries.into_iter().map(|entry| entry.as_traced()).collect(),
58        }
59    }
60
61    /// <https://www.w3.org/TR/performance-timeline/#dfn-filter-buffer-map-by-name-and-type>
62    pub(crate) fn get_entries_by_name_and_type(
63        &self,
64        name: Option<DOMString>,
65        entry_type: Option<EntryType>,
66    ) -> Vec<DomRoot<PerformanceEntry>> {
67        let mut result = self
68            .entries
69            .iter()
70            .filter(|e| {
71                name.as_ref().is_none_or(|name_| *e.name() == *name_) &&
72                    entry_type
73                        .as_ref()
74                        .is_none_or(|type_| e.entry_type() == *type_)
75            })
76            .map(|entry| entry.as_rooted())
77            .collect::<Vec<DomRoot<PerformanceEntry>>>();
78
79        // Step 6. Sort results's entries in chronological order with respect to startTime
80        result.sort_by(|a, b| {
81            a.start_time()
82                .partial_cmp(&b.start_time())
83                .unwrap_or(Ordering::Equal)
84        });
85
86        // Step 7. Return result.
87        result
88    }
89
90    pub(crate) fn clear_entries_by_name_and_type(
91        &mut self,
92        name: Option<DOMString>,
93        entry_type: EntryType,
94    ) {
95        self.entries.retain(|e| {
96            e.entry_type() != entry_type || name.as_ref().is_some_and(|name_| e.name() != name_)
97        });
98    }
99
100    fn get_last_entry_start_time_with_name_and_type(
101        &self,
102        name: DOMString,
103        entry_type: EntryType,
104    ) -> Option<CrossProcessInstant> {
105        self.entries
106            .iter()
107            .rev()
108            .find(|e| e.entry_type() == entry_type && *e.name() == name)
109            .and_then(|entry| entry.start_time())
110    }
111}
112
113#[derive(JSTraceable, MallocSizeOf)]
114#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
115struct PerformanceObserver {
116    observer: Dom<DOMPerformanceObserver>,
117    entry_types: Vec<EntryType>,
118}
119
120#[dom_struct]
121pub(crate) struct Performance {
122    eventtarget: EventTarget,
123    buffer: DomRefCell<PerformanceEntryList>,
124    observers: DomRefCell<Vec<PerformanceObserver>>,
125    pending_notification_observers_task: Cell<bool>,
126    #[no_trace]
127    /// The `timeOrigin` as described in
128    /// <https://html.spec.whatwg.org/multipage/#concept-settings-object-time-origin>.
129    time_origin: CrossProcessInstant,
130    /// <https://w3c.github.io/resource-timing/#performance-resource-timing-buffer-size-limit>
131    /// The max-size of the buffer, set to 0 once the pipeline exits.
132    /// TODO: have one max-size per entry type.
133    resource_timing_buffer_size_limit: Cell<usize>,
134    /// <https://w3c.github.io/resource-timing/#performance-resource-timing-buffer-current-size>
135    resource_timing_buffer_current_size: Cell<usize>,
136    /// <https://w3c.github.io/resource-timing/#performance-resource-timing-buffer-full-event-pending-flag>
137    resource_timing_buffer_pending_full_event: Cell<bool>,
138    /// <https://w3c.github.io/resource-timing/#performance-resource-timing-secondary-buffer>
139    resource_timing_secondary_entries: DomRefCell<VecDeque<Dom<PerformanceEntry>>>,
140    timing: Dom<PerformanceTiming>,
141    navigation: Dom<PerformanceNavigation>,
142}
143
144impl Performance {
145    fn new_inherited(
146        time_origin: CrossProcessInstant,
147        timing: &PerformanceTiming,
148        navigation: &PerformanceNavigation,
149    ) -> Performance {
150        Performance {
151            eventtarget: EventTarget::new_inherited(),
152            buffer: DomRefCell::new(PerformanceEntryList::new(Vec::new())),
153            observers: DomRefCell::new(Vec::new()),
154            pending_notification_observers_task: Cell::new(false),
155            time_origin,
156            resource_timing_buffer_size_limit: Cell::new(250),
157            resource_timing_buffer_current_size: Cell::new(0),
158            resource_timing_buffer_pending_full_event: Cell::new(false),
159            resource_timing_secondary_entries: DomRefCell::new(VecDeque::new()),
160            timing: Dom::from_ref(timing),
161            navigation: Dom::from_ref(navigation),
162        }
163    }
164
165    pub(crate) fn new(
166        cx: &mut JSContext,
167        global: &GlobalScope,
168        navigation_start: CrossProcessInstant,
169    ) -> DomRoot<Performance> {
170        let timing = PerformanceTiming::new(cx, global);
171        let navigation = PerformanceNavigation::new(cx, global);
172        reflect_dom_object(
173            cx,
174            Box::new(Performance::new_inherited(
175                navigation_start,
176                &timing,
177                &navigation,
178            )),
179            global,
180        )
181    }
182
183    pub(crate) fn time_origin(&self) -> CrossProcessInstant {
184        self.time_origin
185    }
186
187    pub(crate) fn to_dom_high_res_time_stamp(
188        &self,
189        instant: CrossProcessInstant,
190    ) -> DOMHighResTimeStamp {
191        (instant - self.time_origin).to_dom_high_res_time_stamp()
192    }
193
194    pub(crate) fn maybe_to_dom_high_res_time_stamp(
195        &self,
196        instant: Option<CrossProcessInstant>,
197    ) -> DOMHighResTimeStamp {
198        self.to_dom_high_res_time_stamp(instant.unwrap_or(self.time_origin))
199    }
200
201    /// Clear all buffered performance entries, and disable the buffer.
202    /// Called as part of the window's "clear_js_runtime" workflow,
203    /// performed when exiting a pipeline.
204    pub(crate) fn clear_and_disable_performance_entry_buffer(&self) {
205        let mut buffer = self.buffer.borrow_mut();
206        buffer.entries.clear();
207        self.resource_timing_buffer_size_limit.set(0);
208    }
209
210    // Add a PerformanceObserver to the list of observers with a set of
211    // observed entry types.
212
213    pub(crate) fn add_multiple_type_observer(
214        &self,
215        observer: &DOMPerformanceObserver,
216        entry_types: Vec<EntryType>,
217    ) {
218        let mut observers = self.observers.borrow_mut();
219        match observers.iter().position(|o| *o.observer == *observer) {
220            // If the observer is already in the list, we only update the observed
221            // entry types.
222            Some(p) => observers[p].entry_types = entry_types,
223            // Otherwise, we create and insert the new PerformanceObserver.
224            None => observers.push(PerformanceObserver {
225                observer: Dom::from_ref(observer),
226                entry_types,
227            }),
228        };
229    }
230
231    pub(crate) fn add_single_type_observer(
232        &self,
233        observer: &DOMPerformanceObserver,
234        entry_type: EntryType,
235        buffered: bool,
236    ) {
237        if buffered {
238            let buffer = self.buffer.borrow();
239            let new_entries = buffer.get_entries_by_name_and_type(None, Some(entry_type));
240            if !new_entries.is_empty() {
241                let new_entries = new_entries.into_iter().map(|entry| entry.as_traced());
242                observer.entries_mut().extend(new_entries);
243            }
244
245            if !self.pending_notification_observers_task.get() {
246                self.pending_notification_observers_task.set(true);
247                let owner = Trusted::new(self);
248                self.global()
249                    .task_manager()
250                    .performance_timeline_task_source()
251                    .queue(task!(notify_performance_observers: move |cx| {
252                        owner.root().notify_observers(cx);
253                    }));
254            }
255        }
256        let mut observers = self.observers.borrow_mut();
257        match observers.iter().position(|o| *o.observer == *observer) {
258            // If the observer is already in the list, we only update
259            // the observed entry types.
260            Some(p) => {
261                // Append the type if not already present, otherwise do nothing
262                if !observers[p].entry_types.contains(&entry_type) {
263                    observers[p].entry_types.push(entry_type)
264                }
265            },
266            // Otherwise, we create and insert the new PerformanceObserver.
267            None => observers.push(PerformanceObserver {
268                observer: Dom::from_ref(observer),
269                entry_types: vec![entry_type],
270            }),
271        };
272    }
273
274    /// Remove a PerformanceObserver from the list of observers.
275    pub(crate) fn remove_observer(&self, observer: &DOMPerformanceObserver) {
276        let mut observers = self.observers.borrow_mut();
277        let index = match observers.iter().position(|o| &(*o.observer) == observer) {
278            Some(p) => p,
279            None => return,
280        };
281
282        observers.remove(index);
283    }
284
285    /// Queue a notification for each performance observer interested in
286    /// this type of performance entry and queue a low priority task to
287    /// notify the observers if no other notification task is already queued.
288    ///
289    /// Algorithm spec:
290    /// <https://w3c.github.io/performance-timeline/#queue-a-performanceentry>
291    /// Also this algorithm has been extented according to :
292    /// <https://w3c.github.io/resource-timing/#sec-extensions-performance-interface>
293    pub(crate) fn queue_entry(&self, entry: &PerformanceEntry) -> Option<usize> {
294        // https://w3c.github.io/performance-timeline/#dfn-determine-eligibility-for-adding-a-performance-entry
295        if entry.entry_type() == EntryType::Resource && !self.should_queue_resource_entry(entry) {
296            return None;
297        }
298
299        // Steps 1-3.
300        // Add the performance entry to the list of performance entries that have not
301        // been notified to each performance observer owner, filtering the ones it's
302        // interested in.
303        for observer in self
304            .observers
305            .borrow()
306            .iter()
307            .filter(|o| o.entry_types.contains(&entry.entry_type()))
308        {
309            observer.observer.queue_entry(entry);
310        }
311
312        // Step 4.
313        // add the new entry to the buffer.
314        self.buffer.borrow_mut().entries.push(Dom::from_ref(entry));
315
316        let entry_last_index = self.buffer.borrow_mut().entries.len() - 1;
317
318        // Step 5.
319        // If there is already a queued notification task, we just bail out.
320        if self.pending_notification_observers_task.get() {
321            return None;
322        }
323
324        // Step 6.
325        // Queue a new notification task.
326        self.pending_notification_observers_task.set(true);
327
328        let owner = Trusted::new(self);
329        self.global()
330            .task_manager()
331            .performance_timeline_task_source()
332            .queue(task!(notify_performance_observers: move |cx| {
333                owner.root().notify_observers(cx);
334            }));
335
336        Some(entry_last_index)
337    }
338
339    /// Observers notifications task.
340    ///
341    /// Algorithm spec (step 7):
342    /// <https://w3c.github.io/performance-timeline/#queue-a-performanceentry>
343    fn notify_observers(&self, cx: &mut JSContext) {
344        // Step 7.1.
345        self.pending_notification_observers_task.set(false);
346
347        // Step 7.2.
348        // We have to operate over a copy of the performance observers to avoid
349        // the risk of an observer's callback modifying the list of registered
350        // observers. This is a shallow copy, so observers can
351        // disconnect themselves by using the argument of their own callback.
352        let observers: Vec<DomRoot<DOMPerformanceObserver>> = self
353            .observers
354            .borrow()
355            .iter()
356            .map(|o| DomRoot::from_ref(&*o.observer))
357            .collect();
358
359        // Step 7.3.
360        for o in observers.iter() {
361            o.notify(cx);
362        }
363    }
364
365    /// <https://w3c.github.io/resource-timing/#performance-can-add-resource-timing-entry>
366    fn can_add_resource_timing_entry(&self) -> bool {
367        // Step 1. If resource timing buffer current size is smaller than resource timing buffer size limit, return true.
368        // Step 2. Return false.
369        self.resource_timing_buffer_current_size.get() <
370            self.resource_timing_buffer_size_limit.get()
371    }
372
373    /// <https://w3c.github.io/resource-timing/#dfn-copy-secondary-buffer>
374    fn copy_secondary_resource_timing_buffer(&self) {
375        // Step 1. While resource timing secondary buffer is not empty and can add resource timing entry returns true, run the following substeps:
376        while self.can_add_resource_timing_entry() {
377            // Step 1.1. Let entry be the oldest PerformanceResourceTiming in resource timing secondary buffer.
378            if let Some(ref entry) = self
379                .resource_timing_secondary_entries
380                .borrow_mut()
381                .pop_front()
382            {
383                // Step 1.2. Add entry to the end of performance entry buffer.
384                self.buffer.borrow_mut().entries.push(Dom::from_ref(entry));
385                // Step 1.3. Increment resource timing buffer current size by 1.
386                self.resource_timing_buffer_current_size
387                    .set(self.resource_timing_buffer_current_size.get() + 1);
388                // Step 1.4. Remove entry from resource timing secondary buffer.
389                // Step 1.5. Decrement resource timing secondary buffer current size by 1.
390                // Handled by popping the entry earlier.
391            } else {
392                break;
393            }
394        }
395    }
396
397    /// <https://w3c.github.io/resource-timing/#dfn-fire-a-buffer-full-event>
398    fn fire_buffer_full_event(&self, cx: &mut js::context::JSContext) {
399        while !self.resource_timing_secondary_entries.borrow().is_empty() {
400            let no_of_excess_entries_before = self.resource_timing_secondary_entries.borrow().len();
401
402            if !self.can_add_resource_timing_entry() {
403                self.upcast::<EventTarget>()
404                    .fire_event(cx, atom!("resourcetimingbufferfull"));
405            }
406            self.copy_secondary_resource_timing_buffer();
407            let no_of_excess_entries_after = self.resource_timing_secondary_entries.borrow().len();
408            if no_of_excess_entries_before <= no_of_excess_entries_after {
409                self.resource_timing_secondary_entries.borrow_mut().clear();
410                break;
411            }
412        }
413        self.resource_timing_buffer_pending_full_event.set(false);
414    }
415
416    /// <https://w3c.github.io/resource-timing/#dfn-add-a-performanceresourcetiming-entry>
417    fn should_queue_resource_entry(&self, entry: &PerformanceEntry) -> bool {
418        // Step 1. If can add resource timing entry returns true and resource timing buffer full event pending flag is false, run the following substeps:
419        if !self.resource_timing_buffer_pending_full_event.get() {
420            if self.can_add_resource_timing_entry() {
421                // Step 1.a.  Add new entry to the performance entry buffer.
422                //   This is done in queue_entry, which calls this method.
423                // Step 1.b. Increase resource timing buffer current size by 1.
424                self.resource_timing_buffer_current_size
425                    .set(self.resource_timing_buffer_current_size.get() + 1);
426                // Step 1.c. Return.
427                return true;
428            }
429
430            // Step 2.a. Set resource timing buffer full event pending flag to true.
431            self.resource_timing_buffer_pending_full_event.set(true);
432            // Step 2.b. Queue a task on the performance timeline task source to run fire a buffer full event.
433            let performance = Trusted::new(self);
434            self.global()
435                .task_manager()
436                .performance_timeline_task_source()
437                .queue(task!(fire_a_buffer_full_event: move |cx| {
438                    performance.root().fire_buffer_full_event(cx);
439                }));
440        }
441
442        // Step 3. Add new entry to the resource timing secondary buffer.
443        self.resource_timing_secondary_entries
444            .borrow_mut()
445            .push_back(Dom::from_ref(entry));
446
447        // Step 4. Increase resource timing secondary buffer current size by 1.
448        //   This is tracked automatically via `.len()`.
449        false
450    }
451
452    pub(crate) fn update_entry(&self, index: usize, entry: &PerformanceEntry) {
453        if let Some(e) = self.buffer.borrow_mut().entries.get_mut(index) {
454            *e = Dom::from_ref(entry);
455        }
456    }
457
458    /// <https://w3c.github.io/user-timing/#convert-a-name-to-a-timestamp>
459    fn convert_a_name_to_a_timestamp(&self, name: &str) -> Fallible<CrossProcessInstant> {
460        // Step 1. If the global object is not a Window object, throw a TypeError.
461        let Some(window) = DomRoot::downcast::<Window>(self.global()) else {
462            return Err(Error::Type(cformat!(
463                "Cannot use {name} from non-window global"
464            )));
465        };
466
467        // Step 2. If name is navigationStart, return 0.
468        if name == "navigationStart" {
469            return Ok(self.time_origin);
470        }
471
472        // Step 3. Let startTime be the value of navigationStart in the PerformanceTiming interface.
473        // FIXME: We don't implement this value yet, so we assume it's zero (and then we don't need it at all)
474
475        // Step 4. Let endTime be the value of name in the PerformanceTiming interface.
476        //
477        // NOTE: We store all performance values on the document
478        let end_time = window.Document().performance_timing_attribute(name)?;
479
480        // Step 5. If endTime is 0, throw an InvalidAccessError.
481        let Some(end_time) = end_time else {
482            return Err(Error::InvalidAccess(Some(format!(
483                "{name} hasn't happened yet"
484            ))));
485        };
486
487        // Step 6. Return result of subtracting startTime from endTime.
488        Ok(end_time)
489    }
490
491    /// <https://w3c.github.io/user-timing/#convert-a-mark-to-a-timestamp>
492    fn convert_a_mark_to_a_timestamp(
493        &self,
494        mark: &StringOrDouble,
495    ) -> Fallible<CrossProcessInstant> {
496        match mark {
497            StringOrDouble::String(name) => {
498                // Step 1. If mark is a DOMString and it has the same name as a read only attribute in the
499                // PerformanceTiming interface, let end time be the value returned by running the convert
500                // a name to a timestamp algorithm with name set to the value of mark.
501                if PERFORMANCE_TIMING_ATTRIBUTES.contains(&&*name.str()) {
502                    self.convert_a_name_to_a_timestamp(&name.str())
503                }
504                // Step 2. Otherwise, if mark is a DOMString, let end time be the value of the startTime
505                // attribute from the most recent occurrence of a PerformanceMark object in the performance entry
506                // buffer whose name is mark. If no matching entry is found, throw a SyntaxError.
507                else {
508                    self.buffer
509                        .borrow()
510                        .get_last_entry_start_time_with_name_and_type(name.clone(), EntryType::Mark)
511                        .ok_or(Error::Syntax(Some(format!(
512                            "No PerformanceMark named {name} exists"
513                        ))))
514                }
515            },
516            // Step 3. Otherwise, if mark is a DOMHighResTimeStamp:
517            StringOrDouble::Double(timestamp) => {
518                // Step 3.1 If mark is negative, throw a TypeError.
519                if timestamp.is_sign_negative() {
520                    return Err(Error::Type(c"Time stamps must not be negative".to_owned()));
521                }
522
523                // Step 3.2 Otherwise, let end time be mark.
524                // NOTE: I think the spec wants us to return the value.
525                Ok(
526                    self.time_origin +
527                        Duration::microseconds(timestamp.mul_add(1000.0, 0.0) as i64),
528                )
529            },
530        }
531    }
532}
533
534impl PerformanceMethods<crate::DomTypeHolder> for Performance {
535    /// <https://w3c.github.io/navigation-timing/#dom-performance-timing>
536    fn Timing(&self) -> DomRoot<PerformanceTiming> {
537        DomRoot::from_ref(&*self.timing)
538    }
539
540    /// <https://w3c.github.io/navigation-timing/#dom-performance-navigation>
541    fn Navigation(&self) -> DomRoot<PerformanceNavigation> {
542        DomRoot::from_ref(&*self.navigation)
543    }
544
545    /// <https://w3c.github.io/hr-time/#dom-performance-now>
546    fn Now(&self) -> DOMHighResTimeStamp {
547        self.to_dom_high_res_time_stamp(CrossProcessInstant::now())
548    }
549
550    /// <https://www.w3.org/TR/hr-time-2/#dom-performance-timeorigin>
551    fn TimeOrigin(&self) -> DOMHighResTimeStamp {
552        (self.time_origin - CrossProcessInstant::epoch()).to_dom_high_res_time_stamp()
553    }
554
555    /// <https://www.w3.org/TR/performance-timeline-2/#dom-performance-getentries>
556    fn GetEntries(&self) -> Vec<DomRoot<PerformanceEntry>> {
557        // > Returns a PerformanceEntryList object returned by the filter buffer map by name and type
558        // > algorithm with name and type set to null.
559        self.buffer
560            .borrow()
561            .get_entries_by_name_and_type(None, None)
562    }
563
564    /// <https://www.w3.org/TR/performance-timeline-2/#dom-performance-getentriesbytype>
565    fn GetEntriesByType(&self, entry_type: DOMString) -> Vec<DomRoot<PerformanceEntry>> {
566        let Ok(entry_type) = EntryType::try_from(&*entry_type.str()) else {
567            return Vec::new();
568        };
569        self.buffer
570            .borrow()
571            .get_entries_by_name_and_type(None, Some(entry_type))
572    }
573
574    /// <https://www.w3.org/TR/performance-timeline-2/#dom-performance-getentriesbyname>
575    fn GetEntriesByName(
576        &self,
577        name: DOMString,
578        entry_type: Option<DOMString>,
579    ) -> Vec<DomRoot<PerformanceEntry>> {
580        let entry_type = match entry_type {
581            Some(entry_type) => {
582                let Ok(entry_type) = EntryType::try_from(&*entry_type.str()) else {
583                    return Vec::new();
584                };
585                Some(entry_type)
586            },
587            None => None,
588        };
589        self.buffer
590            .borrow()
591            .get_entries_by_name_and_type(Some(name), entry_type)
592    }
593
594    /// <https://w3c.github.io/user-timing/#dom-performance-mark>
595    fn Mark(
596        &self,
597        cx: &mut JSContext,
598        mark_name: DOMString,
599        mark_options: &PerformanceMarkOptions,
600    ) -> Fallible<DomRoot<PerformanceMark>> {
601        // Step 1. Run the PerformanceMark constructor and let entry be the newly created object.
602        let entry =
603            PerformanceMark::Constructor(cx, &self.global(), None, mark_name, mark_options)?;
604
605        // Step 2. Queue a PerformanceEntry entry.
606        // Step 3. Add entry to the performance entry buffer. (This is done in queue_entry itself)
607        self.queue_entry(entry.upcast::<PerformanceEntry>());
608
609        // Step 4. Return entry.
610        Ok(entry)
611    }
612
613    /// <https://w3c.github.io/user-timing/#dom-performance-clearmarks>
614    fn ClearMarks(&self, mark_name: Option<DOMString>) {
615        self.buffer
616            .borrow_mut()
617            .clear_entries_by_name_and_type(mark_name, EntryType::Mark);
618    }
619
620    /// <https://w3c.github.io/user-timing/#dom-performance-measure>
621    fn Measure(
622        &self,
623        cx: &mut JSContext,
624        measure_name: DOMString,
625        start_or_measure_options: StringOrPerformanceMeasureOptions,
626        end_mark: Option<DOMString>,
627    ) -> Fallible<DomRoot<PerformanceMeasure>> {
628        // Step 1. If startOrMeasureOptions is a PerformanceMeasureOptions object and at least one of start,
629        // end, duration, and detail exist, run the following checks:
630        if let StringOrPerformanceMeasureOptions::PerformanceMeasureOptions(options) =
631            &start_or_measure_options &&
632            (options.start.is_some() ||
633                options.duration.is_some() ||
634                options.end.is_some() ||
635                options.detail.get().is_object_or_null())
636        {
637            // Step 1.1 If endMark is given, throw a TypeError.
638            if end_mark.is_some() {
639                return Err(Error::Type(
640                    c"Must not provide endMark if PerformanceMeasureOptions is also provided"
641                        .to_owned(),
642                ));
643            }
644
645            // Step 1.2 If startOrMeasureOptions’s start and end members are both omitted, throw a TypeError.
646            if options.start.is_none() && options.end.is_none() {
647                return Err(Error::Type(
648                    c"Either 'start' or 'end' member of PerformanceMeasureOptions must be provided"
649                        .to_owned(),
650                ));
651            }
652
653            // Step 1.3 If startOrMeasureOptions’s start, duration, and end members all exist, throw a TypeError.
654            if options.start.is_some() && options.duration.is_some() && options.end.is_some() {
655                return Err(Error::Type(c"Either 'start' or 'end' or 'duration' member of PerformanceMeasureOptions must be omitted".to_owned()));
656            }
657        }
658
659        // Step 2. Compute end time as follows:
660        // Step 2.1 If endMark is given, let end time be the value returned
661        // by running the convert a mark to a timestamp algorithm passing in endMark.
662        let end_time = if let Some(end_mark) = end_mark {
663            self.convert_a_mark_to_a_timestamp(&StringOrDouble::String(end_mark))?
664        } else {
665            match &start_or_measure_options {
666                StringOrPerformanceMeasureOptions::PerformanceMeasureOptions(options) => {
667                    // Step 2.2 Otherwise, if startOrMeasureOptions is a PerformanceMeasureOptions object,
668                    // and if its end member exists, let end time be the value returned by running the
669                    // convert a mark to a timestamp algorithm passing in startOrMeasureOptions’s end.
670                    if let Some(end) = &options.end {
671                        self.convert_a_mark_to_a_timestamp(end)?
672                    }
673                    // Step 2.3 Otherwise, if startOrMeasureOptions is a PerformanceMeasureOptions object,
674                    // and if its start and duration members both exist:
675                    else if let Some((start, duration)) =
676                        options.start.as_ref().zip(options.duration)
677                    {
678                        // Step 2.3.1 Let start be the value returned by running the convert a mark to a timestamp
679                        // algorithm passing in start.
680                        let start = self.convert_a_mark_to_a_timestamp(start)?;
681
682                        // Step 2.3.2 Let duration be the value returned by running the convert a mark to a timestamp
683                        // algorithm passing in duration.
684                        let duration = self
685                            .convert_a_mark_to_a_timestamp(&StringOrDouble::Double(duration))? -
686                            self.time_origin;
687
688                        // Step 2.3.3 Let end time be start plus duration.
689                        start + duration
690                    } else {
691                        // Step 2.4 Otherwise, let end time be the value that would be returned by the
692                        // Performance object’s now() method.
693                        CrossProcessInstant::now()
694                    }
695                },
696                _ => {
697                    // Step 2.4 Otherwise, let end time be the value that would be returned by the
698                    // Performance object’s now() method.
699                    CrossProcessInstant::now()
700                },
701            }
702        };
703
704        // Step 3. Compute start time as follows:
705        let start_time = match &start_or_measure_options {
706            StringOrPerformanceMeasureOptions::PerformanceMeasureOptions(options) => {
707                // Step 3.1 If startOrMeasureOptions is a PerformanceMeasureOptions object, and if its start member exists,
708                // let start time be the value returned by running the convert a mark to a timestamp algorithm passing in
709                // startOrMeasureOptions’s start.
710                if let Some(start) = &options.start {
711                    self.convert_a_mark_to_a_timestamp(start)?
712                }
713                // Step 3.2 Otherwise, if startOrMeasureOptions is a PerformanceMeasureOptions object,
714                // and if its duration and end members both exist:
715                else if let Some((duration, end)) = options.duration.zip(options.end.as_ref()) {
716                    // Step 3.2.1 Let duration be the value returned by running the convert a mark to a timestamp
717                    // algorithm passing in duration.
718                    let duration = self
719                        .convert_a_mark_to_a_timestamp(&StringOrDouble::Double(duration))? -
720                        self.time_origin;
721
722                    // Step 3.2.2 Let end be the value returned by running the convert a mark to a timestamp algorithm
723                    // passing in end.
724                    let end = self.convert_a_mark_to_a_timestamp(end)?;
725
726                    // Step 3.3.3 Let start time be end minus duration.
727                    end - duration
728                }
729                // Step 3.4 Otherwise, let start time be 0.
730                else {
731                    self.time_origin
732                }
733            },
734            StringOrPerformanceMeasureOptions::String(string) => {
735                // Step 3.3 Otherwise, if startOrMeasureOptions is a DOMString, let start time be the value returned
736                // by running the convert a mark to a timestamp algorithm passing in startOrMeasureOptions.
737                self.convert_a_mark_to_a_timestamp(&StringOrDouble::String(string.clone()))?
738            },
739        };
740
741        // Step 4. Create a new PerformanceMeasure object (entry) with this’s relevant realm.
742        // Step 5. Set entry’s name attribute to measureName.
743        // Step 6. Set entry’s entryType attribute to DOMString "measure".
744        // Step 7. Set entry’s startTime attribute to start time.
745        // Step 8. Set entry’s duration attribute to the duration from start time to end time.
746        // The resulting duration value MAY be negative.
747
748        let entry = PerformanceMeasure::new(
749            cx,
750            &self.global(),
751            measure_name,
752            start_time,
753            end_time - start_time,
754        );
755
756        // Step 9. Set entry’s detail attribute as follows:
757        rooted!(&in(cx) let mut detail = NullValue());
758        // Step 9.1. If startOrMeasureOptions is a PerformanceMeasureOptions object and startOrMeasureOptions’s detail member exists:
759        if let StringOrPerformanceMeasureOptions::PerformanceMeasureOptions(options) =
760            &start_or_measure_options &&
761            !options.detail.get().is_null_or_undefined()
762        {
763            // Step 9.1.1. Let record be the result of calling the StructuredSerialize algorithm on startOrMeasureOptions’s detail.
764            let record = structuredclone::write(cx, options.detail.as_handle_value(), None)?;
765
766            // Step 9.1.2. Set entry’s detail to the result of calling the StructuredDeserialize algorithm on record and the current realm.
767            structuredclone::read(cx, &self.global(), record, detail.handle_mut())?;
768        }
769        // Step 9.2. Otherwise, set it to null.
770        //
771        // Note: This is already the default value we set when creating the detail above
772
773        entry.set_detail(detail.handle());
774
775        // Step 10. Queue a PerformanceEntry entry.
776        // Step 11. Add entry to the performance entry buffer. (This is done in queue_entry itself)
777        self.queue_entry(entry.upcast::<PerformanceEntry>());
778
779        // Step 12. Return entry.
780        Ok(entry)
781    }
782
783    /// <https://w3c.github.io/user-timing/#dom-performance-clearmeasures>
784    fn ClearMeasures(&self, measure_name: Option<DOMString>) {
785        self.buffer
786            .borrow_mut()
787            .clear_entries_by_name_and_type(measure_name, EntryType::Measure);
788    }
789    /// <https://w3c.github.io/resource-timing/#dom-performance-clearresourcetimings>
790    fn ClearResourceTimings(&self) {
791        self.buffer
792            .borrow_mut()
793            .clear_entries_by_name_and_type(None, EntryType::Resource);
794        self.resource_timing_buffer_current_size.set(0);
795    }
796
797    /// <https://w3c.github.io/resource-timing/#performance-setresourcetimingbuffersize>
798    fn SetResourceTimingBufferSize(&self, max_size: u32) {
799        self.resource_timing_buffer_size_limit
800            .set(max_size as usize);
801    }
802
803    // https://w3c.github.io/resource-timing/#dom-performance-onresourcetimingbufferfull
804    event_handler!(
805        resourcetimingbufferfull,
806        GetOnresourcetimingbufferfull,
807        SetOnresourcetimingbufferfull
808    );
809}
810
811pub(crate) trait ToDOMHighResTimeStamp {
812    fn to_dom_high_res_time_stamp(&self) -> DOMHighResTimeStamp;
813}
814
815impl ToDOMHighResTimeStamp for Duration {
816    fn to_dom_high_res_time_stamp(&self) -> DOMHighResTimeStamp {
817        // https://www.w3.org/TR/hr-time-2/#clock-resolution
818        // We need a granularity no finer than 5 microseconds. 5 microseconds isn't an
819        // exactly representable f64 so WPT tests might occasionally corner-case on
820        // rounding.  web-platform-tests/wpt#21526 wants us to use an integer number of
821        // microseconds; the next divisor of milliseconds up from 5 microseconds is 10.
822        let microseconds_rounded = (self.whole_microseconds() as f64 / 10.).floor() * 10.;
823        Finite::wrap(microseconds_rounded / 1000.)
824    }
825}