Skip to content

Commit

Permalink
[INTERNAL] PDFViewer now displays a message using sap.m.IllustratedMe…
Browse files Browse the repository at this point in the history
…ssage when the property isTrustedSource is set to 'false'

If isTrustedSource is set to true, the PDFViewer opens with the displayType set to "Embedded" on desktop devices, which means that the PDF content is directly shown within the PDFViewer. Set this property to true only when the PDF is generated by the application or the PDF is scanned for viruses.

If isTrustedSource is set to false, the PDFViewer opens with the displayType set to "Link" on desktop devices, which overrides any configuration that has been provided by the application for the property displayType. This means that the PDFViewer appears as a toolbar with a download button.

PDF's will continue to be displayed by default if the property is not set. However, in future releases, we plan to change this default setting. If not configured manually, PDFViewer will open with displayType set to "Link"

SNOW: DINC0114788
CR-Id: 002075125800001148692024
Change-Id: I49e954bbcc69ab7516722116f9785589d658acac
(cherry picked from commit 5031852)
  • Loading branch information
AshwinKumarBolar committed May 7, 2024
1 parent db8c993 commit d6a9a0d
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 38 deletions.
29 changes: 12 additions & 17 deletions src/sap.m/src/sap/m/PDFViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ sap.ui.define([
displayType: {type: "sap.m.PDFViewerDisplayType", group: "Misc", defaultValue: PDFViewerDisplayType.Auto},
/**
* Parameter to determine if the given PDF is from a trusted source. If the source is valid this property can be set to true.
* If isTrustedSource is set to true, the PDFViewer opens with the displayType set to "Embedded" on desktop devices, which means that the PDF content is directly shown within the PDFViewer. Set this property to true only when the PDF is generated by the application or the PDF is scanned for viruses.
* If isTrustedSource is set to false, the PDFViewer opens with the displayType set to "Link" on desktop devices, which overrides any configuration that has been provided by the application for the property displayType. This means that the PDFViewer appears as a toolbar with a download button.
* If isTrustedSource is set to true, the PDFViewer opens with the displayType set to "Embedded" on desktop devices. This means that the PDF content is directly shown within the PDFViewer. Set this property to true only when the PDF is generated by the application or the PDF is scanned for viruses.
* If isTrustedSource is set to false, the PDFViewer opens with the displayType set to "Link" on desktop devices. This means that any configuration that has been provided by the application for the property displayType is overridden. In this case, the user would need to download the PDF to view its content.
*/
isTrustedSource: {type: "boolean", group: "Misc", defaultValue: true}
isTrustedSource: {type: "boolean", group: "Misc", defaultValue: false}
},
aggregations: {
/**
Expand All @@ -144,7 +144,12 @@ sap.ui.define([
* A multiple aggregation for buttons that can be added to the footer of the popup
* dialog. Works only if the PDF viewer is set to open in a popup dialog.
*/
popupButtons: {type: "sap.m.Button", multiple: true, singularName: "popupButton"}
popupButtons: {type: "sap.m.Button", multiple: true, singularName: "popupButton"},
/**
* A message page is displayed when the property isTrustedSource = false
* @private
*/
_nonTrustedMessagePage: { type: "sap.m.MessagePage", multiple: false, visibility: "hidden" }
},
events: {
/**
Expand Down Expand Up @@ -192,7 +197,7 @@ sap.ui.define([

this._initPopupControl();
this._initPopupDownloadButtonControl();
this._initPlaceholderMessagePageControl();
this._initErrorPlaceholderMessagePageControl();
this._initToolbarDownloadButtonControl();
this._initOverflowToolbarControl();

Expand Down Expand Up @@ -235,15 +240,11 @@ sap.ui.define([
};

PDFViewer.prototype.onBeforeRendering = function () {
// IE things
// IE things
// because of the detecting error state in IE (double call of unload listener)
// it is important to reset the flag before each render
// otherwise it wrongly detects error state (the unload listener is called once even in valid use case)
this._bOnBeforeUnloadFired = false;
if (!this.getIsTrustedSource() && !this._isDisplayTypeLink()) {
this.sInitialDisplayType = this.getDisplayType();
this.setProperty("displayType", PDFViewerDisplayType.Link, true);
}
};

/**
Expand All @@ -252,9 +253,6 @@ sap.ui.define([
* @private
*/
PDFViewer.prototype.onAfterRendering = function () {
if (this.sInitialDisplayType) {
this.setProperty("displayType", this.sInitialDisplayType, true);
}
var fnInitIframeElement = function () {
// cant use attachBrowserEvent because it attach event to component root node (this.$())
// load event does not bubble so it has to be bind directly to iframe element
Expand Down Expand Up @@ -520,17 +518,14 @@ sap.ui.define([
* @public
*/
PDFViewer.prototype.open = function () {
if (!this.getIsTrustedSource() && !this._isDisplayTypeLink()) {
this.setProperty("displayType", PDFViewerDisplayType.Link, true);
}
if (!this._isSourceValidToDisplay()) {
assert(false, "The PDF file cannot be opened with the given source. Given source: " + this.getSource());
return;
} else if (!PDFViewerRenderer._isPdfPluginEnabled()) {
Log.warning("The PDF plug-in is not available on this device.");
}

if (this._isEmbeddedModeAllowed()) {
if (this._isEmbeddedModeAllowed() && this.getIsTrustedSource()) {
this._openOnDesktop();
} else {
this._openOnMobile();
Expand Down
22 changes: 19 additions & 3 deletions src/sap.m/src/sap/m/PDFViewerRenderManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ sap.ui.define([
}
};

PDFViewer.prototype._initPlaceholderMessagePageControl = function () {
PDFViewer.prototype._initErrorPlaceholderMessagePageControl = function () {
var that = this,
sPlaceholderMessagePageFactoryFunctionName = "getPlaceholderMessagePageControl";
sPlaceholderMessagePageFactoryFunctionName = "getErrorPlaceholderMessagePageControl";

this._objectsRegister[sPlaceholderMessagePageFactoryFunctionName] = function () {
var oMessagePage = new MessagePage({
Expand All @@ -129,7 +129,6 @@ sap.ui.define([

that._objectsRegister[sPlaceholderMessagePageFactoryFunctionName] = function () {
oMessagePage.setText(that._getMessagePageErrorMessage());

return oMessagePage;
};

Expand Down Expand Up @@ -235,6 +234,23 @@ sap.ui.define([
};

};

PDFViewer.prototype._getNonTrustedSourceMessage = function () {
var oButtonContent = this._objectsRegister.getPopupDownloadButtonControl(),
oMessagePage = this.getAggregation("_nonTrustedMessagePage");

if (!oMessagePage) {
oMessagePage = new MessagePage({
showHeader: false,
text: this._getLibraryResourceBundle().getText("PDF_VIEWER_NONTRUSTEDSOURCEMESSAGE_TITLE"),
icon: "sap-icon://download",
buttons: [oButtonContent],
description: ""
});
this.setAggregation("_nonTrustedMessagePage", oMessagePage);
}
return oMessagePage;
};
}
};

Expand Down
30 changes: 22 additions & 8 deletions src/sap.m/src/sap/m/PDFViewerRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,20 @@ sap.ui.define(['sap/ui/Device', "sap/base/Log"],
}

/**
* if displayType is not link and pdfPlugin is not enabled .. render error content.
* case: if "Always download pdf's" option is enabled in browser setting.. in that
* case display error content (to retain control behaviour)
* Case1: If display is in Embedded Mode, PDF Plugin is disabled and is Desktop Device, We render Error Content.
* Case2: If display is in Embedded Mode and PDF Plugin is enabled, We render PDF Content.
* Case3: If display is in Embedded Mode, PDF Plugin is enabled and isTrustedSource = false, We render NonTrustedSource Content.
*/
if (!oControl._isDisplayTypeLink() && !this._isPdfPluginEnabled() && Device.system.desktop) {
this.renderErrorContent(oRm, oControl);
} else if (oControl._isEmbeddedModeAllowed() && this._isPdfPluginEnabled()) {
this.renderPdfContent(oRm, oControl);
var bRenderEmbededMode = (oControl._isDisplayTypeEmbedded() || oControl._isDisplayTypeAuto()) && Device.system.desktop;

if (bRenderEmbededMode) {
if (!this._isPdfPluginEnabled()) {
this.renderErrorContent(oRm, oControl);
} else if (!oControl.getIsTrustedSource()) {
this.renderNonTrustedSourceContent(oRm, oControl);
} else {
this.renderPdfContent(oRm, oControl);
}
}

oRm.write("</div>");
Expand Down Expand Up @@ -135,7 +141,7 @@ sap.ui.define(['sap/ui/Device', "sap/base/Log"],

PDFViewerRenderer.renderErrorContent = function (oRm, oControl) {
var oErrorContent = oControl.getErrorPlaceholder() ? oControl.getErrorPlaceholder() :
oControl._objectsRegister.getPlaceholderMessagePageControl();
oControl._objectsRegister.getErrorPlaceholderMessagePageControl();

oRm.write("<div");
oRm.addClass("sapMPDFViewerError");
Expand All @@ -153,5 +159,13 @@ sap.ui.define(['sap/ui/Device', "sap/base/Log"],
}
};

PDFViewerRenderer.renderNonTrustedSourceContent = function (oRm, oControl) {
oRm.openStart("div");
oRm.class("sapMPDFViewerNonTrustedMessage");
oRm.openEnd();
oRm.renderControl(oControl._getNonTrustedSourceMessage());
oRm.close("div");
};

return PDFViewerRenderer;
}, /* bExport= */ true);
3 changes: 3 additions & 0 deletions src/sap.m/src/sap/m/messagebundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1552,6 +1552,9 @@ PDF_VIEWER_SOURCE_VALIDATION_MESSAGE_HEADER=Source Validation Error
#XMSG: PDF viewer's message box text for source validation error
PDF_VIEWER_SOURCE_VALIDATION_MESSAGE_TEXT=Could not validate the source of the file. Show the file anyway?

#XMSG: PDF viewer's non trusted source Message Text
PDF_VIEWER_NONTRUSTEDSOURCEMESSAGE_TITLE=Download the file to view it

#XBUT: MultiEditField prefilled Item keep
MULTI_EDIT_KEEP_TEXT=Keep Existing Values

Expand Down
5 changes: 5 additions & 0 deletions src/sap.m/src/sap/m/themes/base/PDFViewer.less
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@

.sapMPDFViewerError.sapMPDFViewerReducedContent {
height: calc(~'100% - 3.5rem');
}

.sapMPDFViewerNonTrustedMessage {
width: 100%;
height: 100%;
}
43 changes: 33 additions & 10 deletions src/sap.m/test/sap/m/qunit/PDFViewer.static.qunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
sap.ui.define([
"./PDFViewerTestUtils",
"sap/m/PDFViewerRenderer",
"sap/ui/Device"
], function (TestUtils, PDFViewerRenderer, Device) {
"sap/ui/core/Core"
], function (TestUtils, PDFViewerRenderer, Core) {
"use strict";

var oPdfViewer = null;
Expand Down Expand Up @@ -133,8 +133,7 @@ sap.ui.define([
TestUtils.renderPdfViewer(oPdfViewer);
});

QUnit.test("Test proeprty:isTrustedSource = false", function (assert) {
assert.expect(3);
QUnit.test("Test property:isTrustedSource = false", function (assert) {
var loadDone = assert.async();

var oOptions = {
Expand All @@ -144,16 +143,40 @@ sap.ui.define([
};

oPdfViewer = TestUtils.createPdfViewer(oOptions);
oPdfViewer.addEventDelegate({
onBeforeRendering: function() {
assert.equal(oPdfViewer.getDisplayType(), "Link", "displayType changed to Link");
},
var oEventDelegate = {
onAfterRendering: function() {
assert.equal(oPdfViewer.getDisplayType(), "Embedded", "displayType is set to Embedded");
assert.ok(oPdfViewer.getDomRef().querySelector(".sapMPDFViewerNonTrustedMessage"), "NonTrustedSource Class is present");
assert.ok(Core.byId(oPdfViewer.getDomRef().querySelector(".sapMPDFViewerNonTrustedMessage").children[0].id).isA("sap.m.MessagePage"), "Message is created");
assert.ok(oPdfViewer.getDomRef().querySelectorAll("#" + oPdfViewer.getId() + "-toolbarDownloadButton").length === 1, "Download button is displayed in Link mode");
oPdfViewer.removeEventDelegate(oEventDelegate);
loadDone();
}
};
oPdfViewer.addEventDelegate(oEventDelegate);
TestUtils.renderPdfViewer(oPdfViewer);
});

QUnit.test("Test property:isTrustedSource = false and displayType = Link", function (assert) {
var loadDone = assert.async();

var oOptions = {
"displayType": "Link",
"source": "test-resources/sap/m/qunit/pdfviewer/sample-file.pdf",
"isTrustedSource": false
};

oPdfViewer = TestUtils.createPdfViewer(oOptions);
var oEventDelegate = {
onAfterRendering: function() {
assert.equal(oPdfViewer.getDisplayType(), "Embedded", "displayType reset back to initial");
assert.equal(oPdfViewer.getDisplayType(), "Link", "displayType is set to Link");
assert.notOk(oPdfViewer.getDomRef().querySelector(".sapMPDFViewerNonTrustedMessage"), "NonTrustedSource Class is not created");
assert.ok(oPdfViewer.getDomRef().querySelectorAll("#" + oPdfViewer.getId() + "-toolbarDownloadButton").length === 1, "Download button is displayed in Link mode");
oPdfViewer.removeEventDelegate(oEventDelegate);
loadDone();
}
});
};
oPdfViewer.addEventDelegate(oEventDelegate);
TestUtils.renderPdfViewer(oPdfViewer);
});

Expand Down

0 comments on commit d6a9a0d

Please sign in to comment.