FetchAlgorithms.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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/SafeFunction.h>
  11. #include <LibWeb/Forward.h>
  12. namespace Web::Fetch::Infrastructure {
  13. // https://fetch.spec.whatwg.org/#fetch-elsewhere-fetch
  14. class FetchAlgorithms : public JS::Cell {
  15. JS_CELL(FetchAlgorithms, JS::Cell);
  16. public:
  17. struct ConsumeBodyFailureTag { };
  18. using BodyBytes = Variant<Empty, ConsumeBodyFailureTag, ByteBuffer>;
  19. using ProcessRequestBodyChunkLengthFunction = JS::SafeFunction<void(u64)>;
  20. using ProcessRequestEndOfBodyFunction = JS::SafeFunction<void()>;
  21. using ProcessEarlyHintsResponseFunction = JS::SafeFunction<void(JS::NonnullGCPtr<Infrastructure::Response>)>;
  22. using ProcessResponseFunction = JS::SafeFunction<void(JS::NonnullGCPtr<Infrastructure::Response>)>;
  23. using ProcessResponseEndOfBodyFunction = JS::SafeFunction<void(JS::NonnullGCPtr<Infrastructure::Response>)>;
  24. using ProcessResponseConsumeBodyFunction = JS::SafeFunction<void(JS::NonnullGCPtr<Infrastructure::Response>, BodyBytes)>;
  25. struct Input {
  26. Optional<ProcessRequestBodyChunkLengthFunction> process_request_body_chunk_length;
  27. Optional<ProcessRequestEndOfBodyFunction> process_request_end_of_body;
  28. Optional<ProcessEarlyHintsResponseFunction> process_early_hints_response;
  29. Optional<ProcessResponseFunction> process_response;
  30. Optional<ProcessResponseEndOfBodyFunction> process_response_end_of_body;
  31. Optional<ProcessResponseConsumeBodyFunction> process_response_consume_body;
  32. };
  33. [[nodiscard]] static JS::NonnullGCPtr<FetchAlgorithms> create(JS::VM&, Input);
  34. Optional<ProcessRequestBodyChunkLengthFunction> const& process_request_body_chunk_length() const { return m_process_request_body_chunk_length; }
  35. Optional<ProcessRequestEndOfBodyFunction> const& process_request_end_of_body() const { return m_process_request_end_of_body; }
  36. Optional<ProcessEarlyHintsResponseFunction> const& process_early_hints_response() const { return m_process_early_hints_response; }
  37. Optional<ProcessResponseFunction> const& process_response() const { return m_process_response; }
  38. Optional<ProcessResponseEndOfBodyFunction> const& process_response_end_of_body() const { return m_process_response_end_of_body; }
  39. Optional<ProcessResponseConsumeBodyFunction> const& process_response_consume_body() const { return m_process_response_consume_body; }
  40. private:
  41. explicit FetchAlgorithms(Input);
  42. Optional<ProcessRequestBodyChunkLengthFunction> m_process_request_body_chunk_length;
  43. Optional<ProcessRequestEndOfBodyFunction> m_process_request_end_of_body;
  44. Optional<ProcessEarlyHintsResponseFunction> m_process_early_hints_response;
  45. Optional<ProcessResponseFunction> m_process_response;
  46. Optional<ProcessResponseEndOfBodyFunction> m_process_response_end_of_body;
  47. Optional<ProcessResponseConsumeBodyFunction> m_process_response_consume_body;
  48. };
  49. }