Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 5 | import * as Common from '../common/common.js'; |
| 6 | import * as DataGrid from '../data_grid/data_grid.js'; |
| 7 | import * as Network from '../network/network.js'; |
Simon Zünd | 055385d | 2020-03-02 09:54:44 | [diff] [blame] | 8 | import * as Platform from '../platform/platform.js'; |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 9 | import * as SDK from '../sdk/sdk.js'; |
| 10 | import * as UI from '../ui/ui.js'; |
| 11 | |
| 12 | export class ServiceWorkerCacheView extends UI.View.SimpleView { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 13 | /** |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 14 | * @param {!SDK.ServiceWorkerCacheModel.ServiceWorkerCacheModel} model |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 15 | * @param {!SDK.ServiceWorkerCacheModel.Cache} cache |
| 16 | */ |
| 17 | constructor(model, cache) { |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 18 | super(Common.UIString.UIString('Cache')); |
Jack Franklin | 71519f8 | 2020-11-03 12:08:59 | [diff] [blame^] | 19 | this.registerRequiredCSS('resources/serviceWorkerCacheViews.css', {enableLegacyPatching: true}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 20 | |
| 21 | this._model = model; |
| 22 | this._entriesForTest = null; |
| 23 | |
| 24 | this.element.classList.add('service-worker-cache-data-view'); |
| 25 | this.element.classList.add('storage-view'); |
| 26 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 27 | const editorToolbar = new UI.Toolbar.Toolbar('data-view-toolbar', this.element); |
| 28 | this._splitWidget = new UI.SplitWidget.SplitWidget(false, false); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 29 | this._splitWidget.show(this.element); |
| 30 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 31 | this._previewPanel = new UI.Widget.VBox(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 32 | const resizer = this._previewPanel.element.createChild('div', 'cache-preview-panel-resizer'); |
| 33 | this._splitWidget.setMainWidget(this._previewPanel); |
| 34 | this._splitWidget.installResizer(resizer); |
| 35 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 36 | /** @type {?UI.Widget.Widget} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 37 | this._preview = null; |
| 38 | |
| 39 | this._cache = cache; |
Tim van der Lippe | b842a76 | 2020-10-15 13:12:33 | [diff] [blame] | 40 | /** @type {?DataGrid.DataGrid.DataGridImpl<!DataGridNode>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 41 | this._dataGrid = null; |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 42 | this._refreshThrottler = new Common.Throttler.Throttler(300); |
| 43 | this._refreshButton = new UI.Toolbar.ToolbarButton(Common.UIString.UIString('Refresh'), 'largeicon-refresh'); |
| 44 | this._refreshButton.addEventListener(UI.Toolbar.ToolbarButton.Events.Click, this._refreshButtonClicked, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 45 | editorToolbar.appendToolbarItem(this._refreshButton); |
| 46 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 47 | this._deleteSelectedButton = |
| 48 | new UI.Toolbar.ToolbarButton(Common.UIString.UIString('Delete Selected'), 'largeicon-delete'); |
Tim van der Lippe | 37a35ff | 2020-03-03 13:49:02 | [diff] [blame] | 49 | this._deleteSelectedButton.addEventListener(UI.Toolbar.ToolbarButton.Events.Click, event => { |
| 50 | this._deleteButtonClicked(null); |
| 51 | }); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 52 | editorToolbar.appendToolbarItem(this._deleteSelectedButton); |
| 53 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 54 | const entryPathFilterBox = new UI.Toolbar.ToolbarInput(ls`Filter by Path`, '', 1); |
Harley Li | 574c720 | 2018-12-03 19:55:54 | [diff] [blame] | 55 | editorToolbar.appendToolbarItem(entryPathFilterBox); |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 56 | const entryPathFilterThrottler = new Common.Throttler.Throttler(300); |
Harley Li | 574c720 | 2018-12-03 19:55:54 | [diff] [blame] | 57 | this._entryPathFilter = ''; |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 58 | entryPathFilterBox.addEventListener(UI.Toolbar.ToolbarInput.Event.TextChanged, () => { |
Harley Li | 574c720 | 2018-12-03 19:55:54 | [diff] [blame] | 59 | entryPathFilterThrottler.schedule(() => { |
| 60 | this._entryPathFilter = entryPathFilterBox.value(); |
Harley Li | 574c720 | 2018-12-03 19:55:54 | [diff] [blame] | 61 | return this._updateData(true); |
| 62 | }); |
| 63 | }); |
| 64 | |
Harley Li | 2ba8ce7 | 2019-03-26 21:17:21 | [diff] [blame] | 65 | this._returnCount = /** @type {?number} */ (null); |
| 66 | this._summaryBarElement = /** @type {?Element} */ (null); |
Tim van der Lippe | b842a76 | 2020-10-15 13:12:33 | [diff] [blame] | 67 | /** @type {?Promise<!{entries: !Array<!Protocol.CacheStorage.DataEntry>, returnCount: number}>} */ |
| 68 | this._loadingPromise = null; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 69 | |
| 70 | this.update(cache); |
| 71 | } |
| 72 | |
Harley Li | 574c720 | 2018-12-03 19:55:54 | [diff] [blame] | 73 | _resetDataGrid() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 74 | if (this._dataGrid) { |
Harley Li | 574c720 | 2018-12-03 19:55:54 | [diff] [blame] | 75 | this._dataGrid.asWidget().detach(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 76 | } |
Harley Li | 574c720 | 2018-12-03 19:55:54 | [diff] [blame] | 77 | this._dataGrid = this._createDataGrid(); |
Harley Li | 2ba8ce7 | 2019-03-26 21:17:21 | [diff] [blame] | 78 | const dataGridWidget = this._dataGrid.asWidget(); |
| 79 | this._splitWidget.setSidebarWidget(dataGridWidget); |
| 80 | dataGridWidget.setMinimumSize(0, 250); |
Harley Li | 574c720 | 2018-12-03 19:55:54 | [diff] [blame] | 81 | } |
| 82 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 83 | /** |
| 84 | * @override |
| 85 | */ |
| 86 | wasShown() { |
| 87 | this._model.addEventListener( |
| 88 | SDK.ServiceWorkerCacheModel.Events.CacheStorageContentUpdated, this._cacheContentUpdated, this); |
| 89 | this._updateData(true); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @override |
| 94 | */ |
| 95 | willHide() { |
| 96 | this._model.removeEventListener( |
| 97 | SDK.ServiceWorkerCacheModel.Events.CacheStorageContentUpdated, this._cacheContentUpdated, this); |
| 98 | } |
| 99 | |
| 100 | /** |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 101 | * @param {?UI.Widget.Widget} preview |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 102 | */ |
| 103 | _showPreview(preview) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 104 | if (preview && this._preview === preview) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 105 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 106 | } |
| 107 | if (this._preview) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 108 | this._preview.detach(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 109 | } |
| 110 | if (!preview) { |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 111 | preview = new UI.EmptyWidget.EmptyWidget(Common.UIString.UIString('Select a cache entry above to preview')); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 112 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 113 | this._preview = preview; |
| 114 | this._preview.show(this._previewPanel.element); |
| 115 | } |
| 116 | |
| 117 | /** |
Tim van der Lippe | b842a76 | 2020-10-15 13:12:33 | [diff] [blame] | 118 | * @return {!DataGrid.DataGrid.DataGridImpl<!DataGridNode>} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 119 | */ |
| 120 | _createDataGrid() { |
Tim van der Lippe | 9a9aba8 | 2020-02-14 14:52:29 | [diff] [blame] | 121 | const columns = /** @type {!Array<!DataGrid.DataGrid.ColumnDescriptor>} */ ([ |
Harley Li | 2ba8ce7 | 2019-03-26 21:17:21 | [diff] [blame] | 122 | {id: 'number', title: '#', sortable: false, width: '3px'}, |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 123 | {id: 'name', title: Common.UIString.UIString('Name'), weight: 4, sortable: true}, |
Harley Li | 044c7ac | 2018-10-26 15:14:20 | [diff] [blame] | 124 | {id: 'responseType', title: ls`Response-Type`, weight: 1, align: DataGrid.DataGrid.Align.Right, sortable: true}, |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 125 | {id: 'contentType', title: Common.UIString.UIString('Content-Type'), weight: 1, sortable: true}, { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 126 | id: 'contentLength', |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 127 | title: Common.UIString.UIString('Content-Length'), |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 128 | weight: 1, |
| 129 | align: DataGrid.DataGrid.Align.Right, |
| 130 | sortable: true |
| 131 | }, |
| 132 | { |
| 133 | id: 'responseTime', |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 134 | title: Common.UIString.UIString('Time Cached'), |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 135 | width: '12em', |
| 136 | weight: 1, |
| 137 | align: DataGrid.DataGrid.Align.Right, |
| 138 | sortable: true |
| 139 | } |
| 140 | ]); |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 141 | const dataGrid = new DataGrid.DataGrid.DataGridImpl({ |
Michael Liao | 18769f2 | 2019-12-12 19:01:25 | [diff] [blame] | 142 | displayName: ls`Service Worker Cache`, |
| 143 | columns, |
| 144 | deleteCallback: this._deleteButtonClicked.bind(this), |
Tim van der Lippe | b842a76 | 2020-10-15 13:12:33 | [diff] [blame] | 145 | refreshCallback: this._updateData.bind(this, true), |
| 146 | editCallback: undefined, |
Michael Liao | 18769f2 | 2019-12-12 19:01:25 | [diff] [blame] | 147 | }); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 148 | |
| 149 | dataGrid.addEventListener(DataGrid.DataGrid.Events.SortingChanged, this._sortingChanged, this); |
| 150 | |
Tim van der Lippe | 37a35ff | 2020-03-03 13:49:02 | [diff] [blame] | 151 | dataGrid.addEventListener(DataGrid.DataGrid.Events.SelectedNode, event => { |
| 152 | this._previewCachedResponse(event.data.data); |
| 153 | }, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 154 | dataGrid.setStriped(true); |
| 155 | return dataGrid; |
| 156 | } |
| 157 | |
| 158 | _sortingChanged() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 159 | if (!this._dataGrid) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 160 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 161 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 162 | |
Tim van der Lippe | b842a76 | 2020-10-15 13:12:33 | [diff] [blame] | 163 | const dataGrid = this._dataGrid; |
| 164 | |
| 165 | const accending = dataGrid.isSortOrderAscending(); |
| 166 | const columnId = dataGrid.sortColumnId(); |
| 167 | /** @type {function(!DataGridNode, !DataGridNode): number} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 168 | let comparator; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 169 | if (columnId === 'name') { |
Jan Scheffler | 059ec11 | 2019-09-05 16:54:51 | [diff] [blame] | 170 | comparator = (a, b) => a._name.localeCompare(b._name); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 171 | } else if (columnId === 'contentType') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 172 | comparator = (a, b) => a.data.mimeType.localeCompare(b.data.mimeType); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 173 | } else if (columnId === 'contentLength') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 174 | comparator = (a, b) => a.data.resourceSize - b.data.resourceSize; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 175 | } else if (columnId === 'responseTime') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 176 | comparator = (a, b) => a.data.endTime - b.data.endTime; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 177 | } else if (columnId === 'responseType') { |
Jan Scheffler | 17b984e | 2019-09-05 13:40:56 | [diff] [blame] | 178 | comparator = (a, b) => a._responseType.localeCompare(b._responseType); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 179 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 180 | |
Tim van der Lippe | b842a76 | 2020-10-15 13:12:33 | [diff] [blame] | 181 | const children = dataGrid.rootNode().children.slice(); |
| 182 | dataGrid.rootNode().removeChildren(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 183 | children.sort((a, b) => { |
Andres Olivares | 21ab05b | 2020-10-21 14:00:02 | [diff] [blame] | 184 | const result = comparator(/** @type {!DataGridNode} */ (a), /** @type {!DataGridNode} */ (b)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 185 | return accending ? result : -result; |
| 186 | }); |
Tim van der Lippe | b842a76 | 2020-10-15 13:12:33 | [diff] [blame] | 187 | children.forEach(child => dataGrid.rootNode().appendChild(child)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /** |
Tim van der Lippe | b842a76 | 2020-10-15 13:12:33 | [diff] [blame] | 191 | * @param {?DataGrid.DataGrid.DataGridNode<!DataGridNode>} node |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 192 | */ |
| 193 | async _deleteButtonClicked(node) { |
| 194 | if (!node) { |
| 195 | node = this._dataGrid && this._dataGrid.selectedNode; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 196 | if (!node) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 197 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 198 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 199 | } |
| 200 | await this._model.deleteCacheEntry(this._cache, /** @type {string} */ (node.data.url())); |
| 201 | node.remove(); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * @param {!SDK.ServiceWorkerCacheModel.Cache} cache |
| 206 | */ |
| 207 | update(cache) { |
| 208 | this._cache = cache; |
Harley Li | 574c720 | 2018-12-03 19:55:54 | [diff] [blame] | 209 | this._resetDataGrid(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 210 | this._updateData(true); |
| 211 | } |
| 212 | |
Harley Li | 2ba8ce7 | 2019-03-26 21:17:21 | [diff] [blame] | 213 | _updateSummaryBar() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 214 | if (!this._summaryBarElement) { |
Harley Li | 2ba8ce7 | 2019-03-26 21:17:21 | [diff] [blame] | 215 | this._summaryBarElement = this.element.createChild('div', 'cache-storage-summary-bar'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 216 | } |
Harley Li | 2ba8ce7 | 2019-03-26 21:17:21 | [diff] [blame] | 217 | this._summaryBarElement.removeChildren(); |
| 218 | |
| 219 | const span = this._summaryBarElement.createChild('span'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 220 | if (this._entryPathFilter) { |
Harley Li | 2ba8ce7 | 2019-03-26 21:17:21 | [diff] [blame] | 221 | span.textContent = ls`Matching entries: ${this._returnCount}`; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 222 | } else { |
Harley Li | 2ba8ce7 | 2019-03-26 21:17:21 | [diff] [blame] | 223 | span.textContent = ls`Total entries: ${this._returnCount}`; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 224 | } |
Harley Li | 2ba8ce7 | 2019-03-26 21:17:21 | [diff] [blame] | 225 | } |
| 226 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 227 | /** |
| 228 | * @param {number} skipCount |
| 229 | * @param {!Array<!Protocol.CacheStorage.DataEntry>} entries |
Harley Li | 2ba8ce7 | 2019-03-26 21:17:21 | [diff] [blame] | 230 | * @param {number} returnCount |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 231 | * @this {ServiceWorkerCacheView} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 232 | */ |
Harley Li | 2ba8ce7 | 2019-03-26 21:17:21 | [diff] [blame] | 233 | _updateDataCallback(skipCount, entries, returnCount) { |
Tim van der Lippe | b842a76 | 2020-10-15 13:12:33 | [diff] [blame] | 234 | if (!this._dataGrid) { |
| 235 | return; |
| 236 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 237 | const selected = this._dataGrid.selectedNode && this._dataGrid.selectedNode.data.url(); |
| 238 | this._refreshButton.setEnabled(true); |
| 239 | this._entriesForTest = entries; |
Harley Li | 2ba8ce7 | 2019-03-26 21:17:21 | [diff] [blame] | 240 | this._returnCount = returnCount; |
| 241 | this._updateSummaryBar(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 242 | |
Tim van der Lippe | b842a76 | 2020-10-15 13:12:33 | [diff] [blame] | 243 | /** @type {!Map<string, !DataGridNode>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 244 | const oldEntries = new Map(); |
| 245 | const rootNode = this._dataGrid.rootNode(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 246 | for (const node of rootNode.children) { |
Andres Olivares | 21ab05b | 2020-10-21 14:00:02 | [diff] [blame] | 247 | oldEntries.set(node.data.url, /** @type {!DataGridNode} */ (node)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 248 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 249 | rootNode.removeChildren(); |
| 250 | let selectedNode = null; |
Harley Li | 2ba8ce7 | 2019-03-26 21:17:21 | [diff] [blame] | 251 | for (let i = 0; i < entries.length; ++i) { |
| 252 | const entry = entries[i]; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 253 | let node = oldEntries.get(entry.requestURL); |
| 254 | if (!node || node.data.responseTime !== entry.responseTime) { |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 255 | node = new DataGridNode(i, this._createRequest(entry), entry.responseType); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 256 | node.selectable = true; |
Harley Li | 2ba8ce7 | 2019-03-26 21:17:21 | [diff] [blame] | 257 | } else { |
Jan Scheffler | f46e203 | 2019-11-05 14:53:34 | [diff] [blame] | 258 | node.data.number = i; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 259 | } |
| 260 | rootNode.appendChild(node); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 261 | if (entry.requestURL === selected) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 262 | selectedNode = node; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 263 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 264 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 265 | if (!selectedNode) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 266 | this._showPreview(null); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 267 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 268 | selectedNode.revealAndSelect(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 269 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 270 | this._updatedForTest(); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * @param {boolean} force |
| 275 | */ |
Jan Scheffler | f46e203 | 2019-11-05 14:53:34 | [diff] [blame] | 276 | async _updateData(force) { |
| 277 | if (!force && this._loadingPromise) { |
| 278 | return this._loadingPromise; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 279 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 280 | this._refreshButton.setEnabled(false); |
Harley Li | 574c720 | 2018-12-03 19:55:54 | [diff] [blame] | 281 | |
Jan Scheffler | f46e203 | 2019-11-05 14:53:34 | [diff] [blame] | 282 | if (this._loadingPromise) { |
| 283 | return this._loadingPromise; |
| 284 | } |
| 285 | |
| 286 | this._loadingPromise = new Promise(resolve => { |
| 287 | this._model.loadAllCacheData(this._cache, this._entryPathFilter, (entries, returnCount) => { |
Tim van der Lippe | b842a76 | 2020-10-15 13:12:33 | [diff] [blame] | 288 | resolve({entries, returnCount}); |
Harley Li | 574c720 | 2018-12-03 19:55:54 | [diff] [blame] | 289 | }); |
| 290 | }); |
Jan Scheffler | f46e203 | 2019-11-05 14:53:34 | [diff] [blame] | 291 | |
Tim van der Lippe | b842a76 | 2020-10-15 13:12:33 | [diff] [blame] | 292 | const {entries, returnCount} = await this._loadingPromise; |
Jan Scheffler | f46e203 | 2019-11-05 14:53:34 | [diff] [blame] | 293 | this._updateDataCallback(0, entries, returnCount); |
| 294 | this._loadingPromise = null; |
Tim van der Lippe | b842a76 | 2020-10-15 13:12:33 | [diff] [blame] | 295 | return; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /** |
Tim van der Lippe | c02a97c | 2020-02-14 14:39:27 | [diff] [blame] | 299 | * @param {!Common.EventTarget.EventTargetEvent} event |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 300 | */ |
| 301 | _refreshButtonClicked(event) { |
| 302 | this._updateData(true); |
| 303 | } |
| 304 | |
| 305 | /** |
Tim van der Lippe | c02a97c | 2020-02-14 14:39:27 | [diff] [blame] | 306 | * @param {!Common.EventTarget.EventTargetEvent} event |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 307 | */ |
| 308 | _cacheContentUpdated(event) { |
| 309 | const nameAndOrigin = event.data; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 310 | if (this._cache.securityOrigin !== nameAndOrigin.origin || this._cache.cacheName !== nameAndOrigin.cacheName) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 311 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 312 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 313 | this._refreshThrottler.schedule(() => Promise.resolve(this._updateData(true)), true); |
| 314 | } |
| 315 | |
| 316 | /** |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 317 | * @param {!SDK.NetworkRequest.NetworkRequest} request |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 318 | */ |
| 319 | async _previewCachedResponse(request) { |
Tim van der Lippe | b842a76 | 2020-10-15 13:12:33 | [diff] [blame] | 320 | let preview = networkRequestToPreview.get(request); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 321 | if (!preview) { |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 322 | preview = new RequestView(request); |
Tim van der Lippe | b842a76 | 2020-10-15 13:12:33 | [diff] [blame] | 323 | networkRequestToPreview.set(request, preview); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | // It is possible that table selection changes before the preview opens. |
Tim van der Lippe | b842a76 | 2020-10-15 13:12:33 | [diff] [blame] | 327 | if (this._dataGrid && this._dataGrid.selectedNode && request === this._dataGrid.selectedNode.data) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 328 | this._showPreview(preview); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 329 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /** |
| 333 | * @param {!Protocol.CacheStorage.DataEntry} entry |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 334 | * @return {!SDK.NetworkRequest.NetworkRequest} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 335 | */ |
| 336 | _createRequest(entry) { |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 337 | const request = |
| 338 | new SDK.NetworkRequest.NetworkRequest('cache-storage-' + entry.requestURL, entry.requestURL, '', '', '', null); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 339 | request.requestMethod = entry.requestMethod; |
| 340 | request.setRequestHeaders(entry.requestHeaders); |
| 341 | request.statusCode = entry.responseStatus; |
| 342 | request.statusText = entry.responseStatusText; |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 343 | request.protocol = new Common.ParsedURL.ParsedURL(entry.requestURL).scheme; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 344 | request.responseHeaders = entry.responseHeaders; |
| 345 | request.setRequestHeadersText(''); |
| 346 | request.endTime = entry.responseTime; |
| 347 | |
| 348 | let header = entry.responseHeaders.find(header => header.name.toLowerCase() === 'content-type'); |
Tim van der Lippe | aa80078 | 2020-05-01 11:33:11 | [diff] [blame] | 349 | const contentType = header ? header.value : SDK.NetworkRequest.MIME_TYPE.PLAIN; |
| 350 | request.mimeType = /** @type {!SDK.NetworkRequest.MIME_TYPE} */ (contentType); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 351 | |
| 352 | header = entry.responseHeaders.find(header => header.name.toLowerCase() === 'content-length'); |
Tim van der Lippe | b842a76 | 2020-10-15 13:12:33 | [diff] [blame] | 353 | request.resourceSize = (header && Number(header.value)) || 0; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 354 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 355 | let resourceType = Common.ResourceType.ResourceType.fromMimeType(contentType); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 356 | if (!resourceType) { |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 357 | resourceType = |
| 358 | Common.ResourceType.ResourceType.fromURL(entry.requestURL) || Common.ResourceType.resourceTypes.Other; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 359 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 360 | request.setResourceType(resourceType); |
| 361 | request.setContentDataProvider(this._requestContent.bind(this, request)); |
| 362 | return request; |
| 363 | } |
| 364 | |
| 365 | /** |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 366 | * @param {!SDK.NetworkRequest.NetworkRequest} request |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 367 | * @return {!Promise<!SDK.NetworkRequest.ContentData>} |
| 368 | */ |
| 369 | async _requestContent(request) { |
| 370 | const isText = request.resourceType().isTextType(); |
Tim van der Lippe | b842a76 | 2020-10-15 13:12:33 | [diff] [blame] | 371 | /** @type {!SDK.NetworkRequest.ContentData} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 372 | const contentData = {error: null, content: null, encoded: !isText}; |
Harley Li | 915e7e4 | 2019-02-07 17:53:55 | [diff] [blame] | 373 | const response = await this._cache.requestCachedResponse(request.url(), request.requestHeaders()); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 374 | if (response) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 375 | contentData.content = isText ? window.atob(response.body) : response.body; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 376 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 377 | return contentData; |
| 378 | } |
| 379 | |
| 380 | _updatedForTest() { |
| 381 | } |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 382 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 383 | |
Tim van der Lippe | b842a76 | 2020-10-15 13:12:33 | [diff] [blame] | 384 | /** @type {!WeakMap<!SDK.NetworkRequest.NetworkRequest, !RequestView>} */ |
| 385 | const networkRequestToPreview = new WeakMap(); |
| 386 | |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 387 | ServiceWorkerCacheView._previewSymbol = Symbol('preview'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 388 | |
Tim van der Lippe | b842a76 | 2020-10-15 13:12:33 | [diff] [blame] | 389 | /** |
| 390 | * @extends {DataGrid.DataGrid.DataGridNode<!DataGridNode>} |
| 391 | */ |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 392 | export class DataGridNode extends DataGrid.DataGrid.DataGridNode { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 393 | /** |
Harley Li | 2ba8ce7 | 2019-03-26 21:17:21 | [diff] [blame] | 394 | * @param {number} number |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 395 | * @param {!SDK.NetworkRequest.NetworkRequest} request |
Harley Li | 044c7ac | 2018-10-26 15:14:20 | [diff] [blame] | 396 | * @param {!Protocol.CacheStorage.CachedResponseType} responseType |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 397 | */ |
Harley Li | 2ba8ce7 | 2019-03-26 21:17:21 | [diff] [blame] | 398 | constructor(number, request, responseType) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 399 | super(request); |
Harley Li | 2ba8ce7 | 2019-03-26 21:17:21 | [diff] [blame] | 400 | this._number = number; |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 401 | const parsed = new Common.ParsedURL.ParsedURL(request.url()); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 402 | if (parsed.isValid) { |
Simon Zünd | 055385d | 2020-03-02 09:54:44 | [diff] [blame] | 403 | this._name = Platform.StringUtilities.trimURL(request.url(), parsed.domain()); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 404 | } else { |
Harley Li | 7e32893 | 2019-04-18 18:41:01 | [diff] [blame] | 405 | this._name = request.url(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 406 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 407 | this._request = request; |
Harley Li | 044c7ac | 2018-10-26 15:14:20 | [diff] [blame] | 408 | this._responseType = responseType; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /** |
| 412 | * @override |
| 413 | * @param {string} columnId |
Sigurd Schneider | 1165791 | 2020-06-30 13:26:52 | [diff] [blame] | 414 | * @return {!HTMLElement} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 415 | */ |
| 416 | createCell(columnId) { |
| 417 | const cell = this.createTD(columnId); |
| 418 | let value; |
Harley Li | 2ba8ce7 | 2019-03-26 21:17:21 | [diff] [blame] | 419 | if (columnId === 'number') { |
| 420 | value = String(this._number); |
Harley Li | 7e32893 | 2019-04-18 18:41:01 | [diff] [blame] | 421 | } else if (columnId === 'name') { |
| 422 | value = this._name; |
Harley Li | 044c7ac | 2018-10-26 15:14:20 | [diff] [blame] | 423 | } else if (columnId === 'responseType') { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 424 | if (this._responseType === 'opaqueResponse') { |
Harley Li | 6b1c2da | 2018-11-29 23:19:30 | [diff] [blame] | 425 | value = 'opaque'; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 426 | } else if (this._responseType === 'opaqueRedirect') { |
Harley Li | 044c7ac | 2018-10-26 15:14:20 | [diff] [blame] | 427 | value = 'opaqueredirect'; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 428 | } else { |
Harley Li | 044c7ac | 2018-10-26 15:14:20 | [diff] [blame] | 429 | value = this._responseType; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 430 | } |
Harley Li | 044c7ac | 2018-10-26 15:14:20 | [diff] [blame] | 431 | } else if (columnId === 'contentType') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 432 | value = this._request.mimeType; |
Harley Li | 044c7ac | 2018-10-26 15:14:20 | [diff] [blame] | 433 | } else if (columnId === 'contentLength') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 434 | value = (this._request.resourceSize | 0).toLocaleString('en-US'); |
Harley Li | 044c7ac | 2018-10-26 15:14:20 | [diff] [blame] | 435 | } else if (columnId === 'responseTime') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 436 | value = new Date(this._request.endTime * 1000).toLocaleString(); |
Harley Li | 044c7ac | 2018-10-26 15:14:20 | [diff] [blame] | 437 | } |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 438 | DataGrid.DataGrid.DataGridImpl.setElementText(cell, value || '', true); |
Harley Li | 7e32893 | 2019-04-18 18:41:01 | [diff] [blame] | 439 | cell.title = this._request.url(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 440 | return cell; |
| 441 | } |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 442 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 443 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 444 | export class RequestView extends UI.Widget.VBox { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 445 | /** |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 446 | * @param {!SDK.NetworkRequest.NetworkRequest} request |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 447 | */ |
| 448 | constructor(request) { |
| 449 | super(); |
| 450 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 451 | this._tabbedPane = new UI.TabbedPane.TabbedPane(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 452 | this._tabbedPane.addEventListener(UI.TabbedPane.Events.TabSelected, this._tabSelected, this); |
Paul Lewis | 2d7d65c | 2020-03-16 17:26:30 | [diff] [blame] | 453 | this._resourceViewTabSetting = Common.Settings.Settings.instance().createSetting('cacheStorageViewTab', 'preview'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 454 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 455 | this._tabbedPane.appendTab( |
| 456 | 'headers', Common.UIString.UIString('Headers'), new Network.RequestHeadersView.RequestHeadersView(request)); |
| 457 | this._tabbedPane.appendTab( |
| 458 | 'preview', Common.UIString.UIString('Preview'), new Network.RequestPreviewView.RequestPreviewView(request)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 459 | this._tabbedPane.show(this.element); |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * @override |
| 464 | */ |
| 465 | wasShown() { |
| 466 | super.wasShown(); |
| 467 | this._selectTab(); |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * @param {string=} tabId |
| 472 | */ |
| 473 | _selectTab(tabId) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 474 | if (!tabId) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 475 | tabId = this._resourceViewTabSetting.get(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 476 | } |
Tim van der Lippe | b842a76 | 2020-10-15 13:12:33 | [diff] [blame] | 477 | if (tabId && !this._tabbedPane.selectTab(tabId)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 478 | this._tabbedPane.selectTab('headers'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 479 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 480 | } |
| 481 | |
Tim van der Lippe | b842a76 | 2020-10-15 13:12:33 | [diff] [blame] | 482 | /** |
| 483 | * @param {!Common.EventTarget.EventTargetEvent} event |
| 484 | */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 485 | _tabSelected(event) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 486 | if (!event.data.isUserGesture) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 487 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 488 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 489 | this._resourceViewTabSetting.set(event.data.tabId); |
| 490 | } |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 491 | } |