Skip to content

Commit

Permalink
Update chromium flags (#2364)
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok authored Nov 27, 2023
1 parent 4eb3d16 commit bc76624
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 41 deletions.
45 changes: 45 additions & 0 deletions lib/PuppeteerSharp.Tests/ChromeLauncherTests/GetFeaturesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using PuppeteerSharp.Nunit;
using NUnit.Framework;

namespace PuppeteerSharp.Tests.ChromeLauncherTests
{
public class GetFeaturesTests : PuppeteerPageBaseTest
{

[PuppeteerTest("ChromeLauncher.test.ts", "getFeatures", "returns an empty array when no options are provided")]
public void ReturnsAnEmptyArrayWhenNoOptionsAreProvided()
{
var result = ChromiumLauncher.GetFeatures("--foo", Array.Empty<string>());
Assert.IsEmpty(result);
}

[PuppeteerTest("ChromeLauncher.test.ts", "getFeatures", "returns an empty array when no options match the flag")]
public void ReturnsAnEmptyArrayWhenNoOptionsMatchTheFlag()
{
var result = ChromiumLauncher.GetFeatures("--foo", new[] { "--bar", "--baz" });
Assert.IsEmpty(result);
}

[PuppeteerTest("ChromeLauncher.test.ts", "getFeatures", "returns an array of values when options match the flag")]
public void ReturnsAnArrayOfValuesWhenOptionsMatchTheFlag()
{
var result = ChromiumLauncher.GetFeatures("--foo", new[] { "--foo=bar", "--foo=baz" });
Assert.AreEqual(new[] { "bar", "baz" }, result);
}

[PuppeteerTest("ChromeLauncher.test.ts", "getFeatures", "does not handle whitespace")]
public void DoesNotHandleWhitespace()
{
var result = ChromiumLauncher.GetFeatures("--foo", new[] { "--foo bar", "--foo baz " });
Assert.IsEmpty(result);
}

[PuppeteerTest("ChromeLauncher.test.ts", "getFeatures", "handles equals sign around the flag and value")]
public void HandlesEqualsSignAroundTheFlagAndValue()
{
var result = ChromiumLauncher.GetFeatures("--foo", new[] { "--foo=bar", "--foo=baz" });
Assert.AreEqual(new[] { "bar", "baz" }, result);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using PuppeteerSharp.Nunit;
using NUnit.Framework;

namespace PuppeteerSharp.Tests.ChromeLauncherTests
{
public class RemoveMatchingFlagsTests : PuppeteerPageBaseTest
{

[PuppeteerTest("ChromeLauncher.test.ts", "removeMatchingFlags", "empty")]
public void Empty()
{
var a = Array.Empty<string>();
var result = ChromiumLauncher.RemoveMatchingFlags(a, "--foo");
Assert.IsEmpty(result);
}

[PuppeteerTest("ChromeLauncher.test.ts", "removeMatchingFlags", "with one match")]
public void WithOneMatch()
{
var a = new[] { "--foo=1", "--bar=baz" };
var result = ChromiumLauncher.RemoveMatchingFlags(a, "--foo");
Assert.AreEqual(new[] { "--bar=baz" }, result);
}

[PuppeteerTest("ChromeLauncher.test.ts", "removeMatchingFlags", "with multiple matches")]
public void WithMultipleMatches()
{
var a = new[] { "--foo=1", "--bar=baz", "--foo=2" };
var result = ChromiumLauncher.RemoveMatchingFlags(a, "--foo");
Assert.AreEqual(new[] { "--bar=baz" }, result);
}

[PuppeteerTest("ChromeLauncher.test.ts", "removeMatchingFlags", "with no matches")]
public void WithNoMatches()
{
var a = new[] { "--foo=1", "--bar=baz" };
var result = ChromiumLauncher.RemoveMatchingFlags(a, "--baz");
Assert.AreEqual(new[] { "--foo=1", "--bar=baz" }, result);
}
}
}
118 changes: 79 additions & 39 deletions lib/PuppeteerSharp/ChromiumLauncher.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using PuppeteerSharp.BrowserData;
Expand Down Expand Up @@ -30,41 +29,6 @@ public ChromiumLauncher(string executable, LaunchOptions options)
Process.StartInfo.Arguments = string.Join(" ", chromiumArgs);
}

/// <summary>
/// The default flags that Chromium will be launched with.
/// </summary>
internal static string[] DefaultArgs { get; } =
{
"--allow-pre-commit-input",
"--disable-background-networking",
"--disable-background-timer-throttling",
"--disable-backgrounding-occluded-windows",
"--disable-breakpad",
"--disable-client-side-phishing-detection",
"--disable-component-extensions-with-background-pages",
"--disable-component-update",
"--disable-default-apps",
"--disable-dev-shm-usage",
"--disable-extensions",
"--disable-features=Translate,BackForwardCache,AcceptCHFrame,MediaRouter,OptimizationHints",
"--disable-hang-monitor",
"--disable-ipc-flooding-protection",
"--disable-popup-blocking",
"--disable-prompt-on-repost",
"--disable-renderer-backgrounding",
"--disable-search-engine-choice-screen",
"--disable-sync",
"--enable-automation",
"--enable-blink-features=IdleDetection",
"--enable-features=NetworkServiceInProcess2",
"--export-tagged-pdf",
"--force-color-profile=srgb",
"--metrics-recording-only",
"--no-first-run",
"--password-store=basic",
"--use-mock-keychain",
};

/// <inheritdoc />
public override Task<string> GetDefaultBuildIdAsync() => Task.FromResult(Chrome.DefaultBuildId);

Expand All @@ -73,7 +37,74 @@ public ChromiumLauncher(string executable, LaunchOptions options)

internal static string[] GetDefaultArgs(LaunchOptions options)
{
var chromiumArguments = new List<string>(DefaultArgs);
var userDisabledFeatures = GetFeatures("--disable-features", options.Args);
var args = options.Args;
if (args is not null && userDisabledFeatures.Length > 0)
{
args = RemoveMatchingFlags(options.Args, "--disable-features");
}

// Merge default disabled features with user-provided ones, if any.
var disabledFeatures = new List<string>
{
"Translate",
"AcceptCHFrame",
"MediaRouter",
"OptimizationHints",
"ProcessPerSiteUpToMainFrameThreshold",
};

disabledFeatures.AddRange(userDisabledFeatures);

var userEnabledFeatures = GetFeatures("--enable-features", options.Args);
if (args != null && userEnabledFeatures.Length > 0)
{
args = RemoveMatchingFlags(options.Args, "--enable-features");
}

// Merge default enabled features with user-provided ones, if any.
var enabledFeatures = new List<string>
{
"NetworkServiceInProcess2",
};

disabledFeatures.AddRange(userEnabledFeatures);

var chromiumArguments = new List<string>(
new string[]
{
"--allow-pre-commit-input",
"--disable-background-networking",
"--disable-background-timer-throttling",
"--disable-backgrounding-occluded-windows",
"--disable-breakpad",
"--disable-client-side-phishing-detection",
"--disable-component-extensions-with-background-pages",
"--disable-component-update",
"--disable-default-apps",
"--disable-dev-shm-usage",
"--disable-extensions",
"--disable-field-trial-config",
"--disable-hang-monitor",
"--disable-infobars",
"--disable-ipc-flooding-protection",
"--disable-popup-blocking",
"--disable-prompt-on-repost",
"--disable-renderer-backgrounding",
"--disable-search-engine-choice-screen",
"--disable-sync",
"--enable-automation",
"--enable-blink-features=IdleDetection",
"--export-tagged-pdf",
"--force-color-profile=srgb",
"--metrics-recording-only",
"--no-first-run",
"--password-store=basic",
"--use-mock-keychain",
});

chromiumArguments.Add($"--disable-features={string.Join(",", disabledFeatures)}");
chromiumArguments.Add($"--enable-features={string.Join(",", enabledFeatures)}");

if (!string.IsNullOrEmpty(options.UserDataDir))
{
Expand All @@ -95,15 +126,24 @@ internal static string[] GetDefaultArgs(LaunchOptions options)
});
}

if (options.Args.All(arg => arg.StartsWith("-", StringComparison.Ordinal)))
if (args.All(arg => arg.StartsWith("-", StringComparison.Ordinal)))
{
chromiumArguments.Add("about:blank");
}

chromiumArguments.AddRange(options.Args);
chromiumArguments.AddRange(args);
return chromiumArguments.ToArray();
}

internal static string[] GetFeatures(string flag, string[] options)
=> options
.Where(s => s.StartsWith($"{flag}=", StringComparison.InvariantCultureIgnoreCase))
.Select(s => s.Substring(flag.Length + 1))
.Where(s => !string.IsNullOrEmpty(s)).ToArray();

internal static string[] RemoveMatchingFlags(string[] array, string flag)
=> array.Where(arg => !arg.StartsWith(flag, StringComparison.InvariantCultureIgnoreCase)).ToArray();

private static (List<string> ChromiumArgs, TempDirectory TempUserDataDirectory) PrepareChromiumArgs(LaunchOptions options)
{
var chromiumArgs = new List<string>();
Expand Down
4 changes: 2 additions & 2 deletions lib/PuppeteerSharp/LaunchOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ public bool Devtools
public bool LogProcess { get; set; }

/// <summary>
/// If <c>true</c>, then do not use <see cref="ChromiumLauncher.DefaultArgs"/>.
/// If <c>true</c>, then do not use <see cref="ChromiumLauncher.GetDefaultArgs"/>.
/// Dangerous option; use with care. Defaults to <c>false</c>.
/// </summary>
public bool IgnoreDefaultArgs { get; set; }

/// <summary>
/// if <see cref="IgnoreDefaultArgs"/> is set to <c>false</c> this list will be used to filter <see cref="ChromiumLauncher.DefaultArgs"/>.
/// if <see cref="IgnoreDefaultArgs"/> is set to <c>false</c> this list will be used to filter <see cref="ChromiumLauncher.GetDefaultArgs"/>.
/// </summary>
public string[] IgnoredDefaultArgs
{
Expand Down

0 comments on commit bc76624

Please sign in to comment.