preferences.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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. constructor() {
  30. if (this.constructor === BasePreferences) {
  31. throw new Error("Cannot initialize BasePreferences.");
  32. }
  33. Object.defineProperty(this, "defaults", {
  34. value: Object.freeze({
  35. "cursorToolOnLoad": 0,
  36. "defaultZoomValue": "",
  37. "disablePageLabels": false,
  38. "enablePermissions": false,
  39. "enablePrintAutoRotate": true,
  40. "enableScripting": true,
  41. "externalLinkTarget": 0,
  42. "historyUpdateUrl": false,
  43. "ignoreDestinationZoom": false,
  44. "pdfBugEnabled": false,
  45. "renderer": "canvas",
  46. "renderInteractiveForms": true,
  47. "sidebarViewOnLoad": -1,
  48. "scrollModeOnLoad": -1,
  49. "spreadModeOnLoad": -1,
  50. "textLayerMode": 1,
  51. "useOnlyCssZoom": false,
  52. "viewerCssTheme": 0,
  53. "viewOnLoad": 0,
  54. "disableAutoFetch": false,
  55. "disableFontFace": false,
  56. "disableRange": false,
  57. "disableStream": false,
  58. "enableXfa": false
  59. }),
  60. writable: false,
  61. enumerable: true,
  62. configurable: false
  63. });
  64. this.prefs = Object.create(null);
  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. this.prefs = Object.create(null);
  83. return this._writeToStorage(this.defaults);
  84. }
  85. async set(name, value) {
  86. await this._initializedPromise;
  87. const defaultValue = this.defaults[name];
  88. if (defaultValue === undefined) {
  89. throw new Error(`Set preference: "${name}" is undefined.`);
  90. } else if (value === undefined) {
  91. throw new Error("Set preference: no value is specified.");
  92. }
  93. const valueType = typeof value;
  94. const defaultType = typeof defaultValue;
  95. if (valueType !== defaultType) {
  96. if (valueType === "number" && defaultType === "string") {
  97. value = value.toString();
  98. } else {
  99. throw new Error(`Set preference: "${value}" is a ${valueType}, expected a ${defaultType}.`);
  100. }
  101. } else {
  102. if (valueType === "number" && !Number.isInteger(value)) {
  103. throw new Error(`Set preference: "${value}" must be an integer.`);
  104. }
  105. }
  106. this.prefs[name] = value;
  107. return this._writeToStorage(this.prefs);
  108. }
  109. async get(name) {
  110. await this._initializedPromise;
  111. const defaultValue = this.defaults[name],
  112. prefValue = this.prefs[name];
  113. if (defaultValue === undefined) {
  114. throw new Error(`Get preference: "${name}" is undefined.`);
  115. }
  116. return prefValue !== undefined ? prefValue : defaultValue;
  117. }
  118. async getAll() {
  119. await this._initializedPromise;
  120. const obj = Object.create(null);
  121. for (const name in this.defaults) {
  122. const prefValue = this.prefs[name];
  123. obj[name] = prefValue !== undefined ? prefValue : this.defaults[name];
  124. }
  125. return obj;
  126. }
  127. }
  128. exports.BasePreferences = BasePreferences;