Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / html / preload_entry.lucb

#==============================================================================================
#
#   preload_entry - HTML::PreloadEntry and preload keys: the document's map of preloaded
#   resources, and consuming a preloaded resource
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/HTML/PreloadEntry.cpp (lines 17-100) with PreloadEntry.h's
#       public members (accessed through functions here) and its AK::Traits<PreloadKey>
#       (lines 89-98, the hash and equality HtmlPreloadKeyTraits forwards to).
#
#       Copyright (c) 2026, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: PreloadKey ==============================================================================

## Ported from Libraries/LibWeb/HTML/PreloadEntry.h:37 Web::HTML::PreloadKey::operator== (defaulted).
pub func html_preload_key_eq(a: const HtmlPreloadKey*, b: const HtmlPreloadKey*) -> bool:
    return web_url.url_equals(&a.url, b.url) and a.destination == b.destination and a.mode == b.mode and a.credentials_mode == b.credentials_mode

## Ported from Libraries/LibWeb/HTML/PreloadEntry.h:91 AK::Traits<Web::HTML::PreloadKey>::hash.
pub func html_preload_key_traits_hash(key: const HtmlPreloadKey*) -> u32:
    let url_text = web_url.url_to_string(&key.url)
    let url_hash = ak.string_hash_value(&url_text)
    # Offset the destination by 1 so that "absent" hashes differently from Destination::Audio (value 0).
    var destination_hash: u32 = 0
    if let destination = key.destination:
        destination_hash = (u32)destination + 1
    let mode_hash = (u32)key.mode
    let credentials_mode_hash = (u32)key.credentials_mode
    return ak.pair_int_hash(ak.pair_int_hash(url_hash, destination_hash), ak.pair_int_hash(mode_hash, credentials_mode_hash))

# mark: PreloadEntry ============================================================================

## realm.create<HTML::PreloadEntry>(): a new entry with no response and no callback.
pub func realm_create_html_preload_entry(realm: Realm*) -> HtmlPreloadEntry*:
    let this = (HtmlPreloadEntry*)web_allocate_cell(realm, &html_preload_entry_class)
    web_initialize_cell(this.cell(), realm)
    return this

## Ported from Libraries/LibWeb/HTML/PreloadEntry.cpp:17 Web::HTML::PreloadEntry::visit_edges (override).
pub func html_preload_entry_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (HtmlPreloadEntry*)base
    gc.cell_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_t(visitor, this.response)
    gc.cell_visitor_visit_t(visitor, this.on_response_available)

## `entry->on_response_available`.
pub func html_preload_entry_on_response_available(this: HtmlPreloadEntry*) -> gc.Function1[FetchResponse*, unit]*?:
    return this.on_response_available

## `entry->response = response`.
pub func html_preload_entry_set_response(this: HtmlPreloadEntry*, response: FetchResponse*?):
    this.response = response

## `entry->response`.
pub func html_preload_entry_response(this: HtmlPreloadEntry*) -> FetchResponse*?:
    return this.response

## `entry->integrity_metadata = value`.
pub func html_preload_entry_set_integrity_metadata(this: HtmlPreloadEntry*, integrity_metadata: ak.String):
    this.integrity_metadata = integrity_metadata

# mark: Preloading ==============================================================================

## Ported from Libraries/LibWeb/HTML/PreloadEntry.cpp:25 Web::HTML::create_a_preload_key.
# https://html.spec.whatwg.org/multipage/links.html#create-a-preload-key
pub func html_create_a_preload_key(request: FetchRequest*) -> HtmlPreloadKey:
    # To create a preload key for a request request, return a new preload key whose URL is request's URL, destination
    # is request's destination, mode is request's mode, and credentials mode is request's credentials mode.
    return HtmlPreloadKey(url = fetch_request_url(request), destination = fetch_request_destination(request), mode = fetch_request_mode(request),
                          credentials_mode = fetch_request_credentials_mode(request))

## Ported from Libraries/LibWeb/HTML/PreloadEntry.cpp:38 Web::HTML::translate_a_preload_destination.
# https://html.spec.whatwg.org/multipage/links.html#translate-a-preload-destination
pub func html_translate_a_preload_destination(destination: ak.String?) -> EmptyOrOptionalFetchRequestDestination:
    # 1. If destination is not "fetch", "font", "image", "script", "style", or "track", then return null.
    let value = destination else return EmptyOrOptionalFetchRequestDestination.empty
    let names: str[6] = ["fetch", "font", "image", "script", "style", "track"]
    var is_one_of = false
    for name in names:
        if ak.string_eq_string_view(&value, ak.sv(name)):
            is_one_of = true
    if not is_one_of:
        return EmptyOrOptionalFetchRequestDestination.empty

    # 2. Return the result of translating destination.
    return EmptyOrOptionalFetchRequestDestination.destination(value = fetch_translate_potential_destination(ak.string_bytes_as_string_view(&value)))

## Ported from Libraries/LibWeb/HTML/PreloadEntry.cpp:49 Web::HTML::consume_a_preloaded_resource.
# https://html.spec.whatwg.org/multipage/links.html#consume-a-preloaded-resource
pub func html_consume_a_preloaded_resource(window: HtmlWindow*, url: web_url.Url, destination: FetchRequestDestination?, mode: FetchRequestMode,
                                           credentials_mode: FetchRequestCredentialsMode, integrity_metadata: ak.String,
                                           on_response_available: gc.Function1[FetchResponse*, unit]*) -> bool:
    # 1. Let key be a preload key whose URL is url, destination is destination, mode is mode, and credentials mode is
    #    credentialsMode.
    let key = HtmlPreloadKey(url = url, destination = destination, mode = mode, credentials_mode = credentials_mode)

    # 2. Let preloads be window's associated Document's map of preloaded resources.
    let preloads = dom_document_map_of_preloaded_resources(html_window_associated_document(window))

    # 3. If key does not exist in preloads, then return false.
    let it = ak.hash_map_get_k(preloads, key) else return false

    # 4. Let entry be preloads[key].
    let entry = *it

    # FIXME: 5. Let consumerIntegrityMetadata be the result of parsing integrityMetadata via SRI::parse_metadata.
    # FIXME: 6. Let preloadIntegrityMetadata be the result of parsing entry's integrity metadata via SRI::parse_metadata.
    # FIXME: 7. If none of the following conditions apply:
    #              - consumerIntegrityMetadata is no metadata;
    #              - consumerIntegrityMetadata is equal to preloadIntegrityMetadata;
    #           then return false.
    _ = integrity_metadata

    # 8. Remove preloads[key].
    _ = ak.hash_map_remove_k(preloads, key)

    # 9. If entry's response is null, then set entry's on response available to onResponseAvailable.
    if entry.response == none:
        entry.on_response_available = on_response_available
    # 10. Otherwise, call onResponseAvailable with entry's response.
    else:
        ak.function1_call(&on_response_available.function, entry.response else trap("VERIFY_NOT_REACHED"))

    # 11. Return true.
    return true