11using System ;
22using System . Collections . Generic ;
3- using System . Diagnostics ;
43using System . Linq ;
54using System . Threading . Tasks ;
65using PuppeteerSharp . BrowserData ;
@@ -30,41 +29,6 @@ public ChromiumLauncher(string executable, LaunchOptions options)
3029 Process . StartInfo . Arguments = string . Join ( " " , chromiumArgs ) ;
3130 }
3231
33- /// <summary>
34- /// The default flags that Chromium will be launched with.
35- /// </summary>
36- internal static string [ ] DefaultArgs { get ; } =
37- {
38- "--allow-pre-commit-input" ,
39- "--disable-background-networking" ,
40- "--disable-background-timer-throttling" ,
41- "--disable-backgrounding-occluded-windows" ,
42- "--disable-breakpad" ,
43- "--disable-client-side-phishing-detection" ,
44- "--disable-component-extensions-with-background-pages" ,
45- "--disable-component-update" ,
46- "--disable-default-apps" ,
47- "--disable-dev-shm-usage" ,
48- "--disable-extensions" ,
49- "--disable-features=Translate,BackForwardCache,AcceptCHFrame,MediaRouter,OptimizationHints" ,
50- "--disable-hang-monitor" ,
51- "--disable-ipc-flooding-protection" ,
52- "--disable-popup-blocking" ,
53- "--disable-prompt-on-repost" ,
54- "--disable-renderer-backgrounding" ,
55- "--disable-search-engine-choice-screen" ,
56- "--disable-sync" ,
57- "--enable-automation" ,
58- "--enable-blink-features=IdleDetection" ,
59- "--enable-features=NetworkServiceInProcess2" ,
60- "--export-tagged-pdf" ,
61- "--force-color-profile=srgb" ,
62- "--metrics-recording-only" ,
63- "--no-first-run" ,
64- "--password-store=basic" ,
65- "--use-mock-keychain" ,
66- } ;
67-
6832 /// <inheritdoc />
6933 public override Task < string > GetDefaultBuildIdAsync ( ) => Task . FromResult ( Chrome . DefaultBuildId ) ;
7034
@@ -73,7 +37,74 @@ public ChromiumLauncher(string executable, LaunchOptions options)
7337
7438 internal static string [ ] GetDefaultArgs ( LaunchOptions options )
7539 {
76- var chromiumArguments = new List < string > ( DefaultArgs ) ;
40+ var userDisabledFeatures = GetFeatures ( "--disable-features" , options . Args ) ;
41+ var args = options . Args ;
42+ if ( args is not null && userDisabledFeatures . Length > 0 )
43+ {
44+ args = RemoveMatchingFlags ( options . Args , "--disable-features" ) ;
45+ }
46+
47+ // Merge default disabled features with user-provided ones, if any.
48+ var disabledFeatures = new List < string >
49+ {
50+ "Translate" ,
51+ "AcceptCHFrame" ,
52+ "MediaRouter" ,
53+ "OptimizationHints" ,
54+ "ProcessPerSiteUpToMainFrameThreshold" ,
55+ } ;
56+
57+ disabledFeatures . AddRange ( userDisabledFeatures ) ;
58+
59+ var userEnabledFeatures = GetFeatures ( "--enable-features" , options . Args ) ;
60+ if ( args != null && userEnabledFeatures . Length > 0 )
61+ {
62+ args = RemoveMatchingFlags ( options . Args , "--enable-features" ) ;
63+ }
64+
65+ // Merge default enabled features with user-provided ones, if any.
66+ var enabledFeatures = new List < string >
67+ {
68+ "NetworkServiceInProcess2" ,
69+ } ;
70+
71+ disabledFeatures . AddRange ( userEnabledFeatures ) ;
72+
73+ var chromiumArguments = new List < string > (
74+ new string [ ]
75+ {
76+ "--allow-pre-commit-input" ,
77+ "--disable-background-networking" ,
78+ "--disable-background-timer-throttling" ,
79+ "--disable-backgrounding-occluded-windows" ,
80+ "--disable-breakpad" ,
81+ "--disable-client-side-phishing-detection" ,
82+ "--disable-component-extensions-with-background-pages" ,
83+ "--disable-component-update" ,
84+ "--disable-default-apps" ,
85+ "--disable-dev-shm-usage" ,
86+ "--disable-extensions" ,
87+ "--disable-field-trial-config" ,
88+ "--disable-hang-monitor" ,
89+ "--disable-infobars" ,
90+ "--disable-ipc-flooding-protection" ,
91+ "--disable-popup-blocking" ,
92+ "--disable-prompt-on-repost" ,
93+ "--disable-renderer-backgrounding" ,
94+ "--disable-search-engine-choice-screen" ,
95+ "--disable-sync" ,
96+ "--enable-automation" ,
97+ "--enable-blink-features=IdleDetection" ,
98+ "--export-tagged-pdf" ,
99+ "--force-color-profile=srgb" ,
100+ "--metrics-recording-only" ,
101+ "--no-first-run" ,
102+ "--password-store=basic" ,
103+ "--use-mock-keychain" ,
104+ } ) ;
105+
106+ chromiumArguments . Add ( $ "--disable-features={ string . Join ( "," , disabledFeatures ) } ") ;
107+ chromiumArguments . Add ( $ "--enable-features={ string . Join ( "," , enabledFeatures ) } ") ;
77108
78109 if ( ! string . IsNullOrEmpty ( options . UserDataDir ) )
79110 {
@@ -95,15 +126,24 @@ internal static string[] GetDefaultArgs(LaunchOptions options)
95126 } ) ;
96127 }
97128
98- if ( options . Args . All ( arg => arg . StartsWith ( "-" , StringComparison . Ordinal ) ) )
129+ if ( args . All ( arg => arg . StartsWith ( "-" , StringComparison . Ordinal ) ) )
99130 {
100131 chromiumArguments . Add ( "about:blank" ) ;
101132 }
102133
103- chromiumArguments . AddRange ( options . Args ) ;
134+ chromiumArguments . AddRange ( args ) ;
104135 return chromiumArguments . ToArray ( ) ;
105136 }
106137
138+ internal static string [ ] GetFeatures ( string flag , string [ ] options )
139+ => options
140+ . Where ( s => s . StartsWith ( $ "{ flag } =", StringComparison . InvariantCultureIgnoreCase ) )
141+ . Select ( s => s . Substring ( flag . Length + 1 ) )
142+ . Where ( s => ! string . IsNullOrEmpty ( s ) ) . ToArray ( ) ;
143+
144+ internal static string [ ] RemoveMatchingFlags ( string [ ] array , string flag )
145+ => array . Where ( arg => ! arg . StartsWith ( flag , StringComparison . InvariantCultureIgnoreCase ) ) . ToArray ( ) ;
146+
107147 private static ( List < string > ChromiumArgs , TempDirectory TempUserDataDirectory ) PrepareChromiumArgs ( LaunchOptions options )
108148 {
109149 var chromiumArgs = new List < string > ( ) ;
0 commit comments