blob: 5d2d95f35cf29051ece25fc957cef7e534ea53c2 [file] [log] [blame]
Patrick Hulcea087f622018-05-18 00:37:53 +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
Tim van der Lippe132b4ba2020-09-02 16:00:48 +01005// @ts-nocheck
6// TODO(crbug.com/1011811): Enable TypeScript compiler checks
7
Paul Lewisdaac1062020-03-05 14:37:10 +00008import * as Common from '../common/common.js';
9import * as Components from '../components/components.js';
10import * as HostModule from '../host/host.js';
Simon Zünd2c704cd2020-06-04 11:08:35 +020011import * as Platform from '../platform/platform.js';
Paul Lewisdaac1062020-03-05 14:37:10 +000012import * as SDK from '../sdk/sdk.js';
Paul Lewisca569a52020-09-09 17:11:51 +010013import * as ThemeSupport from '../theme_support/theme_support.js';
Paul Lewisdaac1062020-03-05 14:37:10 +000014import * as Timeline from '../timeline/timeline.js';
15import * as UI from '../ui/ui.js';
Tim van der Lippe696c9262020-08-26 15:39:32 +010016import * as Workspace from '../workspace/workspace.js';
Paul Lewisdaac1062020-03-05 14:37:10 +000017
Paul Lewiscf2ef222019-11-22 14:55:35 +000018const MaxLengthForLinks = 40;
19
Patrick Hulcea087f622018-05-18 00:37:53 +000020/**
21 * @override
22 */
Connor Clark2bc3be22020-02-14 14:34:19 -080023export class LighthouseReportRenderer extends ReportRenderer {
Patrick Hulcea087f622018-05-18 00:37:53 +000024 /**
Paul Irish8f1e33d2018-05-31 02:29:50 +000025 * @param {!Element} el Parent element to render the report into.
26 * @param {!ReportRenderer.RunnerResultArtifacts=} artifacts
Patrick Hulcea087f622018-05-18 00:37:53 +000027 */
Paul Irish8f1e33d2018-05-31 02:29:50 +000028 static addViewTraceButton(el, artifacts) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000029 if (!artifacts || !artifacts.traces || !artifacts.traces.defaultPass) {
Paul Irish8f1e33d2018-05-31 02:29:50 +000030 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000031 }
Patrick Hulcea087f622018-05-18 00:37:53 +000032
Adam Rainef46d1582020-08-17 20:24:32 +000033 const simulated = artifacts.settings.throttlingMethod === 'simulate';
cjamcl@google.com21d2d222019-08-09 01:58:17 +000034 const container = el.querySelector('.lh-audit-group');
Paul Irishbf204682020-05-13 16:11:37 -070035 const disclaimerEl = container.querySelector('.lh-metrics__disclaimer');
36 // If it was a PWA-only run, we'd have a trace but no perf category to add the button to
37 if (!disclaimerEl) {
cjamcl@google.com21d2d222019-08-09 01:58:17 +000038 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000039 }
cjamcl@google.com21d2d222019-08-09 01:58:17 +000040
Paul Irish8f1e33d2018-05-31 02:29:50 +000041 const defaultPassTrace = artifacts.traces.defaultPass;
Adam Rainef46d1582020-08-17 20:24:32 +000042 const label = simulated ? Common.UIString.UIString('View Original Trace') : Common.UIString.UIString('View Trace');
43 const timelineButton = UI.UIUtils.createTextButton(label, onViewTraceClick, 'view-trace');
44 if (simulated) {
45 timelineButton.title = Common.UIString.UIString(
46 'The performance metrics above are simulated and won\'t match the timings found in this trace. Disable simulated throttling in "Lighthouse Settings" if you want the timings to match.');
47 }
Paul Irishbf204682020-05-13 16:11:37 -070048 container.insertBefore(timelineButton, disclaimerEl.nextSibling);
Paul Irish8f1e33d2018-05-31 02:29:50 +000049
50 async function onViewTraceClick() {
Paul Lewisdaac1062020-03-05 14:37:10 +000051 HostModule.userMetrics.actionTaken(Host.UserMetrics.Action.LighthouseViewTrace);
Tim van der Lippe80d82652020-08-27 14:53:44 +010052 await UI.InspectorView.InspectorView.instance().showPanel('timeline');
Paul Lewisdaac1062020-03-05 14:37:10 +000053 Timeline.TimelinePanel.TimelinePanel.instance().loadFromEvents(defaultPassTrace.traceEvents);
Paul Irish8f1e33d2018-05-31 02:29:50 +000054 }
Patrick Hulcea087f622018-05-18 00:37:53 +000055 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000056
57 /**
58 * @param {!Element} el
59 */
60 static async linkifyNodeDetails(el) {
Paul Lewisdaac1062020-03-05 14:37:10 +000061 const mainTarget = SDK.SDKModel.TargetManager.instance().mainTarget();
62 const domModel = mainTarget.model(SDK.DOMModel.DOMModel);
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000063
64 for (const origElement of el.getElementsByClassName('lh-node')) {
65 /** @type {!DetailsRenderer.NodeDetailsJSON} */
66 const detailsItem = origElement.dataset;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000067 if (!detailsItem.path) {
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +000068 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000069 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000070
71 const nodeId = await domModel.pushNodeByPathToFrontend(detailsItem.path);
72
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000073 if (!nodeId) {
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +000074 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000075 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000076 const node = domModel.nodeForId(nodeId);
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000077 if (!node) {
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +000078 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000079 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000080
Paul Lewisdaac1062020-03-05 14:37:10 +000081 const element = await Common.Linkifier.Linkifier.linkify(node, {tooltip: detailsItem.snippet});
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000082 origElement.title = '';
83 origElement.textContent = '';
84 origElement.appendChild(element);
85 }
86 }
cjamcl@google.comf2f8c092019-05-30 22:01:56 +000087
88 /**
89 * @param {!Element} el
90 */
Connor Clark0403a422019-11-18 18:03:18 -080091 static async linkifySourceLocationDetails(el) {
92 for (const origElement of el.getElementsByClassName('lh-source-location')) {
93 /** @type {!DetailsRenderer.SourceLocationDetailsJSON} */
94 const detailsItem = origElement.dataset;
95 if (!detailsItem.sourceUrl || !detailsItem.sourceLine || !detailsItem.sourceColumn) {
96 continue;
97 }
98 const url = detailsItem.sourceUrl;
99 const line = Number(detailsItem.sourceLine);
100 const column = Number(detailsItem.sourceColumn);
Paul Lewisdaac1062020-03-05 14:37:10 +0000101 const element = await Components.Linkifier.Linkifier.linkifyURL(
102 url, {lineNumber: line, column, maxLength: MaxLengthForLinks});
Connor Clark0403a422019-11-18 18:03:18 -0800103 origElement.title = '';
104 origElement.textContent = '';
105 origElement.appendChild(element);
106 }
107 }
108
109 /**
110 * @param {!Element} el
111 */
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000112 static handleDarkMode(el) {
Paul Lewisca569a52020-09-09 17:11:51 +0100113 if (ThemeSupport.ThemeSupport.instance().themeName() === 'dark') {
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000114 el.classList.add('dark');
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000115 }
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000116 }
Paul Lewiscf2ef222019-11-22 14:55:35 +0000117}
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000118
119/**
120 * @override
121 */
Connor Clark2bc3be22020-02-14 14:34:19 -0800122export class LighthouseReportUIFeatures extends ReportUIFeatures {
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000123 /**
Connor Clark99508362019-08-20 19:52:23 +0000124 * @param {!DOM} dom
125 */
126 constructor(dom) {
127 super(dom);
128 this._beforePrint = null;
129 this._afterPrint = null;
130 }
131
132 /**
133 * @param {?function()} beforePrint
134 */
135 setBeforePrint(beforePrint) {
136 this._beforePrint = beforePrint;
137 }
138
139 /**
140 * @param {?function()} afterPrint
141 */
142 setAfterPrint(afterPrint) {
143 this._afterPrint = afterPrint;
144 }
145
146 /**
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000147 * Returns the html that recreates this report.
148 * @return {string}
149 * @protected
150 */
151 getReportHtml() {
152 this.resetUIState();
153 return Lighthouse.ReportGenerator.generateReportHtml(this.json);
154 }
155
156 /**
157 * Downloads a file (blob) using the system dialog prompt.
158 * @param {!Blob|!File} blob The file to save.
159 */
160 async _saveFile(blob) {
Paul Lewisdaac1062020-03-05 14:37:10 +0000161 const domain = new Common.ParsedURL.ParsedURL(this.json.finalUrl).domain();
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000162 const sanitizedDomain = domain.replace(/[^a-z0-9.-]+/gi, '_');
Simon Zünd2c704cd2020-06-04 11:08:35 +0200163 const timestamp = Platform.DateUtilities.toISO8601Compact(new Date(this.json.fetchTime));
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000164 const ext = blob.type.match('json') ? '.json' : '.html';
165 const basename = `${sanitizedDomain}-${timestamp}${ext}`;
166 const text = await blob.text();
Tim van der Lippe696c9262020-08-26 15:39:32 +0100167 Workspace.FileManager.FileManager.instance().save(basename, text, true /* forceSaveAs */);
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000168 }
169
170 async _print() {
171 const document = this.getDocument();
172 const clonedReport = document.querySelector('.lh-root').cloneNode(true /* deep */);
173 const printWindow = window.open('', '_blank', 'channelmode=1,status=1,resizable=1');
174 const style = printWindow.document.createElement('style');
Tim van der Lippe6d51bf02020-03-18 12:15:14 +0000175 style.textContent = self.Runtime.cachedResources['third_party/lighthouse/report-assets/report.css'];
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000176 printWindow.document.head.appendChild(style);
177 printWindow.document.body.replaceWith(clonedReport);
178 // Linkified nodes are shadow elements, which aren't exposed via `cloneNode`.
Connor Clark2bc3be22020-02-14 14:34:19 -0800179 await LighthouseReportRenderer.linkifyNodeDetails(clonedReport);
Connor Clark99508362019-08-20 19:52:23 +0000180
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000181 if (this._beforePrint) {
Connor Clark99508362019-08-20 19:52:23 +0000182 this._beforePrint();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000183 }
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000184 printWindow.focus();
185 printWindow.print();
186 printWindow.close();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000187 if (this._afterPrint) {
Connor Clark99508362019-08-20 19:52:23 +0000188 this._afterPrint();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000189 }
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000190 }
191
192 /**
193 * @suppress {visibility}
194 * @return {!Document}
195 */
196 getDocument() {
197 return this._document;
198 }
199
200 /**
201 * @suppress {visibility}
202 */
203 resetUIState() {
204 this._resetUIState();
205 }
Paul Lewiscf2ef222019-11-22 14:55:35 +0000206}