Skip to content

Commit aada072

Browse files
committed
Net Core - Add SelfHost class to CefSharp.Core
Allows for quick self hosting of the BrowserSubprocess Issue #3197
1 parent 55af252 commit aada072

File tree

6 files changed

+115
-3
lines changed

6 files changed

+115
-3
lines changed

CefSharp.BrowserSubprocess.Core/BrowserSubprocessExecutable.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,25 @@ namespace CefSharp
2828

2929
}
3030

31+
/// <summary>
32+
/// This function should be called from the application entry point function (typically Program.Main)
33+
/// to execute a secondary process e.g. gpu, plugin, renderer, utility
34+
/// This overload is specifically used for .Net Core. For hosting your own BrowserSubProcess
35+
/// it's preferable to use the Main method provided by this class.
36+
/// - Obtains the command line args via a call to Environment::GetCommandLineArgs
37+
/// - Calls CefEnableHighDPISupport before any other processing
38+
/// </summary>
39+
/// <returns>
40+
/// If called for the browser process (identified by no "type" command-line value) it will return immediately
41+
/// with a value of -1. If called for a recognized secondary process it will block until the process should exit
42+
/// and then return the process exit code.
43+
/// </returns
44+
static int MainNetCoreSelfHost(array<String^>^ args)
45+
{
46+
auto subProcess = gcnew BrowserSubprocessExecutable();
47+
return subProcess->Main(args, nullptr);
48+
}
49+
3150
/// <summary>
3251
/// This function should be called from the application entry point function (typically Program.Main)
3352
/// to execute a secondary process e.g. gpu, plugin, renderer, utility
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright © 2020 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
#include "Stdafx.h"
6+
#include "SelfHost.h"
7+
8+
using namespace System;
9+
using namespace System::IO;
10+
using namespace System::Runtime::Loader;
11+
using namespace CefSharp;
12+
13+
namespace CefSharp
14+
{
15+
namespace BrowserSubprocess
16+
{
17+
int SelfHost::MainNetCore(array<String^>^ args)
18+
{
19+
auto type = CommandLineArgsParser::GetArgumentValue(args, CefSharpArguments::SubProcessTypeArgument);
20+
21+
if (String::IsNullOrEmpty(type))
22+
{
23+
//If --type param missing from command line CEF/Chromium assums
24+
//this is the main process (as all subprocesses must have a type param).
25+
//Return -1 to indicate this behaviour.
26+
return -1;
27+
}
28+
29+
auto browserSubprocessDllPath = Path::Combine(Path::GetDirectoryName(SelfHost::typeid->Assembly->Location), "CefSharp.BrowserSubprocess.Core.dll");
30+
auto browserSubprocessDll = AssemblyLoadContext::Default->LoadFromAssemblyPath(browserSubprocessDllPath);
31+
auto browserSubprocessExecutableType = browserSubprocessDll->GetType("CefSharp.BrowserSubprocess.BrowserSubprocessExecutable");
32+
auto browserSubprocessExecutable = Activator::CreateInstance(browserSubprocessExecutableType);
33+
34+
auto mainMethod = browserSubprocessExecutableType->GetMethod("MainNetCoreSelfHost", System::Reflection::BindingFlags::Static | System::Reflection::BindingFlags::Public);
35+
auto argCount = mainMethod->GetParameters();
36+
37+
auto methodArgs = gcnew array<Object^>(1);
38+
methodArgs[0] = args;
39+
40+
auto exitCode = mainMethod->Invoke(nullptr, methodArgs);
41+
42+
return (int)exitCode;
43+
}
44+
45+
}
46+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright © 2020 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
#pragma once
6+
7+
#include "Stdafx.h"
8+
9+
namespace CefSharp
10+
{
11+
namespace BrowserSubprocess
12+
{
13+
/// <summary>
14+
/// SelfHost can be used to self host the BrowserSubProcess in your
15+
/// existing application (preferred approach for .Net Core).
16+
/// </summary>
17+
public ref class SelfHost
18+
{
19+
public:
20+
/// <summary>
21+
/// This function should be called from the application entry point function (typically Program.Main)
22+
/// to execute a secondary process e.g. gpu, plugin, renderer, utility
23+
/// This overload is specifically used for .Net Core. For hosting your own BrowserSubProcess
24+
/// it's preferable to use the Main method provided by this class.
25+
/// - Pass in command line args
26+
/// - To support High DPI Displays you should call Cef.EnableHighDPISupport before any other processing
27+
/// or add the relevant entries to your app.manifest
28+
/// </summary>
29+
/// <param name="args">command line args</param>
30+
/// <returns>
31+
/// If called for the browser process (identified by no "type" command-line value) it will return immediately
32+
/// with a value of -1. If called for a recognized secondary process it will block until the process should exit
33+
/// and then return the process exit code.
34+
/// </returns
35+
static int MainNetCore(array<String^>^ args);
36+
37+
};
38+
}
39+
}

CefSharp.Core/CefSharp.Core.netcore.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@
246246
<ClCompile Include="RequestContext.cpp" />
247247
<ClCompile Include="Internals\CefResourceHandlerAdapter.cpp" />
248248
<ClCompile Include="RequestContextBuilder.cpp" />
249+
<ClCompile Include="BrowserSubprocess\SelfHost.cpp" />
249250
<ClCompile Include="Stdafx.cpp">
250251
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
251252
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
@@ -305,6 +306,7 @@
305306
<ClInclude Include="Request.h" />
306307
<ClInclude Include="RequestContextBuilder.h" />
307308
<ClInclude Include="resource.h" />
309+
<ClInclude Include="BrowserSubprocess\SelfHost.h" />
308310
<ClInclude Include="UrlRequest.h" />
309311
<ClInclude Include="WindowInfo.h" />
310312
<ClInclude Include="Internals\CefCompletionCallbackAdapter.h" />

CefSharp.Core/CefSharp.Core.netcore.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@
8686
<ClCompile Include="RequestContextBuilder.cpp">
8787
<Filter>Source Files</Filter>
8888
</ClCompile>
89+
<ClCompile Include="BrowserSubprocess\SelfHost.cpp">
90+
<Filter>Source Files</Filter>
91+
</ClCompile>
8992
</ItemGroup>
9093
<ItemGroup>
9194
<ClInclude Include="vcclr_local.h">
@@ -334,6 +337,9 @@
334337
<ClInclude Include="RequestContextBuilder.h">
335338
<Filter>Header Files</Filter>
336339
</ClInclude>
340+
<ClInclude Include="BrowserSubprocess\SelfHost.h">
341+
<Filter>Header Files</Filter>
342+
</ClInclude>
337343
</ItemGroup>
338344
<ItemGroup>
339345
<ClInclude Include="Internals\CefFrameWrapper.h">

CefSharp.WinForms.Example/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ public class Program
1717
[STAThread]
1818
public static int Main(string[] args)
1919
{
20-
const bool simpleSubProcess = false;
20+
const bool selfHostSubProcess = true;
2121

2222
Cef.EnableHighDPISupport();
2323

2424
//NOTE: Using a simple sub processes uses your existing application executable to spawn instances of the sub process.
2525
//Features like JSB, EvaluateScriptAsync, custom schemes require the CefSharp.BrowserSubprocess to function
26-
if (simpleSubProcess)
26+
if (selfHostSubProcess)
2727
{
28-
var exitCode = Cef.ExecuteProcess();
28+
var exitCode = CefSharp.BrowserSubprocess.SelfHost.MainNetCore(args);
2929

3030
if (exitCode >= 0)
3131
{

0 commit comments

Comments
 (0)