WebPanel: fix round 2 (unresponsive reset, foreground gate, clipboard, Open rollback)

- Reset the unresponsive counter on teardown, on each new open and on
  navigation completed; harness check UnresponsiveCountResetsPerOpen.
- Show only calls SetForegroundWindow when our process holds the foreground.
- ClipboardRead permission is denied for about:blank.
- Open rolls back live handle and status fields if the STA thread fails to start.
- State struct minimum size is 28 on both platforms; test asserts it unguarded.
- Harness Escape message reports game key count as informational.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Daniel Wallace 2026-11-07 15:38:19 -08:00
commit ba7d96c7d9
5 changed files with 54 additions and 5 deletions

View file

@ -18,7 +18,7 @@ internal static unsafe class WebPanelApiRules
/// <summary>Navigate, Close and GetState: 0 to go on, else ShuttingDown.</summary>
public static int CallRefusal(bool shuttingDown) => shuttingDown ? (int)WebPanelCode.ShuttingDown : 0;
/// <summary>OffsetOf(Reserved): the smallest Size a caller's WebPanelStateNative may declare (32 on x86).</summary>
/// <summary>OffsetOf(Reserved): the smallest Size a caller's WebPanelStateNative may declare (28 on x86 and x64: the struct has no pointer fields).</summary>
public static uint StateMinSize
{
get

View file

@ -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<CoreWebView2?> a) => Post(() => a(_web));
internal bool PanelOpenForTests => _open;
/// <summary>A failure arriving late for <paramref name="handle"/> (the harness passes a superseded one).</summary>
/// <summary>A renderer-unresponsive report, as WebView2 would raise it (counting against UnresponsiveLimit).</summary>
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<string>()))
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;