FetchAlgorithms.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 ProcessRequestBodyChunkLengthFunction = JS::SafeFunction<void(u64)>;
  19. using ProcessRequestEndOfBodyFunction = JS::SafeFunction<void()>;
  20. using ProcessEarlyHintsResponseFunction = JS::SafeFunction<void(JS::NonnullGCPtr<Infrastructure::Response>)>;
  21. using ProcessResponseFunction = JS::SafeFunction<void(JS::NonnullGCPtr<Infrastructure::Response>)>;
  22. using ProcessResponseEndOfBodyFunction = JS::SafeFunction<void(JS::NonnullGCPtr<Infrastructure::Response>)>;
  23. using ProcessResponseConsumeBodyFunction = JS::SafeFunction<void(JS::NonnullGCPtr<Infrastructure::Response>, Variant<Empty, ConsumeBodyFailureTag, ByteBuffer>)>;
  24. struct Input {
  25. Optional<ProcessRequestBodyChunkLengthFunction> process_request_body_chunk_length;
  26. Optional<ProcessRequestEndOfBodyFunction> process_request_end_of_body;
  27. Optional<ProcessEarlyHintsResponseFunction> process_early_hints_response;
  28. Optional<ProcessResponseFunction> process_response;
  29. Optional<ProcessResponseEndOfBodyFunction> process_response_end_of_body;
  30. Optional<ProcessResponseConsumeBodyFunction> process_response_consume_body;
  31. };
  32. [[nodiscard]] static JS::NonnullGCPtr<FetchAlgorithms> create(JS::VM&, Input);
  33. Optional<ProcessRequestBodyChunkLengthFunction> const& process_request_body_chunk_length() const { return m_process_request_body_chunk_length; }
  34. Optional<ProcessRequestEndOfBodyFunction> const& process_request_end_of_body() const { return m_process_request_end_of_body; }
  35. Optional<ProcessEarlyHintsResponseFunction> const& process_early_hints_response() const { return m_process_early_hints_response; }
  36. Optional<ProcessResponseFunction> const& process_response() const { return m_process_response; }
  37. Optional<ProcessResponseEndOfBodyFunction> const& process_response_end_of_body() const { return m_process_response_end_of_body; }
  38. Optional<ProcessResponseConsumeBodyFunction> const& process_response_consume_body() const { return m_process_response_consume_body; }
  39. private:
  40. explicit FetchAlgorithms(Input);
  41. Optional<ProcessRequestBodyChunkLengthFunction> m_process_request_body_chunk_length;
  42. Optional<ProcessRequestEndOfBodyFunction> m_process_request_end_of_body;
  43. Optional<ProcessEarlyHintsResponseFunction> m_process_early_hints_response;
  44. Optional<ProcessResponseFunction> m_process_response;
  45. Optional<ProcessResponseEndOfBodyFunction> m_process_response_end_of_body;
  46. Optional<ProcessResponseConsumeBodyFunction> m_process_response_consume_body;
  47. };
  48. }