Skip to content

Commit

Permalink
Core - Add BrowserSubprocess.SelfHost.Main
Browse files Browse the repository at this point in the history
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
  • Loading branch information
amaitland committed Nov 27, 2020
1 parent 6991d0e commit c63b120
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 4 deletions.
19 changes: 19 additions & 0 deletions CefSharp.BrowserSubprocess.Core/BrowserSubprocessExecutable.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@ namespace CefSharp

}

/// <summary>
/// This function should be called from the application entry point function (typically Program.Main)
/// to execute a secondary process e.g. gpu, plugin, renderer, utility
/// This overload is specifically used for .Net Core. For hosting your own BrowserSubProcess
/// it's preferable to use the Main method provided by this class.
/// - Obtains the command line args via a call to Environment::GetCommandLineArgs
/// - Calls CefEnableHighDPISupport before any other processing
/// </summary>
/// <returns>
/// If called for the browser process (identified by no "type" command-line value) it will return immediately
/// with a value of -1. If called for a recognized secondary process it will block until the process should exit
/// and then return the process exit code.
/// </returns
static int MainSelfHost(array<String^>^ args)
{
auto subProcess = gcnew BrowserSubprocessExecutable();
return subProcess->Main(args, nullptr);
}

/// <summary>
/// This function should be called from the application entry point function (typically Program.Main)
/// to execute a secondary process e.g. gpu, plugin, renderer, utility
Expand Down
10 changes: 9 additions & 1 deletion CefSharp.Core.RefAssembly/CefSharp.Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public partial class ManagedCefBrowserAdapter : CefSharp.Internals.IBrowserAdapt
public ManagedCefBrowserAdapter(CefSharp.Internals.IWebBrowserInternal webBrowserInternal, bool offScreenRendering) { }
public virtual bool IsDisposed { get { throw null; } }
public virtual CefSharp.Internals.IJavascriptCallbackFactory JavascriptCallbackFactory { get { throw null; } }
public virtual CefSharp.Internals.JavascriptObjectRepository JavascriptObjectRepository { get { throw null; } }
public virtual CefSharp.Internals.IJavascriptObjectRepositoryInternal JavascriptObjectRepository { get { throw null; } }
public virtual CefSharp.Internals.IMethodRunnerQueue MethodRunnerQueue { get { throw null; } }
public void CreateBrowser(CefSharp.IWindowInfo windowInfo, CefSharp.BrowserSettings browserSettings, CefSharp.RequestContext requestContext, string address) { }
public void Dispose() { }
Expand Down Expand Up @@ -329,6 +329,14 @@ public partial class WindowInfo : CefSharp.IWindowInfo
public virtual void SetAsWindowless(System.IntPtr parentHandle) { }
}
}
namespace CefSharp.BrowserSubprocess
{
public partial class SelfHost
{
public SelfHost() { }
public static int Main(string[] args) { throw null; }
}
}
namespace CefSharp.Internals
{
public partial class CefDragDataWrapper : CefSharp.Internals.CefWrapper, CefSharp.IDragData
Expand Down
49 changes: 49 additions & 0 deletions CefSharp.Core/BrowserSubprocess/SelfHost.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright © 2020 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.

#include "Stdafx.h"
#include "SelfHost.h"

using namespace System;
using namespace System::IO;
using namespace CefSharp;

namespace CefSharp
{
namespace BrowserSubprocess
{
int SelfHost::Main(array<String^>^ args)
{
auto type = CommandLineArgsParser::GetArgumentValue(args, CefSharpArguments::SubProcessTypeArgument);

if (String::IsNullOrEmpty(type))
{
//If --type param missing from command line CEF/Chromium assums
//this is the main process (as all subprocesses must have a type param).
//Return -1 to indicate this behaviour.
return -1;
}

auto browserSubprocessDllPath = Path::Combine(Path::GetDirectoryName(SelfHost::typeid->Assembly->Location), "CefSharp.BrowserSubprocess.Core.dll");
#ifdef NETCOREAPP
auto browserSubprocessDll = System::Runtime::Loader::AssemblyLoadContext::Default->LoadFromAssemblyPath(browserSubprocessDllPath);
#else
auto browserSubprocessDll = System::Reflection::Assembly::LoadFrom(browserSubprocessDllPath);
#endif
auto browserSubprocessExecutableType = browserSubprocessDll->GetType("CefSharp.BrowserSubprocess.BrowserSubprocessExecutable");
auto browserSubprocessExecutable = Activator::CreateInstance(browserSubprocessExecutableType);

auto mainMethod = browserSubprocessExecutableType->GetMethod("MainSelfHost", System::Reflection::BindingFlags::Static | System::Reflection::BindingFlags::Public);
auto argCount = mainMethod->GetParameters();

auto methodArgs = gcnew array<Object^>(1);
methodArgs[0] = args;

auto exitCode = mainMethod->Invoke(nullptr, methodArgs);

return (int)exitCode;
}

}
}
38 changes: 38 additions & 0 deletions CefSharp.Core/BrowserSubprocess/SelfHost.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright © 2020 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"

namespace CefSharp
{
namespace BrowserSubprocess
{
/// <summary>
/// SelfHost can be used to self host the BrowserSubProcess in your
/// existing application (preferred approach for .Net Core).
/// </summary>
public ref class SelfHost
{
public:
/// <summary>
/// This function should be called from the application entry point function (typically Program.Main)
/// to execute a secondary process e.g. gpu, plugin, renderer, utility
/// This overload is primarily intended to be used for .Net Core.
/// - Pass in command line args
/// - To support High DPI Displays you should call Cef.EnableHighDPISupport before any other processing
/// or add the relevant entries to your app.manifest
/// </summary>
/// <param name="args">command line args</param>
/// <returns>
/// If called for the browser process (identified by no "type" command-line value) it will return immediately
/// with a value of -1. If called for a recognized secondary process it will block until the process should exit
/// and then return the process exit code.
/// </returns
static int Main(array<String^>^ args);

};
}
}
2 changes: 2 additions & 0 deletions CefSharp.Core/CefSharp.Core.netcore.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@
<ClCompile Include="RequestContext.cpp" />
<ClCompile Include="Internals\CefResourceHandlerAdapter.cpp" />
<ClCompile Include="RequestContextBuilder.cpp" />
<ClCompile Include="BrowserSubprocess\SelfHost.cpp" />
<ClCompile Include="Stdafx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
Expand Down Expand Up @@ -305,6 +306,7 @@
<ClInclude Include="Request.h" />
<ClInclude Include="RequestContextBuilder.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="BrowserSubprocess\SelfHost.h" />
<ClInclude Include="UrlRequest.h" />
<ClInclude Include="WindowInfo.h" />
<ClInclude Include="Internals\CefCompletionCallbackAdapter.h" />
Expand Down
6 changes: 6 additions & 0 deletions CefSharp.Core/CefSharp.Core.netcore.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
<ClCompile Include="RequestContextBuilder.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="BrowserSubprocess\SelfHost.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="vcclr_local.h">
Expand Down Expand Up @@ -334,6 +337,9 @@
<ClInclude Include="RequestContextBuilder.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="BrowserSubprocess\SelfHost.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Internals\CefFrameWrapper.h">
Expand Down
2 changes: 2 additions & 0 deletions CefSharp.Core/CefSharp.Core.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="AssemblyInfo.cpp" />
<ClCompile Include="BrowserSubprocess\SelfHost.cpp" />
<ClCompile Include="CookieManager.cpp" />
<ClCompile Include="Internals\CefBrowserHostWrapper.cpp" />
<ClCompile Include="Internals\CefContextMenuParamsWrapper.cpp" />
Expand Down Expand Up @@ -261,6 +262,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="BrowserSettings.h" />
<ClInclude Include="BrowserSubprocess\SelfHost.h" />
<ClInclude Include="Cef.h" />
<ClInclude Include="Internals\CefDevToolsMessageObserverAdapter.h" />
<ClInclude Include="Internals\CefRegistrationWrapper.h" />
Expand Down
6 changes: 6 additions & 0 deletions CefSharp.Core/CefSharp.Core.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
<ClCompile Include="RequestContextBuilder.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="BrowserSubprocess\SelfHost.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="vcclr_local.h">
Expand Down Expand Up @@ -334,6 +337,9 @@
<ClInclude Include="RequestContextBuilder.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="BrowserSubprocess\SelfHost.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Internals\CefFrameWrapper.h">
Expand Down
6 changes: 3 additions & 3 deletions CefSharp.WinForms.Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ public class Program
[STAThread]
public static int Main(string[] args)
{
const bool simpleSubProcess = false;
const bool selfHostSubProcess = false;

Cef.EnableHighDPISupport();

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

if (exitCode >= 0)
{
Expand Down

0 comments on commit c63b120

Please sign in to comment.