dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / high_resolution_time / time_origin.lucb
#==============================================================================================
#
# time_origin - HighResolutionTime: time origins, coarsened and relative high resolution times
#
# DESCRIPTION:
# Port of Libraries/LibWeb/HighResolutionTime/TimeOrigin.cpp (lines 1-113) and
# TimeOrigin.h. The file was outside the skeleton's phase-1 closure; region r19 ported it
# because every DOM::Event records its creation time (Event::Event) and dispatch_event
# stamps user activations. DOMHighResTimeStamp is f64, in milliseconds.
#
# Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
# Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
## Ported from Libraries/LibWeb/HighResolutionTime/TimeOrigin.cpp:16 Web::HighResolutionTime::estimated_monotonic_time_of_the_unix_epoch.
## https://w3c.github.io/hr-time/#dfn-estimated-monotonic-time-of-the-unix-epoch
pub func high_resolution_time_estimated_monotonic_time_of_the_unix_epoch() -> f64:
# Each group of environment settings objects that could possibly communicate in any way
# has an estimated monotonic time of the Unix epoch, a moment on the monotonic clock,
# whose value is initialized by the following steps:
# 1. Let wall time be the wall clock's unsafe current time.
let wall_time = high_resolution_time_wall_clock_unsafe_current_time()
# 2. Let monotonic time be the monotonic clock's unsafe current time.
let monotonic_time = high_resolution_time_unsafe_shared_current_time()
# 3. Let epoch time be monotonic time - (wall time - Unix epoch)
let epoch_time = monotonic_time - (wall_time - 0.0)
# 4. Initialize the estimated monotonic time of the Unix epoch to the result of calling coarsen time with epoch time
let estimated_monotonic_time = high_resolution_time_coarsen_time(epoch_time)
return estimated_monotonic_time
## Ported from Libraries/LibWeb/HighResolutionTime/TimeOrigin.cpp:37 Web::HighResolutionTime::get_time_origin_timestamp.
## https://w3c.github.io/hr-time/#dfn-get-time-origin-timestamp
pub func high_resolution_time_get_time_origin_timestamp(global: JsObject*) -> f64:
# To get time origin timestamp, given a global object global, run the following steps, which return a duration:
# 1. Let timeOrigin be global's relevant settings object's time origin.
let time_origin = html_environment_settings_object_time_origin(html_relevant_settings_object_js_object(global))
# 2. Return the duration from the estimated monotonic time of the Unix epoch to timeOrigin.
return time_origin - high_resolution_time_estimated_monotonic_time_of_the_unix_epoch()
## Ported from Libraries/LibWeb/HighResolutionTime/TimeOrigin.cpp:48 Web::HighResolutionTime::coarsen_time.
## https://w3c.github.io/hr-time/#dfn-coarsen-time
pub func high_resolution_time_coarsen_time(timestamp: f64, cross_origin_isolated_capability: HtmlCanUseCrossOriginIsolatedApIs = HtmlCanUseCrossOriginIsolatedApIs.no) -> f64:
# 1. Let time resolution be 100 microseconds, or a higher implementation-defined value.
var time_resolution_milliseconds = 0.1
# 2. If crossOriginIsolatedCapability is true, set time resolution to be 5 microseconds, or a higher implementation-defined value.
if cross_origin_isolated_capability == HtmlCanUseCrossOriginIsolatedApIs.yes:
time_resolution_milliseconds = 0.005
# 3. In an implementation-defined manner, coarsen and potentially jitter timestamp such that its resolution will not exceed time resolution
let coarsened = math.floor(timestamp / time_resolution_milliseconds) * time_resolution_milliseconds
# FIXME: Applying jitter to the coarsened timestamp here may decrease our susceptibility to timing attacks.
# 4. Return timestamp as a moment
return coarsened
## Ported from Libraries/LibWeb/HighResolutionTime/TimeOrigin.cpp:67 Web::HighResolutionTime::current_high_resolution_time.
## https://w3c.github.io/hr-time/#dfn-current-high-resolution-time
pub func high_resolution_time_current_high_resolution_time(global: JsObject*) -> f64:
# The current high resolution time given a global object current global must return the result
# of relative high resolution time given unsafe shared current time and current global.
return high_resolution_time_relative_high_resolution_time(high_resolution_time_unsafe_shared_current_time(), global)
## Ported from Libraries/LibWeb/HighResolutionTime/TimeOrigin.cpp:75 Web::HighResolutionTime::relative_high_resolution_time.
## https://w3c.github.io/hr-time/#dfn-relative-high-resolution-time
pub func high_resolution_time_relative_high_resolution_time(time: f64, global: JsObject*) -> f64:
# 1. Let coarse time be the result of calling coarsen time with time and global’s relevant settings object’s cross-origin isolated capability.
let coarse_time = high_resolution_time_coarsen_time(time, html_environment_settings_object_cross_origin_isolated_capability(html_relevant_settings_object_js_object(global)))
# 2. Return the relative high resolution coarse time for coarse time and global.
return high_resolution_time_relative_high_resolution_coarsen_time(coarse_time, global)
## Ported from Libraries/LibWeb/HighResolutionTime/TimeOrigin.cpp:85 Web::HighResolutionTime::relative_high_resolution_coarsen_time.
## https://w3c.github.io/hr-time/#dfn-relative-high-resolution-coarse-time
pub func high_resolution_time_relative_high_resolution_coarsen_time(coarsen_time: f64, global: JsObject*) -> f64:
# The relative high resolution coarse time given a moment from the monotonic clock coarseTime and a global object global, is the duration from global's relevant settings object's time origin to coarseTime.
let time_origin = html_environment_settings_object_time_origin(html_relevant_settings_object_js_object(global))
return coarsen_time - time_origin
## Ported from Libraries/LibWeb/HighResolutionTime/TimeOrigin.cpp:93 Web::HighResolutionTime::coarsened_shared_current_time.
## https://w3c.github.io/hr-time/#dfn-coarsened-shared-current-time
pub func high_resolution_time_coarsened_shared_current_time(cross_origin_isolated_capability: HtmlCanUseCrossOriginIsolatedApIs = HtmlCanUseCrossOriginIsolatedApIs.no) -> f64:
# The coarsened shared current time given an optional boolean crossOriginIsolatedCapability (default false), must return the result of calling coarsen time with the unsafe shared current time and crossOriginIsolatedCapability.
return high_resolution_time_coarsen_time(high_resolution_time_unsafe_shared_current_time(), cross_origin_isolated_capability)
## Ported from Libraries/LibWeb/HighResolutionTime/TimeOrigin.cpp:100 Web::HighResolutionTime::wall_clock_unsafe_current_time.
## https://w3c.github.io/hr-time/#wall-clock-unsafe-current-time
pub func high_resolution_time_wall_clock_unsafe_current_time() -> f64:
let now = ak.unix_date_time_now()
return (f64)ak.unix_date_time_nanoseconds_since_epoch(&now) / 1.0e6
## Ported from Libraries/LibWeb/HighResolutionTime/TimeOrigin.cpp:106 Web::HighResolutionTime::unsafe_shared_current_time.
## https://w3c.github.io/hr-time/#dfn-unsafe-shared-current-time
pub func high_resolution_time_unsafe_shared_current_time() -> f64:
# The unsafe shared current time must return the current value of the shared monotonic clock.
# Note: This is in milliseconds (stored as a double).
let now = ak.monotonic_time_now()
return (f64)ak.monotonic_time_nanoseconds(&now) / 1.0e6