@@ -13,7 +13,7 @@ internal class ChromeTargetManager : ITargetManager
1313 {
1414 private readonly List < string > _ignoredTargets = new ( ) ;
1515 private readonly Connection _connection ;
16- private readonly Func < TargetInfo , CDPSession , Target > _targetFactoryFunc ;
16+ private readonly Func < TargetInfo , CDPSession , CDPSession , Target > _targetFactoryFunc ;
1717 private readonly Func < Target , bool > _targetFilterFunc ;
1818 private readonly ILogger < ChromeTargetManager > _logger ;
1919 private readonly AsyncDictionaryHelper < string , Target > _attachedTargetsByTargetId = new ( "Target {0} not found" ) ;
@@ -28,7 +28,7 @@ internal class ChromeTargetManager : ITargetManager
2828
2929 public ChromeTargetManager (
3030 Connection connection ,
31- Func < TargetInfo , CDPSession , Target > targetFactoryFunc ,
31+ Func < TargetInfo , CDPSession , CDPSession , Target > targetFactoryFunc ,
3232 Func < Target , bool > targetFilterFunc ,
3333 int targetDiscoveryTimeout = 0 )
3434 {
@@ -168,7 +168,7 @@ private void OnTargetCreated(TargetCreatedResponse e)
168168 return ;
169169 }
170170
171- var target = _targetFactoryFunc ( e . TargetInfo , null ) ;
171+ var target = _targetFactoryFunc ( e . TargetInfo , null , null ) ;
172172 target . Initialize ( ) ;
173173 _attachedTargetsByTargetId . AddItem ( e . TargetInfo . TargetId , target ) ;
174174 }
@@ -206,6 +206,13 @@ private void OnTargetInfoChanged(TargetCreatedResponse e)
206206
207207 var previousURL = target . Url ;
208208 var wasInitialized = target . IsInitialized ;
209+
210+ if ( IsPageTargetBecomingPrimary ( target , e . TargetInfo ) )
211+ {
212+ var session = target . Session ;
213+ session . ParentSession ? . OnSwapped ( session ) ;
214+ }
215+
209216 target . TargetInfoChanged ( e . TargetInfo ) ;
210217
211218 if ( wasInitialized && previousURL != target . Url )
@@ -218,9 +225,13 @@ private void OnTargetInfoChanged(TargetCreatedResponse e)
218225 }
219226 }
220227
228+ private bool IsPageTargetBecomingPrimary ( Target target , TargetInfo newTargetInfo )
229+ => ! string . IsNullOrEmpty ( target . TargetInfo . Subtype ) && string . IsNullOrEmpty ( newTargetInfo . Subtype ) ;
230+
221231 private async Task OnAttachedToTargetAsync ( object sender , TargetAttachedToTargetResponse e )
222232 {
223- var parentSession = sender as ICDPConnection ;
233+ var parentConnection = sender as ICDPConnection ;
234+ var parentSession = sender as CDPSession ;
224235
225236 var targetInfo = e . TargetInfo ;
226237 var session = _connection . GetSession ( e . SessionId ) ?? throw new PuppeteerException ( $ "Session { e . SessionId } was not created.") ;
@@ -240,7 +251,7 @@ private async Task OnAttachedToTargetAsync(object sender, TargetAttachedToTarget
240251 return ;
241252 }
242253
243- var workerTarget = _targetFactoryFunc ( targetInfo , null ) ;
254+ var workerTarget = _targetFactoryFunc ( targetInfo , null , null ) ;
244255 workerTarget . Initialize ( ) ;
245256 _attachedTargetsByTargetId . AddItem ( targetInfo . TargetId , workerTarget ) ;
246257 TargetAvailable ? . Invoke ( this , new TargetChangedArgs { Target = workerTarget } ) ;
@@ -250,7 +261,7 @@ private async Task OnAttachedToTargetAsync(object sender, TargetAttachedToTarget
250261 var isExistingTarget = _attachedTargetsByTargetId . TryGetValue ( targetInfo . TargetId , out var target ) ;
251262 if ( ! isExistingTarget )
252263 {
253- target = _targetFactoryFunc ( targetInfo , session ) ;
264+ target = _targetFactoryFunc ( targetInfo , session , parentSession ) ;
254265 }
255266
256267 if ( _targetFilterFunc ? . Invoke ( target ) == false )
@@ -277,7 +288,7 @@ private async Task OnAttachedToTargetAsync(object sender, TargetAttachedToTarget
277288 _attachedTargetsBySessionId . TryAdd ( session . Id , target ) ;
278289 }
279290
280- ( parentSession as CDPSession ) ? . OnSessionReady ( session ) ;
291+ ( parentConnection as CDPSession ) ? . OnSessionReady ( session ) ;
281292
282293 await EnsureTargetsIdsForInitAsync ( ) . ConfigureAwait ( false ) ;
283294 _targetsIdsForInit . Remove ( target . TargetId ) ;
@@ -312,7 +323,7 @@ async Task SilentDetach()
312323 try
313324 {
314325 await session . SendAsync ( "Runtime.runIfWaitingForDebugger" ) . ConfigureAwait ( false ) ;
315- await parentSession ! . SendAsync (
326+ await parentConnection ! . SendAsync (
316327 "Target.detachFromTarget" ,
317328 new TargetDetachFromTargetRequest
318329 {
0 commit comments