Bring CEF up to snuff (#4760)

This commit is contained in:
Pieter-Jan Briers
2023-12-25 00:28:51 +01:00
committed by GitHub
parent 7b171b2212
commit 2d3379d7f4
12 changed files with 149 additions and 22 deletions

View File

@@ -0,0 +1,85 @@
using Xilium.CefGlue;
namespace Robust.Client.WebView.Cef;
/// <summary>
/// Shared functionality for all <see cref="CefClient"/> implementations.
/// </summary>
/// <remarks>
/// Locks down a bunch of CEF functionality we absolutely do not need content to do right now.
/// </remarks>
internal abstract class BaseRobustCefClient : CefClient
{
private readonly RobustCefPrintHandler _printHandler = new();
private readonly RobustCefPermissionHandler _permissionHandler = new();
private readonly RobustCefDialogHandler _dialogHandler = new();
private readonly RobustCefDragHandler _dragHandler = new();
protected override CefPrintHandler GetPrintHandler() => _printHandler;
protected override CefPermissionHandler GetPermissionHandler() => _permissionHandler;
protected override CefDialogHandler GetDialogHandler() => _dialogHandler;
protected override CefDragHandler GetDragHandler() => _dragHandler;
private sealed class RobustCefPrintHandler : CefPrintHandler
{
protected override void OnPrintSettings(CefBrowser browser, CefPrintSettings settings, bool getDefaults)
{
}
protected override bool OnPrintDialog(CefBrowser browser, bool hasSelection, CefPrintDialogCallback callback)
{
return false;
}
protected override bool OnPrintJob(CefBrowser browser, string documentName, string pdfFilePath, CefPrintJobCallback callback)
{
return false;
}
protected override void OnPrintReset(CefBrowser browser)
{
}
}
private sealed class RobustCefPermissionHandler : CefPermissionHandler
{
protected override bool OnRequestMediaAccessPermission(
CefBrowser browser,
CefFrame frame,
string requestingOrigin,
CefMediaAccessPermissionTypes requestedPermissions,
CefMediaAccessCallback callback)
{
callback.Cancel();
return true;
}
}
private sealed class RobustCefDialogHandler : CefDialogHandler
{
protected override bool OnFileDialog(
CefBrowser browser,
CefFileDialogMode mode,
string title,
string defaultFilePath,
string[] acceptFilters,
CefFileDialogCallback callback)
{
callback.Cancel();
return true;
}
}
private sealed class RobustCefDragHandler : CefDragHandler
{
protected override bool OnDragEnter(CefBrowser browser, CefDragData dragData, CefDragOperationsMask mask)
{
return true;
}
protected override void OnDraggableRegionsChanged(CefBrowser browser, CefFrame frame, CefDraggableRegion[] regions)
{
}
}
}

View File

@@ -76,12 +76,9 @@ namespace Robust.Client.WebView.Cef
return true;
}
protected override unsafe bool Read(IntPtr dataOut, int bytesToRead, out int bytesRead, CefResourceReadCallback callback)
protected override bool Read(Span<byte> response, out int bytesRead, CefResourceReadCallback callback)
{
var byteSpan = new Span<byte>((void*) dataOut, bytesToRead);
bytesRead = _stream.Read(byteSpan);
bytesRead = _stream.Read(response);
return bytesRead != 0;
}

View File

@@ -31,10 +31,12 @@ namespace Robust.Client.WebView.Cef
// Disable zygote on Linux.
commandLine.AppendSwitch("--no-zygote");
// Work around https://bitbucket.org/chromiumembedded/cef/issues/3213/ozone-egl-initialization-does-not-have
// Work around https://github.com/chromiumembedded/cef/issues/3213
// Desktop GL force makes Chromium not try to load its own ANGLE/Swiftshader so load paths aren't problematic.
if (OperatingSystem.IsLinux())
commandLine.AppendSwitch("--use-gl", "desktop");
// UPDATE: That bug got fixed and now this workaround breaks CEF.
// Keeping all this comment history in case I ever wanan remember what the `--use-gl` flag is.
//if (OperatingSystem.IsLinux())
// commandLine.AppendSwitch("--use-gl", "desktop");
// commandLine.AppendSwitch("--single-process");

View File

@@ -3,7 +3,7 @@ using Xilium.CefGlue;
namespace Robust.Client.WebView.Cef
{
// Simple CEF client.
internal sealed class RobustCefClient : CefClient
internal sealed class RobustCefClient : BaseRobustCefClient
{
private readonly CefRenderHandler _renderHandler;
private readonly CefRequestHandler _requestHandler;

View File

@@ -150,7 +150,7 @@ namespace Robust.Client.WebView.Cef
}
}
private sealed class WindowCefClient : CefClient
private sealed class WindowCefClient : BaseRobustCefClient
{
private readonly CefLifeSpanHandler _lifeSpanHandler;
private readonly CefRequestHandler _requestHandler;

View File

@@ -9,7 +9,7 @@
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2020.3.0" />
<PackageReference Include="Robust.Natives.Cef" Version="102.0.9" />
<PackageReference Include="Robust.Natives.Cef" Version="120.1.9" />
</ItemGroup>
<ItemGroup>

View File

@@ -0,0 +1,28 @@
using System.Numerics;
using Robust.Client.UserInterface.CustomControls;
using Robust.Shared.Console;
namespace Robust.Client.WebView;
internal sealed class TestBrowseWindow : DefaultWindow
{
protected override Vector2 ContentsMinimumSize => new Vector2(640, 480);
public TestBrowseWindow()
{
var wv = new WebViewControl();
wv.Url = "https://spacestation14.io";
Contents.AddChild(wv);
}
}
internal sealed class TestBrowseWindowCommand : LocalizedCommands
{
public override string Command => "test_browse_window";
public override void Execute(IConsoleShell shell, string argStr, string[] args)
{
new TestBrowseWindow().Open();
}
}

View File

@@ -5,6 +5,7 @@ using Robust.Client.WebView.Headless;
using Robust.Client.WebViewHook;
using Robust.Shared.Configuration;
using Robust.Shared.IoC;
using Robust.Shared.Reflection;
using Robust.Shared.Utility;
[assembly: WebViewManagerImpl(typeof(WebViewManager))]
@@ -22,6 +23,9 @@ namespace Robust.Client.WebView
var cfg = dependencies.Resolve<IConfigurationManagerInternal>();
cfg.LoadCVarsFromAssembly(typeof(WebViewManager).Assembly);
var refl = dependencies.Resolve<IReflectionManager>();
refl.LoadAssemblies(typeof(WebViewManager).Assembly);
dependencies.RegisterInstance<IWebViewManager>(this);
dependencies.RegisterInstance<IWebViewManagerInternal>(this);

View File

@@ -109,9 +109,15 @@ namespace Robust.Client
{
IsBackground = false,
Priority = priority,
Name = "Game thread",
Name = "Game thread"
};
if (OperatingSystem.IsWindows())
{
// Necessary for CEF to not complain when using CEF debug binaries.
_gameThread.SetApartmentState(ApartmentState.STA);
}
_gameThread.Start();
// Will block until game exit

View File

@@ -8,6 +8,7 @@ import subprocess
import sys
import zipfile
import argparse
import glob
from typing import List, Optional
@@ -101,10 +102,12 @@ def main() -> None:
if os.path.exists("release"):
print(Fore.BLUE + Style.DIM +
"Cleaning old release packages (release/)..." + Style.RESET_ALL)
shutil.rmtree("release")
"Cleaning old release packages (release/Robust.Client_*)..." + Style.RESET_ALL)
for past in glob.glob("release/Robust.Client_*"):
os.remove(past)
else:
os.mkdir("release")
os.mkdir("release")
if PLATFORM_WINDOWS in platforms:
if not skip_build:

View File

@@ -8,6 +8,7 @@ import subprocess
import sys
import zipfile
import argparse
import glob
from typing import List, Optional
@@ -55,10 +56,11 @@ def main() -> None:
if os.path.exists("release"):
print(Fore.BLUE + Style.DIM +
"Cleaning old release packages (release/)..." + Style.RESET_ALL)
shutil.rmtree("release")
os.mkdir("release")
"Cleaning old release packages (release/Robust.Client.WebView_*)..." + Style.RESET_ALL)
for past in glob.glob("release/Robust.Client.WebView_*"):
os.remove(past)
else:
os.mkdir("release")
if PLATFORM_WINDOWS in platforms:
if not skip_build:
@@ -83,7 +85,7 @@ def build_windows(skip_build: bool) -> None:
# Run a full build.
print(Fore.GREEN + "Building project for Windows x64..." + Style.RESET_ALL)
base_bin = p("Robust.Client.WebView", "bin", "Release", "net7.0")
base_bin = p("Robust.Client.WebView", "bin", "Release", "net8.0")
if not skip_build:
build_client_rid("Windows", "win-x64")
@@ -142,7 +144,7 @@ def build_linux(skip_build: bool) -> None:
# Run a full build.
print(Fore.GREEN + "Building project for Linux x64..." + Style.RESET_ALL)
base_bin = p("Robust.Client.WebView", "bin", "Release", "net7.0")
base_bin = p("Robust.Client.WebView", "bin", "Release", "net8.0")
if not skip_build:
build_client_rid("Linux", "linux-x64")

Submodule cefglue updated: dec642a10f...e265d67a21