diff --git a/CefSharp.Core.Runtime/Internals/CefSharpApp.h b/CefSharp.Core.Runtime/Internals/CefSharpApp.h index f4cfffc7f5..e763c39835 100644 --- a/CefSharp.Core.Runtime/Internals/CefSharpApp.h +++ b/CefSharp.Core.Runtime/Internals/CefSharpApp.h @@ -209,10 +209,30 @@ namespace CefSharp if (kvp->Key == "disable-features" || kvp->Key == "enable-features") { - //Temp workaround so we can set the disable-features/enable-features command line argument - // See https://github.com/cefsharp/CefSharp/issues/2408 - commandLine->AppendSwitchWithValue(name, value); + if (CefSharpSettings::MergeFeaturesCommandLineArgs) + { + CefString existingValue = commandLine->GetSwitchValue(name); + if (existingValue.empty()) + { + commandLine->AppendSwitchWithValue(name, value); + } + else + { + String^ merged = CommandLineArgsMerger::MergeFeatures(StringUtils::ToClr(existingValue), kvp->Value); + + commandLine->RemoveSwitch(name); + commandLine->AppendSwitchWithValue(name, StringUtils::ToNative(merged)); + } + } + else + { + //Temp workaround so we can set the disable-features/enable-features command line argument + // See https://github.com/cefsharp/CefSharp/issues/2408 + commandLine->RemoveSwitch(name); + commandLine->AppendSwitchWithValue(name, value); + } } + // Right now the command line args handed to the application (global command line) have higher // precedence than command line args provided by the app else if (!commandLine->HasSwitch(name)) diff --git a/CefSharp.Test/Framework/CommandLineArgsMergerTests.cs b/CefSharp.Test/Framework/CommandLineArgsMergerTests.cs new file mode 100644 index 0000000000..39e2a6f5b5 --- /dev/null +++ b/CefSharp.Test/Framework/CommandLineArgsMergerTests.cs @@ -0,0 +1,63 @@ +// Copyright © 2022 The CefSharp Authors. All rights reserved. +// +// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. + +using CefSharp.Internals; +using Xunit; +using Xunit.Abstractions; + +namespace CefSharp.Test.Framework +{ + /// + /// CommandLineArgsMergerTests - Test the merging of command line features + /// + public class CommandLineArgsMergerTests + { + private readonly ITestOutputHelper output; + + public CommandLineArgsMergerTests(ITestOutputHelper output) + { + this.output = output; + } + + [Fact] + public void ShouldMergeFeatures() + { + var expected = "A,B,C"; + + var actual = CommandLineArgsMerger.MergeFeatures("A,B", "C"); + + Assert.Equal(expected, actual); + } + + [Fact] + public void ShouldAvoidDuplicates() + { + var expected = "A,B,C"; + + var actual = CommandLineArgsMerger.MergeFeatures("A,B", "B,C"); + + Assert.Equal(expected, actual); + } + + [Fact] + public void ShouldTrimWhitespace() + { + var expected = "A,B,C"; + + var actual = CommandLineArgsMerger.MergeFeatures("A, B", " C "); + + Assert.Equal(expected, actual); + } + + [Fact] + public void ShouldHandleNull() + { + var expected = "A,B"; + + var actual = CommandLineArgsMerger.MergeFeatures("A,B", null); + + Assert.Equal(expected, actual); + } + } +} diff --git a/CefSharp/CefSharpSettings.cs b/CefSharp/CefSharpSettings.cs index d5ca8754c6..7a9f585115 100644 --- a/CefSharp/CefSharpSettings.cs +++ b/CefSharp/CefSharpSettings.cs @@ -21,6 +21,7 @@ static CefSharpSettings() WcfTimeout = TimeSpan.FromSeconds(2); #endif SubprocessExitIfParentProcessClosed = true; + MergeFeaturesCommandLineArgs = true; } #if !NETCOREAPP @@ -82,6 +83,12 @@ static CefSharpSettings() /// public static bool FocusedNodeChangedEnabled { get; set; } + /// + /// Any enable-features/disable-features command line arguments will be automatically merged with existing values if supplied. + /// This currently defaults to true. + /// + public static bool MergeFeaturesCommandLineArgs { get; set; } + /// /// CefSharp.WinForms and CefSharp.Wpf.HwndHost ONLY! /// The default is to create diff --git a/CefSharp/Internals/CommandLineArgsMerger.cs b/CefSharp/Internals/CommandLineArgsMerger.cs new file mode 100644 index 0000000000..c826ee420c --- /dev/null +++ b/CefSharp/Internals/CommandLineArgsMerger.cs @@ -0,0 +1,52 @@ +// Copyright © 2015 The CefSharp Authors. All rights reserved. +// +// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. + +using System; +using System.Collections.Generic; + +namespace CefSharp.Internals +{ + /// + /// Use this static class to merge command line arguments. + /// + public sealed class CommandLineArgsMerger + { + /// + /// Returns the merged command line features. + /// + /// The existing features. + /// The features that should be merged. + /// the merged command line features. + public static string MergeFeatures(string existing, string incoming) + { + var features = new List(); + + if (!string.IsNullOrWhiteSpace(existing)) + { + foreach (var item in existing.Split(',')) + { + var trimmed = item.Trim(); + if (!features.Contains(trimmed)) + { + features.Add(trimmed); + } + } + } + + if (!string.IsNullOrWhiteSpace(incoming)) + { + foreach (var item in incoming.Split(',')) + { + var trimmed = item.Trim(); + if (!features.Contains(trimmed)) + { + features.Add(trimmed); + } + } + } + + return string.Join(",", features); + } + } +}