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 <LibJS/Forward.h>
10#include <LibJS/Heap/Cell.h>
11#include <LibJS/Heap/GCPtr.h>
12#include <LibJS/SafeFunction.h>
13#include <LibWeb/Fetch/Infrastructure/FetchTimingInfo.h>
14
15namespace Web::Fetch::Infrastructure {
16
17// https://fetch.spec.whatwg.org/#fetch-controller
18class FetchController : public JS::Cell {
19 JS_CELL(FetchController, JS::Cell);
20
21public:
22 enum class State {
23 Ongoing,
24 Terminated,
25 Aborted,
26 };
27
28 [[nodiscard]] static JS::NonnullGCPtr<FetchController> create(JS::VM&);
29
30 void set_full_timing_info(JS::NonnullGCPtr<FetchTimingInfo> full_timing_info) { m_full_timing_info = full_timing_info; }
31 void set_report_timing_steps(JS::SafeFunction<void(JS::Object const&)> report_timing_steps) { m_report_timing_steps = move(report_timing_steps); }
32 void set_next_manual_redirect_steps(JS::SafeFunction<void()> next_manual_redirect_steps) { m_next_manual_redirect_steps = move(next_manual_redirect_steps); }
33
34 [[nodiscard]] State state() const { return m_state; }
35
36 void report_timing(JS::Object const&) const;
37 void process_next_manual_redirect() const;
38 [[nodiscard]] JS::NonnullGCPtr<FetchTimingInfo> extract_full_timing_info() const;
39 void abort(JS::Realm&, Optional<JS::Value>);
40 void terminate();
41
42private:
43 FetchController();
44
45 virtual void visit_edges(JS::Cell::Visitor&) override;
46
47 // https://fetch.spec.whatwg.org/#fetch-controller-state
48 // state (default "ongoing")
49 // "ongoing", "terminated", or "aborted"
50 State m_state { State::Ongoing };
51
52 // https://fetch.spec.whatwg.org/#fetch-controller-full-timing-info
53 // full timing info (default null)
54 // Null or a fetch timing info.
55 JS::GCPtr<FetchTimingInfo> m_full_timing_info;
56
57 // https://fetch.spec.whatwg.org/#fetch-controller-report-timing-steps
58 // report timing steps (default null)
59 // Null or an algorithm accepting a global object.
60 Optional<JS::SafeFunction<void(JS::Object const&)>> m_report_timing_steps;
61
62 // https://fetch.spec.whatwg.org/#fetch-controller-report-timing-steps
63 // FIXME: serialized abort reason (default null)
64 // Null or a Record (result of StructuredSerialize).
65
66 // https://fetch.spec.whatwg.org/#fetch-controller-next-manual-redirect-steps
67 // next manual redirect steps (default null)
68 // Null or an algorithm accepting nothing.
69 Optional<JS::SafeFunction<void()>> m_next_manual_redirect_steps;
70};
71
72}