Skip to content

Commit

Permalink
Merge d957e36 into 9eb1975
Browse files Browse the repository at this point in the history
  • Loading branch information
João Neves authored Apr 26, 2018
2 parents 9eb1975 + d957e36 commit 04a14eb
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 23 deletions.
4 changes: 2 additions & 2 deletions CefSharp.BrowserSubprocess.Core/WcfEnabledSubProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ namespace CefSharp
Nullable<int> parentProcessId;

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

void OnBrowserCreated(CefBrowserWrapper^ browser) override;
Expand Down
34 changes: 27 additions & 7 deletions CefSharp.BrowserSubprocess/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
// 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 System.Threading.Tasks;
using CefSharp.Internals;

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

int result;
var type = args.GetArgumentValue(CefSharpArguments.SubProcessTypeArgument);
var parentProcessId = int.Parse(args.GetArgumentValue(CefSharpArguments.HostProcessIdArgument));

const string typePrefix = "--type=";
var typeArgument = args.SingleOrDefault(arg => arg.StartsWith(typePrefix));
var type = typeArgument.Substring(typePrefix.Length);
Task.Run(() => AwaitParentProcessExit(parentProcessId));

//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 All @@ -41,8 +40,29 @@ public static int Main(string[] args)
}

Debug.WriteLine("BrowserSubprocess shutting down.");

return result;
}

private static void AwaitParentProcessExit(int parentProcessId)
{
try
{
var parentProcess = Process.GetProcessById(parentProcessId);
parentProcess.WaitForExit();
}
catch (Exception e)
{
//main process probably died already, bailout
Debug.WriteLine(e);
return;
}

Task.Delay(1000); //wait a bit before exiting

Debug.WriteLine("BrowserSubprocess shutting down forcibly.");

Environment.Exit(0);
}
}
}
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 wait for parent process exit
commandLine->AppendArgument(StringUtils::ToNative(CefSharpArguments::HostProcessIdArgument + "=" + Process::GetCurrentProcess()->Id));

if (_cefSettings->_cefCustomSchemes->Count > 0)
{
String^ argument = "=";
Expand Down
5 changes: 2 additions & 3 deletions CefSharp/CefCustomScheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,12 @@ public CefCustomScheme()
/// <returns>list of scheme objects</returns>
public static List<CefCustomScheme> ParseCommandLineArguments(IEnumerable<string> args)
{
var schemes = args.FirstOrDefault(a => a.StartsWith(CefSharpArguments.CustomSchemeArgument));
var schemes = args.GetArgumentValue(CefSharpArguments.CustomSchemeArgument);
var customSchemes = new List<CefCustomScheme>();

if (!string.IsNullOrEmpty(schemes))
{
//Remove the "--custom-scheme=" part of the argument
schemes.Substring(CefSharpArguments.CustomSchemeArgument.Length + 1).Split(';').ToList().ForEach(x =>
schemes.Split(';').ToList().ForEach(x =>
{
var tokens = x.Split('|');
var customScheme = new CefCustomScheme
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 04a14eb

Please sign in to comment.