From 1e6d1f99221ff180ee8947dbb198f2119418b17d Mon Sep 17 00:00:00 2001 From: Julian Viereck Date: Fri, 28 Oct 2011 14:32:36 +0200 Subject: [PATCH 1/4] Make worker support work again after file split. Add PDFJS_WORKER_DIR/PDFJS_WORKER_FILE to specify where to load files if worker support is enabled --- examples/helloworld/hello.js | 2 +- examples/helloworld/index.html | 43 +++++++++++++++------------ src/core.js | 21 ++++++++++++- src/pdf.js | 1 - src/worker.js | 5 ++-- src/worker_loader.js | 54 ++++++++++++++++++++++------------ web/viewer-snippet.html | 5 ++++ web/viewer.html | 6 ++-- 8 files changed, 91 insertions(+), 46 deletions(-) diff --git a/examples/helloworld/hello.js b/examples/helloworld/hello.js index 45e61eb6f..c97b53c66 100644 --- a/examples/helloworld/hello.js +++ b/examples/helloworld/hello.js @@ -7,7 +7,7 @@ 'use strict'; -PDFJS.getPdf('helloworld.pdf', function getPdfHelloWorld(data) { +getPdf('helloworld.pdf', function getPdfHelloWorld(data) { // // Instantiate PDFDoc with PDF data // diff --git a/examples/helloworld/index.html b/examples/helloworld/index.html index b10e9eaeb..0fa711fe4 100644 --- a/examples/helloworld/index.html +++ b/examples/helloworld/index.html @@ -3,27 +3,32 @@ - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + - + diff --git a/src/core.js b/src/core.js index e7241acfa..8d3d6d434 100644 --- a/src/core.js +++ b/src/core.js @@ -471,7 +471,26 @@ var PDFDoc = (function() { this.pageCache = []; if (useWorker) { - var worker = new Worker('../src/worker_loader.js'); + var worker; + if (typeof PDFJS_WORKER_DIR !== 'undefined') { + // If `PDFJS_WORKER_DIR` is specified, we assume the pdf.js files + // located all in that directory. Create a new worker and tell him + // the directory, such that he can load the scripts from there. + worker = new Worker(PDFJS_WORKER_DIR + 'worker_loader.js'); + console.log('main: post dir'); + + worker.postMessage(PDFJS_WORKER_DIR); + } else if (typeof PDFJS_WORKER_FILE !== 'undefined') { + // If we build the worker using a worker file, then we assume, that + // everything the worker needs is already included in that file. + // Therefore the worker doesn't have to call `importScripts` to load + // all the single files and therefore it's not necessary to tell the + // worker a directory to laod the js files from. + // (Which is different from the PDFJS_WORKER_DIR case above.) + worker = new Worker(PDFJS_WORKER_FILE); + } else { + throw 'No worker file or directory specified.'; + } } else { // If we don't use a worker, just post/sendMessage to the main thread. var worker = { diff --git a/src/pdf.js b/src/pdf.js index 34e163967..2ecd2fddc 100644 --- a/src/pdf.js +++ b/src/pdf.js @@ -4,7 +4,6 @@ var PDFJS = {}; (function pdfjsWrapper() { - // Use strict in our context only - users might not want it 'use strict'; diff --git a/src/worker.js b/src/worker.js index a83f31668..5545fc459 100644 --- a/src/worker.js +++ b/src/worker.js @@ -174,10 +174,9 @@ var workerConsole = { // Worker thread? if (typeof window === 'undefined') { - globalScope.console = workerConsole; + this.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); } diff --git a/src/worker_loader.js b/src/worker_loader.js index fb37ca9c4..69f7d55ba 100644 --- a/src/worker_loader.js +++ b/src/worker_loader.js @@ -3,22 +3,40 @@ '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'); +this.onmessage = function(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. + delete this.onmessage; + // Directory the include files are contained is send as the + // first message to the worker. + var dir = evt.data; + + // 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]); + } +}.bind(this); diff --git a/web/viewer-snippet.html b/web/viewer-snippet.html index c99897a44..4d2a4f7b7 100644 --- a/web/viewer-snippet.html +++ b/web/viewer-snippet.html @@ -1,2 +1,7 @@ + diff --git a/web/viewer.html b/web/viewer.html index f7a5378ed..4505331d6 100644 --- a/web/viewer.html +++ b/web/viewer.html @@ -3,9 +3,9 @@ Simple pdf.js page viewer - + - + @@ -114,7 +114,7 @@ - +
Loading... 0%
From 3b7829d057953204b6a6ef8474dfde4a7708acbc Mon Sep 17 00:00:00 2001 From: Julian Viereck Date: Tue, 1 Nov 2011 19:32:20 +0100 Subject: [PATCH 2/4] Address review comments. Use only one PDFJS.workerSrc variable to specify the worker source --- examples/helloworld/hello.js | 2 +- examples/helloworld/index.html | 7 +++--- src/core.js | 30 ++++++++++--------------- src/pdf.js | 2 +- src/worker.js | 9 +++++++- src/worker_loader.js | 16 ++++++++++---- test/test_slave.html | 40 +++++++++++++++++++--------------- web/viewer-snippet.html | 5 ++--- web/viewer.html | 3 ++- 9 files changed, 63 insertions(+), 51 deletions(-) diff --git a/examples/helloworld/hello.js b/examples/helloworld/hello.js index c97b53c66..45e61eb6f 100644 --- a/examples/helloworld/hello.js +++ b/examples/helloworld/hello.js @@ -7,7 +7,7 @@ 'use strict'; -getPdf('helloworld.pdf', function getPdfHelloWorld(data) { +PDFJS.getPdf('helloworld.pdf', function getPdfHelloWorld(data) { // // Instantiate PDFDoc with PDF data // diff --git a/examples/helloworld/index.html b/examples/helloworld/index.html index 0fa711fe4..a48e3705b 100644 --- a/examples/helloworld/index.html +++ b/examples/helloworld/index.html @@ -3,6 +3,7 @@ + @@ -23,9 +24,9 @@ diff --git a/src/core.js b/src/core.js index 8d3d6d434..48b1d0591 100644 --- a/src/core.js +++ b/src/core.js @@ -471,26 +471,18 @@ var PDFDoc = (function() { this.pageCache = []; if (useWorker) { - var worker; - if (typeof PDFJS_WORKER_DIR !== 'undefined') { - // If `PDFJS_WORKER_DIR` is specified, we assume the pdf.js files - // located all in that directory. Create a new worker and tell him - // the directory, such that he can load the scripts from there. - worker = new Worker(PDFJS_WORKER_DIR + 'worker_loader.js'); - console.log('main: post dir'); - - worker.postMessage(PDFJS_WORKER_DIR); - } else if (typeof PDFJS_WORKER_FILE !== 'undefined') { - // If we build the worker using a worker file, then we assume, that - // everything the worker needs is already included in that file. - // Therefore the worker doesn't have to call `importScripts` to load - // all the single files and therefore it's not necessary to tell the - // worker a directory to laod the js files from. - // (Which is different from the PDFJS_WORKER_DIR case above.) - worker = new Worker(PDFJS_WORKER_FILE); - } else { - throw 'No worker file or directory specified.'; + 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 = { diff --git a/src/pdf.js b/src/pdf.js index 2ecd2fddc..51f606548 100644 --- a/src/pdf.js +++ b/src/pdf.js @@ -10,5 +10,5 @@ var PDFJS = {}; // Files are inserted below - see Makefile /* PDFJSSCRIPT_INCLUDE_ALL */ -})(); +}).call((typeof window === 'undefined') ? this : window); diff --git a/src/worker.js b/src/worker.js index 5545fc459..d46364f60 100644 --- a/src/worker.js +++ b/src/worker.js @@ -47,6 +47,13 @@ var WorkerProcessorHandler = { setup: function(handler) { var pdfDoc = null; + handler.on('workerSrc', function(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(data) { // Create only the model of the PDFDoc, which is enough for // processing the content of the pdf. @@ -174,7 +181,7 @@ var workerConsole = { // Worker thread? if (typeof window === 'undefined') { - this.console = workerConsole; + globalScope.console = workerConsole; var handler = new MessageHandler('worker_processor', this); WorkerProcessorHandler.setup(handler); diff --git a/src/worker_loader.js b/src/worker_loader.js index 69f7d55ba..8eb1da7b2 100644 --- a/src/worker_loader.js +++ b/src/worker_loader.js @@ -7,11 +7,19 @@ this.onmessage = function(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. - delete this.onmessage; + this.onmessage = null; - // Directory the include files are contained is send as the - // first message to the worker. - var dir = evt.data; + 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 = [ diff --git a/test/test_slave.html b/test/test_slave.html index 7ac886769..91852d5a5 100644 --- a/test/test_slave.html +++ b/test/test_slave.html @@ -3,25 +3,29 @@ pdf.js test slave - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + diff --git a/web/viewer-snippet.html b/web/viewer-snippet.html index 4d2a4f7b7..6b0c8821b 100644 --- a/web/viewer-snippet.html +++ b/web/viewer-snippet.html @@ -1,7 +1,6 @@ diff --git a/web/viewer.html b/web/viewer.html index 4505331d6..7dd81e0af 100644 --- a/web/viewer.html +++ b/web/viewer.html @@ -5,7 +5,6 @@ - @@ -25,6 +24,8 @@ + + From 51d4a1723210359abfed47332d3d0352d4f9d7f4 Mon Sep 17 00:00:00 2001 From: Julian Viereck Date: Tue, 1 Nov 2011 22:23:16 +0100 Subject: [PATCH 3/4] Change workerSrc location --- src/core.js | 2 +- web/viewer.html | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core.js b/src/core.js index 2bc4c50aa..5685cdf31 100644 --- a/src/core.js +++ b/src/core.js @@ -7,7 +7,7 @@ var globalScope = (typeof window === 'undefined') ? this : window; var ERRORS = 0, WARNINGS = 1, TODOS = 5; var verbosity = WARNINGS; -var useWorker = false; +var useWorker = true; // The global PDFJS object exposes the API // In production, it will be declared outside a global wrapper diff --git a/web/viewer.html b/web/viewer.html index 60eab8eab..3883804f0 100644 --- a/web/viewer.html +++ b/web/viewer.html @@ -26,7 +26,7 @@ - + From 50fe4f55e2f78320dc265f9cf828dc0d3a25666e Mon Sep 17 00:00:00 2001 From: Julian Viereck Date: Wed, 2 Nov 2011 23:00:08 +0100 Subject: [PATCH 4/4] Fix lint warning + turn off worker support --- src/core.js | 2 +- src/worker_loader.js | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/core.js b/src/core.js index 5685cdf31..2bc4c50aa 100644 --- a/src/core.js +++ b/src/core.js @@ -7,7 +7,7 @@ var globalScope = (typeof window === 'undefined') ? this : window; var ERRORS = 0, WARNINGS = 1, TODOS = 5; var verbosity = WARNINGS; -var useWorker = true; +var useWorker = false; // The global PDFJS object exposes the API // In production, it will be declared outside a global wrapper diff --git a/src/worker_loader.js b/src/worker_loader.js index 8eb1da7b2..8d342d116 100644 --- a/src/worker_loader.js +++ b/src/worker_loader.js @@ -3,7 +3,7 @@ 'use strict'; -this.onmessage = function(evt) { +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. @@ -47,4 +47,6 @@ this.onmessage = function(evt) { for (var i = 0; i < files.length; i++) { importScripts(dir + files[i]); } -}.bind(this); +} + +this.onmessage = onMessageLoader.bind(this);