2121// * SOFTWARE.
2222
2323using System ;
24+ using System . Collections . Concurrent ;
2425using System . Threading . Tasks ;
26+ using PuppeteerSharp . Cdp ;
27+ using PuppeteerSharp . States ;
28+ using WebDriverBiDi . Protocol ;
29+ using WebDriverBiDi . Script ;
2530
2631namespace PuppeteerSharp . Bidi . Core ;
2732
2833internal class Browser ( Session session ) : IDisposable
2934{
35+ private bool _disposed ;
36+
37+ private readonly ConcurrentDictionary < string , CdpWebWorker > _workers = new ( ) ;
38+
3039 public Session Session { get ; } = session ;
3140
41+ public bool Closed { get ; set ; }
42+
43+ public string Reason { get ; set ; }
44+
3245 public async Task < Browser > From ( Session session )
3346 {
3447 var browser = new Browser ( session ) ;
3548 await browser . InitializeAsync ( ) . ConfigureAwait ( false ) ;
3649 return browser ;
3750 }
3851
52+ public void Dispose ( )
53+ {
54+ if ( ! _disposed )
55+ {
56+ Session ? . Dispose ( ) ;
57+ }
58+
59+ _disposed = true ;
60+ }
61+
3962 private async Task InitializeAsync ( )
4063 {
41- var sessionEmitter = this . #disposables . use (
42- new EventEmitter ( this . session )
43- ) ;
44- sessionEmitter . once ( 'ended' , ( { reason } ) => {
45- this . dispose ( reason ) ;
46- } ) ;
64+ Session . Ended += OnSessionEnded ;
65+ Session . Driver . EventReceived += OnEventReceived ;
4766
4867 sessionEmitter . on ( 'script. realmCreated' , info => {
4968 if ( info . type != = 'shared - worker ') {
@@ -58,4 +77,38 @@ private async Task InitializeAsync()
5877 await this . #syncUserContexts ( ) ;
5978 await this . #syncBrowsingContexts ( ) ;
6079 }
80+
81+ private void OnEventReceived ( object sender , EventReceivedEventArgs e )
82+ {
83+ switch ( e . EventName )
84+ {
85+ case "script.realmCreated" :
86+ var realInfo = e . EventData as RealmInfo ;
87+
88+ if ( realInfo . Type != RealmType . SharedWorker )
89+ {
90+ return ;
91+ }
92+
93+ _
94+ }
95+ }
96+
97+ private void OnSessionEnded ( object sender , SessionEndedArgs e )
98+ {
99+ Dispose ( e . Reason ) ;
100+ }
101+
102+ private void Dispose ( string reason = null , bool closed = false )
103+ {
104+ Reason = reason ;
105+ Closed = closed ;
106+ Dispose ( true ) ;
107+ }
108+
109+ protected virtual void Dispose ( bool disposing )
110+ {
111+ Dispose ( ) ;
112+ }
113+
61114}
0 commit comments