diff --git a/CefSharp.BrowserSubprocess.Core/WcfEnabledSubProcess.cpp b/CefSharp.BrowserSubprocess.Core/WcfEnabledSubProcess.cpp index d59ab36cf..b49aaca8f 100644 --- a/CefSharp.BrowserSubprocess.Core/WcfEnabledSubProcess.cpp +++ b/CefSharp.BrowserSubprocess.Core/WcfEnabledSubProcess.cpp @@ -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(); diff --git a/CefSharp.BrowserSubprocess.Core/WcfEnabledSubProcess.h b/CefSharp.BrowserSubprocess.Core/WcfEnabledSubProcess.h index c6b44ff4f..7e9ac28b4 100644 --- a/CefSharp.BrowserSubprocess.Core/WcfEnabledSubProcess.h +++ b/CefSharp.BrowserSubprocess.Core/WcfEnabledSubProcess.h @@ -17,22 +17,21 @@ namespace CefSharp public ref class WcfEnabledSubProcess : SubProcess { private: - Nullable parentBrowserId; + Nullable _parentBrowserId; /// /// The PID for the parent (browser) process /// - Nullable parentProcessId; + int _parentProcessId; public: - WcfEnabledSubProcess(IEnumerable^ args) : SubProcess(args) + WcfEnabledSubProcess(int parentProcessId, IEnumerable^ args) : SubProcess(args) { - parentProcessId = CommandLineArgsParser::LocateParentProcessId(args); + _parentProcessId = parentProcessId; } void OnBrowserCreated(CefBrowserWrapper^ browser) override; void OnBrowserDestroyed(CefBrowserWrapper^ browser) override; - }; } } \ No newline at end of file diff --git a/CefSharp.BrowserSubprocess/Program.cs b/CefSharp.BrowserSubprocess/Program.cs index ef5b5e931..508fa8a67 100644 --- a/CefSharp.BrowserSubprocess/Program.cs +++ b/CefSharp.BrowserSubprocess/Program.cs @@ -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 @@ -19,16 +17,14 @@ namespace CefSharp.BrowserSubprocess 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) { diff --git a/CefSharp.Core/Internals/CefSharpApp.h b/CefSharp.Core/Internals/CefSharpApp.h index e5c79f3f7..beb9c01a1 100644 --- a/CefSharp.Core/Internals/CefSharpApp.h +++ b/CefSharp.Core/Internals/CefSharpApp.h @@ -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 = "="; diff --git a/CefSharp/Internals/CefSharpArguments.cs b/CefSharp/Internals/CefSharpArguments.cs index 40c1d5a03..202020292 100644 --- a/CefSharp/Internals/CefSharpArguments.cs +++ b/CefSharp/Internals/CefSharpArguments.cs @@ -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"; } } diff --git a/CefSharp/Internals/CommandLineArgsParser.cs b/CefSharp/Internals/CommandLineArgsParser.cs index fc3f0bef5..a67c116eb 100644 --- a/CefSharp/Internals/CommandLineArgsParser.cs +++ b/CefSharp/Internals/CommandLineArgsParser.cs @@ -17,18 +17,15 @@ namespace CefSharp.Internals return args.Any(a => a.StartsWith(arg)); } - public static int? LocateParentProcessId(this IEnumerable args) + public static string GetArgumentValue(this IEnumerable 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(); } } }