/src/serenity/Userland/Libraries/LibWeb/Fetch/Fetching/PendingResponse.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2022, Linus Groh <linusg@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <LibJS/Forward.h> |
10 | | #include <LibJS/Heap/Cell.h> |
11 | | #include <LibJS/Heap/GCPtr.h> |
12 | | #include <LibJS/SafeFunction.h> |
13 | | #include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h> |
14 | | |
15 | | namespace Web::Fetch::Fetching { |
16 | | |
17 | | // Non-standard wrapper around a possibly pending Infrastructure::Response. |
18 | | // This is needed to fit the asynchronous nature of ResourceLoader into the synchronous expectations |
19 | | // of the Fetch spec - we run 'in parallel' as a deferred_invoke(), which is still on the main thread; |
20 | | // therefore we use callbacks to run portions of the spec that require waiting for an HTTP load. |
21 | | class PendingResponse : public JS::Cell { |
22 | | JS_CELL(PendingResponse, JS::Cell); |
23 | | JS_DECLARE_ALLOCATOR(PendingResponse); |
24 | | |
25 | | public: |
26 | | using Callback = Function<void(JS::NonnullGCPtr<Infrastructure::Response>)>; |
27 | | |
28 | | [[nodiscard]] static JS::NonnullGCPtr<PendingResponse> create(JS::VM&, JS::NonnullGCPtr<Infrastructure::Request>); |
29 | | [[nodiscard]] static JS::NonnullGCPtr<PendingResponse> create(JS::VM&, JS::NonnullGCPtr<Infrastructure::Request>, JS::NonnullGCPtr<Infrastructure::Response>); |
30 | | |
31 | | void when_loaded(Callback); |
32 | | void resolve(JS::NonnullGCPtr<Infrastructure::Response>); |
33 | 0 | bool is_resolved() const { return m_response != nullptr; } |
34 | | |
35 | | private: |
36 | | PendingResponse(JS::NonnullGCPtr<Infrastructure::Request>, JS::GCPtr<Infrastructure::Response> = {}); |
37 | | |
38 | | virtual void visit_edges(JS::Cell::Visitor&) override; |
39 | | |
40 | | void run_callback(); |
41 | | |
42 | | JS::GCPtr<JS::HeapFunction<void(JS::NonnullGCPtr<Infrastructure::Response>)>> m_callback; |
43 | | JS::NonnullGCPtr<Infrastructure::Request> m_request; |
44 | | JS::GCPtr<Infrastructure::Response> m_response; |
45 | | }; |
46 | | |
47 | | } |