Truly independent web browser
LibWeb: Take AK::Duration in Platform::Timer
Shannon Booth05a441057dcc
diff --git a/Libraries/LibWeb/CSS/FontFace.cpp b/Libraries/LibWeb/CSS/FontFace.cppindex b7b41b3bf04..6baf4939aea 100644--- a/Libraries/LibWeb/CSS/FontFace.cpp+++ b/Libraries/LibWeb/CSS/FontFace.cpp@@ -755,11 +755,11 @@ void FontFaceState::update_font_display_period()if (!next_deadline.has_value() || m_font_display_time_for_testing.has_value())return;if (!m_font_download_timer) {- m_font_download_timer = Platform::Timer::create_single_shot(GC::Heap::the(), 0, GC::create_function(GC::Heap::the(), [this] {+ m_font_download_timer = Platform::Timer::create_single_shot(GC::Heap::the(), AK::Duration::from_milliseconds(0), GC::create_function(GC::Heap::the(), [this] {update_font_display_period();}));}- m_font_download_timer->start(static_cast<int>(*next_deadline - elapsed));+ m_font_download_timer->start(AK::Duration::from_milliseconds(static_cast<int>(*next_deadline - elapsed)));}i64 FontFaceState::font_download_elapsed_time() constdiff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cppindex def9e590531..62ce0708f5c 100644--- a/Libraries/LibWeb/DOM/Document.cpp+++ b/Libraries/LibWeb/DOM/Document.cpp@@ -6432,7 +6432,7 @@ struct DocumentLifecycleState : public GC::Cell {: remaining_children(remaining), document(document), finish_callback(finish_callback)- , timeout(Platform::Timer::create_single_shot(GC::Heap::the(), TIMEOUT_MS, GC::create_function(GC::Heap::the(), [this] {+ , timeout(Platform::Timer::create_single_shot(GC::Heap::the(), AK::Duration::from_milliseconds(TIMEOUT_MS), GC::create_function(GC::Heap::the(), [this] {if (remaining_children > 0)dbgln("FIXME: Document unload/destruction timed out with {} remaining children", remaining_children);})))diff --git a/Libraries/LibWeb/Gamepad/GamepadHapticActuator.cpp b/Libraries/LibWeb/Gamepad/GamepadHapticActuator.cppindex 839b4326076..e529acedbfc 100644--- a/Libraries/LibWeb/Gamepad/GamepadHapticActuator.cpp+++ b/Libraries/LibWeb/Gamepad/GamepadHapticActuator.cpp@@ -339,7 +339,7 @@ void GamepadHapticActuator::issue_haptic_effect(GamepadHapticEffectType type, Ga// for more precise playback timing when params.startDelay is not 0.0. The user agent MAY modify the effect to// increase compatibility. For example, an effect intended for a rumble motor may be transformed into a// waveform-based effect for a device that supports waveform haptics but lacks rumble motors.- m_playing_effect_timer = Platform::Timer::create_single_shot(heap, static_cast<int>(params.start_delay), GC::create_function(heap, [this, type, params, on_complete, &heap] {+ m_playing_effect_timer = Platform::Timer::create_single_shot(heap, AK::Duration::from_milliseconds(static_cast<int>(params.start_delay)), GC::create_function(heap, [this, type, params, on_complete, &heap] {auto& page = m_window->page();auto first_magnitude = type == GamepadHapticEffectType::DualRumble ? params.strong_magnitude : params.left_trigger;@@ -350,7 +350,7 @@ void GamepadHapticActuator::issue_haptic_effect(GamepadHapticEffectType type, Gastatic_cast<u16>(second_magnitude * NumericLimits<u16>::max()),static_cast<u32>(params.duration)));- m_playing_effect_timer = Platform::Timer::create_single_shot(heap, params.duration, GC::create_function(heap, [this, type, on_complete] {+ m_playing_effect_timer = Platform::Timer::create_single_shot(heap, AK::Duration::from_milliseconds(params.duration), GC::create_function(heap, [this, type, on_complete] {// Explicitly stop the rumble before completing, ensuring the stop signal is sent synchronously.auto& page = m_window->page();diff --git a/Libraries/LibWeb/Geolocation/Geolocation.cpp b/Libraries/LibWeb/Geolocation/Geolocation.cppindex 451e8db0a53..b6e5d19b0f7 100644--- a/Libraries/LibWeb/Geolocation/Geolocation.cpp+++ b/Libraries/LibWeb/Geolocation/Geolocation.cpp@@ -385,7 +385,7 @@ void Geolocation::acquire_position_from_page(GC::Ref<WebIDL::CallbackType> succeauto request_state = make_ref_counted<GeolocationRequestState>();auto weak_document = GC::Weak<DOM::Document> { document };auto weak_page = GC::Weak<Page> { page };- auto timeout_timer = Platform::Timer::create_single_shot(heap(), min(options.timeout, NumericLimits<int>::max()), {});+ auto timeout_timer = Platform::Timer::create_single_shot(heap(), AK::Duration::from_milliseconds(min(options.timeout, NumericLimits<int>::max())), {});m_timeout_timers.append(timeout_timer);auto stop_timeout_timer = [this, timeout_timer, request_state] {@@ -522,7 +522,7 @@ void Geolocation::run_in_parallel_when_document_is_visible(DOM::Document& docume// Run the callback as soon as the document becomes visible. If we time out, do not run the callback at all.auto document_observer = DOM::DocumentObserver::create(document);- auto timeout_timer = Platform::Timer::create_single_shot(GC::Heap::the(), VISIBILITY_STATE_TIMEOUT_MS, {});+ auto timeout_timer = Platform::Timer::create_single_shot(GC::Heap::the(), AK::Duration::from_milliseconds(VISIBILITY_STATE_TIMEOUT_MS), {});m_timeout_timers.append(timeout_timer);auto clear_observer_and_timer = [this, document_observer, timeout_timer] {document_observer->set_document_visibility_state_observer({});diff --git a/Libraries/LibWeb/HTML/AnimatedBitmapDecodedImageData.cpp b/Libraries/LibWeb/HTML/AnimatedBitmapDecodedImageData.cppindex 330cba5580e..559d682a153 100644--- a/Libraries/LibWeb/HTML/AnimatedBitmapDecodedImageData.cpp+++ b/Libraries/LibWeb/HTML/AnimatedBitmapDecodedImageData.cpp@@ -155,7 +155,7 @@ void AnimatedBitmapDecodedImageData::start_animation()// of which should guarantee that we are at the beginning of the animation.VERIFY(m_current_frame_index == 0 && m_loops_completed == 0);- m_animation_timer->start(frame_duration(0));+ m_animation_timer->start(AK::Duration::from_milliseconds(frame_duration(0)));}void AnimatedBitmapDecodedImageData::stop_animation()@@ -169,8 +169,8 @@ void AnimatedBitmapDecodedImageData::advance_animation()maybe_request_more_frames(m_current_frame_index);auto current_frame_duration = frame_duration(m_current_frame_index);- if (current_frame_duration != m_animation_timer->interval())- m_animation_timer->restart(current_frame_duration);+ if (AK::Duration::from_milliseconds(current_frame_duration) != m_animation_timer->interval())+ m_animation_timer->restart(AK::Duration::from_milliseconds(current_frame_duration));if (m_current_frame_index == m_frame_count - 1) {++m_loops_completed;diff --git a/Libraries/LibWeb/HTML/CheckUnloadingCanceledState.cpp b/Libraries/LibWeb/HTML/CheckUnloadingCanceledState.cppindex 2c300707c34..bc42e41b75a 100644--- a/Libraries/LibWeb/HTML/CheckUnloadingCanceledState.cpp+++ b/Libraries/LibWeb/HTML/CheckUnloadingCanceledState.cpp@@ -30,7 +30,7 @@ CheckUnloadingCanceledState::CheckUnloadingCanceledState(, m_traversable(traversable), m_user_involvement(user_involvement), m_callback(callback)- , m_timeout(Platform::Timer::create_single_shot(heap(), TIMEOUT_MS, GC::create_function(heap(), [this] {+ , m_timeout(Platform::Timer::create_single_shot(heap(), AK::Duration::from_milliseconds(TIMEOUT_MS), GC::create_function(heap(), [this] {if (!m_completed) {if (has_pending_before_unload_dialog()) {m_timeout->restart();diff --git a/Libraries/LibWeb/HTML/DedicatedWorkerGlobalScope.cpp b/Libraries/LibWeb/HTML/DedicatedWorkerGlobalScope.cppindex 2e7d6db7de7..376f2be2bca 100644--- a/Libraries/LibWeb/HTML/DedicatedWorkerGlobalScope.cpp+++ b/Libraries/LibWeb/HTML/DedicatedWorkerGlobalScope.cpp@@ -125,14 +125,14 @@ bool DedicatedWorkerGlobalScope::has_animation_frame_callbacks() constvoid DedicatedWorkerGlobalScope::schedule_rendering_update(){if (!m_rendering_update_timer) {- m_rendering_update_timer = Platform::Timer::create_single_shot(heap(), 0, GC::create_function(heap(), [this] {+ m_rendering_update_timer = Platform::Timer::create_single_shot(heap(), AK::Duration::from_milliseconds(0), GC::create_function(heap(), [this] {run_rendering_update();}));}if (m_rendering_update_timer->is_active())return;auto interval_ms = static_cast<int>(AK::ceil(1000.0 / max(1.0, page()->client().maximum_frames_per_second())));- m_rendering_update_timer->start(interval_ms);+ m_rendering_update_timer->start(AK::Duration::from_milliseconds(interval_ms));}// https://html.spec.whatwg.org/multipage/webappapis.html#event-loop-processing-modeldiff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cppindex b15f9f38877..b6a18acda4f 100644--- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp+++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp@@ -93,7 +93,7 @@ void EventLoop::visit_edges(Visitor& visitor)void EventLoop::schedule(){if (!m_system_event_loop_timer) {- m_system_event_loop_timer = Platform::Timer::create_single_shot(GC::Heap::the(), 0, GC::create_function(GC::Heap::the(), [this] {+ m_system_event_loop_timer = Platform::Timer::create_single_shot(GC::Heap::the(), AK::Duration::from_milliseconds(0), GC::create_function(GC::Heap::the(), [this] {process();}));}@@ -269,7 +269,7 @@ void EventLoop::process()// during an idle period belong to the next one; immediately starting it lets self-scheduling// callbacks spin continuously even when they have no work to do.if (!m_idle_period_timer) {- m_idle_period_timer = Platform::Timer::create_single_shot(GC::Heap::the(), 50, GC::create_function(GC::Heap::the(), [this] {+ m_idle_period_timer = Platform::Timer::create_single_shot(GC::Heap::the(), AK::Duration::from_milliseconds(50), GC::create_function(GC::Heap::the(), [this] {schedule();}));}diff --git a/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp b/Libraries/LibWeb/HTML/Parser/HTMLParser.cppindex fad04744f10..5f18dfb9486 100644--- a/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp+++ b/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp@@ -710,7 +710,7 @@ HTMLParserEndState::HTMLParserEndState(GC::Ref<DOM::Document> document, GC::Ptr<: m_document(document), m_parser(parser), m_parser_generation(parser_generation)- , m_timeout(Platform::Timer::create_single_shot(GC::Heap::the(), THE_END_TIMEOUT_MS, GC::create_function(GC::Heap::the(), [this] {+ , m_timeout(Platform::Timer::create_single_shot(GC::Heap::the(), AK::Duration::from_milliseconds(THE_END_TIMEOUT_MS), GC::create_function(GC::Heap::the(), [this] {if (m_phase != Phase::Completed && m_phase != Phase::Cancelled)dbgln("HTMLParserEndState: timed out in phase {}", to_underlying(m_phase));})))diff --git a/Libraries/LibWeb/Platform/Timer.cpp b/Libraries/LibWeb/Platform/Timer.cppindex 9a204960e8f..44905fc3923 100644--- a/Libraries/LibWeb/Platform/Timer.cpp+++ b/Libraries/LibWeb/Platform/Timer.cpp@@ -44,20 +44,20 @@ GC::Ref<Timer> Timer::create(GC::Heap& heap)return heap.allocate<Timer>();}-GC::Ref<Timer> Timer::create_repeating(GC::Heap& heap, int interval_ms, GC::Ptr<GC::Function<void()>> timeout_handler)+GC::Ref<Timer> Timer::create_repeating(GC::Heap& heap, AK::Duration interval, GC::Ptr<GC::Function<void()>> timeout_handler){auto timer = heap.allocate<Timer>();timer->set_single_shot(false);- timer->set_interval(interval_ms);+ timer->set_interval(interval);timer->on_timeout = move(timeout_handler);return timer;}-GC::Ref<Timer> Timer::create_single_shot(GC::Heap& heap, int interval_ms, GC::Ptr<GC::Function<void()>> timeout_handler)+GC::Ref<Timer> Timer::create_single_shot(GC::Heap& heap, AK::Duration interval, GC::Ptr<GC::Function<void()>> timeout_handler){auto timer = heap.allocate<Timer>();timer->set_single_shot(true);- timer->set_interval(interval_ms);+ timer->set_interval(interval);timer->on_timeout = move(timeout_handler);return timer;}@@ -68,9 +68,9 @@ void Timer::start()m_activity_root.take(*this);}-void Timer::start(int interval_ms)+void Timer::start(AK::Duration interval){- m_timer->start(AK::Duration::from_milliseconds(interval_ms));+ m_timer->start(interval);m_activity_root.take(*this);}@@ -80,9 +80,9 @@ void Timer::restart()m_activity_root.take(*this);}-void Timer::restart(int interval_ms)+void Timer::restart(AK::Duration interval){- m_timer->restart(AK::Duration::from_milliseconds(interval_ms));+ m_timer->restart(interval);m_activity_root.take(*this);}@@ -106,14 +106,14 @@ bool Timer::is_active() constreturn m_timer->is_active();}-int Timer::interval() const+AK::Duration Timer::interval() const{- return m_timer->interval().to_milliseconds();+ return m_timer->interval();}-void Timer::set_interval(int interval_ms)+void Timer::set_interval(AK::Duration interval){- m_timer->set_interval(AK::Duration::from_milliseconds(interval_ms));+ m_timer->set_interval(interval);}bool Timer::is_single_shot() constdiff --git a/Libraries/LibWeb/Platform/Timer.h b/Libraries/LibWeb/Platform/Timer.hindex aa524ffb06d..4e6d23bca4a 100644--- a/Libraries/LibWeb/Platform/Timer.h+++ b/Libraries/LibWeb/Platform/Timer.h@@ -7,6 +7,7 @@#pragma once#include <AK/NonnullRefPtr.h>+#include <AK/Time.h>#include <LibCore/Forward.h>#include <LibGC/ActivityRoot.h>#include <LibGC/CellAllocator.h>@@ -21,23 +22,23 @@ class WEB_API Timer : public JS::Cell {public:static GC::Ref<Timer> create(GC::Heap&);- static GC::Ref<Timer> create_repeating(GC::Heap&, int interval_ms, GC::Ptr<GC::Function<void()>> timeout_handler);- static GC::Ref<Timer> create_single_shot(GC::Heap&, int interval_ms, GC::Ptr<GC::Function<void()>> timeout_handler);+ static GC::Ref<Timer> create_repeating(GC::Heap&, AK::Duration interval, GC::Ptr<GC::Function<void()>> timeout_handler);+ static GC::Ref<Timer> create_single_shot(GC::Heap&, AK::Duration interval, GC::Ptr<GC::Function<void()>> timeout_handler);virtual ~Timer();virtual void finalize() override;void start();- void start(int interval_ms);+ void start(AK::Duration interval);void restart();- void restart(int interval_ms);+ void restart(AK::Duration interval);void stop();void set_active(bool);bool is_active() const;- int interval() const;- void set_interval(int interval_ms);+ AK::Duration interval() const;+ void set_interval(AK::Duration interval);bool is_single_shot() const;void set_single_shot(bool);diff --git a/Libraries/LibWeb/XHR/XMLHttpRequest.cpp b/Libraries/LibWeb/XHR/XMLHttpRequest.cppindex 4cf40732059..7f22eb0fe08 100644--- a/Libraries/LibWeb/XHR/XMLHttpRequest.cpp+++ b/Libraries/LibWeb/XHR/XMLHttpRequest.cpp@@ -1031,7 +1031,7 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::send(NullableDocumentOrXMLHttpRequestB// 2. If req’s done flag is unset, then set this’s timed out flag and terminate this’s fetch controller.if (m_timeout != 0) {stop_timeout_timer();- m_timeout_timer = Platform::Timer::create_single_shot(heap(), m_timeout, nullptr);+ m_timeout_timer = Platform::Timer::create_single_shot(heap(), AK::Duration::from_milliseconds(m_timeout), nullptr);m_timeout_timer->on_timeout = GC::create_function(GC::Heap::the(), [this, request]() {if (!request->done()) {m_timed_out = true;@@ -1090,7 +1090,7 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::send(NullableDocumentOrXMLHttpRequestB// 5. Pause until either processedResponse is true or this’s timeout is not 0 and this’s timeout milliseconds have passed since now.if (m_timeout != 0) {stop_timeout_timer();- m_timeout_timer = Platform::Timer::create_single_shot(heap(), m_timeout, nullptr);+ m_timeout_timer = Platform::Timer::create_single_shot(heap(), AK::Duration::from_milliseconds(m_timeout), nullptr);m_timeout_timer->on_timeout = GC::create_function(GC::Heap::the(), [this, did_time_out]() {did_time_out->set_value(true);stop_timeout_timer();diff --git a/Tests/LibWeb/TestWrapperWorld.cpp b/Tests/LibWeb/TestWrapperWorld.cppindex c2c1400ed6a..dceac1e8d51 100644--- a/Tests/LibWeb/TestWrapperWorld.cpp+++ b/Tests/LibWeb/TestWrapperWorld.cpp@@ -878,9 +878,9 @@ TEST_CASE(single_shot_timer_restarted_from_callback_keeps_activity_root)IGNORE_USE_IN_ESCAPING_LAMBDA Core::EventLoop event_loop;auto vm = JS::VM::create();IGNORE_USE_IN_ESCAPING_LAMBDA GC::Root<Web::Platform::Timer> timer;- timer = Web::Platform::Timer::create_single_shot(GC::Heap::the(), 0, nullptr);+ timer = Web::Platform::Timer::create_single_shot(GC::Heap::the(), AK::Duration::from_milliseconds(0), nullptr);timer->on_timeout = GC::create_function(GC::Heap::the(), [&] {- timer->restart(1000);+ timer->restart(AK::Duration::from_milliseconds(1000));event_loop.quit(0);});