Skip to content

Truly independent web browser

LibWeb: Use the same time origin for idle callback deadlines

Andreas Kling36566b962e7a
diff --git a/Libraries/LibWeb/RequestIdleCallback/IdleDeadline.cpp b/Libraries/LibWeb/RequestIdleCallback/IdleDeadline.cpp
index de4614a29fe..65a732a8cb7 100644
--- a/Libraries/LibWeb/RequestIdleCallback/IdleDeadline.cpp
+++ b/Libraries/LibWeb/RequestIdleCallback/IdleDeadline.cpp
@@ -36,9 +36,11 @@ double IdleDeadline::time_remaining() const
auto const& event_loop = HTML::main_thread_event_loop();
// 1. Let now be a DOMHighResTimeStamp representing current high resolution time in milliseconds.
- auto now = HighResolutionTime::current_high_resolution_time(HTML::current_global_object());
+ auto& global = HTML::current_global_object();
+ auto now = HighResolutionTime::current_high_resolution_time(global);
// 2. Let deadline be the result of calling IdleDeadline's get deadline time algorithm.
- auto deadline = event_loop.compute_deadline();
+ // NB: The event loop deadline uses the shared monotonic clock, while now is relative to the global's time origin.
+ auto deadline = HighResolutionTime::relative_high_resolution_time(event_loop.compute_deadline(), global);
// 3. Let timeRemaining be deadline - now.
auto time_remaining = deadline - now;
// 4. If timeRemaining is negative, set it to 0.
diff --git a/Tests/LibWeb/Text/expected/request-idle-callback-time-budget.txt b/Tests/LibWeb/Text/expected/request-idle-callback-time-budget.txt
new file mode 100644
index 00000000000..a9844e632d9
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/request-idle-callback-time-budget.txt
@@ -0,0 +1 @@
+Idle budget is between 0 and 50 ms: true
diff --git a/Tests/LibWeb/Text/input/request-idle-callback-time-budget.html b/Tests/LibWeb/Text/input/request-idle-callback-time-budget.html
new file mode 100644
index 00000000000..e19e69df90e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/request-idle-callback-time-budget.html
@@ -0,0 +1,12 @@
+<!doctype html>
+<script src="include.js"></script>
+<script>
+ asyncTest(done => {
+ requestIdleCallback(deadline => {
+ const initialRemaining = deadline.timeRemaining();
+ println(`Idle budget is between 0 and 50 ms: ${initialRemaining >= 0 && initialRemaining <= 50}`);
+
+ done();
+ });
+ });
+</script>