Skip to content

Truly independent web browser

LibWeb: Wait the full timeout of long internal timers

Shannon Booth9224627d5bd2
diff --git a/Libraries/LibWeb/HTML/Window.cpp b/Libraries/LibWeb/HTML/Window.cpp
index 7d8b7b4acf2..696e44c430b 100644
--- a/Libraries/LibWeb/HTML/Window.cpp
+++ b/Libraries/LibWeb/HTML/Window.cpp
@@ -2213,8 +2213,7 @@ u32 Window::request_idle_callback(IdleCallbackHandler callback, IdleRequestOptio
// 6. If the timeout property is present in options and has a positive value:
if (options.timeout.has_value() && *options.timeout > 0) {
// 1. Wait for timeout milliseconds.
- auto timeout = min(*options.timeout, static_cast<u32>(NumericLimits<i32>::max()));
- auto timeout_timer_id = run_steps_after_a_timeout(static_cast<i32>(timeout), GC::create_function(GC::Heap::the(), GC::captures(*this, handle), [](Window& self, u32 handle) {
+ auto timeout_timer_id = run_steps_after_a_timeout(*options.timeout, GC::create_function(GC::Heap::the(), GC::captures(*this, handle), [](Window& self, u32 handle) {
// FIXME: 2. Wait until all invocations of this algorithm, whose timeout added to their posted time occurred before this one's, have completed.
// FIXME: 3. Optionally, wait a further user-agent defined length of time.
diff --git a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp
index c151167601d..9cb33c9eb15 100644
--- a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp
+++ b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp
@@ -1142,20 +1142,33 @@ WindowOrWorkerGlobalScopeMixin::AffectedAnyWebSockets WindowOrWorkerGlobalScopeM
}
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#run-steps-after-a-timeout
-i32 WindowOrWorkerGlobalScopeMixin::run_steps_after_a_timeout(i32 timeout, GC::Ref<GC::Function<void()>> completion_step)
+i32 WindowOrWorkerGlobalScopeMixin::run_steps_after_a_timeout(u64 timeout, GC::Ref<GC::Function<void()>> completion_step)
{
auto timer_key = m_timer_id_allocator.allocate();
m_internal_timer_ids.set(timer_key);
- auto remove_timer_and_complete = GC::create_function(GC::Heap::the(), GC::captures(this_impl(), timer_key, completion_step), [](auto& this_object, auto& timer_key, auto& completion_step) {
+ run_internal_timer(timer_key, timeout, completion_step);
+ return timer_key;
+}
+
+void WindowOrWorkerGlobalScopeMixin::run_internal_timer(i32 timer_key, u64 timeout, GC::Ref<GC::Function<void()>> completion_step)
+{
+ // AD-HOC: A timer waits at most NumericLimits<i32>::max() milliseconds, so a longer timeout waits in parts under
+ // the same timer key.
+ auto part = min(timeout, static_cast<u64>(NumericLimits<i32>::max()));
+ auto remaining = timeout - part;
+ auto complete_part = GC::create_function(GC::Heap::the(), GC::captures(this_impl(), timer_key, remaining, completion_step), [](auto& this_object, auto& timer_key, auto& remaining, auto& completion_step) {
auto& global_scope = as<WindowOrWorkerGlobalScopeMixin>(this_object);
global_scope.m_timers.remove(timer_key);
+ if (remaining > 0) {
+ global_scope.run_internal_timer(timer_key, remaining, completion_step);
+ return;
+ }
global_scope.m_internal_timer_ids.remove(timer_key);
completion_step->function()();
});
// NB: A timeout another specification waits on — an idle callback's, an AbortSignal's — isn't one of the page's
// own yield points, so a hidden document holds it back like a chained timer, however short it is.
- run_steps_after_a_timeout_impl(timeout, TimerThrottlingClass::Chained, remove_timer_and_complete, timer_key);
- return timer_key;
+ run_steps_after_a_timeout_impl(static_cast<i32>(part), TimerThrottlingClass::Chained, complete_part, timer_key);
}
void WindowOrWorkerGlobalScopeMixin::run_steps_after_a_timeout_impl(i32 timeout, TimerThrottlingClass throttling_class, GC::Ref<GC::Function<void()>> completion_step, Optional<i32> timer_key, Repeat repeat)
diff --git a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.h b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.h
index 30a0944e264..a65bdbbdac5 100644
--- a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.h
+++ b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.h
@@ -92,7 +92,7 @@ public:
bool has_pending_activity(DOM::EventTarget const&) const;
void clear_objects_with_pending_activity() { m_objects_with_pending_activity.clear(); }
- i32 run_steps_after_a_timeout(i32 timeout, GC::Ref<GC::Function<void()>> completion_step);
+ i32 run_steps_after_a_timeout(u64 timeout, GC::Ref<GC::Function<void()>> completion_step);
void cancel_timeout_steps(i32 timer_key);
void document_visibility_state_changed(Badge<DOM::Document>);
@@ -151,6 +151,7 @@ private:
WebIDL::ExceptionOr<i32> run_timer_initialization_steps(TimerHandler handler, i32 timeout, GC::RootVector<JS::Value> arguments, Repeat repeat, Optional<i32> previous_id = {});
bool has_expired_timer_yet_to_fire(double now);
void run_steps_after_a_timeout_impl(i32 timeout, TimerThrottlingClass, GC::Ref<GC::Function<void()>> completion_step, Optional<i32> timer_key, Repeat repeat = Repeat::No);
+ void run_internal_timer(i32 timer_key, u64 timeout, GC::Ref<GC::Function<void()>> completion_step);
bool document_is_hidden() const;
bool timers_are_throttled() const;
Optional<i32> throttled_timer_delay(TimerThrottlingClass, double deadline) const;
diff --git a/Tests/LibWeb/Text/expected/abortsignal-timeout-long.txt b/Tests/LibWeb/Text/expected/abortsignal-timeout-long.txt
new file mode 100644
index 00000000000..7c365d87aa4
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/abortsignal-timeout-long.txt
@@ -0,0 +1,4 @@
+2147483647: aborted false
+2147483648: aborted false
+4294967296: aborted false
+9007199254740991: aborted false
diff --git a/Tests/LibWeb/Text/input/abortsignal-timeout-long.html b/Tests/LibWeb/Text/input/abortsignal-timeout-long.html
new file mode 100644
index 00000000000..e98c2bd3aa0
--- /dev/null
+++ b/Tests/LibWeb/Text/input/abortsignal-timeout-long.html
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<script>
+ asyncTest(done => {
+ const signals = [2 ** 31 - 1, 2 ** 31, 2 ** 32, Number.MAX_SAFE_INTEGER].map(milliseconds => [milliseconds, AbortSignal.timeout(milliseconds)]);
+ AbortSignal.timeout(0).onabort = () => {
+ for (const [milliseconds, signal] of signals)
+ println(`${milliseconds}: aborted ${signal.aborted}`);
+ done();
+ };
+ });
+</script>