FetchController.h 3.0 KB

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