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