target-actor-registry.sys.mjs (3007B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 // Keep track of all WindowGlobal target actors. 6 // This is especially used to track the actors using Message manager connector, 7 // or the ones running in the parent process. 8 // Top level actors, like tab's top level target or parent process target 9 // are still using message manager in order to avoid being destroyed on navigation. 10 // And because of this, these actors aren't using JS Window Actor. 11 const windowGlobalTargetActors = new Set(); 12 13 const xpcShellTargetActors = new Set(); 14 15 export var TargetActorRegistry = { 16 registerTargetActor(targetActor) { 17 windowGlobalTargetActors.add(targetActor); 18 }, 19 20 unregisterTargetActor(targetActor) { 21 windowGlobalTargetActors.delete(targetActor); 22 }, 23 24 registerXpcShellTargetActor(targetActor) { 25 xpcShellTargetActors.add(targetActor); 26 }, 27 28 unregisterXpcShellTargetActor(targetActor) { 29 xpcShellTargetActors.delete(targetActor); 30 }, 31 32 get xpcShellTargetActors() { 33 return xpcShellTargetActors; 34 }, 35 36 /** 37 * Return the target actors matching the passed browser element id. 38 * In some scenarios, the registry can have multiple target actors for a given 39 * browserId (e.g. the regular DevTools content toolbox + DevTools WebExtensions targets). 40 * 41 * @param {object} sessionContext: The Session Context to help know what is debugged. 42 * See devtools/server/actors/watcher/session-context.js 43 * @param {string} connectionPrefix: DevToolsServerConnection's prefix, in order to select only actor 44 * related to the same connection. i.e. the same client. 45 * @returns {Array<TargetActor>} 46 */ 47 getTargetActors(sessionContext, connectionPrefix) { 48 const actors = []; 49 for (const actor of windowGlobalTargetActors) { 50 const isMatchingPrefix = actor.actorID.startsWith(connectionPrefix); 51 const isMatchingContext = 52 sessionContext.type == "all" || 53 (sessionContext.type == "browser-element" && 54 (actor.browserId == sessionContext.browserId || 55 actor.openerBrowserId == sessionContext.browserId)) || 56 (sessionContext.type == "webextension" && 57 actor.addonId == sessionContext.addonId); 58 if (isMatchingPrefix && isMatchingContext) { 59 actors.push(actor); 60 } 61 } 62 return actors; 63 }, 64 65 /** 66 * Helper for tests to help track the number of targets created for a given tab. 67 * (Used by browser_ext_devtools_inspectedWindow.js) 68 * 69 * @param {number} browserId: ID for the tab 70 * 71 * @returns {number} Number of targets for this tab. 72 */ 73 74 getTargetActorsCountForBrowserElement(browserId) { 75 let count = 0; 76 for (const actor of windowGlobalTargetActors) { 77 if (actor.browserId == browserId) { 78 count++; 79 } 80 } 81 return count; 82 }, 83 };