FetchController.cpp 3.4 KB

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