Task.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /*
  2. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Fetch/Infrastructure/FetchController.h>
  7. #include <LibWeb/Fetch/Infrastructure/Task.h>
  8. #include <LibWeb/HTML/EventLoop/EventLoop.h>
  9. namespace Web::Fetch::Infrastructure {
  10. // https://fetch.spec.whatwg.org/#queue-a-fetch-task
  11. int queue_fetch_task(JS::Object& task_destination, JS::SafeFunction<void()> algorithm)
  12. {
  13. // FIXME: 1. If taskDestination is a parallel queue, then enqueue algorithm to taskDestination.
  14. // 2. Otherwise, queue a global task on the networking task source with taskDestination and algorithm.
  15. return HTML::queue_global_task(HTML::Task::Source::Networking, task_destination, move(algorithm));
  16. }
  17. // AD-HOC: This overload allows tracking the queued task within the fetch controller so that we may cancel queued tasks
  18. // when the spec indicates that we must stop an ongoing fetch.
  19. int queue_fetch_task(JS::NonnullGCPtr<FetchController> fetch_controller, JS::Object& task_destination, JS::SafeFunction<void()> algorithm)
  20. {
  21. auto fetch_task_id = fetch_controller->next_fetch_task_id();
  22. int event_id = queue_fetch_task(task_destination, [fetch_controller, fetch_task_id, algorithm = move(algorithm)]() {
  23. fetch_controller->fetch_task_complete(fetch_task_id);
  24. algorithm();
  25. });
  26. fetch_controller->fetch_task_queued(fetch_task_id, event_id);
  27. return event_id;
  28. }
  29. }