2022-09-21 00:24:02 +02:00
/*
2024-10-04 13:19:50 +02:00
* Copyright ( c ) 2022 , Andreas Kling < andreas @ ladybird . org >
2022-10-04 21:03:50 +01:00
* Copyright ( c ) 2022 , Linus Groh < linusg @ serenityos . org >
2022-09-21 00:24:02 +02:00
*
* SPDX - License - Identifier : BSD - 2 - Clause
*/
2022-10-04 21:25:00 +01:00
# include <AK/Time.h>
2022-10-13 23:20:27 +02:00
# include <LibWeb/HTML/Scripting/Environments.h>
2022-10-04 21:30:29 +01:00
# include <LibWeb/HighResolutionTime/TimeOrigin.h>
2022-09-21 00:24:02 +02:00
namespace Web : : HighResolutionTime {
2025-01-07 10:08:14 +00:00
// https://w3c.github.io/hr-time/#dfn-estimated-monotonic-time-of-the-unix-epoch
DOMHighResTimeStamp estimated_monotonic_time_of_the_unix_epoch ( )
2022-10-13 23:20:27 +02:00
{
2024-10-05 18:01:19 +02:00
// 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:
2025-01-07 10:08:14 +00:00
// 1. Let wall time be the wall clock's unsafe current time.
auto wall_time = wall_clock_unsafe_current_time ( ) ;
2024-10-05 18:01:19 +02:00
// 2. Let monotonic time be the monotonic clock's unsafe current time.
auto monotonic_time = unsafe_shared_current_time ( ) ;
// 3. Let epoch time be monotonic time - (wall time - Unix epoch)
auto epoch_time = monotonic_time - ( wall_time - 0 ) ;
// 4. Initialize the estimated monotonic time of the Unix epoch to the result of calling coarsen time with epoch time
auto estimated_monotonic_time = coarsen_time ( epoch_time ) ;
2025-01-07 10:08:14 +00:00
return estimated_monotonic_time ;
}
// https://w3c.github.io/hr-time/#dfn-get-time-origin-timestamp
DOMHighResTimeStamp get_time_origin_timestamp ( JS : : Object const & global )
{
// 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.
auto time_origin = HTML : : relevant_principal_settings_object ( global ) . time_origin ( ) ;
2024-10-05 18:01:19 +02:00
// 2. Return the duration from the estimated monotonic time of the Unix epoch to timeOrigin.
2025-01-07 10:08:14 +00:00
return time_origin - estimated_monotonic_time_of_the_unix_epoch ( ) ;
2022-10-13 23:20:27 +02:00
}
2022-09-21 00:24:02 +02:00
// https://w3c.github.io/hr-time/#dfn-coarsen-time
2022-10-04 21:13:35 +01:00
DOMHighResTimeStamp coarsen_time ( DOMHighResTimeStamp timestamp , bool cross_origin_isolated_capability )
2022-09-21 00:24:02 +02:00
{
// FIXME: Implement this.
( void ) cross_origin_isolated_capability ;
return timestamp ;
}
2024-04-11 22:06:19 +02:00
// https://w3c.github.io/hr-time/#dfn-current-high-resolution-time
DOMHighResTimeStamp current_high_resolution_time ( JS : : Object const & global )
{
// 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 HighResolutionTime : : relative_high_resolution_time ( HighResolutionTime : : unsafe_shared_current_time ( ) , global ) ;
}
2022-10-13 23:20:27 +02:00
// https://w3c.github.io/hr-time/#dfn-relative-high-resolution-time
2024-11-28 05:08:25 +13:00
// https://pr-preview.s3.amazonaws.com/w3c/hr-time/pull/168.html#dfn-relative-high-resolution-time
2022-10-13 23:20:27 +02:00
DOMHighResTimeStamp relative_high_resolution_time ( DOMHighResTimeStamp time , JS : : Object const & global )
{
2024-11-28 05:08:25 +13:00
// 1. Let settings be the global's relevant principal settings object.
auto & settings = HTML : : relevant_principal_settings_object ( global ) ;
// 2. Let coarse time be the result of calling coarsen time with time and settings's cross-origin isolated capability.
auto coarse_time = coarsen_time ( time , settings . cross_origin_isolated_capability ( ) = = HTML : : CanUseCrossOriginIsolatedAPIs : : Yes ) ;
2022-10-13 23:20:27 +02:00
// 2. Return the relative high resolution coarse time for coarse time and global.
return relative_high_resolution_coarsen_time ( coarse_time , global ) ;
}
// https://w3c.github.io/hr-time/#dfn-relative-high-resolution-coarse-time
DOMHighResTimeStamp relative_high_resolution_coarsen_time ( DOMHighResTimeStamp coarsen_time , JS : : Object const & global )
{
2025-01-07 10:08:14 +00:00
// 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.
auto time_origin = HTML : : relevant_principal_settings_object ( global ) . time_origin ( ) ;
return coarsen_time - time_origin ;
2022-10-13 23:20:27 +02:00
}
2022-10-04 21:03:50 +01:00
// https://w3c.github.io/hr-time/#dfn-coarsened-shared-current-time
DOMHighResTimeStamp coarsened_shared_current_time ( bool cross_origin_isolated_capability )
{
// 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.
2022-10-04 21:25:00 +01:00
return coarsen_time ( unsafe_shared_current_time ( ) , cross_origin_isolated_capability ) ;
}
2025-01-07 10:08:14 +00:00
// https://w3c.github.io/hr-time/#wall-clock-unsafe-current-time
DOMHighResTimeStamp wall_clock_unsafe_current_time ( )
{
return UnixDateTime : : now ( ) . nanoseconds_since_epoch ( ) / 1.0e6 ;
}
2022-10-04 21:25:00 +01:00
// https://w3c.github.io/hr-time/#dfn-unsafe-shared-current-time
DOMHighResTimeStamp unsafe_shared_current_time ( )
{
// The unsafe shared current time must return the current value of the shared monotonic clock.
2024-02-04 21:14:08 +00:00
// Note: This is in milliseconds (stored as a double).
return MonotonicTime : : now ( ) . nanoseconds ( ) / 1.0e6 ;
2022-10-04 21:03:50 +01:00
}
2022-09-21 00:24:02 +02:00
}