RegExpCache::finalize should not delete code
[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     // Public
58
59     get frameIndex()
60     {
61         return this._frameIndex;
62     }
63
64     get frameNumber()
65     {
66         return this._frameIndex + 1;
67     }
68
69     durationForTask(taskType)
70     {
71         if (this._durationByTaskType.has(taskType))
72             return this._durationByTaskType.get(taskType);
73
74         function validRecordForTaskType(record)
75         {
76             switch(taskType) {
77             case WebInspector.RenderingFrameTimelineRecord.TaskType.Script:
78                 return record.type === WebInspector.TimelineRecord.Type.Script;
79             case WebInspector.RenderingFrameTimelineRecord.TaskType.Layout:
80                 return record.type === WebInspector.TimelineRecord.Type.Layout && record.eventType !== WebInspector.LayoutTimelineRecord.EventType.Paint && record.eventType !== WebInspector.LayoutTimelineRecord.EventType.Composite;
81             case WebInspector.RenderingFrameTimelineRecord.TaskType.Paint:
82                 return record.eventType === WebInspector.LayoutTimelineRecord.EventType.Paint || record.eventType === WebInspector.LayoutTimelineRecord.EventType.Composite;
83             default:
84                 console.error("Unsupported task type: " + taskType);
85                 return false;
86             }
87         }
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 (!validRecordForTaskType(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;