2022-10-23 22:16:14 +01:00
|
|
|
/*
|
|
|
|
|
* 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>
|
2023-02-10 08:29:14 -05:00
|
|
|
#include <LibJS/Heap/GCPtr.h>
|
2022-10-23 22:16:14 +01:00
|
|
|
#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);
|
2023-11-19 19:47:52 +01:00
|
|
|
JS_DECLARE_ALLOCATOR(PendingResponse);
|
2022-10-23 22:16:14 +01:00
|
|
|
|
|
|
|
|
public:
|
2024-04-03 11:49:50 +02:00
|
|
|
using Callback = Function<void(JS::NonnullGCPtr<Infrastructure::Response>)>;
|
2022-10-23 22:16:14 +01:00
|
|
|
|
2022-11-01 19:35:38 +00:00
|
|
|
[[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>);
|
2022-10-23 22:16:14 +01:00
|
|
|
|
|
|
|
|
void when_loaded(Callback);
|
|
|
|
|
void resolve(JS::NonnullGCPtr<Infrastructure::Response>);
|
2024-05-26 08:03:29 -04:00
|
|
|
bool is_resolved() const { return m_response != nullptr; }
|
2022-10-23 22:16:14 +01:00
|
|
|
|
|
|
|
|
private:
|
2022-11-01 19:35:38 +00:00
|
|
|
PendingResponse(JS::NonnullGCPtr<Infrastructure::Request>, JS::GCPtr<Infrastructure::Response> = {});
|
2022-10-23 22:16:14 +01:00
|
|
|
|
|
|
|
|
virtual void visit_edges(JS::Cell::Visitor&) override;
|
|
|
|
|
|
2023-02-25 10:44:31 -07:00
|
|
|
void run_callback();
|
2022-10-23 22:16:14 +01:00
|
|
|
|
2024-04-03 11:49:50 +02:00
|
|
|
JS::GCPtr<JS::HeapFunction<void(JS::NonnullGCPtr<Infrastructure::Response>)>> m_callback;
|
2022-11-01 19:35:38 +00:00
|
|
|
JS::NonnullGCPtr<Infrastructure::Request> m_request;
|
2022-10-23 22:16:14 +01:00
|
|
|
JS::GCPtr<Infrastructure::Response> m_response;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|