PendingResponse.h 1.6 KB

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