1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-22 16:18:08 +02:00

Merge pull request #711 from jviereck/worker_feature

Feature detection
This commit is contained in:
Brendan Dahl 2011-11-02 15:16:31 -07:00
commit 43dbc3a84d
8 changed files with 123 additions and 63 deletions

View file

@ -472,7 +472,18 @@ var PDFDoc = (function pdfDoc() {
this.pageCache = [];
if (useWorker) {
var worker = new Worker('../src/worker_loader.js');
var workerSrc = PDFJS.workerSrc;
if (typeof workerSrc === 'undefined') {
throw 'No PDFJS.workerSrc specified';
}
var worker = new Worker(workerSrc);
// Tell the worker the file it was created from.
worker.postMessage({
action: 'workerSrc',
data: workerSrc
});
} else {
// If we don't use a worker, just post/sendMessage to the main thread.
var worker = {

View file

@ -4,12 +4,11 @@
var PDFJS = {};
(function pdfjsWrapper() {
// Use strict in our context only - users might not want it
'use strict';
// Files are inserted below - see Makefile
/* PDFJSSCRIPT_INCLUDE_ALL */
})();
}).call((typeof window === 'undefined') ? this : window);

View file

@ -47,6 +47,13 @@ var WorkerProcessorHandler = {
setup: function wphSetup(handler) {
var pdfDoc = null;
handler.on('workerSrc', function wphSetupWorkerSrc(data) {
// In development, the `workerSrc` message is handled in the
// `worker_loader.js` file. In production the workerProcessHandler is
// called for this. This servers as a dummy to prevent calling an
// undefined action `workerSrc`.
});
handler.on('doc', function wphSetupDoc(data) {
// Create only the model of the PDFDoc, which is enough for
// processing the content of the pdf.
@ -176,8 +183,7 @@ var workerConsole = {
if (typeof window === 'undefined') {
globalScope.console = workerConsole;
// Listen for messages from the main thread.
var handler = new MessageHandler('worker_processor', globalScope);
var handler = new MessageHandler('worker_processor', this);
WorkerProcessorHandler.setup(handler);
}

View file

@ -3,22 +3,50 @@
'use strict';
importScripts('../src/core.js');
importScripts('../src/util.js');
importScripts('../src/canvas.js');
importScripts('../src/obj.js');
importScripts('../src/function.js');
importScripts('../src/charsets.js');
importScripts('../src/cidmaps.js');
importScripts('../src/colorspace.js');
importScripts('../src/crypto.js');
importScripts('../src/evaluator.js');
importScripts('../src/fonts.js');
importScripts('../src/glyphlist.js');
importScripts('../src/image.js');
importScripts('../src/metrics.js');
importScripts('../src/parser.js');
importScripts('../src/pattern.js');
importScripts('../src/stream.js');
importScripts('../src/worker.js');
function onMessageLoader(evt) {
// Reset the `onmessage` function as it was only set to call
// this function the first time a message is passed to the worker
// but shouldn't get called anytime afterwards.
this.onmessage = null;
if (evt.data.action !== 'workerSrc') {
throw 'Worker expects first message to be `workerSrc`';
}
// Content of `PDFJS.workerSrc` as defined on the main thread.
var workerSrc = evt.data.data;
// Extract the directory that contains the source files to load.
// Assuming the source files have the same relative possition as the
// `workerSrc` file.
var dir = workerSrc.substring(0, workerSrc.lastIndexOf('/') + 1);
// List of files to include;
var files = [
'core.js',
'util.js',
'canvas.js',
'obj.js',
'function.js',
'charsets.js',
'cidmaps.js',
'colorspace.js',
'crypto.js',
'evaluator.js',
'fonts.js',
'glyphlist.js',
'image.js',
'metrics.js',
'parser.js',
'pattern.js',
'stream.js',
'worker.js'
];
// Load all the files.
for (var i = 0; i < files.length; i++) {
importScripts(dir + files[i]);
}
}
this.onmessage = onMessageLoader.bind(this);