Skip to main content

script/dom/performance/
performanceobserverentrylist.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 js::context::JSContext;
7use script_bindings::cell::DomRefCell;
8use script_bindings::reflector::{Reflector, reflect_dom_object_with_cx};
9
10use super::performance::PerformanceEntryList;
11use super::performanceentry::{EntryType, PerformanceEntry};
12use crate::dom::bindings::codegen::Bindings::PerformanceObserverEntryListBinding::PerformanceObserverEntryListMethods;
13use crate::dom::bindings::root::DomRoot;
14use crate::dom::bindings::str::DOMString;
15use crate::dom::globalscope::GlobalScope;
16
17#[dom_struct]
18pub(crate) struct PerformanceObserverEntryList {
19    reflector_: Reflector,
20    entries: DomRefCell<PerformanceEntryList>,
21}
22
23impl PerformanceObserverEntryList {
24    fn new_inherited(entries: Vec<DomRoot<PerformanceEntry>>) -> PerformanceObserverEntryList {
25        PerformanceObserverEntryList {
26            reflector_: Reflector::new(),
27            entries: DomRefCell::new(PerformanceEntryList::new(entries)),
28        }
29    }
30
31    pub(crate) fn new(
32        cx: &mut JSContext,
33        global: &GlobalScope,
34        entries: Vec<DomRoot<PerformanceEntry>>,
35    ) -> DomRoot<PerformanceObserverEntryList> {
36        reflect_dom_object_with_cx(
37            Box::new(PerformanceObserverEntryList::new_inherited(entries)),
38            global,
39            cx,
40        )
41    }
42}
43
44impl PerformanceObserverEntryListMethods<crate::DomTypeHolder> for PerformanceObserverEntryList {
45    /// <https://w3c.github.io/performance-timeline/#dom-performanceobserver>
46    fn GetEntries(&self) -> Vec<DomRoot<PerformanceEntry>> {
47        self.entries
48            .borrow()
49            .get_entries_by_name_and_type(None, None)
50    }
51
52    /// <https://w3c.github.io/performance-timeline/#dom-performanceobserver>
53    fn GetEntriesByType(&self, entry_type: DOMString) -> Vec<DomRoot<PerformanceEntry>> {
54        let Ok(entry_type) = EntryType::try_from(&*entry_type.str()) else {
55            return Vec::new();
56        };
57        self.entries
58            .borrow()
59            .get_entries_by_name_and_type(None, Some(entry_type))
60    }
61
62    /// <https://w3c.github.io/performance-timeline/#dom-performanceobserver>
63    fn GetEntriesByName(
64        &self,
65        name: DOMString,
66        entry_type: Option<DOMString>,
67    ) -> Vec<DomRoot<PerformanceEntry>> {
68        let entry_type = match entry_type {
69            Some(entry_type) => {
70                let Ok(entry_type) = EntryType::try_from(&*entry_type.str()) else {
71                    return Vec::new();
72                };
73                Some(entry_type)
74            },
75            None => None,
76        };
77        self.entries
78            .borrow()
79            .get_entries_by_name_and_type(Some(name), entry_type)
80    }
81}