LibWeb/HTML: Port Window.performance to IDL

This commit is contained in:
Linus Groh 2023-03-06 19:50:46 +00:00
parent 95ce5735ce
commit 7de9179a6d
Notes: sideshowbarker 2024-07-16 23:14:32 +09:00
5 changed files with 25 additions and 39 deletions

View file

@ -2710,6 +2710,7 @@ using namespace Web::DOMParsing;
using namespace Web::Fetch;
using namespace Web::FileAPI;
using namespace Web::Geometry;
using namespace Web::HighResolutionTime;
using namespace Web::HTML;
using namespace Web::IntersectionObserver;
using namespace Web::RequestIdleCallback;
@ -2929,6 +2930,7 @@ using namespace Web::DOMParsing;
using namespace Web::Fetch;
using namespace Web::FileAPI;
using namespace Web::Geometry;
using namespace Web::HighResolutionTime;
using namespace Web::HTML;
using namespace Web::IntersectionObserver;
using namespace Web::NavigationTiming;
@ -3056,6 +3058,7 @@ using namespace Web::DOMParsing;
using namespace Web::Fetch;
using namespace Web::FileAPI;
using namespace Web::Geometry;
using namespace Web::HighResolutionTime;
using namespace Web::HTML;
using namespace Web::IntersectionObserver;
using namespace Web::NavigationTiming;
@ -3185,6 +3188,7 @@ using namespace Web::DOMParsing;
using namespace Web::Fetch;
using namespace Web::FileAPI;
using namespace Web::Geometry;
using namespace Web::HighResolutionTime;
using namespace Web::HTML;
using namespace Web::IntersectionObserver;
using namespace Web::NavigationTiming;

View file

@ -187,8 +187,9 @@ void EventLoop::process()
// FIXME: 12. For each fully active Document in docs, if the user agent detects that the backing storage associated with a CanvasRenderingContext2D or an OffscreenCanvasRenderingContext2D, context, has been lost, then it must run the context lost steps for each such context:
// FIXME: 13. For each fully active Document in docs, run the animation frame callbacks for that Document, passing in now as the timestamp.
auto now = HighResolutionTime::unsafe_shared_current_time();
for_each_fully_active_document_in_docs([&](DOM::Document& document) {
run_animation_frame_callbacks(document, document.window().performance().now());
run_animation_frame_callbacks(document, now);
});
// FIXME: 14. For each fully active Document in docs, run the update intersection observations steps for that Document, passing in now as the timestamp. [INTERSECTIONOBSERVER]

View file

@ -118,13 +118,6 @@ void Window::visit_edges(JS::Cell::Visitor& visitor)
Window::~Window() = default;
HighResolutionTime::Performance& Window::performance()
{
if (!m_performance)
m_performance = heap().allocate<HighResolutionTime::Performance>(realm(), *this).release_allocated_value_but_fixme_should_propagate_errors();
return *m_performance;
}
CSS::Screen& Window::screen()
{
if (!m_screen)
@ -553,9 +546,11 @@ i32 Window::run_timer_initialization_steps(TimerHandler handler, i32 timeout, JS
// https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#run-the-animation-frame-callbacks
i32 Window::request_animation_frame_impl(WebIDL::CallbackType& js_callback)
{
return m_animation_frame_callback_driver.add([this, js_callback = JS::make_handle(js_callback)](auto) {
// FIXME: `now` is supposed to be passed in
auto now = HighResolutionTime::unsafe_shared_current_time();
return m_animation_frame_callback_driver.add([this, now, js_callback = JS::make_handle(js_callback)](auto) {
// 3. Invoke callback, passing now as the only argument,
auto result = WebIDL::invoke_callback(*js_callback, {}, JS::Value(performance().now()));
auto result = WebIDL::invoke_callback(*js_callback, {}, JS::Value(now));
// and if an exception is thrown, report the exception.
if (result.is_error())
@ -1050,7 +1045,6 @@ WebIDL::ExceptionOr<void> Window::initialize_web_interfaces(Badge<WindowEnvironm
MUST_OR_THROW_OOM(Bindings::WindowGlobalMixin::initialize(realm, *this));
// FIXME: These should be native accessors, not properties
define_native_accessor(realm, "performance", performance_getter, performance_setter, JS::Attribute::Enumerable | JS::Attribute::Configurable);
define_native_accessor(realm, "crypto", crypto_getter, {}, JS::Attribute::Enumerable);
define_native_accessor(realm, "screen", screen_getter, screen_setter, JS::Attribute::Enumerable | JS::Attribute::Configurable);
define_native_accessor(realm, "innerWidth", inner_width_getter, {}, JS::Attribute::Enumerable);
@ -1338,6 +1332,14 @@ Variant<JS::Handle<DOM::Event>, JS::Value> Window::event() const
return JS::js_undefined();
}
// https://w3c.github.io/hr-time/#dom-windoworworkerglobalscope-performance
WebIDL::ExceptionOr<JS::NonnullGCPtr<HighResolutionTime::Performance>> Window::performance()
{
if (!m_performance)
m_performance = MUST_OR_THROW_OOM(heap().allocate<HighResolutionTime::Performance>(realm(), *this));
return JS::NonnullGCPtr { *m_performance };
}
static JS::ThrowCompletionOr<TimerHandler> make_timer_handler(JS::VM& vm, JS::Value handler)
{
if (handler.is_function())
@ -1511,29 +1513,6 @@ size_t Window::document_tree_child_browsing_context_count() const
return this_browsing_context->document_tree_child_browsing_context_count();
}
JS_DEFINE_NATIVE_FUNCTION(Window::performance_getter)
{
auto* impl = TRY(impl_from(vm));
return &impl->performance();
}
JS_DEFINE_NATIVE_FUNCTION(Window::performance_setter)
{
// https://webidl.spec.whatwg.org/#dfn-attribute-setter
// 4.1. If no arguments were passed, then throw a TypeError.
if (vm.argument_count() == 0)
return vm.throw_completion<JS::TypeError>(JS::ErrorType::BadArgCountOne, "set performance");
auto* impl = TRY(impl_from(vm));
// 5. If attribute is declared with the [Replaceable] extended attribute, then:
// 1. Perform ? CreateDataProperty(esValue, id, V).
TRY(impl->create_data_property("performance", vm.argument(0)));
// 2. Return undefined.
return JS::js_undefined();
}
JS_DEFINE_NATIVE_FUNCTION(Window::screen_getter)
{
auto* impl = TRY(impl_from(vm));

View file

@ -96,8 +96,6 @@ public:
void deallocate_timer_id(Badge<Timer>, i32);
HighResolutionTime::Performance& performance();
Crypto::Crypto& crypto() { return *m_crypto; }
CSS::Screen& screen();
@ -169,6 +167,8 @@ public:
Variant<JS::Handle<DOM::Event>, JS::Value> event() const;
WebIDL::ExceptionOr<JS::NonnullGCPtr<HighResolutionTime::Performance>> performance();
private:
explicit Window(JS::Realm&);
@ -231,9 +231,6 @@ public:
private:
JS_DECLARE_NATIVE_FUNCTION(location_setter);
JS_DECLARE_NATIVE_FUNCTION(performance_getter);
JS_DECLARE_NATIVE_FUNCTION(performance_setter);
JS_DECLARE_NATIVE_FUNCTION(screen_getter);
JS_DECLARE_NATIVE_FUNCTION(screen_setter);

View file

@ -1,6 +1,7 @@
#import <DOM/Document.idl>
#import <DOM/EventHandler.idl>
#import <DOM/EventTarget.idl>
#import <HighResolutionTime/Performance.idl>
#import <HTML/Navigator.idl>
#import <HTML/WindowOrWorkerGlobalScope.idl>
@ -39,6 +40,10 @@ interface Window : EventTarget {
// https://dom.spec.whatwg.org/#interface-window-extensions
[Replaceable] readonly attribute (Event or undefined) event; // legacy
// FIXME: Everything from here on should be shared through WindowOrWorkerGlobalScope
// https://w3c.github.io/hr-time/#the-performance-attribute
[Replaceable] readonly attribute Performance performance;
};
Window includes GlobalEventHandlers;
Window includes WindowEventHandlers;