FetchAlgorithms.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. JS_DECLARE_ALLOCATOR(FetchAlgorithms);
  18. public:
  19. struct ConsumeBodyFailureTag { };
  20. using BodyBytes = Variant<Empty, ConsumeBodyFailureTag, ByteBuffer>;
  21. using ProcessRequestBodyChunkLengthFunction = Function<void(u64)>;
  22. using ProcessRequestEndOfBodyFunction = Function<void()>;
  23. using ProcessEarlyHintsResponseFunction = Function<void(JS::NonnullGCPtr<Infrastructure::Response>)>;
  24. using ProcessResponseFunction = Function<void(JS::NonnullGCPtr<Infrastructure::Response>)>;
  25. using ProcessResponseEndOfBodyFunction = Function<void(JS::NonnullGCPtr<Infrastructure::Response>)>;
  26. using ProcessResponseConsumeBodyFunction = Function<void(JS::NonnullGCPtr<Infrastructure::Response>, BodyBytes)>;
  27. using ProcessRequestBodyChunkLengthHeapFunction = JS::NonnullGCPtr<JS::HeapFunction<ProcessRequestBodyChunkLengthFunction::FunctionType>>;
  28. using ProcessRequestEndOfBodyHeapFunction = JS::NonnullGCPtr<JS::HeapFunction<ProcessRequestEndOfBodyFunction::FunctionType>>;
  29. using ProcessEarlyHintsResponseHeapFunction = JS::NonnullGCPtr<JS::HeapFunction<ProcessEarlyHintsResponseFunction::FunctionType>>;
  30. using ProcessResponseHeapFunction = JS::NonnullGCPtr<JS::HeapFunction<ProcessResponseFunction::FunctionType>>;
  31. using ProcessResponseEndOfBodyHeapFunction = JS::NonnullGCPtr<JS::HeapFunction<ProcessResponseEndOfBodyFunction::FunctionType>>;
  32. using ProcessResponseConsumeBodyHeapFunction = JS::NonnullGCPtr<JS::HeapFunction<ProcessResponseConsumeBodyFunction::FunctionType>>;
  33. struct Input {
  34. ProcessRequestBodyChunkLengthFunction process_request_body_chunk_length;
  35. ProcessRequestEndOfBodyFunction process_request_end_of_body;
  36. ProcessEarlyHintsResponseFunction process_early_hints_response;
  37. ProcessResponseFunction process_response;
  38. ProcessResponseEndOfBodyFunction process_response_end_of_body;
  39. ProcessResponseConsumeBodyFunction process_response_consume_body;
  40. };
  41. [[nodiscard]] static JS::NonnullGCPtr<FetchAlgorithms> create(JS::VM&, Input);
  42. ProcessRequestBodyChunkLengthFunction const& process_request_body_chunk_length() const { return m_process_request_body_chunk_length->function(); }
  43. ProcessRequestEndOfBodyFunction const& process_request_end_of_body() const { return m_process_request_end_of_body->function(); }
  44. ProcessEarlyHintsResponseFunction const& process_early_hints_response() const { return m_process_early_hints_response->function(); }
  45. ProcessResponseFunction const& process_response() const { return m_process_response->function(); }
  46. ProcessResponseEndOfBodyFunction const& process_response_end_of_body() const { return m_process_response_end_of_body->function(); }
  47. ProcessResponseConsumeBodyFunction const& process_response_consume_body() const { return m_process_response_consume_body->function(); }
  48. virtual void visit_edges(JS::Cell::Visitor&) override;
  49. private:
  50. explicit FetchAlgorithms(
  51. ProcessRequestBodyChunkLengthHeapFunction process_request_body_chunk_length,
  52. ProcessRequestEndOfBodyHeapFunction process_request_end_of_body,
  53. ProcessEarlyHintsResponseHeapFunction process_early_hints_response,
  54. ProcessResponseHeapFunction process_response,
  55. ProcessResponseEndOfBodyHeapFunction process_response_end_of_body,
  56. ProcessResponseConsumeBodyHeapFunction process_response_consume_body);
  57. ProcessRequestBodyChunkLengthHeapFunction m_process_request_body_chunk_length;
  58. ProcessRequestEndOfBodyHeapFunction m_process_request_end_of_body;
  59. ProcessEarlyHintsResponseHeapFunction m_process_early_hints_response;
  60. ProcessResponseHeapFunction m_process_response;
  61. ProcessResponseEndOfBodyHeapFunction m_process_response_end_of_body;
  62. ProcessResponseConsumeBodyHeapFunction m_process_response_consume_body;
  63. };
  64. }