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 { ...@@ -41,7 +41,7 @@ class PDFMetadata {
resolve(app); resolve(app);
}; };
if (app.documentFingerprint) { if (app.downloadComplete) {
resolve(app); resolve(app);
} else { } else {
window.addEventListener('documentload', finish); window.addEventListener('documentload', finish);
...@@ -61,7 +61,7 @@ class PDFMetadata { ...@@ -61,7 +61,7 @@ class PDFMetadata {
return this._loaded.then(app => { return this._loaded.then(app => {
let uri = getPDFURL(app); let uri = getPDFURL(app);
if (!uri) { if (!uri) {
uri = fingerprintToURN(app.documentFingerprint); uri = fingerprintToURN(app.pdfDocument.fingerprint);
} }
return uri; return uri;
}); });
...@@ -86,7 +86,7 @@ class PDFMetadata { ...@@ -86,7 +86,7 @@ class PDFMetadata {
} }
const link = [ const link = [
{href: fingerprintToURN(app.documentFingerprint)}, {href: fingerprintToURN(app.pdfDocument.fingerprint)},
]; ];
const url = getPDFURL(app); const url = getPDFURL(app);
...@@ -97,7 +97,7 @@ class PDFMetadata { ...@@ -97,7 +97,7 @@ class PDFMetadata {
return { return {
title: title, title: title,
link: link, link: link,
documentFingerprint: app.documentFingerprint, documentFingerprint: app.pdfDocument.fingerprint,
}; };
}); });
} }
......
...@@ -24,6 +24,15 @@ class FakeMetadata { ...@@ -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. * Fake implementation of PDF.js `window.PDFViewerApplication` entry point.
* *
...@@ -38,7 +47,7 @@ class FakePDFViewerApplication { ...@@ -38,7 +47,7 @@ class FakePDFViewerApplication {
this.url = url; this.url = url;
this.documentInfo = undefined; this.documentInfo = undefined;
this.metadata = undefined; this.metadata = undefined;
this.documentFingerprint = undefined; this.pdfDocument = null;
} }
/** /**
...@@ -50,6 +59,7 @@ class FakePDFViewerApplication { ...@@ -50,6 +59,7 @@ class FakePDFViewerApplication {
window.dispatchEvent(event); window.dispatchEvent(event);
this.url = url; this.url = url;
this.downloadComplete = true;
this.documentInfo = {}; this.documentInfo = {};
if (typeof title !== undefined) { if (typeof title !== undefined) {
...@@ -60,9 +70,7 @@ class FakePDFViewerApplication { ...@@ -60,9 +70,7 @@ class FakePDFViewerApplication {
this.metadata = new FakeMetadata(metadata); this.metadata = new FakeMetadata(metadata);
} }
// TODO - This property was removed in recent versions of PDF.js. this.pdfDocument = new FakePDFDocumentProxy({ fingerprint });
// The PDFMetadata class should use `pdfDocument.fingerprint` instead.
this.documentFingerprint = fingerprint;
} }
} }
...@@ -149,9 +157,9 @@ describe('annotator/plugin/pdf-metadata', function () { ...@@ -149,9 +157,9 @@ describe('annotator/plugin/pdf-metadata', function () {
it('gets the title from the dc:title field', function () { it('gets the title from the dc:title field', function () {
const expectedMetadata = { const expectedMetadata = {
title: 'dcFakeTitle', title: 'dcFakeTitle',
link: [{href: 'urn:x-pdf:' + fakePDFViewerApplication.documentFingerprint}, link: [{href: 'urn:x-pdf:' + fakePDFViewerApplication.pdfDocument.fingerprint},
{href: fakePDFViewerApplication.url}], {href: fakePDFViewerApplication.url}],
documentFingerprint: fakePDFViewerApplication.documentFingerprint, documentFingerprint: fakePDFViewerApplication.pdfDocument.fingerprint,
}; };
return pdfMetadata.getMetadata().then(function (actualMetadata) { return pdfMetadata.getMetadata().then(function (actualMetadata) {
...@@ -162,9 +170,9 @@ describe('annotator/plugin/pdf-metadata', function () { ...@@ -162,9 +170,9 @@ describe('annotator/plugin/pdf-metadata', function () {
it('gets the title from the documentInfo.Title field', function () { it('gets the title from the documentInfo.Title field', function () {
const expectedMetadata = { const expectedMetadata = {
title: fakePDFViewerApplication.documentInfo.Title, title: fakePDFViewerApplication.documentInfo.Title,
link: [{href: 'urn:x-pdf:' + fakePDFViewerApplication.documentFingerprint}, link: [{href: 'urn:x-pdf:' + fakePDFViewerApplication.pdfDocument.fingerprint},
{href: fakePDFViewerApplication.url}], {href: fakePDFViewerApplication.url}],
documentFingerprint: fakePDFViewerApplication.documentFingerprint, documentFingerprint: fakePDFViewerApplication.pdfDocument.fingerprint,
}; };
fakePDFViewerApplication.metadata.has = sinon.stub().returns(false); fakePDFViewerApplication.metadata.has = sinon.stub().returns(false);
...@@ -182,7 +190,7 @@ describe('annotator/plugin/pdf-metadata', function () { ...@@ -182,7 +190,7 @@ describe('annotator/plugin/pdf-metadata', function () {
url: 'file://fake.pdf', url: 'file://fake.pdf',
}); });
const expectedMetadata = { const expectedMetadata = {
link: [{href: 'urn:x-pdf:' + fakePDFViewerApplication.documentFingerprint}], link: [{href: 'urn:x-pdf:' + fakePDFViewerApplication.pdfDocument.fingerprint}],
}; };
pdfMetadata = new PDFMetadata(fakePDFViewerApplication); 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