diff --git a/src/api.js b/src/api.js index b3ff6f8d6..7429c4779 100644 --- a/src/api.js +++ b/src/api.js @@ -18,63 +18,26 @@ * @return {Promise} A promise that is resolved with {PDFDocumentProxy} object. */ PDFJS.getDocument = function getDocument(source) { - var url, data, headers, password, parameters = {}, workerInitializedPromise, - workerReadyPromise, transport; + var workerInitializedPromise, workerReadyPromise, transport; 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'); + workerInitializedPromise = new PDFJS.Promise(); workerReadyPromise = new PDFJS.Promise(); transport = new WorkerTransport(workerInitializedPromise, workerReadyPromise); - 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) { - workerReadyPromise.progress({ - loaded: evt.loaded, - total: evt.total - }); - } - }, - error: function getPDFError(e) { - workerReadyPromise.reject('Unexpected server response of ' + - e.target.status + '.'); - }, - headers: headers - }, - function getPDFLoad(data) { - // sometimes the pdf has finished downloading before the web worker-test - // has finished. In that case the rendering of the final pdf would cause - // errors. We have to wait for the WorkerTransport to finalize worker- - // support detection - workerInitializedPromise.then(function workerInitialized() { - transport.sendData(data, parameters); - }); - }); - } - + workerInitializedPromise.then(function transportInitialized() { + transport.fetchDocument(source); + }); return workerReadyPromise; }; @@ -601,6 +564,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); + }, this); + messageHandler.on('PageError', function transportError(data) { var page = this.pageCache[data.pageNum - 1]; if (page.displayReadyPromise) @@ -645,11 +619,11 @@ var WorkerTransport = (function WorkerTransportClosure() { }); }, - sendData: function WorkerTransport_sendData(data, params) { - this.messageHandler.send('GetDocRequest', {data: data, params: params}); + fetchDocument: function WorkerTransport_fetchDocument(source) { + this.messageHandler.send('GetDocRequest', {source: source}); }, - getData: function WorkerTransport_sendData(promise) { + 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 2b9481041..e8d008074 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..ad915b03f 100644 --- a/src/util.js +++ b/src/util.js @@ -57,6 +57,29 @@ function assert(cond, msg) { error(msg); } +// Combines two URLs. The baseUrl shall be absolute URL. If the url is an +// absolute URL, it will be returned as is. +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 pathLength = baseUrl.length, i; + i = baseUrl.lastIndexOf('#'); + pathLength = i >= 0 ? i : pathLength; + i = baseUrl.lastIndexOf('?', pathLength); + pathLength = i >= 0 ? i : pathLength; + var prefixLength = baseUrl.lastIndexOf('/', pathLength); + return baseUrl.substring(0, prefixLength + 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..94d009334 100644 --- a/src/worker.js +++ b/src/worker.js @@ -84,15 +84,10 @@ var WorkerMessageHandler = { setup: function wphSetup(handler) { var pdfModel = null; - handler.on('test', function wphSetupTest(data) { - handler.send('test', data instanceof Uint8Array); - }); - - handler.on('GetDocRequest', function wphSetupDoc(data) { + function loadDocument(pdfData, pdfModelSource) { // 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 +117,40 @@ 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 source = data.source; + if (source.data) { + // the data is array, instantiating directly from it + loadDocument(source.data, source); + return; + } + + 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, source); + }); }); handler.on('GetPageRequest', function wphSetupGetPage(data) { diff --git a/test/driver.js b/test/driver.js index cd5ea49e7..6ea1fc531 100644 --- a/test/driver.js +++ b/test/driver.js @@ -98,7 +98,8 @@ function nextTask() { log('Loading file "' + task.file + '"\n'); - getPdf(task.file, function nextTaskGetPdf(data) { + var absoluteUrl = combineUrl(window.location.href, task.file); + getPdf(absoluteUrl, function nextTaskGetPdf(data) { var failure; function continuation() { task.pageNum = task.firstPage || 1; diff --git a/test/unit/api_spec.js b/test/unit/api_spec.js index 5b068fbb9..e1e0a318d 100644 --- a/test/unit/api_spec.js +++ b/test/unit/api_spec.js @@ -6,7 +6,7 @@ describe('api', function() { // TODO run with worker enabled PDFJS.disableWorker = true; - var basicApiUrl = '../pdfs/basicapi.pdf'; + var basicApiUrl = combineUrl(window.location.href, '../pdfs/basicapi.pdf'); function waitsForPromise(promise) { waitsFor(function() { return promise.isResolved || promise.isRejected; diff --git a/test/unit/unit_test.html b/test/unit/unit_test.html index ca0a1aed1..0291f1a0f 100644 --- a/test/unit/unit_test.html +++ b/test/unit/unit_test.html @@ -43,6 +43,7 @@ +