FetchAlgorithms.h 4.2 KB

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