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