PendingResponse.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. JS_DECLARE_ALLOCATOR(PendingResponse);
  20. public:
  21. using Callback = JS::SafeFunction<void(JS::NonnullGCPtr<Infrastructure::Response>)>;
  22. [[nodiscard]] static JS::NonnullGCPtr<PendingResponse> create(JS::VM&, JS::NonnullGCPtr<Infrastructure::Request>);
  23. [[nodiscard]] static JS::NonnullGCPtr<PendingResponse> create(JS::VM&, JS::NonnullGCPtr<Infrastructure::Request>, JS::NonnullGCPtr<Infrastructure::Response>);
  24. void when_loaded(Callback);
  25. void resolve(JS::NonnullGCPtr<Infrastructure::Response>);
  26. private:
  27. PendingResponse(JS::NonnullGCPtr<Infrastructure::Request>, JS::GCPtr<Infrastructure::Response> = {});
  28. virtual void visit_edges(JS::Cell::Visitor&) override;
  29. void run_callback();
  30. Callback m_callback;
  31. JS::NonnullGCPtr<Infrastructure::Request> m_request;
  32. JS::GCPtr<Infrastructure::Response> m_response;
  33. };
  34. }