From 5d86eae1198dd789a05231b026e0ef1b68bea2f9 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Thu, 13 Oct 2022 19:24:23 +0200 Subject: [PATCH] LibWeb: Implement 'fetch params' struct --- Userland/Libraries/LibWeb/CMakeLists.txt | 1 + .../Fetch/Infrastructure/FetchParams.cpp | 56 ++++++++++ .../LibWeb/Fetch/Infrastructure/FetchParams.h | 104 ++++++++++++++++++ Userland/Libraries/LibWeb/Forward.h | 1 + 4 files changed, 162 insertions(+) create mode 100644 Userland/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.cpp create mode 100644 Userland/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.h diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index 15fa404dee..20bfa3c96f 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -127,6 +127,7 @@ set(SOURCES Fetch/Infrastructure/ConnectionTimingInfo.cpp Fetch/Infrastructure/FetchAlgorithms.cpp Fetch/Infrastructure/FetchController.cpp + Fetch/Infrastructure/FetchParams.cpp Fetch/Infrastructure/FetchTimingInfo.cpp Fetch/Infrastructure/HTTP.cpp Fetch/Infrastructure/HTTP/Bodies.cpp diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.cpp b/Userland/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.cpp new file mode 100644 index 0000000000..257c42869d --- /dev/null +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2022, Linus Groh + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include + +namespace Web::Fetch::Infrastructure { + +FetchParams::FetchParams(JS::NonnullGCPtr request, JS::NonnullGCPtr algorithms, JS::NonnullGCPtr controller, JS::NonnullGCPtr timing_info) + : m_request(request) + , m_algorithms(algorithms) + , m_controller(controller) + , m_timing_info(timing_info) +{ +} + +JS::NonnullGCPtr FetchParams::create(JS::VM& vm, JS::NonnullGCPtr request, JS::NonnullGCPtr timing_info) +{ + auto algorithms = Infrastructure::FetchAlgorithms::create(vm, {}); + auto controller = Infrastructure::FetchController::create(vm); + return { *vm.heap().allocate_without_realm(request, algorithms, controller, timing_info) }; +} + +void FetchParams::visit_edges(JS::Cell::Visitor& visitor) +{ + Base::visit_edges(visitor); + visitor.visit(m_request); + visitor.visit(m_algorithms); + visitor.visit(m_controller); + visitor.visit(m_timing_info); + if (m_task_destination.has>()) + visitor.visit(m_task_destination.get>()); + if (m_preloaded_response_candidate.has>()) + visitor.visit(m_preloaded_response_candidate.get>()); +} + +// https://fetch.spec.whatwg.org/#fetch-params-aborted +bool FetchParams::is_aborted() const +{ + // A fetch params fetchParams is aborted if its controller’s state is "aborted". + return m_controller->state() == FetchController::State::Aborted; +} + +// https://fetch.spec.whatwg.org/#fetch-params-canceled +bool FetchParams::is_canceled() const +{ + // A fetch params fetchParams is canceled if its controller’s state is "aborted" or "terminated". + return m_controller->state() == FetchController::State::Aborted || m_controller->state() == FetchController::State::Terminated; +} + +} diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.h b/Userland/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.h new file mode 100644 index 0000000000..ab5da9ace6 --- /dev/null +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/FetchParams.h @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2022, Linus Groh + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +namespace Web::Fetch::Infrastructure { + +// https://fetch.spec.whatwg.org/#fetch-params +class FetchParams : public JS::Cell { + JS_CELL(FetchParams, JS::Cell); + +public: + // FIXME: 'or a parallel queue' + using TaskDestination = Variant>; + + struct PreloadedResponseCandidatePendingTag { }; + using PreloadedResponseCandidate = Variant>; + + [[nodiscard]] static JS::NonnullGCPtr create(JS::VM&, JS::NonnullGCPtr, JS::NonnullGCPtr); + + [[nodiscard]] JS::NonnullGCPtr request() const { return m_request; } + [[nodiscard]] JS::NonnullGCPtr controller() const { return m_controller; } + [[nodiscard]] JS::NonnullGCPtr timing_info() const { return m_timing_info; } + + [[nodiscard]] JS::NonnullGCPtr algorithms() const { return m_algorithms; } + void set_algorithms(JS::NonnullGCPtr algorithms) { m_algorithms = algorithms; } + + [[nodiscard]] TaskDestination& task_destination() { return m_task_destination; } + [[nodiscard]] TaskDestination const& task_destination() const { return m_task_destination; } + void set_task_destination(TaskDestination task_destination) { m_task_destination = move(task_destination); } + + [[nodiscard]] HTML::CanUseCrossOriginIsolatedAPIs cross_origin_isolated_capability() const { return m_cross_origin_isolated_capability; } + void set_cross_origin_isolated_capability(HTML::CanUseCrossOriginIsolatedAPIs cross_origin_isolated_capability) { m_cross_origin_isolated_capability = cross_origin_isolated_capability; } + + [[nodiscard]] PreloadedResponseCandidate& preloaded_response_candidate() { return m_preloaded_response_candidate; } + [[nodiscard]] PreloadedResponseCandidate const& preloaded_response_candidate() const { return m_preloaded_response_candidate; } + void set_preloaded_response_candidate(PreloadedResponseCandidate preloaded_response_candidate) { m_preloaded_response_candidate = move(preloaded_response_candidate); } + + [[nodiscard]] bool is_aborted() const; + [[nodiscard]] bool is_canceled() const; + +private: + FetchParams(JS::NonnullGCPtr, JS::NonnullGCPtr, JS::NonnullGCPtr, JS::NonnullGCPtr); + + virtual void visit_edges(JS::Cell::Visitor&) override; + + // https://fetch.spec.whatwg.org/#fetch-params-request + // request + // A request. + JS::NonnullGCPtr m_request; + + // https://fetch.spec.whatwg.org/#fetch-params-process-request-body + // process request body chunk length (default null) + // https://fetch.spec.whatwg.org/#fetch-params-process-request-end-of-body + // process request end-of-body (default null) + // https://fetch.spec.whatwg.org/#fetch-params-process-early-hints-response + // process early hints response (default null) + // https://fetch.spec.whatwg.org/#fetch-params-process-response + // process response (default null) + // https://fetch.spec.whatwg.org/#fetch-params-process-response-end-of-body + // process response end-of-body (default null) + // https://fetch.spec.whatwg.org/#fetch-params-process-response-consume-body + // process response consume body (default null) + // Null or an algorithm. + JS::NonnullGCPtr m_algorithms; + + // https://fetch.spec.whatwg.org/#fetch-params-task-destination + // task destination (default null) + // Null, a global object, or a parallel queue. + TaskDestination m_task_destination; + + // https://fetch.spec.whatwg.org/#fetch-params-cross-origin-isolated-capability + // cross-origin isolated capability (default false) + // A boolean. + HTML::CanUseCrossOriginIsolatedAPIs m_cross_origin_isolated_capability { HTML::CanUseCrossOriginIsolatedAPIs::No }; + + // https://fetch.spec.whatwg.org/#fetch-params-controller + // controller (default a new fetch controller) + // A fetch controller. + JS::NonnullGCPtr m_controller; + + // https://fetch.spec.whatwg.org/#fetch-params-timing-info + // timing info + // A fetch timing info. + JS::NonnullGCPtr m_timing_info; + + // https://fetch.spec.whatwg.org/#fetch-params-preloaded-response-candidate + // preloaded response candidate (default null) + // Null, "pending", or a response. + PreloadedResponseCandidate m_preloaded_response_candidate; +}; + +} diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index cafdf2dc79..3276677f0d 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -195,6 +195,7 @@ struct BodyWithType; class ConnectionTimingInfo; class FetchAlgorithms; class FetchController; +class FetchParams; class FetchTimingInfo; struct Header; class HeaderList;