Coverage Report

Created: 2026-02-16 07:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/Userland/Libraries/LibWeb/HighResolutionTime/TimeOrigin.cpp
Line
Count
Source
1
/*
2
 * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
3
 * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
4
 *
5
 * SPDX-License-Identifier: BSD-2-Clause
6
 */
7
8
#include <AK/Time.h>
9
#include <LibWeb/HTML/Scripting/Environments.h>
10
#include <LibWeb/HighResolutionTime/TimeOrigin.h>
11
12
namespace Web::HighResolutionTime {
13
14
// https://w3c.github.io/hr-time/#dfn-get-time-origin-timestamp
15
DOMHighResTimeStamp get_time_origin_timestamp(JS::Object const& global)
16
0
{
17
0
    (void)global;
18
19
    // To get time origin timestamp, given a global object global, run the following steps, which return a duration:
20
    // FIXME: 1. Let timeOrigin be global's relevant settings object's time origin.
21
0
    auto time_origin = 0;
22
23
    // Each group of environment settings objects that could possibly communicate in any way
24
    // has an estimated monotonic time of the Unix epoch, a moment on the monotonic clock,
25
    // whose value is initialized by the following steps:
26
27
    // !. Let wall time be the wall clock's unsafe current time.
28
0
    struct timeval tv;
29
0
    gettimeofday(&tv, nullptr);
30
0
    auto wall_time = tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0;
31
32
    // 2. Let monotonic time be the monotonic clock's unsafe current time.
33
0
    auto monotonic_time = unsafe_shared_current_time();
34
35
    // 3. Let epoch time be monotonic time - (wall time - Unix epoch)
36
0
    auto epoch_time = monotonic_time - (wall_time - 0);
37
38
    // 4. Initialize the estimated monotonic time of the Unix epoch to the result of calling coarsen time with epoch time
39
0
    auto estimated_monotonic_time = coarsen_time(epoch_time);
40
41
    // 2. Return the duration from the estimated monotonic time of the Unix epoch to timeOrigin.
42
0
    return estimated_monotonic_time - time_origin;
43
0
}
44
45
// https://w3c.github.io/hr-time/#dfn-coarsen-time
46
DOMHighResTimeStamp coarsen_time(DOMHighResTimeStamp timestamp, bool cross_origin_isolated_capability)
47
0
{
48
    // FIXME: Implement this.
49
0
    (void)cross_origin_isolated_capability;
50
0
    return timestamp;
51
0
}
52
53
// https://w3c.github.io/hr-time/#dfn-current-high-resolution-time
54
DOMHighResTimeStamp current_high_resolution_time(JS::Object const& global)
55
0
{
56
    // The current high resolution time given a global object current global must return the result
57
    // of relative high resolution time given unsafe shared current time and current global.
58
0
    return HighResolutionTime::relative_high_resolution_time(HighResolutionTime::unsafe_shared_current_time(), global);
59
0
}
60
61
// https://w3c.github.io/hr-time/#dfn-relative-high-resolution-time
62
DOMHighResTimeStamp relative_high_resolution_time(DOMHighResTimeStamp time, JS::Object const& global)
63
0
{
64
    // 1. Let coarse time be the result of calling coarsen time with time and global's relevant settings object's cross-origin isolated capability.
65
0
    auto coarse_time = coarsen_time(time, HTML::relevant_settings_object(global).cross_origin_isolated_capability() == HTML::CanUseCrossOriginIsolatedAPIs::Yes);
66
67
    // 2. Return the relative high resolution coarse time for coarse time and global.
68
0
    return relative_high_resolution_coarsen_time(coarse_time, global);
69
0
}
70
71
// https://w3c.github.io/hr-time/#dfn-relative-high-resolution-coarse-time
72
DOMHighResTimeStamp relative_high_resolution_coarsen_time(DOMHighResTimeStamp coarsen_time, JS::Object const& global)
73
0
{
74
    // The relative high resolution coarse time given a DOMHighResTimeStamp coarseTime and a global object global, is the difference between coarseTime and the result of calling get time origin timestamp with global.
75
0
    return coarsen_time - get_time_origin_timestamp(global);
76
0
}
77
78
// https://w3c.github.io/hr-time/#dfn-coarsened-shared-current-time
79
DOMHighResTimeStamp coarsened_shared_current_time(bool cross_origin_isolated_capability)
80
0
{
81
    // 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.
82
0
    return coarsen_time(unsafe_shared_current_time(), cross_origin_isolated_capability);
83
0
}
84
85
// https://w3c.github.io/hr-time/#dfn-unsafe-shared-current-time
86
DOMHighResTimeStamp unsafe_shared_current_time()
87
0
{
88
    // The unsafe shared current time must return the current value of the shared monotonic clock.
89
    // Note: This is in milliseconds (stored as a double).
90
0
    return MonotonicTime::now().nanoseconds() / 1.0e6;
91
0
}
92
93
}