-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BrowserSubprocess - Refactor to support .Net Core
- Added BrowserSubprocessExecutable for use with .Net Core (no WCF) - Added WcfBrowserSubprocessExecutable for use with the existing CefSharp.BrowserSubprocess.exe which supports WCF (conditionally)
- Loading branch information
Showing
7 changed files
with
180 additions
and
54 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
CefSharp.BrowserSubprocess.Core/BrowserSubprocessExecutable.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright © 2019 The CefSharp Authors. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. | ||
|
||
#pragma once | ||
|
||
#include "Stdafx.h" | ||
|
||
#include "SubProcess.h" | ||
#include "WcfEnabledSubProcess.h" | ||
|
||
using namespace System; | ||
using namespace CefSharp::Internals; | ||
|
||
namespace CefSharp | ||
{ | ||
namespace BrowserSubprocess | ||
{ | ||
/// <summary> | ||
/// BrowserSubprocessExecutable provides the fundimental browser process handling for | ||
/// CefSharp.BrowserSubprocess.exe and can be used to self host the BrowserSubProcess in your | ||
/// existing application (preferred approach for .Net Core). | ||
/// </summary> | ||
public ref class BrowserSubprocessExecutable | ||
{ | ||
public: | ||
BrowserSubprocessExecutable() | ||
{ | ||
|
||
} | ||
|
||
int Main(IEnumerable<String^>^ args, IRenderProcessHandler^ handler) | ||
{ | ||
int result; | ||
auto type = CommandLineArgsParser::GetArgumentValue(args, CefSharpArguments::SubProcessTypeArgument); | ||
|
||
auto parentProcessId = -1; | ||
|
||
// The Crashpad Handler doesn't have any HostProcessIdArgument, so we must not try to | ||
// parse it lest we want an ArgumentNullException. | ||
if (type != "crashpad-handler") | ||
{ | ||
parentProcessId = int::Parse(CommandLineArgsParser::GetArgumentValue(args, CefSharpArguments::HostProcessIdArgument)); | ||
if (CommandLineArgsParser::HasArgument(args, CefSharpArguments::ExitIfParentProcessClosed)) | ||
{ | ||
ParentProcessMonitor::StartMonitorTask(parentProcessId); | ||
} | ||
} | ||
|
||
// Use our custom subProcess provides features like EvaluateJavascript | ||
if (type == "renderer") | ||
{ | ||
auto subProcess = GetSubprocess(args, parentProcessId, handler); | ||
|
||
try | ||
{ | ||
result = subProcess->Run(); | ||
} | ||
finally | ||
{ | ||
delete subProcess; | ||
} | ||
} | ||
else | ||
{ | ||
result = SubProcess::ExecuteProcess(args); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
protected: | ||
virtual SubProcess^ GetSubprocess(IEnumerable<String^>^ args, int parentProcessId, IRenderProcessHandler^ handler) | ||
{ | ||
return gcnew SubProcess(handler, args); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
CefSharp.BrowserSubprocess.Core/WcfBrowserSubprocessExecutable.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright © 2019 The CefSharp Authors. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. | ||
|
||
#pragma once | ||
|
||
#include "Stdafx.h" | ||
|
||
#include "SubProcess.h" | ||
#include "WcfEnabledSubProcess.h" | ||
#include "BrowserSubprocessExecutable.h" | ||
|
||
using namespace System; | ||
using namespace CefSharp::Internals; | ||
|
||
namespace CefSharp | ||
{ | ||
namespace BrowserSubprocess | ||
{ | ||
public ref class WcfBrowserSubprocessExecutable : BrowserSubprocessExecutable | ||
{ | ||
public: | ||
WcfBrowserSubprocessExecutable() | ||
{ | ||
|
||
} | ||
protected: | ||
SubProcess^ GetSubprocess(IEnumerable<String^>^ args, int parentProcessId, IRenderProcessHandler^ handler) override | ||
{ | ||
auto wcfEnabled = CommandLineArgsParser::HasArgument(args, CefSharpArguments::WcfEnabledArgument); | ||
if (wcfEnabled) | ||
{ | ||
return gcnew WcfEnabledSubProcess(parentProcessId, handler, args); | ||
} | ||
return gcnew SubProcess(handler, args); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright © 2019 The CefSharp Authors. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Threading.Tasks; | ||
|
||
namespace CefSharp.Internals | ||
{ | ||
/// <summary> | ||
/// Monitor the parent process and exit if the parent process closes | ||
/// before the subprocess. This class is used by the CefSharp.BrowserSubprocess to | ||
/// self terminate if the parent dies without notifying it to exit. | ||
/// See https://github.com/cefsharp/CefSharp/issues/2359 for more information. | ||
/// </summary> | ||
public static class ParentProcessMonitor | ||
{ | ||
/// <summary> | ||
/// Starts a long running task (spawns new thread) used to monitor the parent process | ||
/// and calls <see cref="Process.Kill"/> if the parent exits unexpectedly (usually result of a crash). | ||
/// </summary> | ||
/// <param name="parentProcessId">process Id of the parent application</param> | ||
public static void StartMonitorTask(int parentProcessId) | ||
{ | ||
Task.Factory.StartNew(() => AwaitParentProcessExit(parentProcessId), TaskCreationOptions.LongRunning); | ||
} | ||
|
||
private static async void AwaitParentProcessExit(int parentProcessId) | ||
{ | ||
try | ||
{ | ||
var parentProcess = Process.GetProcessById(parentProcessId); | ||
parentProcess.WaitForExit(); | ||
} | ||
catch (Exception e) | ||
{ | ||
//main process probably died already | ||
Debug.WriteLine(e); | ||
} | ||
|
||
await Task.Delay(1000); //wait a bit before exiting | ||
|
||
Debug.WriteLine("BrowserSubprocess shutting down forcibly."); | ||
|
||
Process.GetCurrentProcess().Kill(); | ||
} | ||
} | ||
} |