Skip to content

Commit 8dd1a1c

Browse files
committed
Add OutOfProcessHost Settings
- Add new Settings object that maps to CefSettings object - Allow for passing of additional command line args to the browser process (BrowserProcess will read args from command line by default)
1 parent 4ad5247 commit 8dd1a1c

File tree

7 files changed

+105
-10
lines changed

7 files changed

+105
-10
lines changed

CefSharp.OutOfProcess.BrowserProcess/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ public static int Main(string[] args)
1818

1919
var parentProcessId = int.Parse(CommandLineArgsParser.GetArgumentValue(args, "--parentProcessId"));
2020
var cachePath = CommandLineArgsParser.GetArgumentValue(args, "--cachePath");
21+
var rootCachePath = CommandLineArgsParser.GetArgumentValue(args, "--rootCachePath");
2122

2223
var parentProcess = Process.GetProcessById(parentProcessId);
2324

2425
var settings = new CefSettings()
2526
{
2627
//By default CefSharp will use an in-memory cache, you need to specify a Cache Folder to persist data
2728
CachePath = cachePath,
29+
RootCachePath = rootCachePath,
2830
MultiThreadedMessageLoop = false
2931
};
3032

CefSharp.OutOfProcess.Core/OutOfProcessHost.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Concurrent;
77
using System.Diagnostics;
88
using System.IO;
9+
using System.Linq;
910
using System.Threading.Tasks;
1011

1112
namespace CefSharp.OutOfProcess
@@ -27,14 +28,14 @@ public class OutOfProcessHost : IOutOfProcessHostRpc, IDisposable
2728
private int _remoteuiThreadId;
2829
private int _browserIdentifier = 1;
2930
private string _outofProcessHostExePath;
30-
private string _cachePath;
31+
private Settings _settings;
3132
private ConcurrentDictionary<int, IChromiumWebBrowserInternal> _browsers = new ConcurrentDictionary<int, IChromiumWebBrowserInternal>();
3233
private TaskCompletionSource<OutOfProcessHost> _processInitialized = new TaskCompletionSource<OutOfProcessHost>(TaskCreationOptions.RunContinuationsAsynchronously);
3334

34-
private OutOfProcessHost(string outOfProcessHostExePath, string cachePath = null)
35+
private OutOfProcessHost(string outOfProcessHostExePath, Settings settings = null)
3536
{
3637
_outofProcessHostExePath = outOfProcessHostExePath;
37-
_cachePath = cachePath;
38+
_settings = settings;
3839
}
3940

4041
/// <summary>
@@ -108,7 +109,22 @@ private void Init()
108109
{
109110
var currentProcess = Process.GetCurrentProcess();
110111

111-
var args = $"--parentProcessId={currentProcess.Id} --cachePath={_cachePath}";
112+
var args = $"--parentProcessId={currentProcess.Id}";
113+
114+
if (_settings != null)
115+
{
116+
if (!string.IsNullOrEmpty(_settings.CachePath))
117+
{
118+
args += $" --cachePath={_settings.CachePath}";
119+
}
120+
121+
if (!string.IsNullOrEmpty(_settings.RootCachePath))
122+
{
123+
args += $" --rootCachePath={_settings.RootCachePath}";
124+
}
125+
126+
args = _settings.AdditionalCommandLineArgs.Aggregate(args, (current, next) => $"{current} {next}");
127+
}
112128

113129
_browserProcess = Process.Start(new ProcessStartInfo(_outofProcessHostExePath, args)
114130
{
@@ -234,7 +250,7 @@ public void Dispose()
234250
_jsonRpc = null;
235251
}
236252

237-
public static Task<OutOfProcessHost> CreateAsync(string path = HostExeName, string cachePath = null)
253+
public static Task<OutOfProcessHost> CreateAsync(string path = HostExeName, Settings settings = null)
238254
{
239255
if(string.IsNullOrEmpty(path))
240256
{
@@ -248,7 +264,7 @@ public static Task<OutOfProcessHost> CreateAsync(string path = HostExeName, stri
248264
throw new FileNotFoundException("Unable to find Host executable.", path);
249265
}
250266

251-
var host = new OutOfProcessHost(fullPath, cachePath);
267+
var host = new OutOfProcessHost(fullPath, settings);
252268

253269
host.Init();
254270

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System.Collections.Generic;
2+
3+
namespace CefSharp.OutOfProcess
4+
{
5+
/// <summary>
6+
/// Initialization settings. Many of these and other settings can also configured using command-line switches.
7+
/// </summary>
8+
public sealed class Settings
9+
{
10+
/// <summary>
11+
/// The location where data for the global browser cache will be stored on disk. In this value is non-empty then it must be
12+
/// an absolute path that is must be either equal to or a child directory of RootCachePath (if RootCachePath is
13+
/// empty it will default to this value). If the value is empty then browsers will be created in "incognito mode" where
14+
/// in-memory caches are used for storage and no data is persisted to disk. HTML5 databases such as localStorage will only
15+
/// persist across sessions if a cache path is specified. Can be overridden for individual RequestContext instances via the
16+
/// RequestContextSettings.CachePath value.
17+
/// </summary>
18+
public string CachePath { get; set; }
19+
20+
/// <summary>
21+
/// The root directory that all CefSettings.CachePath and RequestContextSettings.CachePath values must have in common. If this
22+
/// value is empty and CefSettings.CachePath is non-empty then it will default to the CefSettings.CachePath value.
23+
/// If this value is non-empty then it must be an absolute path. Failure to set this value correctly may result in the sandbox
24+
/// blocking read/write access to the CachePath directory. NOTE: CefSharp does not implement the CHROMIUM SANDBOX. A non-empty
25+
/// RootCachePath can be used in conjuncation with an empty CefSettings.CachePath in instances where you would like browsers
26+
/// attached to the Global RequestContext (the default) created in "incognito mode" and instances created with a custom
27+
/// RequestContext using a disk based cache.
28+
/// </summary>
29+
public string RootCachePath { get; set; }
30+
31+
/// <summary>
32+
/// Command line args passed to the BrowserProcess
33+
/// </summary>
34+
public ICollection<string> AdditionalCommandLineArgs { get; } = new List<string>();
35+
36+
/// <summary>
37+
/// Adds the command line arg to the <see cref="AdditionalCommandLineArgs"/> collection.
38+
/// </summary>
39+
/// <param name="arg">command line arg</param>
40+
/// <returns>The current instance</returns>
41+
public Settings AddCommandLineArg(string arg)
42+
{
43+
AdditionalCommandLineArgs.Add(arg);
44+
45+
return this;
46+
}
47+
48+
/// <summary>
49+
/// Creates a new <see cref="Settings"/> instance with the specified
50+
/// <paramref name="cachePath"/>.
51+
/// </summary>
52+
/// <param name="cachePath">sets <see cref="CachePath"/></param>
53+
/// <returns>new Settings instance.</returns>
54+
public static Settings WithCachePath(string cachePath)
55+
{
56+
return new Settings { CachePath = cachePath };
57+
}
58+
}
59+
}

CefSharp.OutOfProcess.WinForms.Example/BrowserForm.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ private async void BrowserFormLoad(object sender, EventArgs e)
5555
var outOfProcessHostPath = Path.GetFullPath($"..\\..\\..\\..\\..\\CefSharp.OutOfProcess.BrowserProcess\\bin\\{_buildType}");
5656
outOfProcessHostPath = Path.Combine(outOfProcessHostPath, OutOfProcessHost.HostExeName);
5757
var cachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CefSharp\\OutOfProcessCache");
58-
_outOfProcessHost = await OutOfProcessHost.CreateAsync(outOfProcessHostPath, cachePath);
58+
59+
var settings = Settings.WithCachePath(cachePath)
60+
.AddCommandLineArg("--lang=en-GB");
61+
62+
_outOfProcessHost = await OutOfProcessHost.CreateAsync(outOfProcessHostPath, settings);
5963

6064
AddTab(DefaultUrlForAddedTabs);
6165
}

CefSharp.OutOfProcess.Wpf.HwndHost.Example/MainWindow.xaml.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ private async void OnMainWindowLoaded(object sender, RoutedEventArgs e)
2929
var outOfProcessHostPath = Path.GetFullPath($"..\\..\\..\\..\\CefSharp.OutOfProcess.BrowserProcess\\bin\\{_buildType}");
3030
outOfProcessHostPath = Path.Combine(outOfProcessHostPath, OutOfProcessHost.HostExeName);
3131
var cachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CefSharp\\OutOfProcessCache");
32-
_outOfProcessHost = await OutOfProcessHost.CreateAsync(outOfProcessHostPath, cachePath);
32+
33+
var settings = Settings.WithCachePath(cachePath);
34+
35+
_outOfProcessHost = await OutOfProcessHost.CreateAsync(outOfProcessHostPath, settings);
3336

3437
BrowserContentPresenter.Content = new ChromiumWebBrowser(_outOfProcessHost, "https://google.com");
3538
}

CefSharp.OutOfProcess.sln

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.32510.428
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.5.33424.131
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CefSharp.OutOfProcess.WinForms.Example", "CefSharp.OutOfProcess.WinForms.Example\CefSharp.OutOfProcess.WinForms.Example.csproj", "{A1C85784-7C6F-4C83-9AC4-B3413F32FF31}"
77
EndProject
@@ -19,6 +19,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CefSharp.OutOfProcess.Wpf.H
1919
EndProject
2020
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CefSharp.Dom.OutOfProcess", "CefSharp.Dom\lib\PuppeteerSharp\CefSharp.Dom.OutOfProcess.csproj", "{7D39CC02-FB02-4F03-B329-E5DB567EC354}"
2121
EndProject
22+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{3E9517EE-C843-4480-A847-3B8473F6188E}"
23+
ProjectSection(SolutionItems) = preProject
24+
Directory.Build.props = Directory.Build.props
25+
EndProjectSection
26+
EndProject
2227
Global
2328
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2429
Debug|Any CPU = Debug|Any CPU

Directory.Build.props

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<Project>
2+
<PropertyGroup>
3+
<!--<Nullable>enable</Nullable>-->
4+
<LangVersion>latest</LangVersion>
5+
</PropertyGroup>
6+
</Project>

0 commit comments

Comments
 (0)