@@ -70,6 +70,7 @@ internal Broker(BiDi bidi, ITransport transport)
7070 {
7171 new BrowsingContextConverter ( _bidi ) ,
7272 new BrowserUserContextConverter ( bidi ) ,
73+ new BrowserClientWindowConverter ( ) ,
7374 new NavigationConverter ( ) ,
7475 new InterceptConverter ( _bidi ) ,
7576 new RequestConverter ( _bidi ) ,
@@ -82,6 +83,7 @@ internal Broker(BiDi bidi, ITransport transport)
8283 new DateTimeOffsetConverter ( ) ,
8384 new PrintPageRangeConverter ( ) ,
8485 new InputOriginConverter ( ) ,
86+ new SubscriptionConverter ( ) ,
8587 new JsonStringEnumConverter ( JsonNamingPolicy . CamelCase ) ,
8688
8789 // https://github.com/dotnet/runtime/issues/72604
@@ -97,6 +99,7 @@ internal Broker(BiDi bidi, ITransport transport)
9799 new Json . Converters . Enumerable . LocateNodesResultConverter ( ) ,
98100 new Json . Converters . Enumerable . InputSourceActionsConverter ( ) ,
99101 new Json . Converters . Enumerable . GetUserContextsResultConverter ( ) ,
102+ new Json . Converters . Enumerable . GetClientWindowsResultConverter ( ) ,
100103 new Json . Converters . Enumerable . GetRealmsResultConverter ( ) ,
101104 }
102105 } ;
@@ -109,15 +112,17 @@ public async Task ConnectAsync(CancellationToken cancellationToken)
109112 await _transport . ConnectAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
110113
111114 _receiveMessagesCancellationTokenSource = new CancellationTokenSource ( ) ;
112- _receivingMessageTask = _myTaskFactory . StartNew ( async ( ) => await ReceiveMessagesAsync ( _receiveMessagesCancellationTokenSource . Token ) , TaskCreationOptions . LongRunning ) . Unwrap ( ) ;
113- _eventEmitterTask = _myTaskFactory . StartNew ( async ( ) => await ProcessEventsAwaiterAsync ( ) , TaskCreationOptions . LongRunning ) . Unwrap ( ) ;
115+ _receivingMessageTask = _myTaskFactory . StartNew ( async ( ) => await ReceiveMessagesAsync ( _receiveMessagesCancellationTokenSource . Token ) ) . Unwrap ( ) ;
116+ _eventEmitterTask = _myTaskFactory . StartNew ( ProcessEventsAwaiterAsync ) . Unwrap ( ) ;
114117 }
115118
116119 private async Task ReceiveMessagesAsync ( CancellationToken cancellationToken )
117120 {
118121 while ( ! cancellationToken . IsCancellationRequested )
119122 {
120- var message = await _transport . ReceiveAsJsonAsync < Message > ( _jsonSerializerContext , cancellationToken ) ;
123+ var data = await _transport . ReceiveAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
124+
125+ var message = JsonSerializer . Deserialize ( new ReadOnlySpan < byte > ( data ) , _jsonSerializerContext . Message ) ;
121126
122127 switch ( message )
123128 {
@@ -177,21 +182,21 @@ private async Task ProcessEventsAwaiterAsync()
177182 }
178183
179184 public async Task < TResult > ExecuteCommandAsync < TCommand , TResult > ( TCommand command , CommandOptions ? options )
180- where TCommand : Command
185+ where TCommand : Command
181186 {
182187 var jsonElement = await ExecuteCommandCoreAsync ( command , options ) . ConfigureAwait ( false ) ;
183188
184189 return ( TResult ) jsonElement . Deserialize ( typeof ( TResult ) , _jsonSerializerContext ) ! ;
185190 }
186191
187192 public async Task ExecuteCommandAsync < TCommand > ( TCommand command , CommandOptions ? options )
188- where TCommand : Command
193+ where TCommand : Command
189194 {
190195 await ExecuteCommandCoreAsync ( command , options ) . ConfigureAwait ( false ) ;
191196 }
192197
193198 private async Task < JsonElement > ExecuteCommandCoreAsync < TCommand > ( TCommand command , CommandOptions ? options )
194- where TCommand : Command
199+ where TCommand : Command
195200 {
196201 command . Id = Interlocked . Increment ( ref _currentCommandId ) ;
197202
@@ -205,7 +210,9 @@ private async Task<JsonElement> ExecuteCommandCoreAsync<TCommand>(TCommand comma
205210
206211 _pendingCommands [ command . Id ] = tcs ;
207212
208- await _transport . SendAsJsonAsync ( command , _jsonSerializerContext , cts . Token ) . ConfigureAwait ( false ) ;
213+ var data = JsonSerializer . SerializeToUtf8Bytes ( command , typeof ( TCommand ) , _jsonSerializerContext ) ;
214+
215+ await _transport . SendAsync ( data , cts . Token ) . ConfigureAwait ( false ) ;
209216
210217 return await tcs . Task . ConfigureAwait ( false ) ;
211218 }
@@ -217,23 +224,23 @@ public async Task<Subscription> SubscribeAsync<TEventArgs>(string eventName, Act
217224
218225 if ( options is BrowsingContextsSubscriptionOptions browsingContextsOptions )
219226 {
220- await _bidi . SessionModule . SubscribeAsync ( [ eventName ] , new ( ) { Contexts = browsingContextsOptions . Contexts } ) . ConfigureAwait ( false ) ;
227+ var subscribeResult = await _bidi . SessionModule . SubscribeAsync ( [ eventName ] , new ( ) { Contexts = browsingContextsOptions . Contexts } ) . ConfigureAwait ( false ) ;
221228
222229 var eventHandler = new SyncEventHandler < TEventArgs > ( eventName , action , browsingContextsOptions ? . Contexts ) ;
223230
224231 handlers . Add ( eventHandler ) ;
225232
226- return new Subscription ( this , eventHandler ) ;
233+ return new Subscription ( subscribeResult . Subscription , this , eventHandler ) ;
227234 }
228235 else
229236 {
230- await _bidi . SessionModule . SubscribeAsync ( [ eventName ] ) . ConfigureAwait ( false ) ;
237+ var subscribeResult = await _bidi . SessionModule . SubscribeAsync ( [ eventName ] ) . ConfigureAwait ( false ) ;
231238
232239 var eventHandler = new SyncEventHandler < TEventArgs > ( eventName , action ) ;
233240
234241 handlers . Add ( eventHandler ) ;
235242
236- return new Subscription ( this , eventHandler ) ;
243+ return new Subscription ( subscribeResult . Subscription , this , eventHandler ) ;
237244 }
238245 }
239246
@@ -244,44 +251,51 @@ public async Task<Subscription> SubscribeAsync<TEventArgs>(string eventName, Fun
244251
245252 if ( options is BrowsingContextsSubscriptionOptions browsingContextsOptions )
246253 {
247- await _bidi . SessionModule . SubscribeAsync ( [ eventName ] , new ( ) { Contexts = browsingContextsOptions . Contexts } ) . ConfigureAwait ( false ) ;
254+ var subscribeResult = await _bidi . SessionModule . SubscribeAsync ( [ eventName ] , new ( ) { Contexts = browsingContextsOptions . Contexts } ) . ConfigureAwait ( false ) ;
248255
249256 var eventHandler = new AsyncEventHandler < TEventArgs > ( eventName , func , browsingContextsOptions . Contexts ) ;
250257
251258 handlers . Add ( eventHandler ) ;
252259
253- return new Subscription ( this , eventHandler ) ;
260+ return new Subscription ( subscribeResult . Subscription , this , eventHandler ) ;
254261 }
255262 else
256263 {
257- await _bidi . SessionModule . SubscribeAsync ( [ eventName ] ) . ConfigureAwait ( false ) ;
264+ var subscribeResult = await _bidi . SessionModule . SubscribeAsync ( [ eventName ] ) . ConfigureAwait ( false ) ;
258265
259266 var eventHandler = new AsyncEventHandler < TEventArgs > ( eventName , func ) ;
260267
261268 handlers . Add ( eventHandler ) ;
262269
263- return new Subscription ( this , eventHandler ) ;
270+ return new Subscription ( subscribeResult . Subscription , this , eventHandler ) ;
264271 }
265272 }
266273
267- public async Task UnsubscribeAsync ( EventHandler eventHandler )
274+ public async Task UnsubscribeAsync ( Modules . Session . Subscription subscription , EventHandler eventHandler )
268275 {
269276 var eventHandlers = _eventHandlers [ eventHandler . EventName ] ;
270277
271278 eventHandlers . Remove ( eventHandler ) ;
272279
273- if ( eventHandler . Contexts is not null )
280+ if ( subscription is not null )
274281 {
275- if ( ! eventHandlers . Any ( h => eventHandler . Contexts . Equals ( h . Contexts ) ) && ! eventHandlers . Any ( h => h . Contexts is null ) )
276- {
277- await _bidi . SessionModule . UnsubscribeAsync ( [ eventHandler . EventName ] , new ( ) { Contexts = eventHandler . Contexts } ) . ConfigureAwait ( false ) ;
278- }
282+ await _bidi . SessionModule . UnsubscribeAsync ( [ subscription ] ) . ConfigureAwait ( false ) ;
279283 }
280284 else
281285 {
282- if ( ! eventHandlers . Any ( h => h . Contexts is not null ) && ! eventHandlers . Any ( h => h . Contexts is null ) )
286+ if ( eventHandler . Contexts is not null )
287+ {
288+ if ( ! eventHandlers . Any ( h => eventHandler . Contexts . Equals ( h . Contexts ) ) && ! eventHandlers . Any ( h => h . Contexts is null ) )
289+ {
290+ await _bidi . SessionModule . UnsubscribeAsync ( [ eventHandler . EventName ] , new ( ) { Contexts = eventHandler . Contexts } ) . ConfigureAwait ( false ) ;
291+ }
292+ }
293+ else
283294 {
284- await _bidi . SessionModule . UnsubscribeAsync ( [ eventHandler . EventName ] ) . ConfigureAwait ( false ) ;
295+ if ( ! eventHandlers . Any ( h => h . Contexts is not null ) && ! eventHandlers . Any ( h => h . Contexts is null ) )
296+ {
297+ await _bidi . SessionModule . UnsubscribeAsync ( [ eventHandler . EventName ] ) . ConfigureAwait ( false ) ;
298+ }
285299 }
286300 }
287301 }
0 commit comments