Skip to content

Truly independent web browser

9.8 KB
Raw Blame
/*
 * Copyright (c) 2022, Andreas Kling <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Optional.h>
#include <AK/RefCounted.h>
#include <AK/Utf16String.h>
#include <LibGC/CellAllocator.h>
#include <LibGC/Forward.h>
#include <LibGC/Ptr.h>
#include <LibURL/Origin.h>
#include <LibWeb/Bindings/PerformanceNavigationTiming.h>
#include <LibWeb/Export.h>
#include <LibWeb/Fetch/Infrastructure/FetchTimingInfo.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
#include <LibWeb/Forward.h>
#include <LibWeb/HTML/PolicyContainers.h>
#include <LibWeb/HTML/UserNavigationInvolvement.h>
#include <LibWebCommon/HTML/CrossOrigin/OpenerPolicy.h>
#include <LibWebCommon/HTML/CrossOrigin/OpenerPolicyEnforcementResult.h>
#include <LibWebCommon/HTML/CrossOriginIsolationMode.h>
#include <LibWebCommon/HTML/CrossProcessId.h>
#include <LibWebCommon/HTML/NavigationParamsDescriptor.h>
#include <LibWebCommon/HTML/SandboxingFlagSet.h>
#include <LibWebCommon/ReferrerPolicy/ReferrerPolicy.h>

namespace Web::HTML {

// NB: Reconstructed response callbacks and download adoption share one terminal-event owner.
struct NavigationResponseNetworkEventState : RefCounted<NavigationResponseNetworkEventState> {
    AK_ALLOC_WITH_KMALLOC;
    GC_HOLDS_NO_CELL_EDGES;

    enum class Owner {
        Renderer,
        Download,
        Completed,
    };
    Owner owner { Owner::Renderer };
};

// https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigation-params
struct NavigationParams : GC::Cell {
    GC_CELL(NavigationParams, GC::Cell);
    GC_DECLARE_ALLOCATOR(NavigationParams);

    // null or a navigation ID
    Optional<Utf16String> id;

    // the navigable to be navigated
    GC::Ptr<LocalNavigable> navigable;

    // null or a request that started the navigation
    RefPtr<Fetch::Infrastructure::Request> request;

    // a response that ultimately was navigated to (potentially a network error)
    GC::Ptr<Fetch::Infrastructure::ResponseView> response;

    // null or a fetch controller
    GC::Ptr<Fetch::Infrastructure::FetchController> fetch_controller { nullptr };

    // AD-HOC: The fetch controller stays in the process that ran the navigation fetch. Navigation params rebuilt from
    //         a descriptor carry the fetch's timing info here instead, so the new Document's navigation timing entry
    //         still sees it.
    RefPtr<Fetch::Infrastructure::FetchTimingInfo> fetch_timing_info { nullptr };

    // AD-HOC: The terminal network event follows the owner of a transferred
    // response, including a download.
    JsonValue webdriver_bidi_network_response;
    RefPtr<NavigationResponseNetworkEventState> webdriver_bidi_network_response_state;

    // AD-HOC: The agent cluster of the agent the UI process obtained for the new Document's window, and that agent
    //         cluster's cross-origin isolation mode.
    Optional<u64> agent_cluster_id;
    bool agent_cluster_is_origin_keyed { false };
    CrossOriginIsolationMode agent_cluster_cross_origin_isolation_mode { CrossOriginIsolationMode::None };

    // AD-HOC: The id the UI process gave the new Document.
    Optional<CrossProcessId> document_id;

    // AD-HOC: The group of the new top-level browsing context the UI process obtained for the new Document, when a
    //         browsing context group switch gave it one.
    Optional<u64> new_browsing_context_group_id;

    // AD-HOC: The browsing context obtained for the new Document when the response arrived.
    GC::Ptr<BrowsingContext> browsing_context;

    // AD-HOC: The id of the new Window's environment when there is no reserved environment. The UI process learns it
    //         before the Document is created.
    Optional<EnvironmentId> environment_id;

    // AD-HOC: The permissions policy the UI process created for the new Document.
    Optional<PermissionsPolicy::PermissionsPolicy> permissions_policy;

    // null or an algorithm accepting a Document, once it has been created
    GC::Ptr<GC::Function<void(DOM::Document&)>> commit_early_hints { nullptr };

    // an opener policy enforcement result, used for reporting and potentially for causing a browsing context group switch
    OpenerPolicyEnforcementResult coop_enforcement_result;

    // null or an environment reserved for the new Document
    GC::Ptr<Environment> reserved_environment;

    // an origin to use for the new Document
    URL::Origin origin;

    // a policy container to use for the new Document
    RefPtr<PolicyContainer> policy_container;

    // a sandboxing flag set to impose on the new Document
    SandboxingFlagSet final_sandboxing_flag_set = {};

    // a referrer policy, used in the internal ancestor origin objects list creation steps
    ReferrerPolicy::ReferrerPolicy iframe_element_referrer_policy { ReferrerPolicy::ReferrerPolicy::EmptyString };

    // an opener policy to use for the new Document
    OpenerPolicy opener_policy;

    // a NavigationTimingType used for creating the navigation timing entry for the new Document
    Bindings::NavigationTimingType navigation_timing_type { Bindings::NavigationTimingType::Navigate };

    // a URL or null used to populate the new Document's about base URL
    Optional<URL::URL> about_base_url;

    // a user navigation involvement used when obtaining a browsing context for the new Document
    UserNavigationInvolvement user_involvement;

    // The fetch controller, or else the transport of a body adopted without one.
    using BodyDelivery = Variant<Empty, GC::Ref<Fetch::Infrastructure::FetchController>, NonnullRefPtr<Fetch::Engine::NetworkTransportHandle>>;
    [[nodiscard]] BodyDelivery body_delivery() const;

protected:
    void visit_edges(Visitor& visitor) override;

    NavigationParams(
        Optional<Utf16String> id,
        GC::Ptr<LocalNavigable> navigable,
        RefPtr<Fetch::Infrastructure::Request> request,
        GC::Ptr<Fetch::Infrastructure::ResponseView> response,
        GC::Ptr<Fetch::Infrastructure::FetchController> fetch_controller,
        GC::Ptr<GC::Function<void(DOM::Document&)>> commit_early_hints,
        OpenerPolicyEnforcementResult coop_enforcement_result,
        GC::Ptr<Environment> reserved_environment,
        URL::Origin origin,
        RefPtr<PolicyContainer> policy_container,
        SandboxingFlagSet final_sandboxing_flag_set,
        ReferrerPolicy::ReferrerPolicy iframe_element_referrer_policy,
        OpenerPolicy opener_policy,
        Bindings::NavigationTimingType navigation_timing_type,
        Optional<URL::URL> about_base_url,
        UserNavigationInvolvement user_involvement)
        : id(move(id))
        , navigable(navigable)
        , request(request)
        , response(response)
        , fetch_controller(fetch_controller)
        , commit_early_hints(commit_early_hints)
        , coop_enforcement_result(move(coop_enforcement_result))
        , reserved_environment(reserved_environment)
        , origin(move(origin))
        , policy_container(policy_container)
        , final_sandboxing_flag_set(final_sandboxing_flag_set)
        , iframe_element_referrer_policy(iframe_element_referrer_policy)
        , opener_policy(opener_policy)
        , navigation_timing_type(navigation_timing_type)
        , about_base_url(move(about_base_url))
        , user_involvement(user_involvement)
    {
    }
};

// https://html.spec.whatwg.org/multipage/browsing-the-web.html#non-fetch-scheme-navigation-params
struct NonFetchSchemeNavigationParams : JS::Cell {
    GC_CELL(NonFetchSchemeNavigationParams, JS::Cell);
    GC_DECLARE_ALLOCATOR(NonFetchSchemeNavigationParams);

    // null or a navigation ID
    Optional<Utf16String> id;

    // the navigable to be navigated
    GC::Ptr<LocalNavigable> navigable;

    // a URL
    URL::URL url;

    // the target snapshot params's sandboxing flags present during navigation
    SandboxingFlagSet target_snapshot_sandboxing_flags = {};

    // a copy of the source snapshot params's has transient activation boolean present during activation
    bool source_snapshot_has_transient_activation = { false };

    // an origin possibly for use in a user-facing prompt to confirm the invocation of an external software package
    URL::Origin initiator_origin;

    // a NavigationTimingType used for creating the navigation timing entry for the new Document
    Bindings::NavigationTimingType navigation_timing_type { Bindings::NavigationTimingType::Navigate };

    // a user navigation involvement used when obtaining a browsing context for the new Document (if one is created)
    UserNavigationInvolvement user_involvement;

protected:
    NonFetchSchemeNavigationParams(
        Optional<Utf16String> id,
        GC::Ptr<LocalNavigable> navigable,
        URL::URL url,
        SandboxingFlagSet target_snapshot_sandboxing_flags,
        bool source_snapshot_has_transient_activation,
        URL::Origin initiator_origin,
        Bindings::NavigationTimingType navigation_timing_type,
        UserNavigationInvolvement user_involvement)
        : id(move(id))
        , navigable(navigable)
        , url(move(url))
        , target_snapshot_sandboxing_flags(target_snapshot_sandboxing_flags)
        , source_snapshot_has_transient_activation(source_snapshot_has_transient_activation)
        , initiator_origin(move(initiator_origin))
        , navigation_timing_type(navigation_timing_type)
        , user_involvement(user_involvement)
    {
    }

    void visit_edges(Visitor& visitor) override;
};

using NavigationParamsVariant = Variant<NavigationParamsNullOrError, GC::Ref<NavigationParams>, GC::Ref<NonFetchSchemeNavigationParams>>;

// Returns whether a fetch controller or network transport was stopped.
WEB_API bool stop_body_delivery(NavigationParams::BodyDelivery const&);

bool check_a_navigation_responses_adherence_to_x_frame_options(GC::Ptr<Fetch::Infrastructure::ResponseView> response, LocalNavigable* navigable, ContentSecurityPolicy::PolicyList const& csp_list, URL::Origin destination_origin);

}