CEF cookie fixes (#2934)

* Trying to get res:// and usr:// cookies to wrok

* Update CefGlue

* Bump CEF

* Seal types in WebView

Fixes warnings

* Move most of client cleanup to game thread.

This used to run in the windowing thread which broke CEF shutdown, most notably cookie saving.

* "flushcookies" command for CEF.

* Remove unecessary res:// code.

* Fix tests

* More request handler fixes.

* Good thing I don't have to care about commit quality in PRs like this.
This commit is contained in:
Pieter-Jan Briers
2022-06-10 14:30:57 +02:00
committed by GitHub
parent 776669b789
commit 05bb361eb2
12 changed files with 75 additions and 71 deletions

View File

@@ -1,7 +1,4 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using Robust.Shared.ContentPack;
using Xilium.CefGlue;
namespace Robust.Client.WebView.Cef
@@ -19,24 +16,11 @@ namespace Robust.Client.WebView.Cef
Array.Copy(args, 0, argv, 1, args.Length);
argv[0] = "-";
}
/*
if (OperatingSystem.IsLinux())
{
// Chromium tries to load libEGL.so and libGLESv2.so relative to the process executable on Linux.
// (Compared to Windows where it is relative to Chromium's *module*)
// There is a TODO "is this correct?" in the Chromium code for this.
// Great.
//CopyDllToExecutableDir("libEGL.so");
//CopyDllToExecutableDir("libGLESv2.so");
// System.Threading.Thread.Sleep(200000);
}
*/
var mainArgs = new CefMainArgs(argv);
// This will block executing until the subprocess is shut down.
var code = CefRuntime.ExecuteProcess(mainArgs, null, IntPtr.Zero);
var code = CefRuntime.ExecuteProcess(mainArgs, new RobustCefApp(), IntPtr.Zero);
if (code != 0)
{
@@ -45,44 +29,5 @@ namespace Robust.Client.WebView.Cef
return code;
}
/* private static void CopyDllToExecutableDir(string dllName)
{
var executableDir = PathHelpers.GetExecutableDirectory();
var targetPath = Path.Combine(executableDir, dllName);
if (File.Exists(targetPath))
return;
// Find source file.
string? srcFile = null;
foreach (var searchDir in WebViewManagerCef.NativeDllSearchDirectories())
{
var searchPath = Path.Combine(searchDir, dllName);
if (File.Exists(searchPath))
{
srcFile = searchPath;
break;
}
}
if (srcFile == null)
return;
for (var i = 0; i < 5; i++)
{
try
{
if (File.Exists(targetPath))
return;
File.Copy(srcFile, targetPath);
return;
}
catch
{
// Catching race condition lock errors and stuff I guess.
}
}
} */
}
}

View File

@@ -5,7 +5,7 @@ using Xilium.CefGlue;
namespace Robust.Client.WebView.Cef
{
internal class RobustCefApp : CefApp
internal sealed class RobustCefApp : CefApp
{
private readonly BrowserProcessHandler _browserProcessHandler = new();
private readonly RenderProcessHandler _renderProcessHandler = new();
@@ -24,12 +24,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
// 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");
// commandLine.AppendSwitch("--single-process");
//commandLine.AppendSwitch("--disable-gpu");
@@ -43,12 +43,18 @@ namespace Robust.Client.WebView.Cef
Logger.Debug($"{commandLine}");
}
private class BrowserProcessHandler : CefBrowserProcessHandler
protected override void OnRegisterCustomSchemes(CefSchemeRegistrar registrar)
{
registrar.AddCustomScheme("res", CefSchemeOptions.Secure | CefSchemeOptions.Standard);
registrar.AddCustomScheme("usr", CefSchemeOptions.Secure | CefSchemeOptions.Standard);
}
private sealed class BrowserProcessHandler : CefBrowserProcessHandler
{
}
// TODO CEF: Research - Is this even needed?
private class RenderProcessHandler : CefRenderProcessHandler
private sealed class RenderProcessHandler : CefRenderProcessHandler
{
}
}

View File

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

View File

@@ -57,6 +57,14 @@ namespace Robust.Client.WebView.Cef
string requestInitiator,
ref bool disableDefaultHandling)
{
var url = new Uri(request.Url);
if (url.Scheme == "file")
{
// Deny file:// access.
disableDefaultHandling = true;
return null;
}
lock (_resourceRequestHandlers)
{
_sawmill.Debug($"HANDLING REQUEST: {request.Url}");
@@ -113,7 +121,7 @@ namespace Robust.Client.WebView.Cef
CefFrame frame,
CefRequest request)
{
return null;
return new CookieHandler();
}
protected override CefResourceHandler GetResourceHandler(
@@ -124,5 +132,18 @@ namespace Robust.Client.WebView.Cef
return _handler;
}
}
private sealed class CookieHandler : CefCookieAccessFilter
{
protected override bool CanSendCookie(CefBrowser browser, CefFrame frame, CefRequest request, CefCookie cookie)
{
return true;
}
protected override bool CanSaveCookie(CefBrowser browser, CefFrame frame, CefRequest request, CefResponse response, CefCookie cookie)
{
return true;
}
}
}
}

View File

@@ -1,15 +1,19 @@
using System;
using System.IO;
using System.Net;
using System.Reflection;
using Robust.Client.Console;
using Robust.Shared.ContentPack;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Log;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
using Xilium.CefGlue;
namespace Robust.Client.WebView.Cef
{
internal partial class WebViewManagerCef : IWebViewManagerImpl
internal sealed partial class WebViewManagerCef : IWebViewManagerImpl
{
private static readonly string BasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location!)!;
@@ -17,11 +21,18 @@ namespace Robust.Client.WebView.Cef
[Dependency] private readonly IDependencyCollection _dependencyCollection = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IResourceManagerInternal _resourceManager = default!;
[Dependency] private readonly IClientConsoleHost _consoleHost = default!;
public void Initialize()
{
IoCManager.Instance!.InjectDependencies(this, oneOff: true);
_consoleHost.RegisterCommand("flushcookies", Loc.GetString("cmd-flushcookies-desc"), Loc.GetString("cmd-flushcookies-help"), (_, _, _) =>
{
CefCookieManager.GetGlobal(null).FlushStore(null);
});
string subProcessName;
if (OperatingSystem.IsWindows())
subProcessName = "Robust.Client.WebView.exe";
@@ -38,6 +49,10 @@ namespace Robust.Client.WebView.Cef
if (cefResourcesPath == null)
throw new InvalidOperationException("Unable to locate cef_resources directory!");
var cachePath = "";
if (_resourceManager.UserData is WritableDirProvider userData)
cachePath = userData.GetFullPath(new ResourcePath("/cef_cache"));
var settings = new CefSettings()
{
WindowlessRenderingEnabled = true, // So we can render to our UI controls.
@@ -47,6 +62,8 @@ namespace Robust.Client.WebView.Cef
LocalesDirPath = Path.Combine(cefResourcesPath, "locales"),
ResourcesDirPath = cefResourcesPath,
RemoteDebuggingPort = 9222,
CookieableSchemesList = "usr,res",
CachePath = cachePath,
};
Logger.Info($"CEF Version: {CefRuntime.ChromeVersion}");