optional_content_config.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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.OptionalContentConfig = void 0;
  27. var _util = require("../shared/util.js");
  28. var _murmurhash = require("../shared/murmurhash3.js");
  29. const INTERNAL = Symbol("INTERNAL");
  30. class OptionalContentGroup {
  31. #visible = true;
  32. constructor(name, intent) {
  33. this.name = name;
  34. this.intent = intent;
  35. }
  36. get visible() {
  37. return this.#visible;
  38. }
  39. _setVisible(internal, visible) {
  40. if (internal !== INTERNAL) {
  41. (0, _util.unreachable)("Internal method `_setVisible` called.");
  42. }
  43. this.#visible = visible;
  44. }
  45. }
  46. class OptionalContentConfig {
  47. #cachedGetHash = null;
  48. #groups = new Map();
  49. #initialHash = null;
  50. #order = null;
  51. constructor(data) {
  52. this.name = null;
  53. this.creator = null;
  54. if (data === null) {
  55. return;
  56. }
  57. this.name = data.name;
  58. this.creator = data.creator;
  59. this.#order = data.order;
  60. for (const group of data.groups) {
  61. this.#groups.set(group.id, new OptionalContentGroup(group.name, group.intent));
  62. }
  63. if (data.baseState === "OFF") {
  64. for (const group of this.#groups.values()) {
  65. group._setVisible(INTERNAL, false);
  66. }
  67. }
  68. for (const on of data.on) {
  69. this.#groups.get(on)._setVisible(INTERNAL, true);
  70. }
  71. for (const off of data.off) {
  72. this.#groups.get(off)._setVisible(INTERNAL, false);
  73. }
  74. this.#initialHash = this.getHash();
  75. }
  76. #evaluateVisibilityExpression(array) {
  77. const length = array.length;
  78. if (length < 2) {
  79. return true;
  80. }
  81. const operator = array[0];
  82. for (let i = 1; i < length; i++) {
  83. const element = array[i];
  84. let state;
  85. if (Array.isArray(element)) {
  86. state = this.#evaluateVisibilityExpression(element);
  87. } else if (this.#groups.has(element)) {
  88. state = this.#groups.get(element).visible;
  89. } else {
  90. (0, _util.warn)(`Optional content group not found: ${element}`);
  91. return true;
  92. }
  93. switch (operator) {
  94. case "And":
  95. if (!state) {
  96. return false;
  97. }
  98. break;
  99. case "Or":
  100. if (state) {
  101. return true;
  102. }
  103. break;
  104. case "Not":
  105. return !state;
  106. default:
  107. return true;
  108. }
  109. }
  110. return operator === "And";
  111. }
  112. isVisible(group) {
  113. if (this.#groups.size === 0) {
  114. return true;
  115. }
  116. if (!group) {
  117. (0, _util.warn)("Optional content group not defined.");
  118. return true;
  119. }
  120. if (group.type === "OCG") {
  121. if (!this.#groups.has(group.id)) {
  122. (0, _util.warn)(`Optional content group not found: ${group.id}`);
  123. return true;
  124. }
  125. return this.#groups.get(group.id).visible;
  126. } else if (group.type === "OCMD") {
  127. if (group.expression) {
  128. return this.#evaluateVisibilityExpression(group.expression);
  129. }
  130. if (!group.policy || group.policy === "AnyOn") {
  131. for (const id of group.ids) {
  132. if (!this.#groups.has(id)) {
  133. (0, _util.warn)(`Optional content group not found: ${id}`);
  134. return true;
  135. }
  136. if (this.#groups.get(id).visible) {
  137. return true;
  138. }
  139. }
  140. return false;
  141. } else if (group.policy === "AllOn") {
  142. for (const id of group.ids) {
  143. if (!this.#groups.has(id)) {
  144. (0, _util.warn)(`Optional content group not found: ${id}`);
  145. return true;
  146. }
  147. if (!this.#groups.get(id).visible) {
  148. return false;
  149. }
  150. }
  151. return true;
  152. } else if (group.policy === "AnyOff") {
  153. for (const id of group.ids) {
  154. if (!this.#groups.has(id)) {
  155. (0, _util.warn)(`Optional content group not found: ${id}`);
  156. return true;
  157. }
  158. if (!this.#groups.get(id).visible) {
  159. return true;
  160. }
  161. }
  162. return false;
  163. } else if (group.policy === "AllOff") {
  164. for (const id of group.ids) {
  165. if (!this.#groups.has(id)) {
  166. (0, _util.warn)(`Optional content group not found: ${id}`);
  167. return true;
  168. }
  169. if (this.#groups.get(id).visible) {
  170. return false;
  171. }
  172. }
  173. return true;
  174. }
  175. (0, _util.warn)(`Unknown optional content policy ${group.policy}.`);
  176. return true;
  177. }
  178. (0, _util.warn)(`Unknown group type ${group.type}.`);
  179. return true;
  180. }
  181. setVisibility(id, visible = true) {
  182. if (!this.#groups.has(id)) {
  183. (0, _util.warn)(`Optional content group not found: ${id}`);
  184. return;
  185. }
  186. this.#groups.get(id)._setVisible(INTERNAL, !!visible);
  187. this.#cachedGetHash = null;
  188. }
  189. get hasInitialVisibility() {
  190. return this.getHash() === this.#initialHash;
  191. }
  192. getOrder() {
  193. if (!this.#groups.size) {
  194. return null;
  195. }
  196. if (this.#order) {
  197. return this.#order.slice();
  198. }
  199. return [...this.#groups.keys()];
  200. }
  201. getGroups() {
  202. return this.#groups.size > 0 ? (0, _util.objectFromMap)(this.#groups) : null;
  203. }
  204. getGroup(id) {
  205. return this.#groups.get(id) || null;
  206. }
  207. getHash() {
  208. if (this.#cachedGetHash !== null) {
  209. return this.#cachedGetHash;
  210. }
  211. const hash = new _murmurhash.MurmurHash3_64();
  212. for (const [id, group] of this.#groups) {
  213. hash.update(`${id}:${group.visible}`);
  214. }
  215. return this.#cachedGetHash = hash.hexdigest();
  216. }
  217. }
  218. exports.OptionalContentConfig = OptionalContentConfig;