Skip to content

Commit

Permalink
some fixes in the context
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok committed Dec 24, 2024
1 parent d499126 commit be763f4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/PuppeteerSharp/Bidi/Core/BrowsingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
8 changes: 4 additions & 4 deletions lib/PuppeteerSharp/Bidi/Core/DedicatedWorkerRealm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ internal class DedicatedWorkerRealm : Realm
{
private readonly ConcurrentSet<IDedicatedWorkerOwnerRealm> _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;
}
Expand Down
7 changes: 5 additions & 2 deletions lib/PuppeteerSharp/Bidi/Core/Realm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down
16 changes: 9 additions & 7 deletions lib/PuppeteerSharp/Bidi/WindowRealm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, DedicatedWorkerRealm> _workers = [];

public event EventHandler<WorkerRealmEventArgs> 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; }

Expand All @@ -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)
Expand All @@ -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);
}
Expand All @@ -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);
Expand All @@ -91,7 +93,7 @@ private void OnWindowRealmCreated(RealmCreatedEventArgs args)
private void OnDedicatedRealmCreated(RealmCreatedEventArgs args)
{
if (args.Type != RealmType.Window ||
args.As<WindowRealmInfo>().BrowsingContext != browsingContext.Id ||
args.As<WindowRealmInfo>().BrowsingContext != Context.Id ||
args.As<WindowRealmInfo>().Sandbox != _sandbox)
{
return;
Expand Down

0 comments on commit be763f4

Please sign in to comment.