From 56a8c934dde6cce806b1d74fa80c181eb30bbb96 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 19 Jan 2018 18:16:17 +0100 Subject: [PATCH] [api-major] Remove the `PDFJS.disableWorker` option Despite this patch removing the `disableWorker` option itself, please note that we'll still fallback to loading the worker file(s) on the main-thread when running in environments without proper Web Worker support. Furthermore it's still possible, even with this patch, to force the use of fake workers by manually loading the necessary file using a `` to the HTML (obviously with the path adjusted as needed). Finally note that the `disableWorker` option is a performance footgun, and unfortunately many existing third-party examples actually use it without providing any sort of warning/justification. --- [1] This approach is used in the default viewer, since certain kind of debugging may be easier if the code is running directly on the main-thread. --- examples/browserify/main.js | 3 --- examples/learning/helloworld.html | 7 ------ examples/learning/helloworld64.html | 6 ----- examples/learning/prevnext.html | 8 ------ examples/webpack/main.js | 3 --- external/dist/webpack.js | 2 -- src/display/api.js | 24 ++++++++++++++++-- src/display/dom_utils.js | 2 -- src/display/global.js | 12 +-------- test/unit/api_spec.js | 16 ++++++------ web/app.js | 38 ++++++++++++++++++++++++++--- 11 files changed, 64 insertions(+), 57 deletions(-) diff --git a/examples/browserify/main.js b/examples/browserify/main.js index 46a921a59..42c640ef0 100644 --- a/examples/browserify/main.js +++ b/examples/browserify/main.js @@ -10,9 +10,6 @@ var pdfPath = '../helloworld/helloworld.pdf'; // Setting worker path to worker bundle. PDFJS.workerSrc = '../../build/browserify/pdf.worker.bundle.js'; -// It is also possible to disable workers via `PDFJS.disableWorker = true`, -// however that might degrade the UI performance in web browsers. - // Loading a document. var loadingTask = PDFJS.getDocument(pdfPath); loadingTask.promise.then(function (pdfDocument) { diff --git a/examples/learning/helloworld.html b/examples/learning/helloworld.html index 4142ef6f5..c2988c287 100644 --- a/examples/learning/helloworld.html +++ b/examples/learning/helloworld.html @@ -19,13 +19,6 @@ // var url = './helloworld.pdf'; - // - // Disable workers to avoid yet another cross-origin issue (workers need - // the URL of the script to be loaded, and dynamically loading a cross-origin - // script does not work). - // - // PDFJS.disableWorker = true; - // // The workerSrc property shall be specified. // diff --git a/examples/learning/helloworld64.html b/examples/learning/helloworld64.html index 58890fe87..69420495a 100644 --- a/examples/learning/helloworld64.html +++ b/examples/learning/helloworld64.html @@ -31,12 +31,6 @@ 'MDAwIG4gCjAwMDAwMDAzODAgMDAwMDAgbiAKdHJhaWxlcgo8PAogIC9TaXplIDYKICAvUm9v' + 'dCAxIDAgUgo+PgpzdGFydHhyZWYKNDkyCiUlRU9G'); - // Disable workers to avoid yet another cross-origin issue (workers need - // the URL of the script to be loaded, and dynamically loading a cross-origin - // script does not work). - // - // PDFJS.disableWorker = true; - // // The workerSrc property shall be specified. // diff --git a/examples/learning/prevnext.html b/examples/learning/prevnext.html index 463527a3d..f7e68f2b5 100644 --- a/examples/learning/prevnext.html +++ b/examples/learning/prevnext.html @@ -28,14 +28,6 @@ // var url = '../../web/compressed.tracemonkey-pldi-09.pdf'; - - // - // Disable workers to avoid yet another cross-origin issue (workers need - // the URL of the script to be loaded, and dynamically loading a cross-origin - // script does not work). - // - // PDFJS.disableWorker = true; - // // In cases when the pdf.worker.js is located at the different folder than the // pdf.js's one, or the pdf.js is executed via eval(), the workerSrc property diff --git a/examples/webpack/main.js b/examples/webpack/main.js index d0466b86d..d1beb101c 100644 --- a/examples/webpack/main.js +++ b/examples/webpack/main.js @@ -10,9 +10,6 @@ var pdfPath = '../helloworld/helloworld.pdf'; // Setting worker path to worker bundle. pdfjsLib.PDFJS.workerSrc = '../../build/webpack/pdf.worker.bundle.js'; -// It is also possible to disable workers via `PDFJS.disableWorker = true`, -// however that might degrade the UI performance in web browsers. - // Loading a document. var loadingTask = pdfjsLib.getDocument(pdfPath); loadingTask.promise.then(function (pdfDocument) { diff --git a/external/dist/webpack.js b/external/dist/webpack.js index f2c4a6400..af3979439 100644 --- a/external/dist/webpack.js +++ b/external/dist/webpack.js @@ -19,8 +19,6 @@ var PdfjsWorker = require('worker-loader!./build/pdf.worker.js'); if (typeof window !== 'undefined' && 'Worker' in window) { pdfjs.PDFJS.workerPort = new PdfjsWorker(); -} else { - pdfjs.PDFJS.disableWorker = true; } module.exports = pdfjs; diff --git a/src/display/api.js b/src/display/api.js index afc08c047..c62c3e7c1 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -1216,6 +1216,19 @@ var PDFWorker = (function PDFWorkerClosure() { throw new Error('No PDFJS.workerSrc specified'); } + function getMainThreadWorkerMessageHandler() { + if (typeof window === 'undefined') { + return null; + } + if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('PRODUCTION')) { + return (window.pdfjsNonProductionPdfWorker && + window.pdfjsNonProductionPdfWorker.WorkerMessageHandler); + } + // PRODUCTION + return (window.pdfjsDistBuildPdfWorker && + window.pdfjsDistBuildPdfWorker.WorkerMessageHandler); + } + let fakeWorkerFilesLoadedCapability; // Loads worker code into main thread. @@ -1225,6 +1238,13 @@ var PDFWorker = (function PDFWorkerClosure() { return fakeWorkerFilesLoadedCapability.promise; } fakeWorkerFilesLoadedCapability = createPromiseCapability(); + + let mainWorkerMessageHandler = getMainThreadWorkerMessageHandler(); + if (mainWorkerMessageHandler) { + // The worker was already loaded using a `