blob: 501489807860a58878f2bceb0476b0711c640d56 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:37 +00001// Copyright 2018 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
Connor Clark2bc3be22020-02-14 14:34:19 -08005import {Events, LighthouseController, RuntimeSettings} from './LighthouseController.js'; // eslint-disable-line no-unused-vars
Paul Lewis51474192020-01-09 16:02:22 +00006
7export class StatusView {
Patrick Hulcea087f622018-05-18 00:37:53 +00008 /**
Connor Clark2bc3be22020-02-14 14:34:19 -08009 * @param {!LighthouseController} controller
Patrick Hulcea087f622018-05-18 00:37:53 +000010 */
11 constructor(controller) {
12 this._controller = controller;
13
Blink Reformat4c46d092018-04-07 15:32:37 +000014 this._statusView = null;
Patrick Hulcea087f622018-05-18 00:37:53 +000015 this._statusHeader = null;
Blink Reformat4c46d092018-04-07 15:32:37 +000016 this._progressWrapper = null;
17 this._progressBar = null;
18 this._statusText = null;
Connor Clark99508362019-08-20 19:52:23 +000019 this._cancelButton = null;
Blink Reformat4c46d092018-04-07 15:32:37 +000020
Patrick Hulcea087f622018-05-18 00:37:53 +000021 this._inspectedURL = '';
Blink Reformat4c46d092018-04-07 15:32:37 +000022 this._textChangedAt = 0;
Paul Lewiscf2ef222019-11-22 14:55:35 +000023 this._fastFactsQueued = FastFacts.slice();
Blink Reformat4c46d092018-04-07 15:32:37 +000024 this._currentPhase = null;
25 this._scheduledTextChangeTimeout = null;
26 this._scheduledFastFactTimeout = null;
Patrick Hulcea087f622018-05-18 00:37:53 +000027
28 this._dialog = new UI.Dialog();
Patrick Hulce8d387f12018-05-29 18:54:54 +000029 this._dialog.setDimmed(true);
30 this._dialog.setCloseOnEscape(false);
Patrick Hulcea087f622018-05-18 00:37:53 +000031 this._dialog.setOutsideClickCallback(event => event.consume(true));
32 this._render();
Blink Reformat4c46d092018-04-07 15:32:37 +000033 }
34
Patrick Hulcea087f622018-05-18 00:37:53 +000035 _render() {
Connor Clark2bc3be22020-02-14 14:34:19 -080036 const dialogRoot =
37 UI.createShadowRootWithCoreStyles(this._dialog.contentElement, 'lighthouse/lighthouseDialog.css');
38 const lighthouseViewElement = dialogRoot.createChild('div', 'lighthouse-view vbox');
Blink Reformat4c46d092018-04-07 15:32:37 +000039
Patrick Hulcea087f622018-05-18 00:37:53 +000040 const cancelButton = UI.createTextButton(ls`Cancel`, this._cancel.bind(this));
41 const fragment = UI.Fragment.build`
Connor Clark2bc3be22020-02-14 14:34:19 -080042 <div class="lighthouse-view vbox">
Patrick Hulcea087f622018-05-18 00:37:53 +000043 <h2 $="status-header">Auditing your web page\u2026</h2>
Connor Clark2bc3be22020-02-14 14:34:19 -080044 <div class="lighthouse-status vbox" $="status-view">
45 <div class="lighthouse-progress-wrapper" $="progress-wrapper">
46 <div class="lighthouse-progress-bar" $="progress-bar"></div>
Patrick Hulcea087f622018-05-18 00:37:53 +000047 </div>
Connor Clark2bc3be22020-02-14 14:34:19 -080048 <div class="lighthouse-status-text" $="status-text"></div>
Patrick Hulcea087f622018-05-18 00:37:53 +000049 </div>
50 ${cancelButton}
51 </div>
52 `;
Blink Reformat4c46d092018-04-07 15:32:37 +000053
Connor Clark2bc3be22020-02-14 14:34:19 -080054 lighthouseViewElement.appendChild(fragment.element());
Patrick Hulcea087f622018-05-18 00:37:53 +000055
56 this._statusView = fragment.$('status-view');
57 this._statusHeader = fragment.$('status-header');
58 this._progressWrapper = fragment.$('progress-wrapper');
59 this._progressBar = fragment.$('progress-bar');
60 this._statusText = fragment.$('status-text');
John Emau69c38762019-11-25 18:17:47 -080061 // Use StatusPhases array index as progress bar value
Paul Lewis51474192020-01-09 16:02:22 +000062 UI.ARIAUtils.markAsProgressBar(this._progressBar, 0, StatusPhases.length - 1);
Connor Clark99508362019-08-20 19:52:23 +000063 this._cancelButton = cancelButton;
John Emau463280d2019-07-19 00:37:40 +000064 UI.ARIAUtils.markAsStatus(this._statusText);
Patrick Hulcea087f622018-05-18 00:37:53 +000065
66 this._dialog.setDefaultFocusedElement(cancelButton);
67 this._dialog.setSizeBehavior(UI.GlassPane.SizeBehavior.SetExactWidthMaxHeight);
68 this._dialog.setMaxContentSize(new UI.Size(500, 400));
Blink Reformat4c46d092018-04-07 15:32:37 +000069 }
70
Patrick Hulcea087f622018-05-18 00:37:53 +000071 _reset() {
Blink Reformat4c46d092018-04-07 15:32:37 +000072 this._resetProgressBarClasses();
73 clearTimeout(this._scheduledFastFactTimeout);
74
75 this._textChangedAt = 0;
Paul Lewiscf2ef222019-11-22 14:55:35 +000076 this._fastFactsQueued = FastFacts.slice();
Blink Reformat4c46d092018-04-07 15:32:37 +000077 this._currentPhase = null;
78 this._scheduledTextChangeTimeout = null;
79 this._scheduledFastFactTimeout = null;
80 }
81
82 /**
Patrick Hulcea087f622018-05-18 00:37:53 +000083 * @param {!Element} dialogRenderElement
Blink Reformat4c46d092018-04-07 15:32:37 +000084 */
Patrick Hulcea087f622018-05-18 00:37:53 +000085 show(dialogRenderElement) {
86 this._reset();
87 this.updateStatus(ls`Loading\u2026`);
Blink Reformat4c46d092018-04-07 15:32:37 +000088
Peter Marshall3e4e5692019-12-09 17:48:04 +010089 const parsedURL = Common.ParsedURL.fromString(this._inspectedURL);
Patrick Hulcea087f622018-05-18 00:37:53 +000090 const pageHost = parsedURL && parsedURL.host;
91 const statusHeader = pageHost ? ls`Auditing ${pageHost}` : ls`Auditing your web page`;
Connor Clark99508362019-08-20 19:52:23 +000092 this._renderStatusHeader(statusHeader);
Patrick Hulcea087f622018-05-18 00:37:53 +000093 this._dialog.show(dialogRenderElement);
94 }
95
Connor Clark99508362019-08-20 19:52:23 +000096 /**
97 * @param {string=} statusHeader
98 */
99 _renderStatusHeader(statusHeader) {
100 this._statusHeader.textContent = `${statusHeader}\u2026`;
101 }
102
Patrick Hulcea087f622018-05-18 00:37:53 +0000103 hide() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000104 if (this._dialog.isShowing()) {
Patrick Hulcea087f622018-05-18 00:37:53 +0000105 this._dialog.hide();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000106 }
Patrick Hulcea087f622018-05-18 00:37:53 +0000107 }
108
109 /**
110 * @param {string=} url
111 */
112 setInspectedURL(url = '') {
113 this._inspectedURL = url;
Blink Reformat4c46d092018-04-07 15:32:37 +0000114 }
115
116 /**
117 * @param {?string} message
118 */
119 updateStatus(message) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000120 if (!message || !this._statusText) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000121 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000122 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000123
124 if (message.startsWith('Cancel')) {
125 this._commitTextChange(Common.UIString('Cancelling\u2026'));
126 clearTimeout(this._scheduledFastFactTimeout);
127 return;
128 }
129
130 const nextPhase = this._getPhaseForMessage(message);
Paul Lewis51474192020-01-09 16:02:22 +0000131 const nextPhaseIndex = StatusPhases.indexOf(nextPhase);
132 const currentPhaseIndex = StatusPhases.indexOf(this._currentPhase);
Blink Reformat4c46d092018-04-07 15:32:37 +0000133 if (!nextPhase && !this._currentPhase) {
134 this._commitTextChange(Common.UIString('Lighthouse is warming up\u2026'));
135 clearTimeout(this._scheduledFastFactTimeout);
John Emau69c38762019-11-25 18:17:47 -0800136 } else if (nextPhase && (!this._currentPhase || currentPhaseIndex < nextPhaseIndex)) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000137 this._currentPhase = nextPhase;
John Emau69c38762019-11-25 18:17:47 -0800138 const text = this._getMessageForPhase(nextPhase);
139 this._scheduleTextChange(text);
Blink Reformat4c46d092018-04-07 15:32:37 +0000140 this._scheduleFastFactCheck();
141 this._resetProgressBarClasses();
142 this._progressBar.classList.add(nextPhase.progressBarClass);
John Emau69c38762019-11-25 18:17:47 -0800143 UI.ARIAUtils.setProgressBarValue(this._progressBar, nextPhaseIndex, text);
Blink Reformat4c46d092018-04-07 15:32:37 +0000144 }
145 }
146
Patrick Hulcea087f622018-05-18 00:37:53 +0000147 _cancel() {
Connor Clark2bc3be22020-02-14 14:34:19 -0800148 this._controller.dispatchEventToListeners(Events.RequestLighthouseCancel);
Patrick Hulcea087f622018-05-18 00:37:53 +0000149 }
150
Blink Reformat4c46d092018-04-07 15:32:37 +0000151 /**
Paul Lewis51474192020-01-09 16:02:22 +0000152 * @param {!StatusPhases} phase
Blink Reformat4c46d092018-04-07 15:32:37 +0000153 * @return {string}
154 */
155 _getMessageForPhase(phase) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000156 if (phase.message) {
Mandy Chenef16e332019-09-08 05:02:45 +0000157 return phase.message;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000158 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000159
Connor Clark2bc3be22020-02-14 14:34:19 -0800160 const deviceType = RuntimeSettings.find(item => item.setting.name === 'lighthouse.device_type').setting.get();
161 const throttling = RuntimeSettings.find(item => item.setting.name === 'lighthouse.throttling').setting.get();
Paul Lewiscf2ef222019-11-22 14:55:35 +0000162 const match = LoadingMessages.find(item => {
Blink Reformat4c46d092018-04-07 15:32:37 +0000163 return item.deviceType === deviceType && item.throttling === throttling;
164 });
165
Mandy Chenef16e332019-09-08 05:02:45 +0000166 return match ? match.message : ls`Lighthouse is loading your page`;
Blink Reformat4c46d092018-04-07 15:32:37 +0000167 }
168
169 /**
170 * @param {string} message
Paul Lewis51474192020-01-09 16:02:22 +0000171 * @return {?StatusPhases}
Blink Reformat4c46d092018-04-07 15:32:37 +0000172 */
173 _getPhaseForMessage(message) {
Paul Lewiscf2ef222019-11-22 14:55:35 +0000174 return StatusPhases.find(phase => message.startsWith(phase.statusMessagePrefix));
Blink Reformat4c46d092018-04-07 15:32:37 +0000175 }
176
177 _resetProgressBarClasses() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000178 if (!this._progressBar) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000179 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000180 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000181
Connor Clark2bc3be22020-02-14 14:34:19 -0800182 this._progressBar.className = 'lighthouse-progress-bar';
Blink Reformat4c46d092018-04-07 15:32:37 +0000183 }
184
185 _scheduleFastFactCheck() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000186 if (!this._currentPhase || this._scheduledFastFactTimeout) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000187 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000188 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000189
190 this._scheduledFastFactTimeout = setTimeout(() => {
191 this._updateFastFactIfNecessary();
192 this._scheduledFastFactTimeout = null;
193
194 this._scheduleFastFactCheck();
195 }, 100);
196 }
197
198 _updateFastFactIfNecessary() {
199 const now = performance.now();
Paul Lewiscf2ef222019-11-22 14:55:35 +0000200 if (now - this._textChangedAt < fastFactRotationInterval) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000201 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000202 }
203 if (!this._fastFactsQueued.length) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000204 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000205 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000206
207 const fastFactIndex = Math.floor(Math.random() * this._fastFactsQueued.length);
208 this._scheduleTextChange(ls`\ud83d\udca1 ${this._fastFactsQueued[fastFactIndex]}`);
209 this._fastFactsQueued.splice(fastFactIndex, 1);
210 }
211
212 /**
213 * @param {string} text
214 */
215 _commitTextChange(text) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000216 if (!this._statusText) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000217 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000218 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000219 this._textChangedAt = performance.now();
220 this._statusText.textContent = text;
221 }
222
223 /**
224 * @param {string} text
225 */
226 _scheduleTextChange(text) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000227 if (this._scheduledTextChangeTimeout) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000228 clearTimeout(this._scheduledTextChangeTimeout);
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000229 }
Blink Reformat4c46d092018-04-07 15:32:37 +0000230
231 const msSinceLastChange = performance.now() - this._textChangedAt;
Paul Lewiscf2ef222019-11-22 14:55:35 +0000232 const msToTextChange = minimumTextVisibilityDuration - msSinceLastChange;
Blink Reformat4c46d092018-04-07 15:32:37 +0000233
234 this._scheduledTextChangeTimeout = setTimeout(() => {
235 this._commitTextChange(text);
236 }, Math.max(msToTextChange, 0));
237 }
238
239 /**
240 * @param {!Error} err
Blink Reformat4c46d092018-04-07 15:32:37 +0000241 */
Patrick Hulcea087f622018-05-18 00:37:53 +0000242 renderBugReport(err) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000243 console.error(err);
244 clearTimeout(this._scheduledFastFactTimeout);
245 clearTimeout(this._scheduledTextChangeTimeout);
246 this._resetProgressBarClasses();
247 this._progressBar.classList.add('errored');
248
249 this._commitTextChange('');
Paul Irishf2659072019-03-24 19:08:52 +0000250 this._statusText.createChild('p').createTextChild(Common.UIString('Ah, sorry! We ran into an error.'));
Paul Lewiscf2ef222019-11-22 14:55:35 +0000251 if (KnownBugPatterns.some(pattern => pattern.test(err.message))) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000252 const message = Common.UIString(
Lorne Mitchell7aa2c6c2019-04-03 03:50:10 +0000253 'Try to navigate to the URL in a fresh Chrome profile without any other tabs or extensions open and try again.');
Blink Reformat4c46d092018-04-07 15:32:37 +0000254 this._statusText.createChild('p').createTextChild(message);
255 } else {
Paul Irishf2659072019-03-24 19:08:52 +0000256 this._renderBugReportBody(err, this._inspectedURL);
Blink Reformat4c46d092018-04-07 15:32:37 +0000257 }
258 }
259
260 /**
Connor Clark99508362019-08-20 19:52:23 +0000261 * @param {string} statusHeader
262 * @param {string} text
263 */
264 renderText(statusHeader, text) {
265 this._renderStatusHeader(statusHeader);
266 this._commitTextChange(text);
267 }
268
269 /**
270 * @param {boolean} show
271 */
272 toggleCancelButton(show) {
273 this._cancelButton.style.visibility = show ? 'visible' : 'hidden';
274 }
275
276 /**
Blink Reformat4c46d092018-04-07 15:32:37 +0000277 * @param {!Error} err
278 * @param {string} auditURL
279 */
Paul Irishf2659072019-03-24 19:08:52 +0000280 _renderBugReportBody(err, auditURL) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000281 const issueBody = `
Paul Irishf2659072019-03-24 19:08:52 +0000282${err.message}
Blink Reformat4c46d092018-04-07 15:32:37 +0000283\`\`\`
Paul Irishf2659072019-03-24 19:08:52 +0000284Channel: DevTools
285Initial URL: ${auditURL}
286Chrome Version: ${navigator.userAgent.match(/Chrome\/(\S+)/)[1]}
287Stack Trace: ${err.stack}
Blink Reformat4c46d092018-04-07 15:32:37 +0000288\`\`\`
Paul Irishf2659072019-03-24 19:08:52 +0000289`;
290 this._statusText.createChild('p').createTextChild(
291 ls`If this issue is reproducible, please report it at the Lighthouse GitHub repo.`);
292 this._statusText.createChild('code', 'monospace').createTextChild(issueBody.trim());
Blink Reformat4c46d092018-04-07 15:32:37 +0000293 }
Paul Lewiscf2ef222019-11-22 14:55:35 +0000294}
Blink Reformat4c46d092018-04-07 15:32:37 +0000295
Paul Lewiscf2ef222019-11-22 14:55:35 +0000296/** @const */
297export const fastFactRotationInterval = 6000;
298
299/** @const */
300export const minimumTextVisibilityDuration = 3000;
Blink Reformat4c46d092018-04-07 15:32:37 +0000301
302/** @type {!Array.<!RegExp>} */
Tim van der Lippec96ccd92019-11-29 16:23:54 +0000303const KnownBugPatterns = [
Blink Reformat4c46d092018-04-07 15:32:37 +0000304 /PARSING_PROBLEM/,
305 /DOCUMENT_REQUEST/,
306 /READ_FAILED/,
307 /TRACING_ALREADY_STARTED/,
308 /^You must provide a url to the runner/,
309 /^You probably have multiple tabs open/,
310];
311
John Emau69c38762019-11-25 18:17:47 -0800312/** @typedef {{message: string, progressBarClass: string}} */
Paul Lewiscf2ef222019-11-22 14:55:35 +0000313export const StatusPhases = [
Blink Reformat4c46d092018-04-07 15:32:37 +0000314 {
315 id: 'loading',
316 progressBarClass: 'loading',
317 statusMessagePrefix: 'Loading page',
Blink Reformat4c46d092018-04-07 15:32:37 +0000318 },
319 {
320 id: 'gathering',
321 progressBarClass: 'gathering',
Mandy Chenef16e332019-09-08 05:02:45 +0000322 message: ls`Lighthouse is gathering information about the page to compute your score.`,
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000323 statusMessagePrefix: 'Gathering',
Blink Reformat4c46d092018-04-07 15:32:37 +0000324 },
325 {
326 id: 'auditing',
327 progressBarClass: 'auditing',
Mandy Chenef16e332019-09-08 05:02:45 +0000328 message: ls`Almost there! Lighthouse is now generating your report.`,
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000329 statusMessagePrefix: 'Auditing',
Blink Reformat4c46d092018-04-07 15:32:37 +0000330 }
331];
332
333/** @typedef {{message: string, deviceType: string, throttling: string}} */
Tim van der Lippec96ccd92019-11-29 16:23:54 +0000334const LoadingMessages = [
Blink Reformat4c46d092018-04-07 15:32:37 +0000335 {
336 deviceType: 'mobile',
337 throttling: 'on',
Mandy Chenef16e332019-09-08 05:02:45 +0000338 message: ls`Lighthouse is loading your page with throttling to measure performance on a mobile device on 3G.`,
Blink Reformat4c46d092018-04-07 15:32:37 +0000339 },
340 {
341 deviceType: 'desktop',
342 throttling: 'on',
Mandy Chenef16e332019-09-08 05:02:45 +0000343 message: ls`Lighthouse is loading your page with throttling to measure performance on a slow desktop on 3G.`,
Blink Reformat4c46d092018-04-07 15:32:37 +0000344 },
345 {
346 deviceType: 'mobile',
347 throttling: 'off',
Mandy Chenef16e332019-09-08 05:02:45 +0000348 message: ls`Lighthouse is loading your page with mobile emulation.`,
Blink Reformat4c46d092018-04-07 15:32:37 +0000349 },
350 {
351 deviceType: 'desktop',
352 throttling: 'off',
Mandy Chenef16e332019-09-08 05:02:45 +0000353 message: ls`Lighthouse is loading your page.`,
Blink Reformat4c46d092018-04-07 15:32:37 +0000354 },
355];
356
Tim van der Lippec96ccd92019-11-29 16:23:54 +0000357const FastFacts = [
Paul Lewiscf2ef222019-11-22 14:55:35 +0000358 ls
359`1MB takes a minimum of 5 seconds to download on a typical 3G connection [Source: WebPageTest and DevTools 3G definition].`,
Connor Clarkc4170592020-01-13 14:26:54 -0800360 ls`Rebuilding Pinterest pages for performance increased conversion rates by 15% [Source: WPO Stats]`, ls
Paul Lewiscf2ef222019-11-22 14:55:35 +0000361`By reducing the response size of JSON needed for displaying comments, Instagram saw increased impressions [Source: WPO Stats]`,
362 ls`Walmart saw a 1% increase in revenue for every 100ms improvement in page load [Source: WPO Stats]`, ls
363`If a site takes >1 second to become interactive, users lose attention, and their perception of completing the page task is broken [Source: Google Developers Blog]`,
364 ls`75% of global mobile users in 2016 were on 2G or 3G [Source: GSMA Mobile]`,
Connor Clarkc4170592020-01-13 14:26:54 -0800365 ls`The average user device costs less than 200 USD. [Source: International Data Corporation]`, ls
Paul Lewiscf2ef222019-11-22 14:55:35 +0000366`19 seconds is the average time a mobile web page takes to load on a 3G connection [Source: Google DoubleClick blog]`,
367 ls
Paul Lewiscf2ef222019-11-22 14:55:35 +0000368`70% of mobile pages take nearly 7 seconds for the visual content above the fold to display on the screen. [Source: Think with Google]`,
369 ls
370`As page load time increases from one second to seven seconds, the probability of a mobile site visitor bouncing increases 113%. [Source: Think with Google]`,
371 ls
372`As the number of elements on a page increases from 400 to 6,000, the probability of conversion drops 95%. [Source: Think with Google]`,
373 ls`70% of mobile pages weigh over 1MB, 36% over 2MB, and 12% over 4MB. [Source: Think with Google]`, ls
374 `Lighthouse only simulates mobile performance; to measure performance on a real device, try WebPageTest.org [Source: Lighthouse team]`,
Blink Reformat4c46d092018-04-07 15:32:37 +0000375];