Truly independent web browser
/*
* Copyright (c) 2021, Andreas Kling <[email protected]>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/AnyOf.h>
#include <AK/Debug.h>
#include <AK/JsonObject.h>
#include <AK/TemporaryChange.h>
#include <LibCore/EventLoop.h>
#include <LibCore/ThreadEventQueue.h>
#include <LibGC/Heap.h>
#include <LibGC/HeapAccess.h>
#include <LibJS/Runtime/VM.h>
#include <LibWeb/Animations/NamedTimelines.h>
#include <LibWeb/Animations/RecordHandles.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/CSS/FontFaceSet.h>
#include <LibWeb/CSS/StyleComputer.h>
#include <LibWeb/CSS/StyleScope.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Element.h>
#include <LibWeb/EventTiming/EventTiming.h>
#include <LibWeb/HTML/AnimationFrameCallbackDriver.h>
#include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/HTML/EventLoop/ClockPlan.h>
#include <LibWeb/HTML/EventLoop/EventLoop.h>
#include <LibWeb/HTML/EventLoop/FrameCompletion.h>
#include <LibWeb/HTML/EventLoop/RenderingSchedulerCounters.h>
#include <LibWeb/HTML/Focus.h>
#include <LibWeb/HTML/FormAssociatedElement.h>
#include <LibWeb/HTML/HTMLMediaElement.h>
#include <LibWeb/HTML/ListOfAvailableImages.h>
#include <LibWeb/HTML/LocalTraversableNavigable.h>
#include <LibWeb/HTML/NavigableContainer.h>
#include <LibWeb/HTML/Scripting/Agent.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/HTML/WorkletGlobalScope.h>
#include <LibWeb/HighResolutionTime/Performance.h>
#include <LibWeb/HighResolutionTime/TimeOrigin.h>
#include <LibWeb/IndexedDB/IDBFactory.h>
#include <LibWeb/Layout/Node.h>
#include <LibWeb/Layout/NodeArena.h>
#include <LibWeb/Layout/TreeBuilderRustFFI.h>
#include <LibWeb/Page/EventHandler.h>
#include <LibWeb/Page/Page.h>
#include <LibWeb/Page/RenderingStats.h>
#include <LibWeb/PaintTiming/PaintTiming.h>
#include <LibWeb/Painting/BoxViews.h>
#include <LibWeb/Painting/DocumentPaintState.h>
#include <LibWeb/Platform/EventLoopPlugin.h>
#include <LibWeb/Platform/Timer.h>
#include <LibWebCommon/Page/QueuedInputEvent.h>
namespace Web::HTML {
GC_DEFINE_ALLOCATOR(EventLoop);
EventLoop::EventLoop(Type type)
: m_type(type)
{
if (m_type == Type::Window) {
// The threads a window's rendering runs on only read what the host lends them and never touch the heap
// themselves. This makes that a debug assertion (and a collection there a verification failure) for whatever
// C++ code they call.
Layout::RustFFI::stage_thread_set_thread_setup([] { GC::forbid_heap_access_on_this_thread(); });
}
m_task_queue = GC::Heap::the().allocate<TaskQueue>(*this);
m_rendering_task_function = GC::create_function(GC::Heap::the(), GC::captures(*this), [](EventLoop& self) {
VERIFY(self.m_rendering_task_queued);
self.m_rendering_task_queued = false;
self.update_the_rendering();
});
}
EventLoop::~EventLoop() = default;
String EventLoop::take_rendering_trace()
{
StringBuilder builder;
for (auto const& line : m_rendering_trace.value_or({})) {
builder.append(line.text);
if (line.repeats > 1)
builder.appendff(" x{}", line.repeats);
builder.append('\n');
}
m_rendering_trace.clear();
return builder.to_string_without_validation();
}
void EventLoop::note_rendering_trace_event(StringView event)
{
if (!m_rendering_trace.has_value())
return;
if (!m_rendering_trace->is_empty() && m_rendering_trace->last().text == event) {
++m_rendering_trace->last().repeats;
return;
}
m_rendering_trace->append({ MUST(String::from_utf8(event)) });
}
void note_rendering_trace_event(StringView event)
{
main_thread_event_loop().note_rendering_trace_event(event);
}
void EventLoop::visit_edges(Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_reached_step_1_tasks);
visitor.visit(m_task_queue);
m_microtask_queue.for_each([&](auto& task) { visitor.visit(task); });
visitor.visit(m_currently_running_task);
visitor.visit(m_backup_incumbent_realm_stack);
visitor.visit(m_rendering_task_function);
visitor.visit(m_system_event_loop_timer);
visitor.visit(m_idle_period_timer);
visitor.visit(m_navigables_with_frames_in_flight);
visitor.visit(m_navigables_with_clock_lanes);
}
void EventLoop::schedule()
{
m_turn_requested_without_a_task = true;
schedule_for_queued_task();
}
void EventLoop::schedule_for_queued_task()
{
if (!m_system_event_loop_timer) {
m_system_event_loop_timer = Platform::Timer::create_single_shot(GC::Heap::the(), AK::Duration::from_milliseconds(0), GC::create_function(GC::Heap::the(), GC::captures(*this), [](EventLoop& self) {
self.process();
}));
}
if (!m_system_event_loop_timer->is_active())
m_system_event_loop_timer->restart();
}
void EventLoop::destroy_once_script_has_finished()
{
m_destruction_requested = true;
schedule();
}
bool EventLoop::destroy_if_requested()
{
// NB: A paused event loop has script waiting further up the stack, as a spinning one has.
if (!m_destruction_requested || m_spin_depth > 0 || execution_paused())
return false;
if (auto on_destroyed = move(m_on_destroyed))
on_destroyed();
return true;
}
EventLoop& main_thread_event_loop()
{
return *static_cast<HTML::Agent*>(Bindings::main_thread_vm().agent())->event_loop;
}
EventLoop& current_event_loop()
{
return *surrounding_agent().event_loop;
}
// https://html.spec.whatwg.org/multipage/webappapis.html#spin-the-event-loop
void EventLoop::spin_until(GC::Ref<GC::Function<bool()>> goal_condition)
{
// FIXME: The spec wants us to do the rest of the enclosing algorithm (i.e. the caller)
// in the context of the currently running task on entry. That's not possible with this implementation.
// 1. Let task be the event loop's currently running task.
// 2. Let task source be task's source.
// 3. Let old stack be a copy of the JavaScript execution context stack.
// 4. Empty the JavaScript execution context stack.
auto& vm = this->vm();
vm.save_execution_context_stack();
vm.clear_execution_context_stack();
// AD-HOC: A rendering update whose style update flies finishes first, as a pause that updates the rendering
// finishes it: the tasks the nested event loop runs come after the whole of it.
finish_rendering_update_in_flight();
// 5. Perform a microtask checkpoint.
perform_a_microtask_checkpoint();
// 6. In parallel:
// 1. Wait until the condition goal is met.
// 2. Queue a task on task source to:
// 1. Replace the JavaScript execution context stack with old stack.
// 2. Perform any steps that appear after this spin the event loop instance in the original algorithm.
// NOTE: This is achieved by returning from the function.
++m_spin_depth;
ScopeGuard leave_spin = [this] {
--m_spin_depth;
if (m_destruction_requested)
schedule();
};
Platform::EventLoopPlugin::the().spin_until(GC::create_function(GC::Heap::the(), GC::captures(*this, goal_condition), [](auto& self, auto& goal_condition) {
if (goal_condition->function()())
return true;
if (self.m_task_queue->has_runnable_tasks()) {
self.schedule();
// FIXME: Remove the platform event loop plugin so that this doesn't look out of place
Core::EventLoop::current().wake();
}
return goal_condition->function()();
}));
vm.restore_execution_context_stack();
// 7. Stop task, allowing whatever algorithm that invoked it to resume.
// NOTE: This is achieved by returning from the function.
}
void EventLoop::process()
{
m_turn_requested_without_a_task = false;
if (destroy_if_requested() || execution_paused())
return;
// OPTIMIZATION: A trip through the platform event loop costs more than a small task does, so run the tasks that are
// ready here for a short while. Each turn is one turn of the processing model with one task, so this
// is not visible to the page. The platform event loop gets its turn as soon as anything was posted to
// it. What it would only learn from the kernel waits for no longer than this deadline. A nested event
// loop goes back after every task, to check what it is waiting for.
static constexpr auto maximum_time_between_platform_event_loop_turns = AK::Duration::from_microseconds(100);
auto const turn_deadline = MonotonicTime::now() + maximum_time_between_platform_event_loop_turns;
bool has_runnable_tasks = false;
for (;;) {
has_runnable_tasks = run_one_turn_of_the_processing_model();
if (!has_runnable_tasks || execution_paused() || m_spin_depth > 0)
break;
if (Core::ThreadEventQueue::current().has_pending_events() || MonotonicTime::now() >= turn_deadline)
break;
}
if (destroy_if_requested())
return;
// If there are eligible tasks in the queue, schedule a new round of processing. :^)
if (has_runnable_tasks || (!m_microtask_queue.is_empty() && !m_performing_a_microtask_checkpoint)) {
schedule();
return;
}
// NB: A task that the turns above have run may have armed the timer for itself. What asked for a turn without
// queueing a task still gets one.
if (m_system_event_loop_timer && !m_turn_requested_without_a_task)
m_system_event_loop_timer->stop();
}
// https://html.spec.whatwg.org/multipage/webappapis.html#event-loop-processing-model
// Answers whether the event loop has a runnable task after this turn.
bool EventLoop::run_one_turn_of_the_processing_model()
{
if (Core::StatsLog::the())
begin_rendering_pending_task();
ScopeGuard pending_task = [] {
if (Core::StatsLog::the())
end_rendering_pending_task();
};
// 1. Let oldestTask and taskStartTime be null.
GC::Ptr<Task> oldest_task;
[[maybe_unused]] double task_start_time = 0;
m_task_generation++;
// AD-HOC: A rendering update's style update and recording fly beside the tasks after it, and each is taken in
// once it has finished, between two tasks.
take_finished_frames_in();
// Some algorithms request that steps or states only occur once the event loop has reached step 1.
// Invoke a set of tasks that these algorithms request us to in order to achieve this.
GC::RootVector reached_step_1_tasks { move(m_reached_step_1_tasks) };
for (auto& reached_step_1_task : reached_step_1_tasks)
reached_step_1_task->function()();
// 2. If the event loop has a task queue with at least one runnable task, then:
// NB: Looking for the first runnable task once answers this as well.
// 1. Let taskQueue be one such task queue, chosen in an implementation-defined manner.
auto task_queue = m_task_queue;
// 2. Set taskStartTime to the unsafe shared current time.
task_start_time = HighResolutionTime::unsafe_shared_current_time();
// 3. Set oldestTask to the first runnable task in taskQueue, and remove it from taskQueue.
oldest_task = task_queue->take_first_runnable();
// AD-HOC: Beside an idle event loop, whose rendering updates run a document's animations, the render clock's lanes
// only hover what is under the pointer.
if (!oldest_task && !task_queue->has_runnable_tasks())
note_clock_lanes(ClockAnimations::Hold);
if (oldest_task) {
// FIXME: 4. If oldestTask's document is not null, then record task start time given taskStartTime and oldestTask's document.
// AD-HOC: While a task runs, the render clock's lanes tick the animations the last rendering update planned for.
// A task that runs beside a rendering update in flight comes after the whole of it.
if (oldest_task->source() != Task::Source::Rendering) {
note_clock_lanes(ClockAnimations::Run);
note_script_ran_beside_rendering_update_in_flight();
}
// 5. Set the event loop's currently running task to oldestTask.
m_currently_running_task = oldest_task.ptr();
// NB: A task that spins the event loop runs other tasks in the middle of its own, so each restores what it found.
TemporaryChange timer_nesting_level_for_microtasks { m_timer_nesting_level_for_microtasks, oldest_task->timer_nesting_level() };
// 6. Perform oldestTask's steps.
oldest_task->execute();
// 7. Set the event loop's currently running task back to null.
m_currently_running_task = nullptr;
// 8. Perform a microtask checkpoint.
perform_a_microtask_checkpoint();
}
// 3. Let taskEndTime be the unsafe shared current time. [HRT]
[[maybe_unused]] auto task_end_time = HighResolutionTime::unsafe_shared_current_time();
if (oldest_task && oldest_task->source() != Task::Source::Rendering) {
auto task_duration = task_end_time - task_start_time;
auto task_duration_microseconds = rendering_scheduler_duration_microseconds(task_duration);
++m_rendering_scheduler_counters.tasks_between_updates;
m_rendering_scheduler_counters.task_microseconds_between_updates += task_duration_microseconds;
switch (oldest_task->source()) {
case Task::Source::PostedMessage:
++m_rendering_scheduler_counters.posted_message_tasks_between_updates;
m_rendering_scheduler_counters.posted_message_task_microseconds_between_updates += task_duration_microseconds;
break;
case Task::Source::TimerTask:
++m_rendering_scheduler_counters.timer_tasks_between_updates;
m_rendering_scheduler_counters.timer_task_microseconds_between_updates += task_duration_microseconds;
break;
case Task::Source::Networking:
++m_rendering_scheduler_counters.networking_tasks_between_updates;
m_rendering_scheduler_counters.networking_task_microseconds_between_updates += task_duration_microseconds;
break;
case Task::Source::DOMManipulation:
++m_rendering_scheduler_counters.dom_manipulation_tasks_between_updates;
m_rendering_scheduler_counters.dom_manipulation_task_microseconds_between_updates += task_duration_microseconds;
break;
default:
break;
}
}
// 4. If oldestTask is not null, then:
if (oldest_task) {
// FIXME: 1. Let top-level browsing contexts be an empty set.
// FIXME: 2. For each environment settings object settings of oldestTask's script evaluation environment settings object set:
// FIXME: 2.1. Let global be settings's global object.
// FIXME: 2.2. If global is not a Window object, then continue.
// FIXME: 2.3. If global's browsing context is null, then continue.
// FIXME: 2.4. Let tlbc be global's browsing context's top-level browsing context.
// FIXME: 2.5. If tlbc is not null, then append it to top-level browsing contexts.
// FIXME: 3. Report long tasks, passing in taskStartTime, taskEndTime, top-level browsing contexts, and oldestTask.
// FIXME: 4. If oldestTask's document is not null, then record task end time given taskEndTime and oldestTask's document.
}
bool has_runnable_tasks = m_task_queue->has_runnable_tasks();
// 5. If this is a window event loop that has no runnable task in this event loop's task queues, then:
// NB: A callback that asks for the next one through a task still waits, as a page would otherwise spin while idle.
static constexpr double minimum_time_between_idle_periods_in_ms = 1;
bool idle_period_may_start = !m_idle_period_timer || !m_idle_period_timer->is_active()
|| (m_idle_callback_requested_outside_idle_period && HighResolutionTime::unsafe_shared_current_time() - m_last_idle_period_start_time >= minimum_time_between_idle_periods_in_ms);
if (m_type == Type::Window && !has_runnable_tasks && m_may_have_idle_callbacks && idle_period_may_start) {
auto windows = same_loop_windows();
bool has_idle_callbacks = false;
bool has_callbacks_of_hidden_document = false;
for (auto& window : windows) {
if (!window->has_idle_callbacks())
continue;
if (window->associated_document().hidden()) {
has_callbacks_of_hidden_document = true;
continue;
}
has_idle_callbacks = true;
break;
}
// NB: same_loop_windows() leaves out a document that is not fully active, whose callbacks wait for it to come back.
bool has_callbacks_of_inactive_document = any_of(m_documents, [](auto const& document) {
return document && !document->is_fully_active() && document->window() && document->window()->has_idle_callbacks();
});
// OPTIMIZATION: Nothing is looked at here again until an idle callback is asked for.
m_may_have_idle_callbacks = has_idle_callbacks || has_callbacks_of_hidden_document || has_callbacks_of_inactive_document;
if (has_idle_callbacks) {
// NB: Delay the next idle period until this one's 50 ms budget has elapsed. Callbacks registered
// 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. A callback requested from
// anything but an idle callback starts an idle period right away (see did_request_idle_callback()).
m_idle_callback_requested_outside_idle_period = false;
if (!m_idle_period_timer) {
m_idle_period_timer = Platform::Timer::create_single_shot(GC::Heap::the(), AK::Duration::from_milliseconds(50), GC::create_function(GC::Heap::the(), GC::captures(*this), [](EventLoop& self) {
self.schedule();
}));
}
m_idle_period_timer->restart();
// 1. Set this event loop's last idle period start time to the unsafe shared current time.
m_last_idle_period_start_time = HighResolutionTime::unsafe_shared_current_time();
// 2. Let computeDeadline be the following steps:
// Implemented in EventLoop::compute_deadline()
// 3. For each win of the same-loop windows for this event loop, perform the start an idle period algorithm for win with the following step: return the result of calling computeDeadline, coarsened given win's relevant settings object's cross-origin isolated capability. [REQUESTIDLECALLBACK]
for (auto& window : windows)
window->start_an_idle_period();
has_runnable_tasks = m_task_queue->has_runnable_tasks();
}
}
return has_runnable_tasks;
}
void EventLoop::request_rendering_update()
{
++m_rendering_scheduler_counters.update_requests;
if (m_running_rendering_task)
++m_rendering_scheduler_counters.update_requests_while_rendering;
if (m_rendering_update_requested || m_rendering_task_queued) {
++m_rendering_scheduler_counters.coalesced_update_requests;
note_rendering_trace_event(m_running_rendering_task ? "request while rendering, coalesced"sv : "request, coalesced"sv);
return;
}
note_rendering_trace_event(m_running_rendering_task ? "request while rendering"sv : "request"sv);
m_rendering_update_requested = true;
}
// https://html.spec.whatwg.org/multipage/webappapis.html#event-loop-processing-model
bool EventLoop::rendering_opportunity(HighResolutionTime::DOMHighResTimeStamp frame_time, RenderingOpportunitySource source)
{
++m_rendering_scheduler_counters.opportunities_received;
if (source == RenderingOpportunitySource::Watchdog)
++m_rendering_scheduler_counters.watchdog_opportunities;
note_rendering_trace_event(source == RenderingOpportunitySource::Watchdog ? "opportunity (watchdog)"sv : "opportunity"sv);
// FIXME: 1. Wait until at least one navigable whose active document's relevant agent's event loop is eventLoop might have a rendering opportunity.
// 2. Set eventLoop's last render opportunity time to the unsafe shared current time.
// INTEROP: Compositor-provided display timestamps match the shared monotonic clock. Clamp delayed or out-of-order
// IPC delivery so rendering timestamps remain monotonic and never describe a future frame.
auto now = HighResolutionTime::unsafe_shared_current_time();
m_last_render_opportunity_time = max(m_last_render_opportunity_time, min(frame_time, now));
// AD-HOC: A nested event loop can deliver a timer while a rendering update is running. Keep the request pending
// so the PageClient can schedule it for the next opportunity instead of queueing a second rendering task.
if (m_running_rendering_task)
return false;
m_rendering_update_requested = false;
if (m_rendering_task_queued)
return true;
// 3. For each navigable that has a rendering opportunity, queue a global task on the rendering task source given navigable's active window to update the rendering:
bool has_eligible_navigable = false;
for (auto& navigable : all_local_navigables()) {
if (!navigable->is_local_root())
continue;
if (!navigable->has_a_rendering_opportunity())
continue;
auto document = navigable->active_document();
if (!document)
continue;
if (document->is_decoded_svg())
continue;
has_eligible_navigable = true;
break;
}
if (!has_eligible_navigable)
return false;
// AD-HOC: One rendering update services every document in this event loop, so queue one event-loop task for the
// opportunity instead of one global task per page local root.
VERIFY(!m_rendering_task_queued);
m_rendering_task_queued = true;
m_rendering_task_queued_time = MonotonicTime::now();
queue_a_task(Task::Source::Rendering, this, nullptr, *m_rendering_task_function);
++m_rendering_scheduler_counters.opportunities_that_queued_a_task;
return true;
}
static InputRoutingDecision input_routing_decision_for_finished_input_event(EventResult result)
{
switch (result) {
case EventResult::Accepted:
return InputRoutingDecision::FinishedAccepted;
case EventResult::Handled:
return InputRoutingDecision::FinishedHandled;
case EventResult::Dropped:
return InputRoutingDecision::FinishedDropped;
case EventResult::Cancelled:
return InputRoutingDecision::FinishedCancelled;
}
VERIFY_NOT_REACHED();
}
void EventLoop::process_input_events() const
{
auto process_input_events_queue = [&](Page& page) {
auto& page_client = page.client();
auto& input_events_queue = page_client.input_event_queue();
// Process events only for this page, collecting others to re-enqueue
Queue<Web::QueuedInputEvent> events_for_other_pages;
auto record_routing = [&](u64 event_id, InputRoutingDecision decision) {
page.input_trace().record({ .input_event_id = event_id, .stage = InputTraceStage::WebContent, .decision = decision });
};
auto finish_handling_input_event = [&](u64 event_id, EventResult result, InputRoutingDecision decision) {
record_routing(event_id, decision);
page_client.report_finished_handling_input_event(page_client.id(), event_id, result);
};
auto finish_handling_coalesced_input_events = [&](QueuedInputEvent const& event) {
for (auto coalesced_event_id : event.coalesced_event_ids)
finish_handling_input_event(coalesced_event_id, EventResult::Dropped, InputRoutingDecision::CoalescedIntoLaterEvent);
};
while (!input_events_queue.is_empty()) {
auto event = input_events_queue.dequeue();
// Skip events that are not intended for this page
if (event.page_id != page_client.id()) {
events_for_other_pages.enqueue(move(event));
continue;
}
// A key event goes to the tab's focused navigable. Other events go to the local root given by the event's
// navigable ID, or the page's traversable if it has none.
GC::Ptr<LocalNavigable> root;
if (event.event.has<Web::KeyEvent>())
root = page.hosted_focused_navigable();
else if (event.navigable_id.has_value())
root = as_if<LocalNavigable>(page.navigable_with_id(*event.navigable_id).ptr());
else if (page.has_local_traversable())
root = page.local_traversable();
if (!root) {
finish_handling_coalesced_input_events(event);
finish_handling_input_event(input_event_id(event.event), EventResult::Dropped, InputRoutingDecision::DroppedWithoutLocalRoot);
continue;
}
// AD-HOC: The screen showed the hover of a newer pointer move than a mouse event that reached the queue
// before the task the hover of a clock lane took that move beside, which the host keeps where it
// took in what the lanes did: the pointer never moves back to where the event says it was. A move
// or leave is dropped, as the queue coalesces the moves it holds, and the newer move's own event
// follows. Any other event is handled, without moving the hover or the pointer.
bool input_is_outdated = false;
if (auto const* mouse_event = event.event.get_pointer<Web::MouseEvent>(); mouse_event && (!mouse_event->pointer_data.ptr() || mouse_event->pointer_data.ptr()->pointer_type == PointerType::Mouse)) {
auto document = root->active_document();
auto* arena = document ? document->layout_node_arena_if_created() : nullptr;
input_is_outdated = arena && Layout::RustFFI::document_host_mouse_event_is_outdated(arena->host(), mouse_event->id);
// A move with a button held is a drag's, whose script hears every move: it is handled, as outdated.
if (input_is_outdated && mouse_event->buttons == 0 && first_is_one_of(mouse_event->type, Web::MouseEvent::Type::MouseMove, Web::MouseEvent::Type::MouseLeave)) {
finish_handling_coalesced_input_events(event);
finish_handling_input_event(mouse_event->id, EventResult::Dropped, InputRoutingDecision::DroppedOutdatedPointerMove);
continue;
}
}
root->event_handler().set_handling_outdated_input(input_is_outdated);
auto const* handled_mouse_event = event.event.get_pointer<Web::MouseEvent>();
root->event_handler().set_handling_input_event_id(handled_mouse_event ? handled_mouse_event->id : 0);
ScopeGuard end_outdated_input = [&] {
root->event_handler().set_handling_outdated_input(false);
root->event_handler().set_handling_input_event_id(0);
};
Optional<RemoteInputEventTarget> remote_target;
auto result = event.event.visit(
[&](Web::KeyEvent const& key_event) {
switch (key_event.type) {
case Web::KeyEvent::Type::KeyDown:
return page.handle_keydown(key_event.key, key_event.modifiers, key_event.code_point, key_event.repeat, key_event.should_insert_text, key_event.async_scroll_performed_default_action, key_event.editing_commands, key_event.native_editing_binding_resolved, key_event.user_input_scroll_count_with_nothing_to_scroll);
case Web::KeyEvent::Type::KeyUp:
return page.handle_keyup(key_event.key, key_event.modifiers, key_event.code_point, key_event.repeat);
}
VERIFY_NOT_REACHED();
},
[&](Web::MouseEvent const& mouse_event) {
if (auto* pointer = mouse_event.pointer_data.ptr(); pointer && pointer->pointer_type != PointerType::Mouse)
return root->event_handler().handle_pointer_event(mouse_event, page.device_to_css_point(mouse_event.position), page.device_to_css_point(mouse_event.screen_position), &remote_target);
switch (mouse_event.type) {
case Web::MouseEvent::Type::MouseDown:
return page.handle_mousedown(*root, mouse_event.position, mouse_event.screen_position, mouse_event.button, mouse_event.buttons, mouse_event.modifiers, mouse_event.click_count, mouse_event.scrollbar_dragged_by_compositor, &remote_target);
case Web::MouseEvent::Type::MouseUp:
return page.handle_mouseup(*root, mouse_event.position, mouse_event.screen_position, mouse_event.button, mouse_event.buttons, mouse_event.modifiers, &remote_target);
case Web::MouseEvent::Type::MouseMove:
return page.handle_mousemove(*root, mouse_event.position, mouse_event.screen_position, mouse_event.buttons, mouse_event.modifiers, &remote_target);
case Web::MouseEvent::Type::MouseLeave:
return page.handle_mouseleave(*root);
case Web::MouseEvent::Type::MouseCancel:
return page.handle_mousecancel(*root);
case Web::MouseEvent::Type::MouseWheel:
if (mouse_event.async_scroll_performed_default_action) {
dbgln_if(COMPOSITOR_DEBUG, "[Compositor] Main thread handling DOM wheel after async default action");
return page.handle_mousewheel(*root, mouse_event.position, mouse_event.screen_position, mouse_event.button, mouse_event.buttons, mouse_event.modifiers, mouse_event.wheel_delta_x, mouse_event.wheel_delta_y, mouse_event.wheel_tick_120ths_x, mouse_event.wheel_tick_120ths_y, mouse_event.wheel_delta_precision, mouse_event.scroll_gesture_phase, true, nullptr, &remote_target, mouse_event.wheel_gesture_mark);
}
return page.handle_mousewheel(*root, mouse_event.position, mouse_event.screen_position, mouse_event.button, mouse_event.buttons, mouse_event.modifiers, mouse_event.wheel_delta_x, mouse_event.wheel_delta_y, mouse_event.wheel_tick_120ths_x, mouse_event.wheel_tick_120ths_y, mouse_event.wheel_delta_precision, mouse_event.scroll_gesture_phase, false, nullptr, &remote_target, mouse_event.wheel_gesture_mark);
}
VERIFY_NOT_REACHED();
},
[&](Web::DragEvent& drag_event) {
return page.handle_drag_and_drop_event(*root, drag_event.type, drag_event.position, drag_event.screen_position, drag_event.button, drag_event.buttons, drag_event.modifiers, move(drag_event.files));
},
[&](Web::PinchEvent& pinch_event) {
return page.handle_pinch_event(*root, pinch_event, &remote_target);
});
finish_handling_coalesced_input_events(event);
// The pointer is over content another process hosts: that process handles the event, at the position in
// the viewport of the navigable it hosts, and finishes it.
if (remote_target.has_value()) {
record_routing(input_event_id(event.event), InputRoutingDecision::ForwardedToRemoteFrame);
if (auto const* pinch_event = event.event.get_pointer<Web::PinchEvent>()) {
auto forwarded_pinch_event = *pinch_event;
forwarded_pinch_event.position = page.css_to_device_point(remote_target->position);
page_client.forward_pinch_event_to_remote_navigable(event.page_id, remote_target->navigable_id, forwarded_pinch_event);
continue;
}
auto mouse_event = event.event.get<Web::MouseEvent>().clone_without_browser_data();
mouse_event.position = page.css_to_device_point(remote_target->position);
page_client.forward_mouse_event_to_remote_navigable(event.page_id, remote_target->navigable_id, move(mouse_event));
continue;
}
page_client.did_handle_input_event(event.page_id, event.event);
finish_handling_input_event(input_event_id(event.event), result, input_routing_decision_for_finished_input_event(result));
}
// Re-enqueue events for other pages
while (!events_for_other_pages.is_empty()) {
input_events_queue.enqueue(events_for_other_pages.dequeue());
}
};
// Every page hosting a document takes the input events queued for it, once.
GC::RootVector<GC::Ref<Page>> pages;
for (auto& navigable : all_local_navigables()) {
if (!navigable->is_local_root() || navigable->has_been_destroyed())
continue;
auto document = navigable->active_document();
if (!document || document->is_decoded_svg())
continue;
if (!pages.contains_slow(GC::Ref { navigable->page() }))
pages.append(navigable->page());
}
for (auto const& page : pages)
process_input_events_queue(*page);
}
static GC::RootVector<GC::Ref<Page>> pages_of_local_roots()
{
GC::RootVector<GC::Ref<Page>> pages;
for (auto& navigable : all_local_navigables()) {
if (!navigable->is_local_root())
continue;
if (!pages.contains_slow(GC::Ref { navigable->page() }))
pages.append(navigable->page());
}
return pages;
}
enum class HeldTasks : u8 {
None,
OfLastDocument,
All,
};
// A rendering update whose style update flies for its last document, with what its steps from that document's style
// and layout on read.
struct EventLoop::RenderingUpdateInFlight {
AK_ALLOC_WITH_KMALLOC;
DOM::Document& document() const { return *docs.last(); }
// Whether the rest of the update's steps deliver nothing to script: an update of one doc that holds no task back
// ends before them, and they run as its frame is taken in, beside the tasks after it or in the next rendering update,
// which may begin beside the frame.
bool ended() const { return held_tasks == HeldTasks::None && docs.size() == 1; }
// Whether script of `document` ran beside the update.
bool ran_script_of_beside(DOM::Document const& document) const
{
if (!script_ran_beside)
return false;
if (held_for_testing)
return true;
switch (least_held_tasks) {
case HeldTasks::None:
return true;
case HeldTasks::OfLastDocument:
return &document != docs.last().ptr();
case HeldTasks::All:
return false;
}
VERIFY_NOT_REACHED();
}
Vector<GC::Root<DOM::Document>> docs;
double frame_timestamp { 0 };
// The serial number of the update, which the plans it seals carry.
u64 serial { 0 };
// The event loop does not take the transaction in while a test holds it.
bool held_for_testing { false };
// Whose tasks wait for the whole of the update rather than run beside it.
HeldTasks held_tasks { HeldTasks::None };
// Whether the first round of the doc's layout flew: after the transaction, once the event loop took it in, or in its
// place, where the update had no style to change.
bool layout_flew { false };
// The fewest tasks the update held back at any time while it flew.
HeldTasks least_held_tasks { HeldTasks::None };
// Whether a task, or a step of the next rendering update that runs script, ran beside the update.
bool script_ran_beside { false };
};
// The end of a rendering update, once its last step has run.
void EventLoop::finish_rendering_update(double update_start_time)
{
auto update_end_time = HighResolutionTime::unsafe_shared_current_time();
m_rendering_scheduler_counters.update_microseconds += rendering_scheduler_duration_microseconds(update_end_time - update_start_time);
m_running_rendering_task = false;
for (auto const& page : pages_of_local_roots())
page->client().did_finish_rendering_update();
auto const& current = m_rendering_scheduler_counters;
auto const& previous = m_rendering_scheduler_counters_at_last_update;
dbgln_if(RENDERING_SCHEDULER_DEBUG,
"[RenderSched] update #{} duration={:.1f}ms gap={:.1f}ms paints={} tasks={} ({:.1f}ms) "
"[postmsg {} ({:.1f}ms), timer {} ({:.1f}ms), net {} ({:.1f}ms), dom {} ({:.1f}ms)] "
"requests={} coalesced={} during_update={}",
current.updates_run, update_end_time - update_start_time,
m_last_rendering_update_end_time > 0 ? update_start_time - m_last_rendering_update_end_time : 0.0,
current.paints - previous.paints,
current.tasks_between_updates - previous.tasks_between_updates,
static_cast<double>(current.task_microseconds_between_updates - previous.task_microseconds_between_updates) / 1000.0,
current.posted_message_tasks_between_updates - previous.posted_message_tasks_between_updates,
static_cast<double>(current.posted_message_task_microseconds_between_updates - previous.posted_message_task_microseconds_between_updates) / 1000.0,
current.timer_tasks_between_updates - previous.timer_tasks_between_updates,
static_cast<double>(current.timer_task_microseconds_between_updates - previous.timer_task_microseconds_between_updates) / 1000.0,
current.networking_tasks_between_updates - previous.networking_tasks_between_updates,
static_cast<double>(current.networking_task_microseconds_between_updates - previous.networking_task_microseconds_between_updates) / 1000.0,
current.dom_manipulation_tasks_between_updates - previous.dom_manipulation_tasks_between_updates,
static_cast<double>(current.dom_manipulation_task_microseconds_between_updates - previous.dom_manipulation_task_microseconds_between_updates) / 1000.0,
current.update_requests - previous.update_requests,
current.coalesced_update_requests - previous.coalesced_update_requests,
current.update_requests_while_rendering - previous.update_requests_while_rendering);
m_rendering_scheduler_counters_at_last_update = current;
m_last_rendering_update_end_time = update_end_time;
}
enum class StyleTakenIn : u8 {
No,
Yes,
};
enum class LayoutHeldForTesting : bool {
No,
Yes,
};
// Which tasks a rendering update whose style update flies for its last doc holds back until the whole of it has
// run, as the steps after its style and layout deliver to script what comes before any task: the intersection
// observations of every doc, whose roots and targets the script of any document may reach, rendered or not, the resize
// 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.
static HeldTasks tasks_rendering_update_holds(Vector<GC::Root<DOM::Document>> const& docs, StyleTakenIn style_taken_in, LayoutHeldForTesting layout_held_for_testing)
{
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, 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) {
auto* arena = document.layout_node_arena_if_created();
return arena && layout_held_for_testing == LayoutHeldForTesting::No && Layout::RustFFI::document_host_lanes_wait_for_the_update(arena->host());
};
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) || EventTiming::has_pending_event_timing_entries(document))
return HeldTasks::OfLastDocument;
if (lanes_wait(document))
return HeldTasks::OfLastDocument;
if (Animations::RustFFI::animations_document_timelines_hold_rendering_update_tasks(&document.document_animations(), style_taken_in == StyleTakenIn::Yes))
return HeldTasks::OfLastDocument;
if (style_taken_in == StyleTakenIn::No && document.style_computer().style_engine().css_transitions_may_observe_style_changes())
return HeldTasks::OfLastDocument;
return HeldTasks::None;
}
static void update_style_and_layout_for_rendering(DOM::Document&, u64 update_serial);
// https://html.spec.whatwg.org/multipage/webappapis.html#update-the-rendering
// NB: The documents step 3 of update the rendering keeps.
static bool is_renderable(DOM::Document const& document)
{
if (!document.is_fully_active())
return false;
// doc is render-blocked;
if (document.is_render_blocked())
return false;
// doc's visibility state is "hidden";
if (document.hidden())
return false;
// doc's rendering is suppressed for view transitions; or
if (document.rendering_suppression_for_view_transitions())
return false;
auto navigable = document.navigable();
if (!navigable)
return false;
// doc's node navigable doesn't currently have a rendering opportunity.
if (!navigable->has_a_rendering_opportunity())
return false;
return true;
}
// Takes in what the render clock's lanes did, then the queued input events. The lanes wait for the plan of the rendering
// update with the serial number `update_serial`, or tick on beside the task that handles a press where no update comes
// with it. Answers the documents whose lanes were taken in.
Vector<GC::Root<DOM::Document>> EventLoop::take_clock_lanes_and_input_events_in(Optional<u64> update_serial)
{
// AD-HOC: The render clock's lanes present frames of a document's animations and hover beside the tasks since the
// last update, at display ticks later than the opportunity this update renders for, which reached the event
// loop only after those tasks. An update after a lane's tick presented a frame renders at the time it runs,
// so nothing moves back from where the tick showed it.
// The update keeps the hover the lanes moved what the screen shows to, and hovers the newest pointer move the
// compositor told of: the move is the document's from now on, ahead of the input events that hover what is
// under the pointer as the host sees it. The lanes present nothing until the update's frame, which shows
// what the update took in, so the screen never goes back from what a lane showed.
// The transitions the lanes' hover left elements running run on from when the lanes started them, in place
// of those its steps ended, ahead of the update's own style changes.
// A lane presents frames of a fork of the document's render state, which the host's state never held: the
// update records and presents the host's frame again.
bool tick_presented = false;
Vector<GC::Root<DOM::Document>> documents_with_lanes;
for (auto& navigable : all_local_navigables()) {
auto document = navigable->active_document();
if (!document)
continue;
auto* arena = document->layout_node_arena_if_created();
if (!arena)
continue;
// A document the update does not render may still have lanes that follow the last plan it sealed: the update
// takes them in all the same, and drops their plan as it leaves the document out of docs.
if (!is_renderable(*document) && !Layout::RustFFI::document_host_may_have_lanes(arena->host()))
continue;
// A same-process iframe's lanes hear of the pointer from the lanes of its local root's document.
u64 outer_input_event = 0;
if (!navigable->is_local_root()) {
auto root_document = navigable->local_root()->active_document();
if (auto* root_arena = root_document ? root_document->layout_node_arena_if_created() : nullptr)
outer_input_event = Layout::RustFFI::document_host_newest_pointer_input_event(root_arena->host());
}
auto const lane_presented = update_serial.has_value()
? Layout::RustFFI::document_host_take_clock_lanes_in(arena->host(), *update_serial, outer_input_event)
: Layout::RustFFI::document_host_take_in_what_clock_lanes_did(arena->host(), outer_input_event);
if (lane_presented) {
tick_presented = true;
navigable->set_needs_to_record_display_list();
navigable->set_needs_repaint();
}
documents_with_lanes.append(*document);
}
if (tick_presented)
m_last_render_opportunity_time = max(m_last_render_opportunity_time, HighResolutionTime::unsafe_shared_current_time());
if (!documents_with_lanes.is_empty()) {
// Starting and cancelling transitions settles their promises, which a rendering update runs without a script:
// their reactions run once every document took in what its lanes did.
HTML::TemporaryExecutionContext execution_context { documents_with_lanes.first()->relevant_settings_object() };
for (auto& document : documents_with_lanes)
document->style_computer().adopt_lane_transitions();
}
process_input_events();
// AD-HOC: A hover a lane moved, which the update kept, owes the boundary events of the move where the update heard
// of no mouse move: they fire now, from which on script reads the hover, as they would for a mouse move.
struct HoverEventsOwed {
GC::Root<LocalNavigable> navigable;
Optional<DevicePixelPoint> pointer;
GC::Root<DOM::Node> hover_target;
};
Vector<HoverEventsOwed> hover_events_owed;
for (auto& navigable : all_local_navigables()) {
auto document = navigable->active_document();
auto* arena = document ? document->layout_node_arena_if_created() : nullptr;
bool has_pointer = false;
float pointer_x = 0;
float pointer_y = 0;
bool has_target = false;
u32 target = 0;
if (!arena || !Layout::RustFFI::document_host_take_hover_events_owed(arena->host(), &has_pointer, &pointer_x, &pointer_y, &has_target, &target))
continue;
Optional<DevicePixelPoint> pointer;
if (has_pointer)
pointer = DevicePixelPoint { static_cast<i32>(pointer_x), static_cast<i32>(pointer_y) };
// The element the lane hovered there, which the screen shows.
GC::Ptr<DOM::Node> hover_target;
if (has_target && target != 0)
hover_target = document->style_computer().node_for_lane_style_node(CSS::StyleNodeID { target });
hover_events_owed.append({ navigable, pointer, hover_target });
}
for (auto& owed : hover_events_owed) {
auto document = owed.navigable->active_document();
if (!document || !document->is_fully_active() || owed.navigable->has_been_destroyed())
continue;
// Script that ran for an earlier navigable's events may have moved the element the lane hovered elsewhere.
GC::Ptr<DOM::Node> hover_target = owed.hover_target.ptr();
if (hover_target && &hover_target->document() != document.ptr())
hover_target = nullptr;
if (owed.pointer.has_value())
owed.navigable->event_handler().update_hover_at(owed.navigable->page().device_to_css_point(*owed.pointer), hover_target);
else
owed.navigable->event_handler().handle_mouseleave();
}
// AD-HOC: A hover left stale between the tasks, as the style or layout it reads was pending, follows now. The
// update brings the style and layout up to date next, so the hover may bring them up to date first.
update_hovers_that_may_be_stale(HoverUpdatePoint::RenderingUpdateBegins);
// What the lanes reported has been taken in: the style node ids that left their nodes since name nothing of theirs.
// A document with no lanes has none that report any.
for (auto& document : documents_in_this_event_loop_matching([](auto const&) { return true; })) {
// NB: A document that never made a style computer has retired no style nodes.
auto* style_computer = document->style_computer_if_created();
if (!style_computer)
continue;
auto* arena = document->layout_node_arena_if_created();
// NB: Lanes that tick on beside a task may still name them, and report that later.
bool const lanes_stopped = update_serial.has_value() && any_of(documents_with_lanes, [&](auto const& taken_in) { return taken_in.ptr() == document.ptr(); });
if (lanes_stopped || !arena || !Layout::RustFFI::document_host_may_have_lanes(arena->host()))
style_computer->forget_style_nodes_retired_beside_lanes();
}
return documents_with_lanes;
}
// NB: Pointer moves and wheel steps wait for a rendering update, which coalesces those that come at the display's rate.
// A button or key press goes ahead of the next display tick, with the events queued before it, unless an update in
// flight holds what it would read.
void EventLoop::queue_task_to_process_input_events()
{
if (m_input_events_task_queued)
return;
m_input_events_task_queued = true;
queue_a_task(Task::Source::UserInteraction, this, nullptr, GC::create_function(GC::Heap::the(), GC::captures(*this), [](EventLoop& self) {
self.m_input_events_task_queued = false;
if (self.m_running_rendering_task || self.m_rendering_update_in_flight)
return;
// NB: No rendering update comes with this task, and script may run for long before the next one: the lanes
// tick on.
self.take_clock_lanes_and_input_events_in({});
}));
}
// https://html.spec.whatwg.org/multipage/webappapis.html#update-the-rendering
void EventLoop::update_the_rendering()
{
VERIFY(!m_running_rendering_task);
m_running_rendering_task = true;
for (auto const& page : pages_of_local_roots())
page->client().will_begin_rendering_update();
auto update_start_time = HighResolutionTime::unsafe_shared_current_time();
++m_rendering_scheduler_counters.updates_run;
note_rendering_trace_event("update"sv);
++m_rendering_update_serial;
// AD-HOC: The last rendering update may have ended as its style update flew. Its layout runs beside this
// update's steps up to step 16, which takes the rest of it in, or the rest of it runs now.
if (m_rendering_update_in_flight && !m_rendering_update_in_flight->held_for_testing && !m_rendering_update_in_flight->layout_flew
&& !let_layout_of_rendering_update_fly())
finish_rendering_update_in_flight();
auto documents_with_lanes = take_clock_lanes_and_input_events_in(m_rendering_update_serial);
// 1. Let frameTimestamp be eventLoop's last render opportunity time.
auto frame_timestamp = m_last_render_opportunity_time;
// INTEROP: A compositor timestamp may describe a display tick immediately before a newly created document's
// time origin. Keep document-relative rendering timestamps within the DOMHighResTimeStamp domain.
auto relative_frame_timestamp_for = [&](DOM::Document const& document) {
return max(0.0, HighResolutionTime::relative_high_resolution_time(frame_timestamp, relevant_global_object(document)));
};
// 2. Let docs be all fully active Document objects whose relevant agent's event loop is
// eventLoop, sorted arbitrarily except that the following conditions must be met:
// - Any Document B whose container document is A must be listed after A in the list.
// - If there are two documents A and B that both have the same non-null container document
// C, then the order of A and B in the list must match the shadow-including tree order
// of their respective navigable containers in C's node tree.
// 3. Filter non-renderable documents: Remove from docs any Document object doc for which any of the following are true:
auto docs = documents_in_this_event_loop_matching([&](auto const& document) {
return is_renderable(document);
});
// 4. Unnecessary rendering: Remove from docs any Document object doc for which all of the following are true:
// - the user agent believes that updating the rendering of doc's node navigable would have no visible effect; and
// - doc's map of animation frame callbacks is empty.
docs.remove_all_matching([](auto const& document) {
// NB: Nothing of a document whose navigable container isn't being rendered (e.g. a display: none iframe) can be
// seen. Its animations, observers and media query lists still report through the steps below, so keep a
// document that has any of them to report.
auto container = document->navigable()->container();
if (!container || container->has_layout_box())
return false;
return !document->window()->animation_frame_callback_driver().has_callbacks()
&& !document->has_associated_animations()
&& !document->has_intersection_observers()
&& !document->has_resize_observers()
&& !document->needs_media_query_list_evaluation();
});
// FIXME: 5. Remove from docs all Document objects for which the user agent believes that it's preferable to skip updating the rendering for other reasons.
// FIXME: 6. For each doc of docs, reveal doc.
// 7. For each doc of docs, flush autofocus candidates for doc if its node navigable is a top-level traversable.
for (auto& document : docs) {
auto navigable = document->navigable();
if (navigable && navigable->is_top_level_traversable())
document->flush_autofocus_candidates();
}
// 8. For each doc of docs, run the resize steps for doc. [CSSOMVIEW]
for (auto& document : docs) {
document->run_the_resize_steps();
}
// 9. For each doc of docs, run the scroll steps for doc. [CSSOMVIEW]
for (auto& document : docs) {
if (auto navigable = document->navigable()) {
navigable->process_main_thread_smooth_scrolls();
navigable->adopt_pending_async_scroll_offsets();
navigable->end_scroll_sequences_that_completed_since_the_last_frame();
}
// AD-HOC: A text control that script edited reveals its cursor now, once for all of the edits. See the note
// at FormAssociatedTextControlElement::CursorRevealAfterEdit.
if (auto* text_control = as_if<FormAssociatedTextControlElement>(document->focused_area().ptr()))
text_control->reveal_cursor_if_pending();
document->run_the_scroll_steps(frame_timestamp);
}
// 10. For each doc of docs, evaluate media queries and report changes for doc. [CSSOMVIEW]
for (auto& document : docs) {
document->evaluate_media_queries_and_report_changes();
}
// 11. For each doc of docs, update animations and send events for doc, passing in relative high resolution time given frameTimestamp and doc's relevant global object as the timestamp [WEBANIMATIONS]
for (auto& document : docs) {
document->update_animations_and_send_events(relative_frame_timestamp_for(*document));
};
// 12. For each doc of docs, run the fullscreen steps for doc. [FULLSCREEN]
for (auto& document : docs) {
document->run_fullscreen_steps();
}
// FIXME: 13. For each doc of docs, if the user agent detects that the backing storage associated with a CanvasRenderingContext2D or an OffscreenCanvasRenderingContext2D, context, has been lost, then it must run the context lost steps for each such context:
// https://wicg.github.io/video-rvfc/#video-rvfc-rendering-step
// For each fully active Document in docs, for each associated video element for that Document, run the video
// frame request callbacks passing now as the timestamp.
for (auto& document : docs)
document->run_the_video_frame_request_callbacks(relative_frame_timestamp_for(*document));
// 14. For each doc of docs, run the animation frame callbacks for doc, passing in the relative high resolution time given frameTimestamp and doc's relevant global object as the timestamp.
for (auto& document : docs) {
auto now = relative_frame_timestamp_for(*document);
run_animation_frame_callbacks(*document, now);
document->window()->animation_frame_callback_driver().run_after_callbacks(now);
}
// AD-HOC: Captures in this update's video frame and animation frame callbacks draw the frame that the video frame
// callbacks' metadata described. Later captures draw whatever frame is presented then.
for (auto& document : docs)
document->release_video_frames_held_for_callbacks();
// FIXME: 15. Let unsafeStyleAndLayoutStartTime be the unsafe shared current time.
// AD-HOC: The rest of the last rendering update runs before anything of this one's style and layout, and after
// the steps of this one that ran script.
note_script_ran_beside_rendering_update_in_flight();
finish_ended_rendering_update_in_flight();
// AD-HOC: A doc the update took the lanes of in, which was not renderable or which script left unrenderable since,
// seals no plan: its lanes present nothing more, and hold none of its tasks. The plan the last rendering
// update sealed as it finished goes before.
for (auto& document : documents_with_lanes) {
if (any_of(docs, [&](auto const& rendered) { return rendered.ptr() == document.ptr(); }))
continue;
if (auto* arena = document->layout_node_arena_if_created())
Layout::RustFFI::document_host_drop_clock_plan(arena->host(), m_rendering_update_serial);
}
// 16. For each doc of docs:
for (size_t document_index = 0; document_index < docs.size(); ++document_index) {
// AD-HOC: The style update of the last doc of docs flies beside the event loop where nothing keeps it in
// step, and the update ends its task here: it goes on with the doc's style and layout once the event
// loop takes the transaction in, between two tasks. A styled doc with no style to change lets the
// first round of its layout fly the same way. Every doc before it, its container's included, is laid
// out in step first, so that no doc's step waits for another doc's frame to land.
auto& document = *docs[document_index];
if (document_index == docs.size() - 1) {
if (auto blocker = style_flight_blocker(document); blocker == Layout::RustFFI::FfiFlightBlocker::None) {
ensure_frame_completion_registered();
auto style_taken_in = StyleTakenIn::No;
bool flies = document.let_style_update_fly(blocker);
// A doc's first style and layout, as it loads, stay in step with the event loop.
if (!flies && document.has_completed_style_update()) {
style_taken_in = StyleTakenIn::Yes;
flies = document.let_layout_fly(blocker);
}
if (flies) {
// The rendering task ends here: a rendering opportunity meanwhile queues the next one, which keeps
// its place in the queue until this update has finished.
m_running_rendering_task = false;
auto layout_held_for_testing = style_taken_in == StyleTakenIn::Yes && exchange(m_holds_next_layout_for_testing, false) ? LayoutHeldForTesting::Yes : LayoutHeldForTesting::No;
auto held_tasks = tasks_rendering_update_holds(docs, style_taken_in, layout_held_for_testing);
m_rendering_update_in_flight = make<RenderingUpdateInFlight>(move(docs), frame_timestamp, m_rendering_update_serial, exchange(m_holds_next_frame_for_testing, false), held_tasks, style_taken_in == StyleTakenIn::Yes, held_tasks);
// The next rendering opportunity is asked for now, so that the next update can begin as soon as
// this one has run, or has ended.
finish_rendering_update(update_start_time);
return;
}
}
}
update_style_and_layout_for_rendering(document, m_rendering_update_serial);
}
update_the_rendering_after_style_and_layout(docs, frame_timestamp, m_rendering_update_serial);
finish_rendering_update(update_start_time);
}
void EventLoop::update_whether_font_face_sets_are_pending_on_the_environment(Vector<GC::Root<DOM::Document>> const& docs, TakenLayout taken_layout)
{
for (auto& document : docs) {
// https://drafts.csswg.org/css-font-loading/#fontfaceset-pending-on-the-environment
// A FontFaceSet is pending on the environment if any of the following are true:
// - the document is still loading
// - the document has pending stylesheet requests
// - the document has pending layout operations which might cause the user agent to request a font, or which depend on recently-loaded fonts
TemporaryExecutionContext context(document->relevant_settings_object(), TemporaryExecutionContext::CallbacksEnabled::Yes);
document->fonts()->set_is_pending_on_the_environment(document->readiness() == DocumentReadyState::Loading
|| document->has_pending_style_sheet_requests()
|| (taken_layout == TakenLayout::UpToDate && !document->layout_is_up_to_date()));
}
}
// Step 16 of updating the rendering, for one doc of docs: its style and layout, and the resize observations they
// answer.
static void update_style_and_layout_for_rendering(DOM::Document& document, u64 update_serial)
{
// 1. Let resizeObserverDepth be 0.
size_t resize_observer_depth = 0;
// https://github.com/whatwg/html/pull/11613
// AD-HOC: Let didRunSnapshotPostLayoutStateSteps be false.
bool did_run_snapshot_post_layout_state_steps = false;
// 2. While true:
while (true) {
// 1. Recalculate styles and update layout for doc.
// NOTE: Recalculation of styles is handled by update_layout()
Layout::ForcedReadScope read { document };
document.update_layout(DOM::UpdateLayoutReason::HTMLEventLoopRenderingUpdate);
// AD-HOC: Script that ran earlier in this rendering update may have spun the event loop (e.g. with a
// synchronous XHR) and run tasks that stopped document from being actively rendered, for example
// by detaching it from its navigable after its iframe was removed. update_layout() is a no-op for
// such documents, which can leave them without a paint tree, so skip the rest of this step.
if (!document.navigable() || document.navigable()->active_document().ptr() != &document)
break;
// Clamp viewport scroll offset to valid range after layout, in case the
// scrollable overflow area has shrunk (e.g. after a viewport size change).
if (auto navigable = document.navigable()) {
navigable->clamp_viewport_scroll_offset();
// AD-HOC: A user scroll gesture that ended while layout was out of date could not select the snap
// position it ends at, so it does now.
navigable->snap_user_scroll_gestures_that_awaited_layout();
}
// https://github.com/whatwg/html/pull/11613
// AD-HOC: If didRunSnapshotPostLayoutStateSteps is false:
if (!did_run_snapshot_post_layout_state_steps) {
// 1. Run snapshot post-layout state steps for doc.
// NB: The state these steps snapshot is what scroll-state() container queries read. The content-visibility
// steps below are part of them in that change, but they stay where they are until it lands.
bool snapshotted_state_changed = document.scroll_state_query_containers().snapshot_post_layout_state(read, document, CSS::ScrollStateQueryContainers::Snapshot::AllContainers);
// 2. Set didRunSnapshotPostLayoutStateSteps to true.
did_run_snapshot_post_layout_state_steps = true;
// 3. If any snapshotted state changed, then continue.
if (snapshotted_state_changed)
continue;
}
// AD-HOC: Set didRunSnapshotPostLayoutStateSteps to false.
did_run_snapshot_post_layout_state_steps = false;
// 2. Let hadInitialVisibleContentVisibilityDetermination be false.
bool had_initial_visible_content_visibility_determination = false;
// 3. For each element element with 'auto' used value of 'content-visibility':
auto* document_element = document.document_element();
if (document_element) {
for (auto box_slot : document.paint_state().boxes_with_auto_content_visibility()) {
auto* layout_node = Painting::layout_node_for_committed_slot(read, document.layout_node_arena(), box_slot);
if (!layout_node)
continue;
auto* element = as_if<DOM::Element>(layout_node->dom_node());
if (!element)
continue;
// 1. Let checkForInitialDetermination be true if element's proximity to the viewport is not determined and it is not relevant to the user. Otherwise, let checkForInitialDetermination be false.
bool check_for_initial_determination = element->proximity_to_the_viewport() == Web::DOM::ProximityToTheViewport::NotDetermined && !element->is_relevant_to_the_user();
// 2. Determine proximity to the viewport for element.
element->determine_proximity_to_the_viewport(read);
// 3. If checkForInitialDetermination is true and element is now relevant to the user, then set hadInitialVisibleContentVisibilityDetermination to true.
if (check_for_initial_determination && element->is_relevant_to_the_user()) {
had_initial_visible_content_visibility_determination = true;
}
}
}
// 4. If hadInitialVisibleContentVisibilityDetermination is true, then continue.
if (had_initial_visible_content_visibility_determination)
continue;
// 5. Gather active resize observations at depth resizeObserverDepth for doc.
document.gather_active_observations_at_depth(resize_observer_depth, update_serial);
// 6. If doc has active resize observations:
if (document.has_active_resize_observations()) {
// 1. Set resizeObserverDepth to the result of broadcasting active resize observations given doc.
resize_observer_depth = document.broadcast_active_resize_observations(read);
// 2. Continue.
continue;
}
// https://drafts.csswg.org/css-anchor-position-1/#last-successful-position-option
// At the time that ResizeObserver events are determined and delivered, the box must record the last
// successful position option.
if (auto* layout_node_arena = document.layout_node_arena_if_created())
Layout::RustFFI::render_state_record_last_successful_position_options(layout_node_arena->host());
// 7. Otherwise, break.
break;
}
// 3. If doc has skipped resize observations, then deliver resize loop error given doc.
if (document.has_skipped_resize_observations()) {
// FIXME: Deliver resize loop error.
}
// https://drafts.csswg.org/scroll-animations-1/#event-loop
// During step 7.14.1 of the HTML Processing Model, any created scroll progress timelines or view progress
// timelines are collected into a stale timelines set. After step 7.14 if any timelines' named timeline ranges
// have changed, these timelines are added to the stale timelines set. If there are any stale timelines they now
// update their current time and associated ranges, the set of stale timelines is cleared and we run an additional
// step to recalculate styles and update layout.
// AD-HOC: This was step 7.14.1 at the time the CSS web-animations spec was written, but it has since been
// moved, see https://github.com/w3c/csswg-drafts/issues/12120
bool requires_style_and_layout_update = false;
TemporaryExecutionContext context { document.relevant_settings_object() };
// NB: The animations that name their timeline find the timelines that style update and layout declared. An
// animation that moved to another timeline takes its time from it in this frame, so the timelines update their
// current time and style and layout run again.
bool animations_changed_timeline = false;
if (document.has_named_timelines())
animations_changed_timeline = document.named_timelines().update_tracked_animations();
if (animations_changed_timeline)
requires_style_and_layout_update = true;
{
// NB: The animations the timelines update stay alive through it, which can run script.
GC::RootVector<Animations::AnimationRef> updated_animations;
Animations::RustFFI::FfiTimelineAnimationsHost host {
.context = &updated_animations,
.keep_alive = Animations::keep_animation_alive_in,
.perform = Animations::web_animation_perform_host_effect,
};
// https://drafts.csswg.org/scroll-animations-1/#stale-timelines
auto update = animations_changed_timeline ? Animations::RustFFI::FfiTimelinesUpdate::ScrollTimelines : Animations::RustFFI::FfiTimelinesUpdate::StaleScrollTimelines;
if (Animations::RustFFI::animations_document_update_timelines(&document.document_animations(), update, 0, &host))
requires_style_and_layout_update = true;
}
if (requires_style_and_layout_update)
document.update_layout(DOM::UpdateLayoutReason::HTMLEventLoopRenderingUpdate);
}
// Steps 17 to 23 of updating the rendering, once step 16 has laid out every doc of docs.
void EventLoop::update_the_rendering_after_style_and_layout(Vector<GC::Root<DOM::Document>> const& docs, double frame_timestamp, u64 update_serial, TakenLayout taken_layout, RenderingUpdateInFlight const* update_in_flight)
{
auto relative_frame_timestamp_for = [&](DOM::Document const& document) {
return max(0.0, HighResolutionTime::relative_high_resolution_time(frame_timestamp, relevant_global_object(document)));
};
// AD-HOC: The CSS animations and transitions the style updates of step 16 began render their first frame in this
// update, which makes them ready (https://drafts.csswg.org/web-animations-1/#ready): their pending play
// tasks run at its time, and the layout of step 19 samples them from there. Left pending until the next
// update, they would not run in the tasks before it, beside which the render clock ticks a document's
// running animations. What the compositor may run keeps the start it is handed until then.
for (auto& document : docs) {
// NB: A play task resolves the animation's ready promise.
TemporaryExecutionContext execution_context { document->relevant_settings_object() };
// NB: Running a play task can run script, which the animations stay alive through.
GC::RootVector<Animations::AnimationRef> pending_css_animations;
Animations::RustFFI::animations_document_run_pending_css_play_tasks(&document->document_animations(), &pending_css_animations, Animations::keep_animation_alive_in, Animations::web_animation_perform_host_effect);
}
// 17. For each doc of docs, if the focused area of doc is not a focusable area, then run the focusing steps for doc's viewport, and set doc's relevant global object's navigation API's focus changed during ongoing navigation to false.
for (auto& document : docs) {
if (!document->is_fully_active())
continue;
// AD-HOC: Script that ran beside a rendering update in flight comes after the whole of it. The update holds the
// tasks of a doc whose focused area is not its viewport, so that script found the viewport focused, which
// needs no fixup: the next rendering update fixes up what the script focused.
if (update_in_flight && update_in_flight->ran_script_of_beside(*document)) {
if (focus_fixup_rule_applies(*document))
document->page().client().request_frame();
continue;
}
run_focus_fixup_rule(*document);
}
// 18. For each doc of docs, perform pending transition operations for doc. [CSSVIEWTRANSITIONS]
for (auto& document : docs) {
document->perform_pending_transition_operations();
}
// 19. For each doc of docs, run the update intersection observations steps for doc, passing in the relative high resolution time given now and doc's relevant global object as the timestamp. [INTERSECTIONOBSERVER]
for (auto& document : docs) {
// NB: An update whose layout flew had nothing to observe as it ended: what was observed since is the next update's.
if (taken_layout == TakenLayout::AsItFlew)
break;
// AD-HOC: Script that ran earlier in this rendering update may have detached document from its navigable, as
// in step 16. Its layout and paint state stay behind, but nothing is rendered for it anymore.
if (!document->navigable() || document->navigable()->active_document().ptr() != document.ptr())
continue;
// NB: Layout may have been invalidated by previous steps (e.g. view transitions at step 18).
// Re-run layout here since intersection observations need up-to-date geometry.
document->update_layout(DOM::UpdateLayoutReason::HTMLEventLoopRenderingUpdate);
auto now = relative_frame_timestamp_for(*document);
document->run_the_update_intersection_observations_steps(now);
// AD-HOC: The hover and the cursor follow a style the update applied to what the pointer is over.
document->navigable()->event_handler().update_hover_after_rendering_update();
}
// AD-HOC: Whether a video sink is ticked depends on whether the element would be painted, which is only known once
// layout is settled, so it is decided here rather than at the points that invalidate it. A page's media
// elements live in any of its documents, so this waits until step 19 has laid out every one of them: a task
// that ran beside a later document's frame in flight may have left an earlier document's layout stale.
GC::RootVector<GC::Ref<Page>, 1> synced_pages;
for (auto& document : docs) {
if (!document->navigable() || document->navigable()->active_document().ptr() != document.ptr())
continue;
if (synced_pages.contains_slow(GC::Ref { document->page() }))
continue;
synced_pages.append(document->page());
document->page().sync_media_element_video_sink_ticking(docs);
}
// FIXME: 20. For each doc of docs, record rendering time for doc given unsafeStyleAndLayoutStartTime.
// 21. For each doc of docs, mark paint timing for doc.
for (auto& document : docs) {
// AD-HOC: Script that ran earlier in this rendering update may have detached document from its navigable, as
// in step 16. Nothing is rendered for it anymore.
if (!document->navigable() || document->navigable()->active_document().ptr() != document.ptr())
continue;
PaintTiming::mark_paint_timing(*document);
}
// https://w3c.github.io/event-timing/#sec-modifications-HTML
// For each fully active Document in docs, invoke the algorithm to dispatch pending Event Timing entries for that
// Document.
for (auto& document : docs) {
if (document->is_fully_active())
EventTiming::dispatch_pending_event_timing_entries(*document);
}
// AD-HOC: Flush dirty canvas contexts in documents' pages after callbacks
// have had a chance to update them, and before painting snapshots the frame.
for (auto& document : docs)
document->page().prepare_canvas_contexts_for_compositing();
// 22. For each doc of docs, update the rendering or user interface of doc and its node navigable to reflect the current state.
for (auto& doc : docs.in_reverse()) {
auto navigable = doc->navigable();
// AD-HOC: Script that ran earlier in this rendering update may have spun the event loop and run tasks that
// detached doc from its navigable (e.g. after its iframe was removed).
auto lay_out_first = taken_layout == TakenLayout::UpToDate ? LocalNavigable::LayOutFirst::Yes : LocalNavigable::LayOutFirst::No;
if (Core::StatsLog::the() && navigable && !navigable->needs_repaint())
did_update_rendering(*doc);
if (!navigable || !navigable->paint_next_frame_if_needed(DOM::UpdateLayoutReason::HTMLEventLoopRenderingUpdate, lay_out_first))
continue;
++m_rendering_scheduler_counters.paints;
note_rendering_trace_event("paint"sv);
if (navigable->is_local_root())
navigable->page().process_screenshot_requests();
}
// AD-HOC: Any scroll or layout of a document between a navigable container and its local root moves the rect the
// UI process routes input over the container's content navigable by. Report the rects of the containers
// in docs, whose layout is up to date along with that of every document above them.
for (auto* container : NavigableContainer::all_instances()) {
if (any_of(docs, [&](auto const& document) { return document.ptr() == &container->document(); })) {
// The container's box is the rendering update's own read of its document's render state.
Layout::ForcedReadScope read { container->document() };
container->report_content_navigable_viewport_rect(read);
}
}
// 23. For each doc of docs, process top layer removals given doc.
for (auto& document : docs) {
document->process_top_layer_removals();
}
// NB: Where the update's layout flew, this runs once the docs no longer read as that layout left them: a FontFaceSet
// that stops being pending on the environment resolves its ready promise, whose reactions run script that may
// write to the docs and read their layout.
if (taken_layout == TakenLayout::UpToDate)
update_whether_font_face_sets_are_pending_on_the_environment(docs, taken_layout);
// AD-HOC: The lane of the frame the update presented may tick the document's running animations beside the tasks
// after it, until the next of their events, which the main thread sends, and hover what is under the
// pointer. Only a rendering update leaves a plan for that, and each one leaves every document it renders its
// own, or none, in place of the last.
bool const may_plan = !m_running_synchronous_rendering_update && m_spin_depth == 0 && taken_layout == TakenLayout::UpToDate;
for (auto& document : docs) {
auto* navigable = as_if<LocalNavigable>(document->navigable().ptr());
// A same-process iframe's document presents into a compositor context of its own, whose lane ticks its
// animations and hovers what the lane of the document around it hands it of the pointer.
bool const plans = may_plan && navigable && (navigable->is_local_root() || navigable->has_compositor_context()) && navigable->active_document().ptr() == document.ptr();
if (!seal_clock_plan(*document, plans, update_serial))
continue;
navigable->arm_clock_lane(LocalNavigable::TickNow::No);
if (!m_navigables_with_clock_lanes.contains_slow(GC::Ref { *navigable }))
m_navigables_with_clock_lanes.append(*navigable);
}
}
Layout::RustFFI::FfiFlightBlocker EventLoop::style_flight_blocker(DOM::Document& document) const
{
using Blocker = Layout::RustFFI::FfiFlightBlocker;
// A rendering update inside a nested event loop finishes before the task that spins it goes on.
if (m_running_synchronous_rendering_update || m_spin_depth > 0)
return Blocker::NotInRenderingUpdate;
if (document.has_active_view_transition())
return Blocker::ViewTransition;
if (document.scroll_state_query_containers().has_containers())
return Blocker::ScrollStateContainer;
if (document.has_boxes_with_auto_content_visibility())
return Blocker::ContentVisibilityAuto;
if (Animations::RustFFI::animations_document_animations_query(&document.document_animations(), Animations::RustFFI::DocumentAnimationsQuery::AnyScrollTimeline))
return Blocker::ScrollTimeline;
// The animations that name their timeline look it up again after style and layout.
if (document.has_named_timelines())
return Blocker::ScrollTimeline;
return Blocker::None;
}
// The sizes the windows took as the update flew become the sizes of their traversables now that it ended.
static void take_viewport_sizes_held_for_rendering_update_in_flight()
{
for (auto const& page : pages_of_local_roots()) {
if (page->has_local_traversable())
page->local_traversable()->take_viewport_size_held_for_rendering_update_in_flight();
}
}
void EventLoop::resume_rendering_update_in_flight()
{
auto update = m_rendering_update_in_flight.release_nonnull();
{
TemporaryChange running { m_running_rendering_task, true };
// The style and layout of the doc whose transaction flew take it in, waiting for it to land.
update_style_and_layout_for_rendering(update->document(), update->serial);
update_the_rendering_after_style_and_layout(update->docs, update->frame_timestamp, update->serial, TakenLayout::UpToDate, update.ptr());
}
// What the steps asked for, an animation frame of a promise they resolved for one, asks for the next rendering
// opportunity now, unless a rendering update runs, whose end asks for it.
if (!m_running_rendering_task) {
for (auto const& page : pages_of_local_roots())
page->client().did_finish_rendering_update();
}
take_viewport_sizes_held_for_rendering_update_in_flight();
}
// The rest of the last rendering update, which ended as its style update flew, runs before anything of the next
// one's style and layout. Where its layout flew as well, its steps go on from that layout, as it left the document: what
// the next update wrote since is that update's, which the render state takes in behind them.
void EventLoop::finish_ended_rendering_update_in_flight()
{
if (m_rendering_update_in_flight && m_rendering_update_in_flight->ended() && m_rendering_update_in_flight->layout_flew
&& finish_ended_rendering_update_as_its_layout_flew())
return;
finish_rendering_update_in_flight();
}
// Answers whether the layout that flew laid the doc out, as the update's steps then went on from it.
bool EventLoop::finish_ended_rendering_update_as_its_layout_flew()
{
auto& document = m_rendering_update_in_flight->document();
OwnPtr<RenderingUpdateInFlight> update;
{
DOM::Document::LayoutAsItFlew layout_as_it_flew { document };
if (!document.take_flown_layout_in())
return false;
update = m_rendering_update_in_flight.release_nonnull();
if (auto navigable = document.navigable())
navigable->clamp_viewport_scroll_offset();
update_the_rendering_after_style_and_layout(update->docs, update->frame_timestamp, update->serial, TakenLayout::AsItFlew, update.ptr());
}
update_whether_font_face_sets_are_pending_on_the_environment(update->docs, TakenLayout::AsItFlew);
take_viewport_sizes_held_for_rendering_update_in_flight();
return true;
}
void EventLoop::note_script_ran_beside_rendering_update_in_flight()
{
if (m_rendering_update_in_flight)
m_rendering_update_in_flight->script_ran_beside = true;
}
u64 EventLoop::first_rendering_update_to_gather_resize_observations_of(DOM::Document const& document) const
{
if (m_rendering_update_in_flight && &m_rendering_update_in_flight->document() == &document)
return m_rendering_update_in_flight->serial + 1;
return 0;
}
void EventLoop::finish_rendering_update_in_flight()
{
// The steps after the style update take it in, waiting for it to land.
if (m_rendering_update_in_flight)
resume_rendering_update_in_flight();
}
void EventLoop::did_commit_frame(LocalNavigable& navigable, CommittedFrameRecords records)
{
// A test holds a recording itself, on the Paint thread; it only spends the hold the next style flight would take.
if (records == CommittedFrameRecords::Yes)
m_holds_next_frame_for_testing = false;
m_navigables_with_frames_in_flight.append(navigable);
}
void EventLoop::did_take_frame_in(LocalNavigable& navigable)
{
auto removed = m_navigables_with_frames_in_flight.remove_first_matching([&](auto const& entry) { return entry.ptr() == &navigable; });
VERIFY(removed);
}
void EventLoop::take_committed_frames_in()
{
while (!m_navigables_with_frames_in_flight.is_empty())
(void)m_navigables_with_frames_in_flight.last()->take_recording_in_flight_in(LocalNavigable::TakeIn::Wait);
}
void EventLoop::ensure_frame_completion_registered()
{
// Every recording that finishes beside the event loop posts to it, so that an idle event loop wakes to take it in.
// This is set up before the first recording is submitted, so that not even the first finish goes unseen, and not
// in the constructor, which may run before the thread has a Core event loop.
if (exchange(m_frame_completion_registered, true))
return;
Layout::RustFFI::stage_thread_set_flight_finished([] { FrameCompletion::the().post(); });
FrameCompletion::the().register_event_loop([] { main_thread_event_loop().schedule(); });
}
bool EventLoop::has_frame_in_flight() const
{
if (m_rendering_update_in_flight && m_rendering_update_in_flight->document().style_computer().style_engine().render_document().frame_flies())
return true;
return !m_navigables_with_frames_in_flight.is_empty();
}
bool EventLoop::lays_out_rendering_update_in_flight() const
{
return m_rendering_update_in_flight && m_rendering_update_in_flight->layout_flew;
}
// AD-HOC: A rendering update of a document that is still loading lays out content that is about to change, and a first
// layout can take long. The event loop may choose its task queues freely, so the tasks that take the document
// to DOMContentLoaded go first, for a short while. Without this, the first layout of a large document runs
// between the last of its bytes and the steps after them, as the rendering task is queued first.
bool EventLoop::rendering_task_yields_to_document_loading() const
{
static constexpr auto maximum_time_to_yield = AK::Duration::from_milliseconds(4);
if (m_spin_depth > 0 || MonotonicTime::now() - m_rendering_task_queued_time >= maximum_time_to_yield)
return false;
return any_of(m_documents, [](auto const& document) {
return document
&& document->readiness() != DocumentReadyState::Complete
&& document->load_timing_info().dom_content_loaded_event_start_time == 0
&& !document->has_layout_box()
&& !document->is_initial_about_blank()
&& document->is_fully_active();
});
}
bool EventLoop::holds_rendering_opportunity() const
{
return m_rendering_update_in_flight && !m_rendering_update_in_flight->ended();
}
bool EventLoop::holds_tasks_of(DOM::Document const* document) const
{
// A test that holds the update in flight runs the document's tasks beside it.
if (!m_rendering_update_in_flight || m_rendering_update_in_flight->held_for_testing)
return false;
return rendering_update_in_flight_holds_tasks_of(document);
}
bool EventLoop::rendering_update_in_flight_holds_tasks_of_for_testing(DOM::Document const& document) const
{
return m_rendering_update_in_flight && rendering_update_in_flight_holds_tasks_of(&document);
}
bool EventLoop::rendering_update_in_flight_holds_tasks_of(DOM::Document const* document) const
{
auto const& update = *m_rendering_update_in_flight;
switch (update.held_tasks) {
case HeldTasks::None:
return false;
case HeldTasks::OfLastDocument:
return !document || document == &update.document();
case HeldTasks::All:
return true;
}
VERIFY_NOT_REACHED();
}
// The layout of the doc whose style update flew runs beside the event loop as well, once the event loop has taken
// the transaction in. Answers whether it flies.
bool EventLoop::let_layout_of_rendering_update_fly()
{
auto& update = *m_rendering_update_in_flight;
VERIFY(!update.layout_flew);
auto& document = update.document();
if (!document.let_layout_fly(style_flight_blocker(document)))
return false;
update.layout_flew = true;
update.held_tasks = tasks_rendering_update_holds(update.docs, StyleTakenIn::Yes, exchange(m_holds_next_layout_for_testing, false) ? LayoutHeldForTesting::Yes : LayoutHeldForTesting::No);
update.least_held_tasks = min(update.least_held_tasks, update.held_tasks);
return true;
}
void EventLoop::note_hover_may_be_stale(LocalNavigable& navigable)
{
if (!queue_hover_that_may_be_stale(navigable))
return;
// An idle loop comes between two tasks only as the next task comes: this makes it come now.
schedule();
}
bool EventLoop::queue_hover_that_may_be_stale(LocalNavigable& navigable)
{
if (any_of(m_navigables_with_hover_that_may_be_stale, [&](auto const& entry) { return entry.ptr() == &navigable; }))
return false;
m_navigables_with_hover_that_may_be_stale.append(navigable);
return true;
}
void EventLoop::update_hovers_that_may_be_stale(HoverUpdatePoint hover_update_point)
{
// What the hover dispatches may note a hover as stale again, which waits for the next point.
auto navigables = move(m_navigables_with_hover_that_may_be_stale);
for (auto& navigable : navigables) {
if (navigable->has_been_destroyed())
continue;
// A hover that waits for a frame, or for a rendering update, comes here again as it lands or begins.
if (navigable->event_handler().update_hover_where_the_pointer_rests(hover_update_point))
queue_hover_that_may_be_stale(*navigable);
}
}
void EventLoop::take_finished_frames_in()
{
// A rendering update whose style update, then layout, has landed goes on; the tasks before that run beside it,
// but for those it holds back, which wait. None is in flight in a nested event loop, which finishes it as it begins,
// and a paused event loop does not come here.
if (m_rendering_update_in_flight && !m_rendering_update_in_flight->held_for_testing
&& !CSS::StyleEngineFFI::style_engine_style_update_flies(m_rendering_update_in_flight->document().style_computer().style_engine().host())
&& (m_rendering_update_in_flight->layout_flew || !let_layout_of_rendering_update_fly()))
resume_rendering_update_in_flight();
// A frame that was taken in leaves the list as it is, which moves only the entries after it.
for (size_t index = m_navigables_with_frames_in_flight.size(); index-- > 0;)
(void)m_navigables_with_frames_in_flight[index]->take_recording_in_flight_in(LocalNavigable::TakeIn::IfFinished);
// The frames taken in are the ones the hover waits for.
update_hovers_that_may_be_stale(HoverUpdatePoint::BetweenTasks);
// NB: This runs between tasks, so no end of a task takes what the style drains above queued, such as the reaction
// to a promise an animation resolved, and no task may follow for a long time.
if (!m_microtask_queue.is_empty() && vm().execution_context_stack().is_empty())
perform_a_microtask_checkpoint();
}
// AD-HOC: The render clock's lanes follow the frames a document presents: they hover what is under the pointer whenever
// the pointer moves, and sample the document's running animations while a task runs. An idle event loop's
// rendering updates run the animations.
void EventLoop::note_clock_lanes(ClockAnimations animations)
{
m_navigables_with_clock_lanes.remove_all_matching([&](auto const& navigable) {
return !navigable->note_clock_lane(animations);
});
}
void EventLoop::release_held_frames_for_testing()
{
m_holds_next_frame_for_testing = false;
m_holds_next_layout_for_testing = false;
if (m_rendering_update_in_flight)
m_rendering_update_in_flight->held_for_testing = false;
schedule();
}
void run_when_event_loop_reaches_step_1(GC::Ref<GC::Function<void()>> steps)
{
auto& event_loop = current_event_loop();
event_loop.run_upon_reaching_step_1(steps);
}
// https://html.spec.whatwg.org/multipage/webappapis.html#queue-a-task
TaskID queue_a_task(HTML::Task::Source source, GC::Ptr<EventLoop> event_loop, GC::Ptr<DOM::Document> document, GC::Ref<GC::Function<void()>> steps, Task::Priority priority, GC::Ptr<GC::Function<void()>> discard_steps)
{
// 1. If event loop was not given, set event loop to the implied event loop.
if (!event_loop)
event_loop = current_event_loop();
// FIXME: 2. If document was not given, set document to the implied document.
// 3. Let task be a new task.
// 4. Set task's steps to steps.
// 5. Set task's source to source.
// 6. Set task's document to the document.
// 7. Set task's script evaluation environment settings object set to an empty set.
auto task = HTML::Task::create(source, document, steps, priority, discard_steps);
// 8. Let queue be the task queue to which source is associated on event loop.
// 9. Append task to queue.
if (source == HTML::Task::Source::Microtask)
event_loop->enqueue_microtask(task);
else
event_loop->task_queue().add(task);
return task->id();
}
// https://html.spec.whatwg.org/multipage/webappapis.html#queue-a-global-task
TaskID queue_global_task(HTML::Task::Source source, JS::Object& global_object, GC::Ref<GC::Function<void()>> steps, Task::Priority priority, GC::Ptr<GC::Function<void()>> discard_steps)
{
// 1. Let event loop be global's relevant agent's event loop.
auto& event_loop = relevant_agent(global_object).event_loop;
// 2. Let document be global's associated Document, if global is a Window object; otherwise null.
DOM::Document* document { nullptr };
if (auto* window_object = window_from_global_object(global_object))
document = &window_object->associated_document();
// 3. Queue a task given source, event loop, document, and steps.
return queue_a_task(source, *event_loop, document, steps, priority, discard_steps);
}
// https://html.spec.whatwg.org/multipage/webappapis.html#queue-a-microtask
void queue_a_microtask(GC::Ptr<DOM::Document const> document, GC::Ref<GC::Function<void()>> steps)
{
// 1. If event loop was not given, set event loop to the implied event loop.
auto& event_loop = HTML::current_event_loop();
// FIXME: 2. If document was not given, set document to the implied document.
// 3. Let microtask be a new task.
// 4. Set microtask's steps to steps.
// 5. Set microtask's source to the microtask task source.
// 6. Set microtask's document to document.
auto microtask = HTML::Task::create(HTML::Task::Source::Microtask, document, steps);
// FIXME: 7. Set microtask's script evaluation environment settings object set to an empty set.
// 8. Enqueue microtask on event loop's microtask queue.
event_loop.enqueue_microtask(microtask);
}
void perform_a_microtask_checkpoint()
{
current_event_loop().perform_a_microtask_checkpoint();
}
// https://html.spec.whatwg.org/multipage/webappapis.html#perform-a-microtask-checkpoint
void EventLoop::perform_a_microtask_checkpoint()
{
if (execution_paused())
return;
// NB: The checkpoint at the end of the task, or of taking finished frames in between tasks, takes what is queued
// meanwhile.
if (m_microtask_checkpoint_deferrals > 0)
return;
// 1. If the event loop's performing a microtask checkpoint is true, then return.
if (m_performing_a_microtask_checkpoint)
return;
// NOTE: This assertion is per requirement 9.5 of the ECMA-262 spec, see: https://tc39.es/ecma262/#sec-jobs
// > At some future point in time, when there is no running context in the agent for which the job is scheduled and that agent's execution context stack is empty...
VERIFY(vm().execution_context_stack().is_empty());
VERIFY(!vm().has_running_execution_context());
// 2. Set the event loop's performing a microtask checkpoint to true.
m_performing_a_microtask_checkpoint = true;
// 3. While the event loop's microtask queue is not empty:
// AD-HOC: The script these would run was aborted, and nothing in the agent runs anymore.
while (!m_microtask_queue.is_empty() && !vm().interrupt_requested()) {
// 1. Let oldestMicrotask be the result of dequeuing from the event loop's microtask queue.
auto oldest_microtask = m_microtask_queue.dequeue();
// 2. Set the event loop's currently running task to oldestMicrotask.
m_currently_running_task = oldest_microtask;
// AD-HOC: The spec gives a microtask no timer nesting level, so a timer it schedules starts over at 0 — even
// in the checkpoint that ends a deeply-nested timer callback. A page that re-arms a 0ms timer from a
// promise reaction then never reaches the 4ms clamp, and spins. Instead, a microtask carries the
// nesting level of the task whose checkpoint runs it, just as the timer callback itself would have.
oldest_microtask->set_timer_nesting_level(m_timer_nesting_level_for_microtasks);
// 3. Run oldestMicrotask.
oldest_microtask->execute();
// 4. Set the event loop's currently running task back to null.
m_currently_running_task = nullptr;
}
// 4. For each environment settings object settingsObject whose responsible event loop is this event loop, notify about rejected promises given settingsObject's global object.
// OPTIMIZATION: This runs for every callback, and only a promise rejected since the last time gives it anything to do.
if (exchange(m_has_rejected_promises_to_notify_about, false)) {
GC::RootVector<GC::Ref<EnvironmentSettingsObject>> environments;
for (auto const& entry : m_related_environment_settings_objects)
environments.append(*entry.value);
for (auto& environment_settings_object : environments) {
auto& global_object = environment_settings_object->global_object();
if (auto* worklet_global_scope = Bindings::impl_from<WorkletGlobalScope>(&global_object)) {
worklet_global_scope->notify_about_rejected_promises({});
continue;
}
relevant_window_or_worker_global_scope(global_object).notify_about_rejected_promises({});
}
}
// 5. Cleanup Indexed Database transactions.
IndexedDB::cleanup_indexed_database_transactions(*this);
// 6. Perform ClearKeptObjects().
vm().finish_execution_generation();
// 7. Set the event loop's performing a microtask checkpoint to false.
m_performing_a_microtask_checkpoint = false;
if (Core::StatsLog::the())
flush_rendering_pending();
// FIXME: 8. Record timing info for microtask checkpoint.
}
Vector<GC::Root<DOM::Document>> EventLoop::documents_in_this_event_loop_matching(Function<bool(DOM::Document&)> callback) const
{
ensure_documents_sorted();
Vector<GC::Root<DOM::Document>> documents;
for (auto& document : m_documents) {
VERIFY(document);
if (document->is_decoded_svg())
continue;
if (!callback(*document))
continue;
documents.append(GC::make_root(*document));
}
return documents;
}
void EventLoop::register_document(Badge<DOM::Document>, DOM::Document& document)
{
m_documents.append(&document);
m_documents_sort_dirty = true;
}
void EventLoop::add_stats(JsonObject& values) const
{
// NB: Read container sizes from the existing document registry, without sorting it or walking any trees.
size_t available_images = 0;
size_t shared_image_requests = 0;
size_t style_sheets = 0;
size_t font_faces = 0;
size_t destroyed_documents = 0;
for (auto const& document : m_documents) {
if (!document)
continue;
// NB: Unless script still holds its objects, a destroyed document that survives a collection is leaked.
if (document->has_been_destroyed())
++destroyed_documents;
available_images += document->list_of_available_images().size();
shared_image_requests += document->shared_resource_requests().size();
style_sheets += document->style_scope().style_sheets().size();
font_faces += document->fonts()->set_size();
}
values.set("dom.documents"sv, m_documents.size());
values.set("dom.destroyed_documents"sv, destroyed_documents);
values.set("images.available_entries"sv, available_images);
values.set("images.shared_requests"sv, shared_image_requests);
values.set("css.document_style_sheet_entries"sv, style_sheets);
values.set("fonts.face_entries"sv, font_faces);
values.set("rendering.updates"sv, m_rendering_scheduler_counters.updates_run);
values.set("rendering.paints"sv, m_rendering_scheduler_counters.paints);
values.set("rendering.update_duration_us"sv, m_rendering_scheduler_counters.update_microseconds);
}
void EventLoop::unregister_document(Badge<DOM::Document>, DOM::Document& document)
{
forget_rendering_stats(document);
// This runs from the document's finalizer, so the GC may already have cleared our weak pointer to it, as well as to
// any other document dying in the same collection.
m_documents.remove_all_matching([&](auto const& entry) { return !entry || entry.ptr().ptr() == &document; });
}
void EventLoop::document_navigable_did_change(Badge<DOM::Document>)
{
m_documents_sort_dirty = true;
}
void EventLoop::ensure_documents_sorted() const
{
// https://html.spec.whatwg.org/multipage/webappapis.html#update-the-rendering step 3.2:
// - Any Document B whose container document is A must be listed after A in the list.
// - If there are two documents A and B that both have the same non-null container document
// C, then the order of A and B in the list must match the shadow-including tree order
// of their respective navigable containers in C's node tree.
if (!m_documents_sort_dirty)
return;
m_documents_sort_dirty = false;
GC::RootHashMap<DOM::Document*, size_t> doc_to_index;
doc_to_index.ensure_capacity(m_documents.size());
for (size_t i = 0; i < m_documents.size(); ++i)
doc_to_index.set(m_documents[i].ptr().ptr(), i);
Vector<bool> visited;
visited.resize(m_documents.size());
Vector<GC::Weak<DOM::Document>> sorted;
sorted.ensure_capacity(m_documents.size());
auto visit = [&](auto& self, size_t idx) -> void {
if (visited[idx])
return;
visited[idx] = true;
if (auto navigable = m_documents[idx]->navigable()) {
if (auto container_doc = navigable->container_document()) {
if (auto container_idx = doc_to_index.get(container_doc.ptr()); container_idx.has_value())
self(self, *container_idx);
}
}
sorted.append(m_documents[idx]);
};
for (size_t i = 0; i < m_documents.size(); ++i)
visit(visit, i);
m_documents = move(sorted);
}
void EventLoop::push_onto_backup_incumbent_realm_stack(GC::Ref<EnvironmentSettingsObject> environment_settings_object)
{
m_backup_incumbent_realm_stack.append(environment_settings_object);
}
void EventLoop::pop_backup_incumbent_realm_stack()
{
m_backup_incumbent_realm_stack.take_last();
}
EnvironmentSettingsObject& EventLoop::top_of_backup_incumbent_realm_stack()
{
return m_backup_incumbent_realm_stack.last();
}
u32 EventLoop::skip_when_determining_incumbent_counter(JS::ExecutionContext const& execution_context) const
{
for (auto const& counter : m_skip_when_determining_incumbent_counters) {
if (counter.execution_context == &execution_context)
return counter.count;
}
return 0;
}
void EventLoop::increment_skip_when_determining_incumbent_counter(JS::ExecutionContext const& execution_context)
{
for (auto& counter : m_skip_when_determining_incumbent_counters) {
if (counter.execution_context == &execution_context) {
++counter.count;
return;
}
}
m_skip_when_determining_incumbent_counters.append({ &execution_context, 1 });
}
void EventLoop::decrement_skip_when_determining_incumbent_counter(JS::ExecutionContext const& execution_context)
{
auto index = m_skip_when_determining_incumbent_counters.find_first_index_if([&](auto const& counter) {
return counter.execution_context == &execution_context;
});
VERIFY(index.has_value());
auto& counter = m_skip_when_determining_incumbent_counters[*index];
VERIFY(counter.count > 0);
if (--counter.count == 0)
m_skip_when_determining_incumbent_counters.remove(*index);
}
void EventLoop::register_environment_settings_object(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject& environment_settings_object)
{
auto result = m_related_environment_settings_objects.set(environment_settings_object.address(), &environment_settings_object);
VERIFY(result == HashSetResult::InsertedNewEntry);
}
void EventLoop::unregister_environment_settings_object(Badge<EnvironmentSettingsObject>, EnvironmentSettingsObject& environment_settings_object)
{
bool did_remove = m_related_environment_settings_objects.remove(environment_settings_object.address());
VERIFY(did_remove);
}
GC::Ptr<EnvironmentSettingsObject> EventLoop::environment_settings_object_at(EnvironmentSettingsObjectAddress address) const
{
return m_related_environment_settings_objects.get(address).value_or(nullptr);
}
GC::Ptr<EnvironmentSettingsObject> EventLoop::environment_settings_object_with_id(EnvironmentId const& id) const
{
for (auto const& entry : m_related_environment_settings_objects) {
if (entry.value->id == id)
return entry.value;
}
return nullptr;
}
// https://html.spec.whatwg.org/multipage/webappapis.html#same-loop-windows
Vector<GC::Root<HTML::Window>> EventLoop::same_loop_windows() const
{
Vector<GC::Root<HTML::Window>> windows;
for (auto& document : documents_in_this_event_loop_matching([](auto& document) { return document.is_fully_active(); })) {
windows.append(GC::make_root(document->window()));
}
return windows;
}
// NB: A page that asks for an idle callback from a task or a rendering update does not keep the event loop busy by
// doing so, so its callback need not wait for the last idle period's 50 ms to run out. One that an idle callback
// or a reaction to it asks for does wait, or the two would run back to back.
void EventLoop::did_request_idle_callback()
{
m_may_have_idle_callbacks = true;
if (m_task_generation != m_task_generation_of_last_idle_callback)
m_idle_callback_requested_outside_idle_period = true;
}
// https://html.spec.whatwg.org/multipage/webappapis.html#event-loop-processing-model:last-idle-period-start-time
double EventLoop::compute_deadline() const
{
// 1. Let deadline be this event loop's last idle period start time plus 50.
auto deadline = m_last_idle_period_start_time + 50;
// 2. Let hasPendingRenders be false.
auto has_pending_renders = false;
// 3. For each windowInSameLoop of the same-loop windows for this event loop:
for (auto& window : same_loop_windows()) {
// 1. If windowInSameLoop's map of animation frame callbacks is not empty,
// or if the user agent believes that the windowInSameLoop might have pending rendering updates,
// set hasPendingRenders to true.
if (window->has_animation_frame_callbacks())
has_pending_renders = true;
// FIXME: 2. Let timerCallbackEstimates be the result of getting the values of windowInSameLoop's map of active timers.
// FIXME: 3. For each timeoutDeadline of timerCallbackEstimates, if timeoutDeadline is less than deadline, set deadline to timeoutDeadline.
}
// 4. If hasPendingRenders is true, then:
if (has_pending_renders) {
// 1. Let nextRenderDeadline be this event loop's last render opportunity time plus (1000 divided by the current refresh rate).
// FIXME: Hardcoded to 60Hz
auto next_render_deadline = m_last_render_opportunity_time + (1000.0 / 60.0);
// 2. If nextRenderDeadline is less than deadline, then return nextRenderDeadline.
if (next_render_deadline < deadline)
return next_render_deadline;
}
// 5. Return deadline.
return deadline;
}
EventLoop::PauseHandle::PauseHandle(EventLoop& event_loop, JS::Object const& global, HighResolutionTime::DOMHighResTimeStamp time_before_pause)
: event_loop(event_loop)
, global(global)
, time_before_pause(time_before_pause)
{
}
EventLoop::PauseHandle::~PauseHandle()
{
event_loop->unpause({}, *global, time_before_pause);
}
// https://html.spec.whatwg.org/multipage/webappapis.html#pause
EventLoop::PauseHandle EventLoop::pause(UpdateTheRendering should_update_the_rendering)
{
++m_execution_pause_depth;
// 1. Let global be the current global object.
auto& global = current_global_object();
// 2. Let timeBeforePause be the current high resolution time given global.
auto time_before_pause = HighResolutionTime::current_high_resolution_time(global);
// 3. If necessary, update the rendering or user interface of any Document or navigable to reflect the current state.
// NB: UpdateTheRendering::No skips this step, for a caller that must not run author callbacks (e.g., rAF callbacks)
// while it's blocked — a sync XHR send(), which may itself have been invoked from within a microtask.
// A rendering update whose style update flies finishes first, as the update whose rendering the user sees.
if (should_update_the_rendering == UpdateTheRendering::Yes && m_rendering_update_in_flight) {
finish_rendering_update_in_flight();
} else if (should_update_the_rendering == UpdateTheRendering::Yes && !m_running_rendering_task) {
if (m_rendering_task_queued) {
m_task_queue->remove_tasks_matching([](auto const& task) {
return task.source() == Task::Source::Rendering;
});
m_rendering_task_queued = false;
}
m_last_render_opportunity_time = max(m_last_render_opportunity_time, HighResolutionTime::unsafe_shared_current_time());
TemporaryChange synchronous_rendering_update { m_running_synchronous_rendering_update, true };
update_the_rendering();
}
// 4. Wait until the condition goal is met. While a user agent has a paused task, the corresponding event loop must
// not run further tasks, and any script in the currently running task must block. User agents should remain
// responsive to user input while paused, however, albeit in a reduced capacity since the event loop will not be
// doing anything.
return PauseHandle { *this, global, time_before_pause };
}
void EventLoop::unpause(Badge<PauseHandle>, JS::Object const& global, HighResolutionTime::DOMHighResTimeStamp time_before_pause)
{
VERIFY(m_execution_pause_depth > 0);
--m_execution_pause_depth;
if (m_execution_pause_depth == 0)
schedule();
// FIXME: 5. Record pause duration given the duration from timeBeforePause to the current high resolution time given global.
[[maybe_unused]] auto pause_duration = HighResolutionTime::current_high_resolution_time(global) - time_before_pause;
}
}