FetchController.h 2.5 KB

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