Skip to content
This repository has been archived by the owner on Jun 5, 2023. It is now read-only.

Add RunFromString & RunFromResolver for WebWindow.Blazor #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 42 additions & 16 deletions src/WebWindow.Blazor/ComponentsDesktop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -23,7 +24,46 @@ public static class ComponentsDesktop

internal static WebWindow webWindow;

public static void RunFromString<TStartup>(string windowTitle, string indexContent, string rootPath)
{
var contentRootAbsolute = Path.GetDirectoryName(Path.GetFullPath(rootPath));

RunFromResolver<TStartup>(windowTitle, DefaultBlazorResolver(contentRootAbsolute, (string url, out string contentType) =>
{
contentType = "text/html";
return new MemoryStream(Encoding.Default.GetBytes(indexContent));
}));
}

public static void Run<TStartup>(string windowTitle, string hostHtmlPath)
{
var contentRootAbsolute = Path.GetDirectoryName(Path.GetFullPath(hostHtmlPath));

RunFromResolver<TStartup>(windowTitle, DefaultBlazorResolver(contentRootAbsolute, (string url, out string contentType) =>
{
contentType = GetContentType(hostHtmlPath);
return File.Exists(hostHtmlPath) ? File.OpenRead(hostHtmlPath) : null;
}));
}

private static ResolveWebResourceDelegate DefaultBlazorResolver(string contentRootAbsolute, ResolveWebResourceDelegate indexResolver)
{
return (string url, out string contentType) =>
{
// TODO: Only intercept for the hostname 'app' and passthrough for others
// TODO: Prevent directory traversal?
var appFile = Path.Combine(contentRootAbsolute, new Uri(url).AbsolutePath.Substring(1));
if (appFile == contentRootAbsolute)
{
return indexResolver(url, out contentType);
}

contentType = GetContentType(appFile);
return File.Exists(appFile) ? File.OpenRead(appFile) : null;
};
}

public static void RunFromResolver<TStartup>(string windowTitle, ResolveWebResourceDelegate resolver)
{
DesktopSynchronizationContext.UnhandledException += (sender, exception) =>
{
Expand All @@ -32,21 +72,7 @@ public static void Run<TStartup>(string windowTitle, string hostHtmlPath)

webWindow = new WebWindow(windowTitle, options =>
{
var contentRootAbsolute = Path.GetDirectoryName(Path.GetFullPath(hostHtmlPath));

options.SchemeHandlers.Add(BlazorAppScheme, (string url, out string contentType) =>
{
// TODO: Only intercept for the hostname 'app' and passthrough for others
// TODO: Prevent directory traversal?
var appFile = Path.Combine(contentRootAbsolute, new Uri(url).AbsolutePath.Substring(1));
if (appFile == contentRootAbsolute)
{
appFile = hostHtmlPath;
}

contentType = GetContentType(appFile);
return File.Exists(appFile) ? File.OpenRead(appFile) : null;
});
options.SchemeHandlers.Add(BlazorAppScheme, resolver);

// framework:// is resolved as embedded resources
options.SchemeHandlers.Add("framework", (string url, out string contentType) =>
Expand All @@ -57,7 +83,7 @@ public static void Run<TStartup>(string windowTitle, string hostHtmlPath)
});

CancellationTokenSource appLifetimeCts = new CancellationTokenSource();
Task.Factory.StartNew(async() =>
Task.Factory.StartNew(async () =>
{
try
{
Expand Down