Skip to content

Commit a8ff890

Browse files
committed
Always pass through Process Id to subprocess
Improve command line parsing Partial import of changes from #2375
1 parent 801d53b commit a8ff890

File tree

6 files changed

+23
-28
lines changed

6 files changed

+23
-28
lines changed

CefSharp.BrowserSubprocess.Core/WcfEnabledSubProcess.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,20 @@ namespace CefSharp
1414
{
1515
void WcfEnabledSubProcess::OnBrowserCreated(CefBrowserWrapper^ browser)
1616
{
17-
if (!parentBrowserId.HasValue)
17+
if (!_parentBrowserId.HasValue)
1818
{
19-
parentBrowserId = browser->BrowserId;
19+
_parentBrowserId = browser->BrowserId;
2020
}
2121

22-
if (!parentProcessId.HasValue || !parentBrowserId.HasValue)
22+
if (!_parentBrowserId.HasValue)
2323
{
2424
return;
2525
}
2626

27-
auto browserId = browser->IsPopup ? parentBrowserId.Value : browser->BrowserId;
27+
//TODO: This can likely be simplified as both values are likely equal
28+
auto browserId = browser->IsPopup ? _parentBrowserId.Value : browser->BrowserId;
2829

29-
auto serviceName = RenderprocessClientFactory::GetServiceName(parentProcessId.Value, browserId);
30+
auto serviceName = RenderprocessClientFactory::GetServiceName(_parentProcessId, browserId);
3031

3132
auto binding = BrowserProcessServiceHost::CreateBinding();
3233

CefSharp.BrowserSubprocess.Core/WcfEnabledSubProcess.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,21 @@ namespace CefSharp
1717
public ref class WcfEnabledSubProcess : SubProcess
1818
{
1919
private:
20-
Nullable<int> parentBrowserId;
20+
Nullable<int> _parentBrowserId;
2121

2222
/// <summary>
2323
/// The PID for the parent (browser) process
2424
/// </summary>
25-
Nullable<int> parentProcessId;
25+
int _parentProcessId;
2626

2727
public:
28-
WcfEnabledSubProcess(IEnumerable<String^>^ args) : SubProcess(args)
28+
WcfEnabledSubProcess(int parentProcessId, IEnumerable<String^>^ args) : SubProcess(args)
2929
{
30-
parentProcessId = CommandLineArgsParser::LocateParentProcessId(args);
30+
_parentProcessId = parentProcessId;
3131
}
3232

3333
void OnBrowserCreated(CefBrowserWrapper^ browser) override;
3434
void OnBrowserDestroyed(CefBrowserWrapper^ browser) override;
35-
3635
};
3736
}
3837
}

CefSharp.BrowserSubprocess/Program.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
44

55
using System;
6-
using System.Collections.Generic;
76
using System.Diagnostics;
8-
using System.Linq;
97
using CefSharp.Internals;
108

119
namespace CefSharp.BrowserSubprocess
@@ -19,16 +17,14 @@ public static int Main(string[] args)
1917
SubProcess.EnableHighDPISupport();
2018

2119
int result;
22-
23-
const string typePrefix = "--type=";
24-
var typeArgument = args.SingleOrDefault(arg => arg.StartsWith(typePrefix));
25-
var type = typeArgument.Substring(typePrefix.Length);
20+
var type = args.GetArgumentValue(CefSharpArguments.SubProcessTypeArgument);
21+
var parentProcessId = int.Parse(args.GetArgumentValue(CefSharpArguments.HostProcessIdArgument));
2622

2723
//Use our custom subProcess provides features like EvaluateJavascript
2824
if (type == "renderer")
2925
{
3026
var wcfEnabled = args.HasArgument(CefSharpArguments.WcfEnabledArgument);
31-
var subProcess = wcfEnabled ? new WcfEnabledSubProcess(args) : new SubProcess(args);
27+
var subProcess = wcfEnabled ? new WcfEnabledSubProcess(parentProcessId, args) : new SubProcess(args);
3228

3329
using (subProcess)
3430
{

CefSharp.Core/Internals/CefSharpApp.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@ namespace CefSharp
5858
if (CefSharpSettings::WcfEnabled)
5959
{
6060
commandLine->AppendArgument(StringUtils::ToNative(CefSharpArguments::WcfEnabledArgument));
61-
//ChannelId was removed in https://bitbucket.org/chromiumembedded/cef/issues/1912/notreached-in-logchannelidandcookiestores
62-
//We need to know the process Id to establish WCF communication
63-
commandLine->AppendArgument(StringUtils::ToNative(CefSharpArguments::WcfHostProcessIdArgument + "=" + Process::GetCurrentProcess()->Id));
6461
}
6562

63+
//ChannelId was removed in https://bitbucket.org/chromiumembedded/cef/issues/1912/notreached-in-logchannelidandcookiestores
64+
//We need to know the process Id to establish WCF communication and for monitoring of parent process exit
65+
commandLine->AppendArgument(StringUtils::ToNative(CefSharpArguments::HostProcessIdArgument + "=" + Process::GetCurrentProcess()->Id));
66+
6667
if (_cefSettings->_cefCustomSchemes->Count > 0)
6768
{
6869
String^ argument = "=";

CefSharp/Internals/CefSharpArguments.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ namespace CefSharp.Internals
77
public static class CefSharpArguments
88
{
99
public const string WcfEnabledArgument = "--wcf-enabled";
10-
public const string WcfHostProcessIdArgument = "--wcf-host-process-id";
10+
public const string HostProcessIdArgument = "--host-process-id";
1111
public const string CustomSchemeArgument = "--custom-scheme";
1212
public const string FocusedNodeChangedEnabledArgument = "--focused-node-enabled";
13+
public const string SubProcessTypeArgument = "--type";
1314
}
1415
}

CefSharp/Internals/CommandLineArgsParser.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,15 @@ public static bool HasArgument(this IEnumerable<string> args, string arg)
1717
return args.Any(a => a.StartsWith(arg));
1818
}
1919

20-
public static int? LocateParentProcessId(this IEnumerable<string> args)
20+
public static string GetArgumentValue(this IEnumerable<string> args, string argumentName)
2121
{
22-
var hostProcessId = args.SingleOrDefault(arg => arg.StartsWith(CefSharpArguments.WcfHostProcessIdArgument));
23-
if (hostProcessId == null)
22+
var arg = args.FirstOrDefault(a => a.StartsWith(argumentName));
23+
if (arg == null)
2424
{
2525
return null;
2626
}
2727

28-
var parentProcessId = hostProcessId
29-
.Split('=')
30-
.Last();
31-
return int.Parse(parentProcessId);
28+
return arg.Split('=').Last();
3229
}
3330
}
3431
}

0 commit comments

Comments
 (0)