2
0

password_prompt.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * JavaScript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.PasswordPrompt = void 0;
  27. var _pdf = require("../pdf");
  28. class PasswordPrompt {
  29. #activeCapability = null;
  30. #updateCallback = null;
  31. #reason = null;
  32. constructor(options, overlayManager, l10n, isViewerEmbedded = false) {
  33. this.dialog = options.dialog;
  34. this.label = options.label;
  35. this.input = options.input;
  36. this.submitButton = options.submitButton;
  37. this.cancelButton = options.cancelButton;
  38. this.overlayManager = overlayManager;
  39. this.l10n = l10n;
  40. this._isViewerEmbedded = isViewerEmbedded;
  41. this.submitButton.addEventListener("click", this.#verify.bind(this));
  42. this.cancelButton.addEventListener("click", this.close.bind(this));
  43. this.input.addEventListener("keydown", e => {
  44. if (e.keyCode === 13) {
  45. this.#verify();
  46. }
  47. });
  48. this.overlayManager.register(this.dialog, true);
  49. this.dialog.addEventListener("close", this.#cancel.bind(this));
  50. }
  51. async open() {
  52. if (this.#activeCapability) {
  53. await this.#activeCapability.promise;
  54. }
  55. this.#activeCapability = (0, _pdf.createPromiseCapability)();
  56. try {
  57. await this.overlayManager.open(this.dialog);
  58. } catch (ex) {
  59. this.#activeCapability = null;
  60. throw ex;
  61. }
  62. const passwordIncorrect = this.#reason === _pdf.PasswordResponses.INCORRECT_PASSWORD;
  63. if (!this._isViewerEmbedded || passwordIncorrect) {
  64. this.input.focus();
  65. }
  66. this.label.textContent = await this.l10n.get(`password_${passwordIncorrect ? "invalid" : "label"}`);
  67. }
  68. async close() {
  69. if (this.overlayManager.active === this.dialog) {
  70. this.overlayManager.close(this.dialog);
  71. }
  72. }
  73. #verify() {
  74. const password = this.input.value;
  75. if (password?.length > 0) {
  76. this.#invokeCallback(password);
  77. }
  78. }
  79. #cancel() {
  80. this.#invokeCallback(new Error("PasswordPrompt cancelled."));
  81. this.#activeCapability.resolve();
  82. }
  83. #invokeCallback(password) {
  84. if (!this.#updateCallback) {
  85. return;
  86. }
  87. this.close();
  88. this.input.value = "";
  89. this.#updateCallback(password);
  90. this.#updateCallback = null;
  91. }
  92. async setUpdateCallback(updateCallback, reason) {
  93. if (this.#activeCapability) {
  94. await this.#activeCapability.promise;
  95. }
  96. this.#updateCallback = updateCallback;
  97. this.#reason = reason;
  98. }
  99. }
  100. exports.PasswordPrompt = PasswordPrompt;