TimeOrigin.cpp 2.6 KB

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