Web Inspector: Hide child rows for filtered tasks in the Rendering Frames data grid
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Models / RenderingFrameTimelineRecord.js
1 /*
2  * Copyright (C) 2015 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.RenderingFrameTimelineRecord = class RenderingFrameTimelineRecord extends WebInspector.TimelineRecord
27 {
28     constructor(startTime, endTime)
29     {
30         super(WebInspector.TimelineRecord.Type.RenderingFrame, startTime, endTime);
31
32         this._durationByTaskType = new Map;
33         this._frameIndex = WebInspector.RenderingFrameTimelineRecord._nextFrameIndex++;
34     }
35
36     // Static
37
38     static resetFrameIndex()
39     {
40         WebInspector.RenderingFrameTimelineRecord._nextFrameIndex = 0;
41     }
42
43     static displayNameForTaskType(taskType)
44     {
45         switch(taskType) {
46         case WebInspector.RenderingFrameTimelineRecord.TaskType.Script:
47             return WebInspector.UIString("Script");
48         case WebInspector.RenderingFrameTimelineRecord.TaskType.Layout:
49             return WebInspector.UIString("Layout");
50         case WebInspector.RenderingFrameTimelineRecord.TaskType.Paint:
51             return WebInspector.UIString("Paint");
52         case WebInspector.RenderingFrameTimelineRecord.TaskType.Other:
53             return WebInspector.UIString("Other");
54         }
55     }
56
57     static taskTypeForTimelineRecord(record)
58     {
59         switch(record.type) {
60         case WebInspector.TimelineRecord.Type.Script:
61             return WebInspector.RenderingFrameTimelineRecord.TaskType.Script;
62         case WebInspector.TimelineRecord.Type.Layout:
63             if (record.eventType  === WebInspector.LayoutTimelineRecord.EventType.Paint || record.eventType === WebInspector.LayoutTimelineRecord.EventType.Composite)
64                 return WebInspector.RenderingFrameTimelineRecord.TaskType.Paint;
65             return WebInspector.RenderingFrameTimelineRecord.TaskType.Layout;
66         default:
67             console.error("Unsupported timeline record type: " + record.type);
68             return null;
69         }
70     }
71
72     // Public
73
74     get frameIndex()
75     {
76         return this._frameIndex;
77     }
78
79     get frameNumber()
80     {
81         return this._frameIndex + 1;
82     }
83
84     durationForTask(taskType)
85     {
86         if (this._durationByTaskType.has(taskType))
87             return this._durationByTaskType.get(taskType);
88
89         var duration;
90         if (taskType === WebInspector.RenderingFrameTimelineRecord.TaskType.Other)
91             duration = this._calculateDurationRemainder();
92         else {
93             duration = this.children.reduce(function(previousValue, currentValue) {
94                 if (taskType !== WebInspector.RenderingFrameTimelineRecord.taskTypeForTimelineRecord(currentValue))
95                     return previousValue;
96
97                 var currentDuration = currentValue.duration;
98                 if (currentValue.usesActiveStartTime)
99                     currentDuration -= currentValue.inactiveDuration;
100                 return previousValue + currentDuration;
101             }, 0);
102
103             if (taskType === WebInspector.RenderingFrameTimelineRecord.TaskType.Script) {
104                 // Layout events synchronously triggered from JavaScript must be subtracted from the total
105                 // script time, to prevent the time from being counted twice.
106                 duration -= this.children.reduce(function(previousValue, currentValue) {
107                     if (currentValue.type === WebInspector.TimelineRecord.Type.Layout && (currentValue.sourceCodeLocation || currentValue.callFrames))
108                         return previousValue + currentValue.duration;
109                     return previousValue;
110                 }, 0);
111             }
112         }
113
114         this._durationByTaskType.set(taskType, duration);
115         return duration;
116     }
117
118     // Private
119
120     _calculateDurationRemainder()
121     {
122         return Object.keys(WebInspector.RenderingFrameTimelineRecord.TaskType).reduce(function(previousValue, key) {
123             var taskType = WebInspector.RenderingFrameTimelineRecord.TaskType[key];
124             if (taskType === WebInspector.RenderingFrameTimelineRecord.TaskType.Other)
125                 return previousValue;
126             return previousValue - this.durationForTask(taskType);
127         }.bind(this), this.duration);
128     }
129 };
130
131 WebInspector.RenderingFrameTimelineRecord.TaskType = {
132     Script: "rendering-frame-timeline-record-script",
133     Layout: "rendering-frame-timeline-record-layout",
134     Paint: "rendering-frame-timeline-record-paint",
135     Other: "rendering-frame-timeline-record-other"
136 };
137
138 WebInspector.RenderingFrameTimelineRecord.TypeIdentifier = "rendering-frame-timeline-record";
139
140 WebInspector.RenderingFrameTimelineRecord._nextFrameIndex = 0;