PerformanceObserver.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. public:
  20. enum class ObserverType {
  21. Undefined,
  22. Single,
  23. Multiple,
  24. };
  25. static WebIDL::ExceptionOr<JS::NonnullGCPtr<PerformanceObserver>> construct_impl(JS::Realm&, JS::GCPtr<WebIDL::CallbackType>);
  26. virtual ~PerformanceObserver() override;
  27. WebIDL::ExceptionOr<void> observe(PerformanceObserverInit& options);
  28. void disconnect();
  29. Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>> take_records();
  30. bool requires_dropped_entries() const { return m_requires_dropped_entries; }
  31. void unset_requires_dropped_entries(Badge<HTML::WindowOrWorkerGlobalScopeMixin>);
  32. Vector<PerformanceObserverInit> const& options_list() const { return m_options_list; }
  33. WebIDL::CallbackType& callback() { return *m_callback; }
  34. void append_to_observer_buffer(Badge<HTML::WindowOrWorkerGlobalScopeMixin>, JS::NonnullGCPtr<PerformanceTimeline::PerformanceEntry>);
  35. private:
  36. PerformanceObserver(JS::Realm&, JS::GCPtr<WebIDL::CallbackType>);
  37. virtual void initialize(JS::Realm&) override;
  38. virtual void visit_edges(Cell::Visitor&) override;
  39. // https://w3c.github.io/performance-timeline/#dfn-observer-callback
  40. // A PerformanceObserverCallback observer callback set on creation.
  41. JS::GCPtr<WebIDL::CallbackType> m_callback;
  42. // https://w3c.github.io/performance-timeline/#dfn-observer-buffer
  43. // A PerformanceEntryList object called the observer buffer that is initially empty.
  44. Vector<JS::NonnullGCPtr<PerformanceTimeline::PerformanceEntry>> m_observer_buffer;
  45. // https://w3c.github.io/performance-timeline/#dfn-observer-type
  46. // A DOMString observer type which is initially "undefined".
  47. ObserverType m_observer_type { ObserverType::Undefined };
  48. // https://w3c.github.io/performance-timeline/#dfn-requires-dropped-entries
  49. // A boolean requires dropped entries which is initially set to false.
  50. bool m_requires_dropped_entries { false };
  51. // https://w3c.github.io/performance-timeline/#dfn-options-list
  52. // A registered performance observer is a struct consisting of an observer member (a PerformanceObserver object)
  53. // and an options list member (a list of PerformanceObserverInit dictionaries).
  54. // NOTE: This doesn't use a separate struct as methods such as disconnect() assume it can access an options list from `this`: a PerformanceObserver.
  55. Vector<PerformanceObserverInit> m_options_list;
  56. };
  57. }