From 51767d63fea50094e88006c1a8809e6131ef6ce5 Mon Sep 17 00:00:00 2001 From: Yury Delendik Date: Fri, 24 Feb 2017 13:33:18 -0600 Subject: [PATCH] Init PDFWorker via MesssagePort. --- external/dist/webpack.js | 21 +++++++++++++++++++++ make.js | 3 ++- src/display/api.js | 24 +++++++++++++++++++++--- src/display/dom_utils.js | 2 ++ src/display/global.js | 6 ++++++ 5 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 external/dist/webpack.js diff --git a/external/dist/webpack.js b/external/dist/webpack.js new file mode 100644 index 000000000..6cb7561a0 --- /dev/null +++ b/external/dist/webpack.js @@ -0,0 +1,21 @@ +/* Copyright 2017 Mozilla Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +'use strict'; + +var pdfjs = require('./build/pdf.js'); +var PdfjsWorker = require('worker-loader!./build/pdf.worker.js'); +pdfjs.PDFJS.workerPort = new PdfjsWorker(); + +module.exports = pdfjs; diff --git a/make.js b/make.js index b3e1e2ca0..be6e2c808 100644 --- a/make.js +++ b/make.js @@ -205,7 +205,8 @@ target.dist = function() { bugs: DIST_BUGS_URL, license: DIST_LICENSE, dependencies: { - 'node-ensure': '^0.0.0' // shim for node for require.ensure + 'node-ensure': '^0.0.0', // shim for node for require.ensure + 'worker-loader': '^0.7.1', // used in external/dist/webpack.json }, browser: { 'node-ensure': false diff --git a/src/display/api.js b/src/display/api.js index 391a5bd14..a2567c5d6 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -264,8 +264,10 @@ function getDocument(src, pdfDataRangeTransport, var CMapReaderFactory = params.CMapReaderFactory || DOMCMapReaderFactory; if (!worker) { - // Worker was not provided -- creating and owning our own. - worker = new PDFWorker(); + // Worker was not provided -- creating and owning our own. If message port + // is specified in global settings, using it. + var workerPort = getDefaultSetting('workerPort'); + worker = workerPort ? new PDFWorker(null, workerPort) : new PDFWorker(); task._worker = worker; } var docId = task.docId; @@ -1227,7 +1229,7 @@ var PDFWorker = (function PDFWorkerClosure() { return URL.createObjectURL(new Blob([wrapper])); } - function PDFWorker(name) { + function PDFWorker(name, port) { this.name = name; this.destroyed = false; @@ -1235,6 +1237,12 @@ var PDFWorker = (function PDFWorkerClosure() { this._port = null; this._webWorker = null; this._messageHandler = null; + + if (port) { + this._initializeFromPort(port); + return; + } + this._initialize(); } @@ -1251,6 +1259,16 @@ var PDFWorker = (function PDFWorkerClosure() { return this._messageHandler; }, + _initializeFromPort: function PDFWorker_initializeFromPort(port) { + this._port = port; + this._messageHandler = new MessageHandler('main', 'worker', port); + this._messageHandler.on('ready', function () { + // Ignoring 'ready' event -- MessageHandler shall be already initialized + // and ready to accept the messages. + }); + this._readyCapability.resolve(); + }, + _initialize: function PDFWorker_initialize() { // If worker support isn't disabled explicit and the browser has worker // support, create a new web worker and test if it/the browser fulfills diff --git a/src/display/dom_utils.js b/src/display/dom_utils.js index e7df94375..185303a28 100644 --- a/src/display/dom_utils.js +++ b/src/display/dom_utils.js @@ -272,6 +272,8 @@ function getDefaultSetting(id) { return globalSettings ? globalSettings.cMapPacked : false; case 'postMessageTransfers': return globalSettings ? globalSettings.postMessageTransfers : true; + case 'workerPort': + return globalSettings ? globalSettings.workerPort : null; case 'workerSrc': return globalSettings ? globalSettings.workerSrc : null; case 'disableWorker': diff --git a/src/display/global.js b/src/display/global.js index 682e3a712..b06abb5ff 100644 --- a/src/display/global.js +++ b/src/display/global.js @@ -160,6 +160,12 @@ */ PDFJS.workerSrc = (PDFJS.workerSrc === undefined ? null : PDFJS.workerSrc); + /** + * Defines global port for worker process. Overrides workerSrc and + * disableWorker setting. + */ + PDFJS.workerPort = (PDFJS.workerPort === undefined ? null : PDFJS.workerPort); + /** * Disable range request loading of PDF files. When enabled and if the server * supports partial content requests then the PDF will be fetched in chunks.