From be763f4ac06659bf9c9f2ddcfe9c3752ca3d32ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Kondratiuk?= Date: Tue, 24 Dec 2024 14:27:59 -0300 Subject: [PATCH] some fixes in the context --- lib/PuppeteerSharp/Bidi/Core/BrowsingContext.cs | 2 +- .../Bidi/Core/DedicatedWorkerRealm.cs | 8 ++++---- lib/PuppeteerSharp/Bidi/Core/Realm.cs | 7 +++++-- lib/PuppeteerSharp/Bidi/WindowRealm.cs | 16 +++++++++------- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/lib/PuppeteerSharp/Bidi/Core/BrowsingContext.cs b/lib/PuppeteerSharp/Bidi/Core/BrowsingContext.cs index 68de760e6..8ce526386 100644 --- a/lib/PuppeteerSharp/Bidi/Core/BrowsingContext.cs +++ b/lib/PuppeteerSharp/Bidi/Core/BrowsingContext.cs @@ -157,7 +157,7 @@ private void Initialize() return; } - var browsingContext = From(UserContext, this, args.UserContextId, args.Url, args.OriginalOpener); + var browsingContext = From(UserContext, this, args.BrowsingContextId, args.Url, args.OriginalOpener); _children.TryAdd(args.UserContextId, browsingContext); diff --git a/lib/PuppeteerSharp/Bidi/Core/DedicatedWorkerRealm.cs b/lib/PuppeteerSharp/Bidi/Core/DedicatedWorkerRealm.cs index ec12b6194..1ccae26cd 100644 --- a/lib/PuppeteerSharp/Bidi/Core/DedicatedWorkerRealm.cs +++ b/lib/PuppeteerSharp/Bidi/Core/DedicatedWorkerRealm.cs @@ -29,17 +29,17 @@ internal class DedicatedWorkerRealm : Realm { private readonly ConcurrentSet _owners = []; - private DedicatedWorkerRealm(IDedicatedWorkerOwnerRealm owner, string id, string origin) - : base(id, origin) + private DedicatedWorkerRealm(BrowsingContext context, IDedicatedWorkerOwnerRealm owner, string id, string origin) + : base(context, id, origin) { _owners.Add(owner); } public override Session Session => _owners.FirstOrDefault()?.Session; - public static DedicatedWorkerRealm From(IDedicatedWorkerOwnerRealm owner, string id, string origin) + public static DedicatedWorkerRealm From(BrowsingContext context, IDedicatedWorkerOwnerRealm owner, string id, string origin) { - var realm = new DedicatedWorkerRealm(owner, id, origin); + var realm = new DedicatedWorkerRealm(context, owner, id, origin); realm.Initialize(); return realm; } diff --git a/lib/PuppeteerSharp/Bidi/Core/Realm.cs b/lib/PuppeteerSharp/Bidi/Core/Realm.cs index 1eaa094f3..ed7a7c61c 100644 --- a/lib/PuppeteerSharp/Bidi/Core/Realm.cs +++ b/lib/PuppeteerSharp/Bidi/Core/Realm.cs @@ -26,7 +26,7 @@ namespace PuppeteerSharp.Bidi.Core; -internal abstract class Realm(string id, string origin) : IDisposable +internal abstract class Realm(BrowsingContext context, string id, string origin) : IDisposable { private string _reason; @@ -40,10 +40,13 @@ internal abstract class Realm(string id, string origin) : IDisposable public bool Disposed { get; private set; } - public WebDriverBiDi.Script.Target Target => new RealmTarget(Id); + public virtual ContextTarget Target => new ContextTarget(Id); public abstract Session Session { get; } + // Just for testing purposes + internal BrowsingContext Context => context; + public void Dispose(string reason) { _reason = reason; diff --git a/lib/PuppeteerSharp/Bidi/WindowRealm.cs b/lib/PuppeteerSharp/Bidi/WindowRealm.cs index 13a2012bc..1109eba1a 100644 --- a/lib/PuppeteerSharp/Bidi/WindowRealm.cs +++ b/lib/PuppeteerSharp/Bidi/WindowRealm.cs @@ -22,20 +22,22 @@ using System; using System.Collections.Concurrent; +using System.Threading.Tasks; using PuppeteerSharp.Bidi.Core; -using PuppeteerSharp.Helpers; using WebDriverBiDi.Script; namespace PuppeteerSharp.Bidi; -internal class WindowRealm(BrowsingContext browsingContext, string sandbox = null) : Core.Realm(string.Empty, string.Empty), IDedicatedWorkerOwnerRealm +internal class WindowRealm(BrowsingContext browsingContext, string sandbox = null) : Core.Realm(browsingContext, string.Empty, string.Empty), IDedicatedWorkerOwnerRealm { private readonly string _sandbox = sandbox; private readonly ConcurrentDictionary _workers = []; public event EventHandler Worker; - public override Session Session => browsingContext.UserContext.Browser.Session; + public override Session Session => Context.UserContext.Browser.Session; + + public override ContextTarget Target => new(Context.Id); // TODO: Add sandbox public string ExecutionContextId { get; set; } @@ -48,7 +50,7 @@ public static WindowRealm From(BrowsingContext context, string sandbox = null) public override void Dispose() { - browsingContext.Dispose(); + Context.Dispose(); Session.Dispose(); foreach (var worker in _workers.Values) @@ -61,7 +63,7 @@ public override void Dispose() private void Initialize() { - browsingContext.Closed += (sender, args) => Dispose(args.Reason); + Context.Closed += (sender, args) => Dispose(args.Reason); Session.Driver.Script.OnRealmCreated.AddObserver(OnWindowRealmCreated); Session.Driver.Script.OnRealmCreated.AddObserver(OnDedicatedRealmCreated); } @@ -80,7 +82,7 @@ private void OnWindowRealmCreated(RealmCreatedEventArgs args) return; } - var realm = DedicatedWorkerRealm.From(this, dedicatedWorkerInfo.RealmId, dedicatedWorkerInfo.Origin); + 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); @@ -91,7 +93,7 @@ private void OnWindowRealmCreated(RealmCreatedEventArgs args) private void OnDedicatedRealmCreated(RealmCreatedEventArgs args) { if (args.Type != RealmType.Window || - args.As().BrowsingContext != browsingContext.Id || + args.As().BrowsingContext != Context.Id || args.As().Sandbox != _sandbox) { return;