Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / resource_timing / performance_resource_timing.lucb

#==============================================================================================
#
#   performance_resource_timing - ResourceTiming::PerformanceResourceTiming: marking a fetch
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/ResourceTiming/PerformanceResourceTiming.cpp lines 1-110 (the
#       constructor, entry_type, initialize, visit_edges, convert_fetch_timestamp,
#       mark_resource_timing and setup_the_resource_timing_entry) with the header's static
#       registry values and should_add_entry. The JS-facing getters (fetch_start,
#       next_hop_protocol, ...) are phase 3.
#
#       Copyright (c) 2025, Luke Wilde <lukew@serenityos.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from PerformanceResourceTiming.h:32 Web::ResourceTiming::PerformanceResourceTiming::available_from_timeline.
pub func resource_timing_performance_resource_timing_available_from_timeline() -> PerformanceTimelineAvailableFromTimeline:
    return PerformanceTimelineAvailableFromTimeline.yes

## Ported from PerformanceResourceTiming.h:35 Web::ResourceTiming::PerformanceResourceTiming::max_buffer_size.
## https://w3c.github.io/timing-entrytypes-registry/#dfn-maxbuffersize
pub func resource_timing_performance_resource_timing_max_buffer_size() -> u64?:
    return 250

## Ported from PerformanceResourceTiming.h:38 Web::ResourceTiming::PerformanceResourceTiming::should_add_entry (override).
## https://w3c.github.io/timing-entrytypes-registry/#dfn-should-add-entry
pub func resource_timing_performance_resource_timing_should_add_entry_impl(this: PerformanceTimelinePerformanceEntry*) -> PerformanceTimelineShouldAddEntry:
    return PerformanceTimelineShouldAddEntry.yes

## Ported from PerformanceResourceTiming.cpp:16 Web::ResourceTiming::PerformanceResourceTiming::PerformanceResourceTiming.
pub func resource_timing_performance_resource_timing_construct(this: ResourceTimingPerformanceResourceTiming*, realm: Realm*, name: ak.String, start_time: f64, duration: f64, timing_info: FetchTimingInfo*):
    performance_timeline_performance_entry_construct(&this.entry, realm, name, start_time, duration)
    this.m_timing_info = timing_info

## Ported from PerformanceResourceTiming.cpp:25 Web::ResourceTiming::PerformanceResourceTiming::entry_type (override).
## https://w3c.github.io/resource-timing/#dfn-entrytype
pub func resource_timing_performance_resource_timing_entry_type_impl(this: PerformanceTimelinePerformanceEntry*) -> ak.FlyString:
    # entryType
    #  The entryType getter steps are to return the DOMString "resource".
    return performance_timeline_entry_types_resource

## Ported from PerformanceResourceTiming.cpp:32 Web::ResourceTiming::PerformanceResourceTiming::initialize (override).
pub func resource_timing_performance_resource_timing_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.performance_resource_timing)
    performance_timeline_performance_entry_initialize_impl(base, realm)

## Ported from PerformanceResourceTiming.cpp:38 Web::ResourceTiming::PerformanceResourceTiming::visit_edges (override).
pub func resource_timing_performance_resource_timing_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (ResourceTimingPerformanceResourceTiming*)base
    bind_platform_object_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_t(visitor, this.m_timing_info)

## Ported from PerformanceResourceTiming.cpp:45 Web::ResourceTiming::convert_fetch_timestamp.
## https://w3c.github.io/resource-timing/#dfn-convert-fetch-timestamp
pub func resource_timing_convert_fetch_timestamp(time_stamp: f64, global: JsObject*) -> f64:
    # 1. If ts is zero, return zero.
    if time_stamp == 0.0:
        return 0.0

    # 2. Otherwise, return the relative high resolution coarse time given ts and global.
    return high_resolution_time_relative_high_resolution_coarsen_time(time_stamp, global)

## Ported from PerformanceResourceTiming.cpp:58 Web::ResourceTiming::PerformanceResourceTiming::mark_resource_timing.
## https://w3c.github.io/resource-timing/#dfn-mark-resource-timing
pub func resource_timing_performance_resource_timing_mark_resource_timing(timing_info: FetchTimingInfo*, requested_url: ak.String, initiator_type: ak.FlyString, global: JsObject*, cache_mode: FetchResponseCacheState?,
                                                                          body_info: FetchResponseBodyInfo, response_status: u16, delivery_type: ak.FlyString = ak.FlyString()):
    # 1. Create a PerformanceResourceTiming object entry in global's realm.
    let window_or_worker = as_if_window_or_worker_global_scope_mixin((gc.Cell*)global) else trap("as<WindowOrWorkerGlobalScopeMixin> failed")
    let realm = bind_platform_object_realm(html_window_or_worker_global_scope_mixin_this_impl(window_or_worker).bind_platform_object())

    # https://w3c.github.io/resource-timing/#dfn-name
    # name
    #  The name getter steps are to return this's requested URL.

    # https://w3c.github.io/resource-timing/#dfn-starttime
    # startTime
    #  The startTime getter steps are to convert fetch timestamp for this's timing info's start time and this's relevant global object.

    # https://w3c.github.io/resource-timing/#dfn-duration
    # duration
    #  The duration getter steps are to return this's timing info's end time minus this's timing info's start time.
    let converted_start_time = resource_timing_convert_fetch_timestamp(fetch_timing_info_start_time(timing_info), global)
    let converted_end_time = resource_timing_convert_fetch_timestamp(fetch_timing_info_end_time(timing_info), global)
    let entry = realm_create_resource_timing_performance_resource_timing(realm, realm, requested_url, converted_start_time, converted_end_time - converted_start_time, timing_info)

    # Setup the resource timing entry for entry, given initiatorType, requestedURL, timingInfo, cacheMode, bodyInfo, responseStatus, and deliveryType.
    resource_timing_performance_resource_timing_setup_the_resource_timing_entry(entry, initiator_type, requested_url, timing_info, cache_mode, body_info, response_status, delivery_type)

    # 3. Queue entry.
    html_window_or_worker_global_scope_mixin_queue_performance_entry(window_or_worker, &entry.entry)

    # 4. Add entry to global's performance entry buffer.
    html_window_or_worker_global_scope_mixin_add_resource_timing_entry(window_or_worker, entry)

## Ported from PerformanceResourceTiming.cpp:86 Web::ResourceTiming::PerformanceResourceTiming::setup_the_resource_timing_entry.
## https://www.w3.org/TR/resource-timing/#dfn-setup-the-resource-timing-entry
func resource_timing_performance_resource_timing_setup_the_resource_timing_entry(this: ResourceTimingPerformanceResourceTiming*, initiator_type: ak.FlyString, requested_url: ak.String, timing_info: FetchTimingInfo*,
                                                                                 cache_mode: FetchResponseCacheState?, body_info: FetchResponseBodyInfo, response_status: u16, a_delivery_type: ak.FlyString):
    var delivery_type = a_delivery_type

    # 1. Assert that cacheMode is the empty string, "local", or "validated".

    # 2. Set entry's initiator type to initiatorType.
    this.m_initiator_type = initiator_type

    # 3. Set entry's requested URL to requestedURL.
    this.m_requested_url = requested_url

    # 4. Set entry's timing info to timingInfo.
    this.m_timing_info = timing_info

    # 5. Set entry's response body info to bodyInfo.
    this.m_response_body_info = body_info

    # 6. Set entry's cache mode to cacheMode.
    this.m_cache_mode = cache_mode

    # 7. Set entry's response status to responseStatus.
    this.m_response_status = response_status

    # 8. If deliveryType is the empty string and cacheMode is not, then set deliveryType to "cache".
    if ak.fly_string_is_empty(&delivery_type) and cache_mode != none:
        delivery_type = ak.fly_string_from_str("cache")

    # 9. Set entry's delivery type to deliveryType.
    this.m_delivery_type = delivery_type