PerformanceObserverEntryList.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. for (auto& entry : m_entry_list)
  29. visitor.visit(entry);
  30. }
  31. // https://www.w3.org/TR/performance-timeline/#dfn-filter-buffer-by-name-and-type
  32. 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)
  33. {
  34. // 1. Let result be an initially empty list.
  35. Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>> result;
  36. // 2. For each PerformanceEntry entry in buffer, run the following steps:
  37. for (auto const& entry : buffer) {
  38. // 1. If type is not null and if type is not identical to entry's entryType attribute, continue to next entry.
  39. if (type.has_value() && type.value() != entry->entry_type())
  40. continue;
  41. // 2. If name is not null and if name is not identical to entry's name attribute, continue to next entry.
  42. if (name.has_value() && name.value() != entry->name())
  43. continue;
  44. // 3. append entry to result.
  45. TRY(result.try_append(entry));
  46. }
  47. // 3. Sort results's entries in chronological order with respect to startTime
  48. quick_sort(result, [](auto const& left_entry, auto const& right_entry) {
  49. return left_entry->start_time() < right_entry->start_time();
  50. });
  51. // 4. Return result.
  52. return result;
  53. }
  54. // https://w3c.github.io/performance-timeline/#dom-performanceobserverentrylist-getentries
  55. WebIDL::ExceptionOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> PerformanceObserverEntryList::get_entries() const
  56. {
  57. // Returns a PerformanceEntryList object returned by filter buffer by name and type algorithm with this's entry list,
  58. // name and type set to null.
  59. return TRY_OR_THROW_OOM(vm(), filter_buffer_by_name_and_type(m_entry_list, /* name= */ Optional<String> {}, /* type= */ Optional<String> {}));
  60. }
  61. // https://w3c.github.io/performance-timeline/#dom-performanceobserverentrylist-getentriesbytype
  62. WebIDL::ExceptionOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> PerformanceObserverEntryList::get_entries_by_type(String const& type) const
  63. {
  64. // Returns a PerformanceEntryList object returned by filter buffer by name and type algorithm with this's entry list,
  65. // name set to null, and type set to the method's input type parameter.
  66. return TRY_OR_THROW_OOM(vm(), filter_buffer_by_name_and_type(m_entry_list, /* name= */ Optional<String> {}, type));
  67. }
  68. // https://w3c.github.io/performance-timeline/#dom-performanceobserverentrylist-getentriesbyname
  69. WebIDL::ExceptionOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> PerformanceObserverEntryList::get_entries_by_name(String const& name, Optional<String> type) const
  70. {
  71. // Returns a PerformanceEntryList object returned by filter buffer by name and type algorithm with this's entry list,
  72. // name set to the method input name parameter, and type set to null if optional entryType is omitted, or set to the
  73. // method's input type parameter otherwise.
  74. return TRY_OR_THROW_OOM(vm(), filter_buffer_by_name_and_type(m_entry_list, name, type));
  75. }
  76. }