ServerPack web open: fall back only before the panel navigates

PanelResult + pure FallbackToBrowser; guard TryOpenPanel throws; guard done callbacks;
hand ShellExecuteW the validated absolute URI.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Daniel Wallace 2026-11-07 14:38:48 -08:00
commit c085c16e15
3 changed files with 51 additions and 13 deletions

View file

@ -17,13 +17,14 @@ public sealed partial class ServerPackPlugin
[DllImport("shell32.dll", CharSet = CharSet.Unicode, EntryPoint = "ShellExecuteW")]
private static extern IntPtr ShellExecuteW(IntPtr hwnd, string? operation, string file, string? parameters, string? directory, int showCmd);
// Seams for the engine's web panel.
// Seam for the engine's web panel (a stub until the engine API is wired).
private bool PanelAvailable => false;
private bool TryOpenPanel(WebOpenRequest r, out string error)
private PanelResult TryOpenPanel(WebOpenRequest r) => PanelResult.RefusedBeforeNavigate;
private static void Finish(Action<WebOpenOutcome> done, WebOpenOutcome outcome)
{
error = "The web panel is not available.";
return false;
try { done(outcome); } catch { }
}
/// <summary>Opens the page (pump thread). `done` gets the route taken and a message without the URL.</summary>
@ -32,23 +33,35 @@ public sealed partial class ServerPackPlugin
WebOpenRoute route = ServerWebOpen.Route(r, PanelAvailable);
if (route == WebOpenRoute.Refused)
{
done(new WebOpenOutcome(route, false, "That page is not on this server's list of pages, so it was not opened."));
Finish(done, new WebOpenOutcome(route, false, "That page is not on this server's list of pages, so it was not opened."));
return;
}
if (route == WebOpenRoute.Panel)
{
if (TryOpenPanel(r, out string error))
PanelResult result;
try { result = TryOpenPanel(r); }
catch (Exception) { result = PanelResult.RefusedBeforeNavigate; }
if (result == PanelResult.Opened)
{
done(new WebOpenOutcome(route, true, "Opened in the web panel."));
Finish(done, new WebOpenOutcome(WebOpenRoute.Panel, true, "Opened in the web panel."));
return;
}
// The panel failed: the browser is the fallback, and the report below says it was the browser.
LogTagged("web panel did not open: " + error);
route = WebOpenRoute.Browser;
if (!ServerWebOpen.FallbackToBrowser(result))
{
LogTagged("web panel: " + result);
Finish(done, new WebOpenOutcome(WebOpenRoute.Panel, false, "The web panel couldn't open that page."));
return;
}
LogTagged("web panel: " + result + ", using the browser");
}
string url = r.Url;
if (!ServerWebOpen.TryGetAllowedUri(r.Url, r.AllowedHosts, out string url))
{
Finish(done, new WebOpenOutcome(WebOpenRoute.Refused, false, "That page is not on this server's list of pages, so it was not opened."));
return;
}
var thread = new Thread(() =>
{
string message;
@ -64,7 +77,7 @@ public sealed partial class ServerPackPlugin
ok = false;
message = "Couldn't open your browser (code 0).";
}
done(new WebOpenOutcome(WebOpenRoute.Browser, ok, message));
Finish(done, new WebOpenOutcome(WebOpenRoute.Browser, ok, message));
})
{
IsBackground = true,
@ -73,7 +86,6 @@ public sealed partial class ServerPackPlugin
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
/// <summary>Closes the engine's web panel if this plugin opened one. Nothing to close until the panel API is wired.</summary>
internal void CloseWeb()
{