Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/simplify the "delayedFallback" handling in the default viewer #12056

Merged
merged 2 commits into from
Jul 8, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 73 additions & 98 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,51 @@ const ViewOnLoad = {
INITIAL: 1,
};

// Keep these in sync with mozilla-central's Histograms.json.
const KNOWN_VERSIONS = [
"1.0",
"1.1",
"1.2",
"1.3",
"1.4",
"1.5",
"1.6",
"1.7",
"1.8",
"1.9",
"2.0",
"2.1",
"2.2",
"2.3",
];
// Keep these in sync with mozilla-central's Histograms.json.
const KNOWN_GENERATORS = [
"acrobat distiller",
"acrobat pdfwriter",
"adobe livecycle",
"adobe pdf library",
"adobe photoshop",
"ghostscript",
"tcpdf",
"cairo",
"dvipdfm",
"dvips",
"pdftex",
"pdfkit",
"itext",
"prince",
"quarkxpress",
"mac os x",
"microsoft",
"openoffice",
"oracle",
"luradocument",
"pdf-xchange",
"antenna house",
"aspose.cells",
"fpdf",
];

class DefaultExternalServices {
constructor() {
throw new Error("Cannot initialize DefaultExternalServices.");
Expand Down Expand Up @@ -189,8 +234,7 @@ const PDFViewerApplication = {
externalServices: DefaultExternalServices,
_boundEvents: {},
contentDispositionFilename: null,
_hasInteracted: false,
_delayedFallbackFeatureIds: [],
triggerDelayedFallback: null,

// Called once when the document is loaded.
async initialize(appConfig) {
Expand Down Expand Up @@ -690,6 +734,7 @@ const PDFViewerApplication = {
this.url = "";
this.baseUrl = "";
this.contentDispositionFilename = null;
this.triggerDelayedFallback = null;

this.pdfSidebar.reset();
this.pdfOutlineViewer.reset();
Expand Down Expand Up @@ -864,38 +909,38 @@ const PDFViewerApplication = {
.catch(downloadByUrl); // Error occurred, try downloading with the URL.
},

_recordFallbackErrorTelemetry(featureId) {
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("MOZCENTRAL")) {
/**
* For PDF documents that contain e.g. forms and javaScript, we should only
* trigger the fallback bar once the user has interacted with the page.
* @private
*/
_delayedFallback(featureId) {
// Ensure that telemetry is always reported, since it's not guaranteed
// that the fallback bar will be shown (depends on user interaction).
this.externalServices.reportTelemetry({
type: "unsupportedFeature",
featureId,
});

if (!this.triggerDelayedFallback) {
this.triggerDelayedFallback = () => {
this.fallback(featureId);
this.triggerDelayedFallback = null;
};
}
},

fallback(featureId) {
if (
typeof PDFJSDev === "undefined" ||
PDFJSDev.test("MOZCENTRAL || GENERIC")
) {
if (featureId) {
this._recordFallbackErrorTelemetry(featureId);
}

// For PDFs that contain script and form errors, we should only trigger
// the fallback once the user has interacted with the page.
if (this._delayedFallbackFeatureIds.length >= 1 && this._hasInteracted) {
featureId = this._delayedFallbackFeatureIds[0];
// Reset to prevent all click events from showing fallback bar.
this._delayedFallbackFeatureIds = [];
}
this.externalServices.reportTelemetry({
type: "unsupportedFeature",
featureId,
});

// Only trigger the fallback once so we don't spam the user with messages
// for one PDF.
if (this.fellback) {
return;
}

this.fellback = true;
this.externalServices.fallback(
{
Expand All @@ -909,7 +954,6 @@ const PDFViewerApplication = {
PDFViewerApplication.download();
}
);
}
},

/**
Expand Down Expand Up @@ -1259,8 +1303,7 @@ const PDFViewerApplication = {
return false;
}
console.warn("Warning: JavaScript is not supported");
this._delayedFallbackFeatureIds.push(UNSUPPORTED_FEATURES.javaScript);
this._recordFallbackErrorTelemetry(UNSUPPORTED_FEATURES.javaScript);
this._delayedFallback(UNSUPPORTED_FEATURES.javaScript);
return true;
});

Expand Down Expand Up @@ -1311,7 +1354,6 @@ const PDFViewerApplication = {
);

let pdfTitle;

const infoTitle = info && info.Title;
if (infoTitle) {
pdfTitle = infoTitle;
Expand All @@ -1331,7 +1373,6 @@ const PDFViewerApplication = {
pdfTitle = metadataTitle;
}
}

if (pdfTitle) {
this.setTitle(
`${pdfTitle} - ${contentDispositionFilename || document.title}`
Expand All @@ -1342,65 +1383,15 @@ const PDFViewerApplication = {

if (info.IsAcroFormPresent) {
console.warn("Warning: AcroForm/XFA is not supported");
this._delayedFallbackFeatureIds.push(UNSUPPORTED_FEATURES.forms);
this._recordFallbackErrorTelemetry(UNSUPPORTED_FEATURES.forms);
this._delayedFallback(UNSUPPORTED_FEATURES.forms);
}

if (
typeof PDFJSDev === "undefined" ||
PDFJSDev.test("MOZCENTRAL || GENERIC")
) {
// Telemetry labels must be C++ variable friendly.
let versionId = "other";
// Keep these in sync with mozilla central's Histograms.json.
const KNOWN_VERSIONS = [
"1.0",
"1.1",
"1.2",
"1.3",
"1.4",
"1.5",
"1.6",
"1.7",
"1.8",
"1.9",
"2.0",
"2.1",
"2.2",
"2.3",
];
if (KNOWN_VERSIONS.includes(info.PDFFormatVersion)) {
versionId = `v${info.PDFFormatVersion.replace(".", "_")}`;
}

let generatorId = "other";
// Keep these in sync with mozilla central's Histograms.json.
const KNOWN_GENERATORS = [
"acrobat distiller",
"acrobat pdfwriter",
"adobe livecycle",
"adobe pdf library",
"adobe photoshop",
"ghostscript",
"tcpdf",
"cairo",
"dvipdfm",
"dvips",
"pdftex",
"pdfkit",
"itext",
"prince",
"quarkxpress",
"mac os x",
"microsoft",
"openoffice",
"oracle",
"luradocument",
"pdf-xchange",
"antenna house",
"aspose.cells",
"fpdf",
];
if (info.Producer) {
const producer = info.Producer.toLowerCase();
KNOWN_GENERATORS.some(function (generator) {
Expand All @@ -1411,7 +1402,6 @@ const PDFViewerApplication = {
return true;
});
}

let formType = null;
if (info.IsAcroFormPresent) {
formType = info.IsXFAPresent ? "xfa" : "acroform";
Expand All @@ -1422,7 +1412,6 @@ const PDFViewerApplication = {
generator: generatorId,
formType,
});
}
},

/**
Expand Down Expand Up @@ -1635,14 +1624,9 @@ const PDFViewerApplication = {

printService.layout();

if (
typeof PDFJSDev === "undefined" ||
PDFJSDev.test("MOZCENTRAL || GENERIC")
) {
this.externalServices.reportTelemetry({
type: "print",
});
}
},

afterPrint() {
Expand Down Expand Up @@ -2084,10 +2068,6 @@ function webViewerPageRendered(evt) {
});
}

if (
typeof PDFJSDev === "undefined" ||
PDFJSDev.test("MOZCENTRAL || GENERIC")
) {
PDFViewerApplication.externalServices.reportTelemetry({
type: "pageInfo",
timestamp: evt.timestamp,
Expand All @@ -2100,7 +2080,6 @@ function webViewerPageRendered(evt) {
});
});
}
}

function webViewerPageMode({ mode }) {
// Handle the 'pagemode' hash parameter, see also `PDFLinkService_setHash`.
Expand Down Expand Up @@ -2501,15 +2480,13 @@ function webViewerWheel(evt) {
}

function webViewerClick(evt) {
PDFViewerApplication._hasInteracted = true;

// Avoid triggering the fallback bar when the user clicks on the
// toolbar or sidebar.
if (
PDFViewerApplication._delayedFallbackFeatureIds.length >= 1 &&
PDFViewerApplication.triggerDelayedFallback &&
PDFViewerApplication.pdfViewer.containsElement(evt.target)
) {
PDFViewerApplication.fallback();
PDFViewerApplication.triggerDelayedFallback();
}

if (!PDFViewerApplication.secondaryToolbar.isOpen) {
Expand All @@ -2527,12 +2504,10 @@ function webViewerClick(evt) {

function webViewerKeyUp(evt) {
if (evt.keyCode === 9) {
// The user is tabbing into the pdf. Display the error message
// if it has not already been displayed.
PDFViewerApplication._hasInteracted = true;

if (PDFViewerApplication._delayedFallbackFeatureIds.length >= 1) {
PDFViewerApplication.fallback();
// The user is tabbing into the viewer. Trigger the fallback bar if it has
// not already been displayed.
if (PDFViewerApplication.triggerDelayedFallback) {
PDFViewerApplication.triggerDelayedFallback();
}
}
}
Expand Down