1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsDOMNavigationTiming.h"
7 #include "nsPerformance.h"
9 #include "nsContentUtils.h"
10 #include "nsIScriptSecurityManager.h"
13 #include "mozilla/TimeStamp.h"
15 nsDOMNavigationTiming::nsDOMNavigationTiming()
20 nsDOMNavigationTiming::~nsDOMNavigationTiming()
25 nsDOMNavigationTiming::Clear()
27 mNavigationType
= mozilla::dom::PerformanceNavigation::TYPE_RESERVED
;
28 mNavigationStartHighRes
= 0;
29 mBeforeUnloadStart
= 0;
36 mDOMContentLoadedEventStart
= 0;
37 mDOMContentLoadedEventEnd
= 0;
40 mLoadEventStartSet
= false;
41 mLoadEventEndSet
= false;
42 mDOMLoadingSet
= false;
43 mDOMInteractiveSet
= false;
44 mDOMContentLoadedEventStartSet
= false;
45 mDOMContentLoadedEventEndSet
= false;
46 mDOMCompleteSet
= false;
50 nsDOMNavigationTiming::TimeStampToDOM(mozilla::TimeStamp aStamp
) const
52 if (aStamp
.IsNull()) {
55 mozilla::TimeDuration duration
= aStamp
- mNavigationStartTimeStamp
;
56 return GetNavigationStart() + static_cast<int64_t>(duration
.ToMilliseconds());
59 DOMTimeMilliSec
nsDOMNavigationTiming::DurationFromStart(){
60 return TimeStampToDOM(mozilla::TimeStamp::Now());
64 nsDOMNavigationTiming::NotifyNavigationStart()
66 mNavigationStartHighRes
= (double)PR_Now() / PR_USEC_PER_MSEC
;
67 mNavigationStartTimeStamp
= mozilla::TimeStamp::Now();
71 nsDOMNavigationTiming::NotifyFetchStart(nsIURI
* aURI
, nsDOMPerformanceNavigationType aNavigationType
)
73 mNavigationType
= aNavigationType
;
74 // At the unload event time we don't really know the loading uri.
75 // Need it for later check for unload timing access.
80 nsDOMNavigationTiming::NotifyBeforeUnload()
82 mBeforeUnloadStart
= DurationFromStart();
86 nsDOMNavigationTiming::NotifyUnloadAccepted(nsIURI
* aOldURI
)
88 mUnloadStart
= mBeforeUnloadStart
;
89 mUnloadedURI
= aOldURI
;
93 nsDOMNavigationTiming::NotifyUnloadEventStart()
95 mUnloadStart
= DurationFromStart();
99 nsDOMNavigationTiming::NotifyUnloadEventEnd()
101 mUnloadEnd
= DurationFromStart();
105 nsDOMNavigationTiming::NotifyLoadEventStart()
107 if (!mLoadEventStartSet
) {
108 mLoadEventStart
= DurationFromStart();
109 mLoadEventStartSet
= true;
114 nsDOMNavigationTiming::NotifyLoadEventEnd()
116 if (!mLoadEventEndSet
) {
117 mLoadEventEnd
= DurationFromStart();
118 mLoadEventEndSet
= true;
123 nsDOMNavigationTiming::SetDOMLoadingTimeStamp(nsIURI
* aURI
, mozilla::TimeStamp aValue
)
125 if (!mDOMLoadingSet
) {
127 mDOMLoading
= TimeStampToDOM(aValue
);
128 mDOMLoadingSet
= true;
133 nsDOMNavigationTiming::NotifyDOMLoading(nsIURI
* aURI
)
135 if (!mDOMLoadingSet
) {
137 mDOMLoading
= DurationFromStart();
138 mDOMLoadingSet
= true;
143 nsDOMNavigationTiming::NotifyDOMInteractive(nsIURI
* aURI
)
145 if (!mDOMInteractiveSet
) {
147 mDOMInteractive
= DurationFromStart();
148 mDOMInteractiveSet
= true;
153 nsDOMNavigationTiming::NotifyDOMComplete(nsIURI
* aURI
)
155 if (!mDOMCompleteSet
) {
157 mDOMComplete
= DurationFromStart();
158 mDOMCompleteSet
= true;
163 nsDOMNavigationTiming::NotifyDOMContentLoadedStart(nsIURI
* aURI
)
165 if (!mDOMContentLoadedEventStartSet
) {
167 mDOMContentLoadedEventStart
= DurationFromStart();
168 mDOMContentLoadedEventStartSet
= true;
173 nsDOMNavigationTiming::NotifyDOMContentLoadedEnd(nsIURI
* aURI
)
175 if (!mDOMContentLoadedEventEndSet
) {
177 mDOMContentLoadedEventEnd
= DurationFromStart();
178 mDOMContentLoadedEventEndSet
= true;
183 nsDOMNavigationTiming::GetUnloadEventStart()
185 nsIScriptSecurityManager
* ssm
= nsContentUtils::GetSecurityManager();
186 nsresult rv
= ssm
->CheckSameOriginURI(mLoadedURI
, mUnloadedURI
, false);
187 if (NS_SUCCEEDED(rv
)) {
194 nsDOMNavigationTiming::GetUnloadEventEnd()
196 nsIScriptSecurityManager
* ssm
= nsContentUtils::GetSecurityManager();
197 nsresult rv
= ssm
->CheckSameOriginURI(mLoadedURI
, mUnloadedURI
, false);
198 if (NS_SUCCEEDED(rv
)) {