FetchController.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Heap/Heap.h>
  7. #include <LibJS/Runtime/VM.h>
  8. #include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h>
  9. #include <LibWeb/Fetch/Infrastructure/FetchController.h>
  10. #include <LibWeb/Fetch/Infrastructure/FetchParams.h>
  11. #include <LibWeb/WebIDL/DOMException.h>
  12. namespace Web::Fetch::Infrastructure {
  13. FetchController::FetchController() = default;
  14. JS::NonnullGCPtr<FetchController> FetchController::create(JS::VM& vm)
  15. {
  16. return vm.heap().allocate_without_realm<FetchController>();
  17. }
  18. void FetchController::visit_edges(JS::Cell::Visitor& visitor)
  19. {
  20. Base::visit_edges(visitor);
  21. visitor.visit(m_full_timing_info);
  22. visitor.visit(m_report_timing_steps);
  23. visitor.visit(m_next_manual_redirect_steps);
  24. visitor.visit(m_fetch_params);
  25. }
  26. void FetchController::set_report_timing_steps(Function<void(JS::Object const&)> report_timing_steps)
  27. {
  28. m_report_timing_steps = JS::create_heap_function(vm().heap(), move(report_timing_steps));
  29. }
  30. void FetchController::set_next_manual_redirect_steps(Function<void()> next_manual_redirect_steps)
  31. {
  32. m_next_manual_redirect_steps = JS::create_heap_function(vm().heap(), move(next_manual_redirect_steps));
  33. }
  34. // https://fetch.spec.whatwg.org/#finalize-and-report-timing
  35. void FetchController::report_timing(JS::Object const& global) const
  36. {
  37. // 1. Assert: this’s report timing steps is not null.
  38. VERIFY(m_report_timing_steps);
  39. // 2. Call this’s report timing steps with global.
  40. m_report_timing_steps->function()(global);
  41. }
  42. // https://fetch.spec.whatwg.org/#fetch-controller-process-the-next-manual-redirect
  43. void FetchController::process_next_manual_redirect() const
  44. {
  45. // 1. Assert: controller’s next manual redirect steps are not null.
  46. VERIFY(m_next_manual_redirect_steps);
  47. // 2. Call controller’s next manual redirect steps.
  48. m_next_manual_redirect_steps->function()();
  49. }
  50. // https://fetch.spec.whatwg.org/#extract-full-timing-info
  51. JS::NonnullGCPtr<FetchTimingInfo> FetchController::extract_full_timing_info() const
  52. {
  53. // 1. Assert: this’s full timing info is not null.
  54. VERIFY(m_full_timing_info);
  55. // 2. Return this’s full timing info.
  56. return *m_full_timing_info;
  57. }
  58. // https://fetch.spec.whatwg.org/#fetch-controller-abort
  59. void FetchController::abort(JS::Realm& realm, Optional<JS::Value> error)
  60. {
  61. // 1. Set controller’s state to "aborted".
  62. m_state = State::Aborted;
  63. // 2. Let fallbackError be an "AbortError" DOMException.
  64. auto fallback_error = WebIDL::AbortError::create(realm, "Fetch was aborted"_fly_string);
  65. // 3. Set error to fallbackError if it is not given.
  66. if (!error.has_value())
  67. error = fallback_error;
  68. // FIXME: 4. Let serializedError be StructuredSerialize(error). If that threw an exception, catch it, and let serializedError be StructuredSerialize(fallbackError).
  69. // FIXME: 5. Set controller’s serialized abort reason to serializedError.
  70. (void)error;
  71. }
  72. // FIXME: https://fetch.spec.whatwg.org/#deserialize-a-serialized-abort-reason
  73. // https://fetch.spec.whatwg.org/#fetch-controller-terminate
  74. void FetchController::terminate()
  75. {
  76. // To terminate a fetch controller controller, set controller’s state to "terminated".
  77. m_state = State::Terminated;
  78. }
  79. void FetchController::stop_fetch()
  80. {
  81. auto& vm = this->vm();
  82. // AD-HOC: Some HTML elements need to stop an ongoing fetching process without causing any network error to be raised
  83. // (which abort() and terminate() will both do). This is tricky because the fetch process runs across several
  84. // nested Platform::EventLoopPlugin::deferred_invoke() invocations. For now, we "stop" the fetch process by
  85. // ignoring any callbacks.
  86. if (m_fetch_params) {
  87. auto fetch_algorithms = FetchAlgorithms::create(vm, {});
  88. m_fetch_params->set_algorithms(fetch_algorithms);
  89. }
  90. }
  91. }