Init PDFWorker via MesssagePort.

This commit is contained in:
Yury Delendik 2017-02-24 13:33:18 -06:00
parent cada411af4
commit 51767d63fe
5 changed files with 52 additions and 4 deletions

21
external/dist/webpack.js vendored Normal file
View File

@ -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;

View File

@ -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

View File

@ -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

View File

@ -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':

View File

@ -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.