forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPerformanceMainThread.cpp
411 lines (347 loc) · 13.5 KB
/
PerformanceMainThread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/* -*- 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 "PerformanceMainThread.h"
#include "PerformanceNavigation.h"
#include "mozilla/StaticPrefs_dom.h"
#include "mozilla/StaticPrefs_privacy.h"
namespace mozilla {
namespace dom {
namespace {
void GetURLSpecFromChannel(nsITimedChannel* aChannel, nsAString& aSpec) {
aSpec.AssignLiteral("document");
nsCOMPtr<nsIChannel> channel = do_QueryInterface(aChannel);
if (!channel) {
return;
}
nsCOMPtr<nsIURI> uri;
nsresult rv = channel->GetURI(getter_AddRefs(uri));
if (NS_WARN_IF(NS_FAILED(rv)) || !uri) {
return;
}
nsAutoCString spec;
rv = uri->GetSpec(spec);
if (NS_WARN_IF(NS_FAILED(rv))) {
return;
}
aSpec = NS_ConvertUTF8toUTF16(spec);
}
} // namespace
NS_IMPL_CYCLE_COLLECTION_CLASS(PerformanceMainThread)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(PerformanceMainThread,
Performance)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mTiming, mNavigation, mDocEntry)
tmp->mMozMemory = nullptr;
mozilla::DropJSObjects(this);
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(PerformanceMainThread,
Performance)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTiming, mNavigation, mDocEntry)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(PerformanceMainThread,
Performance)
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mMozMemory)
NS_IMPL_CYCLE_COLLECTION_TRACE_END
NS_IMPL_ADDREF_INHERITED(PerformanceMainThread, Performance)
NS_IMPL_RELEASE_INHERITED(PerformanceMainThread, Performance)
// QueryInterface implementation for PerformanceMainThread
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PerformanceMainThread)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END_INHERITING(Performance)
PerformanceMainThread::PerformanceMainThread(nsPIDOMWindowInner* aWindow,
nsDOMNavigationTiming* aDOMTiming,
nsITimedChannel* aChannel,
bool aPrincipal)
: Performance(aWindow, aPrincipal),
mDOMTiming(aDOMTiming),
mChannel(aChannel),
mCrossOriginIsolated(aWindow->AsGlobal()->CrossOriginIsolated()) {
MOZ_ASSERT(aWindow, "Parent window object should be provided");
CreateNavigationTimingEntry();
}
PerformanceMainThread::~PerformanceMainThread() {
mozilla::DropJSObjects(this);
}
void PerformanceMainThread::GetMozMemory(JSContext* aCx,
JS::MutableHandle<JSObject*> aObj) {
if (!mMozMemory) {
JS::Rooted<JSObject*> mozMemoryObj(aCx, JS_NewPlainObject(aCx));
JS::Rooted<JSObject*> gcMemoryObj(aCx, js::gc::NewMemoryInfoObject(aCx));
if (!mozMemoryObj || !gcMemoryObj) {
MOZ_CRASH("out of memory creating performance.mozMemory");
}
if (!JS_DefineProperty(aCx, mozMemoryObj, "gc", gcMemoryObj,
JSPROP_ENUMERATE)) {
MOZ_CRASH("out of memory creating performance.mozMemory");
}
mMozMemory = mozMemoryObj;
mozilla::HoldJSObjects(this);
}
aObj.set(mMozMemory);
}
PerformanceTiming* PerformanceMainThread::Timing() {
if (!mTiming) {
// For navigation timing, the third argument (an nsIHttpChannel) is null
// since the cross-domain redirect were already checked. The last argument
// (zero time) for performance.timing is the navigation start value.
mTiming = new PerformanceTiming(this, mChannel, nullptr,
mDOMTiming->GetNavigationStart());
}
return mTiming;
}
void PerformanceMainThread::DispatchBufferFullEvent() {
RefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr);
// it bubbles, and it isn't cancelable
event->InitEvent(NS_LITERAL_STRING("resourcetimingbufferfull"), true, false);
event->SetTrusted(true);
DispatchEvent(*event);
}
PerformanceNavigation* PerformanceMainThread::Navigation() {
if (!mNavigation) {
mNavigation = new PerformanceNavigation(this);
}
return mNavigation;
}
/**
* An entry should be added only after the resource is loaded.
* This method is not thread safe and can only be called on the main thread.
*/
void PerformanceMainThread::AddEntry(nsIHttpChannel* channel,
nsITimedChannel* timedChannel) {
MOZ_ASSERT(NS_IsMainThread());
nsAutoString initiatorType;
nsAutoString entryName;
UniquePtr<PerformanceTimingData> performanceTimingData(
PerformanceTimingData::Create(timedChannel, channel, 0, initiatorType,
entryName));
if (!performanceTimingData) {
return;
}
// The PerformanceResourceTiming object will use the PerformanceTimingData
// object to get all the required timings.
RefPtr<PerformanceResourceTiming> performanceEntry =
new PerformanceResourceTiming(std::move(performanceTimingData), this,
entryName);
performanceEntry->SetInitiatorType(initiatorType);
InsertResourceEntry(performanceEntry);
}
// To be removed once bug 1124165 lands
bool PerformanceMainThread::IsPerformanceTimingAttribute(
const nsAString& aName) {
// Note that toJSON is added to this list due to bug 1047848
static const char* attributes[] = {"navigationStart",
"unloadEventStart",
"unloadEventEnd",
"redirectStart",
"redirectEnd",
"fetchStart",
"domainLookupStart",
"domainLookupEnd",
"connectStart",
"secureConnectionStart",
"connectEnd",
"requestStart",
"responseStart",
"responseEnd",
"domLoading",
"domInteractive",
"domContentLoadedEventStart",
"domContentLoadedEventEnd",
"domComplete",
"loadEventStart",
"loadEventEnd",
nullptr};
for (uint32_t i = 0; attributes[i]; ++i) {
if (aName.EqualsASCII(attributes[i])) {
return true;
}
}
return false;
}
DOMHighResTimeStamp PerformanceMainThread::GetPerformanceTimingFromString(
const nsAString& aProperty) {
// ::Measure expects the values returned from this function to be passed
// through ReduceTimePrecision already.
if (!IsPerformanceTimingAttribute(aProperty)) {
return 0;
}
// Values from Timing() are already reduced
if (aProperty.EqualsLiteral("redirectStart")) {
return Timing()->RedirectStart();
}
if (aProperty.EqualsLiteral("redirectEnd")) {
return Timing()->RedirectEnd();
}
if (aProperty.EqualsLiteral("fetchStart")) {
return Timing()->FetchStart();
}
if (aProperty.EqualsLiteral("domainLookupStart")) {
return Timing()->DomainLookupStart();
}
if (aProperty.EqualsLiteral("domainLookupEnd")) {
return Timing()->DomainLookupEnd();
}
if (aProperty.EqualsLiteral("connectStart")) {
return Timing()->ConnectStart();
}
if (aProperty.EqualsLiteral("secureConnectionStart")) {
return Timing()->SecureConnectionStart();
}
if (aProperty.EqualsLiteral("connectEnd")) {
return Timing()->ConnectEnd();
}
if (aProperty.EqualsLiteral("requestStart")) {
return Timing()->RequestStart();
}
if (aProperty.EqualsLiteral("responseStart")) {
return Timing()->ResponseStart();
}
if (aProperty.EqualsLiteral("responseEnd")) {
return Timing()->ResponseEnd();
}
// Values from GetDOMTiming() are not.
DOMHighResTimeStamp retValue;
if (aProperty.EqualsLiteral("navigationStart")) {
// DOMHighResTimeStamp is in relation to navigationStart, so this will be
// zero.
retValue = GetDOMTiming()->GetNavigationStart();
} else if (aProperty.EqualsLiteral("unloadEventStart")) {
retValue = GetDOMTiming()->GetUnloadEventStart();
} else if (aProperty.EqualsLiteral("unloadEventEnd")) {
retValue = GetDOMTiming()->GetUnloadEventEnd();
} else if (aProperty.EqualsLiteral("domLoading")) {
retValue = GetDOMTiming()->GetDomLoading();
} else if (aProperty.EqualsLiteral("domInteractive")) {
retValue = GetDOMTiming()->GetDomInteractive();
} else if (aProperty.EqualsLiteral("domContentLoadedEventStart")) {
retValue = GetDOMTiming()->GetDomContentLoadedEventStart();
} else if (aProperty.EqualsLiteral("domContentLoadedEventEnd")) {
retValue = GetDOMTiming()->GetDomContentLoadedEventEnd();
} else if (aProperty.EqualsLiteral("domComplete")) {
retValue = GetDOMTiming()->GetDomComplete();
} else if (aProperty.EqualsLiteral("loadEventStart")) {
retValue = GetDOMTiming()->GetLoadEventStart();
} else if (aProperty.EqualsLiteral("loadEventEnd")) {
retValue = GetDOMTiming()->GetLoadEventEnd();
} else {
MOZ_CRASH(
"IsPerformanceTimingAttribute and GetPerformanceTimingFromString are "
"out "
"of sync");
}
return nsRFPService::ReduceTimePrecisionAsMSecs(
retValue, GetRandomTimelineSeed(), /* aIsSystemPrinciapl */ false,
CrossOriginIsolated());
}
void PerformanceMainThread::InsertUserEntry(PerformanceEntry* aEntry) {
MOZ_ASSERT(NS_IsMainThread());
nsAutoCString uri;
uint64_t markCreationEpoch = 0;
if (StaticPrefs::dom_performance_enable_user_timing_logging() ||
StaticPrefs::dom_performance_enable_notify_performance_timing()) {
nsresult rv = NS_ERROR_FAILURE;
nsCOMPtr<nsPIDOMWindowInner> owner = GetOwner();
if (owner && owner->GetDocumentURI()) {
rv = owner->GetDocumentURI()->GetHost(uri);
}
if (NS_FAILED(rv)) {
// If we have no URI, just put in "none".
uri.AssignLiteral("none");
}
markCreationEpoch = static_cast<uint64_t>(PR_Now() / PR_USEC_PER_MSEC);
if (StaticPrefs::dom_performance_enable_user_timing_logging()) {
Performance::LogEntry(aEntry, uri);
}
}
if (StaticPrefs::dom_performance_enable_notify_performance_timing()) {
TimingNotification(aEntry, uri, markCreationEpoch);
}
Performance::InsertUserEntry(aEntry);
}
TimeStamp PerformanceMainThread::CreationTimeStamp() const {
return GetDOMTiming()->GetNavigationStartTimeStamp();
}
DOMHighResTimeStamp PerformanceMainThread::CreationTime() const {
return GetDOMTiming()->GetNavigationStart();
}
void PerformanceMainThread::CreateNavigationTimingEntry() {
MOZ_ASSERT(!mDocEntry, "mDocEntry should be null.");
if (!StaticPrefs::dom_enable_performance_navigation_timing() ||
StaticPrefs::privacy_resistFingerprinting()) {
return;
}
nsAutoString name;
GetURLSpecFromChannel(mChannel, name);
UniquePtr<PerformanceTimingData> timing(
new PerformanceTimingData(mChannel, nullptr, 0));
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(mChannel);
if (httpChannel) {
timing->SetPropertiesFromHttpChannel(httpChannel, mChannel);
}
mDocEntry = new PerformanceNavigationTiming(std::move(timing), this, name);
}
void PerformanceMainThread::QueueNavigationTimingEntry() {
if (!mDocEntry) {
return;
}
// Let's update some values.
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(mChannel);
if (httpChannel) {
mDocEntry->UpdatePropertiesFromHttpChannel(httpChannel, mChannel);
}
QueueEntry(mDocEntry);
}
bool PerformanceMainThread::CrossOriginIsolated() const {
return mCrossOriginIsolated;
}
void PerformanceMainThread::GetEntries(
nsTArray<RefPtr<PerformanceEntry>>& aRetval) {
// We return an empty list when 'privacy.resistFingerprinting' is on.
if (nsContentUtils::ShouldResistFingerprinting()) {
aRetval.Clear();
return;
}
aRetval = mResourceEntries.Clone();
aRetval.AppendElements(mUserEntries);
if (mDocEntry) {
aRetval.AppendElement(mDocEntry);
}
aRetval.Sort(PerformanceEntryComparator());
}
void PerformanceMainThread::GetEntriesByType(
const nsAString& aEntryType, nsTArray<RefPtr<PerformanceEntry>>& aRetval) {
// We return an empty list when 'privacy.resistFingerprinting' is on.
if (nsContentUtils::ShouldResistFingerprinting()) {
aRetval.Clear();
return;
}
if (aEntryType.EqualsLiteral("navigation")) {
aRetval.Clear();
if (mDocEntry) {
aRetval.AppendElement(mDocEntry);
}
return;
}
Performance::GetEntriesByType(aEntryType, aRetval);
}
void PerformanceMainThread::GetEntriesByName(
const nsAString& aName, const Optional<nsAString>& aEntryType,
nsTArray<RefPtr<PerformanceEntry>>& aRetval) {
// We return an empty list when 'privacy.resistFingerprinting' is on.
if (nsContentUtils::ShouldResistFingerprinting()) {
aRetval.Clear();
return;
}
Performance::GetEntriesByName(aName, aEntryType, aRetval);
// The navigation entry is the first one. If it exists and the name matches,
// let put it in front.
if (mDocEntry && mDocEntry->GetName().Equals(aName)) {
aRetval.InsertElementAt(0, mDocEntry);
return;
}
}
} // namespace dom
} // namespace mozilla