Skip to content

Truly independent web browser

LibWeb: Report Event Timing entries before the tasks after an update

sideshowbarker99a2754c45f7
diff --git a/Libraries/LibWeb/EventTiming/EventTiming.cpp b/Libraries/LibWeb/EventTiming/EventTiming.cpp
index 0e3d56adee1..8d2febc93cf 100644
--- a/Libraries/LibWeb/EventTiming/EventTiming.cpp
+++ b/Libraries/LibWeb/EventTiming/EventTiming.cpp
@@ -561,6 +561,18 @@ static void set_event_timing_entry_duration(PerformanceEventTiming& timing_entry
}
}
+bool has_pending_event_timing_entries(DOM::Document const& document)
+{
+ auto window = document.window();
+ if (!window)
+ return false;
+ auto& state = window->event_timing_state();
+ if (!state.entries_to_be_queued.is_empty())
+ return true;
+ auto has_no_duration = [](auto const& it) { return !it.value->has_duration(); };
+ return any_of(state.pending_pointer_downs, has_no_duration) || any_of(state.pending_key_downs, has_no_duration);
+}
+
// https://w3c.github.io/event-timing/#dispatch-pending-event-timing-entries
void dispatch_pending_event_timing_entries(DOM::Document& document)
{
diff --git a/Libraries/LibWeb/EventTiming/EventTiming.h b/Libraries/LibWeb/EventTiming/EventTiming.h
index 09797f63eed..92ae339c11d 100644
--- a/Libraries/LibWeb/EventTiming/EventTiming.h
+++ b/Libraries/LibWeb/EventTiming/EventTiming.h
@@ -77,4 +77,8 @@ void finalize_event_timing(GC::Ptr<PerformanceEventTiming>, DOM::Event const&, D
// https://w3c.github.io/event-timing/#dispatch-pending-event-timing-entries
void dispatch_pending_event_timing_entries(DOM::Document&);
+// Whether dispatching the document's pending Event Timing entries would deliver anything to script: an entry it would
+// queue, or the duration and event count of a pending pointerdown or keydown it would set.
+bool has_pending_event_timing_entries(DOM::Document const&);
+
}
diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
index e75f0abad2c..ed3c87dcd88 100644
--- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
+++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
@@ -801,6 +801,8 @@ enum class LayoutHeldForTesting : bool {
// observations the last doc's layout answers, and the animations it samples and transitions it starts, whose timing
// script sees follow the update. Once the transaction is taken in, those are the ones its style began, and the ones
// running on the compositor, which the update's frame publishes.
+// The Event Timing entries pending for any doc count too: The update sets their durations from the time it reaches them,
+// and queues those that lasted 16ms or more for observers to see.
// AD-HOC: The render clock's lanes of the docs the update took in present nothing until its frame: a task beside the
// update would leave the pointer unhovered and the animations still for as long as it runs, where the lanes
// would hover or animate beside it. A test that holds the layout that flies runs the doc's tasks beside it.
@@ -809,8 +811,9 @@ static HeldTasks tasks_rendering_update_holds(Vector<GC::Root<DOM::Document>> co
{
if (any_of(docs, [](auto const& document) { return document->has_intersection_observation_targets(); }))
return HeldTasks::All;
- // The focus fixup of every doc whose focused area is not its viewport reads that area as the update leaves it.
- if (any_of(docs.span().slice(0, docs.size() - 1), [](auto const& document) { return focus_fixup_rule_may_apply(*document); }))
+ // The focus fixup of every doc whose focused area is not its viewport reads that area as the update leaves it, and
+ // the Event Timing entries pending for a doc are reported as the update reaches them.
+ if (any_of(docs.span().slice(0, docs.size() - 1), [](auto const& document) { return focus_fixup_rule_may_apply(*document) || EventTiming::has_pending_event_timing_entries(*document); }))
return HeldTasks::All;
// The lanes of a doc the update took in present nothing until it seals their plan, which its tasks would hold up.
auto lanes_wait = [&](DOM::Document const& document) {
@@ -820,7 +823,7 @@ static HeldTasks tasks_rendering_update_holds(Vector<GC::Root<DOM::Document>> co
if (any_of(docs.span().slice(0, docs.size() - 1), [&](auto const& document) { return lanes_wait(*document); }))
return HeldTasks::All;
auto& document = *docs.last();
- if (document.has_resize_observers() || focus_fixup_rule_may_apply(document))
+ if (document.has_resize_observers() || focus_fixup_rule_may_apply(document) || EventTiming::has_pending_event_timing_entries(document))
return HeldTasks::OfLastDocument;
if (lanes_wait(document))
return HeldTasks::OfLastDocument;
diff --git a/Tests/LibWeb/Text/expected/EventTiming/entries-are-reported-before-the-tasks-after-the-rendering-update.txt b/Tests/LibWeb/Text/expected/EventTiming/entries-are-reported-before-the-tasks-after-the-rendering-update.txt
new file mode 100644
index 00000000000..e428786f144
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/EventTiming/entries-are-reported-before-the-tasks-after-the-rendering-update.txt
@@ -0,0 +1 @@
+mouseover on #second
diff --git a/Tests/LibWeb/Text/input/EventTiming/entries-are-reported-before-the-tasks-after-the-rendering-update.html b/Tests/LibWeb/Text/input/EventTiming/entries-are-reported-before-the-tasks-after-the-rendering-update.html
new file mode 100644
index 00000000000..1388efa16be
--- /dev/null
+++ b/Tests/LibWeb/Text/input/EventTiming/entries-are-reported-before-the-tasks-after-the-rendering-update.html
@@ -0,0 +1,48 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ function nextTask() {
+ return new Promise(resolve => setTimeout(resolve, 0));
+ }
+
+ async function afterNextFrame() {
+ await animationFrame();
+ await nextTask();
+ }
+
+ function busyWait(milliseconds) {
+ const end = performance.now() + milliseconds;
+ while (performance.now() < end) { }
+ }
+
+ asyncTest(async done => {
+ // The pointer starts out where the boxes will not be.
+ internals.mouseMove(500, 400);
+ await afterNextFrame();
+
+ const first = document.createElement("div");
+ first.id = "first";
+ first.style = "width: 100px; height: 50px";
+ const second = document.createElement("div");
+ second.id = "second";
+ second.style = "width: 100px; height: 50px";
+ document.body.append(first, second);
+ second.addEventListener("mouseover", () => busyWait(30));
+
+ // Moving the pointer onto the first box dispatches events that Event Timing times. The rendering update after
+ // them reports their entries, and drops those that took less than 16ms, before the tasks that follow it run.
+ internals.mouseMove(50, 25);
+ await afterNextFrame();
+
+ // So an observer that starts only now sees just the entries of the slow move onto the second box.
+ const entries = [];
+ new PerformanceObserver(list => entries.push(...list.getEntriesByName("mouseover"))).observe({ type: "event", durationThreshold: 16 });
+ internals.mouseMove(50, 75);
+ await afterNextFrame();
+ await afterNextFrame();
+
+ for (const entry of entries)
+ println(`mouseover on #${entry.target.id}`);
+ done();
+ });
+</script>