2012-09-01 07:48:21 +09:00
|
|
|
/* Copyright 2012 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.
|
|
|
|
*/
|
2014-09-13 23:47:16 +09:00
|
|
|
/* globals PDFJS, createPromiseCapability, LocalPdfManager, NetworkPdfManager,
|
2015-10-22 08:56:27 +09:00
|
|
|
NetworkManager, isInt, MissingPDFException,
|
2014-09-06 10:02:54 +09:00
|
|
|
UnexpectedResponseException, PasswordException, Promise, warn,
|
2014-09-13 23:47:16 +09:00
|
|
|
PasswordResponses, InvalidPDFException, UnknownErrorException,
|
|
|
|
XRefParseException, Ref, info, globalScope, error, MessageHandler */
|
2011-09-08 02:16:02 +09:00
|
|
|
|
2011-10-26 10:18:22 +09:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-21 10:50:32 +09:00
|
|
|
var WorkerTask = (function WorkerTaskClosure() {
|
|
|
|
function WorkerTask(name) {
|
|
|
|
this.name = name;
|
|
|
|
this.terminated = false;
|
|
|
|
this._capability = createPromiseCapability();
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerTask.prototype = {
|
|
|
|
get finished() {
|
|
|
|
return this._capability.promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
finish: function () {
|
|
|
|
this._capability.resolve();
|
|
|
|
},
|
|
|
|
|
|
|
|
terminate: function () {
|
|
|
|
this.terminated = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
ensureNotTerminated: function () {
|
|
|
|
if (this.terminated) {
|
|
|
|
throw new Error('Worker task was terminated');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return WorkerTask;
|
|
|
|
})();
|
|
|
|
|
2013-08-13 02:48:06 +09:00
|
|
|
var WorkerMessageHandler = PDFJS.WorkerMessageHandler = {
|
2015-10-28 00:07:20 +09:00
|
|
|
setup: function wphSetup(handler, port) {
|
|
|
|
handler.on('test', function wphSetupTest(data) {
|
|
|
|
// check if Uint8Array can be sent to worker
|
|
|
|
if (!(data instanceof Uint8Array)) {
|
|
|
|
handler.send('test', 'main', false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// making sure postMessage transfers are working
|
|
|
|
var supportTransfers = data[0] === 255;
|
|
|
|
handler.postMessageTransfers = supportTransfers;
|
|
|
|
// check if the response property is supported by xhr
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
var responseExists = 'response' in xhr;
|
|
|
|
// check if the property is actually implemented
|
|
|
|
try {
|
|
|
|
var dummy = xhr.responseType;
|
|
|
|
} catch (e) {
|
|
|
|
responseExists = false;
|
|
|
|
}
|
|
|
|
if (!responseExists) {
|
|
|
|
handler.send('test', false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
handler.send('test', {
|
|
|
|
supportTypedArray: true,
|
|
|
|
supportTransfers: supportTransfers
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
handler.on('GetDocRequest', function wphSetupDoc(data) {
|
|
|
|
return WorkerMessageHandler.createDocumentHandler(data, port);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
createDocumentHandler: function wphCreateDocumentHandler(data, port) {
|
2013-02-07 08:19:29 +09:00
|
|
|
var pdfManager;
|
2015-10-21 07:45:55 +09:00
|
|
|
var terminated = false;
|
|
|
|
var cancelXHRs = null;
|
2015-10-21 10:50:32 +09:00
|
|
|
var WorkerTasks = [];
|
2015-10-21 07:45:55 +09:00
|
|
|
|
2015-10-28 00:07:20 +09:00
|
|
|
var mainHandlerName = data.docId;
|
|
|
|
var workerHandlerName = data.docId + '_worker';
|
|
|
|
var handler = new MessageHandler(workerHandlerName, mainHandlerName, port);
|
|
|
|
|
2015-10-21 07:45:55 +09:00
|
|
|
function ensureNotTerminated() {
|
|
|
|
if (terminated) {
|
|
|
|
throw new Error('Worker was terminated');
|
|
|
|
}
|
|
|
|
}
|
2011-10-09 17:37:53 +09:00
|
|
|
|
2015-10-21 10:50:32 +09:00
|
|
|
function startWorkerTask(task) {
|
|
|
|
WorkerTasks.push(task);
|
|
|
|
}
|
|
|
|
|
|
|
|
function finishWorkerTask(task) {
|
|
|
|
task.finish();
|
|
|
|
var i = WorkerTasks.indexOf(task);
|
|
|
|
WorkerTasks.splice(i, 1);
|
|
|
|
}
|
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
function loadDocument(recoveryMode) {
|
2014-05-02 08:38:49 +09:00
|
|
|
var loadDocumentCapability = createPromiseCapability();
|
2013-02-07 08:19:29 +09:00
|
|
|
|
|
|
|
var parseSuccess = function parseSuccess() {
|
2014-03-12 17:51:40 +09:00
|
|
|
var numPagesPromise = pdfManager.ensureDoc('numPages');
|
|
|
|
var fingerprintPromise = pdfManager.ensureDoc('fingerprint');
|
2013-02-07 08:19:29 +09:00
|
|
|
var encryptedPromise = pdfManager.ensureXRef('encrypt');
|
2014-05-08 04:06:44 +09:00
|
|
|
Promise.all([numPagesPromise, fingerprintPromise,
|
2014-05-08 04:38:40 +09:00
|
|
|
encryptedPromise]).then(function onDocReady(results) {
|
2013-02-07 08:19:29 +09:00
|
|
|
var doc = {
|
|
|
|
numPages: results[0],
|
|
|
|
fingerprint: results[1],
|
2014-05-08 04:38:40 +09:00
|
|
|
encrypted: !!results[2],
|
2013-02-07 08:19:29 +09:00
|
|
|
};
|
2014-05-02 08:38:49 +09:00
|
|
|
loadDocumentCapability.resolve(doc);
|
2013-05-09 03:55:29 +09:00
|
|
|
},
|
|
|
|
parseFailure);
|
2013-02-07 08:19:29 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
var parseFailure = function parseFailure(e) {
|
2014-05-02 08:38:49 +09:00
|
|
|
loadDocumentCapability.reject(e);
|
2013-02-07 08:19:29 +09:00
|
|
|
};
|
|
|
|
|
2014-03-12 17:51:40 +09:00
|
|
|
pdfManager.ensureDoc('checkHeader', []).then(function() {
|
|
|
|
pdfManager.ensureDoc('parseStartXRef', []).then(function() {
|
|
|
|
pdfManager.ensureDoc('parse', [recoveryMode]).then(
|
2014-03-23 04:59:16 +09:00
|
|
|
parseSuccess, parseFailure);
|
2013-05-10 00:54:53 +09:00
|
|
|
}, parseFailure);
|
|
|
|
}, parseFailure);
|
2013-02-07 08:19:29 +09:00
|
|
|
|
2014-05-02 08:38:49 +09:00
|
|
|
return loadDocumentCapability.promise;
|
2013-02-07 08:19:29 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
function getPdfManager(data) {
|
2014-05-02 08:38:49 +09:00
|
|
|
var pdfManagerCapability = createPromiseCapability();
|
2015-10-21 07:45:55 +09:00
|
|
|
var pdfManager;
|
2013-02-07 08:19:29 +09:00
|
|
|
|
|
|
|
var source = data.source;
|
|
|
|
var disableRange = data.disableRange;
|
|
|
|
if (source.data) {
|
2013-05-31 06:54:49 +09:00
|
|
|
try {
|
|
|
|
pdfManager = new LocalPdfManager(source.data, source.password);
|
2015-10-21 07:45:55 +09:00
|
|
|
pdfManagerCapability.resolve(pdfManager);
|
2013-05-31 06:54:49 +09:00
|
|
|
} catch (ex) {
|
2014-05-02 08:38:49 +09:00
|
|
|
pdfManagerCapability.reject(ex);
|
2013-05-31 06:54:49 +09:00
|
|
|
}
|
2014-05-02 08:38:49 +09:00
|
|
|
return pdfManagerCapability.promise;
|
2013-02-07 08:19:29 +09:00
|
|
|
} else if (source.chunkedViewerLoading) {
|
2013-05-31 06:54:49 +09:00
|
|
|
try {
|
|
|
|
pdfManager = new NetworkPdfManager(source, handler);
|
2015-10-21 07:45:55 +09:00
|
|
|
pdfManagerCapability.resolve(pdfManager);
|
2013-05-31 06:54:49 +09:00
|
|
|
} catch (ex) {
|
2014-05-02 08:38:49 +09:00
|
|
|
pdfManagerCapability.reject(ex);
|
2013-05-31 06:54:49 +09:00
|
|
|
}
|
2014-05-02 08:38:49 +09:00
|
|
|
return pdfManagerCapability.promise;
|
2013-02-07 08:19:29 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
var networkManager = new NetworkManager(source.url, {
|
2014-01-15 17:57:17 +09:00
|
|
|
httpHeaders: source.httpHeaders,
|
|
|
|
withCredentials: source.withCredentials
|
2013-02-07 08:19:29 +09:00
|
|
|
});
|
2014-09-06 10:02:54 +09:00
|
|
|
var cachedChunks = [];
|
2013-02-07 08:19:29 +09:00
|
|
|
var fullRequestXhrId = networkManager.requestFull({
|
|
|
|
onHeadersReceived: function onHeadersReceived() {
|
|
|
|
if (disableRange) {
|
|
|
|
return;
|
2012-05-15 03:45:07 +09:00
|
|
|
}
|
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
var fullRequestXhr = networkManager.getRequestXhr(fullRequestXhrId);
|
|
|
|
if (fullRequestXhr.getResponseHeader('Accept-Ranges') !== 'bytes') {
|
|
|
|
return;
|
|
|
|
}
|
2012-10-16 19:10:37 +09:00
|
|
|
|
2013-04-21 09:36:10 +09:00
|
|
|
var contentEncoding =
|
|
|
|
fullRequestXhr.getResponseHeader('Content-Encoding') || 'identity';
|
|
|
|
if (contentEncoding !== 'identity') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
var length = fullRequestXhr.getResponseHeader('Content-Length');
|
|
|
|
length = parseInt(length, 10);
|
|
|
|
if (!isInt(length)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-07 07:05:19 +09:00
|
|
|
source.length = length;
|
2015-10-22 08:56:27 +09:00
|
|
|
if (length <= 2 * source.rangeChunkSize) {
|
2014-02-07 07:05:19 +09:00
|
|
|
// The file size is smaller than the size of two chunks, so it does
|
|
|
|
// not make any sense to abort the request and retry with a range
|
|
|
|
// request.
|
|
|
|
return;
|
|
|
|
}
|
2013-01-30 03:13:28 +09:00
|
|
|
|
2014-09-06 10:02:54 +09:00
|
|
|
if (networkManager.isStreamingRequest(fullRequestXhrId)) {
|
|
|
|
// We can continue fetching when progressive loading is enabled,
|
|
|
|
// and we don't need the autoFetch feature.
|
|
|
|
source.disableAutoFetch = true;
|
|
|
|
} else {
|
|
|
|
// NOTE: by cancelling the full request, and then issuing range
|
|
|
|
// requests, there will be an issue for sites where you can only
|
|
|
|
// request the pdf once. However, if this is the case, then the
|
|
|
|
// server should not be returning that it can support range
|
|
|
|
// requests.
|
|
|
|
networkManager.abortRequest(fullRequestXhrId);
|
|
|
|
}
|
2013-02-07 08:19:29 +09:00
|
|
|
|
2013-05-31 06:54:49 +09:00
|
|
|
try {
|
|
|
|
pdfManager = new NetworkPdfManager(source, handler);
|
2014-05-02 08:38:49 +09:00
|
|
|
pdfManagerCapability.resolve(pdfManager);
|
2013-05-31 06:54:49 +09:00
|
|
|
} catch (ex) {
|
2014-05-02 08:38:49 +09:00
|
|
|
pdfManagerCapability.reject(ex);
|
2013-05-31 06:54:49 +09:00
|
|
|
}
|
2015-10-21 07:45:55 +09:00
|
|
|
cancelXHRs = null;
|
2013-02-07 08:19:29 +09:00
|
|
|
},
|
|
|
|
|
2014-10-02 00:41:56 +09:00
|
|
|
onProgressiveData: source.disableStream ? null :
|
2014-09-06 10:02:54 +09:00
|
|
|
function onProgressiveData(chunk) {
|
|
|
|
if (!pdfManager) {
|
|
|
|
cachedChunks.push(chunk);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pdfManager.sendProgressiveData(chunk);
|
|
|
|
},
|
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
onDone: function onDone(args) {
|
2014-09-06 10:02:54 +09:00
|
|
|
if (pdfManager) {
|
|
|
|
return; // already processed
|
|
|
|
}
|
|
|
|
|
|
|
|
var pdfFile;
|
|
|
|
if (args === null) {
|
|
|
|
// TODO add some streaming manager, e.g. for unknown length files.
|
|
|
|
// The data was returned in the onProgressiveData, combining...
|
|
|
|
var pdfFileLength = 0, pos = 0;
|
|
|
|
cachedChunks.forEach(function (chunk) {
|
|
|
|
pdfFileLength += chunk.byteLength;
|
|
|
|
});
|
|
|
|
if (source.length && pdfFileLength !== source.length) {
|
|
|
|
warn('reported HTTP length is different from actual');
|
|
|
|
}
|
|
|
|
var pdfFileArray = new Uint8Array(pdfFileLength);
|
|
|
|
cachedChunks.forEach(function (chunk) {
|
|
|
|
pdfFileArray.set(new Uint8Array(chunk), pos);
|
|
|
|
pos += chunk.byteLength;
|
|
|
|
});
|
|
|
|
pdfFile = pdfFileArray.buffer;
|
|
|
|
} else {
|
|
|
|
pdfFile = args.chunk;
|
|
|
|
}
|
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
// the data is array, instantiating directly from it
|
2013-05-31 06:54:49 +09:00
|
|
|
try {
|
2014-09-06 10:02:54 +09:00
|
|
|
pdfManager = new LocalPdfManager(pdfFile, source.password);
|
2015-10-21 07:45:55 +09:00
|
|
|
pdfManagerCapability.resolve(pdfManager);
|
2013-05-31 06:54:49 +09:00
|
|
|
} catch (ex) {
|
2014-05-02 08:38:49 +09:00
|
|
|
pdfManagerCapability.reject(ex);
|
2013-05-31 06:54:49 +09:00
|
|
|
}
|
2015-10-21 07:45:55 +09:00
|
|
|
cancelXHRs = null;
|
2013-02-07 08:19:29 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
onError: function onError(status) {
|
2014-09-13 23:47:16 +09:00
|
|
|
var exception;
|
2015-11-09 02:03:28 +09:00
|
|
|
if (status === 404 || status === 0 && /^file:/.test(source.url)) {
|
2014-09-13 23:47:16 +09:00
|
|
|
exception = new MissingPDFException('Missing PDF "' +
|
|
|
|
source.url + '".');
|
2014-08-23 23:03:49 +09:00
|
|
|
handler.send('MissingPDF', exception);
|
2013-02-07 08:19:29 +09:00
|
|
|
} else {
|
2014-09-13 23:47:16 +09:00
|
|
|
exception = new UnexpectedResponseException(
|
|
|
|
'Unexpected server response (' + status +
|
|
|
|
') while retrieving PDF "' + source.url + '".', status);
|
|
|
|
handler.send('UnexpectedResponse', exception);
|
2013-02-07 08:19:29 +09:00
|
|
|
}
|
2015-10-21 07:45:55 +09:00
|
|
|
cancelXHRs = null;
|
2013-02-07 08:19:29 +09:00
|
|
|
},
|
2012-10-16 19:10:37 +09:00
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
onProgress: function onProgress(evt) {
|
|
|
|
handler.send('DocProgress', {
|
|
|
|
loaded: evt.loaded,
|
2014-01-31 07:03:15 +09:00
|
|
|
total: evt.lengthComputable ? evt.total : source.length
|
2013-02-07 08:19:29 +09:00
|
|
|
});
|
2012-05-15 03:45:07 +09:00
|
|
|
}
|
2013-02-07 08:19:29 +09:00
|
|
|
});
|
|
|
|
|
2015-10-21 07:45:55 +09:00
|
|
|
cancelXHRs = function () {
|
|
|
|
networkManager.abortRequest(fullRequestXhrId);
|
|
|
|
};
|
|
|
|
|
2014-05-02 08:38:49 +09:00
|
|
|
return pdfManagerCapability.promise;
|
2012-06-24 04:48:33 +09:00
|
|
|
}
|
|
|
|
|
2015-10-28 00:07:20 +09:00
|
|
|
var setupDoc = function(data) {
|
2013-02-07 08:19:29 +09:00
|
|
|
var onSuccess = function(doc) {
|
2015-10-21 07:45:55 +09:00
|
|
|
ensureNotTerminated();
|
2013-02-07 08:19:29 +09:00
|
|
|
handler.send('GetDoc', { pdfInfo: doc });
|
|
|
|
};
|
|
|
|
|
|
|
|
var onFailure = function(e) {
|
|
|
|
if (e instanceof PasswordException) {
|
2013-05-10 07:35:23 +09:00
|
|
|
if (e.code === PasswordResponses.NEED_PASSWORD) {
|
2014-08-23 23:03:49 +09:00
|
|
|
handler.send('NeedPassword', e);
|
2013-05-10 07:35:23 +09:00
|
|
|
} else if (e.code === PasswordResponses.INCORRECT_PASSWORD) {
|
2014-08-23 23:03:49 +09:00
|
|
|
handler.send('IncorrectPassword', e);
|
2013-02-07 08:19:29 +09:00
|
|
|
}
|
|
|
|
} else if (e instanceof InvalidPDFException) {
|
2014-08-23 23:03:49 +09:00
|
|
|
handler.send('InvalidPDF', e);
|
2013-02-07 08:19:29 +09:00
|
|
|
} else if (e instanceof MissingPDFException) {
|
2014-08-23 23:03:49 +09:00
|
|
|
handler.send('MissingPDF', e);
|
2014-09-13 23:47:16 +09:00
|
|
|
} else if (e instanceof UnexpectedResponseException) {
|
|
|
|
handler.send('UnexpectedResponse', e);
|
2013-02-07 08:19:29 +09:00
|
|
|
} else {
|
2014-08-23 23:03:49 +09:00
|
|
|
handler.send('UnknownError',
|
|
|
|
new UnknownErrorException(e.message, e.toString()));
|
2013-02-07 08:19:29 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-10-21 07:45:55 +09:00
|
|
|
ensureNotTerminated();
|
|
|
|
|
2013-07-11 01:52:37 +09:00
|
|
|
PDFJS.maxImageSize = data.maxImageSize === undefined ?
|
|
|
|
-1 : data.maxImageSize;
|
2013-08-20 08:33:20 +09:00
|
|
|
PDFJS.disableFontFace = data.disableFontFace;
|
2014-01-11 07:30:41 +09:00
|
|
|
PDFJS.disableCreateObjectURL = data.disableCreateObjectURL;
|
2013-12-19 06:39:03 +09:00
|
|
|
PDFJS.verbosity = data.verbosity;
|
2014-02-12 03:27:09 +09:00
|
|
|
PDFJS.cMapUrl = data.cMapUrl === undefined ?
|
|
|
|
null : data.cMapUrl;
|
2014-03-15 03:22:02 +09:00
|
|
|
PDFJS.cMapPacked = data.cMapPacked === true;
|
2013-07-11 01:52:37 +09:00
|
|
|
|
2015-10-21 07:45:55 +09:00
|
|
|
getPdfManager(data).then(function (newPdfManager) {
|
|
|
|
if (terminated) {
|
|
|
|
// We were in a process of setting up the manager, but it got
|
|
|
|
// terminated in the middle.
|
|
|
|
newPdfManager.terminate();
|
|
|
|
throw new Error('Worker was terminated');
|
|
|
|
}
|
|
|
|
|
|
|
|
pdfManager = newPdfManager;
|
|
|
|
|
2014-09-06 10:02:54 +09:00
|
|
|
handler.send('PDFManagerReady', null);
|
2014-01-29 06:13:47 +09:00
|
|
|
pdfManager.onLoadedStream().then(function(stream) {
|
|
|
|
handler.send('DataLoaded', { length: stream.bytes.byteLength });
|
|
|
|
});
|
|
|
|
}).then(function pdfManagerReady() {
|
2015-10-21 07:45:55 +09:00
|
|
|
ensureNotTerminated();
|
|
|
|
|
2013-05-10 07:35:23 +09:00
|
|
|
loadDocument(false).then(onSuccess, function loadFailure(ex) {
|
2015-10-21 07:45:55 +09:00
|
|
|
ensureNotTerminated();
|
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
// Try again with recoveryMode == true
|
|
|
|
if (!(ex instanceof XRefParseException)) {
|
2013-05-10 07:35:23 +09:00
|
|
|
if (ex instanceof PasswordException) {
|
|
|
|
// after password exception prepare to receive a new password
|
|
|
|
// to repeat loading
|
2014-05-02 08:38:49 +09:00
|
|
|
pdfManager.passwordChanged().then(pdfManagerReady);
|
2013-05-10 07:35:23 +09:00
|
|
|
}
|
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
onFailure(ex);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-13 03:37:49 +09:00
|
|
|
pdfManager.requestLoadedStream();
|
2013-02-07 08:19:29 +09:00
|
|
|
pdfManager.onLoadedStream().then(function() {
|
2015-10-21 07:45:55 +09:00
|
|
|
ensureNotTerminated();
|
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
loadDocument(true).then(onSuccess, onFailure);
|
|
|
|
});
|
2013-05-31 06:54:49 +09:00
|
|
|
}, onFailure);
|
|
|
|
}, onFailure);
|
2015-10-28 00:07:20 +09:00
|
|
|
};
|
2012-04-12 07:52:15 +09:00
|
|
|
|
2014-05-09 05:02:53 +09:00
|
|
|
handler.on('GetPage', function wphSetupGetPage(data) {
|
|
|
|
return pdfManager.getPage(data.pageIndex).then(function(page) {
|
2013-02-07 08:19:29 +09:00
|
|
|
var rotatePromise = pdfManager.ensure(page, 'rotate');
|
|
|
|
var refPromise = pdfManager.ensure(page, 'ref');
|
|
|
|
var viewPromise = pdfManager.ensure(page, 'view');
|
|
|
|
|
2014-05-09 05:02:53 +09:00
|
|
|
return Promise.all([rotatePromise, refPromise, viewPromise]).then(
|
2013-02-07 08:19:29 +09:00
|
|
|
function(results) {
|
2014-05-09 05:02:53 +09:00
|
|
|
return {
|
2013-02-07 08:19:29 +09:00
|
|
|
rotate: results[0],
|
|
|
|
ref: results[1],
|
|
|
|
view: results[2]
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
2011-09-08 02:16:02 +09:00
|
|
|
});
|
2011-10-09 17:37:53 +09:00
|
|
|
|
2014-05-08 08:15:25 +09:00
|
|
|
handler.on('GetPageIndex', function wphSetupGetPageIndex(data) {
|
2013-11-14 08:27:46 +09:00
|
|
|
var ref = new Ref(data.ref.num, data.ref.gen);
|
2014-03-12 17:51:40 +09:00
|
|
|
var catalog = pdfManager.pdfDocument.catalog;
|
2014-05-08 08:15:25 +09:00
|
|
|
return catalog.getPageIndex(ref);
|
2013-11-14 08:27:46 +09:00
|
|
|
});
|
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
handler.on('GetDestinations',
|
2014-05-08 08:15:25 +09:00
|
|
|
function wphSetupGetDestinations(data) {
|
|
|
|
return pdfManager.ensureCatalog('destinations');
|
2013-02-07 08:19:29 +09:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2014-10-05 22:56:40 +09:00
|
|
|
handler.on('GetDestination',
|
|
|
|
function wphSetupGetDestination(data) {
|
2015-11-22 21:56:52 +09:00
|
|
|
return pdfManager.ensureCatalog('getDestination', [data.id]);
|
2014-10-05 22:56:40 +09:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2014-03-19 05:32:47 +09:00
|
|
|
handler.on('GetAttachments',
|
2014-05-08 08:15:25 +09:00
|
|
|
function wphSetupGetAttachments(data) {
|
|
|
|
return pdfManager.ensureCatalog('attachments');
|
2014-03-19 05:32:47 +09:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2014-05-08 04:15:34 +09:00
|
|
|
handler.on('GetJavaScript',
|
2014-05-08 08:15:25 +09:00
|
|
|
function wphSetupGetJavaScript(data) {
|
|
|
|
return pdfManager.ensureCatalog('javaScript');
|
2014-05-08 04:15:34 +09:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2014-05-08 04:06:44 +09:00
|
|
|
handler.on('GetOutline',
|
2014-05-08 08:15:25 +09:00
|
|
|
function wphSetupGetOutline(data) {
|
|
|
|
return pdfManager.ensureCatalog('documentOutline');
|
2014-05-08 04:06:44 +09:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2014-05-08 04:38:40 +09:00
|
|
|
handler.on('GetMetadata',
|
2014-05-08 08:15:25 +09:00
|
|
|
function wphSetupGetMetadata(data) {
|
|
|
|
return Promise.all([pdfManager.ensureDoc('documentInfo'),
|
|
|
|
pdfManager.ensureCatalog('metadata')]);
|
2014-05-08 04:38:40 +09:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2014-05-08 08:15:25 +09:00
|
|
|
handler.on('GetData', function wphSetupGetData(data) {
|
2013-02-07 08:19:29 +09:00
|
|
|
pdfManager.requestLoadedStream();
|
2014-05-08 08:15:25 +09:00
|
|
|
return pdfManager.onLoadedStream().then(function(stream) {
|
|
|
|
return stream.bytes;
|
2013-02-07 08:19:29 +09:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-06-16 23:52:04 +09:00
|
|
|
handler.on('GetStats',
|
|
|
|
function wphSetupGetStats(data) {
|
|
|
|
return pdfManager.pdfDocument.xref.stats;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2013-05-10 07:35:23 +09:00
|
|
|
handler.on('UpdatePassword', function wphSetupUpdatePassword(data) {
|
|
|
|
pdfManager.updatePassword(data);
|
|
|
|
});
|
|
|
|
|
2014-05-09 05:02:53 +09:00
|
|
|
handler.on('GetAnnotations', function wphSetupGetAnnotations(data) {
|
|
|
|
return pdfManager.getPage(data.pageIndex).then(function(page) {
|
2015-11-22 21:56:52 +09:00
|
|
|
return pdfManager.ensure(page, 'getAnnotationsData', [data.intent]);
|
2012-04-15 05:54:31 +09:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
handler.on('RenderPageRequest', function wphSetupRenderPage(data) {
|
2015-10-21 10:50:32 +09:00
|
|
|
var pageIndex = data.pageIndex;
|
|
|
|
pdfManager.getPage(pageIndex).then(function(page) {
|
|
|
|
var task = new WorkerTask('RenderPageRequest: page ' + pageIndex);
|
|
|
|
startWorkerTask(task);
|
2011-09-08 02:16:02 +09:00
|
|
|
|
2015-10-21 10:50:32 +09:00
|
|
|
var pageNum = pageIndex + 1;
|
2013-02-07 08:19:29 +09:00
|
|
|
var start = Date.now();
|
2011-11-29 09:55:09 +09:00
|
|
|
// Pre compile the pdf page and fetch the fonts/images.
|
2015-10-21 10:50:32 +09:00
|
|
|
page.getOperatorList(handler, task, data.intent).then(
|
|
|
|
function(operatorList) {
|
|
|
|
finishWorkerTask(task);
|
2013-02-01 08:33:59 +09:00
|
|
|
|
2014-02-17 13:23:18 +09:00
|
|
|
info('page=' + pageNum + ' - getOperatorList: time=' +
|
2015-10-27 00:38:06 +09:00
|
|
|
(Date.now() - start) + 'ms, len=' + operatorList.totalLength);
|
2013-02-07 08:19:29 +09:00
|
|
|
}, function(e) {
|
2015-10-21 10:50:32 +09:00
|
|
|
finishWorkerTask(task);
|
|
|
|
if (task.terminated) {
|
|
|
|
return; // ignoring errors from the terminated thread
|
|
|
|
}
|
2011-09-08 02:16:02 +09:00
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
var minimumStackMessage =
|
2014-03-23 04:59:16 +09:00
|
|
|
'worker.js: while trying to getPage() and getOperatorList()';
|
2011-09-08 02:16:02 +09:00
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
var wrappedException;
|
|
|
|
|
|
|
|
// Turn the error into an obj that can be serialized
|
|
|
|
if (typeof e === 'string') {
|
|
|
|
wrappedException = {
|
|
|
|
message: e,
|
|
|
|
stack: minimumStackMessage
|
|
|
|
};
|
|
|
|
} else if (typeof e === 'object') {
|
|
|
|
wrappedException = {
|
|
|
|
message: e.message || e.toString(),
|
|
|
|
stack: e.stack || minimumStackMessage
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
wrappedException = {
|
|
|
|
message: 'Unknown exception type: ' + (typeof e),
|
|
|
|
stack: minimumStackMessage
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
handler.send('PageError', {
|
|
|
|
pageNum: pageNum,
|
2014-03-07 23:48:42 +09:00
|
|
|
error: wrappedException,
|
|
|
|
intent: data.intent
|
2013-02-07 08:19:29 +09:00
|
|
|
});
|
|
|
|
});
|
2011-09-08 02:16:02 +09:00
|
|
|
});
|
|
|
|
}, this);
|
2011-10-09 17:37:53 +09:00
|
|
|
|
2014-05-08 08:15:25 +09:00
|
|
|
handler.on('GetTextContent', function wphExtractText(data) {
|
2015-10-21 10:50:32 +09:00
|
|
|
var pageIndex = data.pageIndex;
|
|
|
|
return pdfManager.getPage(pageIndex).then(function(page) {
|
|
|
|
var task = new WorkerTask('GetTextContent: page ' + pageIndex);
|
|
|
|
startWorkerTask(task);
|
|
|
|
var pageNum = pageIndex + 1;
|
2013-02-07 08:19:29 +09:00
|
|
|
var start = Date.now();
|
2015-10-21 10:50:32 +09:00
|
|
|
return page.extractTextContent(task).then(function(textContent) {
|
|
|
|
finishWorkerTask(task);
|
2014-02-17 13:23:18 +09:00
|
|
|
info('text indexing: page=' + pageNum + ' - time=' +
|
|
|
|
(Date.now() - start) + 'ms');
|
2014-05-08 08:15:25 +09:00
|
|
|
return textContent;
|
2015-10-21 10:50:32 +09:00
|
|
|
}, function (reason) {
|
|
|
|
finishWorkerTask(task);
|
|
|
|
if (task.terminated) {
|
|
|
|
return; // ignoring errors from the terminated thread
|
|
|
|
}
|
|
|
|
throw reason;
|
2013-02-07 08:19:29 +09:00
|
|
|
});
|
|
|
|
});
|
2011-12-11 08:24:54 +09:00
|
|
|
});
|
2013-04-13 03:37:49 +09:00
|
|
|
|
2014-05-08 08:15:25 +09:00
|
|
|
handler.on('Cleanup', function wphCleanup(data) {
|
2014-05-10 10:21:15 +09:00
|
|
|
return pdfManager.cleanup();
|
2013-11-15 06:43:38 +09:00
|
|
|
});
|
|
|
|
|
2014-05-08 08:15:25 +09:00
|
|
|
handler.on('Terminate', function wphTerminate(data) {
|
2015-10-21 07:45:55 +09:00
|
|
|
terminated = true;
|
|
|
|
if (pdfManager) {
|
|
|
|
pdfManager.terminate();
|
|
|
|
pdfManager = null;
|
|
|
|
}
|
|
|
|
if (cancelXHRs) {
|
|
|
|
cancelXHRs();
|
|
|
|
}
|
2015-10-21 10:50:32 +09:00
|
|
|
|
|
|
|
var waitOn = [];
|
|
|
|
WorkerTasks.forEach(function (task) {
|
|
|
|
waitOn.push(task.finished);
|
|
|
|
task.terminate();
|
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.all(waitOn).then(function () {});
|
2013-04-13 03:37:49 +09:00
|
|
|
});
|
2015-10-28 00:07:20 +09:00
|
|
|
|
|
|
|
setupDoc(data);
|
|
|
|
return workerHandlerName;
|
2011-09-08 02:16:02 +09:00
|
|
|
}
|
2011-10-09 17:37:53 +09:00
|
|
|
};
|
2011-10-26 02:16:20 +09:00
|
|
|
|
2011-10-26 07:43:41 +09:00
|
|
|
var consoleTimer = {};
|
|
|
|
|
2011-10-26 02:16:20 +09:00
|
|
|
var workerConsole = {
|
|
|
|
log: function log() {
|
|
|
|
var args = Array.prototype.slice.call(arguments);
|
2012-08-31 20:40:37 +09:00
|
|
|
globalScope.postMessage({
|
2015-10-28 00:07:20 +09:00
|
|
|
targetName: 'main',
|
2011-10-26 02:16:20 +09:00
|
|
|
action: 'console_log',
|
|
|
|
data: args
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
error: function error() {
|
|
|
|
var args = Array.prototype.slice.call(arguments);
|
2012-08-31 20:40:37 +09:00
|
|
|
globalScope.postMessage({
|
2015-10-28 00:07:20 +09:00
|
|
|
targetName: 'main',
|
2011-10-26 02:16:20 +09:00
|
|
|
action: 'console_error',
|
|
|
|
data: args
|
|
|
|
});
|
2012-01-30 05:25:06 +09:00
|
|
|
throw 'pdf.js execution error';
|
2011-10-26 02:16:20 +09:00
|
|
|
},
|
|
|
|
|
2011-10-29 03:23:30 +09:00
|
|
|
time: function time(name) {
|
2011-10-26 02:16:20 +09:00
|
|
|
consoleTimer[name] = Date.now();
|
|
|
|
},
|
|
|
|
|
2011-10-29 03:23:30 +09:00
|
|
|
timeEnd: function timeEnd(name) {
|
2011-10-26 02:16:20 +09:00
|
|
|
var time = consoleTimer[name];
|
2013-02-01 08:33:59 +09:00
|
|
|
if (!time) {
|
2014-01-16 06:28:31 +09:00
|
|
|
error('Unknown timer name ' + name);
|
2011-10-26 02:16:20 +09:00
|
|
|
}
|
|
|
|
this.log('Timer:', name, Date.now() - time);
|
|
|
|
}
|
2011-10-26 02:43:28 +09:00
|
|
|
};
|
2011-10-26 07:43:41 +09:00
|
|
|
|
2014-01-16 06:28:31 +09:00
|
|
|
|
2011-10-26 07:43:41 +09:00
|
|
|
// Worker thread?
|
|
|
|
if (typeof window === 'undefined') {
|
2014-01-16 06:28:31 +09:00
|
|
|
if (!('console' in globalScope)) {
|
|
|
|
globalScope.console = workerConsole;
|
|
|
|
}
|
2011-10-26 07:43:41 +09:00
|
|
|
|
2014-01-04 02:34:13 +09:00
|
|
|
// Listen for unsupported features so we can pass them on to the main thread.
|
|
|
|
PDFJS.UnsupportedManager.listen(function (msg) {
|
|
|
|
globalScope.postMessage({
|
2015-10-28 00:07:20 +09:00
|
|
|
targetName: 'main',
|
2014-01-04 02:34:13 +09:00
|
|
|
action: '_unsupported_feature',
|
|
|
|
data: msg
|
|
|
|
});
|
2012-05-15 09:19:09 +09:00
|
|
|
});
|
|
|
|
|
2015-10-28 00:07:20 +09:00
|
|
|
var handler = new MessageHandler('worker', 'main', this);
|
|
|
|
WorkerMessageHandler.setup(handler, this);
|
2011-10-26 07:43:41 +09:00
|
|
|
}
|