mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-20 15:18:08 +02:00
[api-minor] Ensure that the PDFDocumentLoadingTask
-promise is rejected when cancelling the PasswordPrompt (bug 1754421)
This is essentially a *continuation* of PR 7926, where we added support for rejecting the current `PDFDocumentLoadingTask`-promise by throwing inside of the `onPassword`-callback. Hence the naive way to address [bug 1754421](https://bugzilla.mozilla.org/show_bug.cgi?id=1754421) would be to simply throw in the `onPassword`-callback used in the default viewer. However it unfortunately turns out to not work, since the password input/validation is asynchronous, and we thus need another approach. The simplest solution that I can come up with here, is thus to *extend* the `onPassword`-callback to also reject the current `PDFDocumentLoadingTask`-instance if an `Error` is explicitly passed as the input to the callback function. (This doesn't feel great, but I cannot see a better solution that isn't really complicated.)
This commit is contained in:
parent
acc758c40c
commit
1f0fb270b1
3 changed files with 48 additions and 9 deletions
|
@ -239,7 +239,7 @@ describe("api", function () {
|
|||
const passwordNeededCapability = createPromiseCapability();
|
||||
const passwordIncorrectCapability = createPromiseCapability();
|
||||
// Attach the callback that is used to request a password;
|
||||
// similarly to how viewer.js handles passwords.
|
||||
// similarly to how the default viewer handles passwords.
|
||||
loadingTask.onPassword = function (updatePassword, reason) {
|
||||
if (
|
||||
reason === PasswordResponses.NEED_PASSWORD &&
|
||||
|
@ -405,6 +405,38 @@ describe("api", function () {
|
|||
}
|
||||
);
|
||||
|
||||
it(
|
||||
"creates pdf doc from password protected PDF file and passes an Error " +
|
||||
"(asynchronously) to the onPassword callback (bug 1754421)",
|
||||
async function () {
|
||||
const loadingTask = getDocument(
|
||||
buildGetDocumentParams("issue3371.pdf")
|
||||
);
|
||||
expect(loadingTask instanceof PDFDocumentLoadingTask).toEqual(true);
|
||||
|
||||
// Attach the callback that is used to request a password;
|
||||
// similarly to how the default viewer handles passwords.
|
||||
loadingTask.onPassword = function (updatePassword, reason) {
|
||||
waitSome(() => {
|
||||
updatePassword(new Error("Should reject the loadingTask."));
|
||||
});
|
||||
};
|
||||
|
||||
await loadingTask.promise.then(
|
||||
function () {
|
||||
// Shouldn't get here.
|
||||
expect(false).toEqual(true);
|
||||
},
|
||||
function (reason) {
|
||||
expect(reason instanceof PasswordException).toEqual(true);
|
||||
expect(reason.code).toEqual(PasswordResponses.NEED_PASSWORD);
|
||||
}
|
||||
);
|
||||
|
||||
await loadingTask.destroy();
|
||||
}
|
||||
);
|
||||
|
||||
it("creates pdf doc from empty typed array", async function () {
|
||||
const loadingTask = getDocument(new Uint8Array(0));
|
||||
expect(loadingTask instanceof PDFDocumentLoadingTask).toEqual(true);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue