From cc6bbb793acd9beea61822f633dccbe2c972dc9e Mon Sep 17 00:00:00 2001 From: Sebastian Mayer Date: Thu, 26 Jan 2023 18:00:24 +0100 Subject: [PATCH 1/4] browsers can set cef commandlineargs --- .../Program.cs | 31 +++++++++++++++++-- .../OutOfProcessHost.cs | 18 ++++++----- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/CefSharp.OutOfProcess.BrowserProcess/Program.cs b/CefSharp.OutOfProcess.BrowserProcess/Program.cs index 39f6b0b..c2b4366 100644 --- a/CefSharp.OutOfProcess.BrowserProcess/Program.cs +++ b/CefSharp.OutOfProcess.BrowserProcess/Program.cs @@ -1,5 +1,7 @@ using System; +using System.Collections.Generic; using System.Diagnostics; +using System.Linq; using System.Threading.Tasks; using CefSharp.Internals; @@ -14,20 +16,27 @@ public static int Main(string[] args) { Cef.EnableHighDPISupport(); - //Debugger.Launch(); + Debugger.Launch(); var parentProcessId = int.Parse(CommandLineArgsParser.GetArgumentValue(args, "--parentProcessId")); - var cachePath = CommandLineArgsParser.GetArgumentValue(args, "--cachePath"); var parentProcess = Process.GetProcessById(parentProcessId); + var settings = new CefSettings() { //By default CefSharp will use an in-memory cache, you need to specify a Cache Folder to persist data - CachePath = cachePath, MultiThreadedMessageLoop = false }; + foreach (var arg in args) + { + List splitted = arg.Split('=').ToList(); + var key = splitted.First().Substring(2); + var value = splitted.Count == 1 ? string.Empty : splitted.Last(); + AddArg(settings, key, value); + } + var browserProcessHandler = new BrowserProcessHandler(parentProcessId); Cef.EnableWaitForBrowsersToClose(); @@ -66,5 +75,21 @@ public static int Main(string[] args) return 0; } + + private static void AddArg(CefSettingsBase settings, string key, string value) + { + switch (key) + { + case "accept-lang": + settings.AcceptLanguageList = value; + break; + case "cachePath": + settings.CachePath = value; + break; + default: + settings.CefCommandLineArgs.Add(key, value); + break; + } + } } } diff --git a/CefSharp.OutOfProcess.Core/OutOfProcessHost.cs b/CefSharp.OutOfProcess.Core/OutOfProcessHost.cs index 1fec5c4..f3991b3 100644 --- a/CefSharp.OutOfProcess.Core/OutOfProcessHost.cs +++ b/CefSharp.OutOfProcess.Core/OutOfProcessHost.cs @@ -1,11 +1,13 @@ -using CefSharp.OutOfProcess.Interface; +using CefSharp.OutOfProcess.Interface; using CefSharp.OutOfProcess.Internal; using PInvoke; using StreamJsonRpc; using System; using System.Collections.Concurrent; +using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.Linq; using System.Threading.Tasks; namespace CefSharp.OutOfProcess @@ -28,13 +30,15 @@ public class OutOfProcessHost : IOutOfProcessHostRpc, IDisposable private int _browserIdentifier = 1; private string _outofProcessHostExePath; private string _cachePath; + private readonly IEnumerable _cefCommandLineArgs; private ConcurrentDictionary _browsers = new ConcurrentDictionary(); private TaskCompletionSource _processInitialized = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - private OutOfProcessHost(string outOfProcessHostExePath, string cachePath = null) + private OutOfProcessHost(string outOfProcessHostExePath, string cachePath = null, IEnumerable userargs = null) { _outofProcessHostExePath = outOfProcessHostExePath; _cachePath = cachePath; + _cefCommandLineArgs = userargs; } /// @@ -108,7 +112,7 @@ private void Init() { var currentProcess = Process.GetCurrentProcess(); - var args = $"--parentProcessId={currentProcess.Id} --cachePath={_cachePath}"; + var args = $"--parentProcessId={currentProcess.Id} --cachePath={_cachePath} {_cefCommandLineArgs?.Aggregate((a, p) => $"{a} {p}")}"; _browserProcess = Process.Start(new ProcessStartInfo(_outofProcessHostExePath, args) { @@ -234,7 +238,7 @@ public void Dispose() _jsonRpc = null; } - public static Task CreateAsync(string path = HostExeName, string cachePath = null) + public static Task CreateAsync(string path = HostExeName, string cachePath = null, IEnumerable cefCommandLineArgs = null) { if(string.IsNullOrEmpty(path)) { @@ -248,11 +252,11 @@ public static Task CreateAsync(string path = HostExeName, stri throw new FileNotFoundException("Unable to find Host executable.", path); } - var host = new OutOfProcessHost(fullPath, cachePath); + var host = new OutOfProcessHost(fullPath, cachePath, cefCommandLineArgs); - host.Init(); + host.Init(); return host.InitializedTask; - } + } } } From 3ecc8cc3c2604a1cdfe3ea2039d2dc2e47ad237c Mon Sep 17 00:00:00 2001 From: Sebastian Mayer Date: Thu, 26 Jan 2023 23:01:46 +0100 Subject: [PATCH 2/4] extract method --- .../Program.cs | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/CefSharp.OutOfProcess.BrowserProcess/Program.cs b/CefSharp.OutOfProcess.BrowserProcess/Program.cs index c2b4366..4671775 100644 --- a/CefSharp.OutOfProcess.BrowserProcess/Program.cs +++ b/CefSharp.OutOfProcess.BrowserProcess/Program.cs @@ -29,13 +29,7 @@ public static int Main(string[] args) MultiThreadedMessageLoop = false }; - foreach (var arg in args) - { - List splitted = arg.Split('=').ToList(); - var key = splitted.First().Substring(2); - var value = splitted.Count == 1 ? string.Empty : splitted.Last(); - AddArg(settings, key, value); - } + AddArgs(settings, args); var browserProcessHandler = new BrowserProcessHandler(parentProcessId); @@ -76,7 +70,18 @@ public static int Main(string[] args) return 0; } - private static void AddArg(CefSettingsBase settings, string key, string value) + private static void AddArgs(CefSettings settings, IEnumerable args) + { + foreach (var arg in args) + { + List splitted = arg.Split('=').ToList(); + var key = splitted.First().Substring(2); + var value = splitted.Count == 1 ? string.Empty : splitted.Last(); + AddArg(settings, key, value); + } + } + + private static void AddArg(CefSettings settings, string key, string value) { switch (key) { From b1f4506e4c0c47577bf0a60fdad6519f8f72ffcf Mon Sep 17 00:00:00 2001 From: Sebastian Mayer Date: Mon, 30 Jan 2023 15:24:33 +0100 Subject: [PATCH 3/4] revert style changes --- CefSharp.OutOfProcess.BrowserProcess/Program.cs | 3 +-- CefSharp.OutOfProcess.Core/OutOfProcessHost.cs | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/CefSharp.OutOfProcess.BrowserProcess/Program.cs b/CefSharp.OutOfProcess.BrowserProcess/Program.cs index 4671775..31be59d 100644 --- a/CefSharp.OutOfProcess.BrowserProcess/Program.cs +++ b/CefSharp.OutOfProcess.BrowserProcess/Program.cs @@ -16,13 +16,12 @@ public static int Main(string[] args) { Cef.EnableHighDPISupport(); - Debugger.Launch(); + //Debugger.Launch(); var parentProcessId = int.Parse(CommandLineArgsParser.GetArgumentValue(args, "--parentProcessId")); var parentProcess = Process.GetProcessById(parentProcessId); - var settings = new CefSettings() { //By default CefSharp will use an in-memory cache, you need to specify a Cache Folder to persist data diff --git a/CefSharp.OutOfProcess.Core/OutOfProcessHost.cs b/CefSharp.OutOfProcess.Core/OutOfProcessHost.cs index f3991b3..29d71d7 100644 --- a/CefSharp.OutOfProcess.Core/OutOfProcessHost.cs +++ b/CefSharp.OutOfProcess.Core/OutOfProcessHost.cs @@ -254,9 +254,9 @@ public static Task CreateAsync(string path = HostExeName, stri var host = new OutOfProcessHost(fullPath, cachePath, cefCommandLineArgs); - host.Init(); + host.Init(); return host.InitializedTask; - } + } } } From 2d5aba08b1249d09ab43ef5f081fe45f3bd4f461 Mon Sep 17 00:00:00 2001 From: Sebastian Mayer Date: Mon, 30 Jan 2023 15:56:10 +0100 Subject: [PATCH 4/4] rename variable --- CefSharp.OutOfProcess.Core/OutOfProcessHost.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CefSharp.OutOfProcess.Core/OutOfProcessHost.cs b/CefSharp.OutOfProcess.Core/OutOfProcessHost.cs index 29d71d7..9749d3a 100644 --- a/CefSharp.OutOfProcess.Core/OutOfProcessHost.cs +++ b/CefSharp.OutOfProcess.Core/OutOfProcessHost.cs @@ -34,11 +34,11 @@ public class OutOfProcessHost : IOutOfProcessHostRpc, IDisposable private ConcurrentDictionary _browsers = new ConcurrentDictionary(); private TaskCompletionSource _processInitialized = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - private OutOfProcessHost(string outOfProcessHostExePath, string cachePath = null, IEnumerable userargs = null) + private OutOfProcessHost(string outOfProcessHostExePath, string cachePath = null, IEnumerable cefCommandLineArgs = null) { _outofProcessHostExePath = outOfProcessHostExePath; _cachePath = cachePath; - _cefCommandLineArgs = userargs; + _cefCommandLineArgs = cefCommandLineArgs; } ///