FetchController.cpp 4.5 KB

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