blob: 7820c10ebf7dcfa6115cf15a02c3f52115589db7 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:37 +00001// 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
5/**
Blink Reformat4c46d092018-04-07 15:32:37 +00006 * @unrestricted
7 */
Paul Lewiscf2ef222019-11-22 14:55:35 +00008export default class AuditsPanel extends UI.Panel {
Blink Reformat4c46d092018-04-07 15:32:37 +00009 constructor() {
cjamcl@google.comaa1532c2019-05-31 03:01:24 +000010 super('audits');
11 this.registerRequiredCSS('audits/lighthouse/report.css');
12 this.registerRequiredCSS('audits/auditsPanel.css');
Blink Reformat4c46d092018-04-07 15:32:37 +000013
cjamcl@google.comaa1532c2019-05-31 03:01:24 +000014 this._protocolService = new Audits.ProtocolService();
15 this._controller = new Audits.AuditController(this._protocolService);
16 this._startView = new Audits.StartView(this._controller);
17 this._statusView = new Audits.StatusView(this._controller);
Blink Reformat4c46d092018-04-07 15:32:37 +000018
Patrick Hulcea087f622018-05-18 00:37:53 +000019 this._unauditableExplanation = null;
20 this._cachedRenderedReports = new Map();
Blink Reformat4c46d092018-04-07 15:32:37 +000021
Blink Reformat4c46d092018-04-07 15:32:37 +000022 this._dropTarget = new UI.DropTarget(
23 this.contentElement, [UI.DropTarget.Type.File], Common.UIString('Drop audit file here'),
24 this._handleDrop.bind(this));
25
cjamcl@google.comaa1532c2019-05-31 03:01:24 +000026 this._controller.addEventListener(Audits.Events.PageAuditabilityChanged, this._refreshStartAuditUI.bind(this));
27 this._controller.addEventListener(Audits.Events.AuditProgressChanged, this._refreshStatusUI.bind(this));
28 this._controller.addEventListener(Audits.Events.RequestAuditStart, this._startAudit.bind(this));
29 this._controller.addEventListener(Audits.Events.RequestAuditCancel, this._cancelAudit.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37 +000030
Patrick Hulcea087f622018-05-18 00:37:53 +000031 this._renderToolbar();
cjamcl@google.comaa1532c2019-05-31 03:01:24 +000032 this._auditResultsElement = this.contentElement.createChild('div', 'audits-results-container');
Patrick Hulcea087f622018-05-18 00:37:53 +000033 this._renderStartView();
34
35 this._controller.recomputePageAuditability();
Blink Reformat4c46d092018-04-07 15:32:37 +000036 }
37
38 /**
Patrick Hulcea087f622018-05-18 00:37:53 +000039 * @param {!Common.Event} evt
Blink Reformat4c46d092018-04-07 15:32:37 +000040 */
Patrick Hulcea087f622018-05-18 00:37:53 +000041 _refreshStartAuditUI(evt) {
Connor Clarkca8905e2019-08-23 18:35:10 +000042 // PageAuditabilityChanged fires multiple times during an audit, which we want to ignore.
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000043 if (this._isLHAttached) {
Connor Clarkca8905e2019-08-23 18:35:10 +000044 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000045 }
Connor Clarkca8905e2019-08-23 18:35:10 +000046
Patrick Hulcea087f622018-05-18 00:37:53 +000047 this._unauditableExplanation = evt.data.helpText;
48 this._startView.setUnauditableExplanation(evt.data.helpText);
49 this._startView.setStartButtonEnabled(!evt.data.helpText);
Blink Reformat4c46d092018-04-07 15:32:37 +000050 }
51
52 /**
Patrick Hulcea087f622018-05-18 00:37:53 +000053 * @param {!Common.Event} evt
Blink Reformat4c46d092018-04-07 15:32:37 +000054 */
Patrick Hulcea087f622018-05-18 00:37:53 +000055 _refreshStatusUI(evt) {
56 this._statusView.updateStatus(evt.data.message);
Blink Reformat4c46d092018-04-07 15:32:37 +000057 }
58
59 _refreshToolbarUI() {
Blink Reformat4c46d092018-04-07 15:32:37 +000060 this._clearButton.setEnabled(this._reportSelector.hasItems());
61 }
62
63 _clearAll() {
64 this._reportSelector.clearAll();
Patrick Hulcea087f622018-05-18 00:37:53 +000065 this._renderStartView();
Blink Reformat4c46d092018-04-07 15:32:37 +000066 this._refreshToolbarUI();
67 }
68
Blink Reformat4c46d092018-04-07 15:32:37 +000069 _renderToolbar() {
Connor Clarke66080e2019-11-06 16:35:51 -080070 const auditsToolbarContainer = this.element.createChild('div', 'audits-toolbar-container');
71
72 const toolbar = new UI.Toolbar('', auditsToolbarContainer);
Blink Reformat4c46d092018-04-07 15:32:37 +000073
74 this._newButton = new UI.ToolbarButton(Common.UIString('Perform an audit\u2026'), 'largeicon-add');
75 toolbar.appendToolbarItem(this._newButton);
Patrick Hulcea087f622018-05-18 00:37:53 +000076 this._newButton.addEventListener(UI.ToolbarButton.Events.Click, this._renderStartView.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37 +000077
Blink Reformat4c46d092018-04-07 15:32:37 +000078 toolbar.appendSeparator();
79
cjamcl@google.comaa1532c2019-05-31 03:01:24 +000080 this._reportSelector = new Audits.ReportSelector(() => this._renderStartView());
Blink Reformat4c46d092018-04-07 15:32:37 +000081 toolbar.appendToolbarItem(this._reportSelector.comboBox());
82
83 this._clearButton = new UI.ToolbarButton(Common.UIString('Clear all'), 'largeicon-clear');
84 toolbar.appendToolbarItem(this._clearButton);
85 this._clearButton.addEventListener(UI.ToolbarButton.Events.Click, this._clearAll.bind(this));
86
Connor Clarke66080e2019-11-06 16:35:51 -080087 this._settingsPane = new UI.HBox();
88 this._settingsPane.show(this.contentElement);
89 this._settingsPane.element.classList.add('audits-settings-pane');
90 this._settingsPane.element.appendChild(this._startView.settingsToolbar().element);
91 this._showSettingsPaneSetting = Common.settings.createSetting('auditsShowSettingsToolbar', false);
92
93 this._rightToolbar = new UI.Toolbar('', auditsToolbarContainer);
94 this._rightToolbar.appendSeparator();
95 this._rightToolbar.appendToolbarItem(
96 new UI.ToolbarSettingToggle(this._showSettingsPaneSetting, 'largeicon-settings-gear', ls`Audits settings`));
97 this._showSettingsPaneSetting.addChangeListener(this._updateSettingsPaneVisibility.bind(this));
98 this._updateSettingsPaneVisibility();
99
Patrick Hulcea087f622018-05-18 00:37:53 +0000100 this._refreshToolbarUI();
101 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000102
Connor Clarke66080e2019-11-06 16:35:51 -0800103 _updateSettingsPaneVisibility() {
104 this._settingsPane.element.classList.toggle('hidden', !this._showSettingsPaneSetting.get());
105 }
106
107 /**
108 * @param {boolean} show
109 */
110 _toggleSettingsDisplay(show) {
111 this._rightToolbar.element.classList.toggle('hidden', !show);
112 this._settingsPane.element.classList.toggle('hidden', !show);
113 this._updateSettingsPaneVisibility();
114 }
115
Patrick Hulcea087f622018-05-18 00:37:53 +0000116 _renderStartView() {
117 this._auditResultsElement.removeChildren();
118 this._statusView.hide();
119
120 this._reportSelector.selectNewAudit();
121 this.contentElement.classList.toggle('in-progress', false);
122
123 this._startView.show(this.contentElement);
Connor Clarke66080e2019-11-06 16:35:51 -0800124 this._toggleSettingsDisplay(true);
Patrick Hulcea087f622018-05-18 00:37:53 +0000125 this._startView.setUnauditableExplanation(this._unauditableExplanation);
126 this._startView.setStartButtonEnabled(!this._unauditableExplanation);
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000127 if (!this._unauditableExplanation) {
Patrick Hulce8d387f12018-05-29 18:54:54 +0000128 this._startView.focusStartButton();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000129 }
Patrick Hulce8d387f12018-05-29 18:54:54 +0000130
Patrick Hulce05c18ce2018-05-24 00:34:56 +0000131 this._newButton.setEnabled(false);
Patrick Hulcea087f622018-05-18 00:37:53 +0000132 this._refreshToolbarUI();
Patrick Hulce05c18ce2018-05-24 00:34:56 +0000133 this.setDefaultFocusedChild(this._startView);
Patrick Hulcea087f622018-05-18 00:37:53 +0000134 }
135
136 /**
137 * @param {string} inspectedURL
138 */
139 _renderStatusView(inspectedURL) {
140 this.contentElement.classList.toggle('in-progress', true);
141 this._statusView.setInspectedURL(inspectedURL);
142 this._statusView.show(this.contentElement);
143 }
144
Connor Clark99508362019-08-20 19:52:23 +0000145 _beforePrint() {
146 this._statusView.show(this.contentElement);
147 this._statusView.toggleCancelButton(false);
148 this._statusView.renderText(ls`Printing`, ls`The print popup window is open. Please close it to continue.`);
149 }
150
151 _afterPrint() {
152 this._statusView.hide();
153 this._statusView.toggleCancelButton(true);
154 }
155
Patrick Hulcea087f622018-05-18 00:37:53 +0000156 /**
157 * @param {!ReportRenderer.ReportJSON} lighthouseResult
Paul Irish8f1e33d2018-05-31 02:29:50 +0000158 * @param {!ReportRenderer.RunnerResultArtifacts=} artifacts
Patrick Hulcea087f622018-05-18 00:37:53 +0000159 */
Paul Irish8f1e33d2018-05-31 02:29:50 +0000160 _renderReport(lighthouseResult, artifacts) {
Connor Clarke66080e2019-11-06 16:35:51 -0800161 this._toggleSettingsDisplay(false);
Patrick Hulcea087f622018-05-18 00:37:53 +0000162 this.contentElement.classList.toggle('in-progress', false);
163 this._startView.hideWidget();
164 this._statusView.hide();
165 this._auditResultsElement.removeChildren();
Patrick Hulce05c18ce2018-05-24 00:34:56 +0000166 this._newButton.setEnabled(true);
Patrick Hulcea087f622018-05-18 00:37:53 +0000167 this._refreshToolbarUI();
168
169 const cachedRenderedReport = this._cachedRenderedReports.get(lighthouseResult);
170 if (cachedRenderedReport) {
171 this._auditResultsElement.appendChild(cachedRenderedReport);
172 return;
Blink Reformat4c46d092018-04-07 15:32:37 +0000173 }
174
Patrick Hulcea087f622018-05-18 00:37:53 +0000175 const reportContainer = this._auditResultsElement.createChild('div', 'lh-vars lh-root lh-devtools');
Blink Reformat4c46d092018-04-07 15:32:37 +0000176
Patrick Hulcea087f622018-05-18 00:37:53 +0000177 const dom = new DOM(/** @type {!Document} */ (this._auditResultsElement.ownerDocument));
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000178 const renderer = new Audits.ReportRenderer(dom);
Patrick Hulcea087f622018-05-18 00:37:53 +0000179
Tim van der Lippe99e59b82019-09-30 20:00:59 +0000180 const templatesHTML = Root.Runtime.cachedResources['audits/lighthouse/templates.html'];
Patrick Hulcea087f622018-05-18 00:37:53 +0000181 const templatesDOM = new DOMParser().parseFromString(templatesHTML, 'text/html');
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000182 if (!templatesDOM) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000183 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000184 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000185
Patrick Hulcea087f622018-05-18 00:37:53 +0000186 renderer.setTemplateContext(templatesDOM);
Paul Irish8f1e33d2018-05-31 02:29:50 +0000187 const el = renderer.renderReport(lighthouseResult, reportContainer);
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000188 Audits.ReportRenderer.addViewTraceButton(el, artifacts);
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000189 // Linkifying requires the target be loaded. Do not block the report
190 // from rendering, as this is just an embellishment and the main target
191 // could take awhile to load.
192 this._waitForMainTargetLoad().then(() => {
193 Audits.ReportRenderer.linkifyNodeDetails(el);
Connor Clark0403a422019-11-18 18:03:18 -0800194 Audits.ReportRenderer.linkifySourceLocationDetails(el);
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000195 });
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000196 Audits.ReportRenderer.handleDarkMode(el);
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000197
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000198 const features = new Audits.ReportUIFeatures(dom);
Connor Clark99508362019-08-20 19:52:23 +0000199 features.setBeforePrint(this._beforePrint.bind(this));
200 features.setAfterPrint(this._afterPrint.bind(this));
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000201 features.setTemplateContext(templatesDOM);
202 features.initFeatures(lighthouseResult);
Blink Reformat4c46d092018-04-07 15:32:37 +0000203
Patrick Hulcea087f622018-05-18 00:37:53 +0000204 this._cachedRenderedReports.set(lighthouseResult, reportContainer);
Blink Reformat4c46d092018-04-07 15:32:37 +0000205 }
206
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000207 _waitForMainTargetLoad() {
208 const mainTarget = SDK.targetManager.mainTarget();
209 const resourceTreeModel = mainTarget.model(SDK.ResourceTreeModel);
210 return resourceTreeModel.once(SDK.ResourceTreeModel.Events.Load);
211 }
212
Blink Reformat4c46d092018-04-07 15:32:37 +0000213 /**
214 * @param {!ReportRenderer.ReportJSON} lighthouseResult
Paul Irish8f1e33d2018-05-31 02:29:50 +0000215 * @param {!ReportRenderer.RunnerResultArtifacts=} artifacts
Blink Reformat4c46d092018-04-07 15:32:37 +0000216 */
Paul Irish8f1e33d2018-05-31 02:29:50 +0000217 _buildReportUI(lighthouseResult, artifacts) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000218 if (lighthouseResult === null) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000219 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000220 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000221
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000222 const optionElement = new Audits.ReportSelector.Item(
Paul Irish8f1e33d2018-05-31 02:29:50 +0000223 lighthouseResult, () => this._renderReport(lighthouseResult, artifacts), this._renderStartView.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37 +0000224 this._reportSelector.prepend(optionElement);
Blink Reformat4c46d092018-04-07 15:32:37 +0000225 this._refreshToolbarUI();
Patrick Hulcea087f622018-05-18 00:37:53 +0000226 this._renderReport(lighthouseResult);
Blink Reformat4c46d092018-04-07 15:32:37 +0000227 }
228
229 /**
230 * @param {!DataTransfer} dataTransfer
231 */
232 _handleDrop(dataTransfer) {
233 const items = dataTransfer.items;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000234 if (!items.length) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000235 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000236 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000237 const item = items[0];
238 if (item.kind === 'file') {
239 const entry = items[0].webkitGetAsEntry();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000240 if (!entry.isFile) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000241 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000242 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000243 entry.file(file => {
244 const reader = new FileReader();
245 reader.onload = () => this._loadedFromFile(/** @type {string} */ (reader.result));
246 reader.readAsText(file);
247 });
248 }
249 }
250
251 /**
Patrick Hulcea087f622018-05-18 00:37:53 +0000252 * @param {string} report
Blink Reformat4c46d092018-04-07 15:32:37 +0000253 */
Patrick Hulcea087f622018-05-18 00:37:53 +0000254 _loadedFromFile(report) {
255 const data = JSON.parse(report);
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000256 if (!data['lighthouseVersion']) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000257 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000258 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000259 this._buildReportUI(/** @type {!ReportRenderer.ReportJSON} */ (data));
260 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000261
Patrick Hulcea087f622018-05-18 00:37:53 +0000262 async _startAudit() {
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000263 Host.userMetrics.actionTaken(Host.UserMetrics.Action.AuditsStarted);
Blink Reformat4c46d092018-04-07 15:32:37 +0000264
Patrick Hulcea087f622018-05-18 00:37:53 +0000265 try {
266 const inspectedURL = await this._controller.getInspectedURL({force: true});
267 const categoryIDs = this._controller.getCategoryIDs();
268 const flags = this._controller.getFlags();
Blink Reformat4c46d092018-04-07 15:32:37 +0000269
Patrick Hulcea087f622018-05-18 00:37:53 +0000270 await this._setupEmulationAndProtocolConnection();
Blink Reformat4c46d092018-04-07 15:32:37 +0000271
Patrick Hulcea087f622018-05-18 00:37:53 +0000272 this._renderStatusView(inspectedURL);
Blink Reformat4c46d092018-04-07 15:32:37 +0000273
Paul Irish8f1e33d2018-05-31 02:29:50 +0000274 const lighthouseResponse = await this._protocolService.startLighthouse(inspectedURL, categoryIDs, flags);
Blink Reformat4c46d092018-04-07 15:32:37 +0000275
Paul Irish8f1e33d2018-05-31 02:29:50 +0000276 if (lighthouseResponse && lighthouseResponse.fatal) {
277 const error = new Error(lighthouseResponse.message);
278 error.stack = lighthouseResponse.stack;
Patrick Hulcea087f622018-05-18 00:37:53 +0000279 throw error;
280 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000281
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000282 if (!lighthouseResponse) {
Patrick Hulcea087f622018-05-18 00:37:53 +0000283 throw new Error('Auditing failed to produce a result');
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000284 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000285
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000286 Host.userMetrics.actionTaken(Host.UserMetrics.Action.AuditsFinished);
Blink Reformat4c46d092018-04-07 15:32:37 +0000287
Patrick Hulcea087f622018-05-18 00:37:53 +0000288 await this._resetEmulationAndProtocolConnection();
Paul Irish8f1e33d2018-05-31 02:29:50 +0000289 this._buildReportUI(lighthouseResponse.lhr, lighthouseResponse.artifacts);
Patrick Hulcea087f622018-05-18 00:37:53 +0000290 } catch (err) {
Paul Irishdca01d02019-03-25 20:17:56 +0000291 await this._resetEmulationAndProtocolConnection();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000292 if (err instanceof Error) {
Patrick Hulcea087f622018-05-18 00:37:53 +0000293 this._statusView.renderBugReport(err);
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000294 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000295 }
296 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000297
Patrick Hulcea087f622018-05-18 00:37:53 +0000298 async _cancelAudit() {
299 this._statusView.updateStatus(ls`Cancelling`);
300 await this._resetEmulationAndProtocolConnection();
301 this._renderStartView();
Blink Reformat4c46d092018-04-07 15:32:37 +0000302 }
303
Paul Irishd8495012019-07-16 23:51:47 +0000304 /**
305 * We set the device emulation on the DevTools-side for two reasons:
306 * 1. To workaround some odd device metrics emulation bugs like occuluding viewports
307 * 2. To get the attractive device outline
Connor Clarkca8905e2019-08-23 18:35:10 +0000308 *
Connor Clark3ee5ac72019-11-07 15:11:58 -0800309 * We also set flags.internalDisableDeviceScreenEmulation = true to let LH only apply UA emulation
Paul Irishd8495012019-07-16 23:51:47 +0000310 */
Patrick Hulcea087f622018-05-18 00:37:53 +0000311 async _setupEmulationAndProtocolConnection() {
312 const flags = this._controller.getFlags();
Blink Reformat4c46d092018-04-07 15:32:37 +0000313
Patrick Hulcea087f622018-05-18 00:37:53 +0000314 const emulationModel = self.singleton(Emulation.DeviceModeModel);
Connor Clarkca8905e2019-08-23 18:35:10 +0000315 this._stateBefore = {
316 emulation: {
317 enabled: emulationModel.enabledSetting().get(),
318 outlineEnabled: emulationModel.deviceOutlineSetting().get(),
319 toolbarControlsEnabled: emulationModel.toolbarControlsEnabledSetting().get()
320 },
321 network: {conditions: SDK.multitargetNetworkManager.networkConditions()}
322 };
Patrick Hulcea087f622018-05-18 00:37:53 +0000323
Connor Clarkca8905e2019-08-23 18:35:10 +0000324 emulationModel.toolbarControlsEnabledSetting().set(false);
Connor Clark3f700342019-07-25 02:10:41 +0000325 if (flags.emulatedFormFactor === 'desktop') {
Patrick Hulcea087f622018-05-18 00:37:53 +0000326 emulationModel.enabledSetting().set(false);
Patrick Hulcea087f622018-05-18 00:37:53 +0000327 emulationModel.emulate(Emulation.DeviceModeModel.Type.None, null, null);
Connor Clark3f700342019-07-25 02:10:41 +0000328 } else if (flags.emulatedFormFactor === 'mobile') {
Patrick Hulcea087f622018-05-18 00:37:53 +0000329 emulationModel.enabledSetting().set(true);
330 emulationModel.deviceOutlineSetting().set(true);
331
332 for (const device of Emulation.EmulatedDevicesList.instance().standard()) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000333 if (device.title === 'Nexus 5X') {
Patrick Hulcea087f622018-05-18 00:37:53 +0000334 emulationModel.emulate(Emulation.DeviceModeModel.Type.Device, device, device.modes[0], 1);
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000335 }
Patrick Hulcea087f622018-05-18 00:37:53 +0000336 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000337 }
338
Patrick Hulcea087f622018-05-18 00:37:53 +0000339 await this._protocolService.attach();
340 this._isLHAttached = true;
341 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000342
Patrick Hulcea087f622018-05-18 00:37:53 +0000343 async _resetEmulationAndProtocolConnection() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000344 if (!this._isLHAttached) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000345 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000346 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000347
Patrick Hulcea087f622018-05-18 00:37:53 +0000348 this._isLHAttached = false;
349 await this._protocolService.detach();
Blink Reformat4c46d092018-04-07 15:32:37 +0000350
Connor Clarkca8905e2019-08-23 18:35:10 +0000351 if (this._stateBefore) {
352 const emulationModel = self.singleton(Emulation.DeviceModeModel);
353 emulationModel.enabledSetting().set(this._stateBefore.emulation.enabled);
354 emulationModel.deviceOutlineSetting().set(this._stateBefore.emulation.outlineEnabled);
355 emulationModel.toolbarControlsEnabledSetting().set(this._stateBefore.emulation.toolbarControlsEnabled);
356 SDK.multitargetNetworkManager.setNetworkConditions(this._stateBefore.network.conditions);
357 delete this._stateBefore;
358 }
359
Patrick Hulcea087f622018-05-18 00:37:53 +0000360 Emulation.InspectedPagePlaceholder.instance().update(true);
Blink Reformat4c46d092018-04-07 15:32:37 +0000361
Patrick Hulcea087f622018-05-18 00:37:53 +0000362 const resourceTreeModel = SDK.targetManager.mainTarget().model(SDK.ResourceTreeModel);
363 // reload to reset the page state
364 const inspectedURL = await this._controller.getInspectedURL();
365 await resourceTreeModel.navigate(inspectedURL);
Blink Reformat4c46d092018-04-07 15:32:37 +0000366 }
Paul Lewiscf2ef222019-11-22 14:55:35 +0000367}
368
369/* Legacy exported object */
370self.Audits = self.Audits || {};
371
372/* Legacy exported object */
373Audits = Audits || {};
374
375/**
376 * @constructor
377 */
378Audits.AuditsPanel = AuditsPanel;