diff --git a/Plugins/Shared/ServerPack/ServerPackWeb.cs b/Plugins/Shared/ServerPack/ServerPackWeb.cs index e27e758..102dffc 100644 --- a/Plugins/Shared/ServerPack/ServerPackWeb.cs +++ b/Plugins/Shared/ServerPack/ServerPackWeb.cs @@ -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); -} \ No newline at end of file +} diff --git a/Plugins/Shared/ServerPack/ServerWebPanel.cs b/Plugins/Shared/ServerPack/ServerWebPanel.cs index e0d9e90..ba5b816 100644 --- a/Plugins/Shared/ServerPack/ServerWebPanel.cs +++ b/Plugins/Shared/ServerPack/ServerWebPanel.cs @@ -128,12 +128,11 @@ internal sealed class ServerWebPanel /// /// Opens in the panel. 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. /// public void Open(WebOpenRequest r, long nowMs, Action 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); } /// The tick: answers the waiting open once its state decides, or at the timeout. @@ -200,4 +199,4 @@ internal sealed class ServerWebPanel try { then(result); } catch (Exception) { } } -} \ No newline at end of file +} diff --git a/Tools/RynthServerPack.Tests/ServerWebPanelTests.cs b/Tools/RynthServerPack.Tests/ServerWebPanelTests.cs index c01627e..afe243e 100644 --- a/Tools/RynthServerPack.Tests/ServerWebPanelTests.cs +++ b/Tools/RynthServerPack.Tests/ServerWebPanelTests.cs @@ -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; } -} \ No newline at end of file +}