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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
/*
* Copyright (C) 2013 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
WebInspector.TimelineManager = function()
{
WebInspector.Object.call(this);
WebInspector.Frame.addEventListener(WebInspector.Frame.Event.ProvisionalLoadStarted, this._startAutoRecording, this);
WebInspector.Frame.addEventListener(WebInspector.Frame.Event.MainResourceDidChange, this._mainResourceDidChange, this);
WebInspector.Frame.addEventListener(WebInspector.Frame.Event.ResourceWasAdded, this._resourceWasAdded, this);
this._recording = false;
this._records = [];
this._typeRecords = {};
this._loadEventTime = NaN;
this._eventMarkers = [];
};
WebInspector.TimelineManager.Event = {
RecordsCleared: "timeline-manager-records-cleared",
RecordingStarted: "timeline-manager-recording-started",
RecordingStopped: "timeline-manager-recording-stopped",
RecordAdded: "timeline-manager-record-added",
RecordedEventMarker: "timeline-manager-recorded-event-marker",
};
WebInspector.TimelineManager.MaximumAutoRecordDuration = 90000; // 90 seconds
WebInspector.TimelineManager.MaximumAutoRecordDurationAfterLoadEvent = 10000; // 10 seconds
WebInspector.TimelineManager.DeadTimeRequiredToStopAutoRecordingEarly = 2000; // 2 seconds
WebInspector.TimelineManager.prototype = {
constructor: WebInspector.TimelineManager,
// Public
get recording()
{
return this._recording;
},
get mainFrameLoadEventTime()
{
return this._loadEventTime;
},
get timelineEventMarkers()
{
return this._eventMarkers;
},
get records()
{
return this._records;
},
recordsWithType: function(type)
{
return this._typeRecords[type] || [];
},
startRecording: function()
{
if (this._recording)
return;
this._clear();
this._recording = true;
TimelineAgent.start();
this.dispatchEventToListeners(WebInspector.TimelineManager.Event.RecordingStarted);
},
stopRecording: function()
{
if (!this._recording)
return;
if (this._stopRecordingTimeout) {
clearTimeout(this._stopRecordingTimeout);
delete this._stopRecordingTimeout;
}
if (this._deadTimeTimeout) {
clearTimeout(this._deadTimeTimeout);
delete this._deadTimeTimeout;
}
TimelineAgent.stop();
this._recording = false;
this._autoRecordingMainResource = null;
this.dispatchEventToListeners(WebInspector.TimelineManager.Event.RecordingStopped);
},
eventRecorded: function(originalRecord)
{
// Called from WebInspector.TimelineObserver.
if (!this._recording)
return;
function processRecord(record, parentRecord)
{
// Convert the timestamps to seconds to match the resource timestamps.
var startTime = record.startTime / 1000;
var endTime = record.endTime / 1000;
var callFrames = this._callFramesFromPayload(record.stackTrace);
switch (record.type) {
case "MarkLoad":
var mainFrame = WebInspector.frameResourceManager.mainFrame;
console.assert(mainFrame);
if (!mainFrame || record.frameId !== mainFrame.id)
break;
this._loadEventTime = startTime;
var eventMarker = new WebInspector.TimelineEventMarker(startTime, WebInspector.TimelineEventMarker.Type.LoadEvent);
this._eventMarkers.push(eventMarker);
this.dispatchEventToListeners(WebInspector.TimelineManager.Event.RecordedEventMarker, {eventMarker: eventMarker});
this._stopAutoRecordingSoon();
break;
case "MarkDOMContent":
var mainFrame = WebInspector.frameResourceManager.mainFrame;
console.assert(mainFrame);
if (!mainFrame || record.frameId !== mainFrame.id)
break;
var eventMarker = new WebInspector.TimelineEventMarker(startTime, WebInspector.TimelineEventMarker.Type.DOMContentEvent);
this._eventMarkers.push(eventMarker);
this.dispatchEventToListeners(WebInspector.TimelineManager.Event.RecordedEventMarker, {eventMarker: eventMarker});
break;
case "ScheduleStyleRecalculation":
console.assert(isNaN(endTime));
// Pass the startTime as the endTime since this record type has no duration.
this._addRecord(new WebInspector.LayoutTimelineRecord(WebInspector.LayoutTimelineRecord.EventType.InvalidateStyles, startTime, startTime, callFrames));
break;
case "RecalculateStyles":
this._addRecord(new WebInspector.LayoutTimelineRecord(WebInspector.LayoutTimelineRecord.EventType.RecalculateStyles, startTime, endTime, callFrames));
break;
case "InvalidateLayout":
console.assert(isNaN(endTime));
// Pass the startTime as the endTime since this record type has no duration.
this._addRecord(new WebInspector.LayoutTimelineRecord(WebInspector.LayoutTimelineRecord.EventType.InvalidateLayout, startTime, startTime, callFrames));
break;
case "Layout":
// COMPATIBILITY (iOS 6): Layout records did not contain area properties. This is not exposed via a quad "root".
var quad = record.data.root ? new WebInspector.Quad(record.data.root) : null;
if (quad)
this._addRecord(new WebInspector.LayoutTimelineRecord(WebInspector.LayoutTimelineRecord.EventType.Layout, startTime, endTime, callFrames, quad.points[0].x, quad.points[0].y, quad.width, quad.height, quad));
else
this._addRecord(new WebInspector.LayoutTimelineRecord(WebInspector.LayoutTimelineRecord.EventType.Layout, startTime, endTime, callFrames));
break;
case "Paint":
// COMPATIBILITY (iOS 6): Paint records data contained x, y, width, height properties. This became a quad "clip".
var quad = record.data.clip ? new WebInspector.Quad(record.data.clip) : null;
if (quad)
this._addRecord(new WebInspector.LayoutTimelineRecord(WebInspector.LayoutTimelineRecord.EventType.Paint, startTime, endTime, callFrames, null, null, quad.width, quad.height, quad));
else
this._addRecord(new WebInspector.LayoutTimelineRecord(WebInspector.LayoutTimelineRecord.EventType.Paint, startTime, endTime, callFrames, record.data.x, record.data.y, record.data.width, record.data.height));
break;
case "EvaluateScript":
var resource = WebInspector.frameResourceManager.resourceForURL(record.data.url);
// The lineNumber is 1-based, but we expect 0-based.
var lineNumber = record.data.lineNumber - 1;
switch (parent ? parent.type : null) {
case "TimerFire":
this._addRecord(new WebInspector.ScriptTimelineRecord(WebInspector.ScriptTimelineRecord.EventType.TimerFired, startTime, endTime, parent.data.timerId, resource, lineNumber, callFrames));
break;
default:
this._addRecord(new WebInspector.ScriptTimelineRecord(WebInspector.ScriptTimelineRecord.EventType.ScriptEvaluated, startTime, endTime, null, resource, lineNumber, callFrames));
break;
}
break;
case "TimeStamp":
var eventMarker = new WebInspector.TimelineEventMarker(startTime, WebInspector.TimelineEventMarker.Type.TimeStamp);
this._eventMarkers.push(eventMarker);
this.dispatchEventToListeners(WebInspector.TimelineManager.Event.RecordedEventMarker, {eventMarker: eventMarker});
break;
case "FunctionCall":
// FunctionCall always happens as a child of another record, and since the FunctionCall record
// has useful info we just make the timeline record here (combining the data from both records).
if (!parent)
break;
var resource = WebInspector.frameResourceManager.resourceForURL(record.data.scriptName);
// The scriptLine is 1-based, but we expect 0-based.
var lineNumber = record.data.scriptLine - 1;
switch (parent.type) {
case "TimerFire":
this._addRecord(new WebInspector.ScriptTimelineRecord(WebInspector.ScriptTimelineRecord.EventType.TimerFired, startTime, endTime, parent.data.timerId, resource, lineNumber, callFrames));
break;
case "EventDispatch":
this._addRecord(new WebInspector.ScriptTimelineRecord(WebInspector.ScriptTimelineRecord.EventType.EventDispatched, startTime, endTime, parent.data.type, resource, lineNumber, callFrames));
break;
case "XHRLoad":
this._addRecord(new WebInspector.ScriptTimelineRecord(WebInspector.ScriptTimelineRecord.EventType.EventDispatched, startTime, endTime, "load", resource, lineNumber, callFrames));
break;
case "XHRReadyStateChange":
this._addRecord(new WebInspector.ScriptTimelineRecord(WebInspector.ScriptTimelineRecord.EventType.EventDispatched, startTime, endTime, "readystatechange", resource, lineNumber, callFrames));
break;
case "FireAnimationFrame":
this._addRecord(new WebInspector.ScriptTimelineRecord(WebInspector.ScriptTimelineRecord.EventType.AnimationFrameFired, startTime, endTime, parent.data.id, resource, lineNumber, callFrames));
break;
}
break;
case "TimerInstall":
case "TimerRemove":
// COMPATIBILITY (iOS 6): TimerInstall and TimerRemove did not have a stack trace.
var callFrame = null;
if (callFrames) {
for (var i = 0; i < callFrames.length; ++i) {
if (callFrames[i].nativeCode)
continue;
callFrame = callFrames[i];
break;
}
}
var sourceCodeLocation = callFrame && callFrame.sourceCodeLocation;
var resource = sourceCodeLocation ? sourceCodeLocation.sourceCode : null;
var lineNumber = sourceCodeLocation ? sourceCodeLocation.lineNumber : null;
if (record.type === "TimerInstall")
this._addRecord(new WebInspector.ScriptTimelineRecord(WebInspector.ScriptTimelineRecord.EventType.TimerInstalled, startTime, endTime, record.data.timerId, resource, lineNumber, callFrames));
else
this._addRecord(new WebInspector.ScriptTimelineRecord(WebInspector.ScriptTimelineRecord.EventType.TimerRemoved, startTime, endTime, record.data.timerId, resource, lineNumber, callFrames));
break;
}
return null;
}
// Iterate over the records tree using a stack. Doing this recursively has
// been known to cause a call stack overflow. https://webkit.org/b/79106
var stack = [{array: [originalRecord], parent: null, index: 0}];
while (stack.length) {
var entry = stack.lastValue;
var records = entry.array;
var parent = entry.parent;
if (entry.index < records.length) {
var record = records[entry.index];
processRecord.call(this, record, parent);
if (record.children)
stack.push({array: record.children, parent: record, index: 0});
++entry.index;
} else
stack.pop();
}
},
pageDidLoad: function(timestamp)
{
if (isNaN(this._loadEventTime))
this._loadEventTime = timestamp;
},
// Private
_clear: function()
{
this._records = [];
this._typeRecords = {};
this._loadEventTime = NaN;
this._eventMarkers = [];
this.dispatchEventToListeners(WebInspector.TimelineManager.Event.RecordsCleared);
},
_callFramesFromPayload: function(payload)
{
if (!payload)
return null;
function createCallFrame(payload)
{
var url = payload.url;
var nativeCode = false;
if (url === "[native code]") {
nativeCode = true;
url = null;
}
var sourceCode = WebInspector.frameResourceManager.resourceForURL(url);
if (!sourceCode)
sourceCode = WebInspector.debuggerManager.scriptsForURL(url)[0];
// The lineNumber is 1-based, but we expect 0-based.
var lineNumber = payload.lineNumber - 1;
var sourceCodeLocation = sourceCode ? sourceCode.createSourceCodeLocation(lineNumber, payload.columnNumber) : null;
var functionName = payload.functionName !== "global code" ? payload.functionName : null;
return new WebInspector.CallFrame(null, sourceCodeLocation, functionName, null, null, nativeCode);
}
return payload.map(createCallFrame);
},
_addRecord: function(record)
{
this._records.push(record);
if (!this._typeRecords[record.type])
this._typeRecords[record.type] = [];
this._typeRecords[record.type].push(record);
this.dispatchEventToListeners(WebInspector.TimelineManager.Event.RecordAdded, {record: record});
// Only worry about dead time after the load event.
if (!isNaN(this._loadEventTime))
this._resetAutoRecordingDeadTimeTimeout();
},
_startAutoRecording: function(event)
{
if (!event.target.isMainFrame() || (this._recording && !this._autoRecordingMainResource))
return false;
var mainResource = event.target.provisionalMainResource || event.target.mainResource;
if (mainResource === this._autoRecordingMainResource)
return false;
this.stopRecording();
this._autoRecordingMainResource = mainResource;
this.startRecording();
this._addRecord(new WebInspector.ResourceTimelineRecord(mainResource));
if (this._stopRecordingTimeout)
clearTimeout(this._stopRecordingTimeout);
this._stopRecordingTimeout = setTimeout(this.stopRecording.bind(this), WebInspector.TimelineManager.MaximumAutoRecordDuration);
return true;
},
_stopAutoRecordingSoon: function()
{
// Only auto stop when auto recording.
if (!this._recording || !this._autoRecordingMainResource)
return;
if (this._stopRecordingTimeout)
clearTimeout(this._stopRecordingTimeout);
this._stopRecordingTimeout = setTimeout(this.stopRecording.bind(this), WebInspector.TimelineManager.MaximumAutoRecordDurationAfterLoadEvent);
},
_resetAutoRecordingDeadTimeTimeout: function()
{
// Only monitor dead time when auto recording.
if (!this._recording || !this._autoRecordingMainResource)
return;
if (this._deadTimeTimeout)
clearTimeout(this._deadTimeTimeout);
this._deadTimeTimeout = setTimeout(this.stopRecording.bind(this), WebInspector.TimelineManager.DeadTimeRequiredToStopAutoRecordingEarly);
},
_mainResourceDidChange: function(event)
{
// Ignore resource events when there isn't a main frame yet. Those events are triggered by
// loading the cached resources when the inspector opens, and they do not have timing information.
if (!WebInspector.frameResourceManager.mainFrame)
return;
if (this._startAutoRecording(event))
return;
if (!this._recording)
return;
var mainResource = event.target.mainResource;
if (mainResource === this._autoRecordingMainResource)
return;
this._addRecord(new WebInspector.ResourceTimelineRecord(mainResource));
},
_resourceWasAdded: function(event)
{
// Ignore resource events when there isn't a main frame yet. Those events are triggered by
// loading the cached resources when the inspector opens, and they do not have timing information.
if (!WebInspector.frameResourceManager.mainFrame)
return;
if (!this._recording)
return;
this._addRecord(new WebInspector.ResourceTimelineRecord(event.data.resource));
}
};
WebInspector.TimelineManager.prototype.__proto__ = WebInspector.Object.prototype;
|