/src/serenity/Userland/Libraries/LibWeb/Fetch/Infrastructure/FetchController.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2022, Linus Groh <linusg@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <LibJS/Heap/Heap.h> |
8 | | #include <LibJS/Runtime/VM.h> |
9 | | #include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h> |
10 | | #include <LibWeb/Fetch/Infrastructure/FetchController.h> |
11 | | #include <LibWeb/Fetch/Infrastructure/FetchParams.h> |
12 | | #include <LibWeb/HTML/EventLoop/EventLoop.h> |
13 | | #include <LibWeb/WebIDL/DOMException.h> |
14 | | |
15 | | namespace Web::Fetch::Infrastructure { |
16 | | |
17 | | JS_DEFINE_ALLOCATOR(FetchController); |
18 | | |
19 | 0 | FetchController::FetchController() = default; |
20 | | |
21 | | JS::NonnullGCPtr<FetchController> FetchController::create(JS::VM& vm) |
22 | 0 | { |
23 | 0 | return vm.heap().allocate_without_realm<FetchController>(); |
24 | 0 | } |
25 | | |
26 | | void FetchController::visit_edges(JS::Cell::Visitor& visitor) |
27 | 0 | { |
28 | 0 | Base::visit_edges(visitor); |
29 | 0 | visitor.visit(m_full_timing_info); |
30 | 0 | visitor.visit(m_report_timing_steps); |
31 | 0 | visitor.visit(m_next_manual_redirect_steps); |
32 | 0 | visitor.visit(m_fetch_params); |
33 | 0 | } |
34 | | |
35 | | void FetchController::set_report_timing_steps(Function<void(JS::Object const&)> report_timing_steps) |
36 | 0 | { |
37 | 0 | m_report_timing_steps = JS::create_heap_function(vm().heap(), move(report_timing_steps)); |
38 | 0 | } |
39 | | |
40 | | void FetchController::set_next_manual_redirect_steps(Function<void()> next_manual_redirect_steps) |
41 | 0 | { |
42 | 0 | m_next_manual_redirect_steps = JS::create_heap_function(vm().heap(), move(next_manual_redirect_steps)); |
43 | 0 | } |
44 | | |
45 | | // https://fetch.spec.whatwg.org/#finalize-and-report-timing |
46 | | void FetchController::report_timing(JS::Object const& global) const |
47 | 0 | { |
48 | | // 1. Assert: this’s report timing steps is not null. |
49 | 0 | VERIFY(m_report_timing_steps); |
50 | | |
51 | | // 2. Call this’s report timing steps with global. |
52 | 0 | m_report_timing_steps->function()(global); |
53 | 0 | } |
54 | | |
55 | | // https://fetch.spec.whatwg.org/#fetch-controller-process-the-next-manual-redirect |
56 | | void FetchController::process_next_manual_redirect() const |
57 | 0 | { |
58 | | // 1. Assert: controller’s next manual redirect steps are not null. |
59 | 0 | VERIFY(m_next_manual_redirect_steps); |
60 | | |
61 | | // 2. Call controller’s next manual redirect steps. |
62 | 0 | m_next_manual_redirect_steps->function()(); |
63 | 0 | } |
64 | | |
65 | | // https://fetch.spec.whatwg.org/#extract-full-timing-info |
66 | | JS::NonnullGCPtr<FetchTimingInfo> FetchController::extract_full_timing_info() const |
67 | 0 | { |
68 | | // 1. Assert: this’s full timing info is not null. |
69 | 0 | VERIFY(m_full_timing_info); |
70 | | |
71 | | // 2. Return this’s full timing info. |
72 | 0 | return *m_full_timing_info; |
73 | 0 | } |
74 | | |
75 | | // https://fetch.spec.whatwg.org/#fetch-controller-abort |
76 | | void FetchController::abort(JS::Realm& realm, Optional<JS::Value> error) |
77 | 0 | { |
78 | | // 1. Set controller’s state to "aborted". |
79 | 0 | m_state = State::Aborted; |
80 | | |
81 | | // 2. Let fallbackError be an "AbortError" DOMException. |
82 | 0 | auto fallback_error = WebIDL::AbortError::create(realm, "Fetch was aborted"_string); |
83 | | |
84 | | // 3. Set error to fallbackError if it is not given. |
85 | 0 | if (!error.has_value()) |
86 | 0 | error = fallback_error; |
87 | | |
88 | | // FIXME: 4. Let serializedError be StructuredSerialize(error). If that threw an exception, catch it, and let serializedError be StructuredSerialize(fallbackError). |
89 | | // FIXME: 5. Set controller’s serialized abort reason to serializedError. |
90 | 0 | (void)error; |
91 | 0 | } |
92 | | |
93 | | // FIXME: https://fetch.spec.whatwg.org/#deserialize-a-serialized-abort-reason |
94 | | |
95 | | // https://fetch.spec.whatwg.org/#fetch-controller-terminate |
96 | | void FetchController::terminate() |
97 | 0 | { |
98 | | // To terminate a fetch controller controller, set controller’s state to "terminated". |
99 | 0 | m_state = State::Terminated; |
100 | 0 | } |
101 | | |
102 | | void FetchController::stop_fetch() |
103 | 0 | { |
104 | 0 | auto& vm = this->vm(); |
105 | | |
106 | | // AD-HOC: Some HTML elements need to stop an ongoing fetching process without causing any network error to be raised |
107 | | // (which abort() and terminate() will both do). This is tricky because the fetch process runs across several |
108 | | // nested Platform::EventLoopPlugin::deferred_invoke() invocations. For now, we "stop" the fetch process by |
109 | | // cancelling any queued fetch tasks and then ignoring any callbacks. |
110 | 0 | auto ongoing_fetch_tasks = move(m_ongoing_fetch_tasks); |
111 | |
|
112 | 0 | HTML::main_thread_event_loop().task_queue().remove_tasks_matching([&](auto const& task) { |
113 | 0 | return ongoing_fetch_tasks.remove_all_matching([&](u64, HTML::TaskID task_id) { |
114 | 0 | return task.id() == task_id; |
115 | 0 | }); |
116 | 0 | }); |
117 | |
|
118 | 0 | if (m_fetch_params) { |
119 | 0 | auto fetch_algorithms = FetchAlgorithms::create(vm, {}); |
120 | 0 | m_fetch_params->set_algorithms(fetch_algorithms); |
121 | 0 | } |
122 | 0 | } |
123 | | |
124 | | void FetchController::fetch_task_queued(u64 fetch_task_id, HTML::TaskID event_id) |
125 | 0 | { |
126 | 0 | m_ongoing_fetch_tasks.set(fetch_task_id, event_id); |
127 | 0 | } |
128 | | |
129 | | void FetchController::fetch_task_complete(u64 fetch_task_id) |
130 | 0 | { |
131 | 0 | m_ongoing_fetch_tasks.remove(fetch_task_id); |
132 | 0 | } |
133 | | |
134 | | } |