Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / fetch / infrastructure / task.lucb

#==============================================================================================
#
#   task - Fetch's "queue a fetch task": on a parallel queue, or as a global task on the
#   networking task source; and the AD-HOC overload that a fetch controller tracks
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/Fetch/Infrastructure/Task.cpp (lines 15-41). TaskDestination
#       (Task.h) is the variant FetchTaskDestination; an HTML::TaskID is a u64.
#
#       Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibWeb/Fetch/Infrastructure/Task.cpp:15 Web::Fetch::Infrastructure::queue_fetch_task.
# https://fetch.spec.whatwg.org/#queue-a-fetch-task
pub func fetch_queue_fetch_task_fetch_task_destination(task_destination: FetchTaskDestination, algorithm: gc.Function0[unit]*) -> u64:
    assert(task_destination != .empty)

    # 1. If taskDestination is a parallel queue, then enqueue algorithm to taskDestination.
    match task_destination:
        .html_parallel_queue(parallel_queue): return html_parallel_queue_enqueue(parallel_queue, algorithm)
        _: _ = 0

    # 2. Otherwise, queue a global task on the networking task source with taskDestination and algorithm.
    let global_object = match task_destination:
        .js_object(object) => object
        _ => trap("VERIFY: task_destination.get<GC::Ref<JS::Object>>()")
    return html_queue_global_task(HtmlTaskSource.networking, global_object, algorithm)

## The captures of the tracked fetch task, `[fetch_controller, fetch_task_id, algorithm]()`.
struct FetchQueueFetchTaskTracked:
    var fetch_controller: FetchController*
    var fetch_task_id: u64
    var algorithm: gc.Function0[unit]*

## The tracked fetch task: the controller forgets it, then the algorithm runs.
func fetch_queue_fetch_task_tracked_run(context: void*?):
    let captures = (FetchQueueFetchTaskTracked*)(context else trap("queue_fetch_task: no captures"))
    fetch_controller_fetch_task_complete(captures.fetch_controller, captures.fetch_task_id)
    ak.function0_call(&captures.algorithm.function)

## Ported from Libraries/LibWeb/Fetch/Infrastructure/Task.cpp:29 Web::Fetch::Infrastructure::queue_fetch_task (tracked by a fetch controller).
# AD-HOC: This overload allows tracking the queued task within the fetch controller so that we may cancel queued tasks
#         when the spec indicates that we must stop an ongoing fetch.
pub func fetch_queue_fetch_task_fetch_controller(fetch_controller: FetchController*, task_destination: FetchTaskDestination, algorithm: gc.Function0[unit]*) -> u64:
    let fetch_task_id = fetch_controller_next_fetch_task_id(fetch_controller)

    let heap = gc.cell_heap(fetch_controller.cell())
    let captures = new FetchQueueFetchTaskTracked(fetch_controller = fetch_controller, fetch_task_id = fetch_task_id, algorithm = algorithm) else trap("out of memory")
    let task_id = fetch_queue_fetch_task_fetch_task_destination(task_destination, gc.create_function0(heap, ak.Function0[unit](call = fetch_queue_fetch_task_tracked_run, context = captures)))

    fetch_controller_fetch_task_queued(fetch_controller, fetch_task_id, task_id)
    return task_id