/src/serenity/Userland/Libraries/LibWeb/Fetch/Fetching/PendingResponse.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2022, Linus Groh <linusg@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <LibJS/Heap/Heap.h> |
8 | | #include <LibJS/Runtime/VM.h> |
9 | | #include <LibWeb/Fetch/Fetching/PendingResponse.h> |
10 | | #include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h> |
11 | | #include <LibWeb/Platform/EventLoopPlugin.h> |
12 | | |
13 | | namespace Web::Fetch::Fetching { |
14 | | |
15 | | JS_DEFINE_ALLOCATOR(PendingResponse); |
16 | | |
17 | | JS::NonnullGCPtr<PendingResponse> PendingResponse::create(JS::VM& vm, JS::NonnullGCPtr<Infrastructure::Request> request) |
18 | 0 | { |
19 | 0 | return vm.heap().allocate_without_realm<PendingResponse>(request); |
20 | 0 | } |
21 | | |
22 | | JS::NonnullGCPtr<PendingResponse> PendingResponse::create(JS::VM& vm, JS::NonnullGCPtr<Infrastructure::Request> request, JS::NonnullGCPtr<Infrastructure::Response> response) |
23 | 0 | { |
24 | 0 | return vm.heap().allocate_without_realm<PendingResponse>(request, response); |
25 | 0 | } |
26 | | |
27 | | PendingResponse::PendingResponse(JS::NonnullGCPtr<Infrastructure::Request> request, JS::GCPtr<Infrastructure::Response> response) |
28 | 0 | : m_request(request) |
29 | 0 | , m_response(response) |
30 | 0 | { |
31 | 0 | m_request->add_pending_response({}, *this); |
32 | 0 | } |
33 | | |
34 | | void PendingResponse::visit_edges(JS::Cell::Visitor& visitor) |
35 | 0 | { |
36 | 0 | Base::visit_edges(visitor); |
37 | 0 | visitor.visit(m_callback); |
38 | 0 | visitor.visit(m_request); |
39 | 0 | visitor.visit(m_response); |
40 | 0 | } |
41 | | |
42 | | void PendingResponse::when_loaded(Callback callback) |
43 | 0 | { |
44 | 0 | VERIFY(!m_callback); |
45 | 0 | m_callback = JS::create_heap_function(heap(), move(callback)); |
46 | 0 | if (m_response) |
47 | 0 | run_callback(); |
48 | 0 | } |
49 | | |
50 | | void PendingResponse::resolve(JS::NonnullGCPtr<Infrastructure::Response> response) |
51 | 0 | { |
52 | 0 | VERIFY(!m_response); |
53 | 0 | m_response = response; |
54 | 0 | if (m_callback) |
55 | 0 | run_callback(); |
56 | 0 | } |
57 | | |
58 | | void PendingResponse::run_callback() |
59 | 0 | { |
60 | 0 | VERIFY(m_callback); |
61 | 0 | VERIFY(m_response); |
62 | 0 | Platform::EventLoopPlugin::the().deferred_invoke([this] { |
63 | 0 | VERIFY(m_callback); |
64 | 0 | VERIFY(m_response); |
65 | 0 | m_callback->function()(*m_response); |
66 | 0 | m_request->remove_pending_response({}, *this); |
67 | 0 | }); |
68 | 0 | } |
69 | | |
70 | | } |