1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-18 20:05:06 +00:00
serenity/Userland/Libraries/LibWeb/Fetch/Fetching/PendingResponse.h
Linus Groh 216f68c566 LibWeb: Register PendingResponse with a Request to keep it alive
This was an oversight from when I converted PendingResponse and various
other classes from being ref-counted to GC-allocated last minute - no
one takes care to keep all of them alive. Some are on the stack, and
some might be captured in another PendingResponse's JS::SafeFunction,
but ultimately, we need a better solution.
Since a PendingResponse is *always* the result of someone having created
a Request, let's just let that keep a list of each PendingResponse that
has been created for it, and visit them until they are resolved. After
that, they can be GC'd with no complaints.
2022-11-01 20:14:12 +00:00

44 lines
1.5 KiB
C++

/*
* 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/HTTP/Responses.h>
namespace Web::Fetch::Fetching {
// Non-standard wrapper around a possibly pending Infrastructure::Response.
// This is needed to fit the asynchronous nature of ResourceLoader into the synchronous expectations
// of the Fetch spec - we run 'in parallel' as a deferred_invoke(), which is still on the main thread;
// therefore we use callbacks to run portions of the spec that require waiting for an HTTP load.
class PendingResponse : public JS::Cell {
JS_CELL(PendingResponse, JS::Cell);
public:
using Callback = JS::SafeFunction<void(JS::NonnullGCPtr<Infrastructure::Response>)>;
[[nodiscard]] static JS::NonnullGCPtr<PendingResponse> create(JS::VM&, JS::NonnullGCPtr<Infrastructure::Request>);
[[nodiscard]] static JS::NonnullGCPtr<PendingResponse> create(JS::VM&, JS::NonnullGCPtr<Infrastructure::Request>, JS::NonnullGCPtr<Infrastructure::Response>);
void when_loaded(Callback);
void resolve(JS::NonnullGCPtr<Infrastructure::Response>);
private:
PendingResponse(JS::NonnullGCPtr<Infrastructure::Request>, JS::GCPtr<Infrastructure::Response> = {});
virtual void visit_edges(JS::Cell::Visitor&) override;
void run_callback() const;
Callback m_callback;
JS::NonnullGCPtr<Infrastructure::Request> m_request;
JS::GCPtr<Infrastructure::Response> m_response;
};
}