| 
									
										
										
										
											2020-09-29 18:19:18 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2023-03-22 19:12:57 +00:00
										 |  |  |  * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-09-29 18:19:18 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-09-29 18:19:18 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <LibWeb/DOM/Document.h>
 | 
					
						
							|  |  |  | #include <LibWeb/DOM/Event.h>
 | 
					
						
							|  |  |  | #include <LibWeb/DOM/EventDispatcher.h>
 | 
					
						
							| 
									
										
										
										
											2022-03-07 23:08:26 +01:00
										 |  |  | #include <LibWeb/HTML/Window.h>
 | 
					
						
							| 
									
										
										
										
											2020-09-29 18:19:18 +02:00
										 |  |  | #include <LibWeb/HighResolutionTime/Performance.h>
 | 
					
						
							| 
									
										
										
										
											2022-08-31 19:12:11 +02:00
										 |  |  | #include <LibWeb/NavigationTiming/PerformanceTiming.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-23 17:07:52 +00:00
										 |  |  | #include <LibWeb/PerformanceTimeline/EntryTypes.h>
 | 
					
						
							| 
									
										
										
										
											2020-09-29 18:19:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web::HighResolutionTime { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-07 23:08:26 +01:00
										 |  |  | Performance::Performance(HTML::Window& window) | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     : DOM::EventTarget(window.realm()) | 
					
						
							| 
									
										
										
										
											2020-09-29 18:19:18 +02:00
										 |  |  |     , m_window(window) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_timer.start(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-14 13:21:51 -06:00
										 |  |  | Performance::~Performance() = default; | 
					
						
							| 
									
										
										
										
											2020-09-29 18:19:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:33:35 -05:00
										 |  |  | JS::ThrowCompletionOr<void> Performance::initialize(JS::Realm& realm) | 
					
						
							| 
									
										
										
										
											2023-01-10 06:28:20 -05:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-28 12:33:35 -05:00
										 |  |  |     MUST_OR_THROW_OOM(Base::initialize(realm)); | 
					
						
							| 
									
										
										
										
											2023-01-10 06:28:20 -05:00
										 |  |  |     set_prototype(&Bindings::ensure_web_prototype<Bindings::PerformancePrototype>(realm, "Performance")); | 
					
						
							| 
									
										
										
										
											2023-01-28 12:33:35 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2023-01-10 06:28:20 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  | void Performance::visit_edges(Cell::Visitor& visitor) | 
					
						
							| 
									
										
										
										
											2020-09-29 18:19:18 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     Base::visit_edges(visitor); | 
					
						
							|  |  |  |     visitor.visit(m_window.ptr()); | 
					
						
							| 
									
										
										
										
											2022-08-31 19:12:11 +02:00
										 |  |  |     visitor.visit(m_timing.ptr()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | JS::GCPtr<NavigationTiming::PerformanceTiming> Performance::timing() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (!m_timing) | 
					
						
							| 
									
										
										
										
											2023-01-28 13:39:44 -05:00
										 |  |  |         m_timing = heap().allocate<NavigationTiming::PerformanceTiming>(realm(), *m_window).release_allocated_value_but_fixme_should_propagate_errors(); | 
					
						
							| 
									
										
										
										
											2022-08-31 19:12:11 +02:00
										 |  |  |     return m_timing; | 
					
						
							| 
									
										
										
										
											2020-09-29 18:19:18 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  | double Performance::time_origin() const | 
					
						
							| 
									
										
										
										
											2020-09-29 18:19:18 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-01 22:30:31 -07:00
										 |  |  |     return static_cast<double>(m_timer.origin_time().to_milliseconds()); | 
					
						
							| 
									
										
										
										
											2020-09-29 18:19:18 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-23 17:07:52 +00:00
										 |  |  | // https://w3c.github.io/user-timing/#mark-method
 | 
					
						
							|  |  |  | WebIDL::ExceptionOr<JS::NonnullGCPtr<UserTiming::PerformanceMark>> Performance::mark(String const& mark_name, UserTiming::PerformanceMarkOptions const& mark_options) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto& realm = this->realm(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 1. Run the PerformanceMark constructor and let entry be the newly created object.
 | 
					
						
							|  |  |  |     auto entry = TRY(UserTiming::PerformanceMark::construct_impl(realm, mark_name, mark_options)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 2. Queue entry.
 | 
					
						
							|  |  |  |     auto* window_or_worker = dynamic_cast<HTML::WindowOrWorkerGlobalScopeMixin*>(&realm.global_object()); | 
					
						
							|  |  |  |     VERIFY(window_or_worker); | 
					
						
							|  |  |  |     TRY(window_or_worker->queue_performance_entry(entry)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 3. Add entry to the performance entry buffer.
 | 
					
						
							|  |  |  |     // FIXME: This seems to be a holdover from moving to the `queue` structure for PerformanceObserver, as this would cause a double append.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 4. Return entry.
 | 
					
						
							|  |  |  |     return entry; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Performance::clear_marks(Optional<String> mark_name) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto& realm = this->realm(); | 
					
						
							|  |  |  |     auto* window_or_worker = dynamic_cast<HTML::WindowOrWorkerGlobalScopeMixin*>(&realm.global_object()); | 
					
						
							|  |  |  |     VERIFY(window_or_worker); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 1. If markName is omitted, remove all PerformanceMark objects from the performance entry buffer.
 | 
					
						
							|  |  |  |     if (!mark_name.has_value()) { | 
					
						
							|  |  |  |         window_or_worker->clear_performance_entry_buffer({}, PerformanceTimeline::EntryTypes::mark); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 2. Otherwise, remove all PerformanceMark objects listed in the performance entry buffer whose name is markName.
 | 
					
						
							|  |  |  |     window_or_worker->remove_entries_from_performance_entry_buffer({}, PerformanceTimeline::EntryTypes::mark, mark_name.value()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 3. Return undefined.
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-22 19:12:57 +00:00
										 |  |  | // https://www.w3.org/TR/performance-timeline/#getentries-method
 | 
					
						
							|  |  |  | WebIDL::ExceptionOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> Performance::get_entries() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto& realm = this->realm(); | 
					
						
							|  |  |  |     auto& vm = this->vm(); | 
					
						
							|  |  |  |     auto* window_or_worker = dynamic_cast<HTML::WindowOrWorkerGlobalScopeMixin*>(&realm.global_object()); | 
					
						
							|  |  |  |     VERIFY(window_or_worker); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Returns a PerformanceEntryList object returned by the filter buffer map by name and type algorithm with name and
 | 
					
						
							|  |  |  |     // type set to null.
 | 
					
						
							|  |  |  |     return TRY_OR_THROW_OOM(vm, window_or_worker->filter_buffer_map_by_name_and_type(/* name= */ Optional<String> {}, /* type= */ Optional<String> {})); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://www.w3.org/TR/performance-timeline/#dom-performance-getentriesbytype
 | 
					
						
							|  |  |  | WebIDL::ExceptionOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> Performance::get_entries_by_type(String const& type) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto& realm = this->realm(); | 
					
						
							|  |  |  |     auto& vm = this->vm(); | 
					
						
							|  |  |  |     auto* window_or_worker = dynamic_cast<HTML::WindowOrWorkerGlobalScopeMixin*>(&realm.global_object()); | 
					
						
							|  |  |  |     VERIFY(window_or_worker); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Returns a PerformanceEntryList object returned by filter buffer map by name and type algorithm with name set to null,
 | 
					
						
							|  |  |  |     // and type set to the method's input type parameter.
 | 
					
						
							|  |  |  |     return TRY_OR_THROW_OOM(vm, window_or_worker->filter_buffer_map_by_name_and_type(/* name= */ Optional<String> {}, type)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://www.w3.org/TR/performance-timeline/#dom-performance-getentriesbyname
 | 
					
						
							|  |  |  | WebIDL::ExceptionOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> Performance::get_entries_by_name(String const& name, Optional<String> type) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto& realm = this->realm(); | 
					
						
							|  |  |  |     auto& vm = this->vm(); | 
					
						
							|  |  |  |     auto* window_or_worker = dynamic_cast<HTML::WindowOrWorkerGlobalScopeMixin*>(&realm.global_object()); | 
					
						
							|  |  |  |     VERIFY(window_or_worker); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Returns a PerformanceEntryList object returned by filter buffer map by name and type algorithm with name set to the
 | 
					
						
							|  |  |  |     // method input name parameter, and type set to null if optional entryType is omitted, or set to the method's input type
 | 
					
						
							|  |  |  |     // parameter otherwise.
 | 
					
						
							|  |  |  |     return TRY_OR_THROW_OOM(vm, window_or_worker->filter_buffer_map_by_name_and_type(name, type)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-29 18:19:18 +02:00
										 |  |  | } |