blob: af4105fb5e4dbde0e01818958f8bd55eb2666a84 [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
Patrick Hulcea087f622018-05-18 00:37:53 +00005Audits2.StatusView = class {
6 /**
7 * @param {!Audits2.AuditController} controller
8 */
9 constructor(controller) {
10 this._controller = controller;
11
Blink Reformat4c46d092018-04-07 15:32:37 +000012 this._statusView = null;
Patrick Hulcea087f622018-05-18 00:37:53 +000013 this._statusHeader = null;
Blink Reformat4c46d092018-04-07 15:32:37 +000014 this._progressWrapper = null;
15 this._progressBar = null;
16 this._statusText = null;
17
Patrick Hulcea087f622018-05-18 00:37:53 +000018 this._inspectedURL = '';
Blink Reformat4c46d092018-04-07 15:32:37 +000019 this._textChangedAt = 0;
Patrick Hulcea087f622018-05-18 00:37:53 +000020 this._fastFactsQueued = Audits2.StatusView.FastFacts.slice();
Blink Reformat4c46d092018-04-07 15:32:37 +000021 this._currentPhase = null;
22 this._scheduledTextChangeTimeout = null;
23 this._scheduledFastFactTimeout = null;
Patrick Hulcea087f622018-05-18 00:37:53 +000024
25 this._dialog = new UI.Dialog();
Patrick Hulce8d387f12018-05-29 18:54:54 +000026 this._dialog.setDimmed(true);
27 this._dialog.setCloseOnEscape(false);
Patrick Hulcea087f622018-05-18 00:37:53 +000028 this._dialog.setOutsideClickCallback(event => event.consume(true));
29 this._render();
Blink Reformat4c46d092018-04-07 15:32:37 +000030 }
31
Patrick Hulcea087f622018-05-18 00:37:53 +000032 _render() {
33 const dialogRoot = UI.createShadowRootWithCoreStyles(this._dialog.contentElement, 'audits2/audits2Dialog.css');
34 const auditsViewElement = dialogRoot.createChild('div', 'audits2-view vbox');
Blink Reformat4c46d092018-04-07 15:32:37 +000035
Patrick Hulcea087f622018-05-18 00:37:53 +000036 const cancelButton = UI.createTextButton(ls`Cancel`, this._cancel.bind(this));
37 const fragment = UI.Fragment.build`
38 <div class="audits2-view vbox">
39 <h2 $="status-header">Auditing your web page\u2026</h2>
40 <div class="audits2-status vbox" $="status-view">
41 <div class="audits2-progress-wrapper" $="progress-wrapper">
42 <div class="audits2-progress-bar" $="progress-bar"></div>
43 </div>
44 <div class="audits2-status-text" $="status-text"></div>
45 </div>
46 ${cancelButton}
47 </div>
48 `;
Blink Reformat4c46d092018-04-07 15:32:37 +000049
Patrick Hulcea087f622018-05-18 00:37:53 +000050 auditsViewElement.appendChild(fragment.element());
51 auditsViewElement.tabIndex = 0;
52
53 this._statusView = fragment.$('status-view');
54 this._statusHeader = fragment.$('status-header');
55 this._progressWrapper = fragment.$('progress-wrapper');
56 this._progressBar = fragment.$('progress-bar');
57 this._statusText = fragment.$('status-text');
58
59 this._dialog.setDefaultFocusedElement(cancelButton);
60 this._dialog.setSizeBehavior(UI.GlassPane.SizeBehavior.SetExactWidthMaxHeight);
61 this._dialog.setMaxContentSize(new UI.Size(500, 400));
Blink Reformat4c46d092018-04-07 15:32:37 +000062 }
63
Patrick Hulcea087f622018-05-18 00:37:53 +000064 _reset() {
Blink Reformat4c46d092018-04-07 15:32:37 +000065 this._resetProgressBarClasses();
66 clearTimeout(this._scheduledFastFactTimeout);
67
68 this._textChangedAt = 0;
Patrick Hulcea087f622018-05-18 00:37:53 +000069 this._fastFactsQueued = Audits2.StatusView.FastFacts.slice();
Blink Reformat4c46d092018-04-07 15:32:37 +000070 this._currentPhase = null;
71 this._scheduledTextChangeTimeout = null;
72 this._scheduledFastFactTimeout = null;
73 }
74
75 /**
Patrick Hulcea087f622018-05-18 00:37:53 +000076 * @param {!Element} dialogRenderElement
Blink Reformat4c46d092018-04-07 15:32:37 +000077 */
Patrick Hulcea087f622018-05-18 00:37:53 +000078 show(dialogRenderElement) {
79 this._reset();
80 this.updateStatus(ls`Loading\u2026`);
Blink Reformat4c46d092018-04-07 15:32:37 +000081
Patrick Hulcea087f622018-05-18 00:37:53 +000082 const parsedURL = this._inspectedURL.asParsedURL();
83 const pageHost = parsedURL && parsedURL.host;
84 const statusHeader = pageHost ? ls`Auditing ${pageHost}` : ls`Auditing your web page`;
85 this._statusHeader.textContent = `${statusHeader}\u2026`;
86 this._dialog.show(dialogRenderElement);
87 }
88
89 hide() {
90 if (this._dialog.isShowing())
91 this._dialog.hide();
92 }
93
94 /**
95 * @param {string=} url
96 */
97 setInspectedURL(url = '') {
98 this._inspectedURL = url;
Blink Reformat4c46d092018-04-07 15:32:37 +000099 }
100
101 /**
102 * @param {?string} message
103 */
104 updateStatus(message) {
105 if (!message || !this._statusText)
106 return;
107
108 if (message.startsWith('Cancel')) {
109 this._commitTextChange(Common.UIString('Cancelling\u2026'));
110 clearTimeout(this._scheduledFastFactTimeout);
111 return;
112 }
113
114 const nextPhase = this._getPhaseForMessage(message);
115 if (!nextPhase && !this._currentPhase) {
116 this._commitTextChange(Common.UIString('Lighthouse is warming up\u2026'));
117 clearTimeout(this._scheduledFastFactTimeout);
118 } else if (nextPhase && (!this._currentPhase || this._currentPhase.order < nextPhase.order)) {
119 this._currentPhase = nextPhase;
120 this._scheduleTextChange(this._getMessageForPhase(nextPhase));
121 this._scheduleFastFactCheck();
122 this._resetProgressBarClasses();
123 this._progressBar.classList.add(nextPhase.progressBarClass);
124 }
125 }
126
Patrick Hulcea087f622018-05-18 00:37:53 +0000127 _cancel() {
128 this._controller.dispatchEventToListeners(Audits2.Events.RequestAuditCancel);
129 }
130
Blink Reformat4c46d092018-04-07 15:32:37 +0000131 /**
Patrick Hulcea087f622018-05-18 00:37:53 +0000132 * @param {!Audits2.StatusView.StatusPhases} phase
Blink Reformat4c46d092018-04-07 15:32:37 +0000133 * @return {string}
134 */
135 _getMessageForPhase(phase) {
136 if (phase.message)
137 return Common.UIString(phase.message);
138
Patrick Hulcea087f622018-05-18 00:37:53 +0000139 const deviceType = Audits2.RuntimeSettings.find(item => item.setting.name === 'audits2.device_type').setting.get();
140 const throttling = Audits2.RuntimeSettings.find(item => item.setting.name === 'audits2.throttling').setting.get();
141 const match = Audits2.StatusView.LoadingMessages.find(item => {
Blink Reformat4c46d092018-04-07 15:32:37 +0000142 return item.deviceType === deviceType && item.throttling === throttling;
143 });
144
145 return match ? ls`${match.message}` : ls`Lighthouse is loading your page`;
146 }
147
148 /**
149 * @param {string} message
Patrick Hulcea087f622018-05-18 00:37:53 +0000150 * @return {?Audits2.StatusView.StatusPhases}
Blink Reformat4c46d092018-04-07 15:32:37 +0000151 */
152 _getPhaseForMessage(message) {
Patrick Hulcea087f622018-05-18 00:37:53 +0000153 return Audits2.StatusView.StatusPhases.find(phase => message.startsWith(phase.statusMessagePrefix));
Blink Reformat4c46d092018-04-07 15:32:37 +0000154 }
155
156 _resetProgressBarClasses() {
157 if (!this._progressBar)
158 return;
159
160 this._progressBar.className = 'audits2-progress-bar';
161 }
162
163 _scheduleFastFactCheck() {
164 if (!this._currentPhase || this._scheduledFastFactTimeout)
165 return;
166
167 this._scheduledFastFactTimeout = setTimeout(() => {
168 this._updateFastFactIfNecessary();
169 this._scheduledFastFactTimeout = null;
170
171 this._scheduleFastFactCheck();
172 }, 100);
173 }
174
175 _updateFastFactIfNecessary() {
176 const now = performance.now();
Patrick Hulcea087f622018-05-18 00:37:53 +0000177 if (now - this._textChangedAt < Audits2.StatusView.fastFactRotationInterval)
Blink Reformat4c46d092018-04-07 15:32:37 +0000178 return;
179 if (!this._fastFactsQueued.length)
180 return;
181
182 const fastFactIndex = Math.floor(Math.random() * this._fastFactsQueued.length);
183 this._scheduleTextChange(ls`\ud83d\udca1 ${this._fastFactsQueued[fastFactIndex]}`);
184 this._fastFactsQueued.splice(fastFactIndex, 1);
185 }
186
187 /**
188 * @param {string} text
189 */
190 _commitTextChange(text) {
191 if (!this._statusText)
192 return;
193 this._textChangedAt = performance.now();
194 this._statusText.textContent = text;
195 }
196
197 /**
198 * @param {string} text
199 */
200 _scheduleTextChange(text) {
201 if (this._scheduledTextChangeTimeout)
202 clearTimeout(this._scheduledTextChangeTimeout);
203
204 const msSinceLastChange = performance.now() - this._textChangedAt;
Patrick Hulcea087f622018-05-18 00:37:53 +0000205 const msToTextChange = Audits2.StatusView.minimumTextVisibilityDuration - msSinceLastChange;
Blink Reformat4c46d092018-04-07 15:32:37 +0000206
207 this._scheduledTextChangeTimeout = setTimeout(() => {
208 this._commitTextChange(text);
209 }, Math.max(msToTextChange, 0));
210 }
211
212 /**
213 * @param {!Error} err
Blink Reformat4c46d092018-04-07 15:32:37 +0000214 */
Patrick Hulcea087f622018-05-18 00:37:53 +0000215 renderBugReport(err) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000216 console.error(err);
217 clearTimeout(this._scheduledFastFactTimeout);
218 clearTimeout(this._scheduledTextChangeTimeout);
219 this._resetProgressBarClasses();
220 this._progressBar.classList.add('errored');
221
222 this._commitTextChange('');
Paul Irishf2659072019-03-24 19:08:52 +0000223 this._statusText.createChild('p').createTextChild(Common.UIString('Ah, sorry! We ran into an error.'));
Patrick Hulcea087f622018-05-18 00:37:53 +0000224 if (Audits2.StatusView.KnownBugPatterns.some(pattern => pattern.test(err.message))) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000225 const message = Common.UIString(
226 'Try to navigate to the URL in a fresh Chrome profile without any other tabs or ' +
227 'extensions open and try again.');
228 this._statusText.createChild('p').createTextChild(message);
229 } else {
Paul Irishf2659072019-03-24 19:08:52 +0000230 this._renderBugReportBody(err, this._inspectedURL);
Blink Reformat4c46d092018-04-07 15:32:37 +0000231 }
232 }
233
234 /**
235 * @param {!Error} err
236 * @param {string} auditURL
237 */
Paul Irishf2659072019-03-24 19:08:52 +0000238 _renderBugReportBody(err, auditURL) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000239 const issueBody = `
Paul Irishf2659072019-03-24 19:08:52 +0000240${err.message}
Blink Reformat4c46d092018-04-07 15:32:37 +0000241\`\`\`
Paul Irishf2659072019-03-24 19:08:52 +0000242Channel: DevTools
243Initial URL: ${auditURL}
244Chrome Version: ${navigator.userAgent.match(/Chrome\/(\S+)/)[1]}
245Stack Trace: ${err.stack}
Blink Reformat4c46d092018-04-07 15:32:37 +0000246\`\`\`
Paul Irishf2659072019-03-24 19:08:52 +0000247`;
248 this._statusText.createChild('p').createTextChild(
249 ls`If this issue is reproducible, please report it at the Lighthouse GitHub repo.`);
250 this._statusText.createChild('code', 'monospace').createTextChild(issueBody.trim());
Blink Reformat4c46d092018-04-07 15:32:37 +0000251 }
252};
253
254
255/** @type {!Array.<!RegExp>} */
Patrick Hulcea087f622018-05-18 00:37:53 +0000256Audits2.StatusView.KnownBugPatterns = [
Blink Reformat4c46d092018-04-07 15:32:37 +0000257 /PARSING_PROBLEM/,
258 /DOCUMENT_REQUEST/,
259 /READ_FAILED/,
260 /TRACING_ALREADY_STARTED/,
261 /^You must provide a url to the runner/,
262 /^You probably have multiple tabs open/,
263];
264
265/** @typedef {{message: string, progressBarClass: string, order: number}} */
Patrick Hulcea087f622018-05-18 00:37:53 +0000266Audits2.StatusView.StatusPhases = [
Blink Reformat4c46d092018-04-07 15:32:37 +0000267 {
268 id: 'loading',
269 progressBarClass: 'loading',
270 statusMessagePrefix: 'Loading page',
271 order: 10,
272 },
273 {
274 id: 'gathering',
275 progressBarClass: 'gathering',
276 message: 'Lighthouse is gathering information about the page to compute your score.',
277 statusMessagePrefix: 'Retrieving',
278 order: 20,
279 },
280 {
281 id: 'auditing',
282 progressBarClass: 'auditing',
Paul Irish5726a182018-08-30 17:04:23 +0000283 message: 'Almost there! Lighthouse is now generating your report.',
Blink Reformat4c46d092018-04-07 15:32:37 +0000284 statusMessagePrefix: 'Evaluating',
285 order: 30,
286 }
287];
288
289/** @typedef {{message: string, deviceType: string, throttling: string}} */
Patrick Hulcea087f622018-05-18 00:37:53 +0000290Audits2.StatusView.LoadingMessages = [
Blink Reformat4c46d092018-04-07 15:32:37 +0000291 {
292 deviceType: 'mobile',
293 throttling: 'on',
294 message: 'Lighthouse is loading your page with throttling to measure performance on a mobile device on 3G.',
295 },
296 {
297 deviceType: 'desktop',
298 throttling: 'on',
299 message: 'Lighthouse is loading your page with throttling to measure performance on a slow desktop on 3G.',
300 },
301 {
302 deviceType: 'mobile',
303 throttling: 'off',
304 message: 'Lighthouse is loading your page with mobile emulation.',
305 },
306 {
307 deviceType: 'desktop',
308 throttling: 'off',
309 message: 'Lighthouse is loading your page.',
310 },
311];
312
Patrick Hulcea087f622018-05-18 00:37:53 +0000313Audits2.StatusView.FastFacts = [
Blink Reformat4c46d092018-04-07 15:32:37 +0000314 '1MB takes a minimum of 5 seconds to download on a typical 3G connection [Source: WebPageTest and DevTools 3G definition].',
315 'Rebuilding Pinterest pages for performance increased conversion rates by 15% [Source: WPO Stats]',
316 'BBC has seen a loss of 10% of their users for every extra second of page load [Source: WPO Stats]',
317 'By reducing the response size of JSON needed for displaying comments, Instagram saw increased impressions [Source: WPO Stats]',
318 'Walmart saw a 1% increase in revenue for every 100ms improvement in page load [Source: WPO Stats]',
319 '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]',
320 '75% of global mobile users in 2016 were on 2G or 3G [Source: GSMA Mobile]',
321 'The average user device costs less than 200 USD. [Source: International Data Corporation]',
322 '53% of all site visits are abandoned if page load takes more than 3 seconds [Source: Google DoubleClick blog]',
323 '19 seconds is the average time a mobile web page takes to load on a 3G connection [Source: Google DoubleClick blog]',
324 '14 seconds is the average time a mobile web page takes to load on a 4G connection [Source: Google DoubleClick blog]',
325 '70% of mobile pages take nearly 7 seconds for the visual content above the fold to display on the screen. [Source: Think with Google]',
326 '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]',
327 'As the number of elements on a page increases from 400 to 6,000, the probability of conversion drops 95%. [Source: Think with Google]',
328 '70% of mobile pages weigh over 1MB, 36% over 2MB, and 12% over 4MB. [Source: Think with Google]',
329 'Lighthouse only simulates mobile performance; to measure performance on a real device, try WebPageTest.org [Source: Lighthouse team]',
330];
331
332/** @const */
Patrick Hulcea087f622018-05-18 00:37:53 +0000333Audits2.StatusView.fastFactRotationInterval = 6000;
Blink Reformat4c46d092018-04-07 15:32:37 +0000334/** @const */
Patrick Hulcea087f622018-05-18 00:37:53 +0000335Audits2.StatusView.minimumTextVisibilityDuration = 3000;