2011-10-26 10:18:22 +09:00
|
|
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2011-09-08 02:16:02 +09:00
|
|
|
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
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.
|
|
|
|
*/
|
2013-04-05 04:24:06 +09:00
|
|
|
/* globals error, globalScope, InvalidPDFException, log,
|
2013-02-03 07:49:19 +09:00
|
|
|
MissingPDFException, PasswordException, PDFDocument, PDFJS, Promise,
|
2013-02-07 08:19:29 +09:00
|
|
|
Stream, UnknownErrorException, warn, NetworkManager, LocalPdfManager,
|
|
|
|
NetworkPdfManager, XRefParseException, NotImplementedException,
|
|
|
|
isInt */
|
2011-09-08 02:16:02 +09:00
|
|
|
|
2011-10-26 10:18:22 +09:00
|
|
|
'use strict';
|
|
|
|
|
2011-10-26 02:16:20 +09:00
|
|
|
function MessageHandler(name, comObj) {
|
|
|
|
this.name = name;
|
|
|
|
this.comObj = comObj;
|
2011-12-08 08:36:27 +09:00
|
|
|
this.callbackIndex = 1;
|
2011-12-09 07:51:26 +09:00
|
|
|
var callbacks = this.callbacks = {};
|
2011-10-26 02:16:20 +09:00
|
|
|
var ah = this.actionHandler = {};
|
|
|
|
|
2011-10-29 03:23:30 +09:00
|
|
|
ah['console_log'] = [function ahConsoleLog(data) {
|
2012-08-31 20:40:37 +09:00
|
|
|
log.apply(null, data);
|
2012-05-15 09:19:09 +09:00
|
|
|
}];
|
2012-08-31 22:51:31 +09:00
|
|
|
// If there's no console available, console_error in the
|
|
|
|
// action handler will do nothing.
|
2012-08-31 20:40:37 +09:00
|
|
|
if ('console' in globalScope) {
|
|
|
|
ah['console_error'] = [function ahConsoleError(data) {
|
|
|
|
globalScope['console'].error.apply(null, data);
|
|
|
|
}];
|
|
|
|
} else {
|
|
|
|
ah['console_error'] = [function ahConsoleError(data) {
|
2012-10-05 00:01:53 +09:00
|
|
|
log.apply(null, data);
|
2012-08-31 20:40:37 +09:00
|
|
|
}];
|
|
|
|
}
|
2012-05-15 09:19:09 +09:00
|
|
|
ah['_warn'] = [function ah_Warn(data) {
|
|
|
|
warn(data);
|
2011-10-26 02:16:20 +09:00
|
|
|
}];
|
2011-12-09 07:51:26 +09:00
|
|
|
|
|
|
|
comObj.onmessage = function messageHandlerComObjOnMessage(event) {
|
|
|
|
var data = event.data;
|
|
|
|
if (data.isReply) {
|
2011-12-08 08:36:27 +09:00
|
|
|
var callbackId = data.callbackId;
|
2011-12-09 07:51:26 +09:00
|
|
|
if (data.callbackId in callbacks) {
|
|
|
|
var callback = callbacks[callbackId];
|
|
|
|
delete callbacks[callbackId];
|
2011-12-08 08:36:27 +09:00
|
|
|
callback(data.data);
|
|
|
|
} else {
|
2012-01-25 05:04:59 +09:00
|
|
|
error('Cannot resolve callback ' + callbackId);
|
2011-12-08 08:36:27 +09:00
|
|
|
}
|
2011-12-09 07:51:26 +09:00
|
|
|
} else if (data.action in ah) {
|
2011-10-26 02:16:20 +09:00
|
|
|
var action = ah[data.action];
|
2011-12-15 07:02:00 +09:00
|
|
|
if (data.callbackId) {
|
|
|
|
var promise = new Promise();
|
|
|
|
promise.then(function(resolvedData) {
|
|
|
|
comObj.postMessage({
|
|
|
|
isReply: true,
|
|
|
|
callbackId: data.callbackId,
|
|
|
|
data: resolvedData
|
|
|
|
});
|
|
|
|
});
|
|
|
|
action[0].call(action[1], data.data, promise);
|
|
|
|
} else {
|
|
|
|
action[0].call(action[1], data.data);
|
|
|
|
}
|
2011-10-26 02:16:20 +09:00
|
|
|
} else {
|
2012-01-25 05:04:59 +09:00
|
|
|
error('Unkown action from worker: ' + data.action);
|
2011-10-26 02:16:20 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageHandler.prototype = {
|
2011-10-29 03:23:30 +09:00
|
|
|
on: function messageHandlerOn(actionName, handler, scope) {
|
2011-10-26 02:16:20 +09:00
|
|
|
var ah = this.actionHandler;
|
|
|
|
if (ah[actionName]) {
|
2012-01-25 05:04:59 +09:00
|
|
|
error('There is already an actionName called "' + actionName + '"');
|
2011-10-26 02:16:20 +09:00
|
|
|
}
|
|
|
|
ah[actionName] = [handler, scope];
|
|
|
|
},
|
2011-12-13 04:38:16 +09:00
|
|
|
/**
|
|
|
|
* Sends a message to the comObj to invoke the action with the supplied data.
|
|
|
|
* @param {String} actionName Action to call.
|
|
|
|
* @param {JSON} data JSON data to send.
|
|
|
|
* @param {function} [callback] Optional callback that will handle a reply.
|
|
|
|
*/
|
2011-12-08 08:36:27 +09:00
|
|
|
send: function messageHandlerSend(actionName, data, callback) {
|
|
|
|
var message = {
|
2011-10-26 02:16:20 +09:00
|
|
|
action: actionName,
|
|
|
|
data: data
|
2011-12-08 08:36:27 +09:00
|
|
|
};
|
|
|
|
if (callback) {
|
|
|
|
var callbackId = this.callbackIndex++;
|
|
|
|
this.callbacks[callbackId] = callback;
|
|
|
|
message.callbackId = callbackId;
|
|
|
|
}
|
|
|
|
this.comObj.postMessage(message);
|
2011-10-26 02:16:20 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-11-03 23:30:53 +09:00
|
|
|
var WorkerMessageHandler = {
|
2011-10-29 03:23:30 +09:00
|
|
|
setup: function wphSetup(handler) {
|
2013-02-07 08:19:29 +09:00
|
|
|
var pdfManager;
|
2011-10-09 17:37:53 +09:00
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
function loadDocument(recoveryMode) {
|
2013-04-19 02:41:33 +09:00
|
|
|
var loadDocumentPromise = new Promise();
|
2013-02-07 08:19:29 +09:00
|
|
|
|
|
|
|
var parseSuccess = function parseSuccess() {
|
|
|
|
var numPagesPromise = pdfManager.ensureModel('numPages');
|
|
|
|
var fingerprintPromise = pdfManager.ensureModel('fingerprint');
|
|
|
|
var outlinePromise = pdfManager.ensureCatalog('documentOutline');
|
|
|
|
var infoPromise = pdfManager.ensureModel('documentInfo');
|
|
|
|
var metadataPromise = pdfManager.ensureCatalog('metadata');
|
|
|
|
var encryptedPromise = pdfManager.ensureXRef('encrypt');
|
|
|
|
var javaScriptPromise = pdfManager.ensureCatalog('javaScript');
|
2013-04-19 02:41:33 +09:00
|
|
|
Promise.all([numPagesPromise, fingerprintPromise, outlinePromise,
|
2013-02-07 08:19:29 +09:00
|
|
|
infoPromise, metadataPromise, encryptedPromise,
|
|
|
|
javaScriptPromise]).then(
|
|
|
|
function onDocReady(results) {
|
|
|
|
|
|
|
|
var doc = {
|
|
|
|
numPages: results[0],
|
|
|
|
fingerprint: results[1],
|
|
|
|
outline: results[2],
|
|
|
|
info: results[3],
|
|
|
|
metadata: results[4],
|
|
|
|
encrypted: !!results[5],
|
|
|
|
javaScript: results[6]
|
|
|
|
};
|
|
|
|
loadDocumentPromise.resolve(doc);
|
2013-05-09 03:55:29 +09:00
|
|
|
},
|
|
|
|
parseFailure);
|
2013-02-07 08:19:29 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
var parseFailure = function parseFailure(e) {
|
|
|
|
loadDocumentPromise.reject(e);
|
|
|
|
};
|
|
|
|
|
2013-05-10 00:54:53 +09:00
|
|
|
pdfManager.ensureModel('checkHeader', []).then(function() {
|
|
|
|
pdfManager.ensureModel('parseStartXRef', []).then(function() {
|
|
|
|
pdfManager.ensureModel('parse', [recoveryMode]).then(
|
|
|
|
parseSuccess, parseFailure);
|
|
|
|
}, parseFailure);
|
|
|
|
}, parseFailure);
|
2013-02-07 08:19:29 +09:00
|
|
|
|
|
|
|
return loadDocumentPromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPdfManager(data) {
|
2013-04-19 02:41:33 +09:00
|
|
|
var pdfManagerPromise = new Promise();
|
2013-02-07 08:19:29 +09:00
|
|
|
|
|
|
|
var source = data.source;
|
|
|
|
var disableRange = data.disableRange;
|
|
|
|
if (source.data) {
|
|
|
|
pdfManager = new LocalPdfManager(source.data, source.password);
|
|
|
|
pdfManagerPromise.resolve();
|
|
|
|
return pdfManagerPromise;
|
|
|
|
} else if (source.chunkedViewerLoading) {
|
|
|
|
pdfManager = new NetworkPdfManager(source, handler);
|
|
|
|
pdfManagerPromise.resolve();
|
|
|
|
return pdfManagerPromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
var networkManager = new NetworkManager(source.url, {
|
|
|
|
httpHeaders: source.httpHeaders
|
|
|
|
});
|
|
|
|
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;
|
|
|
|
}
|
2013-01-30 03:13:28 +09:00
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
// 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);
|
|
|
|
|
|
|
|
source.length = length;
|
|
|
|
pdfManager = new NetworkPdfManager(source, handler);
|
|
|
|
pdfManagerPromise.resolve(pdfManager);
|
|
|
|
},
|
|
|
|
|
|
|
|
onDone: function onDone(args) {
|
|
|
|
// the data is array, instantiating directly from it
|
|
|
|
pdfManager = new LocalPdfManager(args.chunk, source.password);
|
|
|
|
pdfManagerPromise.resolve();
|
|
|
|
},
|
|
|
|
|
|
|
|
onError: function onError(status) {
|
|
|
|
if (status == 404) {
|
|
|
|
var exception = new MissingPDFException( 'Missing PDF "' +
|
|
|
|
source.url + '".');
|
|
|
|
handler.send('MissingPDF', { exception: exception });
|
|
|
|
} else {
|
|
|
|
handler.send('DocError', 'Unexpected server response (' +
|
|
|
|
status + ') while retrieving PDF "' +
|
|
|
|
source.url + '".');
|
|
|
|
}
|
|
|
|
},
|
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,
|
|
|
|
total: evt.lengthComputable ? evt.total : void(0)
|
|
|
|
});
|
2012-05-15 03:45:07 +09:00
|
|
|
}
|
2013-02-07 08:19:29 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
return pdfManagerPromise;
|
2012-06-24 04:48:33 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
handler.on('test', function wphSetupTest(data) {
|
2012-12-12 02:07:58 +09:00
|
|
|
// check if Uint8Array can be sent to worker
|
|
|
|
if (!(data instanceof Uint8Array)) {
|
|
|
|
handler.send('test', false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// check if the response property is supported by xhr
|
|
|
|
var xhr = new XMLHttpRequest();
|
2013-02-17 14:30:08 +09:00
|
|
|
var responseExists = 'response' in xhr;
|
|
|
|
// check if the property is actually implemented
|
|
|
|
try {
|
|
|
|
var dummy = xhr.responseType;
|
|
|
|
} catch (e) {
|
|
|
|
responseExists = false;
|
|
|
|
}
|
|
|
|
if (!responseExists) {
|
2012-12-12 02:07:58 +09:00
|
|
|
handler.send('test', false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
handler.send('test', true);
|
2012-06-24 04:48:33 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
handler.on('GetDocRequest', function wphSetupDoc(data) {
|
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
var onSuccess = function(doc) {
|
|
|
|
handler.send('GetDoc', { pdfInfo: doc });
|
2013-05-09 03:55:29 +09:00
|
|
|
pdfManager.ensureModel('traversePages', []).then(null, onFailure);
|
2013-02-07 08:19:29 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
var onFailure = function(e) {
|
|
|
|
if (e instanceof PasswordException) {
|
|
|
|
if (e.code === 'needpassword') {
|
|
|
|
handler.send('NeedPassword', {
|
|
|
|
exception: e
|
2012-08-23 00:48:56 +09:00
|
|
|
});
|
2013-02-07 08:19:29 +09:00
|
|
|
} else if (e.code === 'incorrectpassword') {
|
|
|
|
handler.send('IncorrectPassword', {
|
|
|
|
exception: e
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else if (e instanceof InvalidPDFException) {
|
|
|
|
handler.send('InvalidPDF', {
|
|
|
|
exception: e
|
|
|
|
});
|
|
|
|
} else if (e instanceof MissingPDFException) {
|
|
|
|
handler.send('MissingPDF', {
|
|
|
|
exception: e
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
handler.send('UnknownError', {
|
|
|
|
exception: new UnknownErrorException(e.message, e.toString())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
getPdfManager(data).then(function() {
|
|
|
|
loadDocument(false).then(onSuccess, function(ex) {
|
|
|
|
// Try again with recoveryMode == true
|
|
|
|
if (!(ex instanceof XRefParseException)) {
|
|
|
|
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() {
|
|
|
|
loadDocument(true).then(onSuccess, onFailure);
|
|
|
|
});
|
2012-06-24 04:48:33 +09:00
|
|
|
});
|
2013-02-07 08:19:29 +09:00
|
|
|
});
|
2012-04-12 07:52:15 +09:00
|
|
|
});
|
|
|
|
|
2012-04-15 05:54:31 +09:00
|
|
|
handler.on('GetPageRequest', function wphSetupGetPage(data) {
|
2013-02-07 08:19:29 +09:00
|
|
|
var pageIndex = data.pageIndex;
|
|
|
|
pdfManager.getPage(pageIndex).then(function(page) {
|
|
|
|
var rotatePromise = pdfManager.ensure(page, 'rotate');
|
|
|
|
var refPromise = pdfManager.ensure(page, 'ref');
|
|
|
|
var viewPromise = pdfManager.ensure(page, 'view');
|
|
|
|
|
2013-04-19 02:41:33 +09:00
|
|
|
Promise.all([rotatePromise, refPromise, viewPromise]).then(
|
2013-02-07 08:19:29 +09:00
|
|
|
function(results) {
|
|
|
|
var page = {
|
|
|
|
pageIndex: data.pageIndex,
|
|
|
|
rotate: results[0],
|
|
|
|
ref: results[1],
|
|
|
|
view: results[2]
|
|
|
|
};
|
|
|
|
|
|
|
|
handler.send('GetPage', { pageInfo: page });
|
|
|
|
});
|
|
|
|
});
|
2011-09-08 02:16:02 +09:00
|
|
|
});
|
2011-10-09 17:37:53 +09:00
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
handler.on('GetDestinations',
|
|
|
|
function wphSetupGetDestinations(data, promise) {
|
|
|
|
pdfManager.ensureCatalog('destinations').then(function(destinations) {
|
|
|
|
promise.resolve(destinations);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2012-06-02 06:17:09 +09:00
|
|
|
handler.on('GetData', function wphSetupGetData(data, promise) {
|
2013-02-07 08:19:29 +09:00
|
|
|
pdfManager.requestLoadedStream();
|
|
|
|
pdfManager.onLoadedStream().then(function(stream) {
|
|
|
|
promise.resolve(stream.bytes);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
handler.on('DataLoaded', function wphSetupDataLoaded(data, promise) {
|
|
|
|
pdfManager.onLoadedStream().then(function(stream) {
|
|
|
|
promise.resolve({ length: stream.bytes.byteLength });
|
|
|
|
});
|
2012-06-02 06:17:09 +09:00
|
|
|
});
|
|
|
|
|
2012-04-15 05:54:31 +09:00
|
|
|
handler.on('GetAnnotationsRequest', function wphSetupGetAnnotations(data) {
|
2013-02-07 08:19:29 +09:00
|
|
|
pdfManager.getPage(data.pageIndex).then(function(page) {
|
2013-03-21 17:04:44 +09:00
|
|
|
pdfManager.ensure(page, 'getAnnotationsData', []).then(
|
|
|
|
function(annotationsData) {
|
2013-04-18 16:12:40 +09:00
|
|
|
handler.send('GetAnnotations', {
|
|
|
|
pageIndex: data.pageIndex,
|
2013-03-21 17:04:44 +09:00
|
|
|
annotations: annotationsData
|
2013-04-18 16:12:40 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
2012-04-15 05:54:31 +09:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
handler.on('RenderPageRequest', function wphSetupRenderPage(data) {
|
2013-02-07 08:19:29 +09:00
|
|
|
pdfManager.getPage(data.pageIndex).then(function(page) {
|
2011-09-08 02:16:02 +09:00
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
var pageNum = data.pageIndex + 1;
|
|
|
|
var start = Date.now();
|
2011-11-29 09:55:09 +09:00
|
|
|
// Pre compile the pdf page and fetch the fonts/images.
|
2013-04-09 07:14:56 +09:00
|
|
|
page.getOperatorList(handler).then(function(opListData) {
|
|
|
|
|
|
|
|
var operatorList = opListData.queue;
|
|
|
|
var dependency = Object.keys(opListData.dependencies);
|
2012-01-20 04:19:19 +09:00
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
// The following code does quite the same as
|
|
|
|
// Page.prototype.startRendering, but stops at one point and sends the
|
|
|
|
// result back to the main thread.
|
2013-02-01 08:33:59 +09:00
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
log('page=%d - getOperatorList: time=%dms, len=%d', pageNum,
|
|
|
|
Date.now() - start, operatorList.fnArray.length);
|
2012-01-20 04:19:19 +09:00
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
// Filter the dependecies for fonts.
|
|
|
|
var fonts = {};
|
|
|
|
for (var i = 0, ii = dependency.length; i < ii; i++) {
|
|
|
|
var dep = dependency[i];
|
|
|
|
if (dep.indexOf('g_font_') === 0) {
|
|
|
|
fonts[dep] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
handler.send('RenderPage', {
|
|
|
|
pageIndex: data.pageIndex,
|
|
|
|
operatorList: operatorList,
|
|
|
|
depFonts: Object.keys(fonts)
|
|
|
|
});
|
|
|
|
}, function(e) {
|
2011-09-08 02:16:02 +09:00
|
|
|
|
2013-02-07 08:19:29 +09:00
|
|
|
var minimumStackMessage =
|
|
|
|
'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,
|
|
|
|
error: wrappedException
|
|
|
|
});
|
|
|
|
});
|
2011-09-08 02:16:02 +09:00
|
|
|
});
|
|
|
|
}, this);
|
2011-10-09 17:37:53 +09:00
|
|
|
|
2012-04-21 08:49:08 +09:00
|
|
|
handler.on('GetTextContent', function wphExtractText(data, promise) {
|
2013-02-07 08:19:29 +09:00
|
|
|
pdfManager.getPage(data.pageIndex).then(function(page) {
|
|
|
|
var pageNum = data.pageIndex + 1;
|
|
|
|
var start = Date.now();
|
2013-04-09 07:14:56 +09:00
|
|
|
page.extractTextContent().then(function(textContent) {
|
2013-02-07 08:19:29 +09:00
|
|
|
promise.resolve(textContent);
|
|
|
|
log('text indexing: page=%d - time=%dms', pageNum,
|
|
|
|
Date.now() - start);
|
|
|
|
}, function (e) {
|
|
|
|
// Skip errored pages
|
|
|
|
promise.reject(e);
|
|
|
|
});
|
|
|
|
});
|
2011-12-11 08:24:54 +09:00
|
|
|
});
|
2013-04-13 03:37:49 +09:00
|
|
|
|
|
|
|
handler.on('Terminate', function wphTerminate(data, promise) {
|
|
|
|
pdfManager.streamManager.networkManager.abortAllRequests();
|
|
|
|
promise.resolve();
|
|
|
|
});
|
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({
|
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({
|
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) {
|
2012-01-25 05:04:59 +09:00
|
|
|
error('Unkown 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
|
|
|
|
|
|
|
// Worker thread?
|
|
|
|
if (typeof window === 'undefined') {
|
2011-11-02 03:32:20 +09:00
|
|
|
globalScope.console = workerConsole;
|
2011-10-26 07:43:41 +09:00
|
|
|
|
2012-05-15 09:19:09 +09:00
|
|
|
// Add a logger so we can pass warnings on to the main thread, errors will
|
|
|
|
// throw an exception which will be forwarded on automatically.
|
|
|
|
PDFJS.LogManager.addLogger({
|
|
|
|
warn: function(msg) {
|
2012-08-31 20:40:37 +09:00
|
|
|
globalScope.postMessage({
|
2012-05-15 09:19:09 +09:00
|
|
|
action: '_warn',
|
|
|
|
data: msg
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-10-28 21:32:36 +09:00
|
|
|
var handler = new MessageHandler('worker_processor', this);
|
2011-11-03 23:30:53 +09:00
|
|
|
WorkerMessageHandler.setup(handler);
|
2011-10-26 07:43:41 +09:00
|
|
|
}
|