From 7de9179a6d21757ffa6ee392c40d268b735b29d9 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Mon, 6 Mar 2023 19:50:46 +0000 Subject: [PATCH] LibWeb/HTML: Port Window.performance to IDL --- .../BindingsGenerator/IDLGenerators.cpp | 4 ++ .../LibWeb/HTML/EventLoop/EventLoop.cpp | 3 +- Userland/Libraries/LibWeb/HTML/Window.cpp | 45 +++++-------------- Userland/Libraries/LibWeb/HTML/Window.h | 7 +-- Userland/Libraries/LibWeb/HTML/Window.idl | 5 +++ 5 files changed, 25 insertions(+), 39 deletions(-) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp index 29fa652ad6..9045156fe6 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp @@ -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; diff --git a/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp index cef6687c0f..2df1063607 100644 --- a/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp +++ b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp @@ -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] diff --git a/Userland/Libraries/LibWeb/HTML/Window.cpp b/Userland/Libraries/LibWeb/HTML/Window.cpp index 5db7bcd0ac..33897561d7 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.cpp +++ b/Userland/Libraries/LibWeb/HTML/Window.cpp @@ -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(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 Window::initialize_web_interfaces(Badge, JS::Value> Window::event() const return JS::js_undefined(); } +// https://w3c.github.io/hr-time/#dom-windoworworkerglobalscope-performance +WebIDL::ExceptionOr> Window::performance() +{ + if (!m_performance) + m_performance = MUST_OR_THROW_OOM(heap().allocate(realm(), *this)); + return JS::NonnullGCPtr { *m_performance }; +} + static JS::ThrowCompletionOr 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::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)); diff --git a/Userland/Libraries/LibWeb/HTML/Window.h b/Userland/Libraries/LibWeb/HTML/Window.h index e1bcbba0ce..5c70582186 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.h +++ b/Userland/Libraries/LibWeb/HTML/Window.h @@ -96,8 +96,6 @@ public: void deallocate_timer_id(Badge, i32); - HighResolutionTime::Performance& performance(); - Crypto::Crypto& crypto() { return *m_crypto; } CSS::Screen& screen(); @@ -169,6 +167,8 @@ public: Variant, JS::Value> event() const; + WebIDL::ExceptionOr> 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); diff --git a/Userland/Libraries/LibWeb/HTML/Window.idl b/Userland/Libraries/LibWeb/HTML/Window.idl index 6f210a67d4..afa1099125 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.idl +++ b/Userland/Libraries/LibWeb/HTML/Window.idl @@ -1,6 +1,7 @@ #import #import #import +#import #import #import @@ -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;