browsingContextStorage.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. "use strict";
  2. /**
  3. * Copyright 2022 Google LLC.
  4. * Copyright (c) Microsoft Corporation.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. exports.BrowsingContextStorage = void 0;
  20. const protocol_js_1 = require("../../../protocol/protocol.js");
  21. /** Container class for browsing contexts. */
  22. class BrowsingContextStorage {
  23. /** Map from context ID to context implementation. */
  24. #contexts = new Map();
  25. /** Gets all top-level contexts, i.e. those with no parent. */
  26. getTopLevelContexts() {
  27. return this.getAllContexts().filter((context) => context.isTopLevelContext());
  28. }
  29. /** Gets all contexts. */
  30. getAllContexts() {
  31. return Array.from(this.#contexts.values());
  32. }
  33. /** Deletes the context with the given ID. */
  34. deleteContextById(id) {
  35. this.#contexts.delete(id);
  36. }
  37. /** Deletes the given context. */
  38. deleteContext(context) {
  39. this.#contexts.delete(context.id);
  40. }
  41. /** Tracks the given context. */
  42. addContext(context) {
  43. this.#contexts.set(context.id, context);
  44. }
  45. /** Returns true whether there is an existing context with the given ID. */
  46. hasContext(id) {
  47. return this.#contexts.has(id);
  48. }
  49. /** Gets the context with the given ID, if any. */
  50. findContext(id) {
  51. return this.#contexts.get(id);
  52. }
  53. /** Returns the top-level context ID of the given context, if any. */
  54. findTopLevelContextId(id) {
  55. if (id === null) {
  56. return null;
  57. }
  58. const maybeContext = this.findContext(id);
  59. const parentId = maybeContext?.parentId ?? null;
  60. if (parentId === null) {
  61. return id;
  62. }
  63. return this.findTopLevelContextId(parentId);
  64. }
  65. /** Gets the context with the given ID, if any, otherwise throws. */
  66. getContext(id) {
  67. const result = this.findContext(id);
  68. if (result === undefined) {
  69. throw new protocol_js_1.Message.NoSuchFrameException(`Context ${id} not found`);
  70. }
  71. return result;
  72. }
  73. }
  74. exports.BrowsingContextStorage = BrowsingContextStorage;
  75. //# sourceMappingURL=browsingContextStorage.js.map