2013-09-24 17:46:54 +02: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.
|
|
|
|
*/
|
|
|
|
|
2022-03-07 17:41:41 +01:00
|
|
|
import { PasswordResponses, PromiseCapability } from "pdfjs-lib";
|
2016-04-08 12:34:27 -05:00
|
|
|
|
2016-04-18 23:59:46 +02:00
|
|
|
/**
|
|
|
|
* @typedef {Object} PasswordPromptOptions
|
2022-03-25 14:10:13 +01:00
|
|
|
* @property {HTMLDialogElement} dialog - The overlay's DOM element.
|
2016-04-18 23:59:46 +02:00
|
|
|
* @property {HTMLParagraphElement} label - Label containing instructions for
|
|
|
|
* entering the password.
|
|
|
|
* @property {HTMLInputElement} input - Input field for entering the password.
|
|
|
|
* @property {HTMLButtonElement} submitButton - Button for submitting the
|
|
|
|
* password.
|
|
|
|
* @property {HTMLButtonElement} cancelButton - Button for cancelling password
|
|
|
|
* entry.
|
|
|
|
*/
|
2013-09-24 17:46:54 +02:00
|
|
|
|
2017-04-16 00:11:44 +02:00
|
|
|
class PasswordPrompt {
|
2022-08-19 17:35:49 +02:00
|
|
|
#activeCapability = null;
|
|
|
|
|
2022-03-25 14:10:13 +01:00
|
|
|
#updateCallback = null;
|
|
|
|
|
|
|
|
#reason = null;
|
|
|
|
|
2016-04-18 23:59:46 +02:00
|
|
|
/**
|
|
|
|
* @param {PasswordPromptOptions} options
|
2017-05-26 14:52:23 +02:00
|
|
|
* @param {OverlayManager} overlayManager - Manager for the viewer overlays.
|
2021-02-03 15:48:40 +01:00
|
|
|
* @param {boolean} [isViewerEmbedded] - If the viewer is embedded, in e.g.
|
|
|
|
* an <iframe> or an <object>. The default value is `false`.
|
2016-04-18 23:59:46 +02:00
|
|
|
*/
|
2023-10-19 16:30:57 +02:00
|
|
|
constructor(options, overlayManager, isViewerEmbedded = false) {
|
2022-03-25 14:10:13 +01:00
|
|
|
this.dialog = options.dialog;
|
2016-04-18 23:59:46 +02:00
|
|
|
this.label = options.label;
|
|
|
|
this.input = options.input;
|
|
|
|
this.submitButton = options.submitButton;
|
|
|
|
this.cancelButton = options.cancelButton;
|
2017-05-26 14:52:23 +02:00
|
|
|
this.overlayManager = overlayManager;
|
2021-02-03 15:48:40 +01:00
|
|
|
this._isViewerEmbedded = isViewerEmbedded;
|
2013-09-24 17:46:54 +02:00
|
|
|
|
2016-04-18 23:59:46 +02:00
|
|
|
// Attach the event listeners.
|
2022-02-09 13:48:29 +01:00
|
|
|
this.submitButton.addEventListener("click", this.#verify.bind(this));
|
2022-08-22 10:36:21 +02:00
|
|
|
this.cancelButton.addEventListener("click", this.close.bind(this));
|
2017-04-16 00:11:44 +02:00
|
|
|
this.input.addEventListener("keydown", e => {
|
2019-12-25 20:03:46 +01:00
|
|
|
if (e.keyCode === /* Enter = */ 13) {
|
2022-02-09 13:48:29 +01:00
|
|
|
this.#verify();
|
2014-05-13 12:08:39 +02:00
|
|
|
}
|
2017-04-16 00:11:44 +02:00
|
|
|
});
|
2013-09-24 17:46:54 +02:00
|
|
|
|
2022-03-25 14:10:22 +01:00
|
|
|
this.overlayManager.register(this.dialog, /* canForceClose = */ true);
|
2022-03-25 14:10:13 +01:00
|
|
|
|
|
|
|
this.dialog.addEventListener("close", this.#cancel.bind(this));
|
2016-04-18 23:59:46 +02:00
|
|
|
}
|
2013-09-24 17:46:54 +02:00
|
|
|
|
2021-02-22 12:43:16 +01:00
|
|
|
async open() {
|
2022-08-19 17:35:49 +02:00
|
|
|
if (this.#activeCapability) {
|
|
|
|
await this.#activeCapability.promise;
|
|
|
|
}
|
2022-03-07 17:41:41 +01:00
|
|
|
this.#activeCapability = new PromiseCapability();
|
2022-08-19 17:35:49 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
await this.overlayManager.open(this.dialog);
|
|
|
|
} catch (ex) {
|
2023-07-30 08:34:41 +02:00
|
|
|
this.#activeCapability.resolve();
|
2022-08-19 17:35:49 +02:00
|
|
|
throw ex;
|
|
|
|
}
|
2016-04-18 23:59:46 +02:00
|
|
|
|
2021-02-22 12:43:16 +01:00
|
|
|
const passwordIncorrect =
|
2022-03-25 14:10:13 +01:00
|
|
|
this.#reason === PasswordResponses.INCORRECT_PASSWORD;
|
2016-04-18 23:59:46 +02:00
|
|
|
|
2021-02-22 12:43:16 +01:00
|
|
|
if (!this._isViewerEmbedded || passwordIncorrect) {
|
|
|
|
this.input.focus();
|
|
|
|
}
|
2023-10-19 16:30:57 +02:00
|
|
|
this.label.setAttribute(
|
|
|
|
"data-l10n-id",
|
2023-10-13 16:23:17 +02:00
|
|
|
`pdfjs-password-${passwordIncorrect ? "invalid" : "label"}`
|
2021-02-22 12:43:16 +01:00
|
|
|
);
|
2017-04-16 00:11:44 +02:00
|
|
|
}
|
2016-04-18 23:59:46 +02:00
|
|
|
|
2022-02-14 12:13:30 +01:00
|
|
|
async close() {
|
2022-03-25 14:10:22 +01:00
|
|
|
if (this.overlayManager.active === this.dialog) {
|
|
|
|
this.overlayManager.close(this.dialog);
|
2022-03-25 14:10:13 +01:00
|
|
|
}
|
2017-04-16 00:11:44 +02:00
|
|
|
}
|
2013-09-24 17:46:54 +02:00
|
|
|
|
2022-02-09 13:48:29 +01:00
|
|
|
#verify() {
|
2019-12-27 00:22:32 +01:00
|
|
|
const password = this.input.value;
|
2021-02-05 17:36:28 +01:00
|
|
|
if (password?.length > 0) {
|
2022-03-25 14:10:13 +01:00
|
|
|
this.#invokeCallback(password);
|
2013-09-24 17:46:54 +02:00
|
|
|
}
|
2017-04-16 00:11:44 +02:00
|
|
|
}
|
2016-04-18 23:59:46 +02:00
|
|
|
|
2022-02-09 13:48:29 +01:00
|
|
|
#cancel() {
|
2022-03-25 14:10:13 +01:00
|
|
|
this.#invokeCallback(new Error("PasswordPrompt cancelled."));
|
2022-08-19 17:35:49 +02:00
|
|
|
this.#activeCapability.resolve();
|
2022-03-25 14:10:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#invokeCallback(password) {
|
|
|
|
if (!this.#updateCallback) {
|
|
|
|
return; // Ensure that the callback is only invoked once.
|
|
|
|
}
|
2022-02-09 13:48:29 +01:00
|
|
|
this.close();
|
2022-03-25 14:10:13 +01:00
|
|
|
this.input.value = "";
|
|
|
|
|
|
|
|
this.#updateCallback(password);
|
|
|
|
this.#updateCallback = null;
|
2022-02-09 13:48:29 +01:00
|
|
|
}
|
|
|
|
|
2022-08-19 17:35:49 +02:00
|
|
|
async setUpdateCallback(updateCallback, reason) {
|
|
|
|
if (this.#activeCapability) {
|
|
|
|
await this.#activeCapability.promise;
|
|
|
|
}
|
2022-03-25 14:10:13 +01:00
|
|
|
this.#updateCallback = updateCallback;
|
|
|
|
this.#reason = reason;
|
2017-04-16 00:11:44 +02:00
|
|
|
}
|
|
|
|
}
|
2016-04-08 12:34:27 -05:00
|
|
|
|
2017-03-28 01:07:27 +02:00
|
|
|
export { PasswordPrompt };
|