Only warn about missing WebKit extension when used

Don't log failure to load the extension during initialization, because
it isn't needed for many uses of wxWebView. Instead, only report the
failure at the time when functionality depending on it is used.

Also use g_warning() for logging consistently with other failures in
this file. This doesn't interrupt the user, yet shows the problem highly
visibly in the console.
This commit is contained in:
Václav Slavík
2020-10-05 16:41:26 +02:00
parent 2388f5d33f
commit b355e00149
2 changed files with 40 additions and 31 deletions

View File

@@ -162,6 +162,7 @@ private:
#if wxUSE_WEBVIEW_WEBKIT2 #if wxUSE_WEBVIEW_WEBKIT2
bool CanExecuteEditingCommand(const gchar* command) const; bool CanExecuteEditingCommand(const gchar* command) const;
void SetupWebExtensionServer(); void SetupWebExtensionServer();
GDBusProxy *GetExtensionProxy() const;
bool RunScriptSync(const wxString& javascript, wxString* output = NULL); bool RunScriptSync(const wxString& javascript, wxString* output = NULL);
#endif #endif

View File

@@ -399,6 +399,14 @@ static bool CheckDirectoryForWebExt(const wxString& dirname)
return false; return false;
} }
static wxString GetStandardWebExtensionsDir()
{
wxString dir = wxDynamicLibrary::GetPluginsDirectory();
if ( !dir.empty() )
dir += "/web-extensions";
return dir;
}
static void static void
wxgtk_initialize_web_extensions(WebKitWebContext *context, wxgtk_initialize_web_extensions(WebKitWebContext *context,
GDBusServer *dbusServer) GDBusServer *dbusServer)
@@ -409,40 +417,23 @@ wxgtk_initialize_web_extensions(WebKitWebContext *context,
// The first value is the location in which the extension is supposed to be // The first value is the location in which the extension is supposed to be
// normally installed, while the other three are used as fallbacks to allow // normally installed, while the other three are used as fallbacks to allow
// running the tests and sample using wxWebView before installing it. // running the tests and sample using wxWebView before installing it.
wxString normalLocation = wxDynamicLibrary::GetPluginsDirectory();
if ( !normalLocation.empty() )
normalLocation += "/web-extensions";
wxString const directories[] = wxString const directories[] =
{ {
normalLocation, GetStandardWebExtensionsDir(),
"..", "..",
"../..", "../..",
"lib", "lib",
}; };
wxString dir;
for ( size_t n = 0; n < WXSIZEOF(directories); ++n ) for ( size_t n = 0; n < WXSIZEOF(directories); ++n )
{ {
if ( !directories[n].empty() && CheckDirectoryForWebExt(directories[n]) ) if ( !directories[n].empty() && CheckDirectoryForWebExt(directories[n]) )
{ {
dir = directories[n]; webkit_web_context_set_web_extensions_directory(context, directories[n].utf8_str());
break; break;
} }
} }
if ( !dir.empty() )
{
webkit_web_context_set_web_extensions_directory(context, dir.utf8_str());
}
else
{
wxLogWarning(_("Web extension not found in \"%s\", "
"some wxWebView functionality will be not available"),
directories[0]);
}
webkit_web_context_set_web_extensions_initialization_user_data(context, webkit_web_context_set_web_extensions_initialization_user_data(context,
user_data); user_data);
} }
@@ -1025,10 +1016,11 @@ bool wxWebViewWebKit::IsEditable() const
void wxWebViewWebKit::DeleteSelection() void wxWebViewWebKit::DeleteSelection()
{ {
if (m_extension) GDBusProxy *extension = GetExtensionProxy();
if (extension)
{ {
guint64 page_id = webkit_web_view_get_page_id(m_web_view); guint64 page_id = webkit_web_view_get_page_id(m_web_view);
GVariant *retval = g_dbus_proxy_call_sync(m_extension, GVariant *retval = g_dbus_proxy_call_sync(extension,
"DeleteSelection", "DeleteSelection",
g_variant_new("(t)", page_id), g_variant_new("(t)", page_id),
G_DBUS_CALL_FLAGS_NONE, -1, G_DBUS_CALL_FLAGS_NONE, -1,
@@ -1042,10 +1034,11 @@ void wxWebViewWebKit::DeleteSelection()
bool wxWebViewWebKit::HasSelection() const bool wxWebViewWebKit::HasSelection() const
{ {
if (m_extension) GDBusProxy *extension = GetExtensionProxy();
if (extension)
{ {
guint64 page_id = webkit_web_view_get_page_id(m_web_view); guint64 page_id = webkit_web_view_get_page_id(m_web_view);
GVariant *retval = g_dbus_proxy_call_sync(m_extension, GVariant *retval = g_dbus_proxy_call_sync(extension,
"HasSelection", "HasSelection",
g_variant_new("(t)", page_id), g_variant_new("(t)", page_id),
G_DBUS_CALL_FLAGS_NONE, -1, G_DBUS_CALL_FLAGS_NONE, -1,
@@ -1069,10 +1062,11 @@ void wxWebViewWebKit::SelectAll()
wxString wxWebViewWebKit::GetSelectedText() const wxString wxWebViewWebKit::GetSelectedText() const
{ {
if (m_extension) GDBusProxy *extension = GetExtensionProxy();
if (extension)
{ {
guint64 page_id = webkit_web_view_get_page_id(m_web_view); guint64 page_id = webkit_web_view_get_page_id(m_web_view);
GVariant *retval = g_dbus_proxy_call_sync(m_extension, GVariant *retval = g_dbus_proxy_call_sync(extension,
"GetSelectedText", "GetSelectedText",
g_variant_new("(t)", page_id), g_variant_new("(t)", page_id),
G_DBUS_CALL_FLAGS_NONE, -1, G_DBUS_CALL_FLAGS_NONE, -1,
@@ -1090,10 +1084,11 @@ wxString wxWebViewWebKit::GetSelectedText() const
wxString wxWebViewWebKit::GetSelectedSource() const wxString wxWebViewWebKit::GetSelectedSource() const
{ {
if (m_extension) GDBusProxy *extension = GetExtensionProxy();
if (extension)
{ {
guint64 page_id = webkit_web_view_get_page_id(m_web_view); guint64 page_id = webkit_web_view_get_page_id(m_web_view);
GVariant *retval = g_dbus_proxy_call_sync(m_extension, GVariant *retval = g_dbus_proxy_call_sync(extension,
"GetSelectedSource", "GetSelectedSource",
g_variant_new("(t)", page_id), g_variant_new("(t)", page_id),
G_DBUS_CALL_FLAGS_NONE, -1, G_DBUS_CALL_FLAGS_NONE, -1,
@@ -1111,10 +1106,11 @@ wxString wxWebViewWebKit::GetSelectedSource() const
void wxWebViewWebKit::ClearSelection() void wxWebViewWebKit::ClearSelection()
{ {
if (m_extension) GDBusProxy *extension = GetExtensionProxy();
if (extension)
{ {
guint64 page_id = webkit_web_view_get_page_id(m_web_view); guint64 page_id = webkit_web_view_get_page_id(m_web_view);
GVariant *retval = g_dbus_proxy_call_sync(m_extension, GVariant *retval = g_dbus_proxy_call_sync(extension,
"ClearSelection", "ClearSelection",
g_variant_new("(t)", page_id), g_variant_new("(t)", page_id),
G_DBUS_CALL_FLAGS_NONE, -1, G_DBUS_CALL_FLAGS_NONE, -1,
@@ -1128,10 +1124,11 @@ void wxWebViewWebKit::ClearSelection()
wxString wxWebViewWebKit::GetPageText() const wxString wxWebViewWebKit::GetPageText() const
{ {
if (m_extension) GDBusProxy *extension = GetExtensionProxy();
if (extension)
{ {
guint64 page_id = webkit_web_view_get_page_id(m_web_view); guint64 page_id = webkit_web_view_get_page_id(m_web_view);
GVariant *retval = g_dbus_proxy_call_sync(m_extension, GVariant *retval = g_dbus_proxy_call_sync(extension,
"GetPageText", "GetPageText",
g_variant_new("(t)", page_id), g_variant_new("(t)", page_id),
G_DBUS_CALL_FLAGS_NONE, -1, G_DBUS_CALL_FLAGS_NONE, -1,
@@ -1415,4 +1412,15 @@ void wxWebViewWebKit::SetupWebExtensionServer()
g_object_unref(observer); g_object_unref(observer);
} }
GDBusProxy *wxWebViewWebKit::GetExtensionProxy() const
{
if (!m_extension)
{
g_warning("Web extension not found in \"%s\", "
"some wxWebView functionality will be not available",
(const char*)GetStandardWebExtensionsDir().utf8_str());
}
return m_extension;
}
#endif // wxUSE_WEBVIEW && wxUSE_WEBVIEW_WEBKIT2 #endif // wxUSE_WEBVIEW && wxUSE_WEBVIEW_WEBKIT2