preferences.js 4.2 KB

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