Performance.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/DOM/Event.h>
  9. #include <LibWeb/DOM/EventDispatcher.h>
  10. #include <LibWeb/HTML/Window.h>
  11. #include <LibWeb/HighResolutionTime/Performance.h>
  12. #include <LibWeb/NavigationTiming/PerformanceTiming.h>
  13. #include <LibWeb/PerformanceTimeline/EntryTypes.h>
  14. namespace Web::HighResolutionTime {
  15. Performance::Performance(HTML::Window& window)
  16. : DOM::EventTarget(window.realm())
  17. , m_window(window)
  18. {
  19. m_timer.start();
  20. }
  21. Performance::~Performance() = default;
  22. JS::ThrowCompletionOr<void> Performance::initialize(JS::Realm& realm)
  23. {
  24. MUST_OR_THROW_OOM(Base::initialize(realm));
  25. set_prototype(&Bindings::ensure_web_prototype<Bindings::PerformancePrototype>(realm, "Performance"));
  26. return {};
  27. }
  28. void Performance::visit_edges(Cell::Visitor& visitor)
  29. {
  30. Base::visit_edges(visitor);
  31. visitor.visit(m_window.ptr());
  32. visitor.visit(m_timing.ptr());
  33. }
  34. JS::GCPtr<NavigationTiming::PerformanceTiming> Performance::timing()
  35. {
  36. if (!m_timing)
  37. m_timing = heap().allocate<NavigationTiming::PerformanceTiming>(realm(), *m_window).release_allocated_value_but_fixme_should_propagate_errors();
  38. return m_timing;
  39. }
  40. double Performance::time_origin() const
  41. {
  42. return static_cast<double>(m_timer.origin_time().to_milliseconds());
  43. }
  44. // https://w3c.github.io/user-timing/#mark-method
  45. WebIDL::ExceptionOr<JS::NonnullGCPtr<UserTiming::PerformanceMark>> Performance::mark(String const& mark_name, UserTiming::PerformanceMarkOptions const& mark_options)
  46. {
  47. auto& realm = this->realm();
  48. // 1. Run the PerformanceMark constructor and let entry be the newly created object.
  49. auto entry = TRY(UserTiming::PerformanceMark::construct_impl(realm, mark_name, mark_options));
  50. // 2. Queue entry.
  51. auto* window_or_worker = dynamic_cast<HTML::WindowOrWorkerGlobalScopeMixin*>(&realm.global_object());
  52. VERIFY(window_or_worker);
  53. TRY(window_or_worker->queue_performance_entry(entry));
  54. // 3. Add entry to the performance entry buffer.
  55. // FIXME: This seems to be a holdover from moving to the `queue` structure for PerformanceObserver, as this would cause a double append.
  56. // 4. Return entry.
  57. return entry;
  58. }
  59. void Performance::clear_marks(Optional<String> mark_name)
  60. {
  61. auto& realm = this->realm();
  62. auto* window_or_worker = dynamic_cast<HTML::WindowOrWorkerGlobalScopeMixin*>(&realm.global_object());
  63. VERIFY(window_or_worker);
  64. // 1. If markName is omitted, remove all PerformanceMark objects from the performance entry buffer.
  65. if (!mark_name.has_value()) {
  66. window_or_worker->clear_performance_entry_buffer({}, PerformanceTimeline::EntryTypes::mark);
  67. return;
  68. }
  69. // 2. Otherwise, remove all PerformanceMark objects listed in the performance entry buffer whose name is markName.
  70. window_or_worker->remove_entries_from_performance_entry_buffer({}, PerformanceTimeline::EntryTypes::mark, mark_name.value());
  71. // 3. Return undefined.
  72. }
  73. // https://www.w3.org/TR/performance-timeline/#getentries-method
  74. WebIDL::ExceptionOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> Performance::get_entries() const
  75. {
  76. auto& realm = this->realm();
  77. auto& vm = this->vm();
  78. auto* window_or_worker = dynamic_cast<HTML::WindowOrWorkerGlobalScopeMixin*>(&realm.global_object());
  79. VERIFY(window_or_worker);
  80. // Returns a PerformanceEntryList object returned by the filter buffer map by name and type algorithm with name and
  81. // type set to null.
  82. return TRY_OR_THROW_OOM(vm, window_or_worker->filter_buffer_map_by_name_and_type(/* name= */ Optional<String> {}, /* type= */ Optional<String> {}));
  83. }
  84. // https://www.w3.org/TR/performance-timeline/#dom-performance-getentriesbytype
  85. WebIDL::ExceptionOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> Performance::get_entries_by_type(String const& type) const
  86. {
  87. auto& realm = this->realm();
  88. auto& vm = this->vm();
  89. auto* window_or_worker = dynamic_cast<HTML::WindowOrWorkerGlobalScopeMixin*>(&realm.global_object());
  90. VERIFY(window_or_worker);
  91. // Returns a PerformanceEntryList object returned by filter buffer map by name and type algorithm with name set to null,
  92. // and type set to the method's input type parameter.
  93. return TRY_OR_THROW_OOM(vm, window_or_worker->filter_buffer_map_by_name_and_type(/* name= */ Optional<String> {}, type));
  94. }
  95. // https://www.w3.org/TR/performance-timeline/#dom-performance-getentriesbyname
  96. WebIDL::ExceptionOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> Performance::get_entries_by_name(String const& name, Optional<String> type) const
  97. {
  98. auto& realm = this->realm();
  99. auto& vm = this->vm();
  100. auto* window_or_worker = dynamic_cast<HTML::WindowOrWorkerGlobalScopeMixin*>(&realm.global_object());
  101. VERIFY(window_or_worker);
  102. // Returns a PerformanceEntryList object returned by filter buffer map by name and type algorithm with name set to the
  103. // method input name parameter, and type set to null if optional entryType is omitted, or set to the method's input type
  104. // parameter otherwise.
  105. return TRY_OR_THROW_OOM(vm, window_or_worker->filter_buffer_map_by_name_and_type(name, type));
  106. }
  107. }