From 967b238b77c1912c33e1a508781d9124b9e351a7 Mon Sep 17 00:00:00 2001 From: Dan Vrátil Date: Wed, 8 Aug 2012 19:33:08 +0200 Subject: Remove all references to JavaScriptCore and use of JavaScript JavaScript is disabled in EWebView, so any attempt to evaluate a JavaScript code will fail. We are using DOM bindings instead to interact with the document. This commit removes some helper functions created in the early days of WebKit port which are not used anymore and also fixes mail-to-task plugin, which was relying on some JavaScript. --- configure.ac | 4 +- mail/Makefile.am | 2 - mail/e-mail-display.c | 84 +++++++++++--- mail/e-mail-display.h | 3 +- plugins/mail-to-task/mail-to-task.c | 5 +- widgets/misc/e-web-view.c | 222 +----------------------------------- widgets/misc/e-web-view.h | 14 --- 7 files changed, 75 insertions(+), 259 deletions(-) diff --git a/configure.ac b/configure.ac index 44e3dd5d49..af960211cc 100644 --- a/configure.ac +++ b/configure.ac @@ -39,7 +39,6 @@ m4_define([gnome_desktop_minimum_version], [2.91.3]) m4_define([gnome_icon_theme_minimum_version], [2.30.2.1]) m4_define([gsettings_desktop_schemas_minimum_version], [2.91.92]) m4_define([webkitgtk_minimum_version], [1.8.0]) -m4_define([javascriptcoregtk_minimum_version], [1.8.0]) m4_define([libgdata_minimum_version], [0.10]) m4_define([libsoup_minimum_version], [2.38.1]) m4_define([libxml_minimum_version], [2.7.3]) @@ -251,8 +250,7 @@ PKG_CHECK_MODULES([GNOME_PLATFORM], shared-mime-info >= shared_mime_info_minimum_version gnome-desktop-3.0 >= gnome_desktop_minimum_version gsettings-desktop-schemas >= gsettings_desktop_schemas_minimum_version - webkitgtk-3.0 >= webkitgtk_minimum_version - javascriptcoregtk-3.0 >= javascriptcoregtk_minimum_version]) + webkitgtk-3.0 >= webkitgtk_minimum_version]) AC_SUBST(GNOME_PLATFORM_CFLAGS) AC_SUBST(GNOME_PLATFORM_LIBS) diff --git a/mail/Makefile.am b/mail/Makefile.am index 09c274aad7..3cc30971f7 100644 --- a/mail/Makefile.am +++ b/mail/Makefile.am @@ -23,7 +23,6 @@ libevolution_mail_la_CPPFLAGS = \ $(CANBERRA_CFLAGS) \ $(CLUTTER_CFLAGS) \ $(GTKHTML_CFLAGS) \ - $(JAVASCRIPTCORE_CFLAGS) \ $(LIBSOUP_CFLAGS) \ -DEVOLUTION_DATADIR=\""$(datadir)"\" \ -DEVOLUTION_PRIVDATADIR=\""$(privdatadir)"\" \ @@ -228,7 +227,6 @@ libevolution_mail_la_LIBADD = \ $(CANBERRA_LIBS) \ $(CLUTTER_LIBS) \ $(GTKHTML_LIBS) \ - $(JAVASCRIPTCORE_CFLAGS) \ $(E_WIDGETS_LIBS) \ $(SMIME_LIBS) \ $(LIBSOUP_LIBS) \ diff --git a/mail/e-mail-display.c b/mail/e-mail-display.c index ac98b06e5a..2ca422c353 100644 --- a/mail/e-mail-display.c +++ b/mail/e-mail-display.c @@ -49,8 +49,6 @@ #include -#include - #define d(x) G_DEFINE_TYPE ( @@ -1832,32 +1830,80 @@ e_mail_display_set_status (EMailDisplay *display, gtk_widget_show_all (GTK_WIDGET (display)); } + +static gchar * +mail_display_get_frame_selection_text (WebKitDOMElement *iframe) +{ + WebKitDOMDocument *document; + WebKitDOMDOMWindow *window; + WebKitDOMDOMSelection *selection; + WebKitDOMNodeList *frames; + gulong ii, length; + + document = webkit_dom_html_iframe_element_get_content_document ( + WEBKIT_DOM_HTML_IFRAME_ELEMENT (iframe)); + window = webkit_dom_document_get_default_view (document); + selection = webkit_dom_dom_window_get_selection (window); + if (selection && (webkit_dom_dom_selection_get_range_count (selection) > 0)) { + WebKitDOMRange *range; + + range = webkit_dom_dom_selection_get_range_at (selection, 0, NULL); + if (range) { + return webkit_dom_range_to_string (range, NULL); + } + } + + frames = webkit_dom_document_get_elements_by_tag_name ( + document, "IFRAME"); + length = webkit_dom_node_list_get_length (frames); + for (ii = 0; ii < length; ii++) { + WebKitDOMNode *node; + gchar *text; + + node = webkit_dom_node_list_item (frames, ii); + + text = mail_display_get_frame_selection_text ( + WEBKIT_DOM_ELEMENT (node)); + + if (text) { + return text; + } + } + + return NULL; +} + gchar * -e_mail_display_get_selection_plain_text (EMailDisplay *display, - gint *len) +e_mail_display_get_selection_plain_text (EMailDisplay *display) { - EWebView *web_view; - WebKitWebFrame *frame; - const gchar *frame_name; - GValue value = {0}; - GType type; - const gchar *str; + WebKitDOMDocument *document; + WebKitDOMNodeList *frames; + gulong ii, length; g_return_val_if_fail (E_IS_MAIL_DISPLAY (display), NULL); - web_view = E_WEB_VIEW (display); - frame = webkit_web_view_get_focused_frame (WEBKIT_WEB_VIEW (web_view)); - frame_name = webkit_web_frame_get_name (frame); + if (!webkit_web_view_has_selection (WEBKIT_WEB_VIEW (display))) + return NULL; + + document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (display)); + frames = webkit_dom_document_get_elements_by_tag_name (document, "IFRAME"); + length = webkit_dom_node_list_get_length (frames); - type = e_web_view_frame_exec_script (web_view, frame_name, "window.getSelection().toString()", &value); - g_return_val_if_fail (type == G_TYPE_STRING, NULL); + for (ii = 0; ii < length; ii++) { + gchar *text; + WebKitDOMNode *node; - str = g_value_get_string (&value); + node = webkit_dom_node_list_item (frames, ii); - if (len) - *len = strlen (str); + text = mail_display_get_frame_selection_text ( + WEBKIT_DOM_ELEMENT (node)); - return g_strdup (str); + if (text) { + return text; + } + } + + return NULL; } void diff --git a/mail/e-mail-display.h b/mail/e-mail-display.h index e19895623a..48c16f36a2 100644 --- a/mail/e-mail-display.h +++ b/mail/e-mail-display.h @@ -95,8 +95,7 @@ void e_mail_display_set_status (EMailDisplay *display, const gchar *status); gchar * e_mail_display_get_selection_plain_text - (EMailDisplay *display, - gint *len); + (EMailDisplay *display); void e_mail_display_load_images (EMailDisplay *display); diff --git a/plugins/mail-to-task/mail-to-task.c b/plugins/mail-to-task/mail-to-task.c index 3014388e4c..2992fc961f 100644 --- a/plugins/mail-to-task/mail-to-task.c +++ b/plugins/mail-to-task/mail-to-task.c @@ -1068,16 +1068,15 @@ get_selected_text (EMailReader *reader) { EMailDisplay *display; gchar *text = NULL; - gint len; display = e_mail_reader_get_mail_display (reader); if (!e_web_view_is_selection_active (E_WEB_VIEW (display))) return NULL; - text = e_mail_display_get_selection_plain_text (display, &len); + text = e_mail_display_get_selection_plain_text (display); - if (text == NULL || !text_contains_nonwhitespace (text, len)) { + if (text == NULL || !text_contains_nonwhitespace (text, strlen (text))) { g_free (text); return NULL; } diff --git a/widgets/misc/e-web-view.c b/widgets/misc/e-web-view.c index ddac836f26..b1d726dab6 100644 --- a/widgets/misc/e-web-view.c +++ b/widgets/misc/e-web-view.c @@ -24,8 +24,6 @@ #include -#include - #include #include #include @@ -61,8 +59,6 @@ struct _EWebViewPrivate { GdkPixbufAnimation *cursor_image; gchar *cursor_image_src; - GHashTable *js_callbacks; - GSList *highlights; GtkAction *open_proxy; @@ -807,11 +803,6 @@ web_view_dispose (GObject *object) priv->cursor_image_src = NULL; } - if (priv->js_callbacks != NULL) { - g_hash_table_destroy (priv->js_callbacks); - priv->js_callbacks = NULL; - } - if (priv->highlights != NULL) { g_slist_free_full (priv->highlights, g_free); priv->highlights = NULL; @@ -1739,9 +1730,6 @@ e_web_view_init (EWebView *web_view) ui_manager = gtk_ui_manager_new (); web_view->priv->ui_manager = ui_manager; - web_view->priv->js_callbacks = g_hash_table_new_full (g_str_hash, g_str_equal, - (GDestroyNotify) g_free, NULL); - g_signal_connect_swapped ( ui_manager, "connect-proxy", G_CALLBACK (web_view_connect_proxy_cb), web_view); @@ -2008,214 +1996,16 @@ e_web_view_frame_get_uri (EWebView *web_view, gchar * e_web_view_get_html (EWebView *web_view) { - GValue html = {0}; + WebKitDOMDocument *document; + WebKitDOMElement *element; g_return_val_if_fail (E_IS_WEB_VIEW (web_view), NULL); - if (e_web_view_exec_script (web_view, "return document.documentElement.innerHTML;", &html) == G_TYPE_STRING) - return g_strdup (g_value_get_string (&html)); - else - return NULL; -} - -JSGlobalContextRef -e_web_view_get_global_context (EWebView *web_view) -{ - WebKitWebFrame *main_frame; - - g_return_val_if_fail (E_IS_WEB_VIEW (web_view), 0); - - main_frame = webkit_web_view_get_main_frame (WEBKIT_WEB_VIEW (web_view)); - return webkit_web_frame_get_global_context (main_frame); -} - -GType -e_web_view_exec_script (EWebView *web_view, - const gchar *script, - GValue *value) -{ - WebKitWebFrame *main_frame; - - g_return_val_if_fail (E_IS_WEB_VIEW (web_view), G_TYPE_INVALID); - g_return_val_if_fail (script != NULL, G_TYPE_INVALID); - - main_frame = webkit_web_view_get_main_frame (WEBKIT_WEB_VIEW (web_view)); - - return e_web_view_frame_exec_script (web_view, - webkit_web_frame_get_name (main_frame), - script, value); -} - -GType -e_web_view_frame_exec_script (EWebView *web_view, - const gchar *frame_name, - const gchar *script, - GValue *value) -{ - WebKitWebFrame *main_frame, *frame; - JSGlobalContextRef context; - JSValueRef js_value, error = NULL; - JSType js_type; - JSStringRef js_script; - JSStringRef js_str; - size_t str_len; - gchar *str; - - g_return_val_if_fail (E_IS_WEB_VIEW (web_view), G_TYPE_INVALID); - g_return_val_if_fail (script != NULL, G_TYPE_INVALID); - - main_frame = webkit_web_view_get_main_frame (WEBKIT_WEB_VIEW (web_view)); - frame = webkit_web_frame_find_frame (main_frame, frame_name); - - context = webkit_web_frame_get_global_context (frame); - - js_script = JSStringCreateWithUTF8CString (script); - js_value = JSEvaluateScript (context, js_script, NULL, NULL, 0, &error); - JSStringRelease (js_script); - - if (error) { - gchar *msg; - js_str = JSValueToStringCopy (context, error, NULL); - str_len = JSStringGetLength (js_str); - - msg = g_malloc (str_len + 1); - JSStringGetUTF8CString (js_str, msg, str_len + 1); - JSStringRelease (js_str); - - g_message ("JavaScript Execution Failed: %s", msg); - g_free (msg); - - return G_TYPE_INVALID; - } - - if (!value) - return G_TYPE_NONE; - - js_type = JSValueGetType (context, js_value); - switch (js_type) { - case kJSTypeBoolean: - g_value_init (value, G_TYPE_BOOLEAN); - g_value_set_boolean (value, JSValueToBoolean (context, js_value)); - break; - case kJSTypeNumber: - g_value_init (value, G_TYPE_DOUBLE); - g_value_set_double (value, JSValueToNumber (context, js_value, NULL)); - break; - case kJSTypeString: - js_str = JSValueToStringCopy (context, js_value, NULL); - str_len = JSStringGetLength (js_str); - str = g_malloc (str_len + 1); - JSStringGetUTF8CString (js_str, str, str_len + 1); - JSStringRelease (js_str); - g_value_init (value, G_TYPE_STRING); - g_value_set_string (value, str); - g_free (str); - break; - case kJSTypeObject: - g_value_init (value, G_TYPE_OBJECT); - g_value_set_object (value, JSValueToObject (context, js_value, NULL)); - break; - case kJSTypeNull: - g_value_init (value, G_TYPE_POINTER); - g_value_set_pointer (value, NULL); - break; - case kJSTypeUndefined: - break; - } - - return G_VALUE_TYPE (value); -} - -static JSValueRef -web_view_handle_js_callback (JSContextRef ctx, - JSObjectRef function, - JSObjectRef this_object, - size_t argument_count, - const JSValueRef arguments[], - JSValueRef *exception) -{ - gpointer web_view; - gpointer user_data; - gchar *fnc_name; - size_t fnc_name_len; - - EWebViewJSFunctionCallback callback; - - JSStringRef js_webview_prop = JSStringCreateWithUTF8CString ("webview"); - JSStringRef js_userdata_prop = JSStringCreateWithUTF8CString ("user-data"); - JSStringRef js_fncname_prop = JSStringCreateWithUTF8CString ("fnc-name"); - JSStringRef js_fncname_str; - - JSValueRef js_webview = JSObjectGetProperty (ctx, function, js_webview_prop, NULL); - JSValueRef js_userdata = JSObjectGetProperty (ctx, function, js_userdata_prop, NULL); - JSValueRef js_fncname = JSObjectGetProperty (ctx, function, js_fncname_prop, NULL); - - web_view = GINT_TO_POINTER ((gint) JSValueToNumber (ctx, js_webview, NULL)); - user_data = GINT_TO_POINTER ((gint) JSValueToNumber (ctx, js_userdata, NULL)); - js_fncname_str = JSValueToStringCopy (ctx, js_fncname, NULL); - fnc_name_len = JSStringGetLength (js_fncname_str); - - g_return_val_if_fail (E_IS_WEB_VIEW (web_view), 0); - - /* Convert fncname to gchar* and lookup the callback in hashtable */ - fnc_name = g_malloc (fnc_name_len + 1); - JSStringGetUTF8CString (js_fncname_str, fnc_name, fnc_name_len + 1); - callback = g_hash_table_lookup (E_WEB_VIEW (web_view)->priv->js_callbacks, fnc_name); - - g_return_val_if_fail (callback != NULL, 0); - - /* Call the callback function */ - callback (E_WEB_VIEW (web_view), argument_count, arguments, user_data); - - JSStringRelease (js_fncname_str); - JSStringRelease (js_fncname_prop); - JSStringRelease (js_webview_prop); - JSStringRelease (js_userdata_prop); - - g_free (fnc_name); - - return 0; -} - -void -e_web_view_install_js_callback (EWebView *web_view, - const gchar *fnc_name, - EWebViewJSFunctionCallback callback, - gpointer user_data) -{ - WebKitWebFrame *frame; - JSGlobalContextRef ctx; - JSObjectRef global_obj, js_func; - JSStringRef js_fnc_name, js_webview_prop, js_userdata_prop, js_fncname_prop; + document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (web_view)); + element = webkit_dom_document_get_document_element (document); - g_return_if_fail (E_IS_WEB_VIEW (web_view)); - g_return_if_fail (fnc_name != NULL); - g_return_if_fail (callback != NULL); - - frame = webkit_web_view_get_main_frame (WEBKIT_WEB_VIEW (web_view)); - ctx = webkit_web_frame_get_global_context (frame); - global_obj = JSContextGetGlobalObject (ctx); - js_fnc_name = JSStringCreateWithUTF8CString (fnc_name); - js_func = JSObjectMakeFunctionWithCallback (ctx, NULL, - (JSObjectCallAsFunctionCallback) web_view_handle_js_callback); - js_webview_prop = JSStringCreateWithUTF8CString ("webview"); - js_userdata_prop = JSStringCreateWithUTF8CString ("user-data"); - js_fncname_prop = JSStringCreateWithUTF8CString ("fnc-name"); - - /* Set some properties to the function */ - JSObjectSetProperty (ctx, js_func, js_webview_prop, JSValueMakeNumber (ctx, GPOINTER_TO_INT (web_view)), 0, NULL); - JSObjectSetProperty (ctx, js_func, js_userdata_prop, JSValueMakeNumber (ctx, GPOINTER_TO_INT (user_data)), 0, NULL); - JSObjectSetProperty (ctx, js_func, js_fncname_prop, JSValueMakeString (ctx, js_fnc_name), 0, NULL); - - /* Set the function as a property of global object */ - JSObjectSetProperty (ctx, global_obj, js_fnc_name, js_func, 0, NULL); - - JSStringRelease (js_fncname_prop); - JSStringRelease (js_userdata_prop); - JSStringRelease (js_webview_prop); - JSStringRelease (js_fnc_name); - - g_hash_table_insert (web_view->priv->js_callbacks, g_strdup (fnc_name), callback); + return webkit_dom_html_element_get_outer_html ( + WEBKIT_DOM_HTML_ELEMENT (element)); } gboolean diff --git a/widgets/misc/e-web-view.h b/widgets/misc/e-web-view.h index 66e65c2df5..bb67a12404 100644 --- a/widgets/misc/e-web-view.h +++ b/widgets/misc/e-web-view.h @@ -28,7 +28,6 @@ #define E_WEB_VIEW_H #include -#include /* Standard GObject macros */ #define E_TYPE_WEB_VIEW \ @@ -125,19 +124,6 @@ void e_web_view_frame_load_uri (EWebView *web_view, const gchar *uri); const gchar * e_web_view_frame_get_uri (EWebView *web_view, const gchar *frame_name); -JSGlobalContextRef - e_web_view_get_global_context (EWebView *web_view); -GType e_web_view_exec_script (EWebView *web_view, - const gchar *script, - GValue *value); -GType e_web_view_frame_exec_script (EWebView *web_view, - const gchar *frame_name, - const gchar *script, - GValue *value); -void e_web_view_install_js_callback (EWebView *web_view, - const gchar *fnc_name, - EWebViewJSFunctionCallback callback, - gpointer user_data); gchar * e_web_view_get_html (EWebView *web_view); gboolean e_web_view_get_caret_mode (EWebView *web_view); void e_web_view_set_caret_mode (EWebView *web_view, -- cgit