FetchParams.cpp 2.1 KB

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