blob: 530047dbad9c077672f2c9a25579967fae3cfff4 [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';
17import {LighthouseReportRenderer, LighthouseReportUIFeatures} from './LighthouseReportRenderer.js';
18import {Item, ReportSelector} from './LighthouseReportSelector.js';
19import {StartView} from './LighthouseStartView.js';
20import {StatusView} from './LighthouseStatusView.js';
Paul Lewis51474192020-01-09 16:02:2221
Blink Reformat4c46d092018-04-07 15:32:3722/**
Blink Reformat4c46d092018-04-07 15:32:3723 * @unrestricted
24 */
Paul Lewisdaac1062020-03-05 14:37:1025export class LighthousePanel extends UI.Panel.Panel {
Blink Reformat4c46d092018-04-07 15:32:3726 constructor() {
Connor Clark2bc3be22020-02-14 22:34:1927 super('lighthouse');
Connor Clark933525c2020-02-27 23:27:3928 this.registerRequiredCSS('third_party/lighthouse/report-assets/report.css');
Connor Clark2bc3be22020-02-14 22:34:1929 this.registerRequiredCSS('lighthouse/lighthousePanel.css');
Blink Reformat4c46d092018-04-07 15:32:3730
Paul Lewis51474192020-01-09 16:02:2231 this._protocolService = new ProtocolService();
Connor Clark2bc3be22020-02-14 22:34:1932 this._controller = new LighthouseController(this._protocolService);
Paul Lewis51474192020-01-09 16:02:2233 this._startView = new StartView(this._controller);
34 this._statusView = new StatusView(this._controller);
Blink Reformat4c46d092018-04-07 15:32:3735
Adam Raine3226e932020-10-08 14:42:2136 this._warningText = null;
Patrick Hulcea087f622018-05-18 00:37:5337 this._unauditableExplanation = null;
38 this._cachedRenderedReports = new Map();
Blink Reformat4c46d092018-04-07 15:32:3739
Paul Lewisdaac1062020-03-05 14:37:1040 this._dropTarget = new UI.DropTarget.DropTarget(
Connor Clark2bc3be22020-02-14 22:34:1941 this.contentElement, [UI.DropTarget.Type.File], Common.UIString.UIString('Drop Lighthouse JSON here'),
Blink Reformat4c46d092018-04-07 15:32:3742 this._handleDrop.bind(this));
43
Paul Lewis51474192020-01-09 16:02:2244 this._controller.addEventListener(Events.PageAuditabilityChanged, this._refreshStartAuditUI.bind(this));
Adam Raine3226e932020-10-08 14:42:2145 this._controller.addEventListener(Events.PageWarningsChanged, this._refreshWarningsUI.bind(this));
Paul Lewis51474192020-01-09 16:02:2246 this._controller.addEventListener(Events.AuditProgressChanged, this._refreshStatusUI.bind(this));
Tim van der Lippe37a35ff2020-03-03 13:49:0247 this._controller.addEventListener(Events.RequestLighthouseStart, event => {
48 this._startLighthouse(event);
49 });
50 this._controller.addEventListener(Events.RequestLighthouseCancel, event => {
51 this._cancelLighthouse();
52 });
Blink Reformat4c46d092018-04-07 15:32:3753
Patrick Hulcea087f622018-05-18 00:37:5354 this._renderToolbar();
Connor Clark2bc3be22020-02-14 22:34:1955 this._auditResultsElement = this.contentElement.createChild('div', 'lighthouse-results-container');
Patrick Hulcea087f622018-05-18 00:37:5356 this._renderStartView();
57
58 this._controller.recomputePageAuditability();
Blink Reformat4c46d092018-04-07 15:32:3759 }
60
Adam Raine3226e932020-10-08 14:42:2161 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 Reformat4c46d092018-04-07 15:32:3778 /**
Tim van der Lippec02a97c2020-02-14 14:39:2779 * @param {!Common.EventTarget.EventTargetEvent} evt
Blink Reformat4c46d092018-04-07 15:32:3780 */
Patrick Hulcea087f622018-05-18 00:37:5381 _refreshStartAuditUI(evt) {
Connor Clarkca8905e2019-08-23 18:35:1082 // PageAuditabilityChanged fires multiple times during an audit, which we want to ignore.
Tim van der Lippe1d6e57a2019-09-30 11:55:3483 if (this._isLHAttached) {
Connor Clarkca8905e2019-08-23 18:35:1084 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:3485 }
Connor Clarkca8905e2019-08-23 18:35:1086
Patrick Hulcea087f622018-05-18 00:37:5387 this._unauditableExplanation = evt.data.helpText;
88 this._startView.setUnauditableExplanation(evt.data.helpText);
89 this._startView.setStartButtonEnabled(!evt.data.helpText);
Blink Reformat4c46d092018-04-07 15:32:3790 }
91
92 /**
Tim van der Lippec02a97c2020-02-14 14:39:2793 * @param {!Common.EventTarget.EventTargetEvent} evt
Blink Reformat4c46d092018-04-07 15:32:3794 */
Patrick Hulcea087f622018-05-18 00:37:5395 _refreshStatusUI(evt) {
96 this._statusView.updateStatus(evt.data.message);
Blink Reformat4c46d092018-04-07 15:32:3797 }
98
99 _refreshToolbarUI() {
Blink Reformat4c46d092018-04-07 15:32:37100 this._clearButton.setEnabled(this._reportSelector.hasItems());
101 }
102
103 _clearAll() {
104 this._reportSelector.clearAll();
Patrick Hulcea087f622018-05-18 00:37:53105 this._renderStartView();
Blink Reformat4c46d092018-04-07 15:32:37106 this._refreshToolbarUI();
107 }
108
Blink Reformat4c46d092018-04-07 15:32:37109 _renderToolbar() {
Connor Clark2bc3be22020-02-14 22:34:19110 const lighthouseToolbarContainer = this.element.createChild('div', 'lighthouse-toolbar-container');
Connor Clarke66080e2019-11-07 00:35:51111
Paul Lewisdaac1062020-03-05 14:37:10112 const toolbar = new UI.Toolbar.Toolbar('', lighthouseToolbarContainer);
Blink Reformat4c46d092018-04-07 15:32:37113
Paul Lewisdaac1062020-03-05 14:37:10114 this._newButton = new UI.Toolbar.ToolbarButton(Common.UIString.UIString('Perform an audit…'), 'largeicon-add');
Blink Reformat4c46d092018-04-07 15:32:37115 toolbar.appendToolbarItem(this._newButton);
Paul Lewisdaac1062020-03-05 14:37:10116 this._newButton.addEventListener(UI.Toolbar.ToolbarButton.Events.Click, this._renderStartView.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37117
Blink Reformat4c46d092018-04-07 15:32:37118 toolbar.appendSeparator();
119
Paul Lewis51474192020-01-09 16:02:22120 this._reportSelector = new ReportSelector(() => this._renderStartView());
Blink Reformat4c46d092018-04-07 15:32:37121 toolbar.appendToolbarItem(this._reportSelector.comboBox());
122
Paul Lewisdaac1062020-03-05 14:37:10123 this._clearButton = new UI.Toolbar.ToolbarButton(Common.UIString.UIString('Clear all'), 'largeicon-clear');
Blink Reformat4c46d092018-04-07 15:32:37124 toolbar.appendToolbarItem(this._clearButton);
Paul Lewisdaac1062020-03-05 14:37:10125 this._clearButton.addEventListener(UI.Toolbar.ToolbarButton.Events.Click, this._clearAll.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37126
Paul Lewisdaac1062020-03-05 14:37:10127 this._settingsPane = new UI.Widget.HBox();
Connor Clarke66080e2019-11-07 00:35:51128 this._settingsPane.show(this.contentElement);
Connor Clark2bc3be22020-02-14 22:34:19129 this._settingsPane.element.classList.add('lighthouse-settings-pane');
Connor Clarke66080e2019-11-07 00:35:51130 this._settingsPane.element.appendChild(this._startView.settingsToolbar().element);
Paul Lewis2d7d65c2020-03-16 17:26:30131 this._showSettingsPaneSetting =
132 Common.Settings.Settings.instance().createSetting('lighthouseShowSettingsToolbar', false);
Connor Clarke66080e2019-11-07 00:35:51133
Paul Lewisdaac1062020-03-05 14:37:10134 this._rightToolbar = new UI.Toolbar.Toolbar('', lighthouseToolbarContainer);
Connor Clarke66080e2019-11-07 00:35:51135 this._rightToolbar.appendSeparator();
Paul Lewisdaac1062020-03-05 14:37:10136 this._rightToolbar.appendToolbarItem(new UI.Toolbar.ToolbarSettingToggle(
137 this._showSettingsPaneSetting, 'largeicon-settings-gear', ls`Lighthouse settings`));
Connor Clarke66080e2019-11-07 00:35:51138 this._showSettingsPaneSetting.addChangeListener(this._updateSettingsPaneVisibility.bind(this));
139 this._updateSettingsPaneVisibility();
140
Patrick Hulcea087f622018-05-18 00:37:53141 this._refreshToolbarUI();
142 }
Blink Reformat4c46d092018-04-07 15:32:37143
Connor Clarke66080e2019-11-07 00:35:51144 _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 Hulcea087f622018-05-18 00:37:53157 _renderStartView() {
158 this._auditResultsElement.removeChildren();
159 this._statusView.hide();
160
Connor Clark2bc3be22020-02-14 22:34:19161 this._reportSelector.selectNewReport();
Patrick Hulcea087f622018-05-18 00:37:53162 this.contentElement.classList.toggle('in-progress', false);
163
164 this._startView.show(this.contentElement);
Connor Clarke66080e2019-11-07 00:35:51165 this._toggleSettingsDisplay(true);
Patrick Hulcea087f622018-05-18 00:37:53166 this._startView.setUnauditableExplanation(this._unauditableExplanation);
167 this._startView.setStartButtonEnabled(!this._unauditableExplanation);
Tim van der Lippe1d6e57a2019-09-30 11:55:34168 if (!this._unauditableExplanation) {
Patrick Hulce8d387f12018-05-29 18:54:54169 this._startView.focusStartButton();
Tim van der Lippe1d6e57a2019-09-30 11:55:34170 }
Adam Raine3226e932020-10-08 14:42:21171 this._startView.setWarningText(this._warningText);
Patrick Hulce8d387f12018-05-29 18:54:54172
Patrick Hulce05c18ce2018-05-24 00:34:56173 this._newButton.setEnabled(false);
Patrick Hulcea087f622018-05-18 00:37:53174 this._refreshToolbarUI();
Patrick Hulce05c18ce2018-05-24 00:34:56175 this.setDefaultFocusedChild(this._startView);
Patrick Hulcea087f622018-05-18 00:37:53176 }
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 Clark99508362019-08-20 19:52:23187 _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 Hulcea087f622018-05-18 00:37:53198 /**
199 * @param {!ReportRenderer.ReportJSON} lighthouseResult
Paul Irish8f1e33d2018-05-31 02:29:50200 * @param {!ReportRenderer.RunnerResultArtifacts=} artifacts
Patrick Hulcea087f622018-05-18 00:37:53201 */
Paul Irish8f1e33d2018-05-31 02:29:50202 _renderReport(lighthouseResult, artifacts) {
Connor Clarke66080e2019-11-07 00:35:51203 this._toggleSettingsDisplay(false);
Patrick Hulcea087f622018-05-18 00:37:53204 this.contentElement.classList.toggle('in-progress', false);
205 this._startView.hideWidget();
206 this._statusView.hide();
207 this._auditResultsElement.removeChildren();
Patrick Hulce05c18ce2018-05-24 00:34:56208 this._newButton.setEnabled(true);
Patrick Hulcea087f622018-05-18 00:37:53209 this._refreshToolbarUI();
210
211 const cachedRenderedReport = this._cachedRenderedReports.get(lighthouseResult);
212 if (cachedRenderedReport) {
213 this._auditResultsElement.appendChild(cachedRenderedReport);
214 return;
Blink Reformat4c46d092018-04-07 15:32:37215 }
216
Patrick Hulcea087f622018-05-18 00:37:53217 const reportContainer = this._auditResultsElement.createChild('div', 'lh-vars lh-root lh-devtools');
Blink Reformat4c46d092018-04-07 15:32:37218
Patrick Hulcea087f622018-05-18 00:37:53219 const dom = new DOM(/** @type {!Document} */ (this._auditResultsElement.ownerDocument));
Connor Clark2bc3be22020-02-14 22:34:19220 const renderer = new LighthouseReportRenderer(dom);
Patrick Hulcea087f622018-05-18 00:37:53221
Tim van der Lippe5df64b22020-09-11 12:04:24222 const templatesHTML = Root.Runtime.cachedResources.get('third_party/lighthouse/report-assets/templates.html');
Patrick Hulcea087f622018-05-18 00:37:53223 const templatesDOM = new DOMParser().parseFromString(templatesHTML, 'text/html');
Tim van der Lippe1d6e57a2019-09-30 11:55:34224 if (!templatesDOM) {
Blink Reformat4c46d092018-04-07 15:32:37225 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34226 }
Blink Reformat4c46d092018-04-07 15:32:37227
Patrick Hulcea087f622018-05-18 00:37:53228 renderer.setTemplateContext(templatesDOM);
Paul Irish8f1e33d2018-05-31 02:29:50229 const el = renderer.renderReport(lighthouseResult, reportContainer);
Connor Clark2bc3be22020-02-14 22:34:19230 LighthouseReportRenderer.addViewTraceButton(el, artifacts);
[email protected]c5214af2019-06-25 20:31:21231 // 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 Clark2bc3be22020-02-14 22:34:19235 LighthouseReportRenderer.linkifyNodeDetails(el);
236 LighthouseReportRenderer.linkifySourceLocationDetails(el);
[email protected]c5214af2019-06-25 20:31:21237 });
Connor Clark2bc3be22020-02-14 22:34:19238 LighthouseReportRenderer.handleDarkMode(el);
[email protected]f2f8c092019-05-30 22:01:56239
Connor Clark2bc3be22020-02-14 22:34:19240 const features = new LighthouseReportUIFeatures(dom);
Connor Clark99508362019-08-20 19:52:23241 features.setBeforePrint(this._beforePrint.bind(this));
242 features.setAfterPrint(this._afterPrint.bind(this));
[email protected]f2f8c092019-05-30 22:01:56243 features.setTemplateContext(templatesDOM);
244 features.initFeatures(lighthouseResult);
Blink Reformat4c46d092018-04-07 15:32:37245
Patrick Hulcea087f622018-05-18 00:37:53246 this._cachedRenderedReports.set(lighthouseResult, reportContainer);
Blink Reformat4c46d092018-04-07 15:32:37247 }
248
[email protected]c5214af2019-06-25 20:31:21249 _waitForMainTargetLoad() {
Paul Lewisdaac1062020-03-05 14:37:10250 const mainTarget = SDK.SDKModel.TargetManager.instance().mainTarget();
251 const resourceTreeModel = mainTarget.model(SDK.ResourceTreeModel.ResourceTreeModel);
[email protected]c5214af2019-06-25 20:31:21252 return resourceTreeModel.once(SDK.ResourceTreeModel.Events.Load);
253 }
254
Blink Reformat4c46d092018-04-07 15:32:37255 /**
256 * @param {!ReportRenderer.ReportJSON} lighthouseResult
Paul Irish8f1e33d2018-05-31 02:29:50257 * @param {!ReportRenderer.RunnerResultArtifacts=} artifacts
Blink Reformat4c46d092018-04-07 15:32:37258 */
Paul Irish8f1e33d2018-05-31 02:29:50259 _buildReportUI(lighthouseResult, artifacts) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34260 if (lighthouseResult === null) {
Blink Reformat4c46d092018-04-07 15:32:37261 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34262 }
Blink Reformat4c46d092018-04-07 15:32:37263
Paul Lewis51474192020-01-09 16:02:22264 const optionElement = new Item(
Paul Irish8f1e33d2018-05-31 02:29:50265 lighthouseResult, () => this._renderReport(lighthouseResult, artifacts), this._renderStartView.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37266 this._reportSelector.prepend(optionElement);
Blink Reformat4c46d092018-04-07 15:32:37267 this._refreshToolbarUI();
Patrick Hulcea087f622018-05-18 00:37:53268 this._renderReport(lighthouseResult);
Blink Reformat4c46d092018-04-07 15:32:37269 }
270
271 /**
272 * @param {!DataTransfer} dataTransfer
273 */
274 _handleDrop(dataTransfer) {
275 const items = dataTransfer.items;
Tim van der Lippe1d6e57a2019-09-30 11:55:34276 if (!items.length) {
Blink Reformat4c46d092018-04-07 15:32:37277 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34278 }
Blink Reformat4c46d092018-04-07 15:32:37279 const item = items[0];
280 if (item.kind === 'file') {
281 const entry = items[0].webkitGetAsEntry();
Tim van der Lippe1d6e57a2019-09-30 11:55:34282 if (!entry.isFile) {
Blink Reformat4c46d092018-04-07 15:32:37283 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34284 }
Blink Reformat4c46d092018-04-07 15:32:37285 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 Hulcea087f622018-05-18 00:37:53294 * @param {string} report
Blink Reformat4c46d092018-04-07 15:32:37295 */
Patrick Hulcea087f622018-05-18 00:37:53296 _loadedFromFile(report) {
297 const data = JSON.parse(report);
Tim van der Lippe1d6e57a2019-09-30 11:55:34298 if (!data['lighthouseVersion']) {
Blink Reformat4c46d092018-04-07 15:32:37299 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34300 }
Blink Reformat4c46d092018-04-07 15:32:37301 this._buildReportUI(/** @type {!ReportRenderer.ReportJSON} */ (data));
302 }
Blink Reformat4c46d092018-04-07 15:32:37303
Brandon Goddard04d5ba92019-12-19 16:31:55304 /**
Tim van der Lippec02a97c2020-02-14 14:39:27305 * @param {!Common.EventTarget.EventTargetEvent} event
Brandon Goddard04d5ba92019-12-19 16:31:55306 */
Connor Clark2bc3be22020-02-14 22:34:19307 async _startLighthouse(event) {
Paul Lewisdaac1062020-03-05 14:37:10308 HostModule.userMetrics.actionTaken(Host.UserMetrics.Action.LighthouseStarted);
Blink Reformat4c46d092018-04-07 15:32:37309
Patrick Hulcea087f622018-05-18 00:37:53310 try {
311 const inspectedURL = await this._controller.getInspectedURL({force: true});
312 const categoryIDs = this._controller.getCategoryIDs();
313 const flags = this._controller.getFlags();
Blink Reformat4c46d092018-04-07 15:32:37314
Patrick Hulcea087f622018-05-18 00:37:53315 await this._setupEmulationAndProtocolConnection();
Blink Reformat4c46d092018-04-07 15:32:37316
Patrick Hulcea087f622018-05-18 00:37:53317 this._renderStatusView(inspectedURL);
Blink Reformat4c46d092018-04-07 15:32:37318
Paul Irish8f1e33d2018-05-31 02:29:50319 const lighthouseResponse = await this._protocolService.startLighthouse(inspectedURL, categoryIDs, flags);
Blink Reformat4c46d092018-04-07 15:32:37320
Paul Irish8f1e33d2018-05-31 02:29:50321 if (lighthouseResponse && lighthouseResponse.fatal) {
322 const error = new Error(lighthouseResponse.message);
323 error.stack = lighthouseResponse.stack;
Patrick Hulcea087f622018-05-18 00:37:53324 throw error;
325 }
Blink Reformat4c46d092018-04-07 15:32:37326
Tim van der Lippe1d6e57a2019-09-30 11:55:34327 if (!lighthouseResponse) {
Patrick Hulcea087f622018-05-18 00:37:53328 throw new Error('Auditing failed to produce a result');
Tim van der Lippe1d6e57a2019-09-30 11:55:34329 }
Blink Reformat4c46d092018-04-07 15:32:37330
Paul Lewisdaac1062020-03-05 14:37:10331 HostModule.userMetrics.actionTaken(Host.UserMetrics.Action.LighthouseFinished);
Blink Reformat4c46d092018-04-07 15:32:37332
Patrick Hulcea087f622018-05-18 00:37:53333 await this._resetEmulationAndProtocolConnection();
Paul Irish8f1e33d2018-05-31 02:29:50334 this._buildReportUI(lighthouseResponse.lhr, lighthouseResponse.artifacts);
Brandon Goddard04d5ba92019-12-19 16:31:55335 // Give focus to the new audit button when completed
336 this._newButton.element.focus();
Patrick Hulcea087f622018-05-18 00:37:53337 } catch (err) {
Paul Irishdca01d02019-03-25 20:17:56338 await this._resetEmulationAndProtocolConnection();
Tim van der Lippe1d6e57a2019-09-30 11:55:34339 if (err instanceof Error) {
Patrick Hulcea087f622018-05-18 00:37:53340 this._statusView.renderBugReport(err);
Tim van der Lippe1d6e57a2019-09-30 11:55:34341 }
Blink Reformat4c46d092018-04-07 15:32:37342 }
343 }
Blink Reformat4c46d092018-04-07 15:32:37344
Connor Clark2bc3be22020-02-14 22:34:19345 async _cancelLighthouse() {
Patrick Hulcea087f622018-05-18 00:37:53346 this._statusView.updateStatus(ls`Cancelling`);
347 await this._resetEmulationAndProtocolConnection();
348 this._renderStartView();
Blink Reformat4c46d092018-04-07 15:32:37349 }
350
Paul Irishd8495012019-07-16 23:51:47351 /**
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 Clarkca8905e2019-08-23 18:35:10355 *
Connor Clark3ee5ac72019-11-07 23:11:58356 * We also set flags.internalDisableDeviceScreenEmulation = true to let LH only apply UA emulation
Paul Irishd8495012019-07-16 23:51:47357 */
Patrick Hulcea087f622018-05-18 00:37:53358 async _setupEmulationAndProtocolConnection() {
359 const flags = this._controller.getFlags();
Blink Reformat4c46d092018-04-07 15:32:37360
Paul Lewisdaac1062020-03-05 14:37:10361 const emulationModel = self.singleton(Emulation.DeviceModeModel.DeviceModeModel);
Connor Clarkca8905e2019-08-23 18:35:10362 this._stateBefore = {
363 emulation: {
364 enabled: emulationModel.enabledSetting().get(),
365 outlineEnabled: emulationModel.deviceOutlineSetting().get(),
366 toolbarControlsEnabled: emulationModel.toolbarControlsEnabledSetting().get()
367 },
Tim van der Lippecd0bb372020-05-01 13:53:21368 network: {conditions: SDK.NetworkManager.MultitargetNetworkManager.instance().networkConditions()}
Connor Clarkca8905e2019-08-23 18:35:10369 };
Patrick Hulcea087f622018-05-18 00:37:53370
Connor Clarkca8905e2019-08-23 18:35:10371 emulationModel.toolbarControlsEnabledSetting().set(false);
Connor Clark3f700342019-07-25 02:10:41372 if (flags.emulatedFormFactor === 'desktop') {
Patrick Hulcea087f622018-05-18 00:37:53373 emulationModel.enabledSetting().set(false);
Patrick Hulcea087f622018-05-18 00:37:53374 emulationModel.emulate(Emulation.DeviceModeModel.Type.None, null, null);
Connor Clark3f700342019-07-25 02:10:41375 } else if (flags.emulatedFormFactor === 'mobile') {
Patrick Hulcea087f622018-05-18 00:37:53376 emulationModel.enabledSetting().set(true);
377 emulationModel.deviceOutlineSetting().set(true);
378
Paul Lewisdaac1062020-03-05 14:37:10379 for (const device of Emulation.EmulatedDevices.EmulatedDevicesList.instance().standard()) {
Paul Irishac18cd02020-05-13 00:57:44380 if (device.title === 'Moto G4') {
Patrick Hulcea087f622018-05-18 00:37:53381 emulationModel.emulate(Emulation.DeviceModeModel.Type.Device, device, device.modes[0], 1);
Tim van der Lippe1d6e57a2019-09-30 11:55:34382 }
Patrick Hulcea087f622018-05-18 00:37:53383 }
Blink Reformat4c46d092018-04-07 15:32:37384 }
385
Patrick Hulcea087f622018-05-18 00:37:53386 await this._protocolService.attach();
387 this._isLHAttached = true;
388 }
Blink Reformat4c46d092018-04-07 15:32:37389
Patrick Hulcea087f622018-05-18 00:37:53390 async _resetEmulationAndProtocolConnection() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34391 if (!this._isLHAttached) {
Blink Reformat4c46d092018-04-07 15:32:37392 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34393 }
Blink Reformat4c46d092018-04-07 15:32:37394
Patrick Hulcea087f622018-05-18 00:37:53395 this._isLHAttached = false;
396 await this._protocolService.detach();
Blink Reformat4c46d092018-04-07 15:32:37397
Connor Clarkca8905e2019-08-23 18:35:10398 if (this._stateBefore) {
Paul Lewisdaac1062020-03-05 14:37:10399 const emulationModel = self.singleton(Emulation.DeviceModeModel.DeviceModeModel);
Connor Clarkca8905e2019-08-23 18:35:10400 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 Lippecd0bb372020-05-01 13:53:21403 SDK.NetworkManager.MultitargetNetworkManager.instance().setNetworkConditions(
404 this._stateBefore.network.conditions);
Connor Clarkca8905e2019-08-23 18:35:10405 delete this._stateBefore;
406 }
407
Jack Franklindb9cb272020-10-15 13:11:26408 Emulation.InspectedPagePlaceholder.InspectedPagePlaceholder.instance().update(true);
Blink Reformat4c46d092018-04-07 15:32:37409
Paul Lewisdaac1062020-03-05 14:37:10410 const resourceTreeModel =
411 SDK.SDKModel.TargetManager.instance().mainTarget().model(SDK.ResourceTreeModel.ResourceTreeModel);
Patrick Hulcea087f622018-05-18 00:37:53412 // reload to reset the page state
413 const inspectedURL = await this._controller.getInspectedURL();
414 await resourceTreeModel.navigate(inspectedURL);
Blink Reformat4c46d092018-04-07 15:32:37415 }
Paul Lewiscf2ef222019-11-22 14:55:35416}