blob: 9bde164bb4673b971cf37afb1a11451a3eeb26a2 [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 */
cjamcl@google.comaa1532c2019-05-31 03:01:24 +00008Audits.AuditsPanel = class 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);
194 });
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000195 Audits.ReportRenderer.handleDarkMode(el);
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000196
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000197 const features = new Audits.ReportUIFeatures(dom);
Connor Clark99508362019-08-20 19:52:23 +0000198 features.setBeforePrint(this._beforePrint.bind(this));
199 features.setAfterPrint(this._afterPrint.bind(this));
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000200 features.setTemplateContext(templatesDOM);
201 features.initFeatures(lighthouseResult);
Blink Reformat4c46d092018-04-07 15:32:37 +0000202
Patrick Hulcea087f622018-05-18 00:37:53 +0000203 this._cachedRenderedReports.set(lighthouseResult, reportContainer);
Blink Reformat4c46d092018-04-07 15:32:37 +0000204 }
205
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000206 _waitForMainTargetLoad() {
207 const mainTarget = SDK.targetManager.mainTarget();
208 const resourceTreeModel = mainTarget.model(SDK.ResourceTreeModel);
209 return resourceTreeModel.once(SDK.ResourceTreeModel.Events.Load);
210 }
211
Blink Reformat4c46d092018-04-07 15:32:37 +0000212 /**
213 * @param {!ReportRenderer.ReportJSON} lighthouseResult
Paul Irish8f1e33d2018-05-31 02:29:50 +0000214 * @param {!ReportRenderer.RunnerResultArtifacts=} artifacts
Blink Reformat4c46d092018-04-07 15:32:37 +0000215 */
Paul Irish8f1e33d2018-05-31 02:29:50 +0000216 _buildReportUI(lighthouseResult, artifacts) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000217 if (lighthouseResult === null) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000218 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000219 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000220
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000221 const optionElement = new Audits.ReportSelector.Item(
Paul Irish8f1e33d2018-05-31 02:29:50 +0000222 lighthouseResult, () => this._renderReport(lighthouseResult, artifacts), this._renderStartView.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37 +0000223 this._reportSelector.prepend(optionElement);
Blink Reformat4c46d092018-04-07 15:32:37 +0000224 this._refreshToolbarUI();
Patrick Hulcea087f622018-05-18 00:37:53 +0000225 this._renderReport(lighthouseResult);
Blink Reformat4c46d092018-04-07 15:32:37 +0000226 }
227
228 /**
229 * @param {!DataTransfer} dataTransfer
230 */
231 _handleDrop(dataTransfer) {
232 const items = dataTransfer.items;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000233 if (!items.length) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000234 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000235 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000236 const item = items[0];
237 if (item.kind === 'file') {
238 const entry = items[0].webkitGetAsEntry();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000239 if (!entry.isFile) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000240 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000241 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000242 entry.file(file => {
243 const reader = new FileReader();
244 reader.onload = () => this._loadedFromFile(/** @type {string} */ (reader.result));
245 reader.readAsText(file);
246 });
247 }
248 }
249
250 /**
Patrick Hulcea087f622018-05-18 00:37:53 +0000251 * @param {string} report
Blink Reformat4c46d092018-04-07 15:32:37 +0000252 */
Patrick Hulcea087f622018-05-18 00:37:53 +0000253 _loadedFromFile(report) {
254 const data = JSON.parse(report);
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000255 if (!data['lighthouseVersion']) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000256 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000257 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000258 this._buildReportUI(/** @type {!ReportRenderer.ReportJSON} */ (data));
259 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000260
Patrick Hulcea087f622018-05-18 00:37:53 +0000261 async _startAudit() {
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000262 Host.userMetrics.actionTaken(Host.UserMetrics.Action.AuditsStarted);
Blink Reformat4c46d092018-04-07 15:32:37 +0000263
Patrick Hulcea087f622018-05-18 00:37:53 +0000264 try {
265 const inspectedURL = await this._controller.getInspectedURL({force: true});
266 const categoryIDs = this._controller.getCategoryIDs();
267 const flags = this._controller.getFlags();
Blink Reformat4c46d092018-04-07 15:32:37 +0000268
Patrick Hulcea087f622018-05-18 00:37:53 +0000269 await this._setupEmulationAndProtocolConnection();
Blink Reformat4c46d092018-04-07 15:32:37 +0000270
Patrick Hulcea087f622018-05-18 00:37:53 +0000271 this._renderStatusView(inspectedURL);
Blink Reformat4c46d092018-04-07 15:32:37 +0000272
Paul Irish8f1e33d2018-05-31 02:29:50 +0000273 const lighthouseResponse = await this._protocolService.startLighthouse(inspectedURL, categoryIDs, flags);
Blink Reformat4c46d092018-04-07 15:32:37 +0000274
Paul Irish8f1e33d2018-05-31 02:29:50 +0000275 if (lighthouseResponse && lighthouseResponse.fatal) {
276 const error = new Error(lighthouseResponse.message);
277 error.stack = lighthouseResponse.stack;
Patrick Hulcea087f622018-05-18 00:37:53 +0000278 throw error;
279 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000280
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000281 if (!lighthouseResponse) {
Patrick Hulcea087f622018-05-18 00:37:53 +0000282 throw new Error('Auditing failed to produce a result');
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000283 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000284
cjamcl@google.comaa1532c2019-05-31 03:01:24 +0000285 Host.userMetrics.actionTaken(Host.UserMetrics.Action.AuditsFinished);
Blink Reformat4c46d092018-04-07 15:32:37 +0000286
Patrick Hulcea087f622018-05-18 00:37:53 +0000287 await this._resetEmulationAndProtocolConnection();
Paul Irish8f1e33d2018-05-31 02:29:50 +0000288 this._buildReportUI(lighthouseResponse.lhr, lighthouseResponse.artifacts);
Patrick Hulcea087f622018-05-18 00:37:53 +0000289 } catch (err) {
Paul Irishdca01d02019-03-25 20:17:56 +0000290 await this._resetEmulationAndProtocolConnection();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000291 if (err instanceof Error) {
Patrick Hulcea087f622018-05-18 00:37:53 +0000292 this._statusView.renderBugReport(err);
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000293 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000294 }
295 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000296
Patrick Hulcea087f622018-05-18 00:37:53 +0000297 async _cancelAudit() {
298 this._statusView.updateStatus(ls`Cancelling`);
299 await this._resetEmulationAndProtocolConnection();
300 this._renderStartView();
Blink Reformat4c46d092018-04-07 15:32:37 +0000301 }
302
Paul Irishd8495012019-07-16 23:51:47 +0000303 /**
304 * We set the device emulation on the DevTools-side for two reasons:
305 * 1. To workaround some odd device metrics emulation bugs like occuluding viewports
306 * 2. To get the attractive device outline
Connor Clarkca8905e2019-08-23 18:35:10 +0000307 *
Connor Clark3f700342019-07-25 02:10:41 +0000308 * We also set flags.deviceScreenEmulationMethod = 'provided' to let LH only apply UA emulation
Paul Irishd8495012019-07-16 23:51:47 +0000309 */
Patrick Hulcea087f622018-05-18 00:37:53 +0000310 async _setupEmulationAndProtocolConnection() {
311 const flags = this._controller.getFlags();
Blink Reformat4c46d092018-04-07 15:32:37 +0000312
Patrick Hulcea087f622018-05-18 00:37:53 +0000313 const emulationModel = self.singleton(Emulation.DeviceModeModel);
Connor Clarkca8905e2019-08-23 18:35:10 +0000314 this._stateBefore = {
315 emulation: {
316 enabled: emulationModel.enabledSetting().get(),
317 outlineEnabled: emulationModel.deviceOutlineSetting().get(),
318 toolbarControlsEnabled: emulationModel.toolbarControlsEnabledSetting().get()
319 },
320 network: {conditions: SDK.multitargetNetworkManager.networkConditions()}
321 };
Patrick Hulcea087f622018-05-18 00:37:53 +0000322
Connor Clarkca8905e2019-08-23 18:35:10 +0000323 emulationModel.toolbarControlsEnabledSetting().set(false);
Connor Clark3f700342019-07-25 02:10:41 +0000324 if (flags.emulatedFormFactor === 'desktop') {
Patrick Hulcea087f622018-05-18 00:37:53 +0000325 emulationModel.enabledSetting().set(false);
Patrick Hulcea087f622018-05-18 00:37:53 +0000326 emulationModel.emulate(Emulation.DeviceModeModel.Type.None, null, null);
Connor Clark3f700342019-07-25 02:10:41 +0000327 } else if (flags.emulatedFormFactor === 'mobile') {
Patrick Hulcea087f622018-05-18 00:37:53 +0000328 emulationModel.enabledSetting().set(true);
329 emulationModel.deviceOutlineSetting().set(true);
330
331 for (const device of Emulation.EmulatedDevicesList.instance().standard()) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000332 if (device.title === 'Nexus 5X') {
Patrick Hulcea087f622018-05-18 00:37:53 +0000333 emulationModel.emulate(Emulation.DeviceModeModel.Type.Device, device, device.modes[0], 1);
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000334 }
Patrick Hulcea087f622018-05-18 00:37:53 +0000335 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000336 }
337
Patrick Hulcea087f622018-05-18 00:37:53 +0000338 await this._protocolService.attach();
339 this._isLHAttached = true;
340 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000341
Patrick Hulcea087f622018-05-18 00:37:53 +0000342 async _resetEmulationAndProtocolConnection() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000343 if (!this._isLHAttached) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000344 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000345 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000346
Patrick Hulcea087f622018-05-18 00:37:53 +0000347 this._isLHAttached = false;
348 await this._protocolService.detach();
Blink Reformat4c46d092018-04-07 15:32:37 +0000349
Connor Clarkca8905e2019-08-23 18:35:10 +0000350 if (this._stateBefore) {
351 const emulationModel = self.singleton(Emulation.DeviceModeModel);
352 emulationModel.enabledSetting().set(this._stateBefore.emulation.enabled);
353 emulationModel.deviceOutlineSetting().set(this._stateBefore.emulation.outlineEnabled);
354 emulationModel.toolbarControlsEnabledSetting().set(this._stateBefore.emulation.toolbarControlsEnabled);
355 SDK.multitargetNetworkManager.setNetworkConditions(this._stateBefore.network.conditions);
356 delete this._stateBefore;
357 }
358
Patrick Hulcea087f622018-05-18 00:37:53 +0000359 Emulation.InspectedPagePlaceholder.instance().update(true);
Blink Reformat4c46d092018-04-07 15:32:37 +0000360
Patrick Hulcea087f622018-05-18 00:37:53 +0000361 const resourceTreeModel = SDK.targetManager.mainTarget().model(SDK.ResourceTreeModel);
362 // reload to reset the page state
363 const inspectedURL = await this._controller.getInspectedURL();
364 await resourceTreeModel.navigate(inspectedURL);
Blink Reformat4c46d092018-04-07 15:32:37 +0000365 }
366};