Performance.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. namespace Web::HighResolutionTime {
  14. Performance::Performance(HTML::Window& window)
  15. : DOM::EventTarget(window.realm())
  16. , m_window(window)
  17. {
  18. m_timer.start();
  19. }
  20. Performance::~Performance() = default;
  21. JS::ThrowCompletionOr<void> Performance::initialize(JS::Realm& realm)
  22. {
  23. MUST_OR_THROW_OOM(Base::initialize(realm));
  24. set_prototype(&Bindings::ensure_web_prototype<Bindings::PerformancePrototype>(realm, "Performance"));
  25. return {};
  26. }
  27. void Performance::visit_edges(Cell::Visitor& visitor)
  28. {
  29. Base::visit_edges(visitor);
  30. visitor.visit(m_window.ptr());
  31. visitor.visit(m_timing.ptr());
  32. }
  33. JS::GCPtr<NavigationTiming::PerformanceTiming> Performance::timing()
  34. {
  35. if (!m_timing)
  36. m_timing = heap().allocate<NavigationTiming::PerformanceTiming>(realm(), *m_window).release_allocated_value_but_fixme_should_propagate_errors();
  37. return m_timing;
  38. }
  39. double Performance::time_origin() const
  40. {
  41. return static_cast<double>(m_timer.origin_time().to_milliseconds());
  42. }
  43. // https://www.w3.org/TR/performance-timeline/#getentries-method
  44. WebIDL::ExceptionOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> Performance::get_entries() const
  45. {
  46. auto& realm = this->realm();
  47. auto& vm = this->vm();
  48. auto* window_or_worker = dynamic_cast<HTML::WindowOrWorkerGlobalScopeMixin*>(&realm.global_object());
  49. VERIFY(window_or_worker);
  50. // Returns a PerformanceEntryList object returned by the filter buffer map by name and type algorithm with name and
  51. // type set to null.
  52. return TRY_OR_THROW_OOM(vm, window_or_worker->filter_buffer_map_by_name_and_type(/* name= */ Optional<String> {}, /* type= */ Optional<String> {}));
  53. }
  54. // https://www.w3.org/TR/performance-timeline/#dom-performance-getentriesbytype
  55. WebIDL::ExceptionOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> Performance::get_entries_by_type(String const& type) const
  56. {
  57. auto& realm = this->realm();
  58. auto& vm = this->vm();
  59. auto* window_or_worker = dynamic_cast<HTML::WindowOrWorkerGlobalScopeMixin*>(&realm.global_object());
  60. VERIFY(window_or_worker);
  61. // Returns a PerformanceEntryList object returned by filter buffer map by name and type algorithm with name set to null,
  62. // and type set to the method's input type parameter.
  63. return TRY_OR_THROW_OOM(vm, window_or_worker->filter_buffer_map_by_name_and_type(/* name= */ Optional<String> {}, type));
  64. }
  65. // https://www.w3.org/TR/performance-timeline/#dom-performance-getentriesbyname
  66. WebIDL::ExceptionOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> Performance::get_entries_by_name(String const& name, Optional<String> type) const
  67. {
  68. auto& realm = this->realm();
  69. auto& vm = this->vm();
  70. auto* window_or_worker = dynamic_cast<HTML::WindowOrWorkerGlobalScopeMixin*>(&realm.global_object());
  71. VERIFY(window_or_worker);
  72. // Returns a PerformanceEntryList object returned by filter buffer map by name and type algorithm with name set to the
  73. // method input name parameter, and type set to null if optional entryType is omitted, or set to the method's input type
  74. // parameter otherwise.
  75. return TRY_OR_THROW_OOM(vm, window_or_worker->filter_buffer_map_by_name_and_type(name, type));
  76. }
  77. }