2d4cd55eade5b19fcae6b3db441fa286c321aa0e
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Controllers / TimelineManager.js
1 /*
2  * Copyright (C) 2013 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 WebInspector.TimelineManager = class TimelineManager extends WebInspector.Object
27 {
28     constructor()
29     {
30         super();
31
32         WebInspector.Frame.addEventListener(WebInspector.Frame.Event.ProvisionalLoadStarted, this._startAutoCapturing, this);
33         WebInspector.Frame.addEventListener(WebInspector.Frame.Event.MainResourceDidChange, this._mainResourceDidChange, this);
34         WebInspector.Frame.addEventListener(WebInspector.Frame.Event.ResourceWasAdded, this._resourceWasAdded, this);
35
36         WebInspector.heapManager.addEventListener(WebInspector.HeapManager.Event.GarbageCollected, this._garbageCollected, this);
37
38         this._persistentNetworkTimeline = new WebInspector.NetworkTimeline;
39
40         this._isCapturing = false;
41         this._isCapturingPageReload = false;
42         this._autoCapturingMainResource = null;
43         this._boundStopCapturing = this.stopCapturing.bind(this);
44
45         this.reset();
46     }
47
48     // Public
49
50     reset()
51     {
52         if (this._isCapturing)
53             this.stopCapturing();
54
55         this._recordings = [];
56         this._activeRecording = null;
57         this._nextRecordingIdentifier = 1;
58
59         this._loadNewRecording();
60     }
61
62     // The current recording that new timeline records will be appended to, if any.
63     get activeRecording()
64     {
65         console.assert(this._activeRecording || !this._isCapturing);
66         return this._activeRecording;
67     }
68
69     get persistentNetworkTimeline()
70     {
71         return this._persistentNetworkTimeline;
72     }
73
74     get recordings()
75     {
76         return this._recordings.slice();
77     }
78
79     get autoCaptureOnPageLoad()
80     {
81         return this._autoCaptureOnPageLoad;
82     }
83
84     set autoCaptureOnPageLoad(autoCapture)
85     {
86         this._autoCaptureOnPageLoad = autoCapture;
87     }
88
89     isCapturing()
90     {
91         return this._isCapturing;
92     }
93
94     isCapturingPageReload()
95     {
96         return this._isCapturingPageReload;
97     }
98
99     startCapturing(shouldCreateRecording)
100     {
101         console.assert(!this._isCapturing, "TimelineManager is already capturing.");
102
103         if (!this._activeRecording || shouldCreateRecording)
104             this._loadNewRecording();
105
106         var result = TimelineAgent.start();
107
108         // COMPATIBILITY (iOS 7): recordingStarted event did not exist yet. Start explicitly.
109         if (!TimelineAgent.hasEvent("recordingStarted")) {
110             result.then(function() {
111                 WebInspector.timelineManager.capturingStarted();
112             });
113         }
114     }
115
116     stopCapturing()
117     {
118         console.assert(this._isCapturing, "TimelineManager is not capturing.");
119
120         TimelineAgent.stop();
121
122         // NOTE: Always stop immediately instead of waiting for a Timeline.recordingStopped event.
123         // This way the UI feels as responsive to a stop as possible.
124         this.capturingStopped();
125     }
126
127     unloadRecording()
128     {
129         if (!this._activeRecording)
130             return;
131
132         if (this._isCapturing)
133             this.stopCapturing();
134
135         this._activeRecording.unloaded();
136         this._activeRecording = null;
137     }
138
139     computeElapsedTime(timestamp)
140     {
141         if (!this._activeRecording)
142             return 0;
143
144         return this._activeRecording.computeElapsedTime(timestamp);
145     }
146
147     // Protected
148
149     capturingStarted(startTime)
150     {
151         if (this._isCapturing)
152             return;
153
154         this._isCapturing = true;
155
156         if (startTime)
157             this.activeRecording.initializeTimeBoundsIfNecessary(startTime);
158
159         this.dispatchEventToListeners(WebInspector.TimelineManager.Event.CapturingStarted, {startTime});
160     }
161
162     capturingStopped(endTime)
163     {
164         if (!this._isCapturing)
165             return;
166
167         if (this._stopCapturingTimeout) {
168             clearTimeout(this._stopCapturingTimeout);
169             this._stopCapturingTimeout = undefined;
170         }
171
172         if (this._deadTimeTimeout) {
173             clearTimeout(this._deadTimeTimeout);
174             this._deadTimeTimeout = undefined;
175         }
176
177         this._isCapturing = false;
178         this._isCapturingPageReload = false;
179         this._autoCapturingMainResource = null;
180
181         this.dispatchEventToListeners(WebInspector.TimelineManager.Event.CapturingStopped, {endTime});
182     }
183
184     eventRecorded(recordPayload)
185     {
186         // Called from WebInspector.TimelineObserver.
187
188         if (!this._isCapturing)
189             return;
190
191         var records = [];
192
193         // Iterate over the records tree using a stack. Doing this recursively has
194         // been known to cause a call stack overflow. https://webkit.org/b/79106
195         var stack = [{array: [recordPayload], parent: null, parentRecord: null, index: 0}];
196         while (stack.length) {
197             var entry = stack.lastValue;
198             var recordPayloads = entry.array;
199
200             if (entry.index < recordPayloads.length) {
201                 var recordPayload = recordPayloads[entry.index];
202                 var record = this._processEvent(recordPayload, entry.parent);
203                 if (record) {
204                     record.parent = entry.parentRecord;
205                     records.push(record);
206                     if (entry.parentRecord)
207                         entry.parentRecord.children.push(record);
208                 }
209
210                 if (recordPayload.children && recordPayload.children.length)
211                     stack.push({array: recordPayload.children, parent: recordPayload, parentRecord: record || entry.parentRecord, index: 0});
212                 ++entry.index;
213             } else
214                 stack.pop();
215         }
216
217         for (var record of records) {
218             if (record.type === WebInspector.TimelineRecord.Type.RenderingFrame) {
219                 if (!record.children.length)
220                     continue;
221                 record.setupFrameIndex();
222             }
223
224             this._addRecord(record);
225         }
226     }
227
228     // Protected
229
230     pageDidLoad(timestamp)
231     {
232         // Called from WebInspector.PageObserver.
233
234         if (isNaN(WebInspector.frameResourceManager.mainFrame.loadEventTimestamp))
235             WebInspector.frameResourceManager.mainFrame.markLoadEvent(this.activeRecording.computeElapsedTime(timestamp));
236     }
237
238     // Private
239
240     _processRecord(recordPayload, parentRecordPayload)
241     {
242         var startTime = this.activeRecording.computeElapsedTime(recordPayload.startTime);
243         var endTime = this.activeRecording.computeElapsedTime(recordPayload.endTime);
244         var callFrames = this._callFramesFromPayload(recordPayload.stackTrace);
245
246         var significantCallFrame = null;
247         if (callFrames) {
248             for (var i = 0; i < callFrames.length; ++i) {
249                 if (callFrames[i].nativeCode)
250                     continue;
251                 significantCallFrame = callFrames[i];
252                 break;
253             }
254         }
255
256         var sourceCodeLocation = significantCallFrame && significantCallFrame.sourceCodeLocation;
257
258         switch (recordPayload.type) {
259         case TimelineAgent.EventType.ScheduleStyleRecalculation:
260             console.assert(isNaN(endTime));
261
262             // Pass the startTime as the endTime since this record type has no duration.
263             return new WebInspector.LayoutTimelineRecord(WebInspector.LayoutTimelineRecord.EventType.InvalidateStyles, startTime, startTime, callFrames, sourceCodeLocation);
264
265         case TimelineAgent.EventType.RecalculateStyles:
266             return new WebInspector.LayoutTimelineRecord(WebInspector.LayoutTimelineRecord.EventType.RecalculateStyles, startTime, endTime, callFrames, sourceCodeLocation);
267
268         case TimelineAgent.EventType.InvalidateLayout:
269             console.assert(isNaN(endTime));
270
271             // Pass the startTime as the endTime since this record type has no duration.
272             return new WebInspector.LayoutTimelineRecord(WebInspector.LayoutTimelineRecord.EventType.InvalidateLayout, startTime, startTime, callFrames, sourceCodeLocation);
273
274         case TimelineAgent.EventType.Layout:
275             var layoutRecordType = sourceCodeLocation ? WebInspector.LayoutTimelineRecord.EventType.ForcedLayout : WebInspector.LayoutTimelineRecord.EventType.Layout;
276             var quad = new WebInspector.Quad(recordPayload.data.root);
277             return new WebInspector.LayoutTimelineRecord(layoutRecordType, startTime, endTime, callFrames, sourceCodeLocation, quad);
278
279         case TimelineAgent.EventType.Paint:
280             var quad = new WebInspector.Quad(recordPayload.data.clip);
281             return new WebInspector.LayoutTimelineRecord(WebInspector.LayoutTimelineRecord.EventType.Paint, startTime, endTime, callFrames, sourceCodeLocation, quad);
282
283         case TimelineAgent.EventType.Composite:
284             return new WebInspector.LayoutTimelineRecord(WebInspector.LayoutTimelineRecord.EventType.Composite, startTime, endTime, callFrames, sourceCodeLocation);
285
286         case TimelineAgent.EventType.RenderingFrame:
287             if (!recordPayload.children || !recordPayload.children.length)
288                 return null;
289
290             return new WebInspector.RenderingFrameTimelineRecord(startTime, endTime);
291
292         case TimelineAgent.EventType.EvaluateScript:
293             if (!sourceCodeLocation) {
294                 var mainFrame = WebInspector.frameResourceManager.mainFrame;
295                 var scriptResource = mainFrame.url === recordPayload.data.url ? mainFrame.mainResource : mainFrame.resourceForURL(recordPayload.data.url, true);
296                 if (scriptResource) {
297                     // The lineNumber is 1-based, but we expect 0-based.
298                     var lineNumber = recordPayload.data.lineNumber - 1;
299
300                     // FIXME: No column number is provided.
301                     sourceCodeLocation = scriptResource.createSourceCodeLocation(lineNumber, 0);
302                 }
303             }
304
305             var profileData = recordPayload.data.profile;
306
307             switch (parentRecordPayload && parentRecordPayload.type) {
308             case TimelineAgent.EventType.TimerFire:
309                 return new WebInspector.ScriptTimelineRecord(WebInspector.ScriptTimelineRecord.EventType.TimerFired, startTime, endTime, callFrames, sourceCodeLocation, parentRecordPayload.data.timerId, profileData);
310             default:
311                 return new WebInspector.ScriptTimelineRecord(WebInspector.ScriptTimelineRecord.EventType.ScriptEvaluated, startTime, endTime, callFrames, sourceCodeLocation, null, profileData);
312             }
313
314             break;
315
316         case TimelineAgent.EventType.ConsoleProfile:
317             var profileData = recordPayload.data.profile;
318             console.assert(profileData);
319             return new WebInspector.ScriptTimelineRecord(WebInspector.ScriptTimelineRecord.EventType.ConsoleProfileRecorded, startTime, endTime, callFrames, sourceCodeLocation, recordPayload.data.title, profileData);
320
321         case TimelineAgent.EventType.FunctionCall:
322             // FunctionCall always happens as a child of another record, and since the FunctionCall record
323             // has useful info we just make the timeline record here (combining the data from both records).
324             if (!parentRecordPayload)
325                 break;
326
327             if (!sourceCodeLocation) {
328                 var mainFrame = WebInspector.frameResourceManager.mainFrame;
329                 var scriptResource = mainFrame.url === recordPayload.data.scriptName ? mainFrame.mainResource : mainFrame.resourceForURL(recordPayload.data.scriptName, true);
330                 if (scriptResource) {
331                     // The lineNumber is 1-based, but we expect 0-based.
332                     var lineNumber = recordPayload.data.scriptLine - 1;
333
334                     // FIXME: No column number is provided.
335                     sourceCodeLocation = scriptResource.createSourceCodeLocation(lineNumber, 0);
336                 }
337             }
338
339             var profileData = recordPayload.data.profile;
340
341             switch (parentRecordPayload.type) {
342             case TimelineAgent.EventType.TimerFire:
343                 return new WebInspector.ScriptTimelineRecord(WebInspector.ScriptTimelineRecord.EventType.TimerFired, startTime, endTime, callFrames, sourceCodeLocation, parentRecordPayload.data.timerId, profileData);
344             case TimelineAgent.EventType.EventDispatch:
345                 return new WebInspector.ScriptTimelineRecord(WebInspector.ScriptTimelineRecord.EventType.EventDispatched, startTime, endTime, callFrames, sourceCodeLocation, parentRecordPayload.data.type, profileData);
346             case TimelineAgent.EventType.XHRLoad:
347                 return new WebInspector.ScriptTimelineRecord(WebInspector.ScriptTimelineRecord.EventType.EventDispatched, startTime, endTime, callFrames, sourceCodeLocation, "load", profileData);
348             case TimelineAgent.EventType.XHRReadyStateChange:
349                 return new WebInspector.ScriptTimelineRecord(WebInspector.ScriptTimelineRecord.EventType.EventDispatched, startTime, endTime, callFrames, sourceCodeLocation, "readystatechange", profileData);
350             case TimelineAgent.EventType.FireAnimationFrame:
351                 return new WebInspector.ScriptTimelineRecord(WebInspector.ScriptTimelineRecord.EventType.AnimationFrameFired, startTime, endTime, callFrames, sourceCodeLocation, parentRecordPayload.data.id, profileData);
352             }
353
354             break;
355
356         case TimelineAgent.EventType.ProbeSample:
357             // Pass the startTime as the endTime since this record type has no duration.
358             sourceCodeLocation = WebInspector.probeManager.probeForIdentifier(recordPayload.data.probeId).breakpoint.sourceCodeLocation;
359             return new WebInspector.ScriptTimelineRecord(WebInspector.ScriptTimelineRecord.EventType.ProbeSampleRecorded, startTime, startTime, callFrames, sourceCodeLocation, recordPayload.data.probeId);
360
361         case TimelineAgent.EventType.TimerInstall:
362             console.assert(isNaN(endTime));
363
364             // Pass the startTime as the endTime since this record type has no duration.
365             return new WebInspector.ScriptTimelineRecord(WebInspector.ScriptTimelineRecord.EventType.TimerInstalled, startTime, startTime, callFrames, sourceCodeLocation, recordPayload.data.timerId);
366
367         case TimelineAgent.EventType.TimerRemove:
368             console.assert(isNaN(endTime));
369
370             // Pass the startTime as the endTime since this record type has no duration.
371             return new WebInspector.ScriptTimelineRecord(WebInspector.ScriptTimelineRecord.EventType.TimerRemoved, startTime, startTime, callFrames, sourceCodeLocation, recordPayload.data.timerId);
372
373         case TimelineAgent.EventType.RequestAnimationFrame:
374             console.assert(isNaN(endTime));
375
376             // Pass the startTime as the endTime since this record type has no duration.
377             return new WebInspector.ScriptTimelineRecord(WebInspector.ScriptTimelineRecord.EventType.AnimationFrameRequested, startTime, startTime, callFrames, sourceCodeLocation, recordPayload.data.timerId);
378
379         case TimelineAgent.EventType.CancelAnimationFrame:
380             console.assert(isNaN(endTime));
381
382             // Pass the startTime as the endTime since this record type has no duration.
383             return new WebInspector.ScriptTimelineRecord(WebInspector.ScriptTimelineRecord.EventType.AnimationFrameCanceled, startTime, startTime, callFrames, sourceCodeLocation, recordPayload.data.timerId);
384         }
385
386         return null;
387     }
388
389     _processEvent(recordPayload, parentRecordPayload)
390     {
391         var startTime = this.activeRecording.computeElapsedTime(recordPayload.startTime);
392         var endTime = this.activeRecording.computeElapsedTime(recordPayload.endTime);
393
394         switch (recordPayload.type) {
395         case TimelineAgent.EventType.MarkLoad:
396             console.assert(isNaN(endTime));
397
398             var frame = WebInspector.frameResourceManager.frameForIdentifier(recordPayload.frameId);
399             console.assert(frame);
400             if (!frame)
401                 break;
402
403             frame.markLoadEvent(startTime);
404
405             if (!frame.isMainFrame())
406                 break;
407
408             var eventMarker = new WebInspector.TimelineMarker(startTime, WebInspector.TimelineMarker.Type.LoadEvent);
409             this._activeRecording.addEventMarker(eventMarker);
410
411             this._stopAutoRecordingSoon();
412             break;
413
414         case TimelineAgent.EventType.MarkDOMContent:
415             console.assert(isNaN(endTime));
416
417             var frame = WebInspector.frameResourceManager.frameForIdentifier(recordPayload.frameId);
418             console.assert(frame);
419             if (!frame)
420                 break;
421
422             frame.markDOMContentReadyEvent(startTime);
423
424             if (!frame.isMainFrame())
425                 break;
426
427             var eventMarker = new WebInspector.TimelineMarker(startTime, WebInspector.TimelineMarker.Type.DOMContentEvent);
428             this._activeRecording.addEventMarker(eventMarker);
429             break;
430
431         case TimelineAgent.EventType.TimeStamp:
432             var eventMarker = new WebInspector.TimelineMarker(startTime, WebInspector.TimelineMarker.Type.TimeStamp);
433             this._activeRecording.addEventMarker(eventMarker);
434             break;
435
436         default:
437             return this._processRecord(recordPayload, parentRecordPayload);
438         }
439
440         return null;
441     }
442
443     _loadNewRecording()
444     {
445         if (this._activeRecording && this._activeRecording.isEmpty())
446             return;
447
448         var identifier = this._nextRecordingIdentifier++;
449         var newRecording = new WebInspector.TimelineRecording(identifier, WebInspector.UIString("Timeline Recording %d").format(identifier));
450         newRecording.addTimeline(WebInspector.Timeline.create(WebInspector.TimelineRecord.Type.Network, newRecording));
451         newRecording.addTimeline(WebInspector.Timeline.create(WebInspector.TimelineRecord.Type.Layout, newRecording));
452         newRecording.addTimeline(WebInspector.Timeline.create(WebInspector.TimelineRecord.Type.Script, newRecording));
453
454         // COMPATIBILITY (iOS 8): TimelineAgent.EventType.RenderingFrame did not exist.
455         if (window.TimelineAgent && TimelineAgent.EventType.RenderingFrame)
456             newRecording.addTimeline(WebInspector.Timeline.create(WebInspector.TimelineRecord.Type.RenderingFrame, newRecording));
457
458         this._recordings.push(newRecording);
459         this.dispatchEventToListeners(WebInspector.TimelineManager.Event.RecordingCreated, {recording: newRecording});
460
461         console.assert(newRecording.isWritable());
462
463         if (this._isCapturing)
464             this.stopCapturing();
465
466         var oldRecording = this._activeRecording;
467         if (oldRecording)
468             oldRecording.unloaded();
469
470         this._activeRecording = newRecording;
471
472         // COMPATIBILITY (iOS 8): When using Legacy timestamps, a navigation will have computed
473         // the main resource's will send request timestamp in terms of the last page's base timestamp.
474         // Now that we have navigated, we should reset the legacy base timestamp and the
475         // will send request timestamp for the new main resource. This way, all new timeline
476         // records will be computed relative to the new navigation.
477         if (this._autoCapturingMainResource && WebInspector.TimelineRecording.isLegacy) {
478             console.assert(this._autoCapturingMainResource.originalRequestWillBeSentTimestamp);
479             this._activeRecording.setLegacyBaseTimestamp(this._autoCapturingMainResource.originalRequestWillBeSentTimestamp);
480             this._autoCapturingMainResource._requestSentTimestamp = 0;
481         }
482
483         this.dispatchEventToListeners(WebInspector.TimelineManager.Event.RecordingLoaded, {oldRecording});
484     }
485
486     _callFramesFromPayload(payload)
487     {
488         if (!payload)
489             return null;
490
491         return payload.map(WebInspector.CallFrame.fromPayload);
492     }
493
494     _addRecord(record)
495     {
496         this._activeRecording.addRecord(record);
497
498         // Only worry about dead time after the load event.
499         if (!isNaN(WebInspector.frameResourceManager.mainFrame.loadEventTimestamp))
500             this._resetAutoRecordingDeadTimeTimeout();
501     }
502
503     _startAutoCapturing(event)
504     {
505         if (!this._autoCaptureOnPageLoad)
506             return false;
507
508         if (!event.target.isMainFrame() || (this._isCapturing && !this._autoCapturingMainResource))
509             return false;
510
511         var mainResource = event.target.provisionalMainResource || event.target.mainResource;
512         if (mainResource === this._autoCapturingMainResource)
513             return false;
514
515         var oldMainResource = event.target.mainResource || null;
516         this._isCapturingPageReload = oldMainResource !== null && oldMainResource.url === mainResource.url;
517
518         if (this._isCapturing)
519             this.stopCapturing();
520
521         this._autoCapturingMainResource = mainResource;
522
523         this._loadNewRecording();
524
525         this.startCapturing();
526
527         this._addRecord(new WebInspector.ResourceTimelineRecord(mainResource));
528
529         if (this._stopCapturingTimeout)
530             clearTimeout(this._stopCapturingTimeout);
531         this._stopCapturingTimeout = setTimeout(this._boundStopCapturing, WebInspector.TimelineManager.MaximumAutoRecordDuration);
532
533         return true;
534     }
535
536     _stopAutoRecordingSoon()
537     {
538         // Only auto stop when auto capturing.
539         if (!this._isCapturing || !this._autoCapturingMainResource)
540             return;
541
542         if (this._stopCapturingTimeout)
543             clearTimeout(this._stopCapturingTimeout);
544         this._stopCapturingTimeout = setTimeout(this._boundStopCapturing, WebInspector.TimelineManager.MaximumAutoRecordDurationAfterLoadEvent);
545     }
546
547     _resetAutoRecordingDeadTimeTimeout()
548     {
549         // Only monitor dead time when auto capturing.
550         if (!this._isCapturing || !this._autoCapturingMainResource)
551             return;
552
553         if (this._deadTimeTimeout)
554             clearTimeout(this._deadTimeTimeout);
555         this._deadTimeTimeout = setTimeout(this._boundStopCapturing, WebInspector.TimelineManager.DeadTimeRequiredToStopAutoRecordingEarly);
556     }
557
558     _mainResourceDidChange(event)
559     {
560         if (event.target.isMainFrame())
561             this._persistentNetworkTimeline.reset();
562
563         var mainResource = event.target.mainResource;
564         var record = new WebInspector.ResourceTimelineRecord(mainResource);
565         if (!isNaN(record.startTime))
566             this._persistentNetworkTimeline.addRecord(record);
567
568         // Ignore resource events when there isn't a main frame yet. Those events are triggered by
569         // loading the cached resources when the inspector opens, and they do not have timing information.
570         if (!WebInspector.frameResourceManager.mainFrame)
571             return;
572
573         if (this._startAutoCapturing(event))
574             return;
575
576         if (!this._isCapturing)
577             return;
578
579         if (mainResource === this._autoCapturingMainResource)
580             return;
581
582         this._addRecord(record);
583     }
584
585     _resourceWasAdded(event)
586     {
587         var record = new WebInspector.ResourceTimelineRecord(event.data.resource);
588         if (!isNaN(record.startTime))
589             this._persistentNetworkTimeline.addRecord(record);
590
591         // Ignore resource events when there isn't a main frame yet. Those events are triggered by
592         // loading the cached resources when the inspector opens, and they do not have timing information.
593         if (!WebInspector.frameResourceManager.mainFrame)
594             return;
595
596         if (!this._isCapturing)
597             return;
598
599         this._addRecord(record);
600     }
601
602     _garbageCollected(event)
603     {
604         if (!this._isCapturing)
605             return;
606
607         let collection = event.data.collection;
608         this._addRecord(new WebInspector.ScriptTimelineRecord(WebInspector.ScriptTimelineRecord.EventType.GarbageCollected, collection.startTime, collection.endTime, null, null, collection));
609     }
610 };
611
612 WebInspector.TimelineManager.Event = {
613     RecordingCreated: "timeline-manager-recording-created",
614     RecordingLoaded: "timeline-manager-recording-loaded",
615     CapturingStarted: "timeline-manager-capturing-started",
616     CapturingStopped: "timeline-manager-capturing-stopped"
617 };
618
619 WebInspector.TimelineManager.MaximumAutoRecordDuration = 90000; // 90 seconds
620 WebInspector.TimelineManager.MaximumAutoRecordDurationAfterLoadEvent = 10000; // 10 seconds
621 WebInspector.TimelineManager.DeadTimeRequiredToStopAutoRecordingEarly = 2000; // 2 seconds