FetchParams.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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/FetchParams.h>
  9. #include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
  10. namespace Web::Fetch::Infrastructure {
  11. JS_DEFINE_ALLOCATOR(FetchParams);
  12. FetchParams::FetchParams(JS::NonnullGCPtr<Request> request, JS::NonnullGCPtr<FetchAlgorithms> algorithms, JS::NonnullGCPtr<FetchController> controller, JS::NonnullGCPtr<FetchTimingInfo> timing_info)
  13. : m_request(request)
  14. , m_algorithms(algorithms)
  15. , m_controller(controller)
  16. , m_timing_info(timing_info)
  17. {
  18. m_controller->set_fetch_params({}, *this);
  19. }
  20. JS::NonnullGCPtr<FetchParams> FetchParams::create(JS::VM& vm, JS::NonnullGCPtr<Request> request, JS::NonnullGCPtr<FetchTimingInfo> timing_info)
  21. {
  22. auto algorithms = Infrastructure::FetchAlgorithms::create(vm, {});
  23. auto controller = Infrastructure::FetchController::create(vm);
  24. return vm.heap().allocate_without_realm<FetchParams>(request, algorithms, controller, timing_info);
  25. }
  26. void FetchParams::visit_edges(JS::Cell::Visitor& visitor)
  27. {
  28. Base::visit_edges(visitor);
  29. visitor.visit(m_request);
  30. visitor.visit(m_algorithms);
  31. visitor.visit(m_controller);
  32. visitor.visit(m_timing_info);
  33. if (m_task_destination.has<JS::NonnullGCPtr<JS::Object>>())
  34. visitor.visit(m_task_destination.get<JS::NonnullGCPtr<JS::Object>>());
  35. if (m_preloaded_response_candidate.has<JS::NonnullGCPtr<Response>>())
  36. visitor.visit(m_preloaded_response_candidate.get<JS::NonnullGCPtr<Response>>());
  37. }
  38. // https://fetch.spec.whatwg.org/#fetch-params-aborted
  39. bool FetchParams::is_aborted() const
  40. {
  41. // A fetch params fetchParams is aborted if its controller’s state is "aborted".
  42. return m_controller->state() == FetchController::State::Aborted;
  43. }
  44. // https://fetch.spec.whatwg.org/#fetch-params-canceled
  45. bool FetchParams::is_canceled() const
  46. {
  47. // A fetch params fetchParams is canceled if its controller’s state is "aborted" or "terminated".
  48. return m_controller->state() == FetchController::State::Aborted || m_controller->state() == FetchController::State::Terminated;
  49. }
  50. }