Commit 8ee25496 authored by Robert Knight's avatar Robert Knight

Use alternate method to get document fingerprint

`PDFViewerApplication.documentFingerprint` was removed in recent
versions of PDF.js [1]. This issue causes loading the bookmarklet into
Firefox 63+ to fail because the client thinks the PDF is still loading.

`PDFViewerApplication.pdfDocument.fingerprint` is the replacement
mentioned in the commit that removed the property and it also appears to
be more clearly a part of PDF.js' documented API.

This change is backwards-compatible with the version of PDF.js that
currently ships with Via.

[1] https://github.com/mozilla/pdf.js/commit/e522b2d87e8eb3d4eb8a391a098a0f6f157e8c12
parent 637b3021
......@@ -41,7 +41,7 @@ class PDFMetadata {
resolve(app);
};
if (app.documentFingerprint) {
if (app.downloadComplete) {
resolve(app);
} else {
window.addEventListener('documentload', finish);
......@@ -61,7 +61,7 @@ class PDFMetadata {
return this._loaded.then(app => {
let uri = getPDFURL(app);
if (!uri) {
uri = fingerprintToURN(app.documentFingerprint);
uri = fingerprintToURN(app.pdfDocument.fingerprint);
}
return uri;
});
......@@ -86,7 +86,7 @@ class PDFMetadata {
}
const link = [
{href: fingerprintToURN(app.documentFingerprint)},
{href: fingerprintToURN(app.pdfDocument.fingerprint)},
];
const url = getPDFURL(app);
......@@ -97,7 +97,7 @@ class PDFMetadata {
return {
title: title,
link: link,
documentFingerprint: app.documentFingerprint,
documentFingerprint: app.pdfDocument.fingerprint,
};
});
}
......
......@@ -24,6 +24,15 @@ class FakeMetadata {
}
}
/**
* Fake implementation of PDF.js `window.PDFViewerApplication.pdfDocument`.
*/
class FakePDFDocumentProxy {
constructor({ fingerprint }) {
this.fingerprint = fingerprint;
}
}
/**
* Fake implementation of PDF.js `window.PDFViewerApplication` entry point.
*
......@@ -38,7 +47,7 @@ class FakePDFViewerApplication {
this.url = url;
this.documentInfo = undefined;
this.metadata = undefined;
this.documentFingerprint = undefined;
this.pdfDocument = null;
}
/**
......@@ -50,6 +59,7 @@ class FakePDFViewerApplication {
window.dispatchEvent(event);
this.url = url;
this.downloadComplete = true;
this.documentInfo = {};
if (typeof title !== undefined) {
......@@ -60,9 +70,7 @@ class FakePDFViewerApplication {
this.metadata = new FakeMetadata(metadata);
}
// TODO - This property was removed in recent versions of PDF.js.
// The PDFMetadata class should use `pdfDocument.fingerprint` instead.
this.documentFingerprint = fingerprint;
this.pdfDocument = new FakePDFDocumentProxy({ fingerprint });
}
}
......@@ -149,9 +157,9 @@ describe('annotator/plugin/pdf-metadata', function () {
it('gets the title from the dc:title field', function () {
const expectedMetadata = {
title: 'dcFakeTitle',
link: [{href: 'urn:x-pdf:' + fakePDFViewerApplication.documentFingerprint},
link: [{href: 'urn:x-pdf:' + fakePDFViewerApplication.pdfDocument.fingerprint},
{href: fakePDFViewerApplication.url}],
documentFingerprint: fakePDFViewerApplication.documentFingerprint,
documentFingerprint: fakePDFViewerApplication.pdfDocument.fingerprint,
};
return pdfMetadata.getMetadata().then(function (actualMetadata) {
......@@ -162,9 +170,9 @@ describe('annotator/plugin/pdf-metadata', function () {
it('gets the title from the documentInfo.Title field', function () {
const expectedMetadata = {
title: fakePDFViewerApplication.documentInfo.Title,
link: [{href: 'urn:x-pdf:' + fakePDFViewerApplication.documentFingerprint},
link: [{href: 'urn:x-pdf:' + fakePDFViewerApplication.pdfDocument.fingerprint},
{href: fakePDFViewerApplication.url}],
documentFingerprint: fakePDFViewerApplication.documentFingerprint,
documentFingerprint: fakePDFViewerApplication.pdfDocument.fingerprint,
};
fakePDFViewerApplication.metadata.has = sinon.stub().returns(false);
......@@ -182,7 +190,7 @@ describe('annotator/plugin/pdf-metadata', function () {
url: 'file://fake.pdf',
});
const expectedMetadata = {
link: [{href: 'urn:x-pdf:' + fakePDFViewerApplication.documentFingerprint}],
link: [{href: 'urn:x-pdf:' + fakePDFViewerApplication.pdfDocument.fingerprint}],
};
pdfMetadata = new PDFMetadata(fakePDFViewerApplication);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment