FetchController.h 3.1 KB

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