ladybird/Userland/Libraries/LibWeb/Fetch/Infrastructure/FetchController.cpp
Timothy Flynn 8d4d01d99a LibWeb: Invent a method to stop an in-progress fetch without errors
The HTMLMediaElement will need to stop fetching processes when its load
algorithm is invoked while a fetch is ongoing. We don't have a way to
really stop the process, due to the way it runs on nested deferred task
invocations. So for now, this swaps the fetch callbacks (e.g. to process
a fetch response) with empty callbacks.
2023-04-20 06:19:41 +02:00

101 lines
3.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* 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/FetchAlgorithms.h>
#include <LibWeb/Fetch/Infrastructure/FetchController.h>
#include <LibWeb/Fetch/Infrastructure/FetchParams.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);
visitor.visit(m_fetch_params);
}
// https://fetch.spec.whatwg.org/#finalize-and-report-timing
void FetchController::report_timing(JS::Object const& global) const
{
// 1. Assert: thiss report timing steps is not null.
VERIFY(m_report_timing_steps.has_value());
// 2. Call thiss 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: controllers next manual redirect steps are not null.
VERIFY(m_next_manual_redirect_steps.has_value());
// 2. Call controllers 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: thiss full timing info is not null.
VERIFY(m_full_timing_info);
// 2. Return thiss full timing info.
return *m_full_timing_info;
}
// https://fetch.spec.whatwg.org/#fetch-controller-abort
void FetchController::abort(JS::Realm& realm, Optional<JS::Value> error)
{
// 1. Set controllers 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 controllers 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 controllers state to "terminated".
m_state = State::Terminated;
}
void FetchController::stop_fetch()
{
auto& vm = this->vm();
// AD-HOC: Some HTML elements need to stop an ongoing fetching process without causing any network error to be raised
// (which abort() and terminate() will both do). This is tricky because the fetch process runs across several
// nested Platform::EventLoopPlugin::deferred_invoke() invocations. For now, we "stop" the fetch process by
// ignoring any callbacks.
if (m_fetch_params) {
auto fetch_algorithms = FetchAlgorithms::create(vm, {});
m_fetch_params->set_algorithms(fetch_algorithms);
}
}
}