Merge wxWebView JavaScript improvements branch

This is a squashed commit of the SOC2017_WEBVIEW_JS branch from
https://github.com/joseeloren/wxWidgets.git

Closes https://github.com/wxWidgets/wxWidgets/pull/538
This commit is contained in:
Jose Lorenzo
2017-09-02 23:24:06 +01:00
committed by Vadim Zeitlin
parent 9f4f075034
commit af8f7f33c3
14 changed files with 757 additions and 49 deletions

View File

@@ -19,7 +19,13 @@
#include "wx/base64.h"
#include "wx/log.h"
#include "wx/gtk/private/webview_webkit2_extension.h"
#include "wx/gtk/private/string.h"
#include "wx/gtk/private/webkit.h"
#include "wx/gtk/private/error.h"
#include "wx/private/jsscriptwrapper.h"
#include <webkit2/webkit2.h>
#include <JavaScriptCore/JSValueRef.h>
#include <JavaScriptCore/JSStringRef.h>
// ----------------------------------------------------------------------------
// GTK callbacks
@@ -1093,13 +1099,81 @@ wxString wxWebViewWebKit::GetPageText() const
return wxString();
}
void wxWebViewWebKit::RunScript(const wxString& javascript)
static void wxgtk_run_javascript_cb(WebKitWebView *,
GAsyncResult *res,
GAsyncResult **res_out)
{
g_object_ref(res);
*res_out = res;
}
bool JSResultToString(GObject *object, GAsyncResult *result, wxString* output)
{
wxGtkError error;
wxWebKitJavascriptResult js_result(webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW (object),
(GAsyncResult *)result, error.Out()));
if ( !js_result )
{
wxLogWarning(_("Error running Javascript: %s"), error.GetMessage());
return false;
}
JSGlobalContextRef context = webkit_javascript_result_get_global_context (&*js_result);
JSValueRef value = webkit_javascript_result_get_value (&*js_result);
JSValueRef exception = NULL;
wxJSStringRef js_value(JSValueIsObject(context, value) ?
JSValueCreateJSONString(context, value, 0, &exception) :
JSValueToStringCopy (context, value, &exception));
if ( exception )
{
wxJSStringRef ex_value(JSValueToStringCopy(context, exception, NULL));
wxLogWarning(_("Exception running Javascript: %s"), ex_value.ToWxString());
return false;
}
if ( output != NULL )
*output = js_value.ToWxString();
return true;
}
bool wxWebViewWebKit::RunScriptInternal(const wxString& javascript, wxString* output)
{
GAsyncResult *result = NULL;
webkit_web_view_run_javascript(m_web_view,
javascript.mb_str(wxConvUTF8),
javascript,
NULL,
NULL,
NULL);
(GAsyncReadyCallback)wxgtk_run_javascript_cb,
&result);
GMainContext *main_context = g_main_context_get_thread_default();
while ( !result )
g_main_context_iteration(main_context, TRUE);
return JSResultToString((GObject*)m_web_view, result, output);
}
bool wxWebViewWebKit::RunScript(const wxString& javascript, wxString* output)
{
wxJSScriptWrapper wrapJS(javascript, &m_runScriptCount);
wxString result;
bool isValidJS = RunScriptInternal(wrapJS.GetWrappedCode(), &result);
if ( isValidJS && result == "true" )
{
RunScriptInternal(wrapJS.GetOutputCode(), output);
RunScriptInternal(wrapJS.GetCleanUpCode());
return true;
}
wxLogWarning(_("Javascript error: %s"), result);
return false;
}
void wxWebViewWebKit::RegisterHandler(wxSharedPtr<wxWebViewHandler> handler)