PerformanceObserverEntryList.cpp 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/QuickSort.h>
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/Bindings/PerformanceObserverEntryListPrototype.h>
  9. #include <LibWeb/PerformanceTimeline/PerformanceEntry.h>
  10. #include <LibWeb/PerformanceTimeline/PerformanceObserverEntryList.h>
  11. #include <LibWeb/WebIDL/ExceptionOr.h>
  12. namespace Web::PerformanceTimeline {
  13. JS_DEFINE_ALLOCATOR(PerformanceObserverEntryList);
  14. PerformanceObserverEntryList::PerformanceObserverEntryList(JS::Realm& realm, Vector<JS::NonnullGCPtr<PerformanceTimeline::PerformanceEntry>>&& entry_list)
  15. : Bindings::PlatformObject(realm)
  16. , m_entry_list(move(entry_list))
  17. {
  18. }
  19. PerformanceObserverEntryList::~PerformanceObserverEntryList() = default;
  20. void PerformanceObserverEntryList::initialize(JS::Realm& realm)
  21. {
  22. Base::initialize(realm);
  23. WEB_SET_PROTOTYPE_FOR_INTERFACE(PerformanceObserverEntryList);
  24. }
  25. void PerformanceObserverEntryList::visit_edges(Cell::Visitor& visitor)
  26. {
  27. Base::visit_edges(visitor);
  28. visitor.visit(m_entry_list);
  29. }
  30. // https://www.w3.org/TR/performance-timeline/#dfn-filter-buffer-by-name-and-type
  31. ErrorOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> filter_buffer_by_name_and_type(Vector<JS::NonnullGCPtr<PerformanceTimeline::PerformanceEntry>> const& buffer, Optional<String> name, Optional<String> type)
  32. {
  33. // 1. Let result be an initially empty list.
  34. Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>> result;
  35. // 2. For each PerformanceEntry entry in buffer, run the following steps:
  36. for (auto const& entry : buffer) {
  37. // 1. If type is not null and if type is not identical to entry's entryType attribute, continue to next entry.
  38. if (type.has_value() && type.value() != entry->entry_type())
  39. continue;
  40. // 2. If name is not null and if name is not identical to entry's name attribute, continue to next entry.
  41. if (name.has_value() && name.value() != entry->name())
  42. continue;
  43. // 3. append entry to result.
  44. TRY(result.try_append(entry));
  45. }
  46. // 3. Sort results's entries in chronological order with respect to startTime
  47. quick_sort(result, [](auto const& left_entry, auto const& right_entry) {
  48. return left_entry->start_time() < right_entry->start_time();
  49. });
  50. // 4. Return result.
  51. return result;
  52. }
  53. // https://w3c.github.io/performance-timeline/#dom-performanceobserverentrylist-getentries
  54. WebIDL::ExceptionOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> PerformanceObserverEntryList::get_entries() const
  55. {
  56. // Returns a PerformanceEntryList object returned by filter buffer by name and type algorithm with this's entry list,
  57. // name and type set to null.
  58. return TRY_OR_THROW_OOM(vm(), filter_buffer_by_name_and_type(m_entry_list, /* name= */ Optional<String> {}, /* type= */ Optional<String> {}));
  59. }
  60. // https://w3c.github.io/performance-timeline/#dom-performanceobserverentrylist-getentriesbytype
  61. WebIDL::ExceptionOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> PerformanceObserverEntryList::get_entries_by_type(String const& type) const
  62. {
  63. // Returns a PerformanceEntryList object returned by filter buffer by name and type algorithm with this's entry list,
  64. // name set to null, and type set to the method's input type parameter.
  65. return TRY_OR_THROW_OOM(vm(), filter_buffer_by_name_and_type(m_entry_list, /* name= */ Optional<String> {}, type));
  66. }
  67. // https://w3c.github.io/performance-timeline/#dom-performanceobserverentrylist-getentriesbyname
  68. WebIDL::ExceptionOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> PerformanceObserverEntryList::get_entries_by_name(String const& name, Optional<String> type) const
  69. {
  70. // Returns a PerformanceEntryList object returned by filter buffer by name and type algorithm with this's entry list,
  71. // name set to the method input name parameter, and type set to null if optional entryType is omitted, or set to the
  72. // method's input type parameter otherwise.
  73. return TRY_OR_THROW_OOM(vm(), filter_buffer_by_name_and_type(m_entry_list, name, type));
  74. }
  75. }