From 7187cba9c41d0cc290c19c371835b2e1b242346e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Kondratiuk?= Date: Wed, 20 Sep 2023 10:12:11 -0300 Subject: [PATCH] Use target for filters --- lib/PuppeteerSharp/Browser.cs | 32 ++++++++++++---------- lib/PuppeteerSharp/BrowserContext.cs | 2 +- lib/PuppeteerSharp/ChromeTargetManager.cs | 27 +++++++++++------- lib/PuppeteerSharp/ConnectOptions.cs | 2 +- lib/PuppeteerSharp/FirefoxTargetManager.cs | 8 +++--- lib/PuppeteerSharp/IConnectionOptions.cs | 2 +- lib/PuppeteerSharp/LaunchOptions.cs | 2 +- lib/PuppeteerSharp/PuppeteerSharp.csproj | 8 +++--- 8 files changed, 46 insertions(+), 37 deletions(-) diff --git a/lib/PuppeteerSharp/Browser.cs b/lib/PuppeteerSharp/Browser.cs index 110fd6ee0..d55ae8173 100644 --- a/lib/PuppeteerSharp/Browser.cs +++ b/lib/PuppeteerSharp/Browser.cs @@ -21,7 +21,7 @@ public class Browser : IBrowser private readonly ConcurrentDictionary _contexts; private readonly ILogger _logger; - private readonly Func _targetFilterCallback; + private readonly Func _targetFilterCallback; private readonly BrowserContext _defaultContext; private readonly CustomQueriesManager _customQueriesManager = new(); private Task _closeTask; @@ -33,18 +33,18 @@ internal Browser( bool ignoreHTTPSErrors, ViewPortOptions defaultViewport, LauncherBase launcher, - Func targetFilter = null, - Func isPageTargetFunc = null) + Func targetFilter = null, + Func isPageTargetFunc = null) { IgnoreHTTPSErrors = ignoreHTTPSErrors; DefaultViewport = defaultViewport; Launcher = launcher; Connection = connection; - _targetFilterCallback = targetFilter ?? ((TargetInfo _) => true); + _targetFilterCallback = targetFilter ?? ((Target _) => true); _logger = Connection.LoggerFactory.CreateLogger(); IsPageTargetFunc = isPageTargetFunc ?? - new Func((TargetInfo target) => + new Func((Target target) => { return target.Type == TargetType.Page || @@ -139,7 +139,7 @@ public bool IsClosed internal ITargetManager TargetManager { get; } - internal Func IsPageTargetFunc { get; set; } + internal Func IsPageTargetFunc { get; set; } /// public Task NewPageAsync() => _defaultContext.NewPageAsync(); @@ -269,8 +269,8 @@ internal static async Task CreateAsync( bool ignoreHTTPSErrors, ViewPortOptions defaultViewPort, LauncherBase launcher, - Func targetFilter = null, - Func isPageTargetCallback = null, + Func targetFilter = null, + Func isPageTargetCallback = null, Action initAction = null) { var browser = new Browser( @@ -475,7 +475,14 @@ private Target CreateTarget(TargetInfo targetInfo, CDPSession session) Func> 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, @@ -498,12 +505,7 @@ private Target CreateTarget(TargetInfo targetInfo, CDPSession session) createSession); } - return new OtherTarget( - targetInfo, - session, - context, - TargetManager, - createSession); + return otherTarget; } } } diff --git a/lib/PuppeteerSharp/BrowserContext.cs b/lib/PuppeteerSharp/BrowserContext.cs index e10b89ecf..ec50bb85a 100644 --- a/lib/PuppeteerSharp/BrowserContext.cs +++ b/lib/PuppeteerSharp/BrowserContext.cs @@ -52,7 +52,7 @@ public async Task 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(); diff --git a/lib/PuppeteerSharp/ChromeTargetManager.cs b/lib/PuppeteerSharp/ChromeTargetManager.cs index 4548a9e18..3446df4d2 100644 --- a/lib/PuppeteerSharp/ChromeTargetManager.cs +++ b/lib/PuppeteerSharp/ChromeTargetManager.cs @@ -17,7 +17,7 @@ internal class ChromeTargetManager : ITargetManager private readonly List _ignoredTargets = new(); private readonly Connection _connection; private readonly Func _targetFactoryFunc; - private readonly Func _targetFilterFunc; + private readonly Func _targetFilterFunc; private readonly ILogger _logger; private readonly AsyncDictionaryHelper _attachedTargetsByTargetId = new("Target {0} not found"); private readonly ConcurrentDictionary _attachedTargetsBySessionId = new(); @@ -33,7 +33,7 @@ internal class ChromeTargetManager : ITargetManager public ChromeTargetManager( Connection connection, Func targetFactoryFunc, - Func targetFilterFunc, + Func targetFilterFunc, int targetDiscoveryTimeout = 0) { _connection = connection; @@ -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); @@ -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); @@ -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) diff --git a/lib/PuppeteerSharp/ConnectOptions.cs b/lib/PuppeteerSharp/ConnectOptions.cs index ad821d0a1..983ac1b7d 100644 --- a/lib/PuppeteerSharp/ConnectOptions.cs +++ b/lib/PuppeteerSharp/ConnectOptions.cs @@ -89,7 +89,7 @@ public class ConnectOptions : IBrowserOptions, IConnectionOptions /// /// Callback to decide if Puppeteer should connect to a given target or not. /// - public Func TargetFilter { get; set; } + public Func TargetFilter { get; set; } /// /// Optional callback to initialize properties as soon as the instance is created, i.e., set up event handlers. diff --git a/lib/PuppeteerSharp/FirefoxTargetManager.cs b/lib/PuppeteerSharp/FirefoxTargetManager.cs index 76d1c0ded..12aa7835e 100644 --- a/lib/PuppeteerSharp/FirefoxTargetManager.cs +++ b/lib/PuppeteerSharp/FirefoxTargetManager.cs @@ -14,7 +14,7 @@ internal class FirefoxTargetManager : ITargetManager { private readonly Connection _connection; private readonly Func _targetFactoryFunc; - private readonly Func _targetFilterFunc; + private readonly Func _targetFilterFunc; private readonly ILogger _logger; private readonly ConcurrentDictionary> _targetInterceptors = new(); private readonly AsyncDictionaryHelper _availableTargetsByTargetId = new("Target {0} not found"); @@ -27,7 +27,7 @@ internal class FirefoxTargetManager : ITargetManager public FirefoxTargetManager( Connection connection, Func targetFactoryFunc, - Func targetFilterFunc) + Func targetFilterFunc) { _connection = connection; _targetFilterFunc = targetFilterFunc; @@ -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, diff --git a/lib/PuppeteerSharp/IConnectionOptions.cs b/lib/PuppeteerSharp/IConnectionOptions.cs index 416fedd73..2df26d2bc 100644 --- a/lib/PuppeteerSharp/IConnectionOptions.cs +++ b/lib/PuppeteerSharp/IConnectionOptions.cs @@ -50,6 +50,6 @@ public interface IConnectionOptions /// /// Callback to decide if Puppeteer should connect to a given target or not. /// - public Func TargetFilter { get; set; } + public Func TargetFilter { get; set; } } } diff --git a/lib/PuppeteerSharp/LaunchOptions.cs b/lib/PuppeteerSharp/LaunchOptions.cs index b8bb1f04f..6869be0e7 100644 --- a/lib/PuppeteerSharp/LaunchOptions.cs +++ b/lib/PuppeteerSharp/LaunchOptions.cs @@ -186,6 +186,6 @@ public string[] IgnoredDefaultArgs /// /// Callback to decide if Puppeteer should connect to a given target or not. /// - public Func TargetFilter { get; set; } + public Func TargetFilter { get; set; } } } diff --git a/lib/PuppeteerSharp/PuppeteerSharp.csproj b/lib/PuppeteerSharp/PuppeteerSharp.csproj index 9fbf9cbc6..22c2a34b9 100644 --- a/lib/PuppeteerSharp/PuppeteerSharp.csproj +++ b/lib/PuppeteerSharp/PuppeteerSharp.csproj @@ -12,10 +12,10 @@ Headless Browser .NET API PuppeteerSharp - 11.0.6 - 11.0.6 - 11.0.6 - 11.0.6 + 12.0.0 + 12.0.0 + 12.0.0 + 12.0.0 false false embedded