1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-16 19:35:08 +00:00

LibWeb/Fetch: Use JS::HeapFunction for callback in FetchAlgorithms

In FetchAlgorithms, it is common for callbacks to capture realms. This
can indirectly keep objects alive that hold FetchController with these
callbacks. This creates a cyclic dependency. However, when
JS::HeapFunction is used, this is not a problem, as captured by
callbacks values do not create new roots.
This commit is contained in:
Aliaksandr Kalenik 2023-08-18 18:26:08 +02:00 committed by Andreas Kling
parent 469aea5a5b
commit 9a07ac0b6a
3 changed files with 54 additions and 41 deletions

View file

@ -65,7 +65,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Infrastructure::FetchController>> fetch(JS:
auto& vm = realm.vm();
// 1. Assert: requests mode is "navigate" or processEarlyHintsResponse is null.
VERIFY(request.mode() == Infrastructure::Request::Mode::Navigate || !algorithms.process_early_hints_response().has_value());
VERIFY(request.mode() == Infrastructure::Request::Mode::Navigate || !algorithms.process_early_hints_response());
// 2. Let taskDestination be null.
JS::GCPtr<JS::Object> task_destination;
@ -608,8 +608,8 @@ WebIDL::ExceptionOr<void> fetch_response_handover(JS::Realm& realm, Infrastructu
// 2. If fetchParamss process response end-of-body is non-null, then run fetchParamss process response
// end-of-body given response.
if (fetch_params.algorithms()->process_response_end_of_body().has_value())
(*fetch_params.algorithms()->process_response_end_of_body())(response);
if (fetch_params.algorithms()->process_response_end_of_body())
(fetch_params.algorithms()->process_response_end_of_body())(response);
// 3. If fetchParamss requests initiator type is non-null and fetchParamss requests clients global
// object is fetchParamss task destination, then run fetchParamss controllers report timing steps
@ -634,9 +634,9 @@ WebIDL::ExceptionOr<void> fetch_response_handover(JS::Realm& realm, Infrastructu
// 4. If fetchParamss process response is non-null, then queue a fetch task to run fetchParamss process response
// given response, with fetchParamss task destination.
if (fetch_params.algorithms()->process_response().has_value()) {
if (fetch_params.algorithms()->process_response()) {
Infrastructure::queue_fetch_task(task_destination, [&fetch_params, &response]() {
(*fetch_params.algorithms()->process_response())(response);
fetch_params.algorithms()->process_response()(response);
});
}
@ -657,17 +657,17 @@ WebIDL::ExceptionOr<void> fetch_response_handover(JS::Realm& realm, Infrastructu
}
// 8. If fetchParamss process response consume body is non-null, then:
if (fetch_params.algorithms()->process_response_consume_body().has_value()) {
if (fetch_params.algorithms()->process_response_consume_body()) {
// 1. Let processBody given nullOrBytes be this step: run fetchParamss process response consume body given
// response and nullOrBytes.
auto process_body = [&fetch_params, &response](Variant<ByteBuffer, Empty> const& null_or_bytes) {
(*fetch_params.algorithms()->process_response_consume_body())(response, null_or_bytes);
(fetch_params.algorithms()->process_response_consume_body())(response, null_or_bytes);
};
// 2. Let processBodyError be this step: run fetchParamss process response consume body given response and
// failure.
auto process_body_error = [&fetch_params, &response](auto) {
(*fetch_params.algorithms()->process_response_consume_body())(response, Infrastructure::FetchAlgorithms::ConsumeBodyFailureTag {});
(fetch_params.algorithms()->process_response_consume_body())(response, Infrastructure::FetchAlgorithms::ConsumeBodyFailureTag {});
};
// 3. If internalResponse's body is null, then queue a fetch task to run processBody given null, with