From 48ebcf76720314058905e84c776eba3880977a29 Mon Sep 17 00:00:00 2001 From: younghajung <35090305+younghajung@users.noreply.github.com> Date: Mon, 27 Apr 2020 13:24:38 +0900 Subject: [PATCH] [WebView] Add new WebView API, Tizen.WebView.EvalAsync (#1560) Signed-off-by: yh106.jung --- .../Interop/Interop.ChromiumEwk.View.cs | 3 +- src/Tizen.WebView/Tizen.WebView/WebView.cs | 40 +++++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) mode change 100644 => 100755 src/Tizen.WebView/Interop/Interop.ChromiumEwk.View.cs mode change 100644 => 100755 src/Tizen.WebView/Tizen.WebView/WebView.cs diff --git a/src/Tizen.WebView/Interop/Interop.ChromiumEwk.View.cs b/src/Tizen.WebView/Interop/Interop.ChromiumEwk.View.cs old mode 100644 new mode 100755 index ac64477..8e2c8c0 --- a/src/Tizen.WebView/Interop/Interop.ChromiumEwk.View.cs +++ b/src/Tizen.WebView/Interop/Interop.ChromiumEwk.View.cs @@ -78,7 +78,8 @@ internal static partial class Interop [return: MarshalAs(UnmanagedType.U1)] internal static extern bool ewk_view_forward_possible(IntPtr obj); - internal delegate void ScriptExcuteCallback(IntPtr obj, IntPtr resultValue, IntPtr userData); + [UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)] + internal delegate void ScriptExcuteCallback(IntPtr obj, IntPtr returnValue, IntPtr userData); [DllImport(Libraries.ChromiumEwk)] [return: MarshalAs(UnmanagedType.U1)] diff --git a/src/Tizen.WebView/Tizen.WebView/WebView.cs b/src/Tizen.WebView/Tizen.WebView/WebView.cs old mode 100644 new mode 100755 index dedaadb..27daf73 --- a/src/Tizen.WebView/Tizen.WebView/WebView.cs +++ b/src/Tizen.WebView/Tizen.WebView/WebView.cs @@ -17,8 +17,9 @@ using ElmSharp; using System; using System.Collections.Generic; -using System.Runtime.InteropServices; using System.ComponentModel; +using System.Runtime.InteropServices; +using System.Threading.Tasks; namespace Tizen.WebView { @@ -132,6 +133,9 @@ namespace Tizen.WebView private Context _context; private Settings _settings; + private IDictionary _evalCallbacks = new Dictionary(); + private int _evalCallbackId = 0; + // focus dummy private SmartEvent _focusIn; private SmartEvent _focusOut; @@ -479,6 +483,40 @@ namespace Tizen.WebView } /// + /// Requests the evaluation of the given script. + /// + /// The JavaScript code string to evaluate. + /// A task that contains the result of the evaluation as a string. + /// 8 + /// Thrown when a script is null or empty string. + public async Task EvalAsync(string script) + { + if (string.IsNullOrEmpty(script)) + { + throw new ArgumentException(nameof(script)); + } + + var tcs = new TaskCompletionSource(); + IntPtr id = IntPtr.Zero; + + lock (_evalCallbacks) + { + id = (IntPtr)_evalCallbackId++; + _evalCallbacks[id] = (obj, returnValue, userData) => + { + tcs.SetResult(Marshal.PtrToStringAnsi(returnValue)); + lock (_evalCallbacks) + { + _evalCallbacks.Remove(userData); + } + }; + } + + Interop.ChromiumEwk.ewk_view_script_execute(_realHandle, script, _evalCallbacks[id], id); + return await tcs.Task; + } + + /// /// Requests to set or unset a view as the currently focused one. /// /// 'true' to set the focus on the view, 'false' to remove the focus from the view. -- 2.7.4