FetchController.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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::Realm& realm, Optional<JS::Value> error)
  47. {
  48. // 1. Set controller’s state to "aborted".
  49. m_state = State::Aborted;
  50. // 2. Let fallbackError be an "AbortError" DOMException.
  51. auto fallback_error = WebIDL::AbortError::create(realm, "Fetch was aborted"sv);
  52. // 3. Set error to fallbackError if it is not given.
  53. if (!error.has_value())
  54. error = fallback_error;
  55. // FIXME: 4. Let serializedError be StructuredSerialize(error). If that threw an exception, catch it, and let serializedError be StructuredSerialize(fallbackError).
  56. // FIXME: 5. Set controller’s serialized abort reason to serializedError.
  57. (void)error;
  58. }
  59. // FIXME: https://fetch.spec.whatwg.org/#deserialize-a-serialized-abort-reason
  60. // https://fetch.spec.whatwg.org/#fetch-controller-terminate
  61. void FetchController::terminate()
  62. {
  63. // To terminate a fetch controller controller, set controller’s state to "terminated".
  64. m_state = State::Terminated;
  65. }
  66. }