Bug 1313420 - Implement Performance.timeOrigin - part 1, r=bz

This commit is contained in:
Andrea Marchesini 2016-11-17 10:00:05 +01:00
parent 0b046c7ab4
commit b0435b9411
6 changed files with 117 additions and 1 deletions

View File

@ -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"
@ -141,6 +142,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<JSObject*> aGivenProto)
{

View File

@ -24,6 +24,7 @@ namespace dom {
class PerformanceEntry;
class PerformanceNavigation;
class PerformanceObserver;
class PerformanceService;
class PerformanceTiming;
namespace workers {
@ -70,6 +71,8 @@ public:
virtual DOMHighResTimeStamp Now() const = 0;
DOMHighResTimeStamp TimeOrigin();
void Mark(const nsAString& aName, ErrorResult& aRv);
void ClearMarks(const Optional<nsAString>& aName);
@ -154,6 +157,8 @@ private:
uint64_t mResourceTimingBufferSize;
static const uint64_t kDefaultResourceTimingBufferSize = 150;
bool mPendingNotificationObserversTask;
RefPtr<PerformanceService> mPerformanceService;
};
} // namespace dom

View File

@ -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<PerformanceService> 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

View File

@ -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

View File

@ -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',
]

View File

@ -17,6 +17,9 @@ typedef sequence <PerformanceEntry> PerformanceEntryList;
interface Performance {
[DependsOn=DeviceState, Affects=Nothing]
DOMHighResTimeStamp now();
[Constant]
readonly attribute DOMHighResTimeStamp timeOrigin;
};
[Exposed=Window]