PerformanceObserver.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/Bindings/PerformanceObserverPrototype.h>
  8. #include <LibWeb/HTML/WindowOrWorkerGlobalScope.h>
  9. #include <LibWeb/HighResolutionTime/SupportedPerformanceTypes.h>
  10. #include <LibWeb/PerformanceTimeline/EntryTypes.h>
  11. #include <LibWeb/PerformanceTimeline/PerformanceEntry.h>
  12. #include <LibWeb/PerformanceTimeline/PerformanceObserver.h>
  13. #include <LibWeb/WebIDL/CallbackType.h>
  14. #include <LibWeb/WebIDL/ExceptionOr.h>
  15. namespace Web::PerformanceTimeline {
  16. JS_DEFINE_ALLOCATOR(PerformanceObserver);
  17. WebIDL::ExceptionOr<JS::NonnullGCPtr<PerformanceObserver>> PerformanceObserver::construct_impl(JS::Realm& realm, JS::GCPtr<WebIDL::CallbackType> callback)
  18. {
  19. return realm.heap().allocate<PerformanceObserver>(realm, realm, callback);
  20. }
  21. PerformanceObserver::PerformanceObserver(JS::Realm& realm, JS::GCPtr<WebIDL::CallbackType> callback)
  22. : Bindings::PlatformObject(realm)
  23. , m_callback(move(callback))
  24. {
  25. }
  26. PerformanceObserver::~PerformanceObserver() = default;
  27. void PerformanceObserver::initialize(JS::Realm& realm)
  28. {
  29. Base::initialize(realm);
  30. WEB_SET_PROTOTYPE_FOR_INTERFACE(PerformanceObserver);
  31. }
  32. void PerformanceObserver::visit_edges(Cell::Visitor& visitor)
  33. {
  34. Base::visit_edges(visitor);
  35. visitor.visit(m_callback);
  36. for (auto& entry : m_observer_buffer)
  37. visitor.visit(entry);
  38. }
  39. // https://w3c.github.io/performance-timeline/#dom-performanceobserver-observe
  40. WebIDL::ExceptionOr<void> PerformanceObserver::observe(PerformanceObserverInit& options)
  41. {
  42. auto& realm = this->realm();
  43. // 1. Let relevantGlobal be this's relevant global object.
  44. auto* relevant_global = dynamic_cast<HTML::WindowOrWorkerGlobalScopeMixin*>(&HTML::relevant_global_object(*this));
  45. VERIFY(relevant_global);
  46. // 2. If options's entryTypes and type members are both omitted, then throw a "TypeError".
  47. if (!options.entry_types.has_value() && !options.type.has_value())
  48. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Must specify one of entryTypes or type"sv };
  49. // 3. If options's entryTypes is present and any other member is also present, then throw a "TypeError".
  50. if (options.entry_types.has_value() && (options.type.has_value() || options.buffered.has_value()))
  51. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Cannot specify type or buffered if entryTypes is specified"sv };
  52. // 4. Update or check this's observer type by running these steps:
  53. // 1. If this's observer type is "undefined":
  54. if (m_observer_type == ObserverType::Undefined) {
  55. // 1. If options's entryTypes member is present, then set this's observer type to "multiple".
  56. if (options.entry_types.has_value())
  57. m_observer_type = ObserverType::Multiple;
  58. // 2. If options's type member is present, then set this's observer type to "single".
  59. if (options.type.has_value())
  60. m_observer_type = ObserverType::Single;
  61. }
  62. // 2. If this's observer type is "single" and options's entryTypes member is present, then throw an "InvalidModificationError".
  63. else if (m_observer_type == ObserverType::Single) {
  64. if (options.entry_types.has_value())
  65. return WebIDL::InvalidModificationError::create(realm, "Cannot change a PerformanceObserver from observing a single type to observing multiple types"_fly_string);
  66. }
  67. // 3. If this's observer type is "multiple" and options's type member is present, then throw an "InvalidModificationError".
  68. else if (m_observer_type == ObserverType::Multiple) {
  69. if (options.type.has_value())
  70. return WebIDL::InvalidModificationError::create(realm, "Cannot change a PerformanceObserver from observing multiple types to observing a single type"_fly_string);
  71. }
  72. // 5. Set this's requires dropped entries to true.
  73. m_requires_dropped_entries = true;
  74. // 6. If this's observer type is "multiple", run the following steps:
  75. if (m_observer_type == ObserverType::Multiple) {
  76. // 1. Let entry types be options's entryTypes sequence.
  77. VERIFY(options.entry_types.has_value());
  78. auto& entry_types = options.entry_types.value();
  79. // 2. Remove all types from entry types that are not contained in relevantGlobal's frozen array of supported entry types.
  80. // The user agent SHOULD notify developers if entry types is modified. For example, a console warning listing removed
  81. // types might be appropriate.
  82. entry_types.remove_all_matching([](String const& type) {
  83. #define __ENUMERATE_SUPPORTED_PERFORMANCE_ENTRY_TYPES(entry_type, cpp_class) \
  84. if (entry_type == type) \
  85. return false;
  86. ENUMERATE_SUPPORTED_PERFORMANCE_ENTRY_TYPES
  87. #undef __ENUMERATE_SUPPORTED_PERFORMANCE_ENTRY_TYPES
  88. dbgln("Potential FIXME: Removing unsupported PerformanceEntry type '{}' from list of observed types in PerformanceObserver::observe()", type);
  89. return true;
  90. });
  91. // 3. If the resulting entry types sequence is an empty sequence, abort these steps.
  92. // The user agent SHOULD notify developers when the steps are aborted to notify that registration has been aborted.
  93. // For example, a console warning might be appropriate.
  94. if (entry_types.is_empty()) {
  95. dbgln("Potential FIXME: Returning from PerformanceObserver::observe() as we don't support any of the specified types (or none was specified).");
  96. return {};
  97. }
  98. // 4. If the list of registered performance observer objects of relevantGlobal contains a registered performance
  99. // observer whose observer is this, replace its options list with a list containing options as its only item.
  100. // 5. Otherwise, create and append a registered performance observer object to the list of registered performance
  101. // observer objects of relevantGlobal, with observer set to this and options list set to a list containing
  102. // options as its only item.
  103. // NOTE: See the comment on PerformanceObserver::options_list about why this doesn't create a separate registered
  104. // performance observer object.
  105. m_options_list.clear();
  106. m_options_list.append(options);
  107. relevant_global->register_performance_observer({}, *this);
  108. }
  109. // 7. Otherwise, run the following steps:
  110. else {
  111. // 1. Assert that this's observer type is "single".
  112. VERIFY(m_observer_type == ObserverType::Single);
  113. // 2. If options's type is not contained in the relevantGlobal's frozen array of supported entry types, abort these steps.
  114. // The user agent SHOULD notify developers when this happens, for instance via a console warning.
  115. VERIFY(options.type.has_value());
  116. auto& type = options.type.value();
  117. bool recognized_type = false;
  118. #define __ENUMERATE_SUPPORTED_PERFORMANCE_ENTRY_TYPES(entry_type, cpp_class) \
  119. if (!recognized_type && entry_type == type) \
  120. recognized_type = true;
  121. ENUMERATE_SUPPORTED_PERFORMANCE_ENTRY_TYPES
  122. #undef __ENUMERATE_SUPPORTED_PERFORMANCE_ENTRY_TYPES
  123. if (!recognized_type) {
  124. dbgln("Potential FIXME: Returning from PerformanceObserver::observe() as we don't support the PerformanceEntry type '{}'", type);
  125. return {};
  126. }
  127. // 3. If the list of registered performance observer objects of relevantGlobal contains a registered performance
  128. // observer obs whose observer is this:
  129. if (relevant_global->has_registered_performance_observer(*this)) {
  130. // 1. If obs's options list contains a PerformanceObserverInit item currentOptions whose type is equal to options's type,
  131. // replace currentOptions with options in obs's options list.
  132. auto index = m_options_list.find_first_index_if([&options](PerformanceObserverInit const& entry) {
  133. return entry.type == options.type;
  134. });
  135. if (index.has_value()) {
  136. m_options_list[index.value()] = options;
  137. } else {
  138. // Otherwise, append options to obs's options list.
  139. m_options_list.append(options);
  140. }
  141. }
  142. // 4. Otherwise, create and append a registered performance observer object to the list of registered performance
  143. // observer objects of relevantGlobal, with observer set to the this and options list set to a list containing
  144. // options as its only item.
  145. else {
  146. m_options_list.clear();
  147. m_options_list.append(options);
  148. relevant_global->register_performance_observer({}, *this);
  149. }
  150. // 5. If options's buffered flag is set:
  151. if (options.buffered.has_value() && options.buffered.value()) {
  152. // 1. Let tuple be the relevant performance entry tuple of options's type and relevantGlobal.
  153. auto const& tuple = relevant_global->relevant_performance_entry_tuple(type);
  154. // 2. For each entry in tuple's performance entry buffer:
  155. for (auto const& entry : tuple.performance_entry_buffer) {
  156. // 1. If should add entry with entry and options as parameters returns true, append entry to the observer buffer.
  157. if (entry->should_add_entry(options) == ShouldAddEntry::Yes)
  158. m_observer_buffer.append(*entry);
  159. }
  160. // 3. Queue the PerformanceObserver task with relevantGlobal as input.
  161. relevant_global->queue_the_performance_observer_task();
  162. }
  163. }
  164. return {};
  165. }
  166. // https://w3c.github.io/performance-timeline/#dom-performanceobserver-disconnect
  167. void PerformanceObserver::disconnect()
  168. {
  169. // 1. Remove this from the list of registered performance observer objects of relevant global object.
  170. auto* relevant_global = dynamic_cast<HTML::WindowOrWorkerGlobalScopeMixin*>(&HTML::relevant_global_object(*this));
  171. VERIFY(relevant_global);
  172. relevant_global->unregister_performance_observer({}, *this);
  173. // 2. Empty this's observer buffer.
  174. m_observer_buffer.clear();
  175. // 3. Empty this's options list.
  176. m_options_list.clear();
  177. }
  178. // https://w3c.github.io/performance-timeline/#dom-performanceobserver-takerecords
  179. Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>> PerformanceObserver::take_records()
  180. {
  181. // The takeRecords() method must return a copy of this's observer buffer, and also empty this's observer buffer.
  182. Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>> records;
  183. for (auto& record : m_observer_buffer)
  184. records.append(*record);
  185. m_observer_buffer.clear();
  186. return records;
  187. }
  188. void PerformanceObserver::unset_requires_dropped_entries(Badge<HTML::WindowOrWorkerGlobalScopeMixin>)
  189. {
  190. m_requires_dropped_entries = false;
  191. }
  192. void PerformanceObserver::append_to_observer_buffer(Badge<HTML::WindowOrWorkerGlobalScopeMixin>, JS::NonnullGCPtr<PerformanceTimeline::PerformanceEntry> entry)
  193. {
  194. m_observer_buffer.append(entry);
  195. }
  196. }