FetchController.cpp 3.9 KB

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