preferences.js 4.2 KB

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