Skip to content

Commit

Permalink
Start cleanup of CEF Engine
Browse files Browse the repository at this point in the history
- Separate sub-process into its own process
  • Loading branch information
Voltstro committed Jun 23, 2024
1 parent 53aa974 commit cc5448d
Show file tree
Hide file tree
Showing 40 changed files with 375 additions and 295 deletions.
14 changes: 2 additions & 12 deletions src/DevScripts/publish-cef-engine-linux.ps1
Original file line number Diff line number Diff line change
@@ -1,12 +1,2 @@
# Any platform can compile Linux Ready to Run
# https://docs.microsoft.com/en-us/dotnet/core/deploying/ready-to-run#cross-platformarchitecture-restrictions

dotnet publish ../UnityWebBrowser.Engine.Cef/UnityWebBrowser.Engine.Cef.csproj -c ReleaseUnity -r linux-x64 -p:PublishReadyToRun=true --self-contained true --nologo

#Create Engine~/ directory if it doesn't exist
if (-Not (Test-Path "../Packages/UnityWebBrowser.Engine.Cef.Linux-x64/Engine~/")) {
New-Item -Path "../Packages/UnityWebBrowser.Engine.Cef.Linux-x64/Engine~/" -ItemType Directory
New-Item -Path "../Packages/UnityWebBrowser.Engine.Cef.Linux-x64/Engine~/swiftshader/" -ItemType Directory
}

Copy-Item -Path "../UnityWebBrowser.Engine.Cef/bin/ReleaseUnity/linux-x64/publish/*" -Destination "../Packages/UnityWebBrowser.Engine.Cef.Linux-x64/Engine~/" -Recurse -Force -PassThru
dotnet publish ../UnityWebBrowser.Engine.Cef/SubProcess/UnityWebBrowser.Engine.Cef.SubProcess.csproj -r linux-x64 -p:PublishDir=../../Packages/UnityWebBrowser.Engine.Cef.Linux-x64/Engine~ --nologo
dotnet publish ../UnityWebBrowser.Engine.Cef/Main/UnityWebBrowser.Engine.Cef.csproj -r linux-x64 -p:PublishDir=../../Packages/UnityWebBrowser.Engine.Cef.Linux-x64/Engine~ --nologo
21 changes: 2 additions & 19 deletions src/DevScripts/publish-cef-engine-windows.ps1
Original file line number Diff line number Diff line change
@@ -1,19 +1,2 @@
# Only Windows can compile Windows Ready to Run
# https://docs.microsoft.com/en-us/dotnet/core/deploying/ready-to-run#cross-platformarchitecture-restrictions

if($IsWindows)
{
dotnet publish ../UnityWebBrowser.Engine.Cef/UnityWebBrowser.Engine.Cef.csproj -c ReleaseUnity -r win-x64 -p:PublishReadyToRun=true --self-contained true --nologo
}
else
{
dotnet publish ../UnityWebBrowser.Engine.Cef/UnityWebBrowser.Engine.Cef.csproj -c ReleaseUnity -r win-x64 --self-contained true --nologo
}

#Create Engine~/ directory if it doesn't exist
if (-Not (Test-Path "../Packages/UnityWebBrowser.Engine.Cef.Win-x64/Engine~/")) {
New-Item -Path "../Packages/UnityWebBrowser.Engine.Cef.Win-x64/Engine~/" -ItemType Directory
New-Item -Path "../Packages/UnityWebBrowser.Engine.Cef.Win-x64/Engine~/swiftshader/" -ItemType Directory
}

Copy-Item -Path "../UnityWebBrowser.Engine.Cef/bin/ReleaseUnity/win-x64/publish/*" -Destination "../Packages/UnityWebBrowser.Engine.Cef.Win-x64/Engine~/" -Recurse -Force -PassThru
dotnet publish ../UnityWebBrowser.Engine.Cef/SubProcess/UnityWebBrowser.Engine.Cef.SubProcess.csproj -r win-x64 -p:PublishDir=../../Packages/UnityWebBrowser.Engine.Cef.Win-x64/Engine~ --nologo
dotnet publish ../UnityWebBrowser.Engine.Cef/Main/UnityWebBrowser.Engine.Cef.csproj -r win-x64 -p:PublishDir=../../Packages/UnityWebBrowser.Engine.Cef.Win-x64/Engine~ --nologo
24 changes: 0 additions & 24 deletions src/UnityWebBrowser.Engine.Cef/Core/CefSafeBuffer.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,6 @@ public void EarlyInit(LaunchArguments arguments, string[] rawArguments)
//Set up CEF args and the CEF app
cefMainArgs = new CefMainArgs(argv);
cefApp = new UwbCefApp(launchArguments);

//Run our sub-processes
int exitCode = CefRuntime.ExecuteProcess(cefMainArgs, cefApp, IntPtr.Zero);
if (exitCode != -1)
{
CefLoggerWrapper.Debug("Sub-Process exit: {ExitCode}", exitCode);
Environment.Exit(exitCode);
return;
}

//Backup
if (argv.Any(arg => arg.StartsWith("--type=")))
{
CefLoggerWrapper.Error("Invalid process type!");
Environment.Exit(-2);
throw new Exception("Invalid process type!");
}
}

/// <summary>
Expand Down Expand Up @@ -121,12 +104,9 @@ public void Init(ClientControlsActions clientControlsActions, EnginePopupManager
RemoteDebuggingPort = launchArguments.RemoteDebugging,
PersistSessionCookies = true,
PersistUserPreferences = true,
#if LINUX || MACOS
//On Linux we need to tell CEF where everything is, this will assume that the working directory is where everything is!
ResourcesDirPath = Path.Combine(Environment.CurrentDirectory),
LocalesDirPath = Path.Combine(Environment.CurrentDirectory, "locales"),
BrowserSubprocessPath = Environment.ProcessPath
#endif
BrowserSubprocessPath = Path.Combine(Environment.CurrentDirectory, "UnityWebBrowser.Engine.Cef.SubProcess.exe")
};

//Init CEF
Expand Down Expand Up @@ -165,11 +145,6 @@ public void Init(ClientControlsActions clientControlsActions, EnginePopupManager
CefBrowserHost.CreateBrowser(cefWindowInfo, cefClient, cefBrowserSettings, launchArguments.InitialUrl);
}

public static void PostTask(CefThreadId threadId, Action action)
{
CefRuntime.PostTask(threadId, new CefActionTask(action));
}

#region Engine Actions

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -186,7 +161,7 @@ public void Shutdown()
//We can only quit the message loop on the UI (main) thread
if (!CefRuntime.CurrentlyOn(CefThreadId.UI))
{
PostTask(CefThreadId.UI, Shutdown);
CefActionTask.PostTask(CefThreadId.UI, Shutdown);
return;
}

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="../../Imports/NativeDefines.props" />
<Import Project="../../Imports/Properties.props" />

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ApplicationIcon>../Shared/chromium-logo.ico</ApplicationIcon>
<RuntimeIdentifiers>win-x64;linux-x64;osx-x64</RuntimeIdentifiers>

<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<OutDir>../bin/$(Configuration)/</OutDir>
<PublishDir>../bin/$(Configuration)/publish/$(RuntimeIdentifier)</PublishDir>

<InvariantGlobalization>true</InvariantGlobalization>
<PublishSelfContained>true</PublishSelfContained>
<PublishTrimmed>true</PublishTrimmed>
<DebuggerSupport>false</DebuggerSupport>
<UseSystemResourceKeys>true</UseSystemResourceKeys>
<PublishAot>true</PublishAot>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Shared\UnityWebBrowser.Engine.Cef.Shared.csproj" />
</ItemGroup>

<PropertyGroup Condition="'$(IsReleaseBuild)'=='true'">
<!-- Prevents XML and PDB files being copied -->
<AllowedReferenceRelatedFileExtensions>
.allowedextension
</AllowedReferenceRelatedFileExtensions>
</PropertyGroup>

<PropertyGroup Condition="'$(IsReleaseBuild)'=='true'">
<SelfContained>true</SelfContained>
</PropertyGroup>

<Import Project="../../Imports/NatvieDefines.targets" />
</Project>
13 changes: 0 additions & 13 deletions src/UnityWebBrowser.Engine.Cef/Roots.xml

This file was deleted.

8 changes: 8 additions & 0 deletions src/UnityWebBrowser.Engine.Cef/Shared/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// UnityWebBrowser (UWB)
// Copyright (c) 2021-2024 Voltstro-Studios
//
// This project is under the MIT license. See the LICENSE.md file for more details.

using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("UnityWebBrowser.Engine.Cef")]
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public override void Dispose()
{
if (!CefRuntime.CurrentlyOn(CefThreadId.UI))
{
CefEngineControlsManager.PostTask(CefThreadId.UI, Dispose);
CefActionTask.PostTask(CefThreadId.UI, Dispose);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace UnityWebBrowser.Engine.Cef.Browser;
/// <summary>
/// <see cref="CefApp" /> for CefBrowserProcess
/// </summary>
internal class UwbCefApp : CefApp
public class UwbCefApp : CefApp
{
private readonly bool mediaStreamingEnabled;
private readonly bool noProxyServer;
Expand All @@ -20,7 +20,7 @@ internal class UwbCefApp : CefApp

private UwbCefBrowserProcessHandler browserProcessHandler;

internal UwbCefApp(LaunchArguments launchArguments)
public UwbCefApp(LaunchArguments launchArguments)
{
mediaStreamingEnabled = launchArguments.WebRtc;
noProxyServer = !launchArguments.ProxyEnabled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ protected override void Execute()
action();
action = null;
}

public static void PostTask(CefThreadId threadId, Action action)
{
CefRuntime.PostTask(threadId, new CefActionTask(action));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static void Debug(string message)
{
if (!CefRuntime.CurrentlyOn(CefThreadId.UI))
{
CefEngineControlsManager.PostTask(CefThreadId.UI, () => Debug(message));
CefActionTask.PostTask(CefThreadId.UI, () => Debug(message));
return;
}

Expand All @@ -44,7 +44,7 @@ public static void Debug(string message, params object[] values)
{
if (!CefRuntime.CurrentlyOn(CefThreadId.UI))
{
CefEngineControlsManager.PostTask(CefThreadId.UI, () => Debug(message, values));
CefActionTask.PostTask(CefThreadId.UI, () => Debug(message, values));
return;
}

Expand All @@ -63,7 +63,7 @@ public static void Info(string message)
{
if (!CefRuntime.CurrentlyOn(CefThreadId.UI))
{
CefEngineControlsManager.PostTask(CefThreadId.UI, () => Info(message));
CefActionTask.PostTask(CefThreadId.UI, () => Info(message));
return;
}

Expand All @@ -79,7 +79,7 @@ public static void Info(string message, params object[] values)
{
if (!CefRuntime.CurrentlyOn(CefThreadId.UI))
{
CefEngineControlsManager.PostTask(CefThreadId.UI, () => Info(message, values));
CefActionTask.PostTask(CefThreadId.UI, () => Info(message, values));
return;
}

Expand All @@ -98,7 +98,7 @@ public static void Warn(string message)
{
if (!CefRuntime.CurrentlyOn(CefThreadId.UI))
{
CefEngineControlsManager.PostTask(CefThreadId.UI, () => Warn(message));
CefActionTask.PostTask(CefThreadId.UI, () => Warn(message));
return;
}

Expand All @@ -114,7 +114,7 @@ public static void Warn(string message, params object[] values)
{
if (!CefRuntime.CurrentlyOn(CefThreadId.UI))
{
CefEngineControlsManager.PostTask(CefThreadId.UI, () => Warn(message, values));
CefActionTask.PostTask(CefThreadId.UI, () => Warn(message, values));
return;
}

Expand All @@ -133,7 +133,7 @@ public static void Error(string message)
{
if (!CefRuntime.CurrentlyOn(CefThreadId.UI))
{
CefEngineControlsManager.PostTask(CefThreadId.UI, () => Error(message));
CefActionTask.PostTask(CefThreadId.UI, () => Error(message));
return;
}

Expand All @@ -149,7 +149,7 @@ public static void Error(string message, params object[] values)
{
if (!CefRuntime.CurrentlyOn(CefThreadId.UI))
{
CefEngineControlsManager.PostTask(CefThreadId.UI, () => Error(message, values));
CefActionTask.PostTask(CefThreadId.UI, () => Error(message, values));
return;
}

Expand All @@ -165,7 +165,7 @@ public static void Error(Exception exception, string message)
{
if (!CefRuntime.CurrentlyOn(CefThreadId.UI))
{
CefEngineControlsManager.PostTask(CefThreadId.UI, () => Error(exception, message));
CefActionTask.PostTask(CefThreadId.UI, () => Error(exception, message));
return;
}

Expand All @@ -182,7 +182,7 @@ public static void Error(Exception exception, string message, params object[] va
{
if (!CefRuntime.CurrentlyOn(CefThreadId.UI))
{
CefEngineControlsManager.PostTask(CefThreadId.UI, () => Error(exception, message, values));
CefActionTask.PostTask(CefThreadId.UI, () => Error(exception, message, values));
return;
}

Expand Down
Loading

0 comments on commit cc5448d

Please sign in to comment.