d4b3a22644478e0f4b6eb57afd8c33dde20f599c
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Views / NetworkTimelineView.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.NetworkTimelineView = class NetworkTimelineView extends WebInspector.TimelineView
27 {
28     constructor(timeline, extraArguments)
29     {
30         super(timeline, extraArguments);
31
32         console.assert(timeline.type === WebInspector.TimelineRecord.Type.Network);
33
34         let columns = {name: {}, domain: {}, type: {}, method: {}, scheme: {}, statusCode: {}, cached: {}, size: {}, transferSize: {}, requestSent: {}, latency: {}, duration: {}};
35
36         columns.name.title = WebInspector.UIString("Name");
37         columns.name.icon = true;
38         columns.name.width = "10%";
39
40         columns.domain.title = WebInspector.UIString("Domain");
41         columns.domain.width = "10%";
42
43         columns.type.title = WebInspector.UIString("Type");
44         columns.type.width = "8%";
45
46         var typeToLabelMap = new Map;
47         for (var key in WebInspector.Resource.Type) {
48             var value = WebInspector.Resource.Type[key];
49             typeToLabelMap.set(value, WebInspector.Resource.displayNameForType(value, true));
50         }
51
52         columns.type.scopeBar = WebInspector.TimelineDataGrid.createColumnScopeBar("network", typeToLabelMap);
53         this._scopeBar = columns.type.scopeBar;
54
55         columns.method.title = WebInspector.UIString("Method");
56         columns.method.width = "6%";
57
58         columns.scheme.title = WebInspector.UIString("Scheme");
59         columns.scheme.width = "6%";
60
61         columns.statusCode.title = WebInspector.UIString("Status");
62         columns.statusCode.width = "6%";
63
64         columns.cached.title = WebInspector.UIString("Cached");
65         columns.cached.width = "6%";
66
67         columns.size.title = WebInspector.UIString("Size");
68         columns.size.width = "8%";
69         columns.size.aligned = "right";
70
71         columns.transferSize.title = WebInspector.UIString("Transferred");
72         columns.transferSize.width = "8%";
73         columns.transferSize.aligned = "right";
74
75         columns.requestSent.title = WebInspector.UIString("Start Time");
76         columns.requestSent.width = "9%";
77         columns.requestSent.aligned = "right";
78
79         columns.latency.title = WebInspector.UIString("Latency");
80         columns.latency.width = "9%";
81         columns.latency.aligned = "right";
82
83         columns.duration.title = WebInspector.UIString("Duration");
84         columns.duration.width = "9%";
85         columns.duration.aligned = "right";
86
87         for (var column in columns)
88             columns[column].sortable = true;
89
90         this._dataGrid = new WebInspector.TimelineDataGrid(null, columns);
91         this._dataGrid.addEventListener(WebInspector.TimelineDataGrid.Event.FiltersDidChange, this._dataGridFiltersDidChange, this);
92         this._dataGrid.addEventListener(WebInspector.DataGrid.Event.SelectedNodeChanged, this._dataGridNodeSelected, this);
93         this._dataGrid.sortColumnIdentifierSetting = new WebInspector.Setting("network-timeline-view-sort", "requestSent");
94         this._dataGrid.sortOrderSetting = new WebInspector.Setting("network-timeline-view-sort-order", WebInspector.DataGrid.SortOrder.Ascending);
95
96         this.element.classList.add("network");
97         this.addSubview(this._dataGrid);
98
99         timeline.addEventListener(WebInspector.Timeline.Event.RecordAdded, this._networkTimelineRecordAdded, this);
100
101         this._pendingRecords = [];
102         this._resourceDataGridNodeMap = new Map;
103     }
104
105     // Public
106
107     get selectionPathComponents()
108     {
109         if (!this._dataGrid.selectedNode || this._dataGrid.selectedNode.hidden)
110             return null;
111
112         console.assert(this._dataGrid.selectedNode instanceof WebInspector.ResourceTimelineDataGridNode);
113
114         let pathComponent = new WebInspector.TimelineDataGridNodePathComponent(this._dataGrid.selectedNode, this._dataGrid.selectedNode.resource);
115         pathComponent.addEventListener(WebInspector.HierarchicalPathComponent.Event.SiblingWasSelected, this.dataGridNodePathComponentSelected, this);
116         return [pathComponent];
117     }
118
119     shown()
120     {
121         super.shown();
122
123         this._dataGrid.shown();
124     }
125
126     hidden()
127     {
128         this._dataGrid.hidden();
129
130         super.hidden();
131     }
132
133     closed()
134     {
135         console.assert(this.representedObject instanceof WebInspector.Timeline);
136         this.representedObject.removeEventListener(null, null, this);
137
138         this._dataGrid.closed();
139     }
140
141     matchTreeElementAgainstCustomFilters(treeElement)
142     {
143         return this._dataGrid.treeElementMatchesActiveScopeFilters(treeElement);
144     }
145
146     reset()
147     {
148         super.reset();
149
150         this._dataGrid.reset();
151
152         this._pendingRecords = [];
153     }
154
155     // Protected
156
157     canShowContentViewForTreeElement(treeElement)
158     {
159         if (treeElement instanceof WebInspector.ResourceTreeElement || treeElement instanceof WebInspector.ScriptTreeElement)
160             return true;
161         return super.canShowContentViewForTreeElement(treeElement);
162     }
163
164     showContentViewForTreeElement(treeElement)
165     {
166         if (treeElement instanceof WebInspector.ResourceTreeElement || treeElement instanceof WebInspector.ScriptTreeElement) {
167             WebInspector.showSourceCode(treeElement.representedObject);
168             return;
169         }
170
171         console.error("Unknown tree element selected.", treeElement);
172     }
173
174     dataGridNodePathComponentSelected(event)
175     {
176         let pathComponent = event.data.pathComponent;
177         console.assert(pathComponent instanceof WebInspector.TimelineDataGridNodePathComponent);
178
179         let dataGridNode = pathComponent.timelineDataGridNode;
180         console.assert(dataGridNode.dataGrid === this._dataGrid);
181
182         dataGridNode.revealAndSelect();
183     }
184
185     layout()
186     {
187         this._processPendingRecords();
188     }
189
190     // Private
191
192     _processPendingRecords()
193     {
194         if (!this._pendingRecords.length)
195             return;
196
197         for (let resourceTimelineRecord of this._pendingRecords) {
198             // Skip the record if it already exists in the grid.
199             // FIXME: replace with this._dataGrid.findDataGridNode(resourceTimelineRecord.resource) once <https://webkit.org/b/155305> is fixed.
200             let dataGridNode = this._resourceDataGridNodeMap.get(resourceTimelineRecord.resource);
201             if (dataGridNode)
202                 continue;
203
204             dataGridNode = new WebInspector.ResourceTimelineDataGridNode(resourceTimelineRecord, false, this);
205             this._resourceDataGridNodeMap.set(resourceTimelineRecord.resource, dataGridNode);
206
207             this._dataGrid.addRowInSortOrder(null, dataGridNode);
208         }
209
210         this._pendingRecords = [];
211     }
212
213     _networkTimelineRecordAdded(event)
214     {
215         var resourceTimelineRecord = event.data.record;
216         console.assert(resourceTimelineRecord instanceof WebInspector.ResourceTimelineRecord);
217
218         this._pendingRecords.push(resourceTimelineRecord);
219
220         this.needsLayout();
221     }
222
223     _dataGridFiltersDidChange(event)
224     {
225         // FIXME: <https://webkit.org/b/154924> Web Inspector: hook up grid row filtering in the new Timelines UI
226     }
227
228     _dataGridNodeSelected(event)
229     {
230         this.dispatchEventToListeners(WebInspector.ContentView.Event.SelectionPathComponentsDidChange);
231     }
232 };