PerformanceObserver.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibWeb/Bindings/PlatformObject.h>
  8. #include <LibWeb/HTML/WindowOrWorkerGlobalScope.h>
  9. namespace Web::PerformanceTimeline {
  10. // https://w3c.github.io/performance-timeline/#dom-performanceobserverinit
  11. struct PerformanceObserverInit {
  12. Optional<Vector<String>> entry_types;
  13. Optional<String> type;
  14. Optional<bool> buffered;
  15. };
  16. // https://w3c.github.io/performance-timeline/#dom-performanceobserver
  17. class PerformanceObserver final : public Bindings::PlatformObject {
  18. WEB_PLATFORM_OBJECT(PerformanceObserver, Bindings::PlatformObject);
  19. JS_DECLARE_ALLOCATOR(PerformanceObserver);
  20. public:
  21. enum class ObserverType {
  22. Undefined,
  23. Single,
  24. Multiple,
  25. };
  26. static WebIDL::ExceptionOr<JS::NonnullGCPtr<PerformanceObserver>> construct_impl(JS::Realm&, JS::GCPtr<WebIDL::CallbackType>);
  27. virtual ~PerformanceObserver() override;
  28. WebIDL::ExceptionOr<void> observe(PerformanceObserverInit& options);
  29. void disconnect();
  30. Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>> take_records();
  31. bool requires_dropped_entries() const { return m_requires_dropped_entries; }
  32. void unset_requires_dropped_entries(Badge<HTML::WindowOrWorkerGlobalScopeMixin>);
  33. Vector<PerformanceObserverInit> const& options_list() const { return m_options_list; }
  34. WebIDL::CallbackType& callback() { return *m_callback; }
  35. void append_to_observer_buffer(Badge<HTML::WindowOrWorkerGlobalScopeMixin>, JS::NonnullGCPtr<PerformanceTimeline::PerformanceEntry>);
  36. private:
  37. PerformanceObserver(JS::Realm&, JS::GCPtr<WebIDL::CallbackType>);
  38. virtual void initialize(JS::Realm&) override;
  39. virtual void visit_edges(Cell::Visitor&) override;
  40. // https://w3c.github.io/performance-timeline/#dfn-observer-callback
  41. // A PerformanceObserverCallback observer callback set on creation.
  42. JS::GCPtr<WebIDL::CallbackType> m_callback;
  43. // https://w3c.github.io/performance-timeline/#dfn-observer-buffer
  44. // A PerformanceEntryList object called the observer buffer that is initially empty.
  45. Vector<JS::NonnullGCPtr<PerformanceTimeline::PerformanceEntry>> m_observer_buffer;
  46. // https://w3c.github.io/performance-timeline/#dfn-observer-type
  47. // A DOMString observer type which is initially "undefined".
  48. ObserverType m_observer_type { ObserverType::Undefined };
  49. // https://w3c.github.io/performance-timeline/#dfn-requires-dropped-entries
  50. // A boolean requires dropped entries which is initially set to false.
  51. bool m_requires_dropped_entries { false };
  52. // https://w3c.github.io/performance-timeline/#dfn-options-list
  53. // A registered performance observer is a struct consisting of an observer member (a PerformanceObserver object)
  54. // and an options list member (a list of PerformanceObserverInit dictionaries).
  55. // NOTE: This doesn't use a separate struct as methods such as disconnect() assume it can access an options list from `this`: a PerformanceObserver.
  56. Vector<PerformanceObserverInit> m_options_list;
  57. };
  58. }