mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-26 01:58:06 +02:00
[api-minor] Ensure that the getDocument
Promise is rejected if the loadingTask
is destroyed, or an Error
is thrown, inside of the onPassword
callback (issue 7806)
This patch also removes the `UpdatePassword` message, in favour of using the `sendWithPromise` method of `MessageHandler`. Furthermore, the patch also refactors the `BasePdfManager_updatePassword`/`BasePdfManager_passwordChanged` methods (in pdf_manager.js), and the `pdfManagerReady` function (in worker.js).
This commit is contained in:
parent
59afb4b9f0
commit
27513cd23b
4 changed files with 112 additions and 71 deletions
|
@ -663,19 +663,25 @@ var WorkerMessageHandler = {
|
|||
return pdfManagerCapability.promise;
|
||||
}
|
||||
|
||||
var setupDoc = function(data) {
|
||||
var onSuccess = function(doc) {
|
||||
function setupDoc(data) {
|
||||
function onSuccess(doc) {
|
||||
ensureNotTerminated();
|
||||
handler.send('GetDoc', { pdfInfo: doc });
|
||||
};
|
||||
}
|
||||
|
||||
var onFailure = function(e) {
|
||||
function onFailure(e) {
|
||||
if (e instanceof PasswordException) {
|
||||
if (e.code === PasswordResponses.NEED_PASSWORD) {
|
||||
handler.send('NeedPassword', e);
|
||||
} else if (e.code === PasswordResponses.INCORRECT_PASSWORD) {
|
||||
handler.send('IncorrectPassword', e);
|
||||
}
|
||||
var task = new WorkerTask('PasswordException: response ' + e.code);
|
||||
startWorkerTask(task);
|
||||
|
||||
handler.sendWithPromise('PasswordRequest', e).then(function (data) {
|
||||
finishWorkerTask(task);
|
||||
pdfManager.updatePassword(data.password);
|
||||
pdfManagerReady();
|
||||
}).catch(function (ex) {
|
||||
finishWorkerTask(task);
|
||||
handler.send('PasswordException', ex);
|
||||
}.bind(null, e));
|
||||
} else if (e instanceof InvalidPDFException) {
|
||||
handler.send('InvalidPDF', e);
|
||||
} else if (e instanceof MissingPDFException) {
|
||||
|
@ -686,7 +692,27 @@ var WorkerMessageHandler = {
|
|||
handler.send('UnknownError',
|
||||
new UnknownErrorException(e.message, e.toString()));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function pdfManagerReady() {
|
||||
ensureNotTerminated();
|
||||
|
||||
loadDocument(false).then(onSuccess, function loadFailure(ex) {
|
||||
ensureNotTerminated();
|
||||
|
||||
// Try again with recoveryMode == true
|
||||
if (!(ex instanceof XRefParseException)) {
|
||||
onFailure(ex);
|
||||
return;
|
||||
}
|
||||
pdfManager.requestLoadedStream();
|
||||
pdfManager.onLoadedStream().then(function() {
|
||||
ensureNotTerminated();
|
||||
|
||||
loadDocument(true).then(onSuccess, onFailure);
|
||||
});
|
||||
}, onFailure);
|
||||
}
|
||||
|
||||
ensureNotTerminated();
|
||||
|
||||
|
@ -714,33 +740,8 @@ var WorkerMessageHandler = {
|
|||
pdfManager.onLoadedStream().then(function(stream) {
|
||||
handler.send('DataLoaded', { length: stream.bytes.byteLength });
|
||||
});
|
||||
}).then(function pdfManagerReady() {
|
||||
ensureNotTerminated();
|
||||
|
||||
loadDocument(false).then(onSuccess, function loadFailure(ex) {
|
||||
ensureNotTerminated();
|
||||
|
||||
// Try again with recoveryMode == true
|
||||
if (!(ex instanceof XRefParseException)) {
|
||||
if (ex instanceof PasswordException) {
|
||||
// after password exception prepare to receive a new password
|
||||
// to repeat loading
|
||||
pdfManager.passwordChanged().then(pdfManagerReady);
|
||||
}
|
||||
|
||||
onFailure(ex);
|
||||
return;
|
||||
}
|
||||
|
||||
pdfManager.requestLoadedStream();
|
||||
pdfManager.onLoadedStream().then(function() {
|
||||
ensureNotTerminated();
|
||||
|
||||
loadDocument(true).then(onSuccess, onFailure);
|
||||
});
|
||||
}, onFailure);
|
||||
}, onFailure);
|
||||
};
|
||||
}).then(pdfManagerReady, onFailure);
|
||||
}
|
||||
|
||||
handler.on('GetPage', function wphSetupGetPage(data) {
|
||||
return pdfManager.getPage(data.pageIndex).then(function(page) {
|
||||
|
@ -824,10 +825,6 @@ var WorkerMessageHandler = {
|
|||
}
|
||||
);
|
||||
|
||||
handler.on('UpdatePassword', function wphSetupUpdatePassword(data) {
|
||||
pdfManager.updatePassword(data);
|
||||
});
|
||||
|
||||
handler.on('GetAnnotations', function wphSetupGetAnnotations(data) {
|
||||
return pdfManager.getPage(data.pageIndex).then(function(page) {
|
||||
return pdfManager.ensure(page, 'getAnnotationsData', [data.intent]);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue