PerformanceTiming.cpp [plain text]
#include "config.h"
#include "PerformanceTiming.h"
#if ENABLE(WEB_TIMING)
#include "Document.h"
#include "DocumentLoadTiming.h"
#include "DocumentLoader.h"
#include "DocumentTiming.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "ResourceLoadTiming.h"
#include "ResourceResponse.h"
#include <wtf/CurrentTime.h>
namespace WebCore {
static unsigned long long toIntegerMilliseconds(double seconds)
{
ASSERT(seconds >= 0);
return static_cast<unsigned long long>(seconds * 1000.0);
}
PerformanceTiming::PerformanceTiming(Frame* frame)
: DOMWindowProperty(frame)
{
}
unsigned long long PerformanceTiming::navigationStart() const
{
DocumentLoadTiming* timing = documentLoadTiming();
if (!timing)
return 0;
return monotonicTimeToIntegerMilliseconds(timing->navigationStart());
}
unsigned long long PerformanceTiming::unloadEventStart() const
{
DocumentLoadTiming* timing = documentLoadTiming();
if (!timing)
return 0;
if (timing->hasCrossOriginRedirect() || !timing->hasSameOriginAsPreviousDocument())
return 0;
return monotonicTimeToIntegerMilliseconds(timing->unloadEventStart());
}
unsigned long long PerformanceTiming::unloadEventEnd() const
{
DocumentLoadTiming* timing = documentLoadTiming();
if (!timing)
return 0;
if (timing->hasCrossOriginRedirect() || !timing->hasSameOriginAsPreviousDocument())
return 0;
return monotonicTimeToIntegerMilliseconds(timing->unloadEventEnd());
}
unsigned long long PerformanceTiming::redirectStart() const
{
DocumentLoadTiming* timing = documentLoadTiming();
if (!timing)
return 0;
if (timing->hasCrossOriginRedirect())
return 0;
return monotonicTimeToIntegerMilliseconds(timing->redirectStart());
}
unsigned long long PerformanceTiming::redirectEnd() const
{
DocumentLoadTiming* timing = documentLoadTiming();
if (!timing)
return 0;
if (timing->hasCrossOriginRedirect())
return 0;
return monotonicTimeToIntegerMilliseconds(timing->redirectEnd());
}
unsigned long long PerformanceTiming::fetchStart() const
{
DocumentLoadTiming* timing = documentLoadTiming();
if (!timing)
return 0;
return monotonicTimeToIntegerMilliseconds(timing->fetchStart());
}
unsigned long long PerformanceTiming::domainLookupStart() const
{
DocumentLoader* loader = documentLoader();
if (!loader)
return fetchStart();
const ResourceLoadTiming& timing = loader->response().resourceLoadTiming();
if (timing.domainLookupStart < 0)
return fetchStart();
return resourceLoadTimeRelativeToFetchStart(timing.domainLookupStart);
}
unsigned long long PerformanceTiming::domainLookupEnd() const
{
DocumentLoader* loader = documentLoader();
if (!loader)
return domainLookupStart();
const ResourceLoadTiming& timing = loader->response().resourceLoadTiming();
if (timing.domainLookupEnd < 0)
return domainLookupStart();
return resourceLoadTimeRelativeToFetchStart(timing.domainLookupEnd);
}
unsigned long long PerformanceTiming::connectStart() const
{
DocumentLoader* loader = documentLoader();
if (!loader)
return domainLookupEnd();
const ResourceLoadTiming& timing = loader->response().resourceLoadTiming();
int connectStart = timing.connectStart;
if (connectStart < 0)
return domainLookupEnd();
if (timing.domainLookupEnd >= 0 && timing.domainLookupEnd > connectStart)
connectStart = timing.domainLookupEnd;
return resourceLoadTimeRelativeToFetchStart(connectStart);
}
unsigned long long PerformanceTiming::connectEnd() const
{
DocumentLoader* loader = documentLoader();
if (!loader)
return connectStart();
const ResourceLoadTiming& timing = loader->response().resourceLoadTiming();
if (timing.connectEnd < 0)
return connectStart();
return resourceLoadTimeRelativeToFetchStart(timing.connectEnd);
}
unsigned long long PerformanceTiming::secureConnectionStart() const
{
DocumentLoader* loader = documentLoader();
if (!loader)
return 0;
const ResourceLoadTiming& timing = loader->response().resourceLoadTiming();
if (timing.secureConnectionStart < 0)
return 0;
return resourceLoadTimeRelativeToFetchStart(timing.secureConnectionStart);
}
unsigned long long PerformanceTiming::requestStart() const
{
DocumentLoader* loader = documentLoader();
if (!loader)
return connectEnd();
const ResourceLoadTiming& timing = loader->response().resourceLoadTiming();
ASSERT(timing.requestStart >= 0);
return resourceLoadTimeRelativeToFetchStart(timing.requestStart);
}
unsigned long long PerformanceTiming::responseStart() const
{
DocumentLoader* loader = documentLoader();
if (!loader)
return requestStart();
const ResourceLoadTiming& timing = loader->response().resourceLoadTiming();
ASSERT(timing.responseStart >= 0);
return resourceLoadTimeRelativeToFetchStart(timing.responseStart);
}
unsigned long long PerformanceTiming::responseEnd() const
{
DocumentLoadTiming* timing = documentLoadTiming();
if (!timing)
return 0;
return monotonicTimeToIntegerMilliseconds(timing->responseEnd());
}
unsigned long long PerformanceTiming::domLoading() const
{
const DocumentTiming* timing = documentTiming();
if (!timing)
return fetchStart();
return monotonicTimeToIntegerMilliseconds(timing->domLoading);
}
unsigned long long PerformanceTiming::domInteractive() const
{
const DocumentTiming* timing = documentTiming();
if (!timing)
return 0;
return monotonicTimeToIntegerMilliseconds(timing->domInteractive);
}
unsigned long long PerformanceTiming::domContentLoadedEventStart() const
{
const DocumentTiming* timing = documentTiming();
if (!timing)
return 0;
return monotonicTimeToIntegerMilliseconds(timing->domContentLoadedEventStart);
}
unsigned long long PerformanceTiming::domContentLoadedEventEnd() const
{
const DocumentTiming* timing = documentTiming();
if (!timing)
return 0;
return monotonicTimeToIntegerMilliseconds(timing->domContentLoadedEventEnd);
}
unsigned long long PerformanceTiming::domComplete() const
{
const DocumentTiming* timing = documentTiming();
if (!timing)
return 0;
return monotonicTimeToIntegerMilliseconds(timing->domComplete);
}
unsigned long long PerformanceTiming::loadEventStart() const
{
DocumentLoadTiming* timing = documentLoadTiming();
if (!timing)
return 0;
return monotonicTimeToIntegerMilliseconds(timing->loadEventStart());
}
unsigned long long PerformanceTiming::loadEventEnd() const
{
DocumentLoadTiming* timing = documentLoadTiming();
if (!timing)
return 0;
return monotonicTimeToIntegerMilliseconds(timing->loadEventEnd());
}
DocumentLoader* PerformanceTiming::documentLoader() const
{
if (!m_frame)
return 0;
return m_frame->loader().documentLoader();
}
const DocumentTiming* PerformanceTiming::documentTiming() const
{
if (!m_frame)
return 0;
Document* document = m_frame->document();
if (!document)
return 0;
return &document->timing();
}
DocumentLoadTiming* PerformanceTiming::documentLoadTiming() const
{
DocumentLoader* loader = documentLoader();
if (!loader)
return 0;
return &loader->timing();
}
unsigned long long PerformanceTiming::resourceLoadTimeRelativeToFetchStart(int relativeMilliseconds) const
{
ASSERT(relativeMilliseconds >= 0);
return fetchStart() + relativeMilliseconds;
}
unsigned long long PerformanceTiming::monotonicTimeToIntegerMilliseconds(double monotonicSeconds) const
{
ASSERT(monotonicSeconds >= 0);
if (const DocumentLoadTiming* timing = documentLoadTiming())
return toIntegerMilliseconds(timing->monotonicTimeToPseudoWallTime(monotonicSeconds));
return 0;
}
}
#endif // ENABLE(WEB_TIMING)