Skip to content

Commit b5fef3b

Browse files
authored
Core - Automatically merge enable-features/disable-features command-line arguments with existing values (#5245)
* Core - Automatically merge `enable-features/disable-features` command-line arguments with existing values
1 parent fd87988 commit b5fef3b

4 files changed

Lines changed: 138 additions & 3 deletions

File tree

CefSharp.Core.Runtime/Internals/CefSharpApp.h

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,30 @@ namespace CefSharp
209209

210210
if (kvp->Key == "disable-features" || kvp->Key == "enable-features")
211211
{
212-
//Temp workaround so we can set the disable-features/enable-features command line argument
213-
// See https://github.com/cefsharp/CefSharp/issues/2408
214-
commandLine->AppendSwitchWithValue(name, value);
212+
if (CefSharpSettings::MergeFeaturesCommandLineArgs)
213+
{
214+
CefString existingValue = commandLine->GetSwitchValue(name);
215+
if (existingValue.empty())
216+
{
217+
commandLine->AppendSwitchWithValue(name, value);
218+
}
219+
else
220+
{
221+
String^ merged = CommandLineArgsMerger::MergeFeatures(StringUtils::ToClr(existingValue), kvp->Value);
222+
223+
commandLine->RemoveSwitch(name);
224+
commandLine->AppendSwitchWithValue(name, StringUtils::ToNative(merged));
225+
}
226+
}
227+
else
228+
{
229+
//Temp workaround so we can set the disable-features/enable-features command line argument
230+
// See https://github.com/cefsharp/CefSharp/issues/2408
231+
commandLine->RemoveSwitch(name);
232+
commandLine->AppendSwitchWithValue(name, value);
233+
}
215234
}
235+
216236
// Right now the command line args handed to the application (global command line) have higher
217237
// precedence than command line args provided by the app
218238
else if (!commandLine->HasSwitch(name))
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright © 2022 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
using CefSharp.Internals;
6+
using Xunit;
7+
using Xunit.Abstractions;
8+
9+
namespace CefSharp.Test.Framework
10+
{
11+
/// <summary>
12+
/// CommandLineArgsMergerTests - Test the merging of command line features
13+
/// </summary>
14+
public class CommandLineArgsMergerTests
15+
{
16+
private readonly ITestOutputHelper output;
17+
18+
public CommandLineArgsMergerTests(ITestOutputHelper output)
19+
{
20+
this.output = output;
21+
}
22+
23+
[Fact]
24+
public void ShouldMergeFeatures()
25+
{
26+
var expected = "A,B,C";
27+
28+
var actual = CommandLineArgsMerger.MergeFeatures("A,B", "C");
29+
30+
Assert.Equal(expected, actual);
31+
}
32+
33+
[Fact]
34+
public void ShouldAvoidDuplicates()
35+
{
36+
var expected = "A,B,C";
37+
38+
var actual = CommandLineArgsMerger.MergeFeatures("A,B", "B,C");
39+
40+
Assert.Equal(expected, actual);
41+
}
42+
43+
[Fact]
44+
public void ShouldTrimWhitespace()
45+
{
46+
var expected = "A,B,C";
47+
48+
var actual = CommandLineArgsMerger.MergeFeatures("A, B", " C ");
49+
50+
Assert.Equal(expected, actual);
51+
}
52+
53+
[Fact]
54+
public void ShouldHandleNull()
55+
{
56+
var expected = "A,B";
57+
58+
var actual = CommandLineArgsMerger.MergeFeatures("A,B", null);
59+
60+
Assert.Equal(expected, actual);
61+
}
62+
}
63+
}

CefSharp/CefSharpSettings.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ static CefSharpSettings()
2121
WcfTimeout = TimeSpan.FromSeconds(2);
2222
#endif
2323
SubprocessExitIfParentProcessClosed = true;
24+
MergeFeaturesCommandLineArgs = true;
2425
}
2526

2627
#if !NETCOREAPP
@@ -82,6 +83,12 @@ static CefSharpSettings()
8283
/// </summary>
8384
public static bool FocusedNodeChangedEnabled { get; set; }
8485

86+
/// <summary>
87+
/// Any enable-features/disable-features command line arguments will be automatically merged with existing values if supplied.
88+
/// This currently defaults to true.
89+
/// </summary>
90+
public static bool MergeFeaturesCommandLineArgs { get; set; }
91+
8592
/// <summary>
8693
/// CefSharp.WinForms and CefSharp.Wpf.HwndHost ONLY!
8794
/// The default is to create <see cref="CefRuntimeStyle.Alloy"/>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright © 2015 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
9+
namespace CefSharp.Internals
10+
{
11+
/// <summary>
12+
/// Use this static class to merge command line arguments.
13+
/// </summary>
14+
public sealed class CommandLineArgsMerger
15+
{
16+
/// <summary>
17+
/// Returns the merged command line features.
18+
/// </summary>
19+
/// <param name="existing">The existing features.</param>
20+
/// <param name="incoming">The features that should be merged.</param>
21+
/// <returns> the merged command line features. </returns>
22+
public static string MergeFeatures(string existing, string incoming)
23+
{
24+
var features = new HashSet<string>();
25+
26+
AddFeatures(features, existing);
27+
AddFeatures(features, incoming);
28+
29+
return string.Join(",", features.OrderBy(i => i));
30+
}
31+
32+
private static void AddFeatures(HashSet<string> features, string value)
33+
{
34+
if (string.IsNullOrWhiteSpace(value))
35+
{
36+
return;
37+
}
38+
39+
foreach (var item in value.Split(','))
40+
{
41+
features.Add(item.Trim());
42+
}
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)