Skip to content

Commit 56cc2ba

Browse files
committed
Fix BlazorWebView RequestIntercept issue (dotnet#524)
1 parent 868ba09 commit 56cc2ba

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/BlazorWebView/src/Maui/Tizen/BlazorWebViewHandler.Tizen.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34
using Microsoft.Extensions.DependencyInjection;
45
using Microsoft.Extensions.FileProviders;
@@ -15,6 +16,8 @@ namespace Microsoft.AspNetCore.Components.WebView.Maui
1516
/// </summary>
1617
public partial class BlazorWebViewHandler : ViewHandler<IBlazorWebView, NWebView>
1718
{
19+
private const string BlazorWebViewIdentifier = "BlazorWebView:";
20+
private const string UserAgentHeaderKey = "User-Agent";
1821
private const string AppOrigin = "http://0.0.0.0/";
1922
private const string BlazorInitScript = @"
2023
window.__receiveMessageCallbacks = [];
@@ -41,6 +44,8 @@ public partial class BlazorWebViewHandler : ViewHandler<IBlazorWebView, NWebView
4144
})();
4245
";
4346

47+
static private Dictionary<string, WeakReference<BlazorWebViewHandler>> s_webviewHandlerTable = new Dictionary<string, WeakReference<BlazorWebViewHandler>>();
48+
4449
private TizenWebViewManager? _webviewManager;
4550

4651
private bool RequiredStartupPropertiesSet =>
@@ -62,15 +67,18 @@ protected override NWebView CreatePlatformView()
6267
protected override void ConnectHandler(NWebView platformView)
6368
{
6469
platformView.PageLoadFinished += OnLoadFinished;
65-
platformView.Context.RegisterHttpRequestInterceptedCallback(OnRequestInterceptCallback);
70+
platformView.Context.RegisterHttpRequestInterceptedCallback(OnRequestInterceptStaticCallback);
6671
platformView.AddJavaScriptMessageHandler("BlazorHandler", PostMessageFromJS);
72+
platformView.UserAgent += $" {BlazorWebViewIdentifier}{GetHashCode()}";
73+
s_webviewHandlerTable[GetHashCode().ToString()] = new WeakReference<BlazorWebViewHandler>(this);
6774
}
6875

6976
/// <inheritdoc />
7077
protected override void DisconnectHandler(NWebView platformView)
7178
{
7279
platformView.PageLoadFinished -= OnLoadFinished;
7380
base.DisconnectHandler(platformView);
81+
s_webviewHandlerTable.Remove(GetHashCode().ToString());
7482
}
7583

7684

@@ -88,6 +96,25 @@ private void OnLoadFinished(object? sender, WebViewPageLoadEventArgs e)
8896
PlatformView.EvaluateJavaScript(BlazorInitScript);
8997
}
9098

99+
private static void OnRequestInterceptStaticCallback(WebHttpRequestInterceptor interceptor)
100+
{
101+
if (interceptor.Headers.TryGetValue(UserAgentHeaderKey, out var agent))
102+
{
103+
var idx = agent.IndexOf(BlazorWebViewIdentifier);
104+
if (idx >= 0)
105+
{
106+
var webviewKey = agent.Substring(idx + BlazorWebViewIdentifier.Length);
107+
if (s_webviewHandlerTable.TryGetValue(webviewKey, out var weakHandler)
108+
&& weakHandler.TryGetTarget(out var handler))
109+
{
110+
handler.OnRequestInterceptCallback(interceptor);
111+
return;
112+
}
113+
}
114+
}
115+
interceptor.Ignore();
116+
}
117+
91118
private void OnRequestInterceptCallback(WebHttpRequestInterceptor interceptor)
92119
{
93120
var url = interceptor.Url;

0 commit comments

Comments
 (0)