Performance.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. #pragma once
  8. #include <LibCore/ElapsedTimer.h>
  9. #include <LibWeb/DOM/EventTarget.h>
  10. #include <LibWeb/UserTiming/PerformanceMark.h>
  11. #include <LibWeb/UserTiming/PerformanceMeasure.h>
  12. namespace Web::HighResolutionTime {
  13. class Performance final : public DOM::EventTarget {
  14. WEB_PLATFORM_OBJECT(Performance, DOM::EventTarget);
  15. public:
  16. virtual ~Performance() override;
  17. double now() const { return static_cast<double>(m_timer.elapsed()); }
  18. double time_origin() const;
  19. JS::GCPtr<NavigationTiming::PerformanceTiming> timing();
  20. WebIDL::ExceptionOr<JS::NonnullGCPtr<UserTiming::PerformanceMark>> mark(String const& mark_name, UserTiming::PerformanceMarkOptions const& mark_options = {});
  21. void clear_marks(Optional<String> mark_name);
  22. WebIDL::ExceptionOr<JS::NonnullGCPtr<UserTiming::PerformanceMeasure>> measure(String const& measure_name, Variant<String, UserTiming::PerformanceMeasureOptions> const& start_or_measure_options, Optional<String> end_mark);
  23. void clear_measures(Optional<String> measure_name);
  24. WebIDL::ExceptionOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> get_entries() const;
  25. WebIDL::ExceptionOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> get_entries_by_type(String const& type) const;
  26. WebIDL::ExceptionOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> get_entries_by_name(String const& name, Optional<String> type) const;
  27. private:
  28. explicit Performance(HTML::Window&);
  29. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  30. virtual void visit_edges(Cell::Visitor&) override;
  31. WebIDL::ExceptionOr<HighResolutionTime::DOMHighResTimeStamp> convert_name_to_timestamp(JS::Realm& realm, String const& name);
  32. WebIDL::ExceptionOr<HighResolutionTime::DOMHighResTimeStamp> convert_mark_to_timestamp(JS::Realm& realm, Variant<String, HighResolutionTime::DOMHighResTimeStamp> mark);
  33. JS::NonnullGCPtr<HTML::Window> m_window;
  34. JS::GCPtr<NavigationTiming::PerformanceTiming> m_timing;
  35. Core::ElapsedTimer m_timer;
  36. };
  37. }