FetchController.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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/FetchController.h>
  9. #include <LibWeb/WebIDL/DOMException.h>
  10. namespace Web::Fetch::Infrastructure {
  11. FetchController::FetchController() = default;
  12. JS::NonnullGCPtr<FetchController> FetchController::create(JS::VM& vm)
  13. {
  14. return { *vm.heap().allocate_without_realm<FetchController>() };
  15. }
  16. void FetchController::visit_edges(JS::Cell::Visitor& visitor)
  17. {
  18. Base::visit_edges(visitor);
  19. visitor.visit(m_full_timing_info);
  20. }
  21. // https://fetch.spec.whatwg.org/#finalize-and-report-timing
  22. void FetchController::report_timing(JS::Object const& global) const
  23. {
  24. // 1. Assert: this’s report timing steps is not null.
  25. VERIFY(m_report_timing_steps.has_value());
  26. // 2. Call this’s report timing steps with global.
  27. (*m_report_timing_steps)(global);
  28. }
  29. // https://fetch.spec.whatwg.org/#fetch-controller-process-the-next-manual-redirect
  30. void FetchController::process_next_manual_redirect() const
  31. {
  32. // 1. Assert: controller’s next manual redirect steps are not null.
  33. VERIFY(m_next_manual_redirect_steps.has_value());
  34. // 2. Call controller’s next manual redirect steps.
  35. (*m_next_manual_redirect_steps)();
  36. }
  37. // https://fetch.spec.whatwg.org/#extract-full-timing-info
  38. JS::NonnullGCPtr<FetchTimingInfo> FetchController::extract_full_timing_info() const
  39. {
  40. // 1. Assert: this’s full timing info is not null.
  41. VERIFY(m_full_timing_info);
  42. // 2. Return this’s full timing info.
  43. return *m_full_timing_info;
  44. }
  45. // https://fetch.spec.whatwg.org/#fetch-controller-abort
  46. void FetchController::abort(JS::VM& vm, Optional<JS::Value> error)
  47. {
  48. auto& realm = *vm.current_realm();
  49. // 1. Set controller’s state to "aborted".
  50. m_state = State::Aborted;
  51. // 2. Let fallbackError be an "AbortError" DOMException.
  52. auto fallback_error = WebIDL::AbortError::create(realm, "Fetch was aborted"sv);
  53. // 3. Set error to fallbackError if it is not given.
  54. if (!error.has_value())
  55. error = fallback_error;
  56. // FIXME: 4. Let serializedError be StructuredSerialize(error). If that threw an exception, catch it, and let serializedError be StructuredSerialize(fallbackError).
  57. // FIXME: 5. Set controller’s serialized abort reason to serializedError.
  58. (void)error;
  59. }
  60. // FIXME: https://fetch.spec.whatwg.org/#deserialize-a-serialized-abort-reason
  61. // https://fetch.spec.whatwg.org/#fetch-controller-terminate
  62. void FetchController::terminate()
  63. {
  64. // To terminate a fetch controller controller, set controller’s state to "terminated".
  65. m_state = State::Terminated;
  66. }
  67. }