optional_content_config.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* Copyright 2020 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import { objectFromMap, unreachable, warn } from "../shared/util.js";
  16. import { MurmurHash3_64 } from "../shared/murmurhash3.js";
  17. const INTERNAL = Symbol("INTERNAL");
  18. class OptionalContentGroup {
  19. #visible = true;
  20. constructor(name, intent) {
  21. this.name = name;
  22. this.intent = intent;
  23. }
  24. /**
  25. * @type {boolean}
  26. */
  27. get visible() {
  28. return this.#visible;
  29. }
  30. /**
  31. * @ignore
  32. */
  33. _setVisible(internal, visible) {
  34. if (internal !== INTERNAL) {
  35. unreachable("Internal method `_setVisible` called.");
  36. }
  37. this.#visible = visible;
  38. }
  39. }
  40. class OptionalContentConfig {
  41. #cachedGetHash = null;
  42. #groups = new Map();
  43. #initialHash = null;
  44. #order = null;
  45. constructor(data) {
  46. this.name = null;
  47. this.creator = null;
  48. if (data === null) {
  49. return;
  50. }
  51. this.name = data.name;
  52. this.creator = data.creator;
  53. this.#order = data.order;
  54. for (const group of data.groups) {
  55. this.#groups.set(
  56. group.id,
  57. new OptionalContentGroup(group.name, group.intent)
  58. );
  59. }
  60. if (data.baseState === "OFF") {
  61. for (const group of this.#groups.values()) {
  62. group._setVisible(INTERNAL, false);
  63. }
  64. }
  65. for (const on of data.on) {
  66. this.#groups.get(on)._setVisible(INTERNAL, true);
  67. }
  68. for (const off of data.off) {
  69. this.#groups.get(off)._setVisible(INTERNAL, false);
  70. }
  71. // The following code must always run *last* in the constructor.
  72. this.#initialHash = this.getHash();
  73. }
  74. #evaluateVisibilityExpression(array) {
  75. const length = array.length;
  76. if (length < 2) {
  77. return true;
  78. }
  79. const operator = array[0];
  80. for (let i = 1; i < length; i++) {
  81. const element = array[i];
  82. let state;
  83. if (Array.isArray(element)) {
  84. state = this.#evaluateVisibilityExpression(element);
  85. } else if (this.#groups.has(element)) {
  86. state = this.#groups.get(element).visible;
  87. } else {
  88. warn(`Optional content group not found: ${element}`);
  89. return true;
  90. }
  91. switch (operator) {
  92. case "And":
  93. if (!state) {
  94. return false;
  95. }
  96. break;
  97. case "Or":
  98. if (state) {
  99. return true;
  100. }
  101. break;
  102. case "Not":
  103. return !state;
  104. default:
  105. return true;
  106. }
  107. }
  108. return operator === "And";
  109. }
  110. isVisible(group) {
  111. if (this.#groups.size === 0) {
  112. return true;
  113. }
  114. if (!group) {
  115. warn("Optional content group not defined.");
  116. return true;
  117. }
  118. if (group.type === "OCG") {
  119. if (!this.#groups.has(group.id)) {
  120. warn(`Optional content group not found: ${group.id}`);
  121. return true;
  122. }
  123. return this.#groups.get(group.id).visible;
  124. } else if (group.type === "OCMD") {
  125. // Per the spec, the expression should be preferred if available.
  126. if (group.expression) {
  127. return this.#evaluateVisibilityExpression(group.expression);
  128. }
  129. if (!group.policy || group.policy === "AnyOn") {
  130. // Default
  131. for (const id of group.ids) {
  132. if (!this.#groups.has(id)) {
  133. 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. 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. 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. 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. warn(`Unknown optional content policy ${group.policy}.`);
  176. return true;
  177. }
  178. warn(`Unknown group type ${group.type}.`);
  179. return true;
  180. }
  181. setVisibility(id, visible = true) {
  182. if (!this.#groups.has(id)) {
  183. 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 ? 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 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. export { OptionalContentConfig };