preferences.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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. let defaultPreferences = null;
  29. function getDefaultPreferences() {
  30. if (!defaultPreferences) {
  31. defaultPreferences = Promise.resolve({
  32. "cursorToolOnLoad": 0,
  33. "defaultZoomValue": "",
  34. "disablePageLabels": false,
  35. "enablePermissions": false,
  36. "enablePrintAutoRotate": false,
  37. "enableWebGL": false,
  38. "externalLinkTarget": 0,
  39. "historyUpdateUrl": false,
  40. "ignoreDestinationZoom": false,
  41. "pdfBugEnabled": false,
  42. "renderer": "canvas",
  43. "renderInteractiveForms": true,
  44. "sidebarViewOnLoad": -1,
  45. "scrollModeOnLoad": -1,
  46. "spreadModeOnLoad": -1,
  47. "textLayerMode": 1,
  48. "useOnlyCssZoom": false,
  49. "viewOnLoad": 0,
  50. "disableAutoFetch": false,
  51. "disableFontFace": false,
  52. "disableRange": false,
  53. "disableStream": false
  54. });
  55. }
  56. return defaultPreferences;
  57. }
  58. class BasePreferences {
  59. constructor() {
  60. if (this.constructor === BasePreferences) {
  61. throw new Error("Cannot initialize BasePreferences.");
  62. }
  63. this.prefs = null;
  64. this._initializedPromise = getDefaultPreferences().then(defaults => {
  65. Object.defineProperty(this, "defaults", {
  66. value: Object.freeze(defaults),
  67. writable: false,
  68. enumerable: true,
  69. configurable: false
  70. });
  71. this.prefs = Object.assign(Object.create(null), defaults);
  72. return this._readFromStorage(defaults);
  73. }).then(prefs => {
  74. if (!prefs) {
  75. return;
  76. }
  77. for (const name in prefs) {
  78. const defaultValue = this.defaults[name],
  79. prefValue = prefs[name];
  80. if (defaultValue === undefined || typeof prefValue !== typeof defaultValue) {
  81. continue;
  82. }
  83. this.prefs[name] = prefValue;
  84. }
  85. });
  86. }
  87. async _writeToStorage(prefObj) {
  88. throw new Error("Not implemented: _writeToStorage");
  89. }
  90. async _readFromStorage(prefObj) {
  91. throw new Error("Not implemented: _readFromStorage");
  92. }
  93. async reset() {
  94. await this._initializedPromise;
  95. this.prefs = Object.assign(Object.create(null), this.defaults);
  96. return this._writeToStorage(this.defaults);
  97. }
  98. async set(name, value) {
  99. await this._initializedPromise;
  100. const defaultValue = this.defaults[name];
  101. if (defaultValue === undefined) {
  102. throw new Error(`Set preference: "${name}" is undefined.`);
  103. } else if (value === undefined) {
  104. throw new Error("Set preference: no value is specified.");
  105. }
  106. const valueType = typeof value;
  107. const defaultType = typeof defaultValue;
  108. if (valueType !== defaultType) {
  109. if (valueType === "number" && defaultType === "string") {
  110. value = value.toString();
  111. } else {
  112. throw new Error(`Set preference: "${value}" is a ${valueType}, ` + `expected a ${defaultType}.`);
  113. }
  114. } else {
  115. if (valueType === "number" && !Number.isInteger(value)) {
  116. throw new Error(`Set preference: "${value}" must be an integer.`);
  117. }
  118. }
  119. this.prefs[name] = value;
  120. return this._writeToStorage(this.prefs);
  121. }
  122. async get(name) {
  123. await this._initializedPromise;
  124. const defaultValue = this.defaults[name];
  125. if (defaultValue === undefined) {
  126. throw new Error(`Get preference: "${name}" is undefined.`);
  127. } else {
  128. const prefValue = this.prefs[name];
  129. if (prefValue !== undefined) {
  130. return prefValue;
  131. }
  132. }
  133. return defaultValue;
  134. }
  135. async getAll() {
  136. await this._initializedPromise;
  137. return Object.assign(Object.create(null), this.defaults, this.prefs);
  138. }
  139. }
  140. exports.BasePreferences = BasePreferences;