fix(extension): detach child sessions on parent detach
This commit is contained in:
+23
-32
@@ -566,6 +566,26 @@ function getTabByTargetId(targetId: string): { tabId: number; tab: TabInfo } | u
|
|||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function emitChildDetachesForTab(tabId: number): void {
|
||||||
|
const childEntries = Array.from(childSessions.entries())
|
||||||
|
.filter(([_, parentTab]) => parentTab.tabId === tabId)
|
||||||
|
|
||||||
|
childEntries.forEach(([childSessionId, parentTab]) => {
|
||||||
|
const childDetachParams: Protocol.Target.DetachedFromTargetEvent = parentTab.targetId
|
||||||
|
? { sessionId: childSessionId, targetId: parentTab.targetId }
|
||||||
|
: { sessionId: childSessionId }
|
||||||
|
sendMessage({
|
||||||
|
method: 'forwardCDPEvent',
|
||||||
|
params: {
|
||||||
|
method: 'Target.detachedFromTarget',
|
||||||
|
params: childDetachParams,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
logger.debug('Cleaning up child session:', childSessionId, 'for tab:', tabId)
|
||||||
|
childSessions.delete(childSessionId)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
async function handleCommand(msg: ExtensionCommandMessage): Promise<any> {
|
async function handleCommand(msg: ExtensionCommandMessage): Promise<any> {
|
||||||
if (msg.method !== 'forwardCDPCommand') return
|
if (msg.method !== 'forwardCDPCommand') return
|
||||||
|
|
||||||
@@ -685,6 +705,7 @@ function onDebuggerEvent(source: chrome.debugger.DebuggerSession, method: string
|
|||||||
newTabs.delete(mainTab.tabId)
|
newTabs.delete(mainTab.tabId)
|
||||||
return { tabs: newTabs }
|
return { tabs: newTabs }
|
||||||
})
|
})
|
||||||
|
emitChildDetachesForTab(mainTab.tabId)
|
||||||
} else {
|
} else {
|
||||||
logger.debug('Child target detached:', params.sessionId)
|
logger.debug('Child target detached:', params.sessionId)
|
||||||
childSessions.delete(params.sessionId)
|
childSessions.delete(params.sessionId)
|
||||||
@@ -726,22 +747,7 @@ function onDebuggerDetach(source: chrome.debugger.Debuggee, reason: `${chrome.de
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const [childSessionId, parentTabId] of childSessions.entries()) {
|
emitChildDetachesForTab(tabId)
|
||||||
if (parentTabId.tabId === tabId) {
|
|
||||||
const childDetachParams: Protocol.Target.DetachedFromTargetEvent = parentTabId.targetId
|
|
||||||
? { sessionId: childSessionId, targetId: parentTabId.targetId }
|
|
||||||
: { sessionId: childSessionId }
|
|
||||||
sendMessage({
|
|
||||||
method: 'forwardCDPEvent',
|
|
||||||
params: {
|
|
||||||
method: 'Target.detachedFromTarget',
|
|
||||||
params: childDetachParams,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
logger.debug('Cleaning up child session:', childSessionId, 'for tab:', tabId)
|
|
||||||
childSessions.delete(childSessionId)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
store.setState((state) => {
|
store.setState((state) => {
|
||||||
const newTabs = new Map(state.tabs)
|
const newTabs = new Map(state.tabs)
|
||||||
@@ -859,22 +865,7 @@ function detachTab(tabId: number, shouldDetachDebugger: boolean): void {
|
|||||||
return { tabs: newTabs }
|
return { tabs: newTabs }
|
||||||
})
|
})
|
||||||
|
|
||||||
for (const [childSessionId, parentTabId] of childSessions.entries()) {
|
emitChildDetachesForTab(tabId)
|
||||||
if (parentTabId.tabId === tabId) {
|
|
||||||
const childDetachParams: Protocol.Target.DetachedFromTargetEvent = parentTabId.targetId
|
|
||||||
? { sessionId: childSessionId, targetId: parentTabId.targetId }
|
|
||||||
: { sessionId: childSessionId }
|
|
||||||
sendMessage({
|
|
||||||
method: 'forwardCDPEvent',
|
|
||||||
params: {
|
|
||||||
method: 'Target.detachedFromTarget',
|
|
||||||
params: childDetachParams,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
logger.debug('Cleaning up child session:', childSessionId, 'for tab:', tabId)
|
|
||||||
childSessions.delete(childSessionId)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (shouldDetachDebugger) {
|
if (shouldDetachDebugger) {
|
||||||
chrome.debugger.detach({ tabId }).catch((err) => {
|
chrome.debugger.detach({ tabId }).catch((err) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user