From 98438fa32e4f93b59359662188e164c7c828acb7 Mon Sep 17 00:00:00 2001 From: janekptacijarabaci Date: Sun, 29 Apr 2018 12:15:36 +0200 Subject: Bug 1313420 - Implement Performance.timeOrigin - part 1 https://hg.mozilla.org/mozilla-central/rev/cf0f72f0b0be --- dom/performance/Performance.cpp | 14 +++++++++- dom/performance/Performance.h | 5 ++++ dom/performance/PerformanceService.cpp | 46 ++++++++++++++++++++++++++++++++ dom/performance/PerformanceService.h | 48 ++++++++++++++++++++++++++++++++++ dom/performance/moz.build | 2 ++ 5 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 dom/performance/PerformanceService.cpp create mode 100644 dom/performance/PerformanceService.h (limited to 'dom/performance') diff --git a/dom/performance/Performance.cpp b/dom/performance/Performance.cpp index 074bffe8a..d1547dc06 100755 --- a/dom/performance/Performance.cpp +++ b/dom/performance/Performance.cpp @@ -13,6 +13,7 @@ #include "PerformanceMeasure.h" #include "PerformanceObserver.h" #include "PerformanceResourceTiming.h" +#include "PerformanceService.h" #include "PerformanceWorker.h" #include "mozilla/ErrorResult.h" #include "mozilla/dom/PerformanceBinding.h" @@ -119,6 +120,17 @@ Performance::Performance(nsPIDOMWindowInner* aWindow) Performance::~Performance() {} +DOMHighResTimeStamp +Performance::TimeOrigin() +{ + if (!mPerformanceService) { + mPerformanceService = PerformanceService::GetOrCreate(); + } + + MOZ_ASSERT(mPerformanceService); + return mPerformanceService->TimeOrigin(CreationTimeStamp()); +} + JSObject* Performance::WrapObject(JSContext* aCx, JS::Handle aGivenProto) { @@ -243,7 +255,7 @@ Performance::ClearMarks(const Optional& aName) DOMHighResTimeStamp Performance::ResolveTimestampFromName(const nsAString& aName, - ErrorResult& aRv) + ErrorResult& aRv) { AutoTArray, 1> arr; DOMHighResTimeStamp ts; diff --git a/dom/performance/Performance.h b/dom/performance/Performance.h index 04dd76bc7..8495235a2 100644 --- a/dom/performance/Performance.h +++ b/dom/performance/Performance.h @@ -24,6 +24,7 @@ namespace dom { class PerformanceEntry; class PerformanceNavigation; class PerformanceObserver; +class PerformanceService; class PerformanceTiming; namespace workers { @@ -69,6 +70,8 @@ public: virtual DOMHighResTimeStamp Now() const = 0; + DOMHighResTimeStamp TimeOrigin(); + void Mark(const nsAString& aName, ErrorResult& aRv); void ClearMarks(const Optional& aName); @@ -157,6 +160,8 @@ private: uint64_t mResourceTimingBufferSize; static const uint64_t kDefaultResourceTimingBufferSize = 150; bool mPendingNotificationObserversTask; + + RefPtr mPerformanceService; }; } // namespace dom diff --git a/dom/performance/PerformanceService.cpp b/dom/performance/PerformanceService.cpp new file mode 100644 index 000000000..cf119af89 --- /dev/null +++ b/dom/performance/PerformanceService.cpp @@ -0,0 +1,46 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "PerformanceService.h" + +#include "mozilla/ClearOnShutdown.h" +#include "mozilla/StaticMutex.h" +#include "mozilla/StaticPtr.h" + +namespace mozilla { +namespace dom { + +static StaticRefPtr gPerformanceService; +static StaticMutex gPerformanceServiceMutex; + +/* static */ PerformanceService* +PerformanceService::GetOrCreate() +{ + StaticMutexAutoLock al(gPerformanceServiceMutex); + + if (!gPerformanceService) { + gPerformanceService = new PerformanceService(); + ClearOnShutdown(&gPerformanceService); + } + + return gPerformanceService; +} + +DOMHighResTimeStamp +PerformanceService::TimeOrigin(const TimeStamp& aCreationTimeStamp) const +{ + return (aCreationTimeStamp - mCreationTimeStamp).ToMilliseconds() + + (mCreationEpochTime / PR_USEC_PER_MSEC); +} + +PerformanceService::PerformanceService() +{ + mCreationTimeStamp = TimeStamp::Now(); + mCreationEpochTime = PR_Now(); +} + +} // dom namespace +} // mozilla namespace diff --git a/dom/performance/PerformanceService.h b/dom/performance/PerformanceService.h new file mode 100644 index 000000000..9abbd674d --- /dev/null +++ b/dom/performance/PerformanceService.h @@ -0,0 +1,48 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef dom_performance_PerformanceService_h +#define dom_performance_PerformanceService_h + +#include "mozilla/TimeStamp.h" +#include "nsCOMPtr.h" +#include "nsDOMNavigationTiming.h" + +namespace mozilla { +namespace dom { + +// This class is thread-safe. + +// We use this singleton for having the correct value of performance.timeOrigin. +// This value must be calculated on top of the pair: +// - mCreationTimeStamp (monotonic clock) +// - mCreationEpochTime (unix epoch time) +// These 2 values must be taken "at the same time" in order to be used +// correctly. + +class PerformanceService +{ +public: + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(PerformanceService) + + static PerformanceService* + GetOrCreate(); + + DOMHighResTimeStamp + TimeOrigin(const TimeStamp& aCreationTimeStamp) const; + +private: + PerformanceService(); + ~PerformanceService() = default; + + TimeStamp mCreationTimeStamp; + PRTime mCreationEpochTime; +}; + +} // dom namespace +} // mozilla namespace + +#endif // dom_performance_PerformanceService_h diff --git a/dom/performance/moz.build b/dom/performance/moz.build index 3286a0a4c..04f07f061 100644 --- a/dom/performance/moz.build +++ b/dom/performance/moz.build @@ -13,6 +13,7 @@ EXPORTS.mozilla.dom += [ 'PerformanceObserver.h', 'PerformanceObserverEntryList.h', 'PerformanceResourceTiming.h', + 'PerformanceService.h', 'PerformanceTiming.h', ] @@ -26,6 +27,7 @@ UNIFIED_SOURCES += [ 'PerformanceObserver.cpp', 'PerformanceObserverEntryList.cpp', 'PerformanceResourceTiming.cpp', + 'PerformanceService.cpp', 'PerformanceTiming.cpp', 'PerformanceWorker.cpp', ] -- cgit v1.2.3