FetchAlgorithms.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Optional.h>
  8. #include <LibJS/Heap/Cell.h>
  9. #include <LibJS/Heap/GCPtr.h>
  10. #include <LibJS/Heap/HeapFunction.h>
  11. #include <LibJS/SafeFunction.h>
  12. #include <LibWeb/Forward.h>
  13. namespace Web::Fetch::Infrastructure {
  14. // https://fetch.spec.whatwg.org/#fetch-elsewhere-fetch
  15. class FetchAlgorithms : public JS::Cell {
  16. JS_CELL(FetchAlgorithms, JS::Cell);
  17. public:
  18. struct ConsumeBodyFailureTag { };
  19. using BodyBytes = Variant<Empty, ConsumeBodyFailureTag, ByteBuffer>;
  20. using ProcessRequestBodyChunkLengthFunction = Function<void(u64)>;
  21. using ProcessRequestEndOfBodyFunction = Function<void()>;
  22. using ProcessEarlyHintsResponseFunction = Function<void(JS::NonnullGCPtr<Infrastructure::Response>)>;
  23. using ProcessResponseFunction = Function<void(JS::NonnullGCPtr<Infrastructure::Response>)>;
  24. using ProcessResponseEndOfBodyFunction = Function<void(JS::NonnullGCPtr<Infrastructure::Response>)>;
  25. using ProcessResponseConsumeBodyFunction = Function<void(JS::NonnullGCPtr<Infrastructure::Response>, BodyBytes)>;
  26. struct Input {
  27. ProcessRequestBodyChunkLengthFunction process_request_body_chunk_length;
  28. ProcessRequestEndOfBodyFunction process_request_end_of_body;
  29. ProcessEarlyHintsResponseFunction process_early_hints_response;
  30. ProcessResponseFunction process_response;
  31. ProcessResponseEndOfBodyFunction process_response_end_of_body;
  32. ProcessResponseConsumeBodyFunction process_response_consume_body;
  33. };
  34. [[nodiscard]] static JS::NonnullGCPtr<FetchAlgorithms> create(JS::VM&, Input);
  35. ProcessRequestBodyChunkLengthFunction const& process_request_body_chunk_length() const { return m_process_request_body_chunk_length->function(); }
  36. ProcessRequestEndOfBodyFunction const& process_request_end_of_body() const { return m_process_request_end_of_body->function(); }
  37. ProcessEarlyHintsResponseFunction const& process_early_hints_response() const { return m_process_early_hints_response->function(); }
  38. ProcessResponseFunction const& process_response() const { return m_process_response->function(); }
  39. ProcessResponseEndOfBodyFunction const& process_response_end_of_body() const { return m_process_response_end_of_body->function(); }
  40. ProcessResponseConsumeBodyFunction const& process_response_consume_body() const { return m_process_response_consume_body->function(); }
  41. virtual void visit_edges(JS::Cell::Visitor&) override;
  42. private:
  43. explicit FetchAlgorithms(JS::VM&, Input);
  44. JS::NonnullGCPtr<JS::HeapFunction<void(u64)>> m_process_request_body_chunk_length;
  45. JS::NonnullGCPtr<JS::HeapFunction<void()>> m_process_request_end_of_body;
  46. JS::NonnullGCPtr<JS::HeapFunction<void(JS::NonnullGCPtr<Infrastructure::Response>)>> m_process_early_hints_response;
  47. JS::NonnullGCPtr<JS::HeapFunction<void(JS::NonnullGCPtr<Infrastructure::Response>)>> m_process_response;
  48. JS::NonnullGCPtr<JS::HeapFunction<void(JS::NonnullGCPtr<Infrastructure::Response>)>> m_process_response_end_of_body;
  49. JS::NonnullGCPtr<JS::HeapFunction<void(JS::NonnullGCPtr<Infrastructure::Response>, BodyBytes)>> m_process_response_consume_body;
  50. };
  51. }