Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1312,21 +1312,6 @@
"FAIL"
]
},
{
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
"testIdPattern": "[worker.spec] *",
"platforms": [
"darwin",
"linux",
"win32"
],
"parameters": [
"webDriverBiDi"
],
"expectations": [
"FAIL"
]
},
{
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
"testIdPattern": "[ChromeLauncher.test.ts] *",
Expand Down
15 changes: 13 additions & 2 deletions lib/PuppeteerSharp/Bidi/BidiFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class BidiFrame : Frame
private readonly ConcurrentDictionary<BrowsingContext, BidiFrame> _frames = new();
private readonly Realms _realms;

internal BidiFrame(BidiPage parentPage, BidiFrame parentFrame, BrowsingContext browsingContext)
private BidiFrame(BidiPage parentPage, BidiFrame parentFrame, BrowsingContext browsingContext)
{
Client = new BidiCdpSession(this, parentPage?.BidiBrowser?.LoggerFactory ?? parentFrame?.BidiPage?.BidiBrowser?.LoggerFactory);
ParentPage = parentPage;
Expand Down Expand Up @@ -604,6 +604,17 @@ private void Initialize()
BidiPage.OnPageError(new PageErrorEventArgs(fullStack));
}
};

// Wire up worker events
BrowsingContext.Worker += (_, args) =>
{
var worker = BidiWebWorker.From(this, args.Realm);
args.Realm.Destroyed += (_, _) =>
{
BidiPage.OnWorkerDestroyed(worker);
};
BidiPage.OnWorkerCreated(worker);
};
}

private void CreateFrameTarget(BrowsingContext browsingContext)
Expand All @@ -612,7 +623,7 @@ private void CreateFrameTarget(BrowsingContext browsingContext)
_frames.TryAdd(browsingContext, frame);
((BidiPage)Page).OnFrameAttached(new FrameEventArgs(frame));

browsingContext.Closed += (sender, args) =>
browsingContext.Closed += (_, _) =>
{
_frames.TryRemove(browsingContext, out var _);
};
Expand Down
2 changes: 2 additions & 0 deletions lib/PuppeteerSharp/Bidi/BidiFrameRealm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ internal class BidiFrameRealm(WindowRealm realm, BidiFrame frame) : BidiRealm(re

internal BidiFrame Frame => frame;

internal WindowRealm WindowRealm => _realm;

public static BidiFrameRealm From(WindowRealm realm, BidiFrame frame)
{
var frameRealm = new BidiFrameRealm(realm, frame);
Expand Down
34 changes: 33 additions & 1 deletion lib/PuppeteerSharp/Bidi/BidiPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ namespace PuppeteerSharp.Bidi;
/// <inheritdoc />
public class BidiPage : Page
{
private readonly ConcurrentDictionary<string, BidiWebWorker> _workers = new();
private readonly CdpEmulationManager _cdpEmulationManager;
private InternalNetworkConditions _emulatedNetworkConditions;
private TaskCompletionSource<bool> _closedTcs;
Expand Down Expand Up @@ -74,7 +75,7 @@ public override IFrame[] Frames
}

/// <inheritdoc />
public override WebWorker[] Workers { get; }
public override WebWorker[] Workers => _workers.Values.ToArray();

/// <inheritdoc />
public override bool IsJavaScriptEnabled { get; }
Expand Down Expand Up @@ -518,6 +519,18 @@ internal static BidiPage From(BidiBrowserContext browserContext, BrowsingContext

internal new void OnPageError(PageErrorEventArgs e) => base.OnPageError(e);

internal void OnWorkerCreated(BidiWebWorker worker)
{
_workers[worker.RealmId] = worker;
base.OnWorkerCreated(worker);
}

internal void OnWorkerDestroyed(BidiWebWorker worker)
{
_workers.TryRemove(worker.RealmId, out var _);
base.OnWorkerDestroyed(worker);
}

/// <inheritdoc />
protected override Task<byte[]> PdfInternalAsync(string file, PdfOptions options) => throw new NotImplementedException();

Expand Down Expand Up @@ -669,5 +682,24 @@ private void Initialize()
OnClose();
IsClosed = true;
};

// Track workers
WorkerCreated += (_, e) =>
{
var worker = e.Worker as BidiWebWorker;
if (worker != null)
{
_workers[worker.RealmId] = worker;
}
};

WorkerDestroyed += (_, e) =>
{
var worker = e.Worker as BidiWebWorker;
if (worker != null)
{
_workers.TryRemove(worker.RealmId, out var _);
}
};
}
}
35 changes: 31 additions & 4 deletions lib/PuppeteerSharp/Bidi/BidiWebWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,46 @@
// * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// * SOFTWARE.

using System;
using System.Threading.Tasks;
using PuppeteerSharp.Bidi.Core;

namespace PuppeteerSharp.Bidi;

internal class BidiWebWorker : WebWorker
{
public BidiWebWorker(string url) : base(url)
private readonly BidiFrame _frame;
private readonly BidiWorkerRealm _realm;

private BidiWebWorker(BidiFrame frame, DedicatedWorkerRealm realm) : base(realm.Origin)
{
_frame = frame;
_realm = BidiWorkerRealm.From(realm, this);
RealmId = realm.Id;
}

public override ICDPSession Client { get; }
public override ICDPSession Client => throw new NotSupportedException();

internal override IsolatedWorld World => throw new NotSupportedException();

internal BidiFrame Frame => _frame;

internal TimeoutSettings TimeoutSettings => _frame.TimeoutSettings;

internal string RealmId { get; }

internal override IsolatedWorld World { get; }
public override Task CloseAsync()
{
// BiDi doesn't support closing workers directly.
// Workers are closed when they terminate themselves or when the owning page/frame closes.
// This matches the upstream Puppeteer behavior where close() throws UnsupportedOperation.
throw new NotSupportedException("WebWorker.CloseAsync() is not supported in BiDi protocol");
}

internal static BidiWebWorker From(BidiFrame frame, DedicatedWorkerRealm realm)
{
return new BidiWebWorker(frame, realm);
}

public override Task CloseAsync() => throw new System.NotImplementedException();
internal override Realm GetMainRealm() => _realm;
}
91 changes: 91 additions & 0 deletions lib/PuppeteerSharp/Bidi/BidiWorkerRealm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// * MIT License
// *
// * Copyright (c) Darío Kondratiuk
// *
// * Permission is hereby granted, free of charge, to any person obtaining a copy
// * of this software and associated documentation files (the "Software"), to deal
// * in the Software without restriction, including without limitation the rights
// * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// * copies of the Software, and to permit persons to whom the Software is
// * furnished to do so, subject to the following conditions:
// *
// * The above copyright notice and this permission notice shall be included in all
// * copies or substantial portions of the Software.
// *
// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// * SOFTWARE.

using System.Threading.Tasks;
using PuppeteerSharp.Bidi.Core;
using PuppeteerSharp.Helpers;

namespace PuppeteerSharp.Bidi;

internal class BidiWorkerRealm : BidiRealm
{
private readonly DedicatedWorkerRealm _realm;
private readonly BidiWebWorker _worker;
private readonly TaskQueue _puppeteerUtilQueue = new();
private IJSHandle _puppeteerUtil;

private BidiWorkerRealm(DedicatedWorkerRealm realm, BidiWebWorker worker)
: base(realm, worker.TimeoutSettings)
{
_realm = realm;
_worker = worker;
}

internal override IEnvironment Environment => _worker;

internal BidiWebWorker Worker => _worker;

public static BidiWorkerRealm From(DedicatedWorkerRealm realm, BidiWebWorker worker)
{
var workerRealm = new BidiWorkerRealm(realm, worker);
workerRealm.Initialize();
return workerRealm;
}

public override async Task<IJSHandle> GetPuppeteerUtilAsync()
{
var scriptInjector = _realm.Session.ScriptInjector;

await _puppeteerUtilQueue.Enqueue(async () =>
{
if (_puppeteerUtil == null)
{
await scriptInjector.InjectAsync(
async (script) =>
{
if (_puppeteerUtil != null)
{
await _puppeteerUtil.DisposeAsync().ConfigureAwait(false);
}

_puppeteerUtil = await EvaluateExpressionHandleAsync(script).ConfigureAwait(false);
},
_puppeteerUtil == null).ConfigureAwait(false);
}
}).ConfigureAwait(false);

return _puppeteerUtil;
}

protected override void Initialize()
{
base.Initialize();

_realm.Destroyed += (sender, args) => Dispose();
_realm.Updated += (sender, args) =>
{
// Reset PuppeteerUtil when the realm is updated
_puppeteerUtil = null;
TaskManager.RerunAll();
};
}
}
56 changes: 54 additions & 2 deletions lib/PuppeteerSharp/Bidi/Core/DedicatedWorkerRealm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,27 @@
// * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// * SOFTWARE.

using System;
using System.Collections.Concurrent;
using System.Linq;
using PuppeteerSharp.Helpers;
using WebDriverBiDi.Script;

namespace PuppeteerSharp.Bidi.Core;

internal class DedicatedWorkerRealm : Realm
internal class DedicatedWorkerRealm : Realm, IDedicatedWorkerOwnerRealm
{
private readonly ConcurrentSet<IDedicatedWorkerOwnerRealm> _owners = [];
private readonly ConcurrentDictionary<string, DedicatedWorkerRealm> _workers = [];

private DedicatedWorkerRealm(BrowsingContext context, IDedicatedWorkerOwnerRealm owner, string id, string origin)
: base(context, id, origin)
{
_owners.Add(owner);
}

public event EventHandler<WorkerRealmEventArgs> Worker;

public override Session Session => _owners.FirstOrDefault()?.Session;

public static DedicatedWorkerRealm From(BrowsingContext context, IDedicatedWorkerOwnerRealm owner, string id, string origin)
Expand All @@ -44,8 +50,54 @@ public static DedicatedWorkerRealm From(BrowsingContext context, IDedicatedWorke
return realm;
}

public override void Dispose()
{
foreach (var worker in _workers.Values)
{
worker.Dispose();
}

base.Dispose();
}

private void Initialize()
{
throw new System.NotImplementedException();
// Listen to realm destruction
Session.Driver.Script.OnRealmDestroyed.AddObserver(OnRealmDestroyed);

// Listen to nested worker creation
Session.Driver.Script.OnRealmCreated.AddObserver(OnDedicatedRealmCreated);
}

private void OnRealmDestroyed(RealmDestroyedEventArgs args)
{
if (args.RealmId != Id)
{
return;
}

Dispose("Realm already destroyed.");
}

private void OnDedicatedRealmCreated(RealmCreatedEventArgs args)
{
if (args.Type != RealmType.DedicatedWorker)
{
return;
}

var dedicatedWorkerInfo = args.As<DedicatedWorkerRealmInfo>();

if (!dedicatedWorkerInfo.Owners.Contains(Id))
{
return;
}

var realm = DedicatedWorkerRealm.From(Context, this, dedicatedWorkerInfo.RealmId, dedicatedWorkerInfo.Origin);
_workers.TryAdd(realm.Id, realm);
realm.Destroyed += (sender, args) => _workers.TryRemove(realm.Id, out _);
OnWorker(realm);
}

private void OnWorker(DedicatedWorkerRealm realm) => Worker?.Invoke(this, new WorkerRealmEventArgs(realm));
}
4 changes: 3 additions & 1 deletion lib/PuppeteerSharp/WebWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal WebWorker(string url)
public abstract ICDPSession Client { get; }

/// <inheritdoc/>
Realm IEnvironment.MainRealm => World;
Realm IEnvironment.MainRealm => GetMainRealm();

internal abstract IsolatedWorld World { get; }

Expand Down Expand Up @@ -99,5 +99,7 @@ public async Task<IJSHandle> EvaluateExpressionHandleAsync(string script)
/// </summary>
/// <returns>A <see cref="Task"/> that completes when the worker is closed.</returns>
public abstract Task CloseAsync();

internal virtual Realm GetMainRealm() => World;
}
}
Loading