From: dongsug.song Date: Wed, 21 Aug 2024 06:46:36 +0000 (+0900) Subject: [NUI] Fix the handler's garbage collected exception in WebView X-Git-Tag: submit/tizen/20240827.074151~1^2~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=bd8411fad1fb60e93ed496618faa970eefc9623a;p=platform%2Fcore%2Fcsapi%2Ftizenfx.git [NUI] Fix the handler's garbage collected exception in WebView --- diff --git a/src/Tizen.NUI/src/public/WebView/WebView.cs b/src/Tizen.NUI/src/public/WebView/WebView.cs index b0134213d..bcbe42dbf 100755 --- a/src/Tizen.NUI/src/public/WebView/WebView.cs +++ b/src/Tizen.NUI/src/public/WebView/WebView.cs @@ -162,6 +162,7 @@ namespace Tizen.NUI.BaseComponents private PlainTextReceivedCallback plainTextReceivedCallback; + /// /// Creates a WebView. /// @@ -232,6 +233,27 @@ namespace Tizen.NUI.BaseComponents //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(); } @@ -2443,6 +2465,9 @@ namespace Tizen.NUI.BaseComponents if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } + private Dictionary _evaluateJavaScriptHandlerMap = new Dictionary(); + private int _evaluateJavaScriptCallbackId = 0; + /// /// Evaluates JavaScript code represented as a string. /// @@ -2451,7 +2476,14 @@ namespace Tizen.NUI.BaseComponents [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(); } @@ -2464,12 +2496,7 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public void AddJavaScriptMessageHandler(string objectName, JavaScriptMessageHandler handler) { - if (handlerRootMap.ContainsKey(objectName)) - { - return; - } - - handlerRootMap.Add(objectName, handler); + handlerRootMap[objectName] = handler; System.IntPtr ip = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(handler); Interop.WebView.AddJavaScriptMessageHandler(SwigCPtr, objectName, new System.Runtime.InteropServices.HandleRef(this, ip)); @@ -2477,6 +2504,8 @@ namespace Tizen.NUI.BaseComponents if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); } + private Dictionary _addJavaScriptEntireMessageHandlerMap = new Dictionary(); + /// /// Add a message handler into the WebView. /// @@ -2485,6 +2514,7 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public void AddJavaScriptMessageHandler(string objectName, JavaScriptEntireMessageHandler handler) { + _addJavaScriptEntireMessageHandlerMap[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();