Web panel host fix round 1: non-fatal process kinds, late failures, thread restart, focus and permission guards
- ProcessFailed: only BrowserProcessExited (teardown) and RenderProcessExited (about:blank, recreate on the next Open) fail the panel, as WebView2's docs describe the kinds; GPU, utility, sandbox, PPAPI and frame-only renderer exits are logged only. RenderProcessUnresponsive fails it at 3 reports within 30 s. The handler acts on the panel being served now. - Fail decides "ignored" under the lock first and hides the panel only when the failure is live: a late failure for a superseded handle no longer hides the newer panel. - The panel thread's finally restarts the thread when an Open is still queued and the host is not shutting down; _threadDone goes false only after StartStaThread returns. - Show takes the foreground only when a window of this process (or of this panel's WebView2 browser process) holds it, never while the foreground is null. The visibility rule counts the browser process (CoreWebView2.BrowserProcessId) as ours, so the page's own dialogs do not hide the panel. - ClipboardRead is allowed only for an allowlisted origin. NavigationStarting and NewWindowRequested fail closed on an exception. - WebPanelApiRules: the ABI glue's pure parts (refusals, GetState Size check, state write), tested in WebPanelTests with WebPanelNativeArgs. WebPanelApi.LiveHandle and the browser pid in Describe for Task 4. - Harness: GpuProcessCrashKeepsPanel and LateFailureForSupersededHandle (both RED before the fix), UnloadWhileVisibleCollected reports UNVERIFIED when the window was not visible, the game-side key count is informational. EngineSettingsTests: WebPanel default-on round trip and setter case. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
f130be3d2b
commit
388f3fd908
9 changed files with 427 additions and 64 deletions
|
|
@ -1,9 +1,11 @@
|
|||
using System.Text;
|
||||
using RynthCore.Engine.Plugins;
|
||||
using RynthCore.Engine.WebPanel;
|
||||
|
||||
namespace WebPanelTests;
|
||||
|
||||
/// <summary>The web panel's pure policies: URL allowlist, redaction, visibility, layout, profile folder.</summary>
|
||||
internal static class Program
|
||||
internal static unsafe class Program
|
||||
{
|
||||
private static int _checks, _failed;
|
||||
private static string _case = "";
|
||||
|
|
@ -22,6 +24,8 @@ internal static class Program
|
|||
Visibility();
|
||||
Layout();
|
||||
Profile();
|
||||
ApiRules();
|
||||
NativeArgs();
|
||||
|
||||
Console.WriteLine(_failed == 0 ? $"PASS ({_checks} assertions)" : $"FAIL ({_failed} of {_checks} assertions)");
|
||||
return _failed == 0 ? 0 : 1;
|
||||
|
|
@ -188,6 +192,88 @@ internal static class Program
|
|||
Check(wild.X >= 0 && wild.Y >= 0 && wild.W <= 1 && wild.H <= 1 && wild.X + wild.W <= 1.000001, "FromUserRect clamps to 0..1");
|
||||
}
|
||||
|
||||
// WebPanelApi's pure parts (WebPanelApiRules): the kill switch and shutdown refusals, the GetState Size check.
|
||||
private static void ApiRules()
|
||||
{
|
||||
Case("ApiRules");
|
||||
Eq(WebPanelApiRules.OpenRefusal(shuttingDown: false, enabled: true), 0, "enabled, running: go on");
|
||||
Eq(WebPanelApiRules.OpenRefusal(false, false), (int)WebPanelCode.Unavailable, "engine.json WebPanel false: Unavailable");
|
||||
Eq(WebPanelApiRules.OpenRefusal(true, true), (int)WebPanelCode.ShuttingDown, "shutting down: ShuttingDown");
|
||||
Eq(WebPanelApiRules.OpenRefusal(true, false), (int)WebPanelCode.ShuttingDown, "shutting down wins over the kill switch");
|
||||
Eq(WebPanelApiRules.CallRefusal(false), 0, "calls go on while running");
|
||||
Eq(WebPanelApiRules.CallRefusal(true), (int)WebPanelCode.ShuttingDown, "calls refused after shutdown");
|
||||
|
||||
uint min = WebPanelApiRules.StateMinSize;
|
||||
if (IntPtr.Size == 4) Eq(min, 28u, "OffsetOf(Reserved) of the state struct on x86");
|
||||
Check(min > 0 && min < (uint)sizeof(WebPanelStateNative), "min size is inside the struct");
|
||||
Check(!WebPanelApiRules.StateArgsOk(null), "null state refused");
|
||||
WebPanelStateNative st = default;
|
||||
st.Size = min - 1;
|
||||
Check(!WebPanelApiRules.StateArgsOk(&st), "Size below OffsetOf(Reserved) refused");
|
||||
st.Size = min;
|
||||
Check(WebPanelApiRules.StateArgsOk(&st), "Size == OffsetOf(Reserved) accepted");
|
||||
st.Size = (uint)sizeof(WebPanelStateNative);
|
||||
Check(WebPanelApiRules.StateArgsOk(&st), "full Size accepted");
|
||||
|
||||
st.Reserved[0] = 0xDEADBEEF;
|
||||
WebPanelApiRules.WriteState(&st, new WebPanelSnapshot(7, 3, 5, -9, 200, 4));
|
||||
Eq(st.Handle, 7, "handle written");
|
||||
Eq(st.Status, 3, "status written");
|
||||
Eq(st.Flags, 5u, "flags written");
|
||||
Eq(st.Error, -9, "error written");
|
||||
Eq(st.LastHttpStatus, 200, "http written");
|
||||
Eq(st.NavigationCount, 4u, "navigations written");
|
||||
Eq(st.Reserved[0], 0xDEADBEEFu, "Reserved untouched");
|
||||
Eq(WebPanelApiRules.NoPanel(9).Status, (int)WebPanelStatusCode.Superseded, "no panel yet: Superseded");
|
||||
}
|
||||
|
||||
// WebPanelNativeArgs: the strings are copied out of the caller's buffers; bad arguments are BadArgs.
|
||||
private static void NativeArgs()
|
||||
{
|
||||
Case("NativeArgs");
|
||||
const string lad = @"C:\lad";
|
||||
Eq(WebPanelNativeArgs.TryBuildRequest(null, lad, "W", out _), (int)WebPanelCode.BadArgs, "null struct");
|
||||
|
||||
int Build(string? url, string? allow, string? key, string? title, uint size, uint flags, out WebPanelRequest? req)
|
||||
{
|
||||
byte[] u = Z(url), a = Z(allow), k = Z(key), ti = Z(title);
|
||||
fixed (byte* pu = u, pa = a, pk = k, pt = ti)
|
||||
{
|
||||
var o = new WebPanelOpenNative
|
||||
{
|
||||
Size = size, Flags = flags,
|
||||
Url = url == null ? null : pu, Allowlist = allow == null ? null : pa,
|
||||
ProfileKey = key == null ? null : pk, Title = title == null ? null : pt,
|
||||
};
|
||||
int rc = WebPanelNativeArgs.TryBuildRequest(&o, lad, "W", out req);
|
||||
Array.Fill(u, (byte)'Z'); Array.Fill(a, (byte)'Z'); // the caller's buffers die right after the call
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
static byte[] Z(string? s) => s == null ? new byte[1] : Encoding.UTF8.GetBytes(s + "\0");
|
||||
uint full = (uint)sizeof(WebPanelOpenNative);
|
||||
|
||||
Eq(Build("https://a.test/x?code=1", "a.test", "k", "T", full, 0xFF, out var r), 0, "good request");
|
||||
Eq(r?.Url, "https://a.test/x?code=1", "url copied before the buffer was overwritten");
|
||||
Eq(r?.Allowlist.Length, 1, "allowlist parsed");
|
||||
Eq(r?.Title, "T", "title copied");
|
||||
Eq(r?.Flags, 3u, "unknown flag bits dropped");
|
||||
Check(r != null && r.ProfileFolder.StartsWith(lad, StringComparison.Ordinal), "profile folder under LocalAppData");
|
||||
Eq(Build("https://a.test/", "a.test", null, null, full, 0, out r), 0, "null key and title");
|
||||
Eq(r?.Title, "", "null title is empty");
|
||||
Eq(Build("https://a.test/", "a.test", null, null, 31, 0, out _), (int)WebPanelCode.BadArgs, "Size below OffsetOf(Reserved)");
|
||||
Eq(Build(null, "a.test", null, null, full, 0, out _), (int)WebPanelCode.BadArgs, "no url");
|
||||
Eq(Build("", "a.test", null, null, full, 0, out _), (int)WebPanelCode.BadArgs, "empty url");
|
||||
Eq(Build("https://a.test/", null, null, null, full, 0, out _), (int)WebPanelCode.BadArgs, "no allowlist");
|
||||
Eq(Build("https://a.test/", "*.com", null, null, full, 0, out _), (int)WebPanelCode.BadArgs, "bad allowlist");
|
||||
Eq(Build("https://a.test/", "a.test", null, new string('t', 129), full, 0, out _), (int)WebPanelCode.BadArgs, "title over 128 bytes");
|
||||
Eq(Build("https://a.test/", "a.test", new string('k', 257), null, full, 0, out _), (int)WebPanelCode.BadArgs, "key over 256 bytes");
|
||||
Eq(Build("https://a.test/" + new string('p', 4096), "a.test", null, null, full, 0, out _), (int)WebPanelCode.BadArgs, "url over 4096 bytes");
|
||||
|
||||
byte[] bad = { 0x68, 0xC3, 0x28, 0 }; // invalid UTF-8
|
||||
fixed (byte* pb = bad) Check(!WebPanelNativeArgs.TryReadUtf8(pb, 16, true, out _), "invalid UTF-8 refused");
|
||||
}
|
||||
|
||||
private static void Profile()
|
||||
{
|
||||
Case("Profile");
|
||||
|
|
|
|||
Loading…
Reference in a new issue