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