9fc7da20b8da97f597e087f84132dab481259937
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Views / ScriptTimelineView.js
1 /*
2  * Copyright (C) 2014, 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.ScriptTimelineView = class ScriptTimelineView extends WebInspector.TimelineView
27 {
28     constructor(timeline, extraArguments)
29     {
30         super(timeline, extraArguments);
31
32         console.assert(timeline.type === WebInspector.TimelineRecord.Type.Script);
33
34         this.navigationSidebarTreeOutline.element.classList.add("script");
35
36         var columns = {location: {}, callCount: {}, startTime: {}, totalTime: {}, selfTime: {}, averageTime: {}};
37
38         columns.location.title = WebInspector.UIString("Location");
39         columns.location.width = "15%";
40
41         columns.callCount.title = WebInspector.UIString("Calls");
42         columns.callCount.width = "5%";
43         columns.callCount.aligned = "right";
44
45         columns.startTime.title = WebInspector.UIString("Start Time");
46         columns.startTime.width = "10%";
47         columns.startTime.aligned = "right";
48
49         columns.totalTime.title = WebInspector.UIString("Total Time");
50         columns.totalTime.width = "10%";
51         columns.totalTime.aligned = "right";
52
53         columns.selfTime.title = WebInspector.UIString("Self Time");
54         columns.selfTime.width = "10%";
55         columns.selfTime.aligned = "right";
56
57         columns.averageTime.title = WebInspector.UIString("Average Time");
58         columns.averageTime.width = "10%";
59         columns.averageTime.aligned = "right";
60
61         for (var column in columns)
62             columns[column].sortable = true;
63
64         this._dataGrid = new WebInspector.ScriptTimelineDataGrid(this.navigationSidebarTreeOutline, columns, this);
65         this._dataGrid.addEventListener(WebInspector.TimelineDataGrid.Event.FiltersDidChange, this._dataGridFiltersDidChange, this);
66         this._dataGrid.addEventListener(WebInspector.DataGrid.Event.SelectedNodeChanged, this._dataGridNodeSelected, this);
67         this._dataGrid.sortColumnIdentifier = "startTime";
68         this._dataGrid.sortOrder = WebInspector.DataGrid.SortOrder.Ascending;
69
70         this.element.classList.add("script");
71         this.element.appendChild(this._dataGrid.element);
72
73         timeline.addEventListener(WebInspector.Timeline.Event.RecordAdded, this._scriptTimelineRecordAdded, this);
74
75         this._pendingRecords = [];
76     }
77
78     // Public
79
80     get navigationSidebarTreeOutlineLabel()
81     {
82         return WebInspector.UIString("Records");
83     }
84
85     shown()
86     {
87         super.shown();
88
89         this._dataGrid.shown();
90     }
91
92     hidden()
93     {
94         this._dataGrid.hidden();
95
96         super.hidden();
97     }
98
99     closed()
100     {
101         console.assert(this.representedObject instanceof WebInspector.Timeline);
102         this.representedObject.removeEventListener(null, null, this);
103
104         this._dataGrid.closed();
105     }
106
107     updateLayout()
108     {
109         super.updateLayout();
110
111         this._dataGrid.updateLayout();
112
113         if (this.startTime !== this._oldStartTime || this.endTime !== this._oldEndTime) {
114             var dataGridNode = this._dataGrid.children[0];
115             while (dataGridNode) {
116                 dataGridNode.updateRangeTimes(this.startTime, this.endTime);
117                 if (dataGridNode.revealed)
118                     dataGridNode.refreshIfNeeded();
119                 dataGridNode = dataGridNode.traverseNextNode(false, null, true);
120             }
121
122             this._oldStartTime = this.startTime;
123             this._oldEndTime = this.endTime;
124         }
125
126         this._processPendingRecords();
127     }
128
129     get selectionPathComponents()
130     {
131         var dataGridNode = this._dataGrid.selectedNode;
132         if (!dataGridNode)
133             return null;
134
135         var pathComponents = [];
136
137         while (dataGridNode && !dataGridNode.root) {
138             var treeElement = this._dataGrid.treeElementForDataGridNode(dataGridNode);
139             console.assert(treeElement);
140             if (!treeElement)
141                 break;
142
143             if (treeElement.hidden)
144                 return null;
145
146             var pathComponent = new WebInspector.GeneralTreeElementPathComponent(treeElement);
147             pathComponent.addEventListener(WebInspector.HierarchicalPathComponent.Event.SiblingWasSelected, this.treeElementPathComponentSelected, this);
148             pathComponents.unshift(pathComponent);
149             dataGridNode = dataGridNode.parent;
150         }
151
152         return pathComponents;
153     }
154
155     reset()
156     {
157         super.reset();
158
159         this._dataGrid.reset();
160
161         this._pendingRecords = [];
162     }
163
164     // Protected
165
166     canShowContentViewForTreeElement(treeElement)
167     {
168         if (treeElement instanceof WebInspector.ProfileNodeTreeElement)
169             return !!treeElement.profileNode.sourceCodeLocation;
170         return super.canShowContentViewForTreeElement(treeElement);
171     }
172
173     showContentViewForTreeElement(treeElement)
174     {
175         if (treeElement instanceof WebInspector.ProfileNodeTreeElement) {
176             if (treeElement.profileNode.sourceCodeLocation)
177                 WebInspector.showOriginalOrFormattedSourceCodeLocation(treeElement.profileNode.sourceCodeLocation);
178             return;
179         }
180
181         super.showContentViewForTreeElement(treeElement);
182     }
183
184     treeElementPathComponentSelected(event)
185     {
186         var dataGridNode = this._dataGrid.dataGridNodeForTreeElement(event.data.pathComponent.generalTreeElement);
187         if (!dataGridNode)
188             return;
189         dataGridNode.revealAndSelect();
190     }
191
192     treeElementSelected(treeElement, selectedByUser)
193     {
194         if (this._dataGrid.shouldIgnoreSelectionEvent())
195             return;
196
197         super.treeElementSelected(treeElement, selectedByUser);
198     }
199
200     dataGridNodeForTreeElement(treeElement)
201     {
202         if (treeElement instanceof WebInspector.ProfileNodeTreeElement)
203             return new WebInspector.ProfileNodeDataGridNode(treeElement.profileNode, this.zeroTime, this.startTime, this.endTime);
204         return null;
205     }
206
207     populateProfileNodeTreeElement(treeElement)
208     {
209         var zeroTime = this.zeroTime;
210         var startTime = this.startTime;
211         var endTime = this.endTime;
212
213         for (var childProfileNode of treeElement.profileNode.childNodes) {
214             var profileNodeTreeElement = new WebInspector.ProfileNodeTreeElement(childProfileNode, this);
215             var profileNodeDataGridNode = new WebInspector.ProfileNodeDataGridNode(childProfileNode, zeroTime, startTime, endTime);
216             this._dataGrid.addRowInSortOrder(profileNodeTreeElement, profileNodeDataGridNode, treeElement);
217         }
218     }
219
220     // Private
221
222     _processPendingRecords()
223     {
224         if (!this._pendingRecords.length)
225             return;
226
227         var zeroTime = this.zeroTime;
228         var startTime = this.startTime;
229         var endTime = this.endTime;
230
231         for (var scriptTimelineRecord of this._pendingRecords) {
232             var rootNodes = [];
233             if (scriptTimelineRecord.profile) {
234                 // FIXME: Support using the bottom-up tree once it is implemented.
235                 rootNodes = scriptTimelineRecord.profile.topDownRootNodes;
236             }
237
238             var treeElement = new WebInspector.TimelineRecordTreeElement(scriptTimelineRecord, WebInspector.SourceCodeLocation.NameStyle.Short, true);
239             var dataGridNode = new WebInspector.ScriptTimelineDataGridNode(scriptTimelineRecord, zeroTime);
240
241             this._dataGrid.addRowInSortOrder(treeElement, dataGridNode);
242
243             for (var profileNode of rootNodes) {
244                 var profileNodeTreeElement = new WebInspector.ProfileNodeTreeElement(profileNode, this);
245                 var profileNodeDataGridNode = new WebInspector.ProfileNodeDataGridNode(profileNode, zeroTime, startTime, endTime);
246                 this._dataGrid.addRowInSortOrder(profileNodeTreeElement, profileNodeDataGridNode, treeElement);
247             }
248         }
249
250         this._pendingRecords = [];
251     }
252
253     _scriptTimelineRecordAdded(event)
254     {
255         var scriptTimelineRecord = event.data.record;
256         console.assert(scriptTimelineRecord instanceof WebInspector.ScriptTimelineRecord);
257
258         this._pendingRecords.push(scriptTimelineRecord);
259
260         this.needsLayout();
261     }
262
263     _dataGridFiltersDidChange(event)
264     {
265         this.timelineSidebarPanel.updateFilter();
266     }
267
268     _dataGridNodeSelected(event)
269     {
270         this.dispatchEventToListeners(WebInspector.ContentView.Event.SelectionPathComponentsDidChange);
271     }
272 };