2022-10-23 18:16:14 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
#include <LibGC/Heap.h>
|
2022-10-23 18:16:14 -03:00
|
|
|
#include <LibJS/Runtime/VM.h>
|
|
|
|
|
#include <LibWeb/Fetch/Fetching/PendingResponse.h>
|
2022-11-01 16:35:38 -03:00
|
|
|
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
|
2022-10-23 18:16:14 -03:00
|
|
|
#include <LibWeb/Platform/EventLoopPlugin.h>
|
|
|
|
|
|
|
|
|
|
namespace Web::Fetch::Fetching {
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
GC_DEFINE_ALLOCATOR(PendingResponse);
|
2023-11-19 15:47:52 -03:00
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
GC::Ref<PendingResponse> PendingResponse::create(JS::VM& vm, GC::Ref<Infrastructure::Request> request)
|
2022-10-23 18:16:14 -03:00
|
|
|
{
|
2024-11-13 14:13:46 -03:00
|
|
|
return vm.heap().allocate<PendingResponse>(request);
|
2022-10-23 18:16:14 -03:00
|
|
|
}
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
GC::Ref<PendingResponse> PendingResponse::create(JS::VM& vm, GC::Ref<Infrastructure::Request> request, GC::Ref<Infrastructure::Response> response)
|
2022-10-23 18:16:14 -03:00
|
|
|
{
|
2024-11-13 14:13:46 -03:00
|
|
|
return vm.heap().allocate<PendingResponse>(request, response);
|
2022-10-23 18:16:14 -03:00
|
|
|
}
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
PendingResponse::PendingResponse(GC::Ref<Infrastructure::Request> request, GC::Ptr<Infrastructure::Response> response)
|
2022-11-01 16:35:38 -03:00
|
|
|
: m_request(request)
|
|
|
|
|
, m_response(response)
|
2022-10-23 18:16:14 -03:00
|
|
|
{
|
2022-11-01 16:35:38 -03:00
|
|
|
m_request->add_pending_response({}, *this);
|
2022-10-23 18:16:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PendingResponse::visit_edges(JS::Cell::Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
2024-04-03 06:49:50 -03:00
|
|
|
visitor.visit(m_callback);
|
2022-11-01 16:35:38 -03:00
|
|
|
visitor.visit(m_request);
|
2022-10-23 18:16:14 -03:00
|
|
|
visitor.visit(m_response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PendingResponse::when_loaded(Callback callback)
|
|
|
|
|
{
|
|
|
|
|
VERIFY(!m_callback);
|
2024-11-14 12:01:23 -03:00
|
|
|
m_callback = GC::create_function(heap(), move(callback));
|
2022-10-23 18:16:14 -03:00
|
|
|
if (m_response)
|
|
|
|
|
run_callback();
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
void PendingResponse::resolve(GC::Ref<Infrastructure::Response> response)
|
2022-10-23 18:16:14 -03:00
|
|
|
{
|
|
|
|
|
VERIFY(!m_response);
|
|
|
|
|
m_response = response;
|
|
|
|
|
if (m_callback)
|
|
|
|
|
run_callback();
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-25 14:44:31 -03:00
|
|
|
void PendingResponse::run_callback()
|
2022-10-23 18:16:14 -03:00
|
|
|
{
|
|
|
|
|
VERIFY(m_callback);
|
|
|
|
|
VERIFY(m_response);
|
2024-11-14 12:01:23 -03:00
|
|
|
Platform::EventLoopPlugin::the().deferred_invoke(GC::create_function(heap(), [this] {
|
2024-04-03 06:49:50 -03:00
|
|
|
VERIFY(m_callback);
|
|
|
|
|
VERIFY(m_response);
|
|
|
|
|
m_callback->function()(*m_response);
|
2023-04-21 08:13:04 -03:00
|
|
|
m_request->remove_pending_response({}, *this);
|
2024-10-30 10:39:29 -03:00
|
|
|
}));
|
2022-10-23 18:16:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|