blob: b321c3c89f20c05fc01754ddad3c13019b33bfe0 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371// 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 Lippe132b4ba2020-09-02 15:00:485// @ts-nocheck
6// TODO(crbug.com/1011811): Enable TypeScript compiler checks
7
Paul Lewisdaac1062020-03-05 14:37:108import * as Common from '../common/common.js';
9import * as Emulation from '../emulation/emulation.js'; // eslint-disable-line no-unused-vars
10import * as HostModule from '../host/host.js';
Tim van der Lippe5df64b22020-09-11 12:04:2411import * as Root from '../root/root.js';
Paul Lewisdaac1062020-03-05 14:37:1012import * as SDK from '../sdk/sdk.js';
13import * as UI from '../ui/ui.js';
Tim van der Lippec02a97c2020-02-14 14:39:2714
Connor Clark2bc3be22020-02-14 22:34:1915import {Events, LighthouseController} from './LighthouseController.js';
16import {ProtocolService} from './LighthouseProtocolService.js';
Tim van der Lippe1e10f852020-10-30 14:35:0117import * as ReportRenderer from './LighthouseReporterTypes.js'; // eslint-disable-line no-unused-vars
Connor Clark2bc3be22020-02-14 22:34:1918import {LighthouseReportRenderer, LighthouseReportUIFeatures} from './LighthouseReportRenderer.js';
19import {Item, ReportSelector} from './LighthouseReportSelector.js';
20import {StartView} from './LighthouseStartView.js';
21import {StatusView} from './LighthouseStatusView.js';
Paul Lewis51474192020-01-09 16:02:2222
Blink Reformat4c46d092018-04-07 15:32:3723/**
Blink Reformat4c46d092018-04-07 15:32:3724 * @unrestricted
25 */
Paul Lewisdaac1062020-03-05 14:37:1026export class LighthousePanel extends UI.Panel.Panel {
Blink Reformat4c46d092018-04-07 15:32:3727 constructor() {
Connor Clark2bc3be22020-02-14 22:34:1928 super('lighthouse');
Jack Franklin71519f82020-11-03 12:08:5929 this.registerRequiredCSS('third_party/lighthouse/report-assets/report.css', {enableLegacyPatching: true});
30 this.registerRequiredCSS('lighthouse/lighthousePanel.css', {enableLegacyPatching: true});
Blink Reformat4c46d092018-04-07 15:32:3731
Paul Lewis51474192020-01-09 16:02:2232 this._protocolService = new ProtocolService();
Connor Clark2bc3be22020-02-14 22:34:1933 this._controller = new LighthouseController(this._protocolService);
Paul Lewis51474192020-01-09 16:02:2234 this._startView = new StartView(this._controller);
35 this._statusView = new StatusView(this._controller);
Blink Reformat4c46d092018-04-07 15:32:3736
Adam Raine3226e932020-10-08 14:42:2137 this._warningText = null;
Patrick Hulcea087f622018-05-18 00:37:5338 this._unauditableExplanation = null;
39 this._cachedRenderedReports = new Map();
Blink Reformat4c46d092018-04-07 15:32:3740
Paul Lewisdaac1062020-03-05 14:37:1041 this._dropTarget = new UI.DropTarget.DropTarget(
Connor Clark2bc3be22020-02-14 22:34:1942 this.contentElement, [UI.DropTarget.Type.File], Common.UIString.UIString('Drop Lighthouse JSON here'),
Blink Reformat4c46d092018-04-07 15:32:3743 this._handleDrop.bind(this));
44
Paul Lewis51474192020-01-09 16:02:2245 this._controller.addEventListener(Events.PageAuditabilityChanged, this._refreshStartAuditUI.bind(this));
Adam Raine3226e932020-10-08 14:42:2146 this._controller.addEventListener(Events.PageWarningsChanged, this._refreshWarningsUI.bind(this));
Paul Lewis51474192020-01-09 16:02:2247 this._controller.addEventListener(Events.AuditProgressChanged, this._refreshStatusUI.bind(this));
Tim van der Lippe37a35ff2020-03-03 13:49:0248 this._controller.addEventListener(Events.RequestLighthouseStart, event => {
49 this._startLighthouse(event);
50 });
51 this._controller.addEventListener(Events.RequestLighthouseCancel, event => {
52 this._cancelLighthouse();
53 });
Blink Reformat4c46d092018-04-07 15:32:3754
Patrick Hulcea087f622018-05-18 00:37:5355 this._renderToolbar();
Connor Clark2bc3be22020-02-14 22:34:1956 this._auditResultsElement = this.contentElement.createChild('div', 'lighthouse-results-container');
Patrick Hulcea087f622018-05-18 00:37:5357 this._renderStartView();
58
59 this._controller.recomputePageAuditability();
Blink Reformat4c46d092018-04-07 15:32:3760 }
61
Adam Raine3226e932020-10-08 14:42:2162 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 Reformat4c46d092018-04-07 15:32:3779 /**
Tim van der Lippec02a97c2020-02-14 14:39:2780 * @param {!Common.EventTarget.EventTargetEvent} evt
Blink Reformat4c46d092018-04-07 15:32:3781 */
Patrick Hulcea087f622018-05-18 00:37:5382 _refreshStartAuditUI(evt) {
Connor Clarkca8905e2019-08-23 18:35:1083 // PageAuditabilityChanged fires multiple times during an audit, which we want to ignore.
Tim van der Lippe1d6e57a2019-09-30 11:55:3484 if (this._isLHAttached) {
Connor Clarkca8905e2019-08-23 18:35:1085 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:3486 }
Connor Clarkca8905e2019-08-23 18:35:1087
Patrick Hulcea087f622018-05-18 00:37:5388 this._unauditableExplanation = evt.data.helpText;
89 this._startView.setUnauditableExplanation(evt.data.helpText);
90 this._startView.setStartButtonEnabled(!evt.data.helpText);
Blink Reformat4c46d092018-04-07 15:32:3791 }
92
93 /**
Tim van der Lippec02a97c2020-02-14 14:39:2794 * @param {!Common.EventTarget.EventTargetEvent} evt
Blink Reformat4c46d092018-04-07 15:32:3795 */
Patrick Hulcea087f622018-05-18 00:37:5396 _refreshStatusUI(evt) {
97 this._statusView.updateStatus(evt.data.message);
Blink Reformat4c46d092018-04-07 15:32:3798 }
99
100 _refreshToolbarUI() {
Blink Reformat4c46d092018-04-07 15:32:37101 this._clearButton.setEnabled(this._reportSelector.hasItems());
102 }
103
104 _clearAll() {
105 this._reportSelector.clearAll();
Patrick Hulcea087f622018-05-18 00:37:53106 this._renderStartView();
Blink Reformat4c46d092018-04-07 15:32:37107 this._refreshToolbarUI();
108 }
109
Blink Reformat4c46d092018-04-07 15:32:37110 _renderToolbar() {
Connor Clark2bc3be22020-02-14 22:34:19111 const lighthouseToolbarContainer = this.element.createChild('div', 'lighthouse-toolbar-container');
Connor Clarke66080e2019-11-07 00:35:51112
Paul Lewisdaac1062020-03-05 14:37:10113 const toolbar = new UI.Toolbar.Toolbar('', lighthouseToolbarContainer);
Blink Reformat4c46d092018-04-07 15:32:37114
Paul Lewisdaac1062020-03-05 14:37:10115 this._newButton = new UI.Toolbar.ToolbarButton(Common.UIString.UIString('Perform an audit…'), 'largeicon-add');
Blink Reformat4c46d092018-04-07 15:32:37116 toolbar.appendToolbarItem(this._newButton);
Paul Lewisdaac1062020-03-05 14:37:10117 this._newButton.addEventListener(UI.Toolbar.ToolbarButton.Events.Click, this._renderStartView.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37118
Blink Reformat4c46d092018-04-07 15:32:37119 toolbar.appendSeparator();
120
Paul Lewis51474192020-01-09 16:02:22121 this._reportSelector = new ReportSelector(() => this._renderStartView());
Blink Reformat4c46d092018-04-07 15:32:37122 toolbar.appendToolbarItem(this._reportSelector.comboBox());
123
Paul Lewisdaac1062020-03-05 14:37:10124 this._clearButton = new UI.Toolbar.ToolbarButton(Common.UIString.UIString('Clear all'), 'largeicon-clear');
Blink Reformat4c46d092018-04-07 15:32:37125 toolbar.appendToolbarItem(this._clearButton);
Paul Lewisdaac1062020-03-05 14:37:10126 this._clearButton.addEventListener(UI.Toolbar.ToolbarButton.Events.Click, this._clearAll.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37127
Paul Lewisdaac1062020-03-05 14:37:10128 this._settingsPane = new UI.Widget.HBox();
Connor Clarke66080e2019-11-07 00:35:51129 this._settingsPane.show(this.contentElement);
Connor Clark2bc3be22020-02-14 22:34:19130 this._settingsPane.element.classList.add('lighthouse-settings-pane');
Connor Clarke66080e2019-11-07 00:35:51131 this._settingsPane.element.appendChild(this._startView.settingsToolbar().element);
Paul Lewis2d7d65c2020-03-16 17:26:30132 this._showSettingsPaneSetting =
133 Common.Settings.Settings.instance().createSetting('lighthouseShowSettingsToolbar', false);
Connor Clarke66080e2019-11-07 00:35:51134
Paul Lewisdaac1062020-03-05 14:37:10135 this._rightToolbar = new UI.Toolbar.Toolbar('', lighthouseToolbarContainer);
Connor Clarke66080e2019-11-07 00:35:51136 this._rightToolbar.appendSeparator();
Paul Lewisdaac1062020-03-05 14:37:10137 this._rightToolbar.appendToolbarItem(new UI.Toolbar.ToolbarSettingToggle(
138 this._showSettingsPaneSetting, 'largeicon-settings-gear', ls`Lighthouse settings`));
Connor Clarke66080e2019-11-07 00:35:51139 this._showSettingsPaneSetting.addChangeListener(this._updateSettingsPaneVisibility.bind(this));
140 this._updateSettingsPaneVisibility();
141
Patrick Hulcea087f622018-05-18 00:37:53142 this._refreshToolbarUI();
143 }
Blink Reformat4c46d092018-04-07 15:32:37144
Connor Clarke66080e2019-11-07 00:35:51145 _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 Hulcea087f622018-05-18 00:37:53158 _renderStartView() {
159 this._auditResultsElement.removeChildren();
160 this._statusView.hide();
161
Connor Clark2bc3be22020-02-14 22:34:19162 this._reportSelector.selectNewReport();
Patrick Hulcea087f622018-05-18 00:37:53163 this.contentElement.classList.toggle('in-progress', false);
164
165 this._startView.show(this.contentElement);
Connor Clarke66080e2019-11-07 00:35:51166 this._toggleSettingsDisplay(true);
Patrick Hulcea087f622018-05-18 00:37:53167 this._startView.setUnauditableExplanation(this._unauditableExplanation);
168 this._startView.setStartButtonEnabled(!this._unauditableExplanation);
Tim van der Lippe1d6e57a2019-09-30 11:55:34169 if (!this._unauditableExplanation) {
Patrick Hulce8d387f12018-05-29 18:54:54170 this._startView.focusStartButton();
Tim van der Lippe1d6e57a2019-09-30 11:55:34171 }
Adam Raine3226e932020-10-08 14:42:21172 this._startView.setWarningText(this._warningText);
Patrick Hulce8d387f12018-05-29 18:54:54173
Patrick Hulce05c18ce2018-05-24 00:34:56174 this._newButton.setEnabled(false);
Patrick Hulcea087f622018-05-18 00:37:53175 this._refreshToolbarUI();
Patrick Hulce05c18ce2018-05-24 00:34:56176 this.setDefaultFocusedChild(this._startView);
Patrick Hulcea087f622018-05-18 00:37:53177 }
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 Clark99508362019-08-20 19:52:23188 _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 Hulcea087f622018-05-18 00:37:53199 /**
200 * @param {!ReportRenderer.ReportJSON} lighthouseResult
Paul Irish8f1e33d2018-05-31 02:29:50201 * @param {!ReportRenderer.RunnerResultArtifacts=} artifacts
Patrick Hulcea087f622018-05-18 00:37:53202 */
Paul Irish8f1e33d2018-05-31 02:29:50203 _renderReport(lighthouseResult, artifacts) {
Connor Clarke66080e2019-11-07 00:35:51204 this._toggleSettingsDisplay(false);
Patrick Hulcea087f622018-05-18 00:37:53205 this.contentElement.classList.toggle('in-progress', false);
206 this._startView.hideWidget();
207 this._statusView.hide();
208 this._auditResultsElement.removeChildren();
Patrick Hulce05c18ce2018-05-24 00:34:56209 this._newButton.setEnabled(true);
Patrick Hulcea087f622018-05-18 00:37:53210 this._refreshToolbarUI();
211
212 const cachedRenderedReport = this._cachedRenderedReports.get(lighthouseResult);
213 if (cachedRenderedReport) {
214 this._auditResultsElement.appendChild(cachedRenderedReport);
215 return;
Blink Reformat4c46d092018-04-07 15:32:37216 }
217
Patrick Hulcea087f622018-05-18 00:37:53218 const reportContainer = this._auditResultsElement.createChild('div', 'lh-vars lh-root lh-devtools');
Blink Reformat4c46d092018-04-07 15:32:37219
Patrick Hulcea087f622018-05-18 00:37:53220 const dom = new DOM(/** @type {!Document} */ (this._auditResultsElement.ownerDocument));
Connor Clark2bc3be22020-02-14 22:34:19221 const renderer = new LighthouseReportRenderer(dom);
Patrick Hulcea087f622018-05-18 00:37:53222
Tim van der Lippe5df64b22020-09-11 12:04:24223 const templatesHTML = Root.Runtime.cachedResources.get('third_party/lighthouse/report-assets/templates.html');
Patrick Hulcea087f622018-05-18 00:37:53224 const templatesDOM = new DOMParser().parseFromString(templatesHTML, 'text/html');
Tim van der Lippe1d6e57a2019-09-30 11:55:34225 if (!templatesDOM) {
Blink Reformat4c46d092018-04-07 15:32:37226 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34227 }
Blink Reformat4c46d092018-04-07 15:32:37228
Patrick Hulcea087f622018-05-18 00:37:53229 renderer.setTemplateContext(templatesDOM);
Paul Irish8f1e33d2018-05-31 02:29:50230 const el = renderer.renderReport(lighthouseResult, reportContainer);
Connor Clark2bc3be22020-02-14 22:34:19231 LighthouseReportRenderer.addViewTraceButton(el, artifacts);
[email protected]c5214af2019-06-25 20:31:21232 // 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 Clark2bc3be22020-02-14 22:34:19236 LighthouseReportRenderer.linkifyNodeDetails(el);
237 LighthouseReportRenderer.linkifySourceLocationDetails(el);
[email protected]c5214af2019-06-25 20:31:21238 });
Connor Clark2bc3be22020-02-14 22:34:19239 LighthouseReportRenderer.handleDarkMode(el);
[email protected]f2f8c092019-05-30 22:01:56240
Connor Clark2bc3be22020-02-14 22:34:19241 const features = new LighthouseReportUIFeatures(dom);
Connor Clark99508362019-08-20 19:52:23242 features.setBeforePrint(this._beforePrint.bind(this));
243 features.setAfterPrint(this._afterPrint.bind(this));
[email protected]f2f8c092019-05-30 22:01:56244 features.setTemplateContext(templatesDOM);
245 features.initFeatures(lighthouseResult);
Blink Reformat4c46d092018-04-07 15:32:37246
Patrick Hulcea087f622018-05-18 00:37:53247 this._cachedRenderedReports.set(lighthouseResult, reportContainer);
Blink Reformat4c46d092018-04-07 15:32:37248 }
249
[email protected]c5214af2019-06-25 20:31:21250 _waitForMainTargetLoad() {
Paul Lewisdaac1062020-03-05 14:37:10251 const mainTarget = SDK.SDKModel.TargetManager.instance().mainTarget();
252 const resourceTreeModel = mainTarget.model(SDK.ResourceTreeModel.ResourceTreeModel);
[email protected]c5214af2019-06-25 20:31:21253 return resourceTreeModel.once(SDK.ResourceTreeModel.Events.Load);
254 }
255
Blink Reformat4c46d092018-04-07 15:32:37256 /**
257 * @param {!ReportRenderer.ReportJSON} lighthouseResult
Paul Irish8f1e33d2018-05-31 02:29:50258 * @param {!ReportRenderer.RunnerResultArtifacts=} artifacts
Blink Reformat4c46d092018-04-07 15:32:37259 */
Paul Irish8f1e33d2018-05-31 02:29:50260 _buildReportUI(lighthouseResult, artifacts) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34261 if (lighthouseResult === null) {
Blink Reformat4c46d092018-04-07 15:32:37262 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34263 }
Blink Reformat4c46d092018-04-07 15:32:37264
Paul Lewis51474192020-01-09 16:02:22265 const optionElement = new Item(
Paul Irish8f1e33d2018-05-31 02:29:50266 lighthouseResult, () => this._renderReport(lighthouseResult, artifacts), this._renderStartView.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37267 this._reportSelector.prepend(optionElement);
Blink Reformat4c46d092018-04-07 15:32:37268 this._refreshToolbarUI();
Patrick Hulcea087f622018-05-18 00:37:53269 this._renderReport(lighthouseResult);
Blink Reformat4c46d092018-04-07 15:32:37270 }
271
272 /**
273 * @param {!DataTransfer} dataTransfer
274 */
275 _handleDrop(dataTransfer) {
276 const items = dataTransfer.items;
Tim van der Lippe1d6e57a2019-09-30 11:55:34277 if (!items.length) {
Blink Reformat4c46d092018-04-07 15:32:37278 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34279 }
Blink Reformat4c46d092018-04-07 15:32:37280 const item = items[0];
281 if (item.kind === 'file') {
282 const entry = items[0].webkitGetAsEntry();
Tim van der Lippe1d6e57a2019-09-30 11:55:34283 if (!entry.isFile) {
Blink Reformat4c46d092018-04-07 15:32:37284 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34285 }
Blink Reformat4c46d092018-04-07 15:32:37286 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 Hulcea087f622018-05-18 00:37:53295 * @param {string} report
Blink Reformat4c46d092018-04-07 15:32:37296 */
Patrick Hulcea087f622018-05-18 00:37:53297 _loadedFromFile(report) {
298 const data = JSON.parse(report);
Tim van der Lippe1d6e57a2019-09-30 11:55:34299 if (!data['lighthouseVersion']) {
Blink Reformat4c46d092018-04-07 15:32:37300 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34301 }
Blink Reformat4c46d092018-04-07 15:32:37302 this._buildReportUI(/** @type {!ReportRenderer.ReportJSON} */ (data));
303 }
Blink Reformat4c46d092018-04-07 15:32:37304
Brandon Goddard04d5ba92019-12-19 16:31:55305 /**
Tim van der Lippec02a97c2020-02-14 14:39:27306 * @param {!Common.EventTarget.EventTargetEvent} event
Brandon Goddard04d5ba92019-12-19 16:31:55307 */
Connor Clark2bc3be22020-02-14 22:34:19308 async _startLighthouse(event) {
Paul Lewisdaac1062020-03-05 14:37:10309 HostModule.userMetrics.actionTaken(Host.UserMetrics.Action.LighthouseStarted);
Blink Reformat4c46d092018-04-07 15:32:37310
Patrick Hulcea087f622018-05-18 00:37:53311 try {
312 const inspectedURL = await this._controller.getInspectedURL({force: true});
313 const categoryIDs = this._controller.getCategoryIDs();
314 const flags = this._controller.getFlags();
Blink Reformat4c46d092018-04-07 15:32:37315
Patrick Hulcea087f622018-05-18 00:37:53316 await this._setupEmulationAndProtocolConnection();
Blink Reformat4c46d092018-04-07 15:32:37317
Patrick Hulcea087f622018-05-18 00:37:53318 this._renderStatusView(inspectedURL);
Blink Reformat4c46d092018-04-07 15:32:37319
Paul Irish8f1e33d2018-05-31 02:29:50320 const lighthouseResponse = await this._protocolService.startLighthouse(inspectedURL, categoryIDs, flags);
Blink Reformat4c46d092018-04-07 15:32:37321
Paul Irish8f1e33d2018-05-31 02:29:50322 if (lighthouseResponse && lighthouseResponse.fatal) {
323 const error = new Error(lighthouseResponse.message);
324 error.stack = lighthouseResponse.stack;
Patrick Hulcea087f622018-05-18 00:37:53325 throw error;
326 }
Blink Reformat4c46d092018-04-07 15:32:37327
Tim van der Lippe1d6e57a2019-09-30 11:55:34328 if (!lighthouseResponse) {
Patrick Hulcea087f622018-05-18 00:37:53329 throw new Error('Auditing failed to produce a result');
Tim van der Lippe1d6e57a2019-09-30 11:55:34330 }
Blink Reformat4c46d092018-04-07 15:32:37331
Paul Lewisdaac1062020-03-05 14:37:10332 HostModule.userMetrics.actionTaken(Host.UserMetrics.Action.LighthouseFinished);
Blink Reformat4c46d092018-04-07 15:32:37333
Patrick Hulcea087f622018-05-18 00:37:53334 await this._resetEmulationAndProtocolConnection();
Paul Irish8f1e33d2018-05-31 02:29:50335 this._buildReportUI(lighthouseResponse.lhr, lighthouseResponse.artifacts);
Brandon Goddard04d5ba92019-12-19 16:31:55336 // Give focus to the new audit button when completed
337 this._newButton.element.focus();
Patrick Hulcea087f622018-05-18 00:37:53338 } catch (err) {
Paul Irishdca01d02019-03-25 20:17:56339 await this._resetEmulationAndProtocolConnection();
Tim van der Lippe1d6e57a2019-09-30 11:55:34340 if (err instanceof Error) {
Patrick Hulcea087f622018-05-18 00:37:53341 this._statusView.renderBugReport(err);
Tim van der Lippe1d6e57a2019-09-30 11:55:34342 }
Blink Reformat4c46d092018-04-07 15:32:37343 }
344 }
Blink Reformat4c46d092018-04-07 15:32:37345
Connor Clark2bc3be22020-02-14 22:34:19346 async _cancelLighthouse() {
Patrick Hulcea087f622018-05-18 00:37:53347 this._statusView.updateStatus(ls`Cancelling`);
348 await this._resetEmulationAndProtocolConnection();
349 this._renderStartView();
Blink Reformat4c46d092018-04-07 15:32:37350 }
351
Paul Irishd8495012019-07-16 23:51:47352 /**
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 Clarkca8905e2019-08-23 18:35:10356 *
Connor Clark3ee5ac72019-11-07 23:11:58357 * We also set flags.internalDisableDeviceScreenEmulation = true to let LH only apply UA emulation
Paul Irishd8495012019-07-16 23:51:47358 */
Patrick Hulcea087f622018-05-18 00:37:53359 async _setupEmulationAndProtocolConnection() {
360 const flags = this._controller.getFlags();
Blink Reformat4c46d092018-04-07 15:32:37361
Jack Franklin6f3ee6c2020-10-22 12:54:10362 const emulationModel = Emulation.DeviceModeModel.DeviceModeModel.instance();
Connor Clarkca8905e2019-08-23 18:35:10363 this._stateBefore = {
364 emulation: {
365 enabled: emulationModel.enabledSetting().get(),
366 outlineEnabled: emulationModel.deviceOutlineSetting().get(),
367 toolbarControlsEnabled: emulationModel.toolbarControlsEnabledSetting().get()
368 },
Tim van der Lippecd0bb372020-05-01 13:53:21369 network: {conditions: SDK.NetworkManager.MultitargetNetworkManager.instance().networkConditions()}
Connor Clarkca8905e2019-08-23 18:35:10370 };
Patrick Hulcea087f622018-05-18 00:37:53371
Connor Clarkca8905e2019-08-23 18:35:10372 emulationModel.toolbarControlsEnabledSetting().set(false);
Connor Clark3f700342019-07-25 02:10:41373 if (flags.emulatedFormFactor === 'desktop') {
Patrick Hulcea087f622018-05-18 00:37:53374 emulationModel.enabledSetting().set(false);
Patrick Hulcea087f622018-05-18 00:37:53375 emulationModel.emulate(Emulation.DeviceModeModel.Type.None, null, null);
Connor Clark3f700342019-07-25 02:10:41376 } else if (flags.emulatedFormFactor === 'mobile') {
Patrick Hulcea087f622018-05-18 00:37:53377 emulationModel.enabledSetting().set(true);
378 emulationModel.deviceOutlineSetting().set(true);
379
Paul Lewisdaac1062020-03-05 14:37:10380 for (const device of Emulation.EmulatedDevices.EmulatedDevicesList.instance().standard()) {
Paul Irishac18cd02020-05-13 00:57:44381 if (device.title === 'Moto G4') {
Patrick Hulcea087f622018-05-18 00:37:53382 emulationModel.emulate(Emulation.DeviceModeModel.Type.Device, device, device.modes[0], 1);
Tim van der Lippe1d6e57a2019-09-30 11:55:34383 }
Patrick Hulcea087f622018-05-18 00:37:53384 }
Blink Reformat4c46d092018-04-07 15:32:37385 }
386
Patrick Hulcea087f622018-05-18 00:37:53387 await this._protocolService.attach();
388 this._isLHAttached = true;
389 }
Blink Reformat4c46d092018-04-07 15:32:37390
Patrick Hulcea087f622018-05-18 00:37:53391 async _resetEmulationAndProtocolConnection() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34392 if (!this._isLHAttached) {
Blink Reformat4c46d092018-04-07 15:32:37393 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34394 }
Blink Reformat4c46d092018-04-07 15:32:37395
Patrick Hulcea087f622018-05-18 00:37:53396 this._isLHAttached = false;
397 await this._protocolService.detach();
Blink Reformat4c46d092018-04-07 15:32:37398
Connor Clarkca8905e2019-08-23 18:35:10399 if (this._stateBefore) {
Jack Franklin6f3ee6c2020-10-22 12:54:10400 const emulationModel = Emulation.DeviceModeModel.DeviceModeModel.instance();
Connor Clarkca8905e2019-08-23 18:35:10401 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 Lippecd0bb372020-05-01 13:53:21404 SDK.NetworkManager.MultitargetNetworkManager.instance().setNetworkConditions(
405 this._stateBefore.network.conditions);
Connor Clarkca8905e2019-08-23 18:35:10406 delete this._stateBefore;
407 }
408
Jack Franklindb9cb272020-10-15 13:11:26409 Emulation.InspectedPagePlaceholder.InspectedPagePlaceholder.instance().update(true);
Blink Reformat4c46d092018-04-07 15:32:37410
Paul Lewisdaac1062020-03-05 14:37:10411 const resourceTreeModel =
412 SDK.SDKModel.TargetManager.instance().mainTarget().model(SDK.ResourceTreeModel.ResourceTreeModel);
Patrick Hulcea087f622018-05-18 00:37:53413 // reload to reset the page state
414 const inspectedURL = await this._controller.getInspectedURL();
415 await resourceTreeModel.navigate(inspectedURL);
Blink Reformat4c46d092018-04-07 15:32:37416 }
Paul Lewiscf2ef222019-11-22 14:55:35417}