preferences.js 4.4 KB

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