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