FetchController.h 2.7 KB

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