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