1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 08:35:09 +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

@ -9,6 +9,7 @@
#include <AK/Optional.h>
#include <LibJS/Heap/Cell.h>
#include <LibJS/Heap/GCPtr.h>
#include <LibJS/Heap/HeapFunction.h>
#include <LibJS/SafeFunction.h>
#include <LibWeb/Forward.h>
@ -22,40 +23,42 @@ public:
struct ConsumeBodyFailureTag { };
using BodyBytes = Variant<Empty, ConsumeBodyFailureTag, ByteBuffer>;
using ProcessRequestBodyChunkLengthFunction = JS::SafeFunction<void(u64)>;
using ProcessRequestEndOfBodyFunction = JS::SafeFunction<void()>;
using ProcessEarlyHintsResponseFunction = JS::SafeFunction<void(JS::NonnullGCPtr<Infrastructure::Response>)>;
using ProcessResponseFunction = JS::SafeFunction<void(JS::NonnullGCPtr<Infrastructure::Response>)>;
using ProcessResponseEndOfBodyFunction = JS::SafeFunction<void(JS::NonnullGCPtr<Infrastructure::Response>)>;
using ProcessResponseConsumeBodyFunction = JS::SafeFunction<void(JS::NonnullGCPtr<Infrastructure::Response>, BodyBytes)>;
using ProcessRequestBodyChunkLengthFunction = Function<void(u64)>;
using ProcessRequestEndOfBodyFunction = Function<void()>;
using ProcessEarlyHintsResponseFunction = Function<void(JS::NonnullGCPtr<Infrastructure::Response>)>;
using ProcessResponseFunction = Function<void(JS::NonnullGCPtr<Infrastructure::Response>)>;
using ProcessResponseEndOfBodyFunction = Function<void(JS::NonnullGCPtr<Infrastructure::Response>)>;
using ProcessResponseConsumeBodyFunction = Function<void(JS::NonnullGCPtr<Infrastructure::Response>, BodyBytes)>;
struct Input {
Optional<ProcessRequestBodyChunkLengthFunction> process_request_body_chunk_length;
Optional<ProcessRequestEndOfBodyFunction> process_request_end_of_body;
Optional<ProcessEarlyHintsResponseFunction> process_early_hints_response;
Optional<ProcessResponseFunction> process_response;
Optional<ProcessResponseEndOfBodyFunction> process_response_end_of_body;
Optional<ProcessResponseConsumeBodyFunction> process_response_consume_body;
ProcessRequestBodyChunkLengthFunction process_request_body_chunk_length;
ProcessRequestEndOfBodyFunction process_request_end_of_body;
ProcessEarlyHintsResponseFunction process_early_hints_response;
ProcessResponseFunction process_response;
ProcessResponseEndOfBodyFunction process_response_end_of_body;
ProcessResponseConsumeBodyFunction process_response_consume_body;
};
[[nodiscard]] static JS::NonnullGCPtr<FetchAlgorithms> create(JS::VM&, Input);
Optional<ProcessRequestBodyChunkLengthFunction> const& process_request_body_chunk_length() const { return m_process_request_body_chunk_length; }
Optional<ProcessRequestEndOfBodyFunction> const& process_request_end_of_body() const { return m_process_request_end_of_body; }
Optional<ProcessEarlyHintsResponseFunction> const& process_early_hints_response() const { return m_process_early_hints_response; }
Optional<ProcessResponseFunction> const& process_response() const { return m_process_response; }
Optional<ProcessResponseEndOfBodyFunction> const& process_response_end_of_body() const { return m_process_response_end_of_body; }
Optional<ProcessResponseConsumeBodyFunction> const& process_response_consume_body() const { return m_process_response_consume_body; }
ProcessRequestBodyChunkLengthFunction const& process_request_body_chunk_length() const { return m_process_request_body_chunk_length->function(); }
ProcessRequestEndOfBodyFunction const& process_request_end_of_body() const { return m_process_request_end_of_body->function(); }
ProcessEarlyHintsResponseFunction const& process_early_hints_response() const { return m_process_early_hints_response->function(); }
ProcessResponseFunction const& process_response() const { return m_process_response->function(); }
ProcessResponseEndOfBodyFunction const& process_response_end_of_body() const { return m_process_response_end_of_body->function(); }
ProcessResponseConsumeBodyFunction const& process_response_consume_body() const { return m_process_response_consume_body->function(); }
virtual void visit_edges(JS::Cell::Visitor&) override;
private:
explicit FetchAlgorithms(Input);
explicit FetchAlgorithms(JS::VM&, Input);
Optional<ProcessRequestBodyChunkLengthFunction> m_process_request_body_chunk_length;
Optional<ProcessRequestEndOfBodyFunction> m_process_request_end_of_body;
Optional<ProcessEarlyHintsResponseFunction> m_process_early_hints_response;
Optional<ProcessResponseFunction> m_process_response;
Optional<ProcessResponseEndOfBodyFunction> m_process_response_end_of_body;
Optional<ProcessResponseConsumeBodyFunction> m_process_response_consume_body;
JS::NonnullGCPtr<JS::HeapFunction<void(u64)>> m_process_request_body_chunk_length;
JS::NonnullGCPtr<JS::HeapFunction<void()>> m_process_request_end_of_body;
JS::NonnullGCPtr<JS::HeapFunction<void(JS::NonnullGCPtr<Infrastructure::Response>)>> m_process_early_hints_response;
JS::NonnullGCPtr<JS::HeapFunction<void(JS::NonnullGCPtr<Infrastructure::Response>)>> m_process_response;
JS::NonnullGCPtr<JS::HeapFunction<void(JS::NonnullGCPtr<Infrastructure::Response>)>> m_process_response_end_of_body;
JS::NonnullGCPtr<JS::HeapFunction<void(JS::NonnullGCPtr<Infrastructure::Response>, BodyBytes)>> m_process_response_consume_body;
};
}