blob: c703bbb4c33aff358101abb916caf8979a17de55 [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
Paul Lewisdaac1062020-03-05 14:37:10 +00005import * as Common from '../common/common.js';
6import * as Components from '../components/components.js';
7import * as HostModule from '../host/host.js';
Simon Zünd2c704cd2020-06-04 11:08:35 +02008import * as Platform from '../platform/platform.js';
Paul Lewisdaac1062020-03-05 14:37:10 +00009import * as SDK from '../sdk/sdk.js';
10import * as Timeline from '../timeline/timeline.js';
11import * as UI from '../ui/ui.js';
Tim van der Lippe696c9262020-08-26 15:39:32 +010012import * as Workspace from '../workspace/workspace.js';
Paul Lewisdaac1062020-03-05 14:37:10 +000013
Paul Lewiscf2ef222019-11-22 14:55:35 +000014const MaxLengthForLinks = 40;
15
Patrick Hulcea087f622018-05-18 00:37:53 +000016/**
17 * @override
18 */
Connor Clark2bc3be22020-02-14 14:34:19 -080019export class LighthouseReportRenderer extends ReportRenderer {
Patrick Hulcea087f622018-05-18 00:37:53 +000020 /**
Paul Irish8f1e33d2018-05-31 02:29:50 +000021 * @param {!Element} el Parent element to render the report into.
22 * @param {!ReportRenderer.RunnerResultArtifacts=} artifacts
Patrick Hulcea087f622018-05-18 00:37:53 +000023 */
Paul Irish8f1e33d2018-05-31 02:29:50 +000024 static addViewTraceButton(el, artifacts) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000025 if (!artifacts || !artifacts.traces || !artifacts.traces.defaultPass) {
Paul Irish8f1e33d2018-05-31 02:29:50 +000026 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000027 }
Patrick Hulcea087f622018-05-18 00:37:53 +000028
Adam Rainef46d1582020-08-17 20:24:32 +000029 const simulated = artifacts.settings.throttlingMethod === 'simulate';
cjamcl@google.com21d2d222019-08-09 01:58:17 +000030 const container = el.querySelector('.lh-audit-group');
Paul Irishbf204682020-05-13 16:11:37 -070031 const disclaimerEl = container.querySelector('.lh-metrics__disclaimer');
32 // If it was a PWA-only run, we'd have a trace but no perf category to add the button to
33 if (!disclaimerEl) {
cjamcl@google.com21d2d222019-08-09 01:58:17 +000034 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000035 }
cjamcl@google.com21d2d222019-08-09 01:58:17 +000036
Paul Irish8f1e33d2018-05-31 02:29:50 +000037 const defaultPassTrace = artifacts.traces.defaultPass;
Adam Rainef46d1582020-08-17 20:24:32 +000038 const label = simulated ? Common.UIString.UIString('View Original Trace') : Common.UIString.UIString('View Trace');
39 const timelineButton = UI.UIUtils.createTextButton(label, onViewTraceClick, 'view-trace');
40 if (simulated) {
41 timelineButton.title = Common.UIString.UIString(
42 '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.');
43 }
Paul Irishbf204682020-05-13 16:11:37 -070044 container.insertBefore(timelineButton, disclaimerEl.nextSibling);
Paul Irish8f1e33d2018-05-31 02:29:50 +000045
46 async function onViewTraceClick() {
Paul Lewisdaac1062020-03-05 14:37:10 +000047 HostModule.userMetrics.actionTaken(Host.UserMetrics.Action.LighthouseViewTrace);
Paul Lewis0a7c6b62020-01-23 16:16:22 +000048 await self.UI.inspectorView.showPanel('timeline');
Paul Lewisdaac1062020-03-05 14:37:10 +000049 Timeline.TimelinePanel.TimelinePanel.instance().loadFromEvents(defaultPassTrace.traceEvents);
Paul Irish8f1e33d2018-05-31 02:29:50 +000050 }
Patrick Hulcea087f622018-05-18 00:37:53 +000051 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000052
53 /**
54 * @param {!Element} el
55 */
56 static async linkifyNodeDetails(el) {
Paul Lewisdaac1062020-03-05 14:37:10 +000057 const mainTarget = SDK.SDKModel.TargetManager.instance().mainTarget();
58 const domModel = mainTarget.model(SDK.DOMModel.DOMModel);
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000059
60 for (const origElement of el.getElementsByClassName('lh-node')) {
61 /** @type {!DetailsRenderer.NodeDetailsJSON} */
62 const detailsItem = origElement.dataset;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000063 if (!detailsItem.path) {
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +000064 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000065 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000066
67 const nodeId = await domModel.pushNodeByPathToFrontend(detailsItem.path);
68
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000069 if (!nodeId) {
cjamcl@google.com5c46b7e2018-12-04 14:44:47 +000070 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000071 }
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000072 const node = domModel.nodeForId(nodeId);
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +000073 if (!node) {
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
Paul Lewisdaac1062020-03-05 14:37:10 +000077 const element = await Common.Linkifier.Linkifier.linkify(node, {tooltip: detailsItem.snippet});
cjamcl@google.comda0e4c62018-11-27 23:52:10 +000078 origElement.title = '';
79 origElement.textContent = '';
80 origElement.appendChild(element);
81 }
82 }
cjamcl@google.comf2f8c092019-05-30 22:01:56 +000083
84 /**
85 * @param {!Element} el
86 */
Connor Clark0403a422019-11-18 18:03:18 -080087 static async linkifySourceLocationDetails(el) {
88 for (const origElement of el.getElementsByClassName('lh-source-location')) {
89 /** @type {!DetailsRenderer.SourceLocationDetailsJSON} */
90 const detailsItem = origElement.dataset;
91 if (!detailsItem.sourceUrl || !detailsItem.sourceLine || !detailsItem.sourceColumn) {
92 continue;
93 }
94 const url = detailsItem.sourceUrl;
95 const line = Number(detailsItem.sourceLine);
96 const column = Number(detailsItem.sourceColumn);
Paul Lewisdaac1062020-03-05 14:37:10 +000097 const element = await Components.Linkifier.Linkifier.linkifyURL(
98 url, {lineNumber: line, column, maxLength: MaxLengthForLinks});
Connor Clark0403a422019-11-18 18:03:18 -080099 origElement.title = '';
100 origElement.textContent = '';
101 origElement.appendChild(element);
102 }
103 }
104
105 /**
106 * @param {!Element} el
107 */
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000108 static handleDarkMode(el) {
Paul Lewis93d8e2c2020-01-24 16:34:55 +0000109 if (self.UI.themeSupport.themeName() === 'dark') {
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000110 el.classList.add('dark');
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000111 }
cjamcl@google.comf2f8c092019-05-30 22:01:56 +0000112 }
Paul Lewiscf2ef222019-11-22 14:55:35 +0000113}
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000114
115/**
116 * @override
117 */
Connor Clark2bc3be22020-02-14 14:34:19 -0800118export class LighthouseReportUIFeatures extends ReportUIFeatures {
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000119 /**
Connor Clark99508362019-08-20 19:52:23 +0000120 * @param {!DOM} dom
121 */
122 constructor(dom) {
123 super(dom);
124 this._beforePrint = null;
125 this._afterPrint = null;
126 }
127
128 /**
129 * @param {?function()} beforePrint
130 */
131 setBeforePrint(beforePrint) {
132 this._beforePrint = beforePrint;
133 }
134
135 /**
136 * @param {?function()} afterPrint
137 */
138 setAfterPrint(afterPrint) {
139 this._afterPrint = afterPrint;
140 }
141
142 /**
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000143 * Returns the html that recreates this report.
144 * @return {string}
145 * @protected
146 */
147 getReportHtml() {
148 this.resetUIState();
149 return Lighthouse.ReportGenerator.generateReportHtml(this.json);
150 }
151
152 /**
153 * Downloads a file (blob) using the system dialog prompt.
154 * @param {!Blob|!File} blob The file to save.
155 */
156 async _saveFile(blob) {
Paul Lewisdaac1062020-03-05 14:37:10 +0000157 const domain = new Common.ParsedURL.ParsedURL(this.json.finalUrl).domain();
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000158 const sanitizedDomain = domain.replace(/[^a-z0-9.-]+/gi, '_');
Simon Zünd2c704cd2020-06-04 11:08:35 +0200159 const timestamp = Platform.DateUtilities.toISO8601Compact(new Date(this.json.fetchTime));
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000160 const ext = blob.type.match('json') ? '.json' : '.html';
161 const basename = `${sanitizedDomain}-${timestamp}${ext}`;
162 const text = await blob.text();
Tim van der Lippe696c9262020-08-26 15:39:32 +0100163 Workspace.FileManager.FileManager.instance().save(basename, text, true /* forceSaveAs */);
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000164 }
165
166 async _print() {
167 const document = this.getDocument();
168 const clonedReport = document.querySelector('.lh-root').cloneNode(true /* deep */);
169 const printWindow = window.open('', '_blank', 'channelmode=1,status=1,resizable=1');
170 const style = printWindow.document.createElement('style');
Tim van der Lippe6d51bf02020-03-18 12:15:14 +0000171 style.textContent = self.Runtime.cachedResources['third_party/lighthouse/report-assets/report.css'];
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000172 printWindow.document.head.appendChild(style);
173 printWindow.document.body.replaceWith(clonedReport);
174 // Linkified nodes are shadow elements, which aren't exposed via `cloneNode`.
Connor Clark2bc3be22020-02-14 14:34:19 -0800175 await LighthouseReportRenderer.linkifyNodeDetails(clonedReport);
Connor Clark99508362019-08-20 19:52:23 +0000176
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000177 if (this._beforePrint) {
Connor Clark99508362019-08-20 19:52:23 +0000178 this._beforePrint();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000179 }
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000180 printWindow.focus();
181 printWindow.print();
182 printWindow.close();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000183 if (this._afterPrint) {
Connor Clark99508362019-08-20 19:52:23 +0000184 this._afterPrint();
Tim van der Lippe1d6e57a2019-09-30 11:55:34 +0000185 }
cjamcl@google.comc5214af2019-06-25 20:31:21 +0000186 }
187
188 /**
189 * @suppress {visibility}
190 * @return {!Document}
191 */
192 getDocument() {
193 return this._document;
194 }
195
196 /**
197 * @suppress {visibility}
198 */
199 resetUIState() {
200 this._resetUIState();
201 }
Paul Lewiscf2ef222019-11-22 14:55:35 +0000202}