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