summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/Fetch
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2022-10-13 19:21:50 +0200
committerLinus Groh <mail@linusgroh.de>2022-10-30 20:10:29 +0000
commitdd5d3e2f4fbdf3bc48faba6500793ad12ff87061 (patch)
treec23364a849467b1a98a30069a798db3b29386b2e /Userland/Libraries/LibWeb/Fetch
parentfef331a03f60359961f84b4d892575b441e1ba7d (diff)
downloadserenity-dd5d3e2f4fbdf3bc48faba6500793ad12ff87061.zip
LibWeb: Implement 'fetch controller' struct
Diffstat (limited to 'Userland/Libraries/LibWeb/Fetch')
-rw-r--r--Userland/Libraries/LibWeb/Fetch/Infrastructure/FetchController.cpp86
-rw-r--r--Userland/Libraries/LibWeb/Fetch/Infrastructure/FetchController.h71
2 files changed, 157 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/FetchController.cpp b/Userland/Libraries/LibWeb/Fetch/Infrastructure/FetchController.cpp
new file mode 100644
index 0000000000..451de59ec0
--- /dev/null
+++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/FetchController.cpp
@@ -0,0 +1,86 @@
+/*
+ * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibJS/Heap/Heap.h>
+#include <LibJS/Runtime/VM.h>
+#include <LibWeb/Fetch/Infrastructure/FetchController.h>
+#include <LibWeb/WebIDL/DOMException.h>
+
+namespace Web::Fetch::Infrastructure {
+
+FetchController::FetchController() = default;
+
+JS::NonnullGCPtr<FetchController> FetchController::create(JS::VM& vm)
+{
+ return { *vm.heap().allocate_without_realm<FetchController>() };
+}
+
+void FetchController::visit_edges(JS::Cell::Visitor& visitor)
+{
+ Base::visit_edges(visitor);
+ visitor.visit(m_full_timing_info);
+}
+
+// https://fetch.spec.whatwg.org/#finalize-and-report-timing
+void FetchController::report_timing(JS::Object const& global) const
+{
+ // 1. Assert: thisā€™s report timing steps is not null.
+ VERIFY(m_report_timing_steps.has_value());
+
+ // 2. Call thisā€™s report timing steps with global.
+ (*m_report_timing_steps)(global);
+}
+
+// https://fetch.spec.whatwg.org/#fetch-controller-process-the-next-manual-redirect
+void FetchController::process_next_manual_redirect() const
+{
+ // 1. Assert: controllerā€™s next manual redirect steps are not null.
+ VERIFY(m_next_manual_redirect_steps.has_value());
+
+ // 2. Call controllerā€™s next manual redirect steps.
+ (*m_next_manual_redirect_steps)();
+}
+
+// https://fetch.spec.whatwg.org/#extract-full-timing-info
+JS::NonnullGCPtr<FetchTimingInfo> FetchController::extract_full_timing_info() const
+{
+ // 1. Assert: thisā€™s full timing info is not null.
+ VERIFY(m_full_timing_info);
+
+ // 2. Return thisā€™s full timing info.
+ return *m_full_timing_info;
+}
+
+// https://fetch.spec.whatwg.org/#fetch-controller-abort
+void FetchController::abort(JS::VM& vm, Optional<JS::Value> error)
+{
+ auto& realm = *vm.current_realm();
+
+ // 1. Set controllerā€™s state to "aborted".
+ m_state = State::Aborted;
+
+ // 2. Let fallbackError be an "AbortError" DOMException.
+ auto fallback_error = WebIDL::AbortError::create(realm, "Fetch was aborted"sv);
+
+ // 3. Set error to fallbackError if it is not given.
+ if (!error.has_value())
+ error = fallback_error;
+
+ // FIXME: 4. Let serializedError be StructuredSerialize(error). If that threw an exception, catch it, and let serializedError be StructuredSerialize(fallbackError).
+ // FIXME: 5. Set controllerā€™s serialized abort reason to serializedError.
+ (void)error;
+}
+
+// FIXME: https://fetch.spec.whatwg.org/#deserialize-a-serialized-abort-reason
+
+// https://fetch.spec.whatwg.org/#fetch-controller-terminate
+void FetchController::terminate()
+{
+ // To terminate a fetch controller controller, set controllerā€™s state to "terminated".
+ m_state = State::Terminated;
+}
+
+}
diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/FetchController.h b/Userland/Libraries/LibWeb/Fetch/Infrastructure/FetchController.h
new file mode 100644
index 0000000000..6392804c29
--- /dev/null
+++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/FetchController.h
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <LibJS/Forward.h>
+#include <LibJS/Heap/Cell.h>
+#include <LibJS/SafeFunction.h>
+#include <LibWeb/Fetch/Infrastructure/FetchTimingInfo.h>
+
+namespace Web::Fetch::Infrastructure {
+
+// https://fetch.spec.whatwg.org/#fetch-controller
+class FetchController : public JS::Cell {
+ JS_CELL(FetchController, JS::Cell);
+
+public:
+ enum class State {
+ Ongoing,
+ Terminated,
+ Aborted,
+ };
+
+ [[nodiscard]] static JS::NonnullGCPtr<FetchController> create(JS::VM&);
+
+ void set_full_timing_info(JS::NonnullGCPtr<FetchTimingInfo> full_timing_info) { m_full_timing_info = full_timing_info; }
+ void set_report_timing_steps(JS::SafeFunction<void(JS::Object const&)> report_timing_steps) { m_report_timing_steps = move(report_timing_steps); }
+ void set_next_manual_redirect_steps(JS::SafeFunction<void()> next_manual_redirect_steps) { m_next_manual_redirect_steps = move(next_manual_redirect_steps); }
+
+ [[nodiscard]] State state() const { return m_state; }
+
+ void report_timing(JS::Object const&) const;
+ void process_next_manual_redirect() const;
+ [[nodiscard]] JS::NonnullGCPtr<FetchTimingInfo> extract_full_timing_info() const;
+ void abort(JS::VM&, Optional<JS::Value>);
+ void terminate();
+
+private:
+ FetchController();
+
+ virtual void visit_edges(JS::Cell::Visitor&) override;
+
+ // https://fetch.spec.whatwg.org/#fetch-controller-state
+ // state (default "ongoing")
+ // "ongoing", "terminated", or "aborted"
+ State m_state { State::Ongoing };
+
+ // https://fetch.spec.whatwg.org/#fetch-controller-full-timing-info
+ // full timing info (default null)
+ // Null or a fetch timing info.
+ JS::GCPtr<FetchTimingInfo> m_full_timing_info;
+
+ // https://fetch.spec.whatwg.org/#fetch-controller-report-timing-steps
+ // report timing steps (default null)
+ // Null or an algorithm accepting a global object.
+ Optional<JS::SafeFunction<void(JS::Object const&)>> m_report_timing_steps;
+
+ // https://fetch.spec.whatwg.org/#fetch-controller-report-timing-steps
+ // FIXME: serialized abort reason (default null)
+ // Null or a Record (result of StructuredSerialize).
+
+ // https://fetch.spec.whatwg.org/#fetch-controller-next-manual-redirect-steps
+ // next manual redirect steps (default null)
+ // Null or an algorithm accepting nothing.
+ Optional<JS::SafeFunction<void()>> m_next_manual_redirect_steps;
+};
+
+}