password_prompt.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Copyright 2012 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import { createPromiseCapability, PasswordResponses } from "pdfjs-lib";
  16. /**
  17. * @typedef {Object} PasswordPromptOptions
  18. * @property {HTMLDialogElement} dialog - The overlay's DOM element.
  19. * @property {HTMLParagraphElement} label - Label containing instructions for
  20. * entering the password.
  21. * @property {HTMLInputElement} input - Input field for entering the password.
  22. * @property {HTMLButtonElement} submitButton - Button for submitting the
  23. * password.
  24. * @property {HTMLButtonElement} cancelButton - Button for cancelling password
  25. * entry.
  26. */
  27. class PasswordPrompt {
  28. #activeCapability = null;
  29. #updateCallback = null;
  30. #reason = null;
  31. /**
  32. * @param {PasswordPromptOptions} options
  33. * @param {OverlayManager} overlayManager - Manager for the viewer overlays.
  34. * @param {IL10n} l10n - Localization service.
  35. * @param {boolean} [isViewerEmbedded] - If the viewer is embedded, in e.g.
  36. * an <iframe> or an <object>. The default value is `false`.
  37. */
  38. constructor(options, overlayManager, l10n, isViewerEmbedded = false) {
  39. this.dialog = options.dialog;
  40. this.label = options.label;
  41. this.input = options.input;
  42. this.submitButton = options.submitButton;
  43. this.cancelButton = options.cancelButton;
  44. this.overlayManager = overlayManager;
  45. this.l10n = l10n;
  46. this._isViewerEmbedded = isViewerEmbedded;
  47. // Attach the event listeners.
  48. this.submitButton.addEventListener("click", this.#verify.bind(this));
  49. this.cancelButton.addEventListener("click", this.close.bind(this));
  50. this.input.addEventListener("keydown", e => {
  51. if (e.keyCode === /* Enter = */ 13) {
  52. this.#verify();
  53. }
  54. });
  55. this.overlayManager.register(this.dialog, /* canForceClose = */ true);
  56. this.dialog.addEventListener("close", this.#cancel.bind(this));
  57. }
  58. async open() {
  59. if (this.#activeCapability) {
  60. await this.#activeCapability.promise;
  61. }
  62. this.#activeCapability = createPromiseCapability();
  63. try {
  64. await this.overlayManager.open(this.dialog);
  65. } catch (ex) {
  66. this.#activeCapability = null;
  67. throw ex;
  68. }
  69. const passwordIncorrect =
  70. this.#reason === PasswordResponses.INCORRECT_PASSWORD;
  71. if (!this._isViewerEmbedded || passwordIncorrect) {
  72. this.input.focus();
  73. }
  74. this.label.textContent = await this.l10n.get(
  75. `password_${passwordIncorrect ? "invalid" : "label"}`
  76. );
  77. }
  78. async close() {
  79. if (this.overlayManager.active === this.dialog) {
  80. this.overlayManager.close(this.dialog);
  81. }
  82. }
  83. #verify() {
  84. const password = this.input.value;
  85. if (password?.length > 0) {
  86. this.#invokeCallback(password);
  87. }
  88. }
  89. #cancel() {
  90. this.#invokeCallback(new Error("PasswordPrompt cancelled."));
  91. this.#activeCapability.resolve();
  92. }
  93. #invokeCallback(password) {
  94. if (!this.#updateCallback) {
  95. return; // Ensure that the callback is only invoked once.
  96. }
  97. this.close();
  98. this.input.value = "";
  99. this.#updateCallback(password);
  100. this.#updateCallback = null;
  101. }
  102. async setUpdateCallback(updateCallback, reason) {
  103. if (this.#activeCapability) {
  104. await this.#activeCapability.promise;
  105. }
  106. this.#updateCallback = updateCallback;
  107. this.#reason = reason;
  108. }
  109. }
  110. export { PasswordPrompt };