Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/base/nsDOMNavigationTiming.cpp
Line
Count
Source (jump to first uncovered line)
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 "nsDOMNavigationTiming.h"
8
9
#include "GeckoProfiler.h"
10
#include "nsCOMPtr.h"
11
#include "nsContentUtils.h"
12
#include "nsDocShell.h"
13
#include "nsHttp.h"
14
#include "nsIDocShellTreeItem.h"
15
#include "nsIScriptSecurityManager.h"
16
#include "prtime.h"
17
#include "nsIURI.h"
18
#include "nsPrintfCString.h"
19
#include "mozilla/dom/PerformanceNavigation.h"
20
#include "mozilla/TimeStamp.h"
21
#include "mozilla/Telemetry.h"
22
23
using namespace mozilla;
24
25
nsDOMNavigationTiming::nsDOMNavigationTiming(nsDocShell* aDocShell)
26
0
{
27
0
  Clear();
28
0
29
0
  mDocShell = aDocShell;
30
0
}
31
32
nsDOMNavigationTiming::~nsDOMNavigationTiming()
33
0
{
34
0
}
35
36
void
37
nsDOMNavigationTiming::Clear()
38
0
{
39
0
  mNavigationType = TYPE_RESERVED;
40
0
  mNavigationStartHighRes = 0;
41
0
42
0
  mBeforeUnloadStart = TimeStamp();
43
0
  mUnloadStart = TimeStamp();
44
0
  mUnloadEnd = TimeStamp();
45
0
  mLoadEventStart = TimeStamp();
46
0
  mLoadEventEnd = TimeStamp();
47
0
  mDOMLoading = TimeStamp();
48
0
  mDOMInteractive = TimeStamp();
49
0
  mDOMContentLoadedEventStart = TimeStamp();
50
0
  mDOMContentLoadedEventEnd = TimeStamp();
51
0
  mDOMComplete = TimeStamp();
52
0
53
0
  mDocShellHasBeenActiveSinceNavigationStart = false;
54
0
}
55
56
DOMTimeMilliSec
57
nsDOMNavigationTiming::TimeStampToDOM(TimeStamp aStamp) const
58
0
{
59
0
  if (aStamp.IsNull()) {
60
0
    return 0;
61
0
  }
62
0
63
0
  TimeDuration duration = aStamp - mNavigationStart;
64
0
  return GetNavigationStart() + static_cast<int64_t>(duration.ToMilliseconds());
65
0
}
66
67
void
68
nsDOMNavigationTiming::NotifyNavigationStart(DocShellState aDocShellState)
69
0
{
70
0
  mNavigationStartHighRes = (double)PR_Now() / PR_USEC_PER_MSEC;
71
0
  mNavigationStart = TimeStamp::Now();
72
0
  mDocShellHasBeenActiveSinceNavigationStart = (aDocShellState == DocShellState::eActive);
73
0
  PROFILER_ADD_MARKER("Navigation::Start");
74
0
}
75
76
void
77
nsDOMNavigationTiming::NotifyFetchStart(nsIURI* aURI, Type aNavigationType)
78
0
{
79
0
  mNavigationType = aNavigationType;
80
0
  // At the unload event time we don't really know the loading uri.
81
0
  // Need it for later check for unload timing access.
82
0
  mLoadedURI = aURI;
83
0
}
84
85
void
86
nsDOMNavigationTiming::NotifyBeforeUnload()
87
0
{
88
0
  mBeforeUnloadStart = TimeStamp::Now();
89
0
}
90
91
void
92
nsDOMNavigationTiming::NotifyUnloadAccepted(nsIURI* aOldURI)
93
0
{
94
0
  mUnloadStart = mBeforeUnloadStart;
95
0
  mUnloadedURI = aOldURI;
96
0
}
97
98
void
99
nsDOMNavigationTiming::NotifyUnloadEventStart()
100
0
{
101
0
  mUnloadStart = TimeStamp::Now();
102
0
  PROFILER_TRACING("Navigation", "Unload", TRACING_INTERVAL_START);
103
0
}
104
105
void
106
nsDOMNavigationTiming::NotifyUnloadEventEnd()
107
0
{
108
0
  mUnloadEnd = TimeStamp::Now();
109
0
  PROFILER_TRACING("Navigation", "Unload", TRACING_INTERVAL_END);
110
0
}
111
112
void
113
nsDOMNavigationTiming::NotifyLoadEventStart()
114
0
{
115
0
  if (!mLoadEventStart.IsNull()) {
116
0
    return;
117
0
  }
118
0
  mLoadEventStart = TimeStamp::Now();
119
0
120
0
  PROFILER_TRACING("Navigation", "Load", TRACING_INTERVAL_START);
121
0
122
0
  if (IsTopLevelContentDocumentInContentProcess()) {
123
0
    TimeStamp now = TimeStamp::Now();
124
0
125
0
    Telemetry::AccumulateTimeDelta(Telemetry::TIME_TO_LOAD_EVENT_START_MS,
126
0
                                   mNavigationStart,
127
0
                                   now);
128
0
129
0
    if (mDocShellHasBeenActiveSinceNavigationStart) {
130
0
      if (net::nsHttp::IsBeforeLastActiveTabLoadOptimization(mNavigationStart)) {
131
0
        Telemetry::AccumulateTimeDelta(Telemetry::TIME_TO_LOAD_EVENT_START_ACTIVE_NETOPT_MS,
132
0
                                       mNavigationStart,
133
0
                                       now);
134
0
      } else {
135
0
        Telemetry::AccumulateTimeDelta(Telemetry::TIME_TO_LOAD_EVENT_START_ACTIVE_MS,
136
0
                                       mNavigationStart,
137
0
                                       now);
138
0
      }
139
0
    }
140
0
  }
141
0
}
142
143
void
144
nsDOMNavigationTiming::NotifyLoadEventEnd()
145
0
{
146
0
  if (!mLoadEventEnd.IsNull()) {
147
0
    return;
148
0
  }
149
0
  mLoadEventEnd = TimeStamp::Now();
150
0
151
0
  PROFILER_TRACING("Navigation", "Load", TRACING_INTERVAL_END);
152
0
153
0
  if (IsTopLevelContentDocumentInContentProcess()) {
154
0
    Telemetry::AccumulateTimeDelta(Telemetry::TIME_TO_LOAD_EVENT_END_MS,
155
0
                                   mNavigationStart);
156
0
  }
157
0
}
158
159
void
160
nsDOMNavigationTiming::SetDOMLoadingTimeStamp(nsIURI* aURI, TimeStamp aValue)
161
0
{
162
0
  if (!mDOMLoading.IsNull()) {
163
0
    return;
164
0
  }
165
0
  mLoadedURI = aURI;
166
0
  mDOMLoading = aValue;
167
0
}
168
169
void
170
nsDOMNavigationTiming::NotifyDOMLoading(nsIURI* aURI)
171
0
{
172
0
  if (!mDOMLoading.IsNull()) {
173
0
    return;
174
0
  }
175
0
  mLoadedURI = aURI;
176
0
  mDOMLoading = TimeStamp::Now();
177
0
178
0
  PROFILER_ADD_MARKER("Navigation::DOMLoading");
179
0
}
180
181
void
182
nsDOMNavigationTiming::NotifyDOMInteractive(nsIURI* aURI)
183
0
{
184
0
  if (!mDOMInteractive.IsNull()) {
185
0
    return;
186
0
  }
187
0
  mLoadedURI = aURI;
188
0
  mDOMInteractive = TimeStamp::Now();
189
0
190
0
  PROFILER_ADD_MARKER("Navigation::DOMInteractive");
191
0
}
192
193
void
194
nsDOMNavigationTiming::NotifyDOMComplete(nsIURI* aURI)
195
0
{
196
0
  if (!mDOMComplete.IsNull()) {
197
0
    return;
198
0
  }
199
0
  mLoadedURI = aURI;
200
0
  mDOMComplete = TimeStamp::Now();
201
0
202
0
  PROFILER_ADD_MARKER("Navigation::DOMComplete");
203
0
}
204
205
void
206
nsDOMNavigationTiming::NotifyDOMContentLoadedStart(nsIURI* aURI)
207
0
{
208
0
  if (!mDOMContentLoadedEventStart.IsNull()) {
209
0
    return;
210
0
  }
211
0
212
0
  mLoadedURI = aURI;
213
0
  mDOMContentLoadedEventStart = TimeStamp::Now();
214
0
215
0
  PROFILER_TRACING("Navigation", "DOMContentLoaded", TRACING_INTERVAL_START);
216
0
217
0
  if (IsTopLevelContentDocumentInContentProcess()) {
218
0
    TimeStamp now = TimeStamp::Now();
219
0
220
0
    Telemetry::AccumulateTimeDelta(Telemetry::TIME_TO_DOM_CONTENT_LOADED_START_MS,
221
0
                                   mNavigationStart,
222
0
                                   now);
223
0
224
0
    if (mDocShellHasBeenActiveSinceNavigationStart) {
225
0
      if (net::nsHttp::IsBeforeLastActiveTabLoadOptimization(mNavigationStart)) {
226
0
        Telemetry::AccumulateTimeDelta(Telemetry::TIME_TO_DOM_CONTENT_LOADED_START_ACTIVE_NETOPT_MS,
227
0
                                       mNavigationStart,
228
0
                                       now);
229
0
      } else {
230
0
        Telemetry::AccumulateTimeDelta(Telemetry::TIME_TO_DOM_CONTENT_LOADED_START_ACTIVE_MS,
231
0
                                       mNavigationStart,
232
0
                                       now);
233
0
      }
234
0
    }
235
0
  }
236
0
}
237
238
void
239
nsDOMNavigationTiming::NotifyDOMContentLoadedEnd(nsIURI* aURI)
240
0
{
241
0
  if (!mDOMContentLoadedEventEnd.IsNull()) {
242
0
    return;
243
0
  }
244
0
245
0
  mLoadedURI = aURI;
246
0
  mDOMContentLoadedEventEnd = TimeStamp::Now();
247
0
248
0
  PROFILER_TRACING("Navigation", "DOMContentLoaded", TRACING_INTERVAL_END);
249
0
250
0
  if (IsTopLevelContentDocumentInContentProcess()) {
251
0
    Telemetry::AccumulateTimeDelta(Telemetry::TIME_TO_DOM_CONTENT_LOADED_END_MS,
252
0
                                   mNavigationStart);
253
0
  }
254
0
}
255
256
void
257
nsDOMNavigationTiming::NotifyNonBlankPaintForRootContentDocument()
258
0
{
259
0
  MOZ_ASSERT(NS_IsMainThread());
260
0
  MOZ_ASSERT(!mNavigationStart.IsNull());
261
0
262
0
  if (!mNonBlankPaint.IsNull()) {
263
0
    return;
264
0
  }
265
0
266
0
  mNonBlankPaint = TimeStamp::Now();
267
0
268
0
#ifdef MOZ_GECKO_PROFILER
269
0
  if (profiler_is_active()) {
270
0
    TimeDuration elapsed = mNonBlankPaint - mNavigationStart;
271
0
    nsAutoCString spec;
272
0
    if (mLoadedURI) {
273
0
      mLoadedURI->GetSpec(spec);
274
0
    }
275
0
    nsPrintfCString marker("Non-blank paint after %dms for URL %s, %s",
276
0
                           int(elapsed.ToMilliseconds()), spec.get(),
277
0
                           mDocShellHasBeenActiveSinceNavigationStart ? "foreground tab" : "this tab was inactive some of the time between navigation start and first non-blank paint");
278
0
    profiler_add_marker(marker.get());
279
0
  }
280
0
#endif
281
0
282
0
  if (mDocShellHasBeenActiveSinceNavigationStart) {
283
0
    if (net::nsHttp::IsBeforeLastActiveTabLoadOptimization(mNavigationStart)) {
284
0
      Telemetry::AccumulateTimeDelta(Telemetry::TIME_TO_NON_BLANK_PAINT_NETOPT_MS,
285
0
                                     mNavigationStart,
286
0
                                     mNonBlankPaint);
287
0
    } else {
288
0
      Telemetry::AccumulateTimeDelta(Telemetry::TIME_TO_NON_BLANK_PAINT_NO_NETOPT_MS,
289
0
                                     mNavigationStart,
290
0
                                     mNonBlankPaint);
291
0
    }
292
0
293
0
    Telemetry::AccumulateTimeDelta(Telemetry::TIME_TO_NON_BLANK_PAINT_MS,
294
0
                                   mNavigationStart,
295
0
                                   mNonBlankPaint);
296
0
  }
297
0
}
298
299
void
300
nsDOMNavigationTiming::NotifyDOMContentFlushedForRootContentDocument()
301
0
{
302
0
  MOZ_ASSERT(NS_IsMainThread());
303
0
  MOZ_ASSERT(!mNavigationStart.IsNull());
304
0
305
0
  if (!mDOMContentFlushed.IsNull()) {
306
0
    return;
307
0
  }
308
0
309
0
  mDOMContentFlushed = TimeStamp::Now();
310
0
311
0
#ifdef MOZ_GECKO_PROFILER
312
0
  if (profiler_is_active()) {
313
0
    TimeDuration elapsed = mDOMContentFlushed - mNavigationStart;
314
0
    nsAutoCString spec;
315
0
    if (mLoadedURI) {
316
0
      mLoadedURI->GetSpec(spec);
317
0
    }
318
0
    nsPrintfCString marker("DOMContentFlushed after %dms for URL %s, %s",
319
0
                           int(elapsed.ToMilliseconds()), spec.get(),
320
0
                           mDocShellHasBeenActiveSinceNavigationStart ? "foreground tab" : "this tab was inactive some of the time between navigation start and DOMContentFlushed");
321
0
    profiler_add_marker(marker.get());
322
0
  }
323
0
#endif
324
0
}
325
326
void
327
nsDOMNavigationTiming::NotifyDocShellStateChanged(DocShellState aDocShellState)
328
0
{
329
0
  mDocShellHasBeenActiveSinceNavigationStart &=
330
0
    (aDocShellState == DocShellState::eActive);
331
0
}
332
333
mozilla::TimeStamp
334
nsDOMNavigationTiming::GetUnloadEventStartTimeStamp() const
335
0
{
336
0
  nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager();
337
0
  // todo: if you intend to update CheckSameOriginURI to log the error to the
338
0
  // console you also need to update the 'aFromPrivateWindow' argument.
339
0
  nsresult rv = ssm->CheckSameOriginURI(mLoadedURI, mUnloadedURI, false, false);
340
0
  if (NS_SUCCEEDED(rv)) {
341
0
    return mUnloadStart;
342
0
  }
343
0
  return mozilla::TimeStamp();
344
0
}
345
346
mozilla::TimeStamp
347
nsDOMNavigationTiming::GetUnloadEventEndTimeStamp() const
348
0
{
349
0
  nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager();
350
0
  // todo: if you intend to update CheckSameOriginURI to log the error to the
351
0
  // console you also need to update the 'aFromPrivateWindow' argument.
352
0
  nsresult rv = ssm->CheckSameOriginURI(mLoadedURI, mUnloadedURI, false, false);
353
0
  if (NS_SUCCEEDED(rv)) {
354
0
    return mUnloadEnd;
355
0
  }
356
0
  return mozilla::TimeStamp();
357
0
}
358
359
bool
360
nsDOMNavigationTiming::IsTopLevelContentDocumentInContentProcess() const
361
0
{
362
0
  if (!mDocShell) {
363
0
    return false;
364
0
  }
365
0
  if (!XRE_IsContentProcess()) {
366
0
    return false;
367
0
  }
368
0
  nsCOMPtr<nsIDocShellTreeItem> rootItem;
369
0
  Unused << mDocShell->GetSameTypeRootTreeItem(getter_AddRefs(rootItem));
370
0
  if (rootItem.get() != static_cast<nsIDocShellTreeItem*>(mDocShell.get())) {
371
0
    return false;
372
0
  }
373
0
  return rootItem->ItemType() == nsIDocShellTreeItem::typeContent;
374
0
}