Skip to content

Commit 2723ae2

Browse files
authored
Merge branch 'master' into type_dictionary
2 parents 501f7c7 + c4df9a1 commit 2723ae2

File tree

5 files changed

+48
-52
lines changed

5 files changed

+48
-52
lines changed

lib/PuppeteerSharp/BrowserData/InstalledBrowser.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.IO;
43

54
namespace PuppeteerSharp.BrowserData
@@ -9,14 +8,6 @@ namespace PuppeteerSharp.BrowserData
98
/// </summary>
109
public class InstalledBrowser
1110
{
12-
private static readonly Dictionary<SupportedBrowser, Func<Platform, string, string>> _executablePathByBrowser = new()
13-
{
14-
[SupportedBrowser.Chrome] = Chrome.RelativeExecutablePath,
15-
[SupportedBrowser.ChromeHeadlessShell] = ChromeHeadlessShell.RelativeExecutablePath,
16-
[SupportedBrowser.Chromium] = Chromium.RelativeExecutablePath,
17-
[SupportedBrowser.Firefox] = Firefox.RelativeExecutablePath,
18-
};
19-
2011
/// <summary>
2112
/// Initializes a new instance of the <see cref="InstalledBrowser"/> class.
2213
/// </summary>
@@ -62,7 +53,16 @@ public string GetExecutablePath()
6253
var installationDir = Cache.GetInstallationDir(Browser, Platform, BuildId);
6354
return Path.Combine(
6455
installationDir,
65-
_executablePathByBrowser[Browser](Platform, BuildId));
56+
GetExecutablePath(Browser, Platform, BuildId));
6657
}
58+
59+
private static string GetExecutablePath(SupportedBrowser browser, Platform platform, string buildId) => browser switch
60+
{
61+
SupportedBrowser.Chrome => Chrome.RelativeExecutablePath(platform, buildId),
62+
SupportedBrowser.ChromeHeadlessShell => ChromeHeadlessShell.RelativeExecutablePath(platform, buildId),
63+
SupportedBrowser.Chromium => Chromium.RelativeExecutablePath(platform, buildId),
64+
SupportedBrowser.Firefox => Firefox.RelativeExecutablePath(platform, buildId),
65+
_ => throw new NotSupportedException(),
66+
};
6767
}
6868
}

lib/PuppeteerSharp/BrowserFetcher.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,6 @@ public sealed class BrowserFetcher : IBrowserFetcher
2222
{
2323
private const string PublishSingleFileLocalApplicationDataFolderName = "PuppeteerSharp";
2424

25-
private static readonly Dictionary<SupportedBrowser, Func<Platform, string, string, string>> _downloadsUrl = new()
26-
{
27-
[SupportedBrowser.Chrome] = Chrome.ResolveDownloadUrl,
28-
[SupportedBrowser.ChromeHeadlessShell] = ChromeHeadlessShell.ResolveDownloadUrl,
29-
[SupportedBrowser.Chromium] = Chromium.ResolveDownloadUrl,
30-
[SupportedBrowser.Firefox] = Firefox.ResolveDownloadUrl,
31-
};
32-
3325
private readonly CustomFileDownloadAction _customFileDownload;
3426
private readonly ILogger<BrowserFetcher> _logger;
3527

@@ -206,8 +198,14 @@ internal static string GetBrowsersLocation()
206198
return assemblyDirectory.FullName;
207199
}
208200

209-
private static string GetDownloadURL(SupportedBrowser browser, Platform platform, string baseUrl, string buildId)
210-
=> _downloadsUrl[browser](platform, buildId, baseUrl);
201+
private static string GetDownloadURL(SupportedBrowser browser, Platform platform, string baseUrl, string buildId) => browser switch
202+
{
203+
SupportedBrowser.Chrome => Chrome.ResolveDownloadUrl(platform, buildId, baseUrl),
204+
SupportedBrowser.ChromeHeadlessShell => ChromeHeadlessShell.ResolveDownloadUrl(platform, buildId, baseUrl),
205+
SupportedBrowser.Chromium => Chromium.ResolveDownloadUrl(platform, buildId, baseUrl),
206+
SupportedBrowser.Firefox => Firefox.ResolveDownloadUrl(platform, buildId, baseUrl),
207+
_ => throw new NotSupportedException(),
208+
};
211209

212210
private static void ExtractTar(string zipPath, string folderPath)
213211
{
@@ -236,7 +234,7 @@ private static void ExecuteSetup(string exePath, string folderPath)
236234

237235
private async Task<InstalledBrowser> DownloadAsync(SupportedBrowser browser, string buildId)
238236
{
239-
var url = _downloadsUrl[browser](Platform, buildId, BaseUrl);
237+
var url = GetDownloadURL(browser, Platform, BaseUrl, buildId);
240238
var fileName = url.Split('/').Last();
241239
var cache = new Cache(CacheDir);
242240
var archivePath = Path.Combine(CacheDir, fileName);

lib/PuppeteerSharp/Cdp/CdpPage.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,6 @@ namespace PuppeteerSharp.Cdp;
4343
/// <inheritdoc />
4444
public class CdpPage : Page
4545
{
46-
private static readonly Dictionary<string, decimal> _unitToPixels = new()
47-
{
48-
["px"] = 1,
49-
["in"] = 96,
50-
["cm"] = 37.8m,
51-
["mm"] = 3.78m,
52-
};
53-
5446
private readonly ConcurrentDictionary<string, CdpWebWorker> _workers = new();
5547
private readonly ITargetManager _targetManager;
5648
private readonly EmulationManager _emulationManager;
@@ -920,6 +912,15 @@ protected override async Task<string> PerformScreenshotAsync(ScreenshotType type
920912
}
921913
}
922914

915+
private static decimal? GetPixels(string unit) => unit switch
916+
{
917+
"px" => 1,
918+
"in" => 96,
919+
"cm" => 37.8m,
920+
"mm" => 3.78m,
921+
_ => null,
922+
};
923+
923924
private void SetupPrimaryTargetListeners()
924925
{
925926
PrimaryTargetClient.Ready += OnAttachedToTarget;
@@ -1230,7 +1231,7 @@ private decimal ConvertPrintParameterToInches(object parameter)
12301231
var text = parameter.ToString();
12311232
var unit = text.Length > 2 ? text.Substring(text.Length - 2).ToLower(CultureInfo.CurrentCulture) : string.Empty;
12321233
string valueText;
1233-
if (_unitToPixels.ContainsKey(unit))
1234+
if (GetPixels(unit) is { })
12341235
{
12351236
valueText = text.Substring(0, text.Length - 2);
12361237
}
@@ -1244,7 +1245,7 @@ private decimal ConvertPrintParameterToInches(object parameter)
12441245

12451246
if (decimal.TryParse(valueText, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var number))
12461247
{
1247-
pixels = number * _unitToPixels[unit];
1248+
pixels = number * GetPixels(unit).Value;
12481249
}
12491250
else
12501251
{

lib/PuppeteerSharp/Cdp/LifecycleWatcher.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,6 @@ namespace PuppeteerSharp.Cdp
1111
{
1212
internal sealed class LifecycleWatcher : IDisposable
1313
{
14-
private static readonly Dictionary<WaitUntilNavigation, string> _puppeteerToProtocolLifecycle =
15-
new()
16-
{
17-
[WaitUntilNavigation.Load] = "load",
18-
[WaitUntilNavigation.DOMContentLoaded] = "DOMContentLoaded",
19-
[WaitUntilNavigation.Networkidle0] = "networkIdle",
20-
[WaitUntilNavigation.Networkidle2] = "networkAlmostIdle",
21-
};
22-
2314
private static readonly WaitUntilNavigation[] _defaultWaitUntil = [WaitUntilNavigation.Load];
2415

2516
private readonly NetworkManager _networkManager;
@@ -44,7 +35,7 @@ public LifecycleWatcher(
4435
{
4536
_expectedLifecycle = (waitUntil ?? _defaultWaitUntil).Select(w =>
4637
{
47-
var protocolEvent = _puppeteerToProtocolLifecycle.GetValue(w);
38+
var protocolEvent = GetProtocolEvent(w);
4839
Contract.Assert(protocolEvent != null, $"Unknown value for options.waitUntil: {w}");
4940
return protocolEvent;
5041
});
@@ -87,6 +78,15 @@ public void Dispose()
8778
_terminationCancellationToken.Dispose();
8879
}
8980

81+
private static string GetProtocolEvent(WaitUntilNavigation waitUntil) => waitUntil switch
82+
{
83+
WaitUntilNavigation.Load => "load",
84+
WaitUntilNavigation.DOMContentLoaded => "DOMContentLoaded",
85+
WaitUntilNavigation.Networkidle0 => "networkIdle",
86+
WaitUntilNavigation.Networkidle2 => "networkAlmostIdle",
87+
_ => null,
88+
};
89+
9090
private void Navigated(object sender, FrameNavigatedEventArgs e)
9191
{
9292
if (e.Type == NavigationType.BackForwardCacheRestore)

lib/PuppeteerSharp/ScreenshotOptions.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Collections.Generic;
21
using System.IO;
32
using System.Text.Json.Serialization;
43
using PuppeteerSharp.Media;
@@ -10,14 +9,6 @@ namespace PuppeteerSharp
109
/// </summary>
1110
public class ScreenshotOptions
1211
{
13-
private static readonly Dictionary<string, ScreenshotType?> _extensionScreenshotTypeMap = new()
14-
{
15-
["jpe"] = ScreenshotType.Jpeg,
16-
["jpeg"] = ScreenshotType.Jpeg,
17-
["jpg"] = ScreenshotType.Jpeg,
18-
["png"] = ScreenshotType.Png,
19-
};
20-
2112
/// <summary>
2213
/// Specifies clipping region of the page.
2314
/// </summary>
@@ -87,8 +78,14 @@ public class ScreenshotOptions
8778
internal static ScreenshotType? GetScreenshotTypeFromFile(string file)
8879
{
8980
var extension = new FileInfo(file).Extension.Replace(".", string.Empty);
90-
_extensionScreenshotTypeMap.TryGetValue(extension, out var result);
91-
return result;
81+
return GetScreenshotType(extension);
9282
}
83+
84+
private static ScreenshotType? GetScreenshotType(string extension) => extension switch
85+
{
86+
"jpe" or "jpeg" or "jpg" => ScreenshotType.Jpeg,
87+
"png" => ScreenshotType.Png,
88+
_ => null,
89+
};
9390
}
9491
}

0 commit comments

Comments
 (0)