diff --git a/Tests/LibWeb/Text/expected/performance-now.txt b/Tests/LibWeb/Text/expected/performance-now.txt new file mode 100644 index 00000000000..7ef22e9a431 --- /dev/null +++ b/Tests/LibWeb/Text/expected/performance-now.txt @@ -0,0 +1 @@ +PASS diff --git a/Tests/LibWeb/Text/input/performance-now.html b/Tests/LibWeb/Text/input/performance-now.html new file mode 100644 index 00000000000..3cc8fefb246 --- /dev/null +++ b/Tests/LibWeb/Text/input/performance-now.html @@ -0,0 +1,25 @@ + + + diff --git a/Userland/Libraries/LibWeb/HighResolutionTime/Performance.cpp b/Userland/Libraries/LibWeb/HighResolutionTime/Performance.cpp index a323270a04a..2149b88def8 100644 --- a/Userland/Libraries/LibWeb/HighResolutionTime/Performance.cpp +++ b/Userland/Libraries/LibWeb/HighResolutionTime/Performance.cpp @@ -65,11 +65,20 @@ JS::GCPtr Performance::navigation() return m_navigation; } +// https://w3c.github.io/hr-time/#timeorigin-attribute double Performance::time_origin() const { + // FIXME: The timeOrigin attribute MUST return the number of milliseconds in the duration returned by get time origin timestamp for the relevant global object of this. return static_cast(m_timer.origin_time().nanoseconds()) / 1e6; } +// https://w3c.github.io/hr-time/#now-method +double Performance::now() const +{ + // The now() method MUST return the number of milliseconds in the current high resolution time given this's relevant global object (a duration). + return current_high_resolution_time(HTML::relevant_global_object(*this)); +} + // https://w3c.github.io/user-timing/#mark-method WebIDL::ExceptionOr> Performance::mark(String const& mark_name, UserTiming::PerformanceMarkOptions const& mark_options) { @@ -232,8 +241,7 @@ WebIDL::ExceptionOr> Performanc } // 4. Otherwise, let end time be the value that would be returned by the Performance object's now() method. else { - // FIXME: Performance#now doesn't currently use TimeOrigin's functions, update this and Performance#now to match Performance#now's specification. - end_time = HighResolutionTime::unsafe_shared_current_time(); + end_time = now(); } // 3. Compute start time as follows: diff --git a/Userland/Libraries/LibWeb/HighResolutionTime/Performance.h b/Userland/Libraries/LibWeb/HighResolutionTime/Performance.h index a737cbbb804..acfb678e6d4 100644 --- a/Userland/Libraries/LibWeb/HighResolutionTime/Performance.h +++ b/Userland/Libraries/LibWeb/HighResolutionTime/Performance.h @@ -22,7 +22,7 @@ class Performance final : public DOM::EventTarget { public: virtual ~Performance() override; - double now() const { return static_cast(m_timer.elapsed_time().to_nanoseconds()) / 1e6; } + double now() const; double time_origin() const; WebIDL::ExceptionOr> mark(String const& mark_name, UserTiming::PerformanceMarkOptions const& mark_options = {}); diff --git a/Userland/Libraries/LibWeb/HighResolutionTime/TimeOrigin.cpp b/Userland/Libraries/LibWeb/HighResolutionTime/TimeOrigin.cpp index 013db59f8ea..451e7a0eaa8 100644 --- a/Userland/Libraries/LibWeb/HighResolutionTime/TimeOrigin.cpp +++ b/Userland/Libraries/LibWeb/HighResolutionTime/TimeOrigin.cpp @@ -14,9 +14,32 @@ namespace Web::HighResolutionTime { // https://w3c.github.io/hr-time/#dfn-get-time-origin-timestamp DOMHighResTimeStamp get_time_origin_timestamp(JS::Object const& global) { - // FIXME: Implement this. (void)global; - return 0; + + // To get time origin timestamp, given a global object global, run the following steps, which return a duration: + // FIXME: 1. Let timeOrigin be global's relevant settings object's time origin. + auto time_origin = 0; + + // 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: + + // !. Let wall time be the wall clock's unsafe current time. + struct timeval tv; + gettimeofday(&tv, nullptr); + auto wall_time = tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0; + + // 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); + + // 2. Return the duration from the estimated monotonic time of the Unix epoch to timeOrigin. + return estimated_monotonic_time - time_origin; } // https://w3c.github.io/hr-time/#dfn-coarsen-time