diff --git a/src/RynthCore.Engine/WebPanel/WebPanelApiRules.cs b/src/RynthCore.Engine/WebPanel/WebPanelApiRules.cs index 68ed920..e2d1dd5 100644 --- a/src/RynthCore.Engine/WebPanel/WebPanelApiRules.cs +++ b/src/RynthCore.Engine/WebPanel/WebPanelApiRules.cs @@ -18,7 +18,7 @@ internal static unsafe class WebPanelApiRules /// Navigate, Close and GetState: 0 to go on, else ShuttingDown. public static int CallRefusal(bool shuttingDown) => shuttingDown ? (int)WebPanelCode.ShuttingDown : 0; - /// OffsetOf(Reserved): the smallest Size a caller's WebPanelStateNative may declare (32 on x86). + /// OffsetOf(Reserved): the smallest Size a caller's WebPanelStateNative may declare (28 on x86 and x64: the struct has no pointer fields). public static uint StateMinSize { get diff --git a/src/RynthCore.Engine/WebPanel/WebPanelHost.cs b/src/RynthCore.Engine/WebPanel/WebPanelHost.cs index 1bf2096..aec7a07 100644 --- a/src/RynthCore.Engine/WebPanel/WebPanelHost.cs +++ b/src/RynthCore.Engine/WebPanel/WebPanelHost.cs @@ -101,6 +101,11 @@ internal sealed unsafe class WebPanelHost if (_shuttingDown) return (int)WebPanelCode.ShuttingDown; if (_nextHandle <= 0) _nextHandle = (Environment.TickCount & 0x3FFFFFFF) | 1; int handle = _nextHandle++; + // Kept to undo below if the thread cannot start: the caller then sees a failure, and the state must agree. + int prevLive = _live, prevStatus = _status, prevError = _error, prevHttp = _http; + uint prevFlags = _flags, prevNav = _navCount; + string prevUrl = _liveUrl; + string[] prevAllow = _currentAllowlist; _live = handle; _status = (int)WebPanelStatusCode.Starting; _flags = 0; @@ -114,7 +119,18 @@ internal sealed unsafe class WebPanelHost { // First call (or the thread died): start it and return at once. The thread creates its // window first, then drains the queue. - StartThreadLocked(); + try + { + StartThreadLocked(); + } + catch + { + // The queued ProcessOpen stays behind but is skipped: its handle is no longer live. + _live = prevLive; _status = prevStatus; _flags = prevFlags; _error = prevError; + _http = prevHttp; _navCount = prevNav; _liveUrl = prevUrl; _currentAllowlist = prevAllow; + _threadDone = true; + throw; // WebPanelApi reports Unavailable + } } else if (_hwnd != IntPtr.Zero) { @@ -251,6 +267,8 @@ internal sealed unsafe class WebPanelHost internal bool PostForTests(Action a) => Post(() => a(_web)); internal bool PanelOpenForTests => _open; /// A failure arriving late for (the harness passes a superseded one). + /// A renderer-unresponsive report, as WebView2 would raise it (counting against UnresponsiveLimit). + internal bool UnresponsiveForTests() => Post(() => HandleProcessFailed(CoreWebView2ProcessFailedKind.RenderProcessUnresponsive)); internal bool FailForTests(int handle) => Post(() => Fail(handle, WebPanelCode.ProcessFailed, "synthetic (harness)", allowFallback: false)); // ============================ panel thread ============================ @@ -463,6 +481,7 @@ internal sealed unsafe class WebPanelHost { if (_tornDown || !IsLive(handle)) return; // a newer Open is queued behind this one _serving = handle; + _unresponsiveCount = 0; // a new panel starts its own count _req = r; _title = string.IsNullOrWhiteSpace(r.Title) ? "RynthCore" : r.Title.Trim(); _fractions = WebPanelLayoutStore.TryLoad(_env.LocalAppData, out PanelFractions stored) @@ -669,6 +688,7 @@ internal sealed unsafe class WebPanelHost private void OnNavigationCompleted(object? sender, CoreWebView2NavigationCompletedEventArgs e) { + _unresponsiveCount = 0; // a completed navigation means the renderer answered string source = _web?.Source ?? ""; if (string.Equals(source, "about:blank", StringComparison.OrdinalIgnoreCase) || !_open) return; _source = source; @@ -743,7 +763,9 @@ internal sealed unsafe class WebPanelHost CoreWebView2PermissionState state = CoreWebView2PermissionState.Deny; try { + // IsAllowed accepts about:blank (for our own close); a blank frame gets no clipboard. if (e.PermissionKind == CoreWebView2PermissionKind.ClipboardRead + && !string.Equals(e.Uri, "about:blank", StringComparison.OrdinalIgnoreCase) && WebPanelUrlPolicy.IsAllowed(e.Uri, _req?.Allowlist ?? Array.Empty())) state = CoreWebView2PermissionState.Allow; } @@ -861,10 +883,12 @@ internal sealed unsafe class WebPanelHost // Focus the panel only from within this process (the game, or a pop-out): a plugin opening it while the // player is in another program must not pull the foreground away (the PID rule keeps it hidden meanwhile). // Never while the foreground is null (mid alt-tab): that would grab focus on the way to another program. + // Only this process: a foreground in the WebView2 browser process is the page's own dialog, which keeps + // it (IsOurPid, which includes the browser, is for the visibility rule only). IntPtr fg = GetForegroundWindow(); uint fgPid = 0; if (fg != IntPtr.Zero) GetWindowThreadProcessId(fg, out fgPid); - if (fg != IntPtr.Zero && IsOurPid(fgPid)) SetForegroundWindow(_staHwnd); + if (fg != IntPtr.Zero && fgPid == _ourPid) SetForegroundWindow(_staHwnd); if (_ctl != null) { try { _ctl.IsVisible = true; } catch { } } FocusPage(); if (!_timerOn) { SetTimer(_staHwnd, TimerId, 50, IntPtr.Zero); _timerOn = true; } @@ -1019,6 +1043,7 @@ internal sealed unsafe class WebPanelHost _createSeq++; _creating = false; _needsRecreate = false; + _unresponsiveCount = 0; CoreWebView2? web = _web; CoreWebView2Controller? ctl = _ctl; _web = null; diff --git a/tools/WebPanelHarness.Payload/PayloadEntry.cs b/tools/WebPanelHarness.Payload/PayloadEntry.cs index 80b7385..d270cae 100644 --- a/tools/WebPanelHarness.Payload/PayloadEntry.cs +++ b/tools/WebPanelHarness.Payload/PayloadEntry.cs @@ -55,6 +55,7 @@ public static unsafe class PayloadEntry public static bool ThreadRunning => _host!.ThreadRunningForTests; public static bool PanelOpen => _host!.PanelOpenForTests; public static bool FailHandle(int handle) => _host!.FailForTests(handle); + public static bool ReportUnresponsive() => _host!.UnresponsiveForTests(); public static string ProfileFolder(string world, string key) => WebPanelProfile.Folder(_env!.LocalAppData, world, key); diff --git a/tools/WebPanelHarness/Program.cs b/tools/WebPanelHarness/Program.cs index 0af02d7..edc372c 100644 --- a/tools/WebPanelHarness/Program.cs +++ b/tools/WebPanelHarness/Program.cs @@ -105,6 +105,7 @@ internal static unsafe class Program Guard("NoBrowserFallbackAfterNavigation", () => CheckNoFallbackAfterNavigation(s, handle)); Guard("GpuProcessCrashKeepsPanel", () => handle = CheckGpuCrash(s)); Guard("LateFailureForSupersededHandle", () => handle = CheckLateFailure(s)); + Guard("UnresponsiveCountResetsPerOpen", () => handle = CheckUnresponsiveReset(s)); Guard("CreateFailureFallsBack", () => CheckCreateFailure(s)); Guard("VisibilitySameProcess", () => handle = CheckVisibilitySameProcess(s)); Unverified("VisibilityOtherProcess", "needs another process to take the foreground (alt-tab); the harness never forces it. Owner in-game check."); @@ -249,7 +250,7 @@ internal static unsafe class Program return; } if (!closed) { Fail("EscapeClosesAndRefocusesGame", $"AcceleratorKeyPressed fired but the panel is {st[2]}, not Closed"); return; } - if (!panelFg) { Unverified("EscapeClosesAndRefocusesGame", "closed, game saw 0 keys; the foreground hand-back could not be checked (the panel did not hold the foreground)"); return; } + if (!panelFg) { Unverified("EscapeClosesAndRefocusesGame", $"closed (game key messages {g}, informational only); the foreground hand-back could not be checked (the panel did not hold the foreground)"); return; } if (fg != _game) { Fail("EscapeClosesAndRefocusesGame", "closed, but the foreground is not the game"); return; } Ok("EscapeClosesAndRefocusesGame"); } @@ -447,6 +448,28 @@ internal static unsafe class Program return b; } + // Two unresponsive reports, a new Open, one more report: the count starts again per panel, so the new one + // stays Ready (the limit is 3 within 30 s). Reports are injected through the host's own handler. + private static int CheckUnresponsiveReset(Session s) + { + var (a, _) = Open(s, "https://panel.test/index.html", Allow, title: "Hung A"); + bool ra = WaitStatus(s, a, StatusReady, 30000, out _); + s.Call("ReportUnresponsive"); s.Call("ReportUnresponsive"); + Thread.Sleep(300); + int[] sa = State(s, a); + var (b, _) = Open(s, "https://panel.test/b.html", Allow, title: "Hung B"); + bool rb = WaitStatus(s, b, StatusReady, 30000, out _); + s.Call("ReportUnresponsive"); + Thread.Sleep(500); + int[] sb = State(s, b); + W($"two reports on {a}: state [{string.Join(",", sa)}]; then Open {b} Ready {rb}, one more report: state [{string.Join(",", sb)}]"); + if (!ra || !rb) Fail("UnresponsiveCountResetsPerOpen", "setup: not Ready"); + else if (sa[2] != StatusReady) Fail("UnresponsiveCountResetsPerOpen", "two reports already failed the panel (limit is 3)"); + else if (sb[2] != StatusReady) Fail("UnresponsiveCountResetsPerOpen", $"the third report overall failed the new panel: status {sb[2]} error {sb[4]}"); + else Ok("UnresponsiveCountResetsPerOpen"); + return b; + } + private static uint ParentPid(int pid) { IntPtr h = OpenProcess(0x1000 /* PROCESS_QUERY_LIMITED_INFORMATION */, false, pid); diff --git a/tools/WebPanelTests/Program.cs b/tools/WebPanelTests/Program.cs index 76e04bf..c89857e 100644 --- a/tools/WebPanelTests/Program.cs +++ b/tools/WebPanelTests/Program.cs @@ -204,7 +204,7 @@ internal static unsafe class Program 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"); + Eq(min, 28u, "OffsetOf(Reserved) of the state struct (no pointer fields: 28 on x86 and x64)"); Check(min > 0 && min < (uint)sizeof(WebPanelStateNative), "min size is inside the struct"); Check(!WebPanelApiRules.StateArgsOk(null), "null state refused"); WebPanelStateNative st = default;