Skip to content

Commit ba490de

Browse files
committed
more progress
1 parent 966fdc3 commit ba490de

File tree

5 files changed

+99
-27
lines changed

5 files changed

+99
-27
lines changed

lib/PuppeteerSharp/Bidi/BidiBrowser.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using System.Diagnostics;
2525
using System.Threading.Tasks;
2626
using Microsoft.Extensions.Logging;
27+
using PuppeteerSharp.Bidi.Core;
2728
using PuppeteerSharp.Transport;
2829
using WebDriverBiDi;
2930
using WebDriverBiDi.Session;

lib/PuppeteerSharp/Bidi/Core/Browser.cs

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,48 @@
2121
// * SOFTWARE.
2222

2323
using System;
24+
using System.Collections.Concurrent;
2425
using System.Threading.Tasks;
26+
using PuppeteerSharp.Cdp;
27+
using PuppeteerSharp.States;
28+
using WebDriverBiDi.Protocol;
29+
using WebDriverBiDi.Script;
2530

2631
namespace PuppeteerSharp.Bidi.Core;
2732

2833
internal 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
}

lib/PuppeteerSharp/Bidi/Core/Session.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,23 @@
2828

2929
namespace PuppeteerSharp.Bidi.Core;
3030

31-
internal class Session(BiDiDriver driver, NewCommandResult newCommandResult)
31+
internal class Session(BiDiDriver driver, NewCommandResult newCommandResult) : IDisposable
3232
{
33+
public event EventHandler<SessionEndedArgs> Ended;
34+
3335
public BiDiDriver Driver { get; } = driver;
3436

3537
public NewCommandResult NewCommandResult { get; } = newCommandResult;
3638

3739
public Browser Browser { get; private set; }
3840

39-
internal static async Task<Session> FromAsync(BiDiDriver driver, NewCommandParameters capabilities, ILoggerFactory loggerFactory)
41+
public void Dispose()
4042
{
41-
NewCommandResult result = null;
42-
try
43-
{
44-
result = await driver.Session.NewSessionAsync(capabilities).ConfigureAwait(false);
45-
}
46-
catch (Exception ex)
47-
{
48-
loggerFactory.CreateLogger<Session>().LogError(ex, "Unable to create session");
49-
result = new NewCommandResult()
50-
{
51-
SessionId = string.Empty,
52-
Capabilities = new CapabilitiesResult(),
53-
};
54-
}
43+
}
5544

45+
internal static async Task<Session> FromAsync(BiDiDriver driver, NewCommandParameters capabilities, ILoggerFactory loggerFactory)
46+
{
47+
var result = await driver.Session.NewSessionAsync(capabilities).ConfigureAwait(false);
5648
var session = new Session(driver, result);
5749
await session.InitializeAsync().ConfigureAwait(false);
5850
return session;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// * MIT License
2+
// *
3+
// * Copyright (c) Darío Kondratiuk
4+
// *
5+
// * Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// * of this software and associated documentation files (the "Software"), to deal
7+
// * in the Software without restriction, including without limitation the rights
8+
// * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// * copies of the Software, and to permit persons to whom the Software is
10+
// * furnished to do so, subject to the following conditions:
11+
// *
12+
// * The above copyright notice and this permission notice shall be included in all
13+
// * copies or substantial portions of the Software.
14+
// *
15+
// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// * SOFTWARE.
22+
23+
namespace PuppeteerSharp.Bidi.Core;
24+
25+
internal record SessionEndedArgs
26+
{
27+
public string Reason { get; set; }
28+
}
29+

lib/PuppeteerSharp/Launcher.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,10 @@ public async Task<IBrowser> LaunchAsync(LaunchOptions options)
8080
if (options.Protocol == ProtocolType.WebdriverBiDi)
8181
{
8282
Process.StateManager.LineOutputExpression = "^WebDriver BiDi listening on (ws:\\/\\/.*)$";
83-
}
84-
85-
if (options.Protocol == ProtocolType.WebdriverBiDi)
86-
{
87-
var driver = new BiDiDriver(options.ProtocolTimeout);
83+
var driver = new BiDiDriver(TimeSpan.FromMilliseconds(options.ProtocolTimeout));
8884
await driver.StartAsync(Process.EndPoint).ConfigureAwait(false);
8985

86+
browser = await BidiBrowser.CreateAsync(driver, options, _loggerFactory).ConfigureAwait(false);
9087
}
9188
else
9289
{

0 commit comments

Comments
 (0)