Skip to content

Commit

Permalink
Use target for filters
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok committed Sep 20, 2023
1 parent 4218fd4 commit 7187cba
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 37 deletions.
32 changes: 17 additions & 15 deletions lib/PuppeteerSharp/Browser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class Browser : IBrowser

private readonly ConcurrentDictionary<string, BrowserContext> _contexts;
private readonly ILogger<Browser> _logger;
private readonly Func<TargetInfo, bool> _targetFilterCallback;
private readonly Func<Target, bool> _targetFilterCallback;
private readonly BrowserContext _defaultContext;
private readonly CustomQueriesManager _customQueriesManager = new();
private Task _closeTask;
Expand All @@ -33,18 +33,18 @@ internal Browser(
bool ignoreHTTPSErrors,
ViewPortOptions defaultViewport,
LauncherBase launcher,
Func<TargetInfo, bool> targetFilter = null,
Func<TargetInfo, bool> isPageTargetFunc = null)
Func<Target, bool> targetFilter = null,
Func<Target, bool> isPageTargetFunc = null)
{
IgnoreHTTPSErrors = ignoreHTTPSErrors;
DefaultViewport = defaultViewport;
Launcher = launcher;
Connection = connection;
_targetFilterCallback = targetFilter ?? ((TargetInfo _) => true);
_targetFilterCallback = targetFilter ?? ((Target _) => true);
_logger = Connection.LoggerFactory.CreateLogger<Browser>();
IsPageTargetFunc =
isPageTargetFunc ??
new Func<TargetInfo, bool>((TargetInfo target) =>
new Func<Target, bool>((Target target) =>
{
return
target.Type == TargetType.Page ||
Expand Down Expand Up @@ -139,7 +139,7 @@ public bool IsClosed

internal ITargetManager TargetManager { get; }

internal Func<TargetInfo, bool> IsPageTargetFunc { get; set; }
internal Func<Target, bool> IsPageTargetFunc { get; set; }

/// <inheritdoc/>
public Task<IPage> NewPageAsync() => _defaultContext.NewPageAsync();
Expand Down Expand Up @@ -269,8 +269,8 @@ internal static async Task<Browser> CreateAsync(
bool ignoreHTTPSErrors,
ViewPortOptions defaultViewPort,
LauncherBase launcher,
Func<TargetInfo, bool> targetFilter = null,
Func<TargetInfo, bool> isPageTargetCallback = null,
Func<Target, bool> targetFilter = null,
Func<Target, bool> isPageTargetCallback = null,
Action<IBrowser> initAction = null)
{
var browser = new Browser(
Expand Down Expand Up @@ -475,7 +475,14 @@ private Target CreateTarget(TargetInfo targetInfo, CDPSession session)

Func<bool, Task<CDPSession>> createSession = (bool isAutoAttachEmulated) => Connection.CreateSessionAsync(targetInfo, isAutoAttachEmulated);

if (IsPageTargetFunc(targetInfo))
var otherTarget = new OtherTarget(
targetInfo,
session,
context,
TargetManager,
createSession);

if (IsPageTargetFunc(otherTarget))
{
return new PageTarget(
targetInfo,
Expand All @@ -498,12 +505,7 @@ private Target CreateTarget(TargetInfo targetInfo, CDPSession session)
createSession);
}

return new OtherTarget(
targetInfo,
session,
context,
TargetManager,
createSession);
return otherTarget;
}
}
}
2 changes: 1 addition & 1 deletion lib/PuppeteerSharp/BrowserContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public async Task<IPage[]> PagesAsync()
Targets()
.Where(t =>
t.Type == TargetType.Page ||
(t.Type == TargetType.Other && Browser.IsPageTargetFunc((t as Target).TargetInfo)))
(t.Type == TargetType.Other && Browser.IsPageTargetFunc(t as Target)))
.Select(t => t.PageAsync())).ConfigureAwait(false))
.Where(p => p != null).ToArray();

Expand Down
27 changes: 17 additions & 10 deletions lib/PuppeteerSharp/ChromeTargetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal class ChromeTargetManager : ITargetManager
private readonly List<string> _ignoredTargets = new();
private readonly Connection _connection;
private readonly Func<TargetInfo, CDPSession, Target> _targetFactoryFunc;
private readonly Func<TargetInfo, bool> _targetFilterFunc;
private readonly Func<Target, bool> _targetFilterFunc;
private readonly ILogger<ChromeTargetManager> _logger;
private readonly AsyncDictionaryHelper<string, Target> _attachedTargetsByTargetId = new("Target {0} not found");
private readonly ConcurrentDictionary<string, Target> _attachedTargetsBySessionId = new();
Expand All @@ -33,7 +33,7 @@ internal class ChromeTargetManager : ITargetManager
public ChromeTargetManager(
Connection connection,
Func<TargetInfo, CDPSession, Target> targetFactoryFunc,
Func<TargetInfo, bool> targetFilterFunc,
Func<Target, bool> targetFilterFunc,
int targetDiscoveryTimeout = 0)
{
_connection = connection;
Expand Down Expand Up @@ -119,7 +119,14 @@ private void StoreExistingTargetsForInit()
{
foreach (var kv in _discoveredTargetsByTargetId)
{
if ((_targetFilterFunc == null || _targetFilterFunc(kv.Value)) &&
var targetForFilter = new Target(
kv.Value,
null,
null,
this,
null);

if ((_targetFilterFunc == null || _targetFilterFunc(targetForFilter)) &&
kv.Value.Type != TargetType.Browser)
{
_targetsIdsForInit.Add(kv.Key);
Expand Down Expand Up @@ -286,7 +293,13 @@ await parent.SendAsync(
return;
}

if (_targetFilterFunc?.Invoke(targetInfo) == false)
var existingTarget = _attachedTargetsByTargetId.TryGetValue(targetInfo.TargetId, out var target);
if (!existingTarget)
{
target = _targetFactoryFunc(targetInfo, session);
}

if (_targetFilterFunc?.Invoke(target) == false)
{
_ignoredTargets.Add(targetInfo.TargetId);
await EnsureTargetsIdsForInitAsync().ConfigureAwait(false);
Expand All @@ -295,12 +308,6 @@ await parent.SendAsync(
return;
}

var existingTarget = _attachedTargetsByTargetId.TryGetValue(targetInfo.TargetId, out var target);
if (!existingTarget)
{
target = _targetFactoryFunc(targetInfo, session);
}

session.MessageReceived += OnMessageReceived;

if (existingTarget)
Expand Down
2 changes: 1 addition & 1 deletion lib/PuppeteerSharp/ConnectOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public class ConnectOptions : IBrowserOptions, IConnectionOptions
/// <summary>
/// Callback to decide if Puppeteer should connect to a given target or not.
/// </summary>
public Func<TargetInfo, bool> TargetFilter { get; set; }
public Func<Target, bool> TargetFilter { get; set; }

/// <summary>
/// Optional callback to initialize properties as soon as the <see cref="IBrowser"/> instance is created, i.e., set up event handlers.
Expand Down
8 changes: 4 additions & 4 deletions lib/PuppeteerSharp/FirefoxTargetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal class FirefoxTargetManager : ITargetManager
{
private readonly Connection _connection;
private readonly Func<TargetInfo, CDPSession, Target> _targetFactoryFunc;
private readonly Func<TargetInfo, bool> _targetFilterFunc;
private readonly Func<Target, bool> _targetFilterFunc;
private readonly ILogger<FirefoxTargetManager> _logger;
private readonly ConcurrentDictionary<ICDPConnection, List<TargetInterceptor>> _targetInterceptors = new();
private readonly AsyncDictionaryHelper<string, Target> _availableTargetsByTargetId = new("Target {0} not found");
Expand All @@ -27,7 +27,7 @@ internal class FirefoxTargetManager : ITargetManager
public FirefoxTargetManager(
Connection connection,
Func<TargetInfo, CDPSession, Target> targetFactoryFunc,
Func<TargetInfo, bool> targetFilterFunc)
Func<Target, bool> targetFilterFunc)
{
_connection = connection;
_targetFilterFunc = targetFilterFunc;
Expand Down Expand Up @@ -121,14 +121,14 @@ private void OnTargetCreated(TargetCreatedResponse e)
FinishInitializationIfReady(e.TargetInfo.TargetId);
}

if (_targetFilterFunc != null && !_targetFilterFunc(e.TargetInfo))
var target = _targetFactoryFunc(e.TargetInfo, null);
if (_targetFilterFunc != null && !_targetFilterFunc(target))
{
_ignoredTargets.Add(e.TargetInfo.TargetId);
FinishInitializationIfReady(e.TargetInfo.TargetId);
return;
}

var target = _targetFactoryFunc(e.TargetInfo, null);
_availableTargetsByTargetId.AddItem(e.TargetInfo.TargetId, target);
TargetAvailable?.Invoke(
this,
Expand Down
2 changes: 1 addition & 1 deletion lib/PuppeteerSharp/IConnectionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ public interface IConnectionOptions
/// <summary>
/// Callback to decide if Puppeteer should connect to a given target or not.
/// </summary>
public Func<TargetInfo, bool> TargetFilter { get; set; }
public Func<Target, bool> TargetFilter { get; set; }
}
}
2 changes: 1 addition & 1 deletion lib/PuppeteerSharp/LaunchOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,6 @@ public string[] IgnoredDefaultArgs
/// <summary>
/// Callback to decide if Puppeteer should connect to a given target or not.
/// </summary>
public Func<TargetInfo, bool> TargetFilter { get; set; }
public Func<Target, bool> TargetFilter { get; set; }
}
}
8 changes: 4 additions & 4 deletions lib/PuppeteerSharp/PuppeteerSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
<Description>Headless Browser .NET API</Description>
<PackageId>PuppeteerSharp</PackageId>
<PackageReleaseNotes></PackageReleaseNotes>
<PackageVersion>11.0.6</PackageVersion>
<ReleaseVersion>11.0.6</ReleaseVersion>
<AssemblyVersion>11.0.6</AssemblyVersion>
<FileVersion>11.0.6</FileVersion>
<PackageVersion>12.0.0</PackageVersion>
<ReleaseVersion>12.0.0</ReleaseVersion>
<AssemblyVersion>12.0.0</AssemblyVersion>
<FileVersion>12.0.0</FileVersion>
<SynchReleaseVersion>false</SynchReleaseVersion>
<StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
<DebugType>embedded</DebugType>
Expand Down

0 comments on commit 7187cba

Please sign in to comment.