Skip to content

Truly independent web browser

9.7 KB
Raw Blame
/*
 * Copyright (c) 2026-present, the Ladybird developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/NeverDestroyed.h>
#include <LibGC/Heap.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/LocalNavigable.h>
#include <LibWeb/HTML/NavigableContainer.h>
#include <LibWeb/HTML/PreparedNavigationDescriptor.h>
#include <LibWeb/HTML/RemoteNavigable.h>
#include <LibWeb/HTML/RemoteWindow.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Scripting/FetchClient.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/HTML/WindowProxy.h>
#include <LibWeb/Layout/Node.h>
#include <LibWeb/Page/Page.h>

namespace Web::HTML {

GC_DEFINE_ALLOCATOR(RemoteNavigable);

HashTable<GC::RawRef<RemoteNavigable>>& all_remote_navigables()
{
    // NB: Worker agents have no navigables, and these are cells of the window agent's heap.
    static thread_local NeverDestroyed<HashTable<GC::RawRef<RemoteNavigable>>> set;
    return *set;
}

GC::Ptr<RemoteNavigable> remote_navigable_with_id(Page const& page, CrossProcessId id)
{
    for (auto& navigable : all_remote_navigables()) {
        if (navigable->id() == id && &navigable->page() == &page)
            return navigable;
    }
    return nullptr;
}

GC::Ref<RemoteNavigable> RemoteNavigable::create(GC::Ref<Page> page, CrossProcessId id, GC::Ptr<Navigable> parent, ReplicatedNavigableState replicated_state)
{
    return GC::Heap::the().allocate<RemoteNavigable>(page, id, parent, move(replicated_state));
}

RemoteNavigable::RemoteNavigable(GC::Ref<Page> page, CrossProcessId id, GC::Ptr<Navigable> parent, ReplicatedNavigableState replicated_state)
    : Navigable(page)
    , m_replicated_state(move(replicated_state))
{
    set_id(id);
    set_parent(parent);
    all_remote_navigables().set(*this);
}

RemoteNavigable::~RemoteNavigable() = default;

void RemoteNavigable::finalize()
{
    all_remote_navigables().remove(*this);
    Base::finalize();
}

void RemoteNavigable::remove_from_all_remote_navigables()
{
    all_remote_navigables().remove(*this);
}

void RemoteNavigable::visit_edges(Cell::Visitor& visitor)
{
    Base::visit_edges(visitor);
    visitor.visit(m_children);
    visitor.visit(m_window_proxy);
    visitor.visit(m_active_window);
    visitor.visit(m_provisional_navigable);
}

void RemoteNavigable::append_child(GC::Ref<Navigable> child)
{
    VERIFY(child->parent().ptr() == this);
    m_children.append(child);
}

void RemoteNavigable::remove_child(Navigable& child)
{
    auto removed = m_children.remove_first_matching([&](auto const& existing_child) { return existing_child.ptr() == &child; });
    VERIFY(removed);
}

void RemoteNavigable::replace_child(Navigable& child, GC::Ref<Navigable> replacement)
{
    VERIFY(replacement->id() == child.id());
    VERIFY(replacement->parent().ptr() == this);
    auto index = m_children.find_first_index_if([&](auto const& existing_child) { return existing_child.ptr() == &child; });
    VERIFY(index.has_value());
    m_children[*index] = replacement;
}

// The WindowProxy of a navigable hosted by another process answers every access on the cross-origin path, so it
// belongs to no realm.
GC::Ptr<WindowProxy> RemoteNavigable::active_window_proxy()
{
    if (!m_window_proxy) {
        m_window_proxy = WindowProxy::create(vm().empty_realm());
        m_window_proxy->set_window(active_window());
    }
    return m_window_proxy;
}

GC::Ptr<WindowProxy> RemoteNavigable::active_browsing_context_opener_window_proxy() const
{
    if (auto opener_navigable = active_browsing_context_opener_navigable())
        return opener_navigable->active_window_proxy();
    return nullptr;
}

GC::Ptr<Navigable> RemoteNavigable::active_browsing_context_opener_navigable() const
{
    if (!m_replicated_state.opener_navigable_id.has_value())
        return nullptr;
    return navigable_with_id_in_any_page(page(), *m_replicated_state.opener_navigable_id);
}

GC::Ptr<Navigable> RemoteNavigable::active_browsing_context_one_permitted_sandboxed_navigator() const
{
    if (!m_replicated_state.one_permitted_sandboxed_navigator_navigable_id.has_value())
        return nullptr;
    return navigable_with_id_in_any_page(page(), *m_replicated_state.one_permitted_sandboxed_navigator_navigable_id);
}

GC::Ref<RemoteWindow> RemoteNavigable::active_window()
{
    if (!m_active_window)
        m_active_window = RemoteWindow::create(*this);
    return *m_active_window;
}

Vector<GC::Root<Navigable>> RemoteNavigable::document_tree_child_navigables()
{
    // NB: The children are in the order the UI process learned of their creation, not in tree order.
    Vector<GC::Root<Navigable>> navigables;
    for (auto& child : m_children) {
        if (child->container_is_in_document_tree())
            navigables.append(*child);
    }
    return navigables;
}

Vector<GC::Root<Navigable>> RemoteNavigable::active_document_inclusive_descendant_navigables()
{
    // The navigable's subtree as the UI process replicates it: itself, then each child's inclusive descendants.
    Vector<GC::Root<Navigable>> navigables;
    navigables.append(*this);
    for (auto& child : m_children)
        navigables.extend(child->active_document_inclusive_descendant_navigables());
    return navigables;
}

void RemoteNavigable::set_has_been_destroyed()
{
    m_has_been_destroyed = true;
    DOM::Document::invalidate_full_activity();
}

void RemoteNavigable::set_replicated_state(ReplicatedNavigableState state)
{
    auto was_delaying_the_load_event_of_its_container = m_replicated_state.delays_the_load_event_of_its_container;
    auto previous_compositor_context_id = m_replicated_state.compositor_context_id;
    m_replicated_state = move(state);
    DOM::Document::invalidate_full_activity();

    // The documents of the navigable's children hosted here composite into the context its document is painted
    // through, wherever that document is hosted.
    if (previous_compositor_context_id != m_replicated_state.compositor_context_id) {
        for (auto& child : m_children) {
            if (auto* local_child = as_if<LocalNavigable>(*child))
                local_child->set_parent_compositor_context(m_replicated_state.compositor_context_id);
        }
    }

    // A container here hears from the document through the replicated state what a document here tells it directly.
    auto container = this->container();
    if (!container)
        return;
    if (previous_compositor_context_id != m_replicated_state.compositor_context_id) {
        container->publish_dom_paint_facts();
        container->set_needs_repaint();
    }
    if (was_delaying_the_load_event_of_its_container != m_replicated_state.delays_the_load_event_of_its_container)
        container->document().schedule_html_parser_end_check();
}

ReplicatedContainerState RemoteNavigable::container_state() const
{
    if (auto container = this->container())
        return container->replicated_container_state();
    return m_replicated_state.container;
}

// https://html.spec.whatwg.org/multipage/nav-history-apis.html#script-closable
bool RemoteNavigable::is_script_closable() const
{
    // A navigable is script-closable if it is a top-level traversable, and any of the following are true:
    // - its is created by web content is true; or
    // - its session history entries's size is 1.
    if (!is_top_level_traversable())
        return false;

    return m_replicated_state.top_level_is_created_by_web_content
        || m_replicated_state.top_level_session_history_entry_count == 1;
}

bool RemoteNavigable::has_session_history_entry_and_ready_for_navigation() const
{
    return m_replicated_state.has_session_history_entry_and_ready_for_navigation;
}

bool RemoteNavigable::delays_the_load_event_of_its_container() const
{
    // A destroyed navigable's document is on its way out, as a local one's is once its delaying flag is cleared.
    return !has_been_destroyed() && m_replicated_state.delays_the_load_event_of_its_container;
}

// https://html.spec.whatwg.org/multipage/interaction.html#currently-focused-area-of-a-top-level-traversable
GC::Ptr<DOM::Node> RemoteNavigable::currently_focused_area()
{
    // 1. If traversable does not have system focus, then return null.
    // 2. Let candidate be traversable's active document.
    // 3. While candidate's focused area is a navigable container with a non-null content navigable: set candidate to
    //    the active document of that navigable container's content navigable.
    // 4. If candidate's focused area is non-null, set candidate to candidate's focused area.
    // 5. Return candidate.
    // NB: The active document is in the process hosting this navigable. The tab's focused navigable shows where the
    //     walk goes below it.
    return currently_focused_area_shown_by_focused_navigable();
}

// https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate
WebIDL::ExceptionOr<void> RemoteNavigable::continue_navigation_in_active_document_agent(PreparedNavigation navigation)
{
    // 8. If the surrounding agent is equal to navigable's active document's relevant agent, then continue these
    //    steps. Otherwise, queue a global task on the navigation and traversal task source given navigable's active
    //    window to continue these steps.
    // NB: The active window lives in the process hosting the active document, so the task is a request to the UI
    //     process, which forwards it to that process.
    auto fetch_client = settings_object_of_fetch_client(navigation.source_snapshot_params->fetch_client);
    if (!fetch_client)
        return {};
    auto& source_document = window_from_global_object(fetch_client->global_object())->associated_document();
    auto source_navigable = source_document.navigable();
    if (!source_navigable)
        return {};
    page().client().request_navigation_of_navigable(*this, *source_navigable, source_document, create_prepared_navigation_descriptor(navigation));
    return {};
}

}