Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 1 | // Copyright 2016 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 | |
Paul Lewis | 5147419 | 2020-01-09 16:02:22 +0000 | [diff] [blame] | 5 | import {AuditController, Events} from './AuditsController.js'; |
| 6 | import {ProtocolService} from './AuditsProtocolService.js'; |
| 7 | import {AuditsReportRenderer, AuditsReportUIFeatures} from './AuditsReportRenderer.js'; |
| 8 | import {Item, ReportSelector} from './AuditsReportSelector.js'; |
| 9 | import {StartView} from './AuditsStartView.js'; |
| 10 | import {StatusView} from './AuditsStatusView.js'; |
| 11 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 12 | /** |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 13 | * @unrestricted |
| 14 | */ |
Paul Lewis | 5147419 | 2020-01-09 16:02:22 +0000 | [diff] [blame] | 15 | export class AuditsPanel extends UI.Panel { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 16 | constructor() { |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 17 | super('audits'); |
| 18 | this.registerRequiredCSS('audits/lighthouse/report.css'); |
| 19 | this.registerRequiredCSS('audits/auditsPanel.css'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 20 | |
Paul Lewis | 5147419 | 2020-01-09 16:02:22 +0000 | [diff] [blame] | 21 | this._protocolService = new ProtocolService(); |
| 22 | this._controller = new AuditController(this._protocolService); |
| 23 | this._startView = new StartView(this._controller); |
| 24 | this._statusView = new StatusView(this._controller); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 25 | |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 26 | this._unauditableExplanation = null; |
| 27 | this._cachedRenderedReports = new Map(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 28 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 29 | this._dropTarget = new UI.DropTarget( |
| 30 | this.contentElement, [UI.DropTarget.Type.File], Common.UIString('Drop audit file here'), |
| 31 | this._handleDrop.bind(this)); |
| 32 | |
Paul Lewis | 5147419 | 2020-01-09 16:02:22 +0000 | [diff] [blame] | 33 | this._controller.addEventListener(Events.PageAuditabilityChanged, this._refreshStartAuditUI.bind(this)); |
| 34 | this._controller.addEventListener(Events.AuditProgressChanged, this._refreshStatusUI.bind(this)); |
| 35 | this._controller.addEventListener(Events.RequestAuditStart, this._startAudit.bind(this)); |
| 36 | this._controller.addEventListener(Events.RequestAuditCancel, this._cancelAudit.bind(this)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 37 | |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 38 | this._renderToolbar(); |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 39 | this._auditResultsElement = this.contentElement.createChild('div', 'audits-results-container'); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 40 | this._renderStartView(); |
| 41 | |
| 42 | this._controller.recomputePageAuditability(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /** |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 46 | * @param {!Common.Event} evt |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 47 | */ |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 48 | _refreshStartAuditUI(evt) { |
Connor Clark | ca8905e | 2019-08-23 18:35:10 +0000 | [diff] [blame] | 49 | // PageAuditabilityChanged fires multiple times during an audit, which we want to ignore. |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 50 | if (this._isLHAttached) { |
Connor Clark | ca8905e | 2019-08-23 18:35:10 +0000 | [diff] [blame] | 51 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 52 | } |
Connor Clark | ca8905e | 2019-08-23 18:35:10 +0000 | [diff] [blame] | 53 | |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 54 | this._unauditableExplanation = evt.data.helpText; |
| 55 | this._startView.setUnauditableExplanation(evt.data.helpText); |
| 56 | this._startView.setStartButtonEnabled(!evt.data.helpText); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 60 | * @param {!Common.Event} evt |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 61 | */ |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 62 | _refreshStatusUI(evt) { |
| 63 | this._statusView.updateStatus(evt.data.message); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | _refreshToolbarUI() { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 67 | this._clearButton.setEnabled(this._reportSelector.hasItems()); |
| 68 | } |
| 69 | |
| 70 | _clearAll() { |
| 71 | this._reportSelector.clearAll(); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 72 | this._renderStartView(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 73 | this._refreshToolbarUI(); |
| 74 | } |
| 75 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 76 | _renderToolbar() { |
Connor Clark | e66080e | 2019-11-06 16:35:51 -0800 | [diff] [blame] | 77 | const auditsToolbarContainer = this.element.createChild('div', 'audits-toolbar-container'); |
| 78 | |
| 79 | const toolbar = new UI.Toolbar('', auditsToolbarContainer); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 80 | |
| 81 | this._newButton = new UI.ToolbarButton(Common.UIString('Perform an audit\u2026'), 'largeicon-add'); |
| 82 | toolbar.appendToolbarItem(this._newButton); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 83 | this._newButton.addEventListener(UI.ToolbarButton.Events.Click, this._renderStartView.bind(this)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 84 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 85 | toolbar.appendSeparator(); |
| 86 | |
Paul Lewis | 5147419 | 2020-01-09 16:02:22 +0000 | [diff] [blame] | 87 | this._reportSelector = new ReportSelector(() => this._renderStartView()); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 88 | toolbar.appendToolbarItem(this._reportSelector.comboBox()); |
| 89 | |
| 90 | this._clearButton = new UI.ToolbarButton(Common.UIString('Clear all'), 'largeicon-clear'); |
| 91 | toolbar.appendToolbarItem(this._clearButton); |
| 92 | this._clearButton.addEventListener(UI.ToolbarButton.Events.Click, this._clearAll.bind(this)); |
| 93 | |
Connor Clark | e66080e | 2019-11-06 16:35:51 -0800 | [diff] [blame] | 94 | this._settingsPane = new UI.HBox(); |
| 95 | this._settingsPane.show(this.contentElement); |
| 96 | this._settingsPane.element.classList.add('audits-settings-pane'); |
| 97 | this._settingsPane.element.appendChild(this._startView.settingsToolbar().element); |
| 98 | this._showSettingsPaneSetting = Common.settings.createSetting('auditsShowSettingsToolbar', false); |
| 99 | |
| 100 | this._rightToolbar = new UI.Toolbar('', auditsToolbarContainer); |
| 101 | this._rightToolbar.appendSeparator(); |
| 102 | this._rightToolbar.appendToolbarItem( |
| 103 | new UI.ToolbarSettingToggle(this._showSettingsPaneSetting, 'largeicon-settings-gear', ls`Audits settings`)); |
| 104 | this._showSettingsPaneSetting.addChangeListener(this._updateSettingsPaneVisibility.bind(this)); |
| 105 | this._updateSettingsPaneVisibility(); |
| 106 | |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 107 | this._refreshToolbarUI(); |
| 108 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 109 | |
Connor Clark | e66080e | 2019-11-06 16:35:51 -0800 | [diff] [blame] | 110 | _updateSettingsPaneVisibility() { |
| 111 | this._settingsPane.element.classList.toggle('hidden', !this._showSettingsPaneSetting.get()); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @param {boolean} show |
| 116 | */ |
| 117 | _toggleSettingsDisplay(show) { |
| 118 | this._rightToolbar.element.classList.toggle('hidden', !show); |
| 119 | this._settingsPane.element.classList.toggle('hidden', !show); |
| 120 | this._updateSettingsPaneVisibility(); |
| 121 | } |
| 122 | |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 123 | _renderStartView() { |
| 124 | this._auditResultsElement.removeChildren(); |
| 125 | this._statusView.hide(); |
| 126 | |
| 127 | this._reportSelector.selectNewAudit(); |
| 128 | this.contentElement.classList.toggle('in-progress', false); |
| 129 | |
| 130 | this._startView.show(this.contentElement); |
Connor Clark | e66080e | 2019-11-06 16:35:51 -0800 | [diff] [blame] | 131 | this._toggleSettingsDisplay(true); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 132 | this._startView.setUnauditableExplanation(this._unauditableExplanation); |
| 133 | this._startView.setStartButtonEnabled(!this._unauditableExplanation); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 134 | if (!this._unauditableExplanation) { |
Patrick Hulce | 8d387f1 | 2018-05-29 18:54:54 +0000 | [diff] [blame] | 135 | this._startView.focusStartButton(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 136 | } |
Patrick Hulce | 8d387f1 | 2018-05-29 18:54:54 +0000 | [diff] [blame] | 137 | |
Patrick Hulce | 05c18ce | 2018-05-24 00:34:56 +0000 | [diff] [blame] | 138 | this._newButton.setEnabled(false); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 139 | this._refreshToolbarUI(); |
Patrick Hulce | 05c18ce | 2018-05-24 00:34:56 +0000 | [diff] [blame] | 140 | this.setDefaultFocusedChild(this._startView); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @param {string} inspectedURL |
| 145 | */ |
| 146 | _renderStatusView(inspectedURL) { |
| 147 | this.contentElement.classList.toggle('in-progress', true); |
| 148 | this._statusView.setInspectedURL(inspectedURL); |
| 149 | this._statusView.show(this.contentElement); |
| 150 | } |
| 151 | |
Connor Clark | 9950836 | 2019-08-20 19:52:23 +0000 | [diff] [blame] | 152 | _beforePrint() { |
| 153 | this._statusView.show(this.contentElement); |
| 154 | this._statusView.toggleCancelButton(false); |
| 155 | this._statusView.renderText(ls`Printing`, ls`The print popup window is open. Please close it to continue.`); |
| 156 | } |
| 157 | |
| 158 | _afterPrint() { |
| 159 | this._statusView.hide(); |
| 160 | this._statusView.toggleCancelButton(true); |
| 161 | } |
| 162 | |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 163 | /** |
| 164 | * @param {!ReportRenderer.ReportJSON} lighthouseResult |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 165 | * @param {!ReportRenderer.RunnerResultArtifacts=} artifacts |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 166 | */ |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 167 | _renderReport(lighthouseResult, artifacts) { |
Connor Clark | e66080e | 2019-11-06 16:35:51 -0800 | [diff] [blame] | 168 | this._toggleSettingsDisplay(false); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 169 | this.contentElement.classList.toggle('in-progress', false); |
| 170 | this._startView.hideWidget(); |
| 171 | this._statusView.hide(); |
| 172 | this._auditResultsElement.removeChildren(); |
Patrick Hulce | 05c18ce | 2018-05-24 00:34:56 +0000 | [diff] [blame] | 173 | this._newButton.setEnabled(true); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 174 | this._refreshToolbarUI(); |
| 175 | |
| 176 | const cachedRenderedReport = this._cachedRenderedReports.get(lighthouseResult); |
| 177 | if (cachedRenderedReport) { |
| 178 | this._auditResultsElement.appendChild(cachedRenderedReport); |
| 179 | return; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 182 | const reportContainer = this._auditResultsElement.createChild('div', 'lh-vars lh-root lh-devtools'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 183 | |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 184 | const dom = new DOM(/** @type {!Document} */ (this._auditResultsElement.ownerDocument)); |
Paul Lewis | 5147419 | 2020-01-09 16:02:22 +0000 | [diff] [blame] | 185 | const renderer = new AuditsReportRenderer(dom); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 186 | |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 +0000 | [diff] [blame] | 187 | const templatesHTML = Root.Runtime.cachedResources['audits/lighthouse/templates.html']; |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 188 | const templatesDOM = new DOMParser().parseFromString(templatesHTML, 'text/html'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 189 | if (!templatesDOM) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 190 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 191 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 192 | |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 193 | renderer.setTemplateContext(templatesDOM); |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 194 | const el = renderer.renderReport(lighthouseResult, reportContainer); |
Paul Lewis | 5147419 | 2020-01-09 16:02:22 +0000 | [diff] [blame] | 195 | AuditsReportRenderer.addViewTraceButton(el, artifacts); |
cjamcl@google.com | c5214af | 2019-06-25 20:31:21 +0000 | [diff] [blame] | 196 | // Linkifying requires the target be loaded. Do not block the report |
| 197 | // from rendering, as this is just an embellishment and the main target |
| 198 | // could take awhile to load. |
| 199 | this._waitForMainTargetLoad().then(() => { |
Paul Lewis | 5147419 | 2020-01-09 16:02:22 +0000 | [diff] [blame] | 200 | AuditsReportRenderer.linkifyNodeDetails(el); |
| 201 | AuditsReportRenderer.linkifySourceLocationDetails(el); |
cjamcl@google.com | c5214af | 2019-06-25 20:31:21 +0000 | [diff] [blame] | 202 | }); |
Paul Lewis | 5147419 | 2020-01-09 16:02:22 +0000 | [diff] [blame] | 203 | AuditsReportRenderer.handleDarkMode(el); |
cjamcl@google.com | f2f8c09 | 2019-05-30 22:01:56 +0000 | [diff] [blame] | 204 | |
Paul Lewis | 5147419 | 2020-01-09 16:02:22 +0000 | [diff] [blame] | 205 | const features = new AuditsReportUIFeatures(dom); |
Connor Clark | 9950836 | 2019-08-20 19:52:23 +0000 | [diff] [blame] | 206 | features.setBeforePrint(this._beforePrint.bind(this)); |
| 207 | features.setAfterPrint(this._afterPrint.bind(this)); |
cjamcl@google.com | f2f8c09 | 2019-05-30 22:01:56 +0000 | [diff] [blame] | 208 | features.setTemplateContext(templatesDOM); |
| 209 | features.initFeatures(lighthouseResult); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 210 | |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 211 | this._cachedRenderedReports.set(lighthouseResult, reportContainer); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 212 | } |
| 213 | |
cjamcl@google.com | c5214af | 2019-06-25 20:31:21 +0000 | [diff] [blame] | 214 | _waitForMainTargetLoad() { |
Paul Lewis | 4ae5f4f | 2020-01-23 10:19:33 +0000 | [diff] [blame^] | 215 | const mainTarget = self.SDK.targetManager.mainTarget(); |
cjamcl@google.com | c5214af | 2019-06-25 20:31:21 +0000 | [diff] [blame] | 216 | const resourceTreeModel = mainTarget.model(SDK.ResourceTreeModel); |
| 217 | return resourceTreeModel.once(SDK.ResourceTreeModel.Events.Load); |
| 218 | } |
| 219 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 220 | /** |
| 221 | * @param {!ReportRenderer.ReportJSON} lighthouseResult |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 222 | * @param {!ReportRenderer.RunnerResultArtifacts=} artifacts |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 223 | */ |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 224 | _buildReportUI(lighthouseResult, artifacts) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 225 | if (lighthouseResult === null) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 226 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 227 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 228 | |
Paul Lewis | 5147419 | 2020-01-09 16:02:22 +0000 | [diff] [blame] | 229 | const optionElement = new Item( |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 230 | lighthouseResult, () => this._renderReport(lighthouseResult, artifacts), this._renderStartView.bind(this)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 231 | this._reportSelector.prepend(optionElement); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 232 | this._refreshToolbarUI(); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 233 | this._renderReport(lighthouseResult); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /** |
| 237 | * @param {!DataTransfer} dataTransfer |
| 238 | */ |
| 239 | _handleDrop(dataTransfer) { |
| 240 | const items = dataTransfer.items; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 241 | if (!items.length) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 242 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 243 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 244 | const item = items[0]; |
| 245 | if (item.kind === 'file') { |
| 246 | const entry = items[0].webkitGetAsEntry(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 247 | if (!entry.isFile) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 248 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 249 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 250 | entry.file(file => { |
| 251 | const reader = new FileReader(); |
| 252 | reader.onload = () => this._loadedFromFile(/** @type {string} */ (reader.result)); |
| 253 | reader.readAsText(file); |
| 254 | }); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | /** |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 259 | * @param {string} report |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 260 | */ |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 261 | _loadedFromFile(report) { |
| 262 | const data = JSON.parse(report); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 263 | if (!data['lighthouseVersion']) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 264 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 265 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 266 | this._buildReportUI(/** @type {!ReportRenderer.ReportJSON} */ (data)); |
| 267 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 268 | |
Brandon Goddard | 04d5ba9 | 2019-12-19 08:31:55 -0800 | [diff] [blame] | 269 | /** |
| 270 | * @param {!Common.Event} event |
| 271 | */ |
| 272 | async _startAudit(event) { |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 273 | Host.userMetrics.actionTaken(Host.UserMetrics.Action.AuditsStarted); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 274 | |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 275 | try { |
| 276 | const inspectedURL = await this._controller.getInspectedURL({force: true}); |
| 277 | const categoryIDs = this._controller.getCategoryIDs(); |
| 278 | const flags = this._controller.getFlags(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 279 | |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 280 | await this._setupEmulationAndProtocolConnection(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 281 | |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 282 | this._renderStatusView(inspectedURL); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 283 | |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 284 | const lighthouseResponse = await this._protocolService.startLighthouse(inspectedURL, categoryIDs, flags); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 285 | |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 286 | if (lighthouseResponse && lighthouseResponse.fatal) { |
| 287 | const error = new Error(lighthouseResponse.message); |
| 288 | error.stack = lighthouseResponse.stack; |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 289 | throw error; |
| 290 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 291 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 292 | if (!lighthouseResponse) { |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 293 | throw new Error('Auditing failed to produce a result'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 294 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 295 | |
cjamcl@google.com | aa1532c | 2019-05-31 03:01:24 +0000 | [diff] [blame] | 296 | Host.userMetrics.actionTaken(Host.UserMetrics.Action.AuditsFinished); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 297 | |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 298 | await this._resetEmulationAndProtocolConnection(); |
Paul Irish | 8f1e33d | 2018-05-31 02:29:50 +0000 | [diff] [blame] | 299 | this._buildReportUI(lighthouseResponse.lhr, lighthouseResponse.artifacts); |
Brandon Goddard | 04d5ba9 | 2019-12-19 08:31:55 -0800 | [diff] [blame] | 300 | // Give focus to the new audit button when completed |
| 301 | this._newButton.element.focus(); |
| 302 | const keyboardInitiated = /** @type {boolean} */ (event.data); |
| 303 | if (keyboardInitiated) { |
| 304 | UI.markAsFocusedByKeyboard(this._newButton.element); |
| 305 | } |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 306 | } catch (err) { |
Paul Irish | dca01d0 | 2019-03-25 20:17:56 +0000 | [diff] [blame] | 307 | await this._resetEmulationAndProtocolConnection(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 308 | if (err instanceof Error) { |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 309 | this._statusView.renderBugReport(err); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 310 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 313 | |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 314 | async _cancelAudit() { |
| 315 | this._statusView.updateStatus(ls`Cancelling`); |
| 316 | await this._resetEmulationAndProtocolConnection(); |
| 317 | this._renderStartView(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Paul Irish | d849501 | 2019-07-16 23:51:47 +0000 | [diff] [blame] | 320 | /** |
| 321 | * We set the device emulation on the DevTools-side for two reasons: |
| 322 | * 1. To workaround some odd device metrics emulation bugs like occuluding viewports |
| 323 | * 2. To get the attractive device outline |
Connor Clark | ca8905e | 2019-08-23 18:35:10 +0000 | [diff] [blame] | 324 | * |
Connor Clark | 3ee5ac7 | 2019-11-07 15:11:58 -0800 | [diff] [blame] | 325 | * We also set flags.internalDisableDeviceScreenEmulation = true to let LH only apply UA emulation |
Paul Irish | d849501 | 2019-07-16 23:51:47 +0000 | [diff] [blame] | 326 | */ |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 327 | async _setupEmulationAndProtocolConnection() { |
| 328 | const flags = this._controller.getFlags(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 329 | |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 330 | const emulationModel = self.singleton(Emulation.DeviceModeModel); |
Connor Clark | ca8905e | 2019-08-23 18:35:10 +0000 | [diff] [blame] | 331 | this._stateBefore = { |
| 332 | emulation: { |
| 333 | enabled: emulationModel.enabledSetting().get(), |
| 334 | outlineEnabled: emulationModel.deviceOutlineSetting().get(), |
| 335 | toolbarControlsEnabled: emulationModel.toolbarControlsEnabledSetting().get() |
| 336 | }, |
| 337 | network: {conditions: SDK.multitargetNetworkManager.networkConditions()} |
| 338 | }; |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 339 | |
Connor Clark | ca8905e | 2019-08-23 18:35:10 +0000 | [diff] [blame] | 340 | emulationModel.toolbarControlsEnabledSetting().set(false); |
Connor Clark | 3f70034 | 2019-07-25 02:10:41 +0000 | [diff] [blame] | 341 | if (flags.emulatedFormFactor === 'desktop') { |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 342 | emulationModel.enabledSetting().set(false); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 343 | emulationModel.emulate(Emulation.DeviceModeModel.Type.None, null, null); |
Connor Clark | 3f70034 | 2019-07-25 02:10:41 +0000 | [diff] [blame] | 344 | } else if (flags.emulatedFormFactor === 'mobile') { |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 345 | emulationModel.enabledSetting().set(true); |
| 346 | emulationModel.deviceOutlineSetting().set(true); |
| 347 | |
| 348 | for (const device of Emulation.EmulatedDevicesList.instance().standard()) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 349 | if (device.title === 'Nexus 5X') { |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 350 | emulationModel.emulate(Emulation.DeviceModeModel.Type.Device, device, device.modes[0], 1); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 351 | } |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 352 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 355 | await this._protocolService.attach(); |
| 356 | this._isLHAttached = true; |
| 357 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 358 | |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 359 | async _resetEmulationAndProtocolConnection() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 360 | if (!this._isLHAttached) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 361 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 +0000 | [diff] [blame] | 362 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 363 | |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 364 | this._isLHAttached = false; |
| 365 | await this._protocolService.detach(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 366 | |
Connor Clark | ca8905e | 2019-08-23 18:35:10 +0000 | [diff] [blame] | 367 | if (this._stateBefore) { |
| 368 | const emulationModel = self.singleton(Emulation.DeviceModeModel); |
| 369 | emulationModel.enabledSetting().set(this._stateBefore.emulation.enabled); |
| 370 | emulationModel.deviceOutlineSetting().set(this._stateBefore.emulation.outlineEnabled); |
| 371 | emulationModel.toolbarControlsEnabledSetting().set(this._stateBefore.emulation.toolbarControlsEnabled); |
| 372 | SDK.multitargetNetworkManager.setNetworkConditions(this._stateBefore.network.conditions); |
| 373 | delete this._stateBefore; |
| 374 | } |
| 375 | |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 376 | Emulation.InspectedPagePlaceholder.instance().update(true); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 377 | |
Paul Lewis | 4ae5f4f | 2020-01-23 10:19:33 +0000 | [diff] [blame^] | 378 | const resourceTreeModel = self.SDK.targetManager.mainTarget().model(SDK.ResourceTreeModel); |
Patrick Hulce | a087f62 | 2018-05-18 00:37:53 +0000 | [diff] [blame] | 379 | // reload to reset the page state |
| 380 | const inspectedURL = await this._controller.getInspectedURL(); |
| 381 | await resourceTreeModel.navigate(inspectedURL); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 382 | } |
Paul Lewis | cf2ef22 | 2019-11-22 14:55:35 +0000 | [diff] [blame] | 383 | } |