blob: 3198f953edd4cedbde84d2038b62e5f0e3e4b710 [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('');
223 this._statusText.createTextChild(Common.UIString('Ah, sorry! We ran into an error: '));
224 this._statusText.createChild('em').createTextChild(err.message);
Patrick Hulcea087f622018-05-18 00:37:53 +0000225 if (Audits2.StatusView.KnownBugPatterns.some(pattern => pattern.test(err.message))) {
Blink Reformat4c46d092018-04-07 15:32:37 +0000226 const message = Common.UIString(
227 'Try to navigate to the URL in a fresh Chrome profile without any other tabs or ' +
228 'extensions open and try again.');
229 this._statusText.createChild('p').createTextChild(message);
230 } else {
Patrick Hulcea087f622018-05-18 00:37:53 +0000231 this._renderBugReportLink(err, this._inspectedURL);
Blink Reformat4c46d092018-04-07 15:32:37 +0000232 }
233 }
234
235 /**
236 * @param {!Error} err
237 * @param {string} auditURL
238 */
239 _renderBugReportLink(err, auditURL) {
240 const baseURI = 'https://github.com/GoogleChrome/lighthouse/issues/new?';
241 const title = encodeURI('title=DevTools Error: ' + err.message.substring(0, 60));
242
243 const issueBody = `
244**Initial URL**: ${auditURL}
245**Chrome Version**: ${navigator.userAgent.match(/Chrome\/(\S+)/)[1]}
246**Error Message**: ${err.message}
247**Stack Trace**:
248\`\`\`
249${err.stack}
250\`\`\`
251 `;
252 const body = '&body=' + encodeURIComponent(issueBody.trim());
253 const reportErrorEl = UI.XLink.create(
254 baseURI + title + body, Common.UIString('Report this bug'), 'audits2-link audits2-report-error');
255 this._statusText.appendChild(reportErrorEl);
256 }
257};
258
259
260/** @type {!Array.<!RegExp>} */
Patrick Hulcea087f622018-05-18 00:37:53 +0000261Audits2.StatusView.KnownBugPatterns = [
Blink Reformat4c46d092018-04-07 15:32:37 +0000262 /PARSING_PROBLEM/,
263 /DOCUMENT_REQUEST/,
264 /READ_FAILED/,
265 /TRACING_ALREADY_STARTED/,
266 /^You must provide a url to the runner/,
267 /^You probably have multiple tabs open/,
268];
269
270/** @typedef {{message: string, progressBarClass: string, order: number}} */
Patrick Hulcea087f622018-05-18 00:37:53 +0000271Audits2.StatusView.StatusPhases = [
Blink Reformat4c46d092018-04-07 15:32:37 +0000272 {
273 id: 'loading',
274 progressBarClass: 'loading',
275 statusMessagePrefix: 'Loading page',
276 order: 10,
277 },
278 {
279 id: 'gathering',
280 progressBarClass: 'gathering',
281 message: 'Lighthouse is gathering information about the page to compute your score.',
282 statusMessagePrefix: 'Retrieving',
283 order: 20,
284 },
285 {
286 id: 'auditing',
287 progressBarClass: 'auditing',
288 message: 'Almost there! Lighthouse is now generating your own special pretty report!',
289 statusMessagePrefix: 'Evaluating',
290 order: 30,
291 }
292];
293
294/** @typedef {{message: string, deviceType: string, throttling: string}} */
Patrick Hulcea087f622018-05-18 00:37:53 +0000295Audits2.StatusView.LoadingMessages = [
Blink Reformat4c46d092018-04-07 15:32:37 +0000296 {
297 deviceType: 'mobile',
298 throttling: 'on',
299 message: 'Lighthouse is loading your page with throttling to measure performance on a mobile device on 3G.',
300 },
301 {
302 deviceType: 'desktop',
303 throttling: 'on',
304 message: 'Lighthouse is loading your page with throttling to measure performance on a slow desktop on 3G.',
305 },
306 {
307 deviceType: 'mobile',
308 throttling: 'off',
309 message: 'Lighthouse is loading your page with mobile emulation.',
310 },
311 {
312 deviceType: 'desktop',
313 throttling: 'off',
314 message: 'Lighthouse is loading your page.',
315 },
316];
317
Patrick Hulcea087f622018-05-18 00:37:53 +0000318Audits2.StatusView.FastFacts = [
Blink Reformat4c46d092018-04-07 15:32:37 +0000319 '1MB takes a minimum of 5 seconds to download on a typical 3G connection [Source: WebPageTest and DevTools 3G definition].',
320 'Rebuilding Pinterest pages for performance increased conversion rates by 15% [Source: WPO Stats]',
321 'BBC has seen a loss of 10% of their users for every extra second of page load [Source: WPO Stats]',
322 'By reducing the response size of JSON needed for displaying comments, Instagram saw increased impressions [Source: WPO Stats]',
323 'Walmart saw a 1% increase in revenue for every 100ms improvement in page load [Source: WPO Stats]',
324 '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]',
325 '75% of global mobile users in 2016 were on 2G or 3G [Source: GSMA Mobile]',
326 'The average user device costs less than 200 USD. [Source: International Data Corporation]',
327 '53% of all site visits are abandoned if page load takes more than 3 seconds [Source: Google DoubleClick blog]',
328 '19 seconds is the average time a mobile web page takes to load on a 3G connection [Source: Google DoubleClick blog]',
329 '14 seconds is the average time a mobile web page takes to load on a 4G connection [Source: Google DoubleClick blog]',
330 '70% of mobile pages take nearly 7 seconds for the visual content above the fold to display on the screen. [Source: Think with Google]',
331 '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]',
332 'As the number of elements on a page increases from 400 to 6,000, the probability of conversion drops 95%. [Source: Think with Google]',
333 '70% of mobile pages weigh over 1MB, 36% over 2MB, and 12% over 4MB. [Source: Think with Google]',
334 'Lighthouse only simulates mobile performance; to measure performance on a real device, try WebPageTest.org [Source: Lighthouse team]',
335];
336
337/** @const */
Patrick Hulcea087f622018-05-18 00:37:53 +0000338Audits2.StatusView.fastFactRotationInterval = 6000;
Blink Reformat4c46d092018-04-07 15:32:37 +0000339/** @const */
Patrick Hulcea087f622018-05-18 00:37:53 +0000340Audits2.StatusView.minimumTextVisibilityDuration = 3000;