realmStorage.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.RealmStorage = void 0;
  4. const protocol_js_1 = require("../../../protocol/protocol.js");
  5. /** Container class for browsing realms. */
  6. class RealmStorage {
  7. /** Tracks handles and their realms sent to the client. */
  8. #knownHandlesToRealm = new Map();
  9. /** Map from realm ID to Realm. */
  10. #realmMap = new Map();
  11. get knownHandlesToRealm() {
  12. return this.#knownHandlesToRealm;
  13. }
  14. addRealm(realm) {
  15. this.#realmMap.set(realm.realmId, realm);
  16. }
  17. /** Finds all realms that match the given filter. */
  18. findRealms(filter) {
  19. return Array.from(this.#realmMap.values()).filter((realm) => {
  20. if (filter.realmId !== undefined && filter.realmId !== realm.realmId) {
  21. return false;
  22. }
  23. if (filter.browsingContextId !== undefined &&
  24. filter.browsingContextId !== realm.browsingContextId) {
  25. return false;
  26. }
  27. if (filter.navigableId !== undefined &&
  28. filter.navigableId !== realm.navigableId) {
  29. return false;
  30. }
  31. if (filter.executionContextId !== undefined &&
  32. filter.executionContextId !== realm.executionContextId) {
  33. return false;
  34. }
  35. if (filter.origin !== undefined && filter.origin !== realm.origin) {
  36. return false;
  37. }
  38. if (filter.type !== undefined && filter.type !== realm.type) {
  39. return false;
  40. }
  41. if (filter.sandbox !== undefined && filter.sandbox !== realm.sandbox) {
  42. return false;
  43. }
  44. if (filter.cdpSessionId !== undefined &&
  45. filter.cdpSessionId !== realm.cdpSessionId) {
  46. return false;
  47. }
  48. return true;
  49. });
  50. }
  51. findRealm(filter) {
  52. const maybeRealms = this.findRealms(filter);
  53. if (maybeRealms.length !== 1) {
  54. return undefined;
  55. }
  56. return maybeRealms[0];
  57. }
  58. /** Gets the only realm that matches the given filter, if any, otherwise throws. */
  59. getRealm(filter) {
  60. const maybeRealm = this.findRealm(filter);
  61. if (maybeRealm === undefined) {
  62. throw new protocol_js_1.Message.NoSuchFrameException(`Realm ${JSON.stringify(filter)} not found`);
  63. }
  64. return maybeRealm;
  65. }
  66. /** Deletes all realms that match the given filter. */
  67. deleteRealms(filter) {
  68. this.findRealms(filter).map((realm) => {
  69. realm.delete();
  70. this.#realmMap.delete(realm.realmId);
  71. Array.from(this.knownHandlesToRealm.entries())
  72. .filter(([, r]) => r === realm.realmId)
  73. .map(([handle]) => this.knownHandlesToRealm.delete(handle));
  74. });
  75. }
  76. }
  77. exports.RealmStorage = RealmStorage;
  78. //# sourceMappingURL=realmStorage.js.map