ServerPack: one browser decision after the panel, pinned by scan
AfterPanel now asks ServerWebOpen.FallbackToBrowser first and opens the browser only there; the RefusedBeforeNavigate switch arm is gone. A source scan pins OpenInBrowser(r, done) to exactly two places (OpenWeb's browser route and right after the guard), so a browser open added to another arm (a spent code opened twice) fails the tests. Checked by mutation: an extra arm gives 3, a moved call fails the guard check. A newer Open answers the older waiting open Closed only once the engine has queued it; a refused or throwing second open leaves the first waiting (test added, RED against the old order). The SDK-name scan also covers the WebPanel*Fn table slots. Trailing newlines restored. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
0ba3b5909e
commit
9a2cbb59c7
3 changed files with 58 additions and 12 deletions
|
|
@ -71,6 +71,14 @@ public sealed partial class ServerPackPlugin
|
|||
{
|
||||
try
|
||||
{
|
||||
// The one browser decision after the panel (ServerWebOpen.FallbackToBrowser: a refusal before
|
||||
// navigating only). Every other answer below ends without the browser: the code may be spent.
|
||||
if (ServerWebOpen.FallbackToBrowser(result))
|
||||
{
|
||||
LogTagged("web panel: " + result + ", using the browser");
|
||||
OpenInBrowser(r, done);
|
||||
return;
|
||||
}
|
||||
switch (result)
|
||||
{
|
||||
case PanelResult.Opened:
|
||||
|
|
@ -81,10 +89,6 @@ public sealed partial class ServerPackPlugin
|
|||
LogTagged("web panel: OpenedInBrowserByEngine");
|
||||
Finish(done, new WebOpenOutcome(WebOpenRoute.Browser, true, "Opened in your browser."));
|
||||
return;
|
||||
case PanelResult.RefusedBeforeNavigate:
|
||||
LogTagged("web panel: RefusedBeforeNavigate, using the browser");
|
||||
OpenInBrowser(r, done);
|
||||
return;
|
||||
case PanelResult.UrlNotAllowed:
|
||||
case PanelResult.BadArgs:
|
||||
// A plugin bug: the request did not pass the engine's checks. The browser would hide it.
|
||||
|
|
@ -225,4 +229,4 @@ public sealed partial class ServerPackPlugin
|
|||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private int WebPanelCloseCore(int handle) => Host.CloseWebPanel(handle);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -128,12 +128,11 @@ internal sealed class ServerWebPanel
|
|||
|
||||
/// <summary>
|
||||
/// Opens <paramref name="r"/> in the panel. <paramref name="then"/> gets the answer exactly once: now
|
||||
/// for a refusal, else later from Poll, Close or a newer Open. A newer Open answers the older one Closed.
|
||||
/// for a refusal, else later from Poll, Close or a newer Open. A newer Open that the engine queued
|
||||
/// answers the older one Closed (the engine superseded it); a refused one leaves the older one waiting.
|
||||
/// </summary>
|
||||
public void Open(WebOpenRequest r, long nowMs, Action<PanelResult> then)
|
||||
{
|
||||
Answer(Interlocked.Exchange(ref _waiting, null), PanelResult.Closed);
|
||||
|
||||
int code;
|
||||
try
|
||||
{
|
||||
|
|
@ -152,7 +151,7 @@ internal sealed class ServerWebPanel
|
|||
return;
|
||||
}
|
||||
Interlocked.Exchange(ref _handle, code);
|
||||
Interlocked.Exchange(ref _waiting, new WaitingOpen(code, nowMs + PollTimeoutMs, then));
|
||||
Answer(Interlocked.Exchange(ref _waiting, new WaitingOpen(code, nowMs + PollTimeoutMs, then)), PanelResult.Closed);
|
||||
}
|
||||
|
||||
/// <summary>The tick: answers the waiting open once its state decides, or at the timeout.</summary>
|
||||
|
|
@ -200,4 +199,4 @@ internal sealed class ServerWebPanel
|
|||
try { then(result); }
|
||||
catch (Exception) { }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -232,6 +232,33 @@ internal static class ServerWebPanelTests
|
|||
p3.Open(Req, 2, _ => { });
|
||||
p3.Close();
|
||||
Check.Eq(string.Join(",", e3.Closed), "9", "a refused open keeps the earlier handle ours to close");
|
||||
|
||||
// A refused second open leaves the first one waiting; the tick still answers it.
|
||||
foreach (int refusal in new[] { -1, -2, -3, -4, -5 })
|
||||
{
|
||||
var e4 = new FakeEngine { OpenReturns = 7, Status = S_Starting };
|
||||
var p4 = new ServerWebPanel(e4);
|
||||
var a1 = Answers(out var t1);
|
||||
var a2 = Answers(out var t2);
|
||||
p4.Open(Req, 0, t1);
|
||||
e4.OpenReturns = refusal;
|
||||
p4.Open(Req, 10, t2);
|
||||
Check.Eq(a1.Count, 0, "refused second open (" + refusal + "): the first is not answered");
|
||||
Check.True(p4.Waiting, "refused second open (" + refusal + "): the first still waits");
|
||||
Check.Eq(a2.Count, 1, "refused second open (" + refusal + "): answered at once");
|
||||
e4.Status = S_Ready;
|
||||
p4.Poll(20);
|
||||
Check.Eq(string.Join(",", a1), PanelResult.Opened.ToString(), "refused second open (" + refusal + "): the first opens on the tick");
|
||||
}
|
||||
// Thrown second open: the same.
|
||||
var e5 = new FakeEngine { OpenReturns = 7, Status = S_Starting };
|
||||
var p5 = new ServerWebPanel(e5);
|
||||
var b1 = Answers(out var u1);
|
||||
p5.Open(Req, 0, u1);
|
||||
e5.ThrowOnOpen = true;
|
||||
p5.Open(Req, 10, _ => { });
|
||||
Check.Eq(b1.Count, 0, "a second open that throws leaves the first unanswered");
|
||||
Check.True(p5.Waiting, "and waiting");
|
||||
}
|
||||
|
||||
private static void Exceptions()
|
||||
|
|
@ -282,7 +309,7 @@ internal static class ServerWebPanelTests
|
|||
string panel = DreamWeaveWebTests.StripComments(File.ReadAllText(panelPath));
|
||||
|
||||
// The SDK web-panel names, and the only methods allowed to name them.
|
||||
var sdkName = new System.Text.RegularExpressions.Regex(@"\b(HasWebPanel|OpenWebPanel|NavigateWebPanel|CloseWebPanel|GetWebPanelState|WebPanelOptions|WebPanelState|WebPanelStatus|WebPanelFlags|WebPanelResult|WebPanelStateFlags|WebPanelOpenNative|WebPanelStateNative)\b");
|
||||
var sdkName = new System.Text.RegularExpressions.Regex(@"\b(HasWebPanel|OpenWebPanel|NavigateWebPanel|CloseWebPanel|GetWebPanelState|WebPanelOptions|WebPanelState|WebPanelStatus|WebPanelFlags|WebPanelResult|WebPanelStateFlags|WebPanelOpenNative|WebPanelStateNative|WebPanelOpenFn|WebPanelNavigateFn|WebPanelCloseFn|WebPanelGetStateFn)\b");
|
||||
string[] helpers = { "WebPanelHasCore", "WebPanelOpenCore", "WebPanelStateCore", "WebPanelCloseCore" };
|
||||
|
||||
// Every server plugin source other than ServerPackWeb.cs names none of them (the pure seam uses ints).
|
||||
|
|
@ -327,6 +354,15 @@ internal static class ServerWebPanelTests
|
|||
}
|
||||
Check.True(web.Contains("Flags = WebPanelFlags.FallbackToBrowser", StringComparison.Ordinal), "the open asks the engine to fall back to the browser");
|
||||
|
||||
// The browser opens from two places only: OpenWeb's browser route, and right after AfterPanel's
|
||||
// FallbackToBrowser guard. A browser open in any switch arm would spend the code twice.
|
||||
string collapsed = System.Text.RegularExpressions.Regex.Replace(web, @"\s+", "");
|
||||
Check.Eq(CountOf(web, "OpenInBrowser(r, done)"), 2, "ServerPackWeb.cs: OpenInBrowser(r, done) appears exactly twice");
|
||||
Check.Eq(CountOf(collapsed, "if(ServerWebOpen.FallbackToBrowser(result)){LogTagged(\"webpanel:\"+result+\",usingthebrowser\");OpenInBrowser(r,done);return;}"), 1,
|
||||
"AfterPanel: the browser open sits right after the FallbackToBrowser guard");
|
||||
Check.Eq(CountOf(collapsed, "}OpenInBrowser(r,done);}"), 1, "OpenWeb: the other is the browser route at its end");
|
||||
Check.Eq(CountOf(web, "ServerWebOpen.FallbackToBrowser("), 1, "AfterPanel asks FallbackToBrowser once");
|
||||
|
||||
// No locks in the web code: the waiting open and the handle move by Interlocked only.
|
||||
Check.Eq(DreamWeaveWebTests.Locks(web).Count, 0, "ServerPackWeb.cs takes no lock");
|
||||
Check.Eq(DreamWeaveWebTests.Locks(panel).Count, 0, "ServerWebPanel.cs takes no lock");
|
||||
|
|
@ -343,6 +379,13 @@ internal static class ServerWebPanelTests
|
|||
Check.Eq(System.Text.RegularExpressions.Regex.Matches(web, @"\b_webPanel\?\.Poll\(Environment\.TickCount64\)").Count, 1, "PollWeb polls with Environment.TickCount64");
|
||||
}
|
||||
|
||||
private static int CountOf(string text, string needle)
|
||||
{
|
||||
int n = 0, at = 0;
|
||||
while ((at = text.IndexOf(needle, at, StringComparison.Ordinal)) >= 0) { n++; at += needle.Length; }
|
||||
return n;
|
||||
}
|
||||
|
||||
private static int BodyEnd(string code, int from)
|
||||
{
|
||||
int open = code.IndexOf('{', from);
|
||||
|
|
@ -357,4 +400,4 @@ internal static class ServerWebPanelTests
|
|||
}
|
||||
return code.Length;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue