preferences.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.BasePreferences = void 0;
  27. var _app_options = require("./app_options.js");
  28. class BasePreferences {
  29. #defaults = Object.freeze({
  30. "annotationMode": 2,
  31. "cursorToolOnLoad": 0,
  32. "defaultZoomValue": "",
  33. "disablePageLabels": false,
  34. "enablePermissions": false,
  35. "enablePrintAutoRotate": true,
  36. "enableScripting": true,
  37. "externalLinkTarget": 0,
  38. "historyUpdateUrl": false,
  39. "ignoreDestinationZoom": false,
  40. "pageColorsBackground": "Canvas",
  41. "pageColorsForeground": "CanvasText",
  42. "pdfBugEnabled": false,
  43. "renderer": "canvas",
  44. "sidebarViewOnLoad": -1,
  45. "scrollModeOnLoad": -1,
  46. "spreadModeOnLoad": -1,
  47. "textLayerMode": 1,
  48. "useOnlyCssZoom": false,
  49. "viewerCssTheme": 0,
  50. "viewOnLoad": 0,
  51. "disableAutoFetch": false,
  52. "disableFontFace": false,
  53. "disableRange": false,
  54. "disableStream": false,
  55. "enableXfa": true
  56. });
  57. #prefs = Object.create(null);
  58. #initializedPromise = null;
  59. constructor() {
  60. if (this.constructor === BasePreferences) {
  61. throw new Error("Cannot initialize BasePreferences.");
  62. }
  63. this.#initializedPromise = this._readFromStorage(this.#defaults).then(prefs => {
  64. for (const name in this.#defaults) {
  65. const prefValue = prefs?.[name];
  66. if (typeof prefValue === typeof this.#defaults[name]) {
  67. this.#prefs[name] = prefValue;
  68. }
  69. }
  70. });
  71. }
  72. async _writeToStorage(prefObj) {
  73. throw new Error("Not implemented: _writeToStorage");
  74. }
  75. async _readFromStorage(prefObj) {
  76. throw new Error("Not implemented: _readFromStorage");
  77. }
  78. async reset() {
  79. await this.#initializedPromise;
  80. const prefs = this.#prefs;
  81. this.#prefs = Object.create(null);
  82. return this._writeToStorage(this.#defaults).catch(reason => {
  83. this.#prefs = prefs;
  84. throw reason;
  85. });
  86. }
  87. async set(name, value) {
  88. await this.#initializedPromise;
  89. const defaultValue = this.#defaults[name],
  90. prefs = this.#prefs;
  91. if (defaultValue === undefined) {
  92. throw new Error(`Set preference: "${name}" is undefined.`);
  93. } else if (value === undefined) {
  94. throw new Error("Set preference: no value is specified.");
  95. }
  96. const valueType = typeof value,
  97. defaultType = typeof defaultValue;
  98. if (valueType !== defaultType) {
  99. if (valueType === "number" && defaultType === "string") {
  100. value = value.toString();
  101. } else {
  102. throw new Error(`Set preference: "${value}" is a ${valueType}, expected a ${defaultType}.`);
  103. }
  104. } else {
  105. if (valueType === "number" && !Number.isInteger(value)) {
  106. throw new Error(`Set preference: "${value}" must be an integer.`);
  107. }
  108. }
  109. this.#prefs[name] = value;
  110. return this._writeToStorage(this.#prefs).catch(reason => {
  111. this.#prefs = prefs;
  112. throw reason;
  113. });
  114. }
  115. async get(name) {
  116. await this.#initializedPromise;
  117. const defaultValue = this.#defaults[name];
  118. if (defaultValue === undefined) {
  119. throw new Error(`Get preference: "${name}" is undefined.`);
  120. }
  121. return this.#prefs[name] ?? defaultValue;
  122. }
  123. async getAll() {
  124. await this.#initializedPromise;
  125. const obj = Object.create(null);
  126. for (const name in this.#defaults) {
  127. obj[name] = this.#prefs[name] ?? this.#defaults[name];
  128. }
  129. return obj;
  130. }
  131. }
  132. exports.BasePreferences = BasePreferences;