Serenity Operating System
at master 61 lines 2.7 kB view raw
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 12namespace Web::HighResolutionTime { 13 14// https://w3c.github.io/hr-time/#dfn-get-time-origin-timestamp 15DOMHighResTimeStamp get_time_origin_timestamp(JS::Object const& global) 16{ 17 // FIXME: Implement this. 18 (void)global; 19 return 0; 20} 21 22// https://w3c.github.io/hr-time/#dfn-coarsen-time 23DOMHighResTimeStamp coarsen_time(DOMHighResTimeStamp timestamp, bool cross_origin_isolated_capability) 24{ 25 // FIXME: Implement this. 26 (void)cross_origin_isolated_capability; 27 return timestamp; 28} 29 30// https://w3c.github.io/hr-time/#dfn-relative-high-resolution-time 31DOMHighResTimeStamp relative_high_resolution_time(DOMHighResTimeStamp time, JS::Object const& global) 32{ 33 // 1. Let coarse time be the result of calling coarsen time with time and global's relevant settings object's cross-origin isolated capability. 34 auto coarse_time = coarsen_time(time, HTML::relevant_settings_object(global).cross_origin_isolated_capability() == HTML::CanUseCrossOriginIsolatedAPIs::Yes); 35 36 // 2. Return the relative high resolution coarse time for coarse time and global. 37 return relative_high_resolution_coarsen_time(coarse_time, global); 38} 39 40// https://w3c.github.io/hr-time/#dfn-relative-high-resolution-coarse-time 41DOMHighResTimeStamp relative_high_resolution_coarsen_time(DOMHighResTimeStamp coarsen_time, JS::Object const& global) 42{ 43 // 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. 44 return coarsen_time - get_time_origin_timestamp(global); 45} 46 47// https://w3c.github.io/hr-time/#dfn-coarsened-shared-current-time 48DOMHighResTimeStamp coarsened_shared_current_time(bool cross_origin_isolated_capability) 49{ 50 // 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. 51 return coarsen_time(unsafe_shared_current_time(), cross_origin_isolated_capability); 52} 53 54// https://w3c.github.io/hr-time/#dfn-unsafe-shared-current-time 55DOMHighResTimeStamp unsafe_shared_current_time() 56{ 57 // The unsafe shared current time must return the current value of the shared monotonic clock. 58 return Time::now_monotonic().to_nanoseconds() / 1e6; 59} 60 61}