TimeOrigin.cpp 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <andreas@ladybird.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. (void)global;
  15. // To get time origin timestamp, given a global object global, run the following steps, which return a duration:
  16. // FIXME: 1. Let timeOrigin be global's relevant settings object's time origin.
  17. auto time_origin = 0;
  18. // Each group of environment settings objects that could possibly communicate in any way
  19. // has an estimated monotonic time of the Unix epoch, a moment on the monotonic clock,
  20. // whose value is initialized by the following steps:
  21. // !. Let wall time be the wall clock's unsafe current time.
  22. struct timeval tv;
  23. gettimeofday(&tv, nullptr);
  24. auto wall_time = tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0;
  25. // 2. Let monotonic time be the monotonic clock's unsafe current time.
  26. auto monotonic_time = unsafe_shared_current_time();
  27. // 3. Let epoch time be monotonic time - (wall time - Unix epoch)
  28. auto epoch_time = monotonic_time - (wall_time - 0);
  29. // 4. Initialize the estimated monotonic time of the Unix epoch to the result of calling coarsen time with epoch time
  30. auto estimated_monotonic_time = coarsen_time(epoch_time);
  31. // 2. Return the duration from the estimated monotonic time of the Unix epoch to timeOrigin.
  32. return estimated_monotonic_time - time_origin;
  33. }
  34. // https://w3c.github.io/hr-time/#dfn-coarsen-time
  35. DOMHighResTimeStamp coarsen_time(DOMHighResTimeStamp timestamp, bool cross_origin_isolated_capability)
  36. {
  37. // FIXME: Implement this.
  38. (void)cross_origin_isolated_capability;
  39. return timestamp;
  40. }
  41. // https://w3c.github.io/hr-time/#dfn-current-high-resolution-time
  42. DOMHighResTimeStamp current_high_resolution_time(JS::Object const& global)
  43. {
  44. // The current high resolution time given a global object current global must return the result
  45. // of relative high resolution time given unsafe shared current time and current global.
  46. return HighResolutionTime::relative_high_resolution_time(HighResolutionTime::unsafe_shared_current_time(), global);
  47. }
  48. // https://w3c.github.io/hr-time/#dfn-relative-high-resolution-time
  49. // https://pr-preview.s3.amazonaws.com/w3c/hr-time/pull/168.html#dfn-relative-high-resolution-time
  50. DOMHighResTimeStamp relative_high_resolution_time(DOMHighResTimeStamp time, JS::Object const& global)
  51. {
  52. // 1. Let settings be the global's relevant principal settings object.
  53. auto& settings = HTML::relevant_principal_settings_object(global);
  54. // 2. Let coarse time be the result of calling coarsen time with time and settings's cross-origin isolated capability.
  55. auto coarse_time = coarsen_time(time, settings.cross_origin_isolated_capability() == HTML::CanUseCrossOriginIsolatedAPIs::Yes);
  56. // 2. Return the relative high resolution coarse time for coarse time and global.
  57. return relative_high_resolution_coarsen_time(coarse_time, global);
  58. }
  59. // https://w3c.github.io/hr-time/#dfn-relative-high-resolution-coarse-time
  60. DOMHighResTimeStamp relative_high_resolution_coarsen_time(DOMHighResTimeStamp coarsen_time, JS::Object const& global)
  61. {
  62. // 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.
  63. return coarsen_time - get_time_origin_timestamp(global);
  64. }
  65. // https://w3c.github.io/hr-time/#dfn-coarsened-shared-current-time
  66. DOMHighResTimeStamp coarsened_shared_current_time(bool cross_origin_isolated_capability)
  67. {
  68. // 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.
  69. return coarsen_time(unsafe_shared_current_time(), cross_origin_isolated_capability);
  70. }
  71. // https://w3c.github.io/hr-time/#dfn-unsafe-shared-current-time
  72. DOMHighResTimeStamp unsafe_shared_current_time()
  73. {
  74. // The unsafe shared current time must return the current value of the shared monotonic clock.
  75. // Note: This is in milliseconds (stored as a double).
  76. return MonotonicTime::now().nanoseconds() / 1.0e6;
  77. }
  78. }