Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions CefSharp.Core.Runtime/Internals/CefSharpApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
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))
Expand Down
63 changes: 63 additions & 0 deletions CefSharp.Test/Framework/CommandLineArgsMergerTests.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// CommandLineArgsMergerTests - Test the merging of command line features
/// </summary>
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);
}
}
}
7 changes: 7 additions & 0 deletions CefSharp/CefSharpSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ static CefSharpSettings()
WcfTimeout = TimeSpan.FromSeconds(2);
#endif
SubprocessExitIfParentProcessClosed = true;
MergeFeaturesCommandLineArgs = true;
}

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

/// <summary>
/// Any enable-features/disable-features command line arguments will be automatically merged with existing values if supplied.
/// This currently defaults to true.
/// </summary>
public static bool MergeFeaturesCommandLineArgs { get; set; }

/// <summary>
/// CefSharp.WinForms and CefSharp.Wpf.HwndHost ONLY!
/// The default is to create <see cref="CefRuntimeStyle.Alloy"/>
Expand Down
45 changes: 45 additions & 0 deletions CefSharp/Internals/CommandLineArgsMerger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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;
using System.Linq;

namespace CefSharp.Internals
{
/// <summary>
/// Use this static class to merge command line arguments.
/// </summary>
public sealed class CommandLineArgsMerger
{
/// <summary>
/// Returns the merged command line features.
/// </summary>
/// <param name="existing">The existing features.</param>
/// <param name="incoming">The features that should be merged.</param>
/// <returns> the merged command line features. </returns>
public static string MergeFeatures(string existing, string incoming)
{
var features = new HashSet<string>();

AddFeatures(features, existing);
AddFeatures(features, incoming);

return string.Join(",", features.OrderBy(i => i));
}

private static void AddFeatures(HashSet<string> features, string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return;
}

foreach (var item in value.Split(','))
{
features.Add(item.Trim());
}
}
}
}