Merge branch 'webview-js-retval'

Integrate GSoC 2017 work by Jose Lorenzo on allowing returning values
from JavaScript code via wxWebView::RunScript().
This commit is contained in:
Vadim Zeitlin
2017-11-04 14:33:42 +01:00
15 changed files with 931 additions and 49 deletions

View File

@@ -0,0 +1,70 @@
///////////////////////////////////////////////////////////////////////////////
// Name: wx/gtk/private/webkit.h
// Purpose: wxWebKitGtk RAII wrappers declaration
// Author: Jose Lorenzo
// Created: 2017-08-21
// Copyright: (c) 2017 Jose Lorenzo <josee.loren@gmail.com>
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#ifndef _WX_GTK_PRIVATE_WEBKIT_H_
#define _WX_GTK_PRIVATE_WEBKIT_H_
#include "wx/buffer.h"
#include <webkit2/webkit2.h>
#include <JavaScriptCore/JSStringRef.h>
// ----------------------------------------------------------------------------
// RAII wrapper of WebKitJavascriptResult taking care of freeing it
// ----------------------------------------------------------------------------
class wxWebKitJavascriptResult
{
public:
explicit wxWebKitJavascriptResult(WebKitJavascriptResult *r)
: m_jsresult(r)
{
}
~wxWebKitJavascriptResult()
{
webkit_javascript_result_unref(m_jsresult);
}
operator WebKitJavascriptResult *() const { return m_jsresult; }
private:
WebKitJavascriptResult *m_jsresult;
wxDECLARE_NO_COPY_CLASS(wxWebKitJavascriptResult);
};
// ----------------------------------------------------------------------------
// RAII wrapper of JSStringRef, also providing conversion to wxString
// ----------------------------------------------------------------------------
class wxJSStringRef
{
public:
explicit wxJSStringRef(JSStringRef r) : m_jssref(r) { }
~wxJSStringRef() { JSStringRelease(m_jssref); }
wxString ToWxString() const
{
const size_t length = JSStringGetMaximumUTF8CStringSize(m_jssref);
wxCharBuffer str(length);
JSStringGetUTF8CString(m_jssref, str.data(), length);
return wxString::FromUTF8(str);
}
private:
JSStringRef m_jssref;
wxDECLARE_NO_COPY_CLASS(wxJSStringRef);
};
#endif // _WX_GTK_PRIVATE_WEBKIT_H_

View File

@@ -114,7 +114,7 @@ public:
virtual wxString GetSelectedSource() const wxOVERRIDE;
virtual void ClearSelection() wxOVERRIDE;
virtual void RunScript(const wxString& javascript) wxOVERRIDE;
virtual bool RunScript(const wxString& javascript, wxString* output = NULL) wxOVERRIDE;
//Virtual Filesystem Support
virtual void RegisterHandler(wxSharedPtr<wxWebViewHandler> handler) wxOVERRIDE;
@@ -160,6 +160,7 @@ private:
#if wxUSE_WEBVIEW_WEBKIT2
bool CanExecuteEditingCommand(const gchar* command) const;
void SetupWebExtensionServer();
bool RunScriptSync(const wxString& javascript, wxString* output = NULL);
#endif
WebKitWebView *m_web_view;