Skip to content

Commit

Permalink
Android WeakRef
Browse files Browse the repository at this point in the history
  • Loading branch information
Eilon committed Jul 11, 2024
1 parent 215b7bb commit 2929e41
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,20 @@ protected override AWebView CreatePlatformView()

private sealed class HybridWebViewJavaScriptInterface : Java.Lang.Object
{
private readonly HybridWebViewHandler _hybridWebViewHandler;
private readonly WeakReference<HybridWebViewHandler> _hybridWebViewHandler;

public HybridWebViewJavaScriptInterface(HybridWebViewHandler hybridWebViewHandler)
{
_hybridWebViewHandler = hybridWebViewHandler;
_hybridWebViewHandler = new(hybridWebViewHandler);
}

private HybridWebViewHandler? Handler => _hybridWebViewHandler is not null && _hybridWebViewHandler.TryGetTarget(out var h) ? h : null;

[JavascriptInterface]
[Export("sendRawMessage")]
public void SendRawMessage(string message)
{
_hybridWebViewHandler.VirtualView?.RawMessageReceived(message);
Handler?.VirtualView?.RawMessageReceived(message);
}
}

Expand Down
22 changes: 17 additions & 5 deletions src/Core/src/Platform/Android/MauiHybridWebViewClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,22 @@ namespace Microsoft.Maui.Platform
{
public class MauiHybridWebViewClient : WebViewClient
{
private readonly HybridWebViewHandler _handler;
private readonly WeakReference<HybridWebViewHandler> _handler;

public MauiHybridWebViewClient(HybridWebViewHandler handler)
{
_handler = handler;
_handler = new(handler);
}

private HybridWebViewHandler? Handler => _handler is not null && _handler.TryGetTarget(out var h) ? h : null;

public override WebResourceResponse? ShouldInterceptRequest(AWebView? view, IWebResourceRequest? request)
{
if (Handler is null)
{
return base.ShouldInterceptRequest(view, request);
}

var fullUrl = request?.Url?.ToString();
var requestUri = HybridWebViewQueryStringHelper.RemovePossibleQueryString(fullUrl);

Expand All @@ -31,7 +38,7 @@ public MauiHybridWebViewClient(HybridWebViewHandler handler)
string contentType;
if (string.IsNullOrEmpty(relativePath))
{
relativePath = _handler.DefaultFile;
relativePath = Handler.DefaultFile;
contentType = "text/html";
}
else
Expand All @@ -43,7 +50,7 @@ public MauiHybridWebViewClient(HybridWebViewHandler handler)
}
}

var assetPath = Path.Combine(_handler.HybridRoot!, relativePath!);
var assetPath = Path.Combine(Handler.HybridRoot!, relativePath!);
var contentStream = PlatformOpenAppPackageFile(assetPath);

if (contentStream is null)
Expand All @@ -69,11 +76,16 @@ public MauiHybridWebViewClient(HybridWebViewHandler handler)

private Stream? PlatformOpenAppPackageFile(string filename)
{
if (Handler is null)
{
return null;
}

filename = PathUtils.NormalizePath(filename);

try
{
return _handler.Context.Assets?.Open(filename);
return Handler.Context.Assets?.Open(filename);
}
catch (Java.IO.FileNotFoundException)
{
Expand Down

0 comments on commit 2929e41

Please sign in to comment.