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