Skip to content

Truly independent web browser

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

#include <LibGC/Heap.h>
#include <LibURL/RustIntegration.h>
#include <LibWeb/HTML/LocalNavigable.h>
#include <LibWeb/HTML/Location.h>
#include <LibWeb/HTML/NavigationSourceSnapshot.h>
#include <LibWeb/HTML/RemoteNavigable.h>
#include <LibWeb/HTML/RemoteWindow.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/SourceSnapshotParams.h>
#include <LibWeb/HTML/WindowProxy.h>
#include <LibWeb/Page/Page.h>
#include <LibWebCommon/HTML/PostedMessageDescriptor.h>

namespace Web::HTML {

GC_DEFINE_ALLOCATOR(RemoteWindow);

GC::Ref<RemoteWindow> RemoteWindow::create(RemoteNavigable& navigable)
{
    return GC::Heap::the().allocate<RemoteWindow>(navigable);
}

RemoteWindow::RemoteWindow(RemoteNavigable& navigable)
    : m_navigable(navigable)
{
}

RemoteWindow::~RemoteWindow() = default;

void RemoteWindow::visit_edges(Cell::Visitor& visitor)
{
    Base::visit_edges(visitor);
    visitor.visit(m_navigable);
    visitor.visit(m_location);
    visitor.visit(m_cross_origin_property_descriptor_map);
}

CrossOriginPropertyDescriptorMap& RemoteWindow::cross_origin_property_descriptor_map()
{
    if (!m_cross_origin_property_descriptor_map)
        m_cross_origin_property_descriptor_map = heap().allocate<CrossOriginPropertyDescriptorMap>();
    return *m_cross_origin_property_descriptor_map;
}

// https://html.spec.whatwg.org/multipage/nav-history-apis.html#window-navigable
GC::Ptr<RemoteNavigable> RemoteWindow::navigable() const
{
    if (m_navigable->has_been_destroyed())
        return nullptr;
    return m_navigable;
}

URL::Origin const& RemoteWindow::origin() const
{
    return m_navigable->replicated_state().active_document_origin;
}

// https://html.spec.whatwg.org/multipage/window-object.html#dom-window
GC::Ref<WindowProxy> RemoteWindow::window() const
{
    // The window, frames, and self getter steps are to return this's relevant realm.[[GlobalEnv]].[[GlobalThisValue]].
    return *m_navigable->active_window_proxy();
}

// https://html.spec.whatwg.org/multipage/window-object.html#dom-self
GC::Ref<WindowProxy> RemoteWindow::self() const
{
    // The window, frames, and self getter steps are to return this's relevant realm.[[GlobalEnv]].[[GlobalThisValue]].
    return window();
}

// https://html.spec.whatwg.org/multipage/window-object.html#dom-frames
GC::Ref<WindowProxy> RemoteWindow::frames() const
{
    // The window, frames, and self getter steps are to return this's relevant realm.[[GlobalEnv]].[[GlobalThisValue]].
    return window();
}

// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-location
GC::Ref<Location> RemoteWindow::location()
{
    // The Window object's location getter steps are to return this's Location object.
    if (!m_location)
        m_location = GC::Heap::the().allocate<Location>(*this);
    return *m_location;
}

// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-window-closed
bool RemoteWindow::closed() const
{
    // The closed getter steps are to return true if this's navigable is null or its is closing is true; otherwise
    // false.
    if (auto navigable = this->navigable(); !navigable || navigable->is_closing())
        return true;

    return false;
}

// https://html.spec.whatwg.org/multipage/window-object.html#dom-length
u32 RemoteWindow::length()
{
    // The length getter steps are to return this's associated Document's document-tree child navigables's size.
    return document_tree_child_navigables().size();
}

// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-top
GC::Ptr<WindowProxy const> RemoteWindow::top() const
{
    // 1. If this's navigable is null, then return null.
    auto navigable = this->navigable();
    if (!navigable)
        return {};

    // 2. Return this's navigable's top-level traversable's active WindowProxy.
    return navigable->top_level_traversable()->active_window_proxy();
}

// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-opener
JS::Value RemoteWindow::opener() const
{
    // 1. Let current be this's browsing context.
    // 2. If current is null, then return null.
    auto navigable = this->navigable();
    if (!navigable)
        return JS::js_null();

    // 3. If current's opener browsing context is null, then return null.
    auto opener_window_proxy = navigable->active_browsing_context_opener_window_proxy();
    if (!opener_window_proxy)
        return JS::js_null();

    // 4. Return current's opener browsing context's WindowProxy object.
    return &opener_window_proxy->object();
}

// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-parent
GC::Ptr<WindowProxy const> RemoteWindow::parent() const
{
    // 1. Let navigable be this's navigable.
    GC::Ptr<Navigable> navigable = this->navigable();

    // 2. If navigable is null, then return null.
    if (!navigable)
        return {};

    // 3. If navigable's parent is not null, then set navigable to navigable's parent.
    if (auto parent = navigable->parent())
        navigable = parent;

    // 4. Return navigable's active WindowProxy.
    return navigable->active_window_proxy();
}

// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-window-close
void RemoteWindow::close()
{
    // 1. Let thisTraversable be this's navigable.
    auto traversable = navigable();

    // 2. If thisTraversable is not a top-level traversable, then return.
    if (!traversable || !traversable->is_top_level_traversable())
        return;

    // 3. If thisTraversable's is closing is true, then return.
    if (traversable->is_closing())
        return;

    // 4. Let browsingContext be thisTraversable's active browsing context.
    // NB: Familiarity is checked on the navigables whose active browsing contexts these are.

    auto& incumbent_global_object = incumbent_window();

    // 5. Let sourceSnapshotParams be the result of snapshotting source snapshot params given the incumbent global object's
    //    associated Document.
    auto source_snapshot_params = snapshot_source_snapshot_params(incumbent_global_object.associated_document());

    auto incumbent_navigable = incumbent_global_object.navigable();
    if (!incumbent_navigable)
        return;

    // 6. If all the following are true:
    if (
        // thisTraversable is script-closable;
        traversable->is_script_closable()

        // the incumbent global object's browsing context is familiar with browsingContext; and
        && incumbent_navigable->is_familiar_with(*traversable)

        // the incumbent global object's navigable is allowed by sandboxing to navigate thisTraversable, given sourceSnapshotParams,
        && incumbent_navigable->allowed_by_sandboxing_to_navigate(*traversable, source_snapshot_params))
    // then:
    {
        // 1. Set thisTraversable's is closing to true.
        traversable->set_closing(true);

        // 2. Queue a task on the DOM manipulation task source to definitely close thisTraversable.
        // NB: The process hosting thisTraversable queues it once the UI process has checked the request.
        m_navigable->page().client().request_close_of_remote_traversable(*traversable, *incumbent_navigable, create_navigation_source_snapshot(source_snapshot_params));
    }
}

// https://html.spec.whatwg.org/multipage/interaction.html#dom-window-focus
void RemoteWindow::focus()
{
    // 1. Let current be this's navigable.
    auto current = navigable();

    // 2. If current is null, then return.
    if (!current)
        return;

    // 3. If the allow focus steps given current's active document return false, then return.
    // 4. Run the focusing steps with current.
    // 5. If current is a top-level traversable, user agents are encouraged to trigger some sort of notification to
    //    indicate to the user that the page is attempting to gain focus.
    // NB: current's active document is in the process hosting it, which runs these steps.
    m_navigable->page().client().request_window_focus_of_remote_navigable(*current);
}

// https://html.spec.whatwg.org/multipage/interaction.html#dom-window-blur
void RemoteWindow::blur()
{
    // The Window blur() method steps are to do nothing.
}

// https://html.spec.whatwg.org/multipage/web-messaging.html#dom-window-postmessage
WebIDL::ExceptionOr<void> RemoteWindow::post_message(JS::Realm& realm, JS::Value message, Utf16String const& target_origin, GC::RootVector<GC::Ref<JS::Object>> const& transfer)
{
    // The Window interface's postMessage(message, targetOrigin, transfer) method steps are to run the window post message
    // steps given this, message, and «[ "targetOrigin" → targetOrigin, "transfer" → transfer ]».
    return post_message(realm, message, Window::PostMessageOptions { { .transfer = transfer }, target_origin });
}

// https://html.spec.whatwg.org/multipage/web-messaging.html#dom-window-postmessage-options
WebIDL::ExceptionOr<void> RemoteWindow::post_message(JS::Realm& realm, JS::Value message, Window::PostMessageOptions const& options)
{
    // The Window interface's postMessage(message, options) method steps are to run the window post message steps given
    // this, message, and options.
    return Window::window_post_message_steps(realm, m_navigable, message, options);
}

extern "C" void web_window_post_message_send_remote(RemoteNavigable* target, EnvironmentSettingsObject* settings, SerializedTransferRecord* record, URL::FFI::FfiOrigin const* target_origin, URL::FFI::FfiOrigin const* source_origin, WindowProxy* source);

void web_window_post_message_send_remote(RemoteNavigable* target, EnvironmentSettingsObject* settings, SerializedTransferRecord* record, URL::FFI::FfiOrigin const* target_origin, URL::FFI::FfiOrigin const* source_origin, WindowProxy* source)
{
    Optional<CrossProcessId> source_navigable_id;
    // AD-HOC: The UI process takes the source only from a navigable this process hosts, so a source whose [[Window]] is
    //         in another process is null.
    // NB: A navigable in a destroyed subtree is discarded, like it is for the source's window.parent.
    if (auto window = source->window()) {
        if (auto source_navigable = window->navigable(); source_navigable && !source_navigable->is_in_a_destroyed_subtree())
            source_navigable_id = source_navigable->id();
    }
    Variant<Utf16String, URL::Origin> target_origin_value = u"*"_utf16;
    if (target_origin)
        target_origin_value = URL::RustIntegration::origin_from_ffi(*target_origin);
    PostedMessageDescriptor posted_message {
        .serialize_with_transfer_result = move(*record),
        .target_origin = move(target_origin_value),
        .incumbent_settings_id = settings->id,
        .source_origin = URL::RustIntegration::origin_from_ffi(*source_origin),
        .source_navigable_id = source_navigable_id,
    };
    target->page().client().request_post_message_to_remote_navigable(*target, move(posted_message));
}

// https://html.spec.whatwg.org/multipage/document-sequences.html#document-tree-child-navigables
Vector<GC::Root<Navigable>> RemoteWindow::document_tree_child_navigables()
{
    // 1. If document's node navigable is null, then return the empty list.
    // AD-HOC: A document in a destroyed navigable's subtree is no longer in the navigable tree, although it keeps its
    //         navigable until the destruction completes.
    auto navigable = this->navigable();
    if (!navigable || navigable->is_in_a_destroyed_subtree())
        return {};

    // 2-5.
    // NB: The document's navigable containers are in the process hosting it, which replicates their content
    //     navigables to the navigable here.
    return navigable->document_tree_child_navigables();
}

// https://html.spec.whatwg.org/multipage/nav-history-apis.html#document-tree-child-navigable-target-name-property-set
GC::OrderedRootHashMap<Utf16FlyString, GC::Ref<Navigable>> RemoteWindow::document_tree_child_navigable_target_name_property_set()
{
    // The document-tree child navigable target name property set of a Window object window is the return value of running these steps:

    // 1. Let children be the document-tree child navigables of window's associated Document.
    auto children = document_tree_child_navigables();

    // 2. Let firstNamedChildren be an empty ordered set.
    GC::OrderedRootHashMap<Utf16FlyString, GC::Ref<Navigable>> first_named_children;

    // 3. For each navigable of children:
    for (auto const& navigable : children) {
        // 1. Let name be navigable's target name.
        // 2. If name is the empty string, then continue.
        auto const& target_name = navigable->target_name();
        if (target_name.is_empty())
            continue;

        auto name = Utf16FlyString::from_utf16(target_name.utf16_view());

        // 3. If firstNamedChildren contains a navigable whose target name is name, then continue.
        if (first_named_children.contains(name))
            continue;

        // 4. Append navigable to firstNamedChildren.
        (void)first_named_children.set(name, *navigable);
    }

    // 4. Let names be an empty ordered set.
    GC::OrderedRootHashMap<Utf16FlyString, GC::Ref<Navigable>> names;

    // 5. For each navigable of firstNamedChildren:
    for (auto const& [name, navigable] : first_named_children) {
        // 1. Let name be navigable's target name.
        // 2. If navigable's active document's origin is same origin with window's relevant settings object's origin, then append name to names.
        auto origin = navigable->active_document_origin();
        if (origin.has_value() && origin->is_same_origin(this->origin())) {
            names.set(name, navigable);
            continue;
        }

        // NB: Browsers also expose cross-origin children whose target name matches their container's name content
        //     attribute, as Window::document_tree_child_navigable_target_name_property_set() does.
        if (auto container_name = navigable->container_name(); container_name.has_value() && *container_name == name)
            names.set(name, navigable);
    }

    // 6. Return names.
    return names;
}

}