1111
1212namespace PuppeteerSharp
1313{
14- internal class FrameManager : IDisposable , IAsyncDisposable
14+ internal class FrameManager : IDisposable , IAsyncDisposable , IFrameProvider
1515 {
1616 private const int TimeForWaitingForSwap = 200 ;
1717 private const string UtilityWorldName = "__puppeteer_utility_world__" ;
@@ -28,7 +28,7 @@ internal FrameManager(CDPSession client, Page page, bool ignoreHTTPSErrors, Time
2828 Client = client ;
2929 Page = page ;
3030 _logger = Client . Connection . LoggerFactory . CreateLogger < FrameManager > ( ) ;
31- NetworkManager = new NetworkManager ( client , ignoreHTTPSErrors , this ) ;
31+ NetworkManager = new NetworkManager ( ignoreHTTPSErrors , this , client . Connection . LoggerFactory ) ;
3232 TimeoutSettings = timeoutSettings ;
3333
3434 Client . MessageReceived += Client_MessageReceived ;
@@ -59,10 +59,7 @@ internal FrameManager(CDPSession client, Page page, bool ignoreHTTPSErrors, Time
5959
6060 internal Frame MainFrame => FrameTree . MainFrame ;
6161
62- public void Dispose ( )
63- {
64- _eventsQueue ? . Dispose ( ) ;
65- }
62+ public void Dispose ( ) => _eventsQueue ? . Dispose ( ) ;
6663
6764 public async ValueTask DisposeAsync ( )
6865 {
@@ -72,6 +69,8 @@ public async ValueTask DisposeAsync()
7269 }
7370 }
7471
72+ public Task < Frame > GetFrameAsync ( string frameId ) => FrameTree . TryGetFrameAsync ( frameId ) ;
73+
7574 internal ExecutionContext ExecutionContextById ( int contextId , CDPSession session = null )
7675 {
7776 session ??= Client ;
@@ -111,11 +110,11 @@ internal DeviceRequestPromptManager GetDeviceRequestPromptManager(CDPSession cli
111110
112111 internal Frame [ ] GetFrames ( ) => FrameTree . Frames ;
113112
114- internal async Task InitializeAsync ( CDPSession client = null )
113+ internal async Task InitializeAsync ( CDPSession client )
115114 {
116115 try
117116 {
118- client ??= Client ;
117+ var networkInitTask = NetworkManager . AddClientAsync ( client ) ;
119118 var getFrameTreeTask = client . SendAsync < PageGetFrameTreeResponse > ( "Page.getFrameTree" ) ;
120119 var autoAttachTask = client != Client
121120 ? client . SendAsync ( "Target.setAutoAttach" , new TargetSetAutoAttachRequest
@@ -136,7 +135,7 @@ await Task.WhenAll(
136135 await Task . WhenAll (
137136 client . SendAsync ( "Page.setLifecycleEventsEnabled" , new PageSetLifecycleEventsEnabledRequest { Enabled = true } ) ,
138137 client . SendAsync ( "Runtime.enable" ) ,
139- client == Client ? NetworkManager . InitializeAsync ( ) : Task . CompletedTask ) . ConfigureAwait ( false ) ;
138+ networkInitTask ) . ConfigureAwait ( false ) ;
140139
141140 await CreateIsolatedWorldAsync ( client , UtilityWorldName ) . ConfigureAwait ( false ) ;
142141 }
@@ -154,11 +153,12 @@ await Task.WhenAll(
154153 }
155154 }
156155
157- /**
158- * When the main frame is replaced by another main frame,
159- * we maintain the main frame object identity while updating
160- * its frame tree and ID.
161- */
156+ /// <summary>
157+ /// When the main frame is replaced by another main frame
158+ /// we maintain the main frame object identity while updating
159+ /// its frame tree and ID.
160+ /// </summary>
161+ /// <param name="client">New session.</param>
162162 internal async Task SwapFrameTreeAsync ( CDPSession client )
163163 {
164164 OnExecutionContextsCleared ( Client ) ;
@@ -181,11 +181,14 @@ internal async Task SwapFrameTreeAsync(CDPSession client)
181181 Client . Disconnected += ( sender , e ) => _ = OnClientDisconnectAsync ( ) ;
182182
183183 await InitializeAsync ( client ) . ConfigureAwait ( false ) ;
184- await NetworkManager . UpdateClientAsync ( client ) . ConfigureAwait ( false ) ;
184+ await NetworkManager . AddClientAsync ( client ) . ConfigureAwait ( false ) ;
185185
186186 frame ? . OnFrameSwappedByActivation ( ) ;
187187 }
188188
189+ internal Task RegisterSpeculativeSessionAsync ( CDPSession client )
190+ => NetworkManager . AddClientAsync ( client ) ;
191+
189192 private Frame GetFrame ( string frameId ) => FrameTree . GetById ( frameId ) ;
190193
191194 private void Client_MessageReceived ( object sender , MessageEventArgs e )
0 commit comments