FetchController.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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/Badge.h>
  8. #include <LibJS/Forward.h>
  9. #include <LibJS/Heap/Cell.h>
  10. #include <LibJS/Heap/GCPtr.h>
  11. #include <LibJS/Heap/HeapFunction.h>
  12. #include <LibJS/Runtime/VM.h>
  13. #include <LibJS/SafeFunction.h>
  14. #include <LibWeb/Fetch/Infrastructure/FetchTimingInfo.h>
  15. #include <LibWeb/Forward.h>
  16. namespace Web::Fetch::Infrastructure {
  17. // https://fetch.spec.whatwg.org/#fetch-controller
  18. class FetchController : public JS::Cell {
  19. JS_CELL(FetchController, JS::Cell);
  20. public:
  21. enum class State {
  22. Ongoing,
  23. Terminated,
  24. Aborted,
  25. };
  26. [[nodiscard]] static JS::NonnullGCPtr<FetchController> create(JS::VM&);
  27. void set_full_timing_info(JS::NonnullGCPtr<FetchTimingInfo> full_timing_info) { m_full_timing_info = full_timing_info; }
  28. void set_report_timing_steps(Function<void(JS::Object const&)> report_timing_steps);
  29. void set_next_manual_redirect_steps(Function<void()> next_manual_redirect_steps);
  30. [[nodiscard]] State state() const { return m_state; }
  31. void report_timing(JS::Object const&) const;
  32. void process_next_manual_redirect() const;
  33. [[nodiscard]] JS::NonnullGCPtr<FetchTimingInfo> extract_full_timing_info() const;
  34. void abort(JS::Realm&, Optional<JS::Value>);
  35. void terminate();
  36. void set_fetch_params(Badge<FetchParams>, JS::NonnullGCPtr<FetchParams> fetch_params) { m_fetch_params = fetch_params; }
  37. void stop_fetch();
  38. private:
  39. FetchController();
  40. virtual void visit_edges(JS::Cell::Visitor&) override;
  41. // https://fetch.spec.whatwg.org/#fetch-controller-state
  42. // state (default "ongoing")
  43. // "ongoing", "terminated", or "aborted"
  44. State m_state { State::Ongoing };
  45. // https://fetch.spec.whatwg.org/#fetch-controller-full-timing-info
  46. // full timing info (default null)
  47. // Null or a fetch timing info.
  48. JS::GCPtr<FetchTimingInfo> m_full_timing_info;
  49. // https://fetch.spec.whatwg.org/#fetch-controller-report-timing-steps
  50. // report timing steps (default null)
  51. // Null or an algorithm accepting a global object.
  52. JS::GCPtr<JS::HeapFunction<void(JS::Object const&)>> m_report_timing_steps;
  53. // https://fetch.spec.whatwg.org/#fetch-controller-report-timing-steps
  54. // FIXME: serialized abort reason (default null)
  55. // Null or a Record (result of StructuredSerialize).
  56. // https://fetch.spec.whatwg.org/#fetch-controller-next-manual-redirect-steps
  57. // next manual redirect steps (default null)
  58. // Null or an algorithm accepting nothing.
  59. JS::GCPtr<JS::HeapFunction<void()>> m_next_manual_redirect_steps;
  60. JS::GCPtr<FetchParams> m_fetch_params;
  61. };
  62. }