Serenity Operating System
1/*
2 * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <AK/Optional.h>
10#include <LibJS/Heap/Cell.h>
11#include <LibJS/Heap/GCPtr.h>
12#include <LibJS/SafeFunction.h>
13#include <LibWeb/Forward.h>
14
15namespace Web::Fetch::Infrastructure {
16
17// https://fetch.spec.whatwg.org/#fetch-elsewhere-fetch
18class FetchAlgorithms : public JS::Cell {
19 JS_CELL(FetchAlgorithms, JS::Cell);
20
21public:
22 struct ConsumeBodyFailureTag { };
23 using ProcessRequestBodyChunkLengthFunction = JS::SafeFunction<void(u64)>;
24 using ProcessRequestEndOfBodyFunction = JS::SafeFunction<void()>;
25 using ProcessEarlyHintsResponseFunction = JS::SafeFunction<void(JS::NonnullGCPtr<Infrastructure::Response>)>;
26 using ProcessResponseFunction = JS::SafeFunction<void(JS::NonnullGCPtr<Infrastructure::Response>)>;
27 using ProcessResponseEndOfBodyFunction = JS::SafeFunction<void(JS::NonnullGCPtr<Infrastructure::Response>)>;
28 using ProcessResponseConsumeBodyFunction = JS::SafeFunction<void(JS::NonnullGCPtr<Infrastructure::Response>, Variant<Empty, ConsumeBodyFailureTag, ByteBuffer>)>;
29
30 struct Input {
31 Optional<ProcessRequestBodyChunkLengthFunction> process_request_body_chunk_length;
32 Optional<ProcessRequestEndOfBodyFunction> process_request_end_of_body;
33 Optional<ProcessEarlyHintsResponseFunction> process_early_hints_response;
34 Optional<ProcessResponseFunction> process_response;
35 Optional<ProcessResponseEndOfBodyFunction> process_response_end_of_body;
36 Optional<ProcessResponseConsumeBodyFunction> process_response_consume_body;
37 };
38
39 [[nodiscard]] static JS::NonnullGCPtr<FetchAlgorithms> create(JS::VM&, Input);
40
41 Optional<ProcessRequestBodyChunkLengthFunction> const& process_request_body_chunk_length() const { return m_process_request_body_chunk_length; }
42 Optional<ProcessRequestEndOfBodyFunction> const& process_request_end_of_body() const { return m_process_request_end_of_body; }
43 Optional<ProcessEarlyHintsResponseFunction> const& process_early_hints_response() const { return m_process_early_hints_response; }
44 Optional<ProcessResponseFunction> const& process_response() const { return m_process_response; }
45 Optional<ProcessResponseEndOfBodyFunction> const& process_response_end_of_body() const { return m_process_response_end_of_body; }
46 Optional<ProcessResponseConsumeBodyFunction> const& process_response_consume_body() const { return m_process_response_consume_body; }
47
48private:
49 explicit FetchAlgorithms(Input);
50
51 Optional<ProcessRequestBodyChunkLengthFunction> m_process_request_body_chunk_length;
52 Optional<ProcessRequestEndOfBodyFunction> m_process_request_end_of_body;
53 Optional<ProcessEarlyHintsResponseFunction> m_process_early_hints_response;
54 Optional<ProcessResponseFunction> m_process_response;
55 Optional<ProcessResponseEndOfBodyFunction> m_process_response_end_of_body;
56 Optional<ProcessResponseConsumeBodyFunction> m_process_response_consume_body;
57};
58
59}