Skip to content

Truly independent web browser

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

#include <LibURL/Site.h>
#include <LibWebView/BlobURLStore.h>
#include <LibWebView/CanonicalBrowsingContext.h>
#include <LibWebView/CanonicalBrowsingContextGroup.h>
#include <LibWebView/CanonicalDocument.h>
#include <LibWebView/CanonicalWindow.h>
#include <LibWebView/WebContentClient.h>
#include <LibWebView/WebContentPage.h>
#include <LibWebView/WebUI.h>

namespace WebView {

NonnullRefPtr<CanonicalDocument> CanonicalDocument::create(Web::HTML::CrossProcessId id, URL::URL creation_url, URL::Origin origin, NonnullRefPtr<CanonicalBrowsingContext> browsing_context, NonnullRefPtr<CanonicalWindow> relevant_global_object, Web::HTML::SandboxingFlagSet active_sandboxing_flag_set, IsInitialAboutBlank is_initial_about_blank)
{
    auto document = adopt_ref(*new CanonicalDocument(id, move(creation_url), move(origin), move(browsing_context), move(relevant_global_object), active_sandboxing_flag_set, is_initial_about_blank));

    // https://html.spec.whatwg.org/multipage/document-lifecycle.html#initialise-the-document-object
    // 10. Set window's associated Document to document.
    // AD-HOC: Make a window reused from an initial about:blank keep the about:blank as its associated Document until
    //         this document is made active, since the navigation creating this document can be abandoned before this
    //         document is made active. That leaves the about:blank active, and the origin of the about:blank's window's
    //         relevant settings object is then the origin of the window's associated Document. Blink, WebKit, and Gecko
    //         all hand a reused window over to the new document only when the new document commits: WebKit in
    //         DocumentWriter::begin() (takeDOMWindowFrom()), Gecko in nsGlobalWindowOuter::SetNewDocument(), and Blink
    //         in DocumentLoader::CommitNavigation() (ShouldReuseDOMWindow()).
    if (!document->m_relevant_global_object->has_associated_document())
        document->m_relevant_global_object->set_associated_document({}, document);
    return document;
}

CanonicalDocument::CanonicalDocument(Web::HTML::CrossProcessId id, URL::URL creation_url, URL::Origin origin, NonnullRefPtr<CanonicalBrowsingContext> browsing_context, NonnullRefPtr<CanonicalWindow> relevant_global_object, Web::HTML::SandboxingFlagSet active_sandboxing_flag_set, IsInitialAboutBlank is_initial_about_blank)
    : m_id(id)
    , m_creation_url(move(creation_url))
    , m_origin(move(origin))
    , m_browsing_context(move(browsing_context))
    , m_relevant_global_object(move(relevant_global_object))
    , m_active_sandboxing_flag_set(active_sandboxing_flag_set)
    , m_is_initial_about_blank(is_initial_about_blank)
{
    if (m_creation_url.scheme() == "about"sv && m_creation_url.path_segment_count() == 1) {
        if (auto page = WebUI::page_for_host(m_creation_url.path_segments().first()); page.has_value() && page->type != WebUI::PageType::Static)
            m_web_ui_host = MUST(String::from_utf8(page->host));
    }
}

CanonicalDocument::~CanonicalDocument() = default;

bool CanonicalDocument::is_allowed_to_use(Web::PermissionsPolicy::PolicyControlledFeature feature) const
{
    return Web::PermissionsPolicy::check_permissions_policy(m_permissions_policy, feature, m_origin, m_origin) == Web::PermissionsPolicy::FeatureValue::Enabled;
}

Optional<URL::Origin> CanonicalDocument::top_level_origin() const
{
    auto& top_level_browsing_context = m_browsing_context->top_level_browsing_context();

    // NB: A top-level document is not yet its browsing context's active document when it is placed in a process.
    if (&top_level_browsing_context == m_browsing_context.ptr())
        return m_origin;
    if (top_level_browsing_context.has_been_discarded())
        return {};
    return top_level_browsing_context.active_document()->origin();
}

bool CanonicalDocument::is_top_level() const
{
    return &m_browsing_context->top_level_browsing_context() == m_browsing_context.ptr();
}

bool CanonicalDocument::may_share_a_process_with_tabs_of_its_origin() const
{
    if (!is_top_level() || m_origin.is_opaque() || m_web_ui_host.has_value() || is_local_file_content())
        return false;

    auto const& scheme = m_origin.scheme();
    if (!scheme.has_value() || !scheme->is_one_of("http"sv, "https"sv))
        return false;

    auto const& agent = m_relevant_global_object->agent();
    return agent.agent_cluster_cross_origin_isolation_mode() == Web::HTML::CrossOriginIsolationMode::None
        && m_opener_policy.value != Web::HTML::OpenerPolicyValue::SameOriginPlusCOEP;
}

void CanonicalDocument::set_host(RefPtr<WebContentPage> host)
{
    m_host = move(host);
    if (m_host) {
        m_relevant_global_object->agent().set_hosting_process_if_unset(m_host->client());
        // The process makes the document's requests through the RequestServer of the document's top-level site.
        m_host->client().request_server_site_bindings().bind_sites_of(*this);
        // NB: The PDF viewer has an origin of its own, but it only ever replaces a document in the process of the PDF's
        //     site, so it brings no second site along.
        auto is_pdf_viewer = !m_origin.is_opaque() && m_origin.scheme() == "resource"sv;
        if (auto site = URL::Site::serialize_for_partitioning(m_origin); site.has_value() && !is_pdf_viewer)
            m_host->client().did_host_document_of_site(site.release_value());
        m_host->client().note_hosted_document(*this);
    }
}

// NB: Only a navigation the UI process admitted to a file: URL creates a document from one, and the document takes the
//     root the UI process used to read the file. A blob: URL entry local file content added carries that content's origin, as a document
//     created from it does. The entry can be revoked once the document exists, so this is settled when the document is
//     populated, before a process is chosen to host it.
void CanonicalDocument::determine_whether_it_is_local_file_content(BlobURLStore const* blob_url_store, Optional<LocalFileRoot> root_of_read_file)
{
    if (m_creation_url.scheme() == "file"sv)
        m_local_file_root = move(root_of_read_file);
    else if (m_creation_url.scheme() == "blob"sv && blob_url_store)
        m_local_file_root = blob_url_store->local_file_root_of_entry(Utf16String::from_utf8(m_creation_url.serialize(URL::ExcludeFragment::Yes)), m_origin);
    else
        m_local_file_root.clear();
}

// https://html.spec.whatwg.org/multipage/browsing-the-web.html#make-active
void CanonicalDocument::make_active()
{
    // 1. Let window be document's relevant global object.
    auto& window = relevant_global_object();

    // 2. Set document's browsing context's active document to document.
    m_browsing_context->set_active_document({}, *this);

    // 3. Set document's browsing context's WindowProxy's [[Window]] internal slot value to window.
    m_browsing_context->set_active_window({}, window);

    // 4. Set window's relevant settings object's execution ready flag.
    // NB: The process hosting the document sets it.

    // AD-HOC: Make a window reused from an initial about:blank take this document as its associated Document only now
    //         that this document is made active. See create().
    window.set_associated_document({}, *this);
}

}