FetchController.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibJS/Forward.h>
  8. #include <LibJS/Heap/Cell.h>
  9. #include <LibJS/SafeFunction.h>
  10. #include <LibWeb/Fetch/Infrastructure/FetchTimingInfo.h>
  11. namespace Web::Fetch::Infrastructure {
  12. // https://fetch.spec.whatwg.org/#fetch-controller
  13. class FetchController : public JS::Cell {
  14. JS_CELL(FetchController, JS::Cell);
  15. public:
  16. enum class State {
  17. Ongoing,
  18. Terminated,
  19. Aborted,
  20. };
  21. [[nodiscard]] static JS::NonnullGCPtr<FetchController> create(JS::VM&);
  22. void set_full_timing_info(JS::NonnullGCPtr<FetchTimingInfo> full_timing_info) { m_full_timing_info = full_timing_info; }
  23. void set_report_timing_steps(JS::SafeFunction<void(JS::Object const&)> report_timing_steps) { m_report_timing_steps = move(report_timing_steps); }
  24. void set_next_manual_redirect_steps(JS::SafeFunction<void()> next_manual_redirect_steps) { m_next_manual_redirect_steps = move(next_manual_redirect_steps); }
  25. [[nodiscard]] State state() const { return m_state; }
  26. void report_timing(JS::Object const&) const;
  27. void process_next_manual_redirect() const;
  28. [[nodiscard]] JS::NonnullGCPtr<FetchTimingInfo> extract_full_timing_info() const;
  29. void abort(JS::VM&, Optional<JS::Value>);
  30. void terminate();
  31. private:
  32. FetchController();
  33. virtual void visit_edges(JS::Cell::Visitor&) override;
  34. // https://fetch.spec.whatwg.org/#fetch-controller-state
  35. // state (default "ongoing")
  36. // "ongoing", "terminated", or "aborted"
  37. State m_state { State::Ongoing };
  38. // https://fetch.spec.whatwg.org/#fetch-controller-full-timing-info
  39. // full timing info (default null)
  40. // Null or a fetch timing info.
  41. JS::GCPtr<FetchTimingInfo> m_full_timing_info;
  42. // https://fetch.spec.whatwg.org/#fetch-controller-report-timing-steps
  43. // report timing steps (default null)
  44. // Null or an algorithm accepting a global object.
  45. Optional<JS::SafeFunction<void(JS::Object const&)>> m_report_timing_steps;
  46. // https://fetch.spec.whatwg.org/#fetch-controller-report-timing-steps
  47. // FIXME: serialized abort reason (default null)
  48. // Null or a Record (result of StructuredSerialize).
  49. // https://fetch.spec.whatwg.org/#fetch-controller-next-manual-redirect-steps
  50. // next manual redirect steps (default null)
  51. // Null or an algorithm accepting nothing.
  52. Optional<JS::SafeFunction<void()>> m_next_manual_redirect_steps;
  53. };
  54. }