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