mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-09 09:34:57 +09:00
Everywhere: Hoist the Libraries folder to the top-level
This commit is contained in:
parent
950e819ee7
commit
93712b24bf
Notes:
github-actions[bot]
2024-11-10 11:51:52 +00:00
Author: https://github.com/trflynn89
Commit: 93712b24bf
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2256
Reviewed-by: https://github.com/sideshowbarker
4547 changed files with 104 additions and 113 deletions
33
Libraries/LibWeb/PerformanceTimeline/EntryTypes.cpp
Normal file
33
Libraries/LibWeb/PerformanceTimeline/EntryTypes.cpp
Normal file
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibWeb/PerformanceTimeline/EntryTypes.h>
|
||||
|
||||
namespace Web::PerformanceTimeline::EntryTypes {
|
||||
|
||||
#define __ENUMERATE_PERFORMANCE_TIMELINE_ENTRY_TYPE(name) FlyString name;
|
||||
ENUMERATE_PERFORMANCE_TIMELINE_ENTRY_TYPES
|
||||
#undef __ENUMERATE_PERFORMANCE_TIMELINE_ENTRY_TYPE
|
||||
|
||||
void initialize_strings()
|
||||
{
|
||||
static bool s_initialized = false;
|
||||
VERIFY(!s_initialized);
|
||||
|
||||
#define __ENUMERATE_PERFORMANCE_TIMELINE_ENTRY_TYPE(name) \
|
||||
name = #name##_fly_string;
|
||||
ENUMERATE_PERFORMANCE_TIMELINE_ENTRY_TYPES
|
||||
#undef __ENUMERATE_PERFORMANCE_TIMELINE_ENTRY_TYPE
|
||||
|
||||
// NOTE: Special cases for attributes with dashes in them.
|
||||
first_input = "first-input"_fly_string;
|
||||
largest_contentful_paint = "largest-contentful-paint"_fly_string;
|
||||
layout_shift = "layout-shift"_fly_string;
|
||||
|
||||
s_initialized = true;
|
||||
}
|
||||
|
||||
}
|
33
Libraries/LibWeb/PerformanceTimeline/EntryTypes.h
Normal file
33
Libraries/LibWeb/PerformanceTimeline/EntryTypes.h
Normal file
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/FlyString.h>
|
||||
|
||||
namespace Web::PerformanceTimeline::EntryTypes {
|
||||
|
||||
// https://w3c.github.io/timing-entrytypes-registry/#registry
|
||||
#define ENUMERATE_PERFORMANCE_TIMELINE_ENTRY_TYPES \
|
||||
__ENUMERATE_PERFORMANCE_TIMELINE_ENTRY_TYPE(element) \
|
||||
__ENUMERATE_PERFORMANCE_TIMELINE_ENTRY_TYPE(event) \
|
||||
__ENUMERATE_PERFORMANCE_TIMELINE_ENTRY_TYPE(first_input) \
|
||||
__ENUMERATE_PERFORMANCE_TIMELINE_ENTRY_TYPE(largest_contentful_paint) \
|
||||
__ENUMERATE_PERFORMANCE_TIMELINE_ENTRY_TYPE(layout_shift) \
|
||||
__ENUMERATE_PERFORMANCE_TIMELINE_ENTRY_TYPE(longtask) \
|
||||
__ENUMERATE_PERFORMANCE_TIMELINE_ENTRY_TYPE(mark) \
|
||||
__ENUMERATE_PERFORMANCE_TIMELINE_ENTRY_TYPE(measure) \
|
||||
__ENUMERATE_PERFORMANCE_TIMELINE_ENTRY_TYPE(navigation) \
|
||||
__ENUMERATE_PERFORMANCE_TIMELINE_ENTRY_TYPE(resource) \
|
||||
__ENUMERATE_PERFORMANCE_TIMELINE_ENTRY_TYPE(paint)
|
||||
|
||||
#define __ENUMERATE_PERFORMANCE_TIMELINE_ENTRY_TYPE(name) extern FlyString name;
|
||||
ENUMERATE_PERFORMANCE_TIMELINE_ENTRY_TYPES
|
||||
#undef __ENUMERATE_PERFORMANCE_TIMELINE_ENTRY_TYPE
|
||||
|
||||
void initialize_strings();
|
||||
|
||||
}
|
29
Libraries/LibWeb/PerformanceTimeline/PerformanceEntry.cpp
Normal file
29
Libraries/LibWeb/PerformanceTimeline/PerformanceEntry.cpp
Normal file
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibWeb/Bindings/Intrinsics.h>
|
||||
#include <LibWeb/Bindings/PerformanceEntryPrototype.h>
|
||||
#include <LibWeb/PerformanceTimeline/PerformanceEntry.h>
|
||||
|
||||
namespace Web::PerformanceTimeline {
|
||||
|
||||
PerformanceEntry::PerformanceEntry(JS::Realm& realm, String const& name, HighResolutionTime::DOMHighResTimeStamp start_time, HighResolutionTime::DOMHighResTimeStamp duration)
|
||||
: Bindings::PlatformObject(realm)
|
||||
, m_name(name)
|
||||
, m_start_time(start_time)
|
||||
, m_duration(duration)
|
||||
{
|
||||
}
|
||||
|
||||
PerformanceEntry::~PerformanceEntry() = default;
|
||||
|
||||
void PerformanceEntry::initialize(JS::Realm& realm)
|
||||
{
|
||||
Base::initialize(realm);
|
||||
WEB_SET_PROTOTYPE_FOR_INTERFACE(PerformanceEntry);
|
||||
}
|
||||
|
||||
}
|
56
Libraries/LibWeb/PerformanceTimeline/PerformanceEntry.h
Normal file
56
Libraries/LibWeb/PerformanceTimeline/PerformanceEntry.h
Normal file
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <LibWeb/Bindings/PlatformObject.h>
|
||||
#include <LibWeb/HighResolutionTime/DOMHighResTimeStamp.h>
|
||||
|
||||
namespace Web::PerformanceTimeline {
|
||||
|
||||
enum class AvailableFromTimeline {
|
||||
No,
|
||||
Yes,
|
||||
};
|
||||
|
||||
enum class ShouldAddEntry {
|
||||
No,
|
||||
Yes,
|
||||
};
|
||||
|
||||
// https://www.w3.org/TR/performance-timeline/#dom-performanceentry
|
||||
class PerformanceEntry : public Bindings::PlatformObject {
|
||||
WEB_PLATFORM_OBJECT(PerformanceEntry, Bindings::PlatformObject);
|
||||
|
||||
public:
|
||||
virtual ~PerformanceEntry();
|
||||
|
||||
// https://www.w3.org/TR/performance-timeline/#dom-performanceentry-entrytype
|
||||
virtual FlyString const& entry_type() const = 0;
|
||||
|
||||
String const& name() const { return m_name; }
|
||||
HighResolutionTime::DOMHighResTimeStamp start_time() const { return m_start_time; }
|
||||
HighResolutionTime::DOMHighResTimeStamp duration() const { return m_duration; }
|
||||
|
||||
// https://w3c.github.io/timing-entrytypes-registry/#dfn-should-add-entry
|
||||
virtual PerformanceTimeline::ShouldAddEntry should_add_entry(Optional<PerformanceObserverInit const&> = {}) const = 0;
|
||||
|
||||
protected:
|
||||
PerformanceEntry(JS::Realm&, String const& name, HighResolutionTime::DOMHighResTimeStamp start_time, HighResolutionTime::DOMHighResTimeStamp duration);
|
||||
virtual void initialize(JS::Realm&) override;
|
||||
|
||||
private:
|
||||
// https://www.w3.org/TR/performance-timeline/#dom-performanceentry-name
|
||||
String m_name;
|
||||
|
||||
// https://www.w3.org/TR/performance-timeline/#dom-performanceentry-starttime
|
||||
HighResolutionTime::DOMHighResTimeStamp m_start_time { 0.0 };
|
||||
|
||||
// https://www.w3.org/TR/performance-timeline/#dom-performanceentry-duration
|
||||
HighResolutionTime::DOMHighResTimeStamp m_duration { 0.0 };
|
||||
};
|
||||
|
||||
}
|
11
Libraries/LibWeb/PerformanceTimeline/PerformanceEntry.idl
Normal file
11
Libraries/LibWeb/PerformanceTimeline/PerformanceEntry.idl
Normal file
|
@ -0,0 +1,11 @@
|
|||
#import <HighResolutionTime/DOMHighResTimeStamp.idl>
|
||||
|
||||
// https://www.w3.org/TR/performance-timeline/#dom-performanceentry
|
||||
[Exposed=(Window,Worker)]
|
||||
interface PerformanceEntry {
|
||||
readonly attribute DOMString name;
|
||||
readonly attribute DOMString entryType;
|
||||
readonly attribute DOMHighResTimeStamp startTime;
|
||||
readonly attribute DOMHighResTimeStamp duration;
|
||||
[Default] object toJSON();
|
||||
};
|
55
Libraries/LibWeb/PerformanceTimeline/PerformanceEntryTuple.h
Normal file
55
Libraries/LibWeb/PerformanceTimeline/PerformanceEntryTuple.h
Normal file
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <LibWeb/PerformanceTimeline/PerformanceEntry.h>
|
||||
|
||||
namespace Web::PerformanceTimeline {
|
||||
|
||||
// https://www.w3.org/TR/performance-timeline/#dfn-performance-entry-buffer-map
|
||||
struct PerformanceEntryTuple {
|
||||
// https://www.w3.org/TR/performance-timeline/#dfn-performance-entry-buffer
|
||||
// A performance entry buffer to store PerformanceEntry objects, that is initially empty.
|
||||
Vector<JS::NonnullGCPtr<PerformanceEntry>> performance_entry_buffer;
|
||||
|
||||
// https://www.w3.org/TR/performance-timeline/#dfn-maxbuffersize
|
||||
// An integer maxBufferSize, initialized to the registry value for this entry type.
|
||||
// NOTE: The empty state represents Infinite size.
|
||||
Optional<u64> max_buffer_size;
|
||||
|
||||
// https://www.w3.org/TR/performance-timeline/#dfn-availablefromtimeline
|
||||
// A boolean availableFromTimeline, initialized to the registry value for this entry type.
|
||||
AvailableFromTimeline available_from_timeline { AvailableFromTimeline::No };
|
||||
|
||||
// https://www.w3.org/TR/performance-timeline/#dfn-dropped-entries-count
|
||||
// An integer dropped entries count that is initially 0.
|
||||
u64 dropped_entries_count { 0 };
|
||||
|
||||
// https://www.w3.org/TR/performance-timeline/#dfn-determine-if-a-performance-entry-buffer-is-full
|
||||
bool is_full()
|
||||
{
|
||||
// 1. Let num current entries be the size of tuple's performance entry buffer.
|
||||
auto num_current_entries = performance_entry_buffer.size();
|
||||
|
||||
// 2. If num current entries is less than tuples's maxBufferSize, return false.
|
||||
if (!max_buffer_size.has_value() || num_current_entries < max_buffer_size.value())
|
||||
return false;
|
||||
|
||||
// 3. Increase tuple's dropped entries count by 1.
|
||||
++dropped_entries_count;
|
||||
|
||||
// 4. Return true.
|
||||
return true;
|
||||
}
|
||||
|
||||
void visit_edges(JS::Cell::Visitor& visitor)
|
||||
{
|
||||
visitor.visit(performance_entry_buffer);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
241
Libraries/LibWeb/PerformanceTimeline/PerformanceObserver.cpp
Normal file
241
Libraries/LibWeb/PerformanceTimeline/PerformanceObserver.cpp
Normal file
|
@ -0,0 +1,241 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibWeb/Bindings/Intrinsics.h>
|
||||
#include <LibWeb/Bindings/PerformanceObserverPrototype.h>
|
||||
#include <LibWeb/HTML/WindowOrWorkerGlobalScope.h>
|
||||
#include <LibWeb/HighResolutionTime/SupportedPerformanceTypes.h>
|
||||
#include <LibWeb/PerformanceTimeline/EntryTypes.h>
|
||||
#include <LibWeb/PerformanceTimeline/PerformanceEntry.h>
|
||||
#include <LibWeb/PerformanceTimeline/PerformanceObserver.h>
|
||||
#include <LibWeb/WebIDL/CallbackType.h>
|
||||
#include <LibWeb/WebIDL/ExceptionOr.h>
|
||||
|
||||
namespace Web::PerformanceTimeline {
|
||||
|
||||
JS_DEFINE_ALLOCATOR(PerformanceObserver);
|
||||
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<PerformanceObserver>> PerformanceObserver::construct_impl(JS::Realm& realm, JS::GCPtr<WebIDL::CallbackType> callback)
|
||||
{
|
||||
return realm.heap().allocate<PerformanceObserver>(realm, realm, callback);
|
||||
}
|
||||
|
||||
PerformanceObserver::PerformanceObserver(JS::Realm& realm, JS::GCPtr<WebIDL::CallbackType> callback)
|
||||
: Bindings::PlatformObject(realm)
|
||||
, m_callback(move(callback))
|
||||
{
|
||||
}
|
||||
|
||||
PerformanceObserver::~PerformanceObserver() = default;
|
||||
|
||||
void PerformanceObserver::initialize(JS::Realm& realm)
|
||||
{
|
||||
Base::initialize(realm);
|
||||
WEB_SET_PROTOTYPE_FOR_INTERFACE(PerformanceObserver);
|
||||
}
|
||||
|
||||
void PerformanceObserver::visit_edges(Cell::Visitor& visitor)
|
||||
{
|
||||
Base::visit_edges(visitor);
|
||||
visitor.visit(m_callback);
|
||||
visitor.visit(m_observer_buffer);
|
||||
}
|
||||
|
||||
// https://w3c.github.io/performance-timeline/#dom-performanceobserver-observe
|
||||
WebIDL::ExceptionOr<void> PerformanceObserver::observe(PerformanceObserverInit& options)
|
||||
{
|
||||
auto& realm = this->realm();
|
||||
|
||||
// 1. Let relevantGlobal be this's relevant global object.
|
||||
auto* relevant_global = dynamic_cast<HTML::WindowOrWorkerGlobalScopeMixin*>(&HTML::relevant_global_object(*this));
|
||||
VERIFY(relevant_global);
|
||||
|
||||
// 2. If options's entryTypes and type members are both omitted, then throw a "TypeError".
|
||||
if (!options.entry_types.has_value() && !options.type.has_value())
|
||||
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Must specify one of entryTypes or type"sv };
|
||||
|
||||
// 3. If options's entryTypes is present and any other member is also present, then throw a "TypeError".
|
||||
if (options.entry_types.has_value() && (options.type.has_value() || options.buffered.has_value()))
|
||||
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Cannot specify type or buffered if entryTypes is specified"sv };
|
||||
|
||||
// 4. Update or check this's observer type by running these steps:
|
||||
// 1. If this's observer type is "undefined":
|
||||
if (m_observer_type == ObserverType::Undefined) {
|
||||
// 1. If options's entryTypes member is present, then set this's observer type to "multiple".
|
||||
if (options.entry_types.has_value())
|
||||
m_observer_type = ObserverType::Multiple;
|
||||
|
||||
// 2. If options's type member is present, then set this's observer type to "single".
|
||||
if (options.type.has_value())
|
||||
m_observer_type = ObserverType::Single;
|
||||
}
|
||||
// 2. If this's observer type is "single" and options's entryTypes member is present, then throw an "InvalidModificationError".
|
||||
else if (m_observer_type == ObserverType::Single) {
|
||||
if (options.entry_types.has_value())
|
||||
return WebIDL::InvalidModificationError::create(realm, "Cannot change a PerformanceObserver from observing a single type to observing multiple types"_string);
|
||||
}
|
||||
// 3. If this's observer type is "multiple" and options's type member is present, then throw an "InvalidModificationError".
|
||||
else if (m_observer_type == ObserverType::Multiple) {
|
||||
if (options.type.has_value())
|
||||
return WebIDL::InvalidModificationError::create(realm, "Cannot change a PerformanceObserver from observing multiple types to observing a single type"_string);
|
||||
}
|
||||
|
||||
// 5. Set this's requires dropped entries to true.
|
||||
m_requires_dropped_entries = true;
|
||||
|
||||
// 6. If this's observer type is "multiple", run the following steps:
|
||||
if (m_observer_type == ObserverType::Multiple) {
|
||||
// 1. Let entry types be options's entryTypes sequence.
|
||||
VERIFY(options.entry_types.has_value());
|
||||
auto& entry_types = options.entry_types.value();
|
||||
|
||||
// 2. Remove all types from entry types that are not contained in relevantGlobal's frozen array of supported entry types.
|
||||
// The user agent SHOULD notify developers if entry types is modified. For example, a console warning listing removed
|
||||
// types might be appropriate.
|
||||
entry_types.remove_all_matching([](String const& type) {
|
||||
#define __ENUMERATE_SUPPORTED_PERFORMANCE_ENTRY_TYPES(entry_type, cpp_class) \
|
||||
if (entry_type == type) \
|
||||
return false;
|
||||
ENUMERATE_SUPPORTED_PERFORMANCE_ENTRY_TYPES
|
||||
#undef __ENUMERATE_SUPPORTED_PERFORMANCE_ENTRY_TYPES
|
||||
|
||||
dbgln("Potential FIXME: Removing unsupported PerformanceEntry type '{}' from list of observed types in PerformanceObserver::observe()", type);
|
||||
return true;
|
||||
});
|
||||
|
||||
// 3. If the resulting entry types sequence is an empty sequence, abort these steps.
|
||||
// The user agent SHOULD notify developers when the steps are aborted to notify that registration has been aborted.
|
||||
// For example, a console warning might be appropriate.
|
||||
if (entry_types.is_empty()) {
|
||||
dbgln("Potential FIXME: Returning from PerformanceObserver::observe() as we don't support any of the specified types (or none was specified).");
|
||||
return {};
|
||||
}
|
||||
|
||||
// 4. If the list of registered performance observer objects of relevantGlobal contains a registered performance
|
||||
// observer whose observer is this, replace its options list with a list containing options as its only item.
|
||||
// 5. Otherwise, create and append a registered performance observer object to the list of registered performance
|
||||
// observer objects of relevantGlobal, with observer set to this and options list set to a list containing
|
||||
// options as its only item.
|
||||
// NOTE: See the comment on PerformanceObserver::options_list about why this doesn't create a separate registered
|
||||
// performance observer object.
|
||||
m_options_list.clear();
|
||||
m_options_list.append(options);
|
||||
relevant_global->register_performance_observer({}, *this);
|
||||
}
|
||||
// 7. Otherwise, run the following steps:
|
||||
else {
|
||||
// 1. Assert that this's observer type is "single".
|
||||
VERIFY(m_observer_type == ObserverType::Single);
|
||||
|
||||
// 2. If options's type is not contained in the relevantGlobal's frozen array of supported entry types, abort these steps.
|
||||
// The user agent SHOULD notify developers when this happens, for instance via a console warning.
|
||||
VERIFY(options.type.has_value());
|
||||
auto& type = options.type.value();
|
||||
bool recognized_type = false;
|
||||
|
||||
#define __ENUMERATE_SUPPORTED_PERFORMANCE_ENTRY_TYPES(entry_type, cpp_class) \
|
||||
if (!recognized_type && entry_type == type) \
|
||||
recognized_type = true;
|
||||
ENUMERATE_SUPPORTED_PERFORMANCE_ENTRY_TYPES
|
||||
#undef __ENUMERATE_SUPPORTED_PERFORMANCE_ENTRY_TYPES
|
||||
|
||||
if (!recognized_type) {
|
||||
dbgln("Potential FIXME: Returning from PerformanceObserver::observe() as we don't support the PerformanceEntry type '{}'", type);
|
||||
return {};
|
||||
}
|
||||
|
||||
// 3. If the list of registered performance observer objects of relevantGlobal contains a registered performance
|
||||
// observer obs whose observer is this:
|
||||
if (relevant_global->has_registered_performance_observer(*this)) {
|
||||
// 1. If obs's options list contains a PerformanceObserverInit item currentOptions whose type is equal to options's type,
|
||||
// replace currentOptions with options in obs's options list.
|
||||
auto index = m_options_list.find_first_index_if([&options](PerformanceObserverInit const& entry) {
|
||||
return entry.type == options.type;
|
||||
});
|
||||
if (index.has_value()) {
|
||||
m_options_list[index.value()] = options;
|
||||
} else {
|
||||
// Otherwise, append options to obs's options list.
|
||||
m_options_list.append(options);
|
||||
}
|
||||
}
|
||||
// 4. Otherwise, create and append a registered performance observer object to the list of registered performance
|
||||
// observer objects of relevantGlobal, with observer set to the this and options list set to a list containing
|
||||
// options as its only item.
|
||||
else {
|
||||
m_options_list.clear();
|
||||
m_options_list.append(options);
|
||||
relevant_global->register_performance_observer({}, *this);
|
||||
}
|
||||
|
||||
// 5. If options's buffered flag is set:
|
||||
if (options.buffered.has_value() && options.buffered.value()) {
|
||||
// 1. Let tuple be the relevant performance entry tuple of options's type and relevantGlobal.
|
||||
auto const& tuple = relevant_global->relevant_performance_entry_tuple(type);
|
||||
|
||||
// 2. For each entry in tuple's performance entry buffer:
|
||||
for (auto const& entry : tuple.performance_entry_buffer) {
|
||||
// 1. If should add entry with entry and options as parameters returns true, append entry to the observer buffer.
|
||||
if (entry->should_add_entry(options) == ShouldAddEntry::Yes)
|
||||
m_observer_buffer.append(*entry);
|
||||
}
|
||||
|
||||
// 3. Queue the PerformanceObserver task with relevantGlobal as input.
|
||||
relevant_global->queue_the_performance_observer_task();
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
// https://w3c.github.io/performance-timeline/#dom-performanceobserver-disconnect
|
||||
void PerformanceObserver::disconnect()
|
||||
{
|
||||
// 1. Remove this from the list of registered performance observer objects of relevant global object.
|
||||
auto* relevant_global = dynamic_cast<HTML::WindowOrWorkerGlobalScopeMixin*>(&HTML::relevant_global_object(*this));
|
||||
VERIFY(relevant_global);
|
||||
relevant_global->unregister_performance_observer({}, *this);
|
||||
|
||||
// 2. Empty this's observer buffer.
|
||||
m_observer_buffer.clear();
|
||||
|
||||
// 3. Empty this's options list.
|
||||
m_options_list.clear();
|
||||
}
|
||||
|
||||
// https://w3c.github.io/performance-timeline/#dom-performanceobserver-takerecords
|
||||
Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>> PerformanceObserver::take_records()
|
||||
{
|
||||
// The takeRecords() method must return a copy of this's observer buffer, and also empty this's observer buffer.
|
||||
Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>> records;
|
||||
for (auto& record : m_observer_buffer)
|
||||
records.append(*record);
|
||||
m_observer_buffer.clear();
|
||||
return records;
|
||||
}
|
||||
|
||||
// https://w3c.github.io/performance-timeline/#dom-performanceobserver-supportedentrytypes
|
||||
JS::NonnullGCPtr<JS::Object> PerformanceObserver::supported_entry_types(JS::VM& vm)
|
||||
{
|
||||
// 1. Let globalObject be the environment settings object's global object.
|
||||
auto* window_or_worker = dynamic_cast<HTML::WindowOrWorkerGlobalScopeMixin*>(&vm.get_global_object());
|
||||
VERIFY(window_or_worker);
|
||||
|
||||
// 2. Return globalObject's frozen array of supported entry types.
|
||||
return window_or_worker->supported_entry_types();
|
||||
}
|
||||
|
||||
void PerformanceObserver::unset_requires_dropped_entries(Badge<HTML::WindowOrWorkerGlobalScopeMixin>)
|
||||
{
|
||||
m_requires_dropped_entries = false;
|
||||
}
|
||||
|
||||
void PerformanceObserver::append_to_observer_buffer(Badge<HTML::WindowOrWorkerGlobalScopeMixin>, JS::NonnullGCPtr<PerformanceTimeline::PerformanceEntry> entry)
|
||||
{
|
||||
m_observer_buffer.append(entry);
|
||||
}
|
||||
|
||||
}
|
80
Libraries/LibWeb/PerformanceTimeline/PerformanceObserver.h
Normal file
80
Libraries/LibWeb/PerformanceTimeline/PerformanceObserver.h
Normal file
|
@ -0,0 +1,80 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <LibWeb/Bindings/PlatformObject.h>
|
||||
#include <LibWeb/HTML/WindowOrWorkerGlobalScope.h>
|
||||
|
||||
namespace Web::PerformanceTimeline {
|
||||
|
||||
// https://w3c.github.io/performance-timeline/#dom-performanceobserverinit
|
||||
struct PerformanceObserverInit {
|
||||
Optional<Vector<String>> entry_types;
|
||||
Optional<String> type;
|
||||
Optional<bool> buffered;
|
||||
};
|
||||
|
||||
// https://w3c.github.io/performance-timeline/#dom-performanceobserver
|
||||
class PerformanceObserver final : public Bindings::PlatformObject {
|
||||
WEB_PLATFORM_OBJECT(PerformanceObserver, Bindings::PlatformObject);
|
||||
JS_DECLARE_ALLOCATOR(PerformanceObserver);
|
||||
|
||||
public:
|
||||
enum class ObserverType {
|
||||
Undefined,
|
||||
Single,
|
||||
Multiple,
|
||||
};
|
||||
|
||||
static WebIDL::ExceptionOr<JS::NonnullGCPtr<PerformanceObserver>> construct_impl(JS::Realm&, JS::GCPtr<WebIDL::CallbackType>);
|
||||
virtual ~PerformanceObserver() override;
|
||||
|
||||
WebIDL::ExceptionOr<void> observe(PerformanceObserverInit& options);
|
||||
void disconnect();
|
||||
Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>> take_records();
|
||||
|
||||
bool requires_dropped_entries() const { return m_requires_dropped_entries; }
|
||||
void unset_requires_dropped_entries(Badge<HTML::WindowOrWorkerGlobalScopeMixin>);
|
||||
|
||||
Vector<PerformanceObserverInit> const& options_list() const { return m_options_list; }
|
||||
|
||||
WebIDL::CallbackType& callback() { return *m_callback; }
|
||||
|
||||
void append_to_observer_buffer(Badge<HTML::WindowOrWorkerGlobalScopeMixin>, JS::NonnullGCPtr<PerformanceTimeline::PerformanceEntry>);
|
||||
|
||||
static JS::NonnullGCPtr<JS::Object> supported_entry_types(JS::VM&);
|
||||
|
||||
private:
|
||||
PerformanceObserver(JS::Realm&, JS::GCPtr<WebIDL::CallbackType>);
|
||||
|
||||
virtual void initialize(JS::Realm&) override;
|
||||
virtual void visit_edges(Cell::Visitor&) override;
|
||||
|
||||
// https://w3c.github.io/performance-timeline/#dfn-observer-callback
|
||||
// A PerformanceObserverCallback observer callback set on creation.
|
||||
JS::GCPtr<WebIDL::CallbackType> m_callback;
|
||||
|
||||
// https://w3c.github.io/performance-timeline/#dfn-observer-buffer
|
||||
// A PerformanceEntryList object called the observer buffer that is initially empty.
|
||||
Vector<JS::NonnullGCPtr<PerformanceTimeline::PerformanceEntry>> m_observer_buffer;
|
||||
|
||||
// https://w3c.github.io/performance-timeline/#dfn-observer-type
|
||||
// A DOMString observer type which is initially "undefined".
|
||||
ObserverType m_observer_type { ObserverType::Undefined };
|
||||
|
||||
// https://w3c.github.io/performance-timeline/#dfn-requires-dropped-entries
|
||||
// A boolean requires dropped entries which is initially set to false.
|
||||
bool m_requires_dropped_entries { false };
|
||||
|
||||
// https://w3c.github.io/performance-timeline/#dfn-options-list
|
||||
// A registered performance observer is a struct consisting of an observer member (a PerformanceObserver object)
|
||||
// and an options list member (a list of PerformanceObserverInit dictionaries).
|
||||
// NOTE: This doesn't use a separate struct as methods such as disconnect() assume it can access an options list from `this`: a PerformanceObserver.
|
||||
Vector<PerformanceObserverInit> m_options_list;
|
||||
};
|
||||
|
||||
}
|
26
Libraries/LibWeb/PerformanceTimeline/PerformanceObserver.idl
Normal file
26
Libraries/LibWeb/PerformanceTimeline/PerformanceObserver.idl
Normal file
|
@ -0,0 +1,26 @@
|
|||
#import <PerformanceTimeline/PerformanceObserverEntryList.idl>
|
||||
|
||||
// https://w3c.github.io/performance-timeline/#dom-performanceobservercallbackoptions
|
||||
dictionary PerformanceObserverCallbackOptions {
|
||||
unsigned long long droppedEntriesCount;
|
||||
};
|
||||
|
||||
callback PerformanceObserverCallback = undefined (PerformanceObserverEntryList entries, PerformanceObserver observer, optional PerformanceObserverCallbackOptions options = {});
|
||||
|
||||
// https://w3c.github.io/performance-timeline/#dom-performanceobserverinit
|
||||
dictionary PerformanceObserverInit {
|
||||
sequence<DOMString> entryTypes;
|
||||
DOMString type;
|
||||
boolean buffered;
|
||||
};
|
||||
|
||||
// https://w3c.github.io/performance-timeline/#dom-performanceobserver
|
||||
[Exposed=(Window,Worker)]
|
||||
interface PerformanceObserver {
|
||||
constructor(PerformanceObserverCallback callback);
|
||||
undefined observe(optional PerformanceObserverInit options = {});
|
||||
undefined disconnect();
|
||||
PerformanceEntryList takeRecords();
|
||||
// FIXME: [SameObject] static readonly attribute FrozenArray<DOMString> supportedEntryTypes;
|
||||
[SameObject] static readonly attribute any supportedEntryTypes;
|
||||
};
|
|
@ -0,0 +1,92 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/QuickSort.h>
|
||||
#include <LibWeb/Bindings/Intrinsics.h>
|
||||
#include <LibWeb/Bindings/PerformanceObserverEntryListPrototype.h>
|
||||
#include <LibWeb/PerformanceTimeline/PerformanceEntry.h>
|
||||
#include <LibWeb/PerformanceTimeline/PerformanceObserverEntryList.h>
|
||||
#include <LibWeb/WebIDL/ExceptionOr.h>
|
||||
|
||||
namespace Web::PerformanceTimeline {
|
||||
|
||||
JS_DEFINE_ALLOCATOR(PerformanceObserverEntryList);
|
||||
|
||||
PerformanceObserverEntryList::PerformanceObserverEntryList(JS::Realm& realm, Vector<JS::NonnullGCPtr<PerformanceTimeline::PerformanceEntry>>&& entry_list)
|
||||
: Bindings::PlatformObject(realm)
|
||||
, m_entry_list(move(entry_list))
|
||||
{
|
||||
}
|
||||
|
||||
PerformanceObserverEntryList::~PerformanceObserverEntryList() = default;
|
||||
|
||||
void PerformanceObserverEntryList::initialize(JS::Realm& realm)
|
||||
{
|
||||
Base::initialize(realm);
|
||||
WEB_SET_PROTOTYPE_FOR_INTERFACE(PerformanceObserverEntryList);
|
||||
}
|
||||
|
||||
void PerformanceObserverEntryList::visit_edges(Cell::Visitor& visitor)
|
||||
{
|
||||
Base::visit_edges(visitor);
|
||||
visitor.visit(m_entry_list);
|
||||
}
|
||||
|
||||
// https://www.w3.org/TR/performance-timeline/#dfn-filter-buffer-by-name-and-type
|
||||
ErrorOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> filter_buffer_by_name_and_type(Vector<JS::NonnullGCPtr<PerformanceTimeline::PerformanceEntry>> const& buffer, Optional<String> name, Optional<String> type)
|
||||
{
|
||||
// 1. Let result be an initially empty list.
|
||||
Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>> result;
|
||||
|
||||
// 2. For each PerformanceEntry entry in buffer, run the following steps:
|
||||
for (auto const& entry : buffer) {
|
||||
// 1. If type is not null and if type is not identical to entry's entryType attribute, continue to next entry.
|
||||
if (type.has_value() && type.value() != entry->entry_type())
|
||||
continue;
|
||||
|
||||
// 2. If name is not null and if name is not identical to entry's name attribute, continue to next entry.
|
||||
if (name.has_value() && name.value() != entry->name())
|
||||
continue;
|
||||
|
||||
// 3. append entry to result.
|
||||
TRY(result.try_append(entry));
|
||||
}
|
||||
|
||||
// 3. Sort results's entries in chronological order with respect to startTime
|
||||
quick_sort(result, [](auto const& left_entry, auto const& right_entry) {
|
||||
return left_entry->start_time() < right_entry->start_time();
|
||||
});
|
||||
|
||||
// 4. Return result.
|
||||
return result;
|
||||
}
|
||||
|
||||
// https://w3c.github.io/performance-timeline/#dom-performanceobserverentrylist-getentries
|
||||
WebIDL::ExceptionOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> PerformanceObserverEntryList::get_entries() const
|
||||
{
|
||||
// Returns a PerformanceEntryList object returned by filter buffer by name and type algorithm with this's entry list,
|
||||
// name and type set to null.
|
||||
return TRY_OR_THROW_OOM(vm(), filter_buffer_by_name_and_type(m_entry_list, /* name= */ Optional<String> {}, /* type= */ Optional<String> {}));
|
||||
}
|
||||
|
||||
// https://w3c.github.io/performance-timeline/#dom-performanceobserverentrylist-getentriesbytype
|
||||
WebIDL::ExceptionOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> PerformanceObserverEntryList::get_entries_by_type(String const& type) const
|
||||
{
|
||||
// Returns a PerformanceEntryList object returned by filter buffer by name and type algorithm with this's entry list,
|
||||
// name set to null, and type set to the method's input type parameter.
|
||||
return TRY_OR_THROW_OOM(vm(), filter_buffer_by_name_and_type(m_entry_list, /* name= */ Optional<String> {}, type));
|
||||
}
|
||||
|
||||
// https://w3c.github.io/performance-timeline/#dom-performanceobserverentrylist-getentriesbyname
|
||||
WebIDL::ExceptionOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> PerformanceObserverEntryList::get_entries_by_name(String const& name, Optional<String> type) const
|
||||
{
|
||||
// Returns a PerformanceEntryList object returned by filter buffer by name and type algorithm with this's entry list,
|
||||
// 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(), filter_buffer_by_name_and_type(m_entry_list, name, type));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <LibWeb/Bindings/PlatformObject.h>
|
||||
|
||||
namespace Web::PerformanceTimeline {
|
||||
|
||||
// https://w3c.github.io/performance-timeline/#performanceobserverentrylist-interface
|
||||
class PerformanceObserverEntryList final : public Bindings::PlatformObject {
|
||||
WEB_PLATFORM_OBJECT(PerformanceObserverEntryList, Bindings::PlatformObject);
|
||||
JS_DECLARE_ALLOCATOR(PerformanceObserverEntryList);
|
||||
|
||||
public:
|
||||
virtual ~PerformanceObserverEntryList() override;
|
||||
|
||||
WebIDL::ExceptionOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> get_entries() const;
|
||||
WebIDL::ExceptionOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> get_entries_by_type(String const& type) const;
|
||||
WebIDL::ExceptionOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> get_entries_by_name(String const& name, Optional<String> type) const;
|
||||
|
||||
private:
|
||||
PerformanceObserverEntryList(JS::Realm&, Vector<JS::NonnullGCPtr<PerformanceTimeline::PerformanceEntry>>&&);
|
||||
|
||||
virtual void initialize(JS::Realm&) override;
|
||||
virtual void visit_edges(Cell::Visitor&) override;
|
||||
|
||||
// https://w3c.github.io/performance-timeline/#dfn-entry-list
|
||||
// Returns a PerformanceEntryList object returned by filter buffer by name and type algorithm with this's entry list,
|
||||
// name and type set to null.
|
||||
Vector<JS::NonnullGCPtr<PerformanceTimeline::PerformanceEntry>> m_entry_list;
|
||||
};
|
||||
|
||||
ErrorOr<Vector<JS::Handle<PerformanceTimeline::PerformanceEntry>>> filter_buffer_by_name_and_type(Vector<JS::NonnullGCPtr<PerformanceTimeline::PerformanceEntry>> const& buffer, Optional<String> name, Optional<String> type);
|
||||
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
#import <HighResolutionTime/Performance.idl>
|
||||
|
||||
// https://w3c.github.io/performance-timeline/#performanceobserverentrylist-interface
|
||||
[Exposed=(Window,Worker)]
|
||||
interface PerformanceObserverEntryList {
|
||||
PerformanceEntryList getEntries();
|
||||
PerformanceEntryList getEntriesByType(DOMString type);
|
||||
PerformanceEntryList getEntriesByName(DOMString name, optional DOMString type);
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue