@@ -19,8 +19,7 @@ internal class ChromeTargetManager : ITargetManager
1919 private readonly AsyncDictionaryHelper < string , Target > _attachedTargetsByTargetId = new ( "Target {0} not found" ) ;
2020 private readonly ConcurrentDictionary < string , Target > _attachedTargetsBySessionId = new ( ) ;
2121 private readonly ConcurrentDictionary < string , TargetInfo > _discoveredTargetsByTargetId = new ( ) ;
22- private readonly ConcurrentDictionary < ICDPConnection , List < TargetInterceptor > > _targetInterceptors = new ( ) ;
23- private readonly List < string > _targetsIdsForInit = new ( ) ;
22+ private readonly List < string > _targetsIdsForInit = [ ] ;
2423 private readonly TaskCompletionSource < bool > _initializeCompletionSource = new ( ) ;
2524
2625 // Needed for .NET only to prevent race conditions between StoreExistingTargetsForInit and OnAttachedToTarget
@@ -100,18 +99,6 @@ public async Task InitializeAsync()
10099 await _initializeCompletionSource . Task . ConfigureAwait ( false ) ;
101100 }
102101
103- public void AddTargetInterceptor ( CDPSession session , TargetInterceptor interceptor )
104- {
105- var interceptors = _targetInterceptors . GetOrAdd ( session , static _ => new ( ) ) ;
106- interceptors . Add ( interceptor ) ;
107- }
108-
109- public void RemoveTargetInterceptor ( CDPSession session , TargetInterceptor interceptor )
110- {
111- _targetInterceptors . TryGetValue ( session , out var interceptors ) ;
112- interceptors ? . Remove ( interceptor ) ;
113- }
114-
115102 private void StoreExistingTargetsForInit ( )
116103 {
117104 foreach ( var kv in _discoveredTargetsByTargetId )
@@ -179,7 +166,6 @@ private void OnMessageReceived(object sender, MessageEventArgs e)
179166 private void Connection_SessionDetached ( object sender , SessionEventArgs e )
180167 {
181168 e . Session . MessageReceived -= OnMessageReceived ;
182- _targetInterceptors . TryRemove ( e . Session , out var _ ) ;
183169 }
184170
185171 private void OnTargetCreated ( TargetCreatedResponse e )
@@ -247,35 +233,17 @@ private void OnTargetInfoChanged(TargetCreatedResponse e)
247233
248234 private async Task OnAttachedToTargetAsync ( object sender , TargetAttachedToTargetResponse e )
249235 {
250- var parent = sender as ICDPConnection ;
236+ var parentSession = sender as ICDPConnection ;
237+
251238 var targetInfo = e . TargetInfo ;
252239 var session = _connection . GetSession ( e . SessionId ) ?? throw new PuppeteerException ( $ "Session { e . SessionId } was not created.") ;
253240
254- async Task SilentDetach ( )
255- {
256- try
257- {
258- await session . SendAsync ( "Runtime.runIfWaitingForDebugger" ) . ConfigureAwait ( false ) ;
259- await parent . SendAsync (
260- "Target.detachFromTarget" ,
261- new TargetDetachFromTargetRequest
262- {
263- SessionId = session . Id ,
264- } ) . ConfigureAwait ( false ) ;
265- }
266- catch ( Exception ex )
267- {
268- _logger . LogError ( ex , "silentDetach failed." ) ;
269- }
270- }
271-
272241 if ( ! _connection . IsAutoAttached ( targetInfo . TargetId ) )
273242 {
274243 return ;
275244 }
276245
277- if ( targetInfo . Type == TargetType . ServiceWorker &&
278- _connection . IsAutoAttached ( targetInfo . TargetId ) )
246+ if ( targetInfo . Type == TargetType . ServiceWorker )
279247 {
280248 await EnsureTargetsIdsForInitAsync ( ) . ConfigureAwait ( false ) ;
281249 FinishInitializationIfReady ( targetInfo . TargetId ) ;
@@ -292,8 +260,8 @@ await parent.SendAsync(
292260 return ;
293261 }
294262
295- var existingTarget = _attachedTargetsByTargetId . TryGetValue ( targetInfo . TargetId , out var target ) ;
296- if ( ! existingTarget )
263+ var isExistingTarget = _attachedTargetsByTargetId . TryGetValue ( targetInfo . TargetId , out var target ) ;
264+ if ( ! isExistingTarget )
297265 {
298266 target = _targetFactoryFunc ( targetInfo , session ) ;
299267 }
@@ -309,8 +277,9 @@ await parent.SendAsync(
309277
310278 session . MessageReceived += OnMessageReceived ;
311279
312- if ( existingTarget )
280+ if ( isExistingTarget )
313281 {
282+ session . Target = target ;
314283 _attachedTargetsBySessionId . TryAdd ( session . Id , target ) ;
315284 }
316285 else
@@ -321,24 +290,12 @@ await parent.SendAsync(
321290 _attachedTargetsBySessionId . TryAdd ( session . Id , target ) ;
322291 }
323292
324- if ( _targetInterceptors . TryGetValue ( parent , out var interceptors ) )
325- {
326- foreach ( var interceptor in interceptors )
327- {
328- Target parentTarget = null ;
329- if ( parent is CDPSession parentSession && ! _attachedTargetsBySessionId . TryGetValue ( parentSession . Id , out parentTarget ) )
330- {
331- throw new PuppeteerException ( "Parent session not found in attached targets" ) ;
332- }
333-
334- interceptor ( target , parentTarget ) ;
335- }
336- }
293+ ( parentSession as CDPSession ) ? . OnSessionReady ( session ) ;
337294
338295 await EnsureTargetsIdsForInitAsync ( ) . ConfigureAwait ( false ) ;
339296 _targetsIdsForInit . Remove ( target . TargetId ) ;
340297
341- if ( ! existingTarget )
298+ if ( ! isExistingTarget )
342299 {
343300 TargetAvailable ? . Invoke ( this , new TargetChangedArgs { Target = target } ) ;
344301 }
@@ -360,6 +317,26 @@ await Task.WhenAll(
360317 {
361318 _logger . LogError ( ex , "Failed to call setAutoAttach and runIfWaitingForDebugger" ) ;
362319 }
320+
321+ return ;
322+
323+ async Task SilentDetach ( )
324+ {
325+ try
326+ {
327+ await session . SendAsync ( "Runtime.runIfWaitingForDebugger" ) . ConfigureAwait ( false ) ;
328+ await parentSession ! . SendAsync (
329+ "Target.detachFromTarget" ,
330+ new TargetDetachFromTargetRequest
331+ {
332+ SessionId = session . Id ,
333+ } ) . ConfigureAwait ( false ) ;
334+ }
335+ catch ( Exception ex )
336+ {
337+ _logger . LogError ( ex , "silentDetach failed." ) ;
338+ }
339+ }
363340 }
364341
365342 private async Task OnAttachedToTargetHandlingExceptionsAsync ( object sender , string messageId , TargetAttachedToTargetResponse e )
0 commit comments