From 00f1d6dbf1ecee9b1d4aa286a25224ad311255b9 Mon Sep 17 00:00:00 2001 From: Yury Delendik Date: Sat, 23 Jun 2012 14:48:33 -0500 Subject: [PATCH] Fetches pdf data in the worker --- src/api.js | 94 ++++++++++++++++++++++++++++---------------------- src/core.js | 4 +-- src/util.js | 18 ++++++++++ src/worker.js | 61 ++++++++++++++++++++++++++++---- test/driver.js | 6 +++- web/viewer.js | 6 +++- 6 files changed, 137 insertions(+), 52 deletions(-) diff --git a/src/api.js b/src/api.js index 7d65f96b4..51d42b4c1 100644 --- a/src/api.js +++ b/src/api.js @@ -18,52 +18,21 @@ * @return {Promise} A promise that is resolved with {PDFDocumentProxy} object. */ PDFJS.getDocument = function getDocument(source) { - var url, data, headers, password, parameters = {}; if (typeof source === 'string') { - url = source; + source = { url: source }; } else if (isArrayBuffer(source)) { - data = source; - } else if (typeof source === 'object') { - url = source.url; - data = source.data; - headers = source.httpHeaders; - password = source.password; - parameters.password = password || null; - - if (!url && !data) - error('Invalid parameter array, need either .data or .url'); - } else { + source = { data: source }; + } else if (typeof source !== 'object') { error('Invalid parameter in getDocument, need either Uint8Array, ' + 'string or a parameter object'); } + if (!source.url && !source.data) + error('Invalid parameter array, need either .data or .url'); + var promise = new PDFJS.Promise(); var transport = new WorkerTransport(promise); - if (data) { - // assuming the data is array, instantiating directly from it - transport.sendData(data, parameters); - } else if (url) { - // fetch url - PDFJS.getPdf( - { - url: url, - progress: function getPDFProgress(evt) { - if (evt.lengthComputable) - promise.progress({ - loaded: evt.loaded, - total: evt.total - }); - }, - error: function getPDFError(e) { - promise.reject('Unexpected server response of ' + - e.target.status + '.'); - }, - headers: headers - }, - function getPDFLoad(data) { - transport.sendData(data, parameters); - }); - } + transport.fetchDocument(source); return promise; }; @@ -511,6 +480,34 @@ var WorkerTransport = (function WorkerTransportClosure() { this.workerReadyPromise.resolve(pdfDocument); }, this); + messageHandler.on('FetchDoc', function transportFetchDoc(data) { + var url = data.url; + var headers = data.httpHeaders; + var password = data.password; + + var promise = this.workerReadyPromise; + var transport = this; + PDFJS.getPdf( + { + url: url, + progress: function getPDFProgress(evt) { + if (evt.lengthComputable) + promise.progress({ + loaded: evt.loaded, + total: evt.total + }); + }, + error: function getPDFError(e) { + promise.reject('Unexpected server response of ' + + e.target.status + '.'); + }, + headers: headers + }, + function getPDFLoad(data) { + transport.sendData(data); + }); + }, this); + messageHandler.on('NeedPassword', function transportPassword(data) { this.workerReadyPromise.reject(data.exception.message, data.exception); }, this); @@ -577,6 +574,17 @@ var WorkerTransport = (function WorkerTransportClosure() { } }, this); + messageHandler.on('DocProgress', function transportDocProgress(data) { + this.workerReadyPromise.progress({ + loaded: data.loaded, + total: data.total + }); + }, this); + + messageHandler.on('DocError', function transportDocError(data) { + this.workerReadyPromise.reject(data.message); + }, this); + messageHandler.on('PageError', function transportError(data) { var page = this.pageCache[data.pageNum - 1]; if (page.displayReadyPromise) @@ -621,11 +629,15 @@ var WorkerTransport = (function WorkerTransportClosure() { }); }, - sendData: function WorkerTransport_sendData(data, params) { - this.messageHandler.send('GetDocRequest', {data: data, params: params}); + sendData: function WorkerTransport_sendData(data) { + this.messageHandler.send('GetDocRequest', {data: data}); }, - getData: function WorkerTransport_sendData(promise) { + fetchDocument: function WorkerTransport_fetchDocument(source) { + this.messageHandler.send('FetchDocRequest', {source: source}); + }, + + getData: function WorkerTransport_getData(promise) { this.messageHandler.send('GetData', null, function(data) { promise.resolve(data); }); diff --git a/src/core.js b/src/core.js index 8e07078ef..0a49ae0f8 100644 --- a/src/core.js +++ b/src/core.js @@ -45,8 +45,8 @@ function getPdf(arg, callback) { } xhr.mozResponseType = xhr.responseType = 'arraybuffer'; - var protocol = params.url.indexOf(':') < 0 ? window.location.protocol : - params.url.substring(0, params.url.indexOf(':') + 1); + + var protocol = params.url.substring(0, params.url.indexOf(':') + 1); xhr.expected = (protocol === 'http:' || protocol === 'https:') ? 200 : 0; if ('progress' in params) diff --git a/src/util.js b/src/util.js index 03d0cae65..75190f653 100644 --- a/src/util.js +++ b/src/util.js @@ -57,6 +57,24 @@ function assert(cond, msg) { error(msg); } +function combineUrl(baseUrl, url) { + if (url.indexOf(':') >= 0) + return url; + if (url.charAt(0) == '/') { + // absolute path + var i = baseUrl.indexOf('://'); + i = baseUrl.indexOf('/', i + 3); + return baseUrl.substring(0, i) + url; + } else { + // relative path + var i = baseUrl.lastIndexOf('#'); + i = baseUrl.lastIndexOf('?', i < 0 ? baseUrl.length : i); + i = baseUrl.lastIndexOf('/', i < 0 ? baseUrl.length : i); + return baseUrl.substring(0, i + 1) + url; + } +} +PDFJS.combineUrl = combineUrl; + // In a well-formed PDF, |cond| holds. If it doesn't, subsequent // behavior is undefined. function assertWellFormed(cond, msg) { diff --git a/src/worker.js b/src/worker.js index c1dfa79af..54bdf024f 100644 --- a/src/worker.js +++ b/src/worker.js @@ -3,6 +3,8 @@ 'use strict'; +var useMainThreadToDownload = false; + function MessageHandler(name, comObj) { this.name = name; this.comObj = comObj; @@ -83,16 +85,12 @@ MessageHandler.prototype = { var WorkerMessageHandler = { setup: function wphSetup(handler) { var pdfModel = null; + var pdfModelSource = null; - handler.on('test', function wphSetupTest(data) { - handler.send('test', data instanceof Uint8Array); - }); - - handler.on('GetDocRequest', function wphSetupDoc(data) { + function loadDocument(pdfData) { // Create only the model of the PDFDoc, which is enough for // processing the content of the pdf. - var pdfData = data.data; - var pdfPassword = data.params.password; + var pdfPassword = pdfModelSource.password; try { pdfModel = new PDFDocument(new Stream(pdfData), pdfPassword); } catch (e) { @@ -122,6 +120,55 @@ var WorkerMessageHandler = { encrypted: !!pdfModel.xref.encrypt }; handler.send('GetDoc', {pdfInfo: doc}); + } + + handler.on('test', function wphSetupTest(data) { + handler.send('test', data instanceof Uint8Array); + }); + + handler.on('GetDocRequest', function wphSetupDoc(data) { + var pdfData = data.data; + loadDocument(pdfData); + }); + + handler.on('FetchDocRequest', function wphSetupFetchDoc(data) { + var source = data.source; + pdfModelSource = source; + + if (source.data) { + // the data is array, instantiating directly from it + loadDocument(source.data); + return; + } + + if (useMainThreadToDownload) { + // fallback to main thread to download PDF + handler.send('FetchDoc', { + url: source.url, + httpHeaders: source.httpHeaders + }); + } + + PDFJS.getPdf( + { + url: source.url, + progress: function getPDFProgress(evt) { + if (evt.lengthComputable) { + handler.send('DocProgress', { + loaded: evt.loaded, + total: evt.total + }); + } + }, + error: function getPDFError(e) { + handler.send('DocError', 'Unexpected server response of ' + + e.target.status + '.'); + }, + headers: source.httpHeaders + }, + function getPDFLoad(data) { + loadDocument(data); + }); }); handler.on('GetPageRequest', function wphSetupGetPage(data) { diff --git a/test/driver.js b/test/driver.js index cd5ea49e7..a2d469f3d 100644 --- a/test/driver.js +++ b/test/driver.js @@ -86,6 +86,10 @@ function exceptionToString(e) { return e.message + ('stack' in e ? ' at ' + e.stack.split('\n')[0] : ''); } +function expandUrl(url) { + return combineUrl(window.location.href, url); +} + function nextTask() { cleanup(); @@ -98,7 +102,7 @@ function nextTask() { log('Loading file "' + task.file + '"\n'); - getPdf(task.file, function nextTaskGetPdf(data) { + getPdf(expandUrl(task.file), function nextTaskGetPdf(data) { var failure; function continuation() { task.pageNum = task.firstPage || 1; diff --git a/web/viewer.js b/web/viewer.js index d371c3242..631a6eeb0 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -26,6 +26,10 @@ function getFileName(url) { return url.substring(url.lastIndexOf('/', end) + 1, end); } +function expandUrl(url) { + return PDFJS.combineUrl(window.location.href, url); +} + var Cache = function cacheCache(size) { var data = []; this.push = function cachePush(view) { @@ -376,7 +380,7 @@ var PDFView = { if (typeof url === 'string') { // URL this.url = url; document.title = decodeURIComponent(getFileName(url)) || url; - parameters.url = url; + parameters.url = expandUrl(url); } else if (url && 'byteLength' in url) { // ArrayBuffer parameters.data = url; }