1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "mozilla/dom/PerformanceNavigationTiming.h"
8 #include "mozilla/dom/PerformanceNavigationTimingBinding.h"
9 
10 using namespace mozilla::dom;
11 
12 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PerformanceNavigationTiming)
NS_INTERFACE_MAP_END_INHERITING(PerformanceResourceTiming)13 NS_INTERFACE_MAP_END_INHERITING(PerformanceResourceTiming)
14 
15 NS_IMPL_ADDREF_INHERITED(PerformanceNavigationTiming, PerformanceResourceTiming)
16 NS_IMPL_RELEASE_INHERITED(PerformanceNavigationTiming,
17                           PerformanceResourceTiming)
18 
19 JSObject* PerformanceNavigationTiming::WrapObject(
20     JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
21   return PerformanceNavigationTimingBinding::Wrap(aCx, this, aGivenProto);
22 }
23 
24 #define REDUCE_TIME_PRECISION                      \
25   if (mPerformance->IsSystemPrincipal()) {         \
26     return rawValue;                               \
27   }                                                \
28   return nsRFPService::ReduceTimePrecisionAsMSecs( \
29       rawValue, mPerformance->GetRandomTimelineSeed())
30 
UnloadEventStart() const31 DOMHighResTimeStamp PerformanceNavigationTiming::UnloadEventStart() const {
32   DOMHighResTimeStamp rawValue = 0;
33   /*
34    * Per Navigation Timing Level 2, the value is 0 if
35    * a. there is no previous document or
36    * b. the same-origin-check fails.
37    *
38    * The same-origin-check is defined as:
39    * 1. If the previous document exists and its origin is not same
40    *    origin as the current document's origin, return "fail".
41    * 2. Let request be the current document's request.
42    * 3. If request's redirect count is not zero, and all of request's
43    *    HTTP redirects have the same origin as the current document,
44    *    return "pass".
45    * 4. Otherwise, return "fail".
46    */
47   if (mTimingData->AllRedirectsSameOrigin()) {  // same-origin-check:2/3
48     /*
49      * GetUnloadEventStartHighRes returns 0 if
50      * 1. there is no previous document (a, above) or
51      * 2. the current URI does not have the same origin as
52      *    the previous document's URI. (same-origin-check:1)
53      */
54     rawValue = mPerformance->GetDOMTiming()->GetUnloadEventStartHighRes();
55   }
56 
57   REDUCE_TIME_PRECISION;
58 }
59 
UnloadEventEnd() const60 DOMHighResTimeStamp PerformanceNavigationTiming::UnloadEventEnd() const {
61   DOMHighResTimeStamp rawValue = 0;
62 
63   // See comments in PerformanceNavigationTiming::UnloadEventEnd().
64   if (mTimingData->AllRedirectsSameOrigin()) {
65     rawValue = mPerformance->GetDOMTiming()->GetUnloadEventEndHighRes();
66   }
67 
68   REDUCE_TIME_PRECISION;
69 }
70 
DomInteractive() const71 DOMHighResTimeStamp PerformanceNavigationTiming::DomInteractive() const {
72   DOMHighResTimeStamp rawValue =
73       mPerformance->GetDOMTiming()->GetDomInteractiveHighRes();
74 
75   REDUCE_TIME_PRECISION;
76 }
77 
DomContentLoadedEventStart() const78 DOMHighResTimeStamp PerformanceNavigationTiming::DomContentLoadedEventStart()
79     const {
80   DOMHighResTimeStamp rawValue =
81       mPerformance->GetDOMTiming()->GetDomContentLoadedEventStartHighRes();
82 
83   REDUCE_TIME_PRECISION;
84 }
85 
DomContentLoadedEventEnd() const86 DOMHighResTimeStamp PerformanceNavigationTiming::DomContentLoadedEventEnd()
87     const {
88   DOMHighResTimeStamp rawValue =
89       mPerformance->GetDOMTiming()->GetDomContentLoadedEventEndHighRes();
90 
91   REDUCE_TIME_PRECISION;
92 }
93 
DomComplete() const94 DOMHighResTimeStamp PerformanceNavigationTiming::DomComplete() const {
95   DOMHighResTimeStamp rawValue =
96       mPerformance->GetDOMTiming()->GetDomCompleteHighRes();
97 
98   REDUCE_TIME_PRECISION;
99 }
100 
LoadEventStart() const101 DOMHighResTimeStamp PerformanceNavigationTiming::LoadEventStart() const {
102   DOMHighResTimeStamp rawValue =
103       mPerformance->GetDOMTiming()->GetLoadEventStartHighRes();
104 
105   REDUCE_TIME_PRECISION;
106 }
107 
LoadEventEnd() const108 DOMHighResTimeStamp PerformanceNavigationTiming::LoadEventEnd() const {
109   DOMHighResTimeStamp rawValue =
110       mPerformance->GetDOMTiming()->GetLoadEventEndHighRes();
111 
112   REDUCE_TIME_PRECISION;
113 }
114 
Type() const115 NavigationType PerformanceNavigationTiming::Type() const {
116   switch (mPerformance->GetDOMTiming()->GetType()) {
117     case nsDOMNavigationTiming::TYPE_NAVIGATE:
118       return NavigationType::Navigate;
119       break;
120     case nsDOMNavigationTiming::TYPE_RELOAD:
121       return NavigationType::Reload;
122       break;
123     case nsDOMNavigationTiming::TYPE_BACK_FORWARD:
124       return NavigationType::Back_forward;
125       break;
126     default:
127       // The type is TYPE_RESERVED or some other value that was later added.
128       // We fallback to the default of Navigate.
129       return NavigationType::Navigate;
130   }
131 }
132 
RedirectCount() const133 uint16_t PerformanceNavigationTiming::RedirectCount() const {
134   return mTimingData->GetRedirectCount();
135 }
136