From e91f4dcd795a1dc2abed083bb816154d4b1366cb Mon Sep 17 00:00:00 2001 From: Matthew Olsson Date: Sat, 9 Mar 2024 14:58:10 -0700 Subject: [PATCH] LibWeb: Use Performance for animation time instead of MonotonicTime Performance handles the document origin time correctly, and prevents these times from being unusually large. Also initialize the DocumentTimeline time in the constructor, since these can be created from JS. --- Userland/Libraries/LibWeb/Animations/Animation.cpp | 5 +++-- Userland/Libraries/LibWeb/Animations/DocumentTimeline.cpp | 5 ++++- Userland/Libraries/LibWeb/DOM/Document.cpp | 7 +++---- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Userland/Libraries/LibWeb/Animations/Animation.cpp b/Userland/Libraries/LibWeb/Animations/Animation.cpp index b62e47a1d0..cb6a503fcc 100644 --- a/Userland/Libraries/LibWeb/Animations/Animation.cpp +++ b/Userland/Libraries/LibWeb/Animations/Animation.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -661,7 +662,7 @@ WebIDL::ExceptionOr Animation::play_an_animation(AutoRewind auto_rewind) // If a user agent determines that animation is immediately ready, it may schedule the above task as a microtask // such that it runs at the next microtask checkpoint, but it must not perform the task synchronously. m_pending_play_task = TaskState::Scheduled; - m_saved_play_time = MonotonicTime::now().milliseconds(); + m_saved_play_time = global_object().performance()->now(); // 13. Run the procedure to update an animation’s finished state for animation with the did seek flag set to false, // and the synchronously notify flag set to false. @@ -745,7 +746,7 @@ WebIDL::ExceptionOr Animation::pause() // // Note: This is run_pending_pause_task() m_pending_pause_task = TaskState::Scheduled; - m_saved_pause_time = MonotonicTime::now().milliseconds(); + m_saved_pause_time = global_object().performance()->now(); // 11. Run the procedure to update an animation’s finished state for animation with the did seek flag set to false, // and the synchronously notify flag set to false. diff --git a/Userland/Libraries/LibWeb/Animations/DocumentTimeline.cpp b/Userland/Libraries/LibWeb/Animations/DocumentTimeline.cpp index a9f43730bf..6982cc3512 100644 --- a/Userland/Libraries/LibWeb/Animations/DocumentTimeline.cpp +++ b/Userland/Libraries/LibWeb/Animations/DocumentTimeline.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include namespace Web::Animations { @@ -17,7 +18,9 @@ JS_DEFINE_ALLOCATOR(DocumentTimeline); JS::NonnullGCPtr DocumentTimeline::create(JS::Realm& realm, DOM::Document& document, HighResolutionTime::DOMHighResTimeStamp origin_time) { - return realm.heap().allocate(realm, realm, document, origin_time); + auto timeline = realm.heap().allocate(realm, realm, document, origin_time); + timeline->set_current_time(document.window().performance()->now()); + return timeline; } // https://www.w3.org/TR/web-animations-1/#dom-documenttimeline-documenttimeline diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index 34ff642720..2aad5211d6 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -92,6 +92,7 @@ #include #include #include +#include #include #include #include @@ -3847,10 +3848,8 @@ JS::NonnullGCPtr Document::timeline() { // The DocumentTimeline object representing the default document timeline. The default document timeline has an // origin time of zero. - if (!m_default_timeline) { + if (!m_default_timeline) m_default_timeline = Animations::DocumentTimeline::create(realm(), *this, 0.0); - m_default_timeline->set_current_time(MonotonicTime::now().milliseconds()); - } return *m_default_timeline; } @@ -4037,7 +4036,7 @@ void Document::ensure_animation_timer() return; } - update_animations_and_send_events(MonotonicTime::now().milliseconds()); + update_animations_and_send_events(window().performance()->now()); for (auto& timeline : m_associated_animation_timelines) { for (auto& animation : timeline->associated_animations())