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