Skip to content

Commit c63b120

Browse files
committed
Core - Add BrowserSubprocess.SelfHost.Main
Can be used to self host the browser SubProcess Uses reflection to load CefSharp.BrowserSubprocess.Core.dll This method is primarily used for .Net Core and does not load the WCF specific features required for synchronous javascript binding
1 parent 6991d0e commit c63b120

File tree

9 files changed

+134
-4
lines changed

9 files changed

+134
-4
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 MainSelfHost(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

CefSharp.Core.RefAssembly/CefSharp.Core.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public partial class ManagedCefBrowserAdapter : CefSharp.Internals.IBrowserAdapt
152152
public ManagedCefBrowserAdapter(CefSharp.Internals.IWebBrowserInternal webBrowserInternal, bool offScreenRendering) { }
153153
public virtual bool IsDisposed { get { throw null; } }
154154
public virtual CefSharp.Internals.IJavascriptCallbackFactory JavascriptCallbackFactory { get { throw null; } }
155-
public virtual CefSharp.Internals.JavascriptObjectRepository JavascriptObjectRepository { get { throw null; } }
155+
public virtual CefSharp.Internals.IJavascriptObjectRepositoryInternal JavascriptObjectRepository { get { throw null; } }
156156
public virtual CefSharp.Internals.IMethodRunnerQueue MethodRunnerQueue { get { throw null; } }
157157
public void CreateBrowser(CefSharp.IWindowInfo windowInfo, CefSharp.BrowserSettings browserSettings, CefSharp.RequestContext requestContext, string address) { }
158158
public void Dispose() { }
@@ -329,6 +329,14 @@ public virtual void SetAsPopup(System.IntPtr parentHandle, string windowName) {
329329
public virtual void SetAsWindowless(System.IntPtr parentHandle) { }
330330
}
331331
}
332+
namespace CefSharp.BrowserSubprocess
333+
{
334+
public partial class SelfHost
335+
{
336+
public SelfHost() { }
337+
public static int Main(string[] args) { throw null; }
338+
}
339+
}
332340
namespace CefSharp.Internals
333341
{
334342
public partial class CefDragDataWrapper : CefSharp.Internals.CefWrapper, CefSharp.IDragData
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 CefSharp;
11+
12+
namespace CefSharp
13+
{
14+
namespace BrowserSubprocess
15+
{
16+
int SelfHost::Main(array<String^>^ args)
17+
{
18+
auto type = CommandLineArgsParser::GetArgumentValue(args, CefSharpArguments::SubProcessTypeArgument);
19+
20+
if (String::IsNullOrEmpty(type))
21+
{
22+
//If --type param missing from command line CEF/Chromium assums
23+
//this is the main process (as all subprocesses must have a type param).
24+
//Return -1 to indicate this behaviour.
25+
return -1;
26+
}
27+
28+
auto browserSubprocessDllPath = Path::Combine(Path::GetDirectoryName(SelfHost::typeid->Assembly->Location), "CefSharp.BrowserSubprocess.Core.dll");
29+
#ifdef NETCOREAPP
30+
auto browserSubprocessDll = System::Runtime::Loader::AssemblyLoadContext::Default->LoadFromAssemblyPath(browserSubprocessDllPath);
31+
#else
32+
auto browserSubprocessDll = System::Reflection::Assembly::LoadFrom(browserSubprocessDllPath);
33+
#endif
34+
auto browserSubprocessExecutableType = browserSubprocessDll->GetType("CefSharp.BrowserSubprocess.BrowserSubprocessExecutable");
35+
auto browserSubprocessExecutable = Activator::CreateInstance(browserSubprocessExecutableType);
36+
37+
auto mainMethod = browserSubprocessExecutableType->GetMethod("MainSelfHost", System::Reflection::BindingFlags::Static | System::Reflection::BindingFlags::Public);
38+
auto argCount = mainMethod->GetParameters();
39+
40+
auto methodArgs = gcnew array<Object^>(1);
41+
methodArgs[0] = args;
42+
43+
auto exitCode = mainMethod->Invoke(nullptr, methodArgs);
44+
45+
return (int)exitCode;
46+
}
47+
48+
}
49+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 primarily intended to be used for .Net Core.
24+
/// - Pass in command line args
25+
/// - To support High DPI Displays you should call Cef.EnableHighDPISupport before any other processing
26+
/// or add the relevant entries to your app.manifest
27+
/// </summary>
28+
/// <param name="args">command line args</param>
29+
/// <returns>
30+
/// If called for the browser process (identified by no "type" command-line value) it will return immediately
31+
/// with a value of -1. If called for a recognized secondary process it will block until the process should exit
32+
/// and then return the process exit code.
33+
/// </returns
34+
static int Main(array<String^>^ args);
35+
36+
};
37+
}
38+
}

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.Core/CefSharp.Core.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@
229229
</ItemGroup>
230230
<ItemGroup>
231231
<ClCompile Include="AssemblyInfo.cpp" />
232+
<ClCompile Include="BrowserSubprocess\SelfHost.cpp" />
232233
<ClCompile Include="CookieManager.cpp" />
233234
<ClCompile Include="Internals\CefBrowserHostWrapper.cpp" />
234235
<ClCompile Include="Internals\CefContextMenuParamsWrapper.cpp" />
@@ -261,6 +262,7 @@
261262
</ItemGroup>
262263
<ItemGroup>
263264
<ClInclude Include="BrowserSettings.h" />
265+
<ClInclude Include="BrowserSubprocess\SelfHost.h" />
264266
<ClInclude Include="Cef.h" />
265267
<ClInclude Include="Internals\CefDevToolsMessageObserverAdapter.h" />
266268
<ClInclude Include="Internals\CefRegistrationWrapper.h" />

CefSharp.Core/CefSharp.Core.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 = false;
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.Main(args);
2929

3030
if (exitCode >= 0)
3131
{

0 commit comments

Comments
 (0)