Skip to content

Commit

Permalink
[NUI] Fix the handler's garbage collected exception in WebView
Browse files Browse the repository at this point in the history
  • Loading branch information
dongsug-song committed Aug 22, 2024
1 parent 251b62e commit fcc01aa
Showing 1 changed file with 48 additions and 4 deletions.
52 changes: 48 additions & 4 deletions src/Tizen.NUI/src/public/WebView/WebView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ static WebView()

private PlainTextReceivedCallback plainTextReceivedCallback;


/// <summary>
/// Creates a WebView.
/// </summary>
Expand Down Expand Up @@ -232,6 +233,27 @@ protected override void Dispose(DisposeTypes type)
//Called by User
//Release your own managed resources here.
//You should release all of your own disposable objects here.

if(handlerRootMap != null)
{
foreach (string key in handlerRootMap?.Keys)
{
Interop.WebView.AddJavaScriptMessageHandler(SwigCPtr, key, new HandleRef(null, IntPtr.Zero));
}
handlerRootMap?.Clear();
handlerRootMap = null;
}

if(_addJavaScriptEntireMessageHandlerMap != null)
{
foreach (string key in _addJavaScriptEntireMessageHandlerMap?.Keys)
{
Interop.WebView.AddJavaScriptEntireMessageHandler(SwigCPtr, key, new HandleRef(null, IntPtr.Zero));
}
_addJavaScriptEntireMessageHandlerMap?.Clear();
_addJavaScriptEntireMessageHandlerMap = null;
}

BackForwardList.Dispose();
Settings.Dispose();
}
Expand Down Expand Up @@ -2443,6 +2465,9 @@ public void EvaluateJavaScript(string script)
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}

private Dictionary<int, JavaScriptMessageHandler> _evaluateJavaScriptHandlerMap = new Dictionary<int, JavaScriptMessageHandler>();
private int _evaluateJavaScriptCallbackId = 0;

/// <summary>
/// Evaluates JavaScript code represented as a string.
/// </summary>
Expand All @@ -2451,7 +2476,14 @@ public void EvaluateJavaScript(string script)
[EditorBrowsable(EditorBrowsableState.Never)]
public void EvaluateJavaScript(string script, JavaScriptMessageHandler handler)
{
System.IntPtr ip = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(handler);
var id = ++_evaluateJavaScriptCallbackId;
JavaScriptMessageHandler wrapper = (msg) =>
{
handler(msg);
_evaluateJavaScriptHandlerMap.Remove(id);
};
_evaluateJavaScriptHandlerMap.Add(id, wrapper);
System.IntPtr ip = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(wrapper);
Interop.WebView.EvaluateJavaScript(SwigCPtr, script, new global::System.Runtime.InteropServices.HandleRef(this, ip));
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
Expand All @@ -2466,17 +2498,21 @@ public void AddJavaScriptMessageHandler(string objectName, JavaScriptMessageHand
{
if (handlerRootMap.ContainsKey(objectName))
{
return;
handlerRootMap[objectName] = handler;
}
else
{
handlerRootMap.Add(objectName, handler);
}

handlerRootMap.Add(objectName, handler);

System.IntPtr ip = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(handler);
Interop.WebView.AddJavaScriptMessageHandler(SwigCPtr, objectName, new System.Runtime.InteropServices.HandleRef(this, ip));

if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}

private Dictionary<string, JavaScriptEntireMessageHandler> _addJavaScriptEntireMessageHandlerMap = new Dictionary<string, JavaScriptEntireMessageHandler>();

/// <summary>
/// Add a message handler into the WebView.
/// </summary>
Expand All @@ -2485,6 +2521,14 @@ public void AddJavaScriptMessageHandler(string objectName, JavaScriptMessageHand
[EditorBrowsable(EditorBrowsableState.Never)]
public void AddJavaScriptMessageHandler(string objectName, JavaScriptEntireMessageHandler handler)
{
if(_addJavaScriptEntireMessageHandlerMap.ContainsKey(objectName))
{
_addJavaScriptEntireMessageHandlerMap[objectName] = handler;
}
else
{
_addJavaScriptEntireMessageHandlerMap.Add(objectName, handler);
}
System.IntPtr ip = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(handler);
Interop.WebView.AddJavaScriptEntireMessageHandler(SwigCPtr, objectName, new System.Runtime.InteropServices.HandleRef(this, ip));
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
Expand Down

0 comments on commit fcc01aa

Please sign in to comment.