2022-10-23 22:16:14 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
#include <LibGC/Ptr.h>
|
2022-10-23 22:16:14 +01:00
|
|
|
#include <LibJS/Forward.h>
|
|
|
|
#include <LibJS/Heap/Cell.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 {
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_CELL(PendingResponse, JS::Cell);
|
|
|
|
GC_DECLARE_ALLOCATOR(PendingResponse);
|
2022-10-23 22:16:14 +01:00
|
|
|
|
|
|
|
public:
|
2024-11-15 04:01:23 +13:00
|
|
|
using Callback = Function<void(GC::Ref<Infrastructure::Response>)>;
|
2022-10-23 22:16:14 +01:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
[[nodiscard]] static GC::Ref<PendingResponse> create(JS::VM&, GC::Ref<Infrastructure::Request>);
|
|
|
|
[[nodiscard]] static GC::Ref<PendingResponse> create(JS::VM&, GC::Ref<Infrastructure::Request>, GC::Ref<Infrastructure::Response>);
|
2022-10-23 22:16:14 +01:00
|
|
|
|
|
|
|
void when_loaded(Callback);
|
2024-11-15 04:01:23 +13:00
|
|
|
void resolve(GC::Ref<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:
|
2024-11-15 04:01:23 +13:00
|
|
|
PendingResponse(GC::Ref<Infrastructure::Request>, GC::Ptr<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-11-15 04:01:23 +13:00
|
|
|
GC::Ptr<GC::Function<void(GC::Ref<Infrastructure::Response>)>> m_callback;
|
|
|
|
GC::Ref<Infrastructure::Request> m_request;
|
|
|
|
GC::Ptr<Infrastructure::Response> m_response;
|
2022-10-23 22:16:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|