Skip to content

Commit

Permalink
Always pass through Process Id to subprocess
Browse files Browse the repository at this point in the history
Improve command line parsing

Partial import of changes from #2375
  • Loading branch information
amaitland committed Apr 30, 2018
1 parent 8f7692a commit f2fcbf0
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 28 deletions.
11 changes: 6 additions & 5 deletions CefSharp.BrowserSubprocess.Core/WcfEnabledSubProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,20 @@ namespace CefSharp
{
void WcfEnabledSubProcess::OnBrowserCreated(CefBrowserWrapper^ browser)
{
if (!parentBrowserId.HasValue)
if (!_parentBrowserId.HasValue)
{
parentBrowserId = browser->BrowserId;
_parentBrowserId = browser->BrowserId;
}

if (!parentProcessId.HasValue || !parentBrowserId.HasValue)
if (!_parentBrowserId.HasValue)
{
return;
}

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

auto serviceName = RenderprocessClientFactory::GetServiceName(parentProcessId.Value, browserId);
auto serviceName = RenderprocessClientFactory::GetServiceName(_parentProcessId, browserId);

auto binding = BrowserProcessServiceHost::CreateBinding();

Expand Down
9 changes: 4 additions & 5 deletions CefSharp.BrowserSubprocess.Core/WcfEnabledSubProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,21 @@ namespace CefSharp
public ref class WcfEnabledSubProcess : SubProcess
{
private:
Nullable<int> parentBrowserId;
Nullable<int> _parentBrowserId;

/// <summary>
/// The PID for the parent (browser) process
/// </summary>
Nullable<int> parentProcessId;
int _parentProcessId;

public:
WcfEnabledSubProcess(IEnumerable<String^>^ args) : SubProcess(args)
WcfEnabledSubProcess(int parentProcessId, IEnumerable<String^>^ args) : SubProcess(args)
{
parentProcessId = CommandLineArgsParser::LocateParentProcessId(args);
_parentProcessId = parentProcessId;
}

void OnBrowserCreated(CefBrowserWrapper^ browser) override;
void OnBrowserDestroyed(CefBrowserWrapper^ browser) override;

};
}
}
10 changes: 3 additions & 7 deletions CefSharp.BrowserSubprocess/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
// 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.Diagnostics;
using System.Linq;
using CefSharp.Internals;

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

int result;

const string typePrefix = "--type=";
var typeArgument = args.SingleOrDefault(arg => arg.StartsWith(typePrefix));
var type = typeArgument.Substring(typePrefix.Length);
var type = args.GetArgumentValue(CefSharpArguments.SubProcessTypeArgument);
var parentProcessId = int.Parse(args.GetArgumentValue(CefSharpArguments.HostProcessIdArgument));

//Use our custom subProcess provides features like EvaluateJavascript
if (type == "renderer")
{
var wcfEnabled = args.HasArgument(CefSharpArguments.WcfEnabledArgument);
var subProcess = wcfEnabled ? new WcfEnabledSubProcess(args) : new SubProcess(args);
var subProcess = wcfEnabled ? new WcfEnabledSubProcess(parentProcessId, args) : new SubProcess(args);

using (subProcess)
{
Expand Down
7 changes: 4 additions & 3 deletions CefSharp.Core/Internals/CefSharpApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ namespace CefSharp
if (CefSharpSettings::WcfEnabled)
{
commandLine->AppendArgument(StringUtils::ToNative(CefSharpArguments::WcfEnabledArgument));
//ChannelId was removed in https://bitbucket.org/chromiumembedded/cef/issues/1912/notreached-in-logchannelidandcookiestores
//We need to know the process Id to establish WCF communication
commandLine->AppendArgument(StringUtils::ToNative(CefSharpArguments::WcfHostProcessIdArgument + "=" + Process::GetCurrentProcess()->Id));
}

//ChannelId was removed in https://bitbucket.org/chromiumembedded/cef/issues/1912/notreached-in-logchannelidandcookiestores
//We need to know the process Id to establish WCF communication and for monitoring of parent process exit
commandLine->AppendArgument(StringUtils::ToNative(CefSharpArguments::HostProcessIdArgument + "=" + Process::GetCurrentProcess()->Id));

if (_cefSettings->_cefCustomSchemes->Count > 0)
{
String^ argument = "=";
Expand Down
3 changes: 2 additions & 1 deletion CefSharp/Internals/CefSharpArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ namespace CefSharp.Internals
public static class CefSharpArguments
{
public const string WcfEnabledArgument = "--wcf-enabled";
public const string WcfHostProcessIdArgument = "--wcf-host-process-id";
public const string HostProcessIdArgument = "--host-process-id";
public const string CustomSchemeArgument = "--custom-scheme";
public const string FocusedNodeChangedEnabledArgument = "--focused-node-enabled";
public const string SubProcessTypeArgument = "--type";
}
}
11 changes: 4 additions & 7 deletions CefSharp/Internals/CommandLineArgsParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,15 @@ public static bool HasArgument(this IEnumerable<string> args, string arg)
return args.Any(a => a.StartsWith(arg));
}

public static int? LocateParentProcessId(this IEnumerable<string> args)
public static string GetArgumentValue(this IEnumerable<string> args, string argumentName)
{
var hostProcessId = args.SingleOrDefault(arg => arg.StartsWith(CefSharpArguments.WcfHostProcessIdArgument));
if (hostProcessId == null)
var arg = args.FirstOrDefault(a => a.StartsWith(argumentName));
if (arg == null)
{
return null;
}

var parentProcessId = hostProcessId
.Split('=')
.Last();
return int.Parse(parentProcessId);
return arg.Split('=').Last();
}
}
}

0 comments on commit f2fcbf0

Please sign in to comment.