ght now. const browsingContext = node.ownerGlobal?.browsingContext; // For not yet cached nodes generate a unique id without curly braces. nodeId = lazy.generateUUID(); const details = { browserId: browsingContext?.browserId, browsingContextGroupId: browsingContext?.group.id, browsingContextId: browsingContext?.id, isTopBrowsingContext: browsingContext?.parent === null, nodeWeakRef: Cu.getWeakReference(node), }; this.#nodeIdMap.set(node, nodeId); this.#seenNodesMap.set(nodeId, details); // Also add the information for the node id and its correlated browsing // context to allow the parent process to update the seen nodes. if (!seenNodeIds.has(browsingContext)) { seenNodeIds.set(browsingContext, []); } seenNodeIds.get(browsingContext).push(nodeId); } return nodeId; } /** * Clear known DOM nodes. * * @param {object=} options * @param {boolean=} options.all * Clear all references from any browsing context. Defaults to false. * @param {BrowsingContext=} options.browsingContext * Clear all references living in that browsing context. */ clear(options = {}) { const { all = false, browsingContext } = options; if (all) { this.#nodeIdMap = new WeakMap(); this.#seenNodesMap.clear(); return; } if (browsingContext) { for (const [nodeId, identifier] of this.#seenNodesMap.entries()) { const { browsingContextId, nodeWeakRef } = identifier; const node = nodeWeakRef.get(); if (browsingContextId === browsingContext.id) { this.#nodeIdMap.delete(node); this.#seenNodesMap.delete(nodeId); } } return; } throw new Error(`Requires "browsingContext" or "all" to be set.`); } /** * Get a DOM node by its unique reference. * * @param {BrowsingContext} browsingContext * The browsing context the node should be part of. * @param {string} nodeId * The unique node reference of the DOM node. * * @returns {Node|null} * The DOM node that the unique identifier was generated for or * `null` if the node does not exist anymore. */ getNode(browsingContext, nodeId) { const nodeDetails = this.getReferenceDetails(nodeId); // Check that the node reference is known, and is associated with a // browsing context that shares the same browsing context group. if ( nodeDetails === null || nodeDetails.browsingContextGroupId !== browsingContext.group.id ) { return null; } if (nodeDetails.nodeWeakRef) { return nodeDetails.nodeWeakRef.get(); } return null; } /** * Get detailed information for the node reference. * * @param {string} nodeId * * @returns {NodeReferenceDetails} * Node details like: browsingContextId */ getReferenceDetails(nodeId) { const details = this.#seenNodesMap.get(nodeId); return details !== undefined ? details : null; } } PK