[evolution/wip/webkit2] Merge prefer-plain and text-hightlight web extensions into the core EWebView web extension



commit 18cf2e3f7f37e609b0871950c835517d8d56aadc
Author: Milan Crha <mcrha redhat com>
Date:   Tue Mar 8 18:33:57 2016 +0100

    Merge prefer-plain and text-hightlight web extensions into the core EWebView web extension

 configure.ac                                       |    2 -
 e-util/e-web-view.c                                |  120 ++++++++++
 e-util/e-web-view.h                                |    8 +
 mail/e-mail-display-popup-extension.c              |    7 +-
 mail/e-mail-display-popup-extension.h              |    6 +-
 mail/e-mail-display.c                              |   31 ++--
 modules/prefer-plain/Makefile.am                   |    2 +-
 .../e-mail-display-popup-prefer-plain.c            |  216 +++---------------
 modules/prefer-plain/web-extension/Makefile.am     |   24 --
 .../module-prefer-plain-web-extension.c            |  175 ---------------
 .../module-prefer-plain-web-extension.h            |   26 ---
 modules/text-highlight/Makefile.am                 |    2 -
 .../e-mail-display-popup-text-highlight.c          |  235 ++++---------------
 modules/text-highlight/web-extension/Makefile.am   |   24 --
 .../module-text-highlight-web-extension.c          |  175 ---------------
 .../module-text-highlight-web-extension.h          |   26 ---
 web-extensions/e-dom-utils.c                       |   59 +++++
 web-extensions/e-dom-utils.h                       |    5 +
 web-extensions/e-web-extension.c                   |   57 +++++
 19 files changed, 359 insertions(+), 841 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index bef310e..d93559c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1611,12 +1611,10 @@ modules/plugin-lib/Makefile
 modules/plugin-manager/Makefile
 modules/prefer-plain/Makefile
 modules/prefer-plain/plugin/Makefile
-modules/prefer-plain/web-extension/Makefile
 modules/settings/Makefile
 modules/spamassassin/Makefile
 modules/startup-wizard/Makefile
 modules/text-highlight/Makefile
-modules/text-highlight/web-extension/Makefile
 modules/tnef-attachment/Makefile
 modules/vcard-inline/Makefile
 modules/web-inspector/Makefile
diff --git a/e-util/e-web-view.c b/e-util/e-web-view.c
index 5e4c0c4..93f278c 100644
--- a/e-util/e-web-view.c
+++ b/e-util/e-web-view.c
@@ -4131,3 +4131,123 @@ e_web_view_add_css_rule_into_style_sheet (EWebView *web_view,
                        NULL);
        }
 }
+
+/**
+ * e_web_view_get_document_uri_from_point:
+ * @web_view: an #EWebView
+ * @x: x-coordinate
+ * @y: y-coordinate
+ *
+ * Returns: A document URI which is under the @x, @y coordinates or %NULL,
+ * if there is none. Free the returned pointer with g_free() when done with it.
+ *
+ * Since: 3.22
+ **/
+gchar *
+e_web_view_get_document_uri_from_point (EWebView *web_view,
+                                       gint32 x,
+                                       gint32 y)
+{
+       GDBusProxy *web_extension;
+       GVariant *result;
+       GError *local_error = NULL;
+
+       g_return_val_if_fail (E_IS_WEB_VIEW (web_view), NULL);
+
+       web_extension = e_web_view_get_web_extension_proxy (web_view);
+       if (!web_extension)
+               return NULL;
+
+       result = g_dbus_proxy_call_sync (
+               web_extension,
+               "GetDocumentURIFromPoint",
+               g_variant_new (
+                       "(tii)",
+                       webkit_web_view_get_page_id (WEBKIT_WEB_VIEW (web_view)),
+                       x,
+                       y),
+               G_DBUS_CALL_FLAGS_NONE,
+               -1,
+               NULL,
+               &local_error);
+
+       if (local_error)
+               g_warning ("%s: Failed with error: %s", G_STRFUNC, local_error->message);
+
+       g_clear_error (&local_error);
+
+       if (result) {
+               gchar *uri = NULL;
+
+               g_variant_get (result, "(s)", &uri);
+               g_variant_unref (result);
+
+               if (g_strcmp0 (uri, "") == 0) {
+                       g_free (uri);
+                       uri = NULL;
+               }
+
+               return uri;
+       }
+
+       return NULL;
+}
+
+static void
+e_web_view_set_document_iframe_src_done_cb (GObject *source_object,
+                                           GAsyncResult *result,
+                                           gpointer user_data)
+{
+       GVariant *variant;
+       GError *local_error = NULL;
+
+       variant = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object), result, &local_error);
+       if (variant)
+               g_variant_unref (variant);
+
+       if (local_error)
+               g_warning ("%s: Failed with error: %s", G_STRFUNC, local_error->message);
+
+       g_clear_error (&local_error);
+}
+
+/**
+ * e_web_view_set_document_iframe_src:
+ * @web_view: an #EWebView
+ * @document_uri: a document URI for whose IFrame change the source
+ * @new_iframe_src: the source to change the IFrame to
+ *
+ * Change IFrame source for the given @document_uri IFrame
+ * to the @new_iframe_src.
+ *
+ * Since: 3.22
+ **/
+void
+e_web_view_set_document_iframe_src (EWebView *web_view,
+                                   const gchar *document_uri,
+                                   const gchar *new_iframe_src)
+{
+       GDBusProxy *web_extension;
+
+       g_return_if_fail (E_IS_WEB_VIEW (web_view));
+
+       web_extension = e_web_view_get_web_extension_proxy (web_view);
+       if (!web_extension)
+               return;
+
+       /* Cannot call this synchronously, blocking the local main loop, because the reload
+          can on the WebProcess side can be asking for a redirection policy, waiting
+          for a response which may be waiting in the blocked main loop. */
+       g_dbus_proxy_call (
+               web_extension,
+               "SetDocumentIFrameSrc",
+               g_variant_new (
+                       "(tss)",
+                       webkit_web_view_get_page_id (WEBKIT_WEB_VIEW (web_view)),
+                       document_uri,
+                       new_iframe_src),
+               G_DBUS_CALL_FLAGS_NONE,
+               -1,
+               NULL,
+               e_web_view_set_document_iframe_src_done_cb, NULL);
+}
diff --git a/e-util/e-web-view.h b/e-util/e-web-view.h
index 38d5e6c..fb54a90 100644
--- a/e-util/e-web-view.h
+++ b/e-util/e-web-view.h
@@ -248,6 +248,14 @@ void               e_web_view_add_css_rule_into_style_sheet
                                                 const gchar *style);
 const gchar *  e_web_view_get_citation_color_for_level
                                                (gint level);
+gchar *                e_web_view_get_document_uri_from_point
+                                               (EWebView *web_view,
+                                                gint32 x,
+                                                gint32 y);
+void           e_web_view_set_document_iframe_src
+                                               (EWebView *web_view,
+                                                const gchar *document_uri,
+                                                const gchar *new_iframe_src);
 G_END_DECLS
 
 #endif /* E_WEB_VIEW_H */
diff --git a/mail/e-mail-display-popup-extension.c b/mail/e-mail-display-popup-extension.c
index 5c5f891..2f9e919 100644
--- a/mail/e-mail-display-popup-extension.c
+++ b/mail/e-mail-display-popup-extension.c
@@ -33,13 +33,14 @@ e_mail_display_popup_extension_default_init (EMailDisplayPopupExtensionInterface
  * e_mail_display_popup_extension_update_actions:
  *
  * @extension: An object derived from #EMailDisplayPopupExtension
- * @context: A #WebKitHitTestResult describing context of the popup menu
+ * @popup_document_uri: Document URI on top of which the popup menu had been invoked
  *
  * When #EMailDisplay is about to display a popup menu, it calls this function
  * on every extension so that they can add their items to the menu.
  */
 void
-e_mail_display_popup_extension_update_actions (EMailDisplayPopupExtension *extension)
+e_mail_display_popup_extension_update_actions (EMailDisplayPopupExtension *extension,
+                                              const gchar *popup_document_uri)
 {
        EMailDisplayPopupExtensionInterface *iface;
 
@@ -48,5 +49,5 @@ e_mail_display_popup_extension_update_actions (EMailDisplayPopupExtension *exten
        iface = E_MAIL_DISPLAY_POPUP_EXTENSION_GET_INTERFACE (extension);
        g_return_if_fail (iface->update_actions != NULL);
 
-       iface->update_actions (extension);
+       iface->update_actions (extension, popup_document_uri);
 }
diff --git a/mail/e-mail-display-popup-extension.h b/mail/e-mail-display-popup-extension.h
index 964b0c5..bedd23e 100644
--- a/mail/e-mail-display-popup-extension.h
+++ b/mail/e-mail-display-popup-extension.h
@@ -47,13 +47,15 @@ typedef struct _EMailDisplayPopupExtensionInterface EMailDisplayPopupExtensionIn
 struct _EMailDisplayPopupExtensionInterface {
        GTypeInterface parent_interface;
 
-       void    (*update_actions)               (EMailDisplayPopupExtension *extension);
+       void    (*update_actions)               (EMailDisplayPopupExtension *extension,
+                                                const gchar *popup_document_uri);
 };
 
 GType          e_mail_display_popup_extension_get_type (void);
 
 void           e_mail_display_popup_extension_update_actions
-                                                       (EMailDisplayPopupExtension *extension);
+                                                       (EMailDisplayPopupExtension *extension,
+                                                        const gchar *popup_document_uri);
 
 G_END_DECLS
 
diff --git a/mail/e-mail-display.c b/mail/e-mail-display.c
index 92955c0..332f54b 100644
--- a/mail/e-mail-display.c
+++ b/mail/e-mail-display.c
@@ -1482,26 +1482,29 @@ static gboolean
 mail_display_button_press_event (GtkWidget *widget,
                                  GdkEventButton *event)
 {
-       EWebView *web_view = E_WEB_VIEW (widget);
-       GList *list, *link;
+       if (event->button == 3) {
+               EWebView *web_view = E_WEB_VIEW (widget);
+               gchar *popup_document_uri;
+               GList *list, *link;
 
-       if (event->button != 3)
-               goto chainup;
+               popup_document_uri = e_web_view_get_document_uri_from_point (web_view, event->x, event->y);
 
-       list = e_extensible_list_extensions (
-               E_EXTENSIBLE (web_view), E_TYPE_EXTENSION);
-       for (link = list; link != NULL; link = g_list_next (link)) {
-               EExtension *extension = link->data;
+               list = e_extensible_list_extensions (
+                       E_EXTENSIBLE (web_view), E_TYPE_EXTENSION);
+               for (link = list; link != NULL; link = g_list_next (link)) {
+                       EExtension *extension = link->data;
 
-               if (!E_IS_MAIL_DISPLAY_POPUP_EXTENSION (extension))
-                       continue;
+                       if (!E_IS_MAIL_DISPLAY_POPUP_EXTENSION (extension))
+                               continue;
 
-               e_mail_display_popup_extension_update_actions (
-                       E_MAIL_DISPLAY_POPUP_EXTENSION (extension));
+                       e_mail_display_popup_extension_update_actions (
+                               E_MAIL_DISPLAY_POPUP_EXTENSION (extension), popup_document_uri);
+               }
+
+               g_list_free (list);
+               g_free (popup_document_uri);
        }
-       g_list_free (list);
 
-chainup:
        /* Chain up to parent's button_press_event() method. */
        return GTK_WIDGET_CLASS (e_mail_display_parent_class)->
                button_press_event (widget, event);
diff --git a/modules/prefer-plain/Makefile.am b/modules/prefer-plain/Makefile.am
index 99ec301..9b6c8b3 100644
--- a/modules/prefer-plain/Makefile.am
+++ b/modules/prefer-plain/Makefile.am
@@ -1,4 +1,4 @@
-SUBDIRS=plugin web-extension
+SUBDIRS=plugin
 
 module_LTLIBRARIES = module-prefer-plain.la
 
diff --git a/modules/prefer-plain/e-mail-display-popup-prefer-plain.c 
b/modules/prefer-plain/e-mail-display-popup-prefer-plain.c
index d916e40..21fc1ce 100644
--- a/modules/prefer-plain/e-mail-display-popup-prefer-plain.c
+++ b/modules/prefer-plain/e-mail-display-popup-prefer-plain.c
@@ -23,8 +23,6 @@
 #include <shell/e-shell-window.h>
 #include "mail/e-mail-browser.h"
 
-#include "web-extension/module-prefer-plain-web-extension.h"
-
 #include <libebackend/libebackend.h>
 
 #include <glib/gi18n-lib.h>
@@ -39,11 +37,9 @@ struct _EMailDisplayPopupPreferPlain {
 
        gchar *text_plain_id;
        gchar *text_html_id;
+       gchar *document_uri;
 
        GtkActionGroup *action_group;
-
-       GDBusProxy *web_extension;
-       gint web_extension_watch_name_id;
 };
 
 struct _EMailDisplayPopupPreferPlainClass {
@@ -93,79 +89,18 @@ static const gchar *ui_reader =
 "</ui>";
 
 static void
-web_extension_proxy_created_cb (GDBusProxy *proxy,
-                                GAsyncResult *result,
-                                EMailDisplayPopupPreferPlain *pp_extension)
-{
-       GError *error = NULL;
-
-       pp_extension->web_extension = g_dbus_proxy_new_finish (result, &error);
-       if (!pp_extension->web_extension) {
-               g_warning ("Error creating web extension proxy: %s\n", error->message);
-               g_error_free (error);
-       }
-}
-
-static void
-web_extension_appeared_cb (GDBusConnection *connection,
-                           const gchar *name,
-                           const gchar *name_owner,
-                           EMailDisplayPopupPreferPlain *pp_extension)
-{
-       g_dbus_proxy_new (
-               connection,
-               G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START |
-               G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
-               G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
-               NULL,
-               name,
-               MODULE_PREFER_PLAIN_WEB_EXTENSION_OBJECT_PATH,
-               MODULE_PREFER_PLAIN_WEB_EXTENSION_INTERFACE,
-               NULL,
-               (GAsyncReadyCallback)web_extension_proxy_created_cb,
-               pp_extension);
-}
-
-static void
-web_extension_vanished_cb (GDBusConnection *connection,
-                           const gchar *name,
-                           EMailDisplayPopupPreferPlain *pp_extension)
-{
-       g_clear_object (&pp_extension->web_extension);
-}
-
-static void
-mail_display_popup_prefer_plain_watch_web_extension (EMailDisplayPopupPreferPlain *pp_extension)
-{
-       pp_extension->web_extension_watch_name_id =
-               g_bus_watch_name (
-                       G_BUS_TYPE_SESSION,
-                       MODULE_PREFER_PLAIN_WEB_EXTENSION_SERVICE_NAME,
-                       G_BUS_NAME_WATCHER_FLAGS_NONE,
-                       (GBusNameAppearedCallback) web_extension_appeared_cb,
-                       (GBusNameVanishedCallback) web_extension_vanished_cb,
-                       pp_extension, NULL);
-}
-
-static void
-toggle_part_get_document_uri_cb (GDBusProxy *web_extension,
-                                 GAsyncResult *result,
-                                 EMailDisplayPopupExtension *extension)
+toggle_part (GtkAction *action,
+             EMailDisplayPopupExtension *extension)
 {
        EMailDisplayPopupPreferPlain *pp_extension = (EMailDisplayPopupPreferPlain *) extension;
        SoupURI *soup_uri;
        GHashTable *query;
        gchar *uri;
-       GVariant *result_variant;
 
-       result_variant = g_dbus_proxy_call_finish (web_extension, result, NULL);
-       if (result_variant) {
-               const gchar *document_uri;
+       if (!pp_extension->document_uri)
+               return;
 
-               g_variant_get (result_variant, "(&s)", &document_uri);
-               soup_uri = soup_uri_new (document_uri);
-               g_variant_unref (result_variant);
-       }
+       soup_uri = soup_uri_new (pp_extension->document_uri);
 
        if (!soup_uri || !soup_uri->query) {
                if (soup_uri)
@@ -191,40 +126,12 @@ toggle_part_get_document_uri_cb (GDBusProxy *web_extension,
        uri = soup_uri_to_string (soup_uri, FALSE);
        soup_uri_free (soup_uri);
 
-       g_dbus_proxy_call (
-               pp_extension->web_extension,
-               "ChangeIFrameSource",
-               g_variant_new ("(s)", uri),
-               G_DBUS_CALL_FLAGS_NONE,
-               -1,
-               NULL,
-               NULL,
-               NULL);
+       e_web_view_set_document_iframe_src (E_WEB_VIEW (e_extension_get_extensible (E_EXTENSION (extension))),
+               pp_extension->document_uri, uri);
 
        g_free (uri);
 }
 
-static void
-toggle_part (GtkAction *action,
-             EMailDisplayPopupExtension *extension)
-{
-       EMailDisplayPopupPreferPlain *pp_extension = (EMailDisplayPopupPreferPlain *) extension;
-
-       if (!pp_extension->web_extension)
-               return;
-
-       /* Get URI from saved document */
-       g_dbus_proxy_call (
-               pp_extension->web_extension,
-               "GetDocumentURI",
-               NULL,
-               G_DBUS_CALL_FLAGS_NONE,
-               -1,
-               NULL,
-               (GAsyncReadyCallback) toggle_part_get_document_uri_cb,
-               extension);
-}
-
 GtkActionEntry entries[] = {
 
        { "show-plain-text-part",
@@ -262,6 +169,17 @@ set_text_html_id (EMailDisplayPopupPreferPlain *extension,
        extension->text_html_id = g_strdup (id);
 }
 
+static void
+set_document_uri (EMailDisplayPopupPreferPlain *extension,
+                  const gchar *document_uri)
+{
+       if (extension->document_uri == document_uri)
+               return;
+
+       g_free (extension->document_uri);
+       extension->document_uri = g_strdup (document_uri);
+}
+
 static GtkActionGroup *
 create_group (EMailDisplayPopupExtension *extension)
 {
@@ -310,11 +228,11 @@ create_group (EMailDisplayPopupExtension *extension)
 }
 
 static void
-get_document_uri_cb (GDBusProxy *web_extension,
-                     GAsyncResult *result,
-                     EMailDisplayPopupExtension *extension)
+mail_display_popup_prefer_plain_update_actions (EMailDisplayPopupExtension *extension,
+                                               const gchar *popup_document_uri)
 {
        EMailDisplay *display;
+       EMailDisplayPopupPreferPlain *pp_extension;
        GtkAction *action;
        gchar *part_id, *pos, *prefix;
        SoupURI *soup_uri;
@@ -322,24 +240,23 @@ get_document_uri_cb (GDBusProxy *web_extension,
        EMailPartList *part_list;
        gboolean is_text_plain;
        const gchar *action_name;
-       EMailDisplayPopupPreferPlain *pp_extension;
        GQueue queue = G_QUEUE_INIT;
        GList *head, *link;
-       GVariant *result_variant;
 
        display = E_MAIL_DISPLAY (e_extension_get_extensible (
                        E_EXTENSION (extension)));
 
        pp_extension = E_MAIL_DISPLAY_POPUP_PREFER_PLAIN (extension);
 
-       result_variant = g_dbus_proxy_call_finish (web_extension, result, NULL);
-       if (result_variant) {
-               const gchar *document_uri;
+       if (!pp_extension->action_group)
+               pp_extension->action_group = create_group (extension);
 
-               g_variant_get (result_variant, "(&s)", &document_uri);
-               soup_uri = soup_uri_new (document_uri);
-               g_variant_unref (result_variant);
-       }
+       set_document_uri (pp_extension, popup_document_uri);
+
+       if (pp_extension->document_uri)
+               soup_uri = soup_uri_new (pp_extension->document_uri);
+       else
+               soup_uri = NULL;
 
        if (!soup_uri || !soup_uri->query) {
                gtk_action_group_set_visible (pp_extension->action_group, FALSE);
@@ -437,60 +354,6 @@ get_document_uri_cb (GDBusProxy *web_extension,
        soup_uri_free (soup_uri);
 }
 
-static void
-mail_display_popup_prefer_plain_update_actions (EMailDisplayPopupExtension *extension)
-{
-       EMailDisplay *display;
-       EMailDisplayPopupPreferPlain *pp_extension;
-       gint32 x, y;
-       GdkDeviceManager *device_manager;
-       GdkDevice *pointer;
-
-       display = E_MAIL_DISPLAY (e_extension_get_extensible (
-                       E_EXTENSION (extension)));
-
-       pp_extension = E_MAIL_DISPLAY_POPUP_PREFER_PLAIN (extension);
-
-       if (!pp_extension->action_group)
-               pp_extension->action_group = create_group (extension);
-
-       /* In WK2 you can't get the node on what WebKitHitTest was performed,
-        * we have to use other way */
-       device_manager = gdk_display_get_device_manager (
-               gtk_widget_get_display (GTK_WIDGET(display)));
-       pointer = gdk_device_manager_get_client_pointer (device_manager);
-       gdk_window_get_device_position (
-               gtk_widget_get_window (GTK_WIDGET (display)), pointer, &x, &y, NULL);
-
-       if (!pp_extension->web_extension)
-                       return;
-
-       g_dbus_proxy_call (
-               pp_extension->web_extension,
-               "SaveDocumentFromPoint",
-               g_variant_new (
-                       "(tii)",
-                       webkit_web_view_get_page_id (
-                               WEBKIT_WEB_VIEW (display)),
-                       x, y),
-               G_DBUS_CALL_FLAGS_NONE,
-               -1,
-               NULL,
-               NULL,
-               NULL);
-
-       /* Get URI from saved document */
-       g_dbus_proxy_call (
-               pp_extension->web_extension,
-               "GetDocumentURI",
-               NULL,
-               G_DBUS_CALL_FLAGS_NONE,
-               -1,
-               NULL,
-               (GAsyncReadyCallback) get_document_uri_cb,
-               extension);
-}
-
 void
 e_mail_display_popup_prefer_plain_type_register (GTypeModule *type_module)
 {
@@ -509,16 +372,8 @@ e_mail_display_popup_prefer_plain_dispose (GObject *object)
                extension->action_group = NULL;
        }
 
-       if (extension->web_extension_watch_name_id > 0) {
-               g_bus_unwatch_name (extension->web_extension_watch_name_id);
-               extension->web_extension_watch_name_id = 0;
-       }
-
-       g_clear_object (&extension->web_extension);
-
        /* Chain up to parent's dispose() method. */
-       G_OBJECT_CLASS (e_mail_display_popup_prefer_plain_parent_class)->
-               dispose (object);
+       G_OBJECT_CLASS (e_mail_display_popup_prefer_plain_parent_class)->dispose (object);
 }
 
 static void
@@ -530,10 +385,10 @@ e_mail_display_popup_prefer_plain_finalize (GObject *object)
 
        g_free (extension->text_html_id);
        g_free (extension->text_plain_id);
+       g_free (extension->document_uri);
 
        /* Chain up to parent's finalize() method. */
-       G_OBJECT_CLASS (e_mail_display_popup_prefer_plain_parent_class)->
-               finalize (object);
+       G_OBJECT_CLASS (e_mail_display_popup_prefer_plain_parent_class)->finalize (object);
 }
 
 static void
@@ -559,7 +414,6 @@ e_mail_display_popup_extension_interface_init (EMailDisplayPopupExtensionInterfa
 void
 e_mail_display_popup_prefer_plain_class_finalize (EMailDisplayPopupPreferPlainClass *class)
 {
-
 }
 
 static void
@@ -568,7 +422,5 @@ e_mail_display_popup_prefer_plain_init (EMailDisplayPopupPreferPlain *extension)
        extension->action_group = NULL;
        extension->text_html_id = NULL;
        extension->text_plain_id = NULL;
-       extension->web_extension = NULL;
-
-       mail_display_popup_prefer_plain_watch_web_extension (extension);
+       extension->document_uri = NULL;
 }
diff --git a/modules/text-highlight/Makefile.am b/modules/text-highlight/Makefile.am
index 2a0f45f..ab5df67 100644
--- a/modules/text-highlight/Makefile.am
+++ b/modules/text-highlight/Makefile.am
@@ -1,5 +1,3 @@
-SUBDIRS = web-extension
-
 module_LTLIBRARIES = module-text-highlight.la
 
 module_text_highlight_la_CPPFLAGS =                                    \
diff --git a/modules/text-highlight/e-mail-display-popup-text-highlight.c 
b/modules/text-highlight/e-mail-display-popup-text-highlight.c
index e790412..a28932e 100644
--- a/modules/text-highlight/e-mail-display-popup-text-highlight.c
+++ b/modules/text-highlight/e-mail-display-popup-text-highlight.c
@@ -23,8 +23,6 @@
 #include <shell/e-shell-window.h>
 #include "mail/e-mail-browser.h"
 
-#include "web-extension/module-text-highlight-web-extension.h"
-
 #include <libebackend/libebackend.h>
 
 #include <glib/gi18n-lib.h>
@@ -38,8 +36,8 @@ typedef struct _EMailDisplayPopupTextHighlight {
 
        GtkActionGroup *action_group;
 
-       GDBusProxy *web_extension;
-       gint web_extension_watch_name_id;
+       volatile gint updating;
+       gchar *document_uri;
 } EMailDisplayPopupTextHighlight;
 
 typedef struct _EMailDisplayPopupTextHighlightClass {
@@ -110,78 +108,35 @@ static GtkActionEntry entries[] = {
 };
 
 static void
-web_extension_proxy_created_cb (GDBusProxy *proxy,
-                                GAsyncResult *result,
-                                EMailDisplayPopupTextHighlight *th_extension)
-{
-       GError *error = NULL;
-
-       th_extension->web_extension = g_dbus_proxy_new_finish (result, &error);
-       if (!th_extension->web_extension) {
-               g_warning ("Error creating web extension proxy: %s\n", error->message);
-               g_error_free (error);
-       }
-}
-
-static void
-web_extension_appeared_cb (GDBusConnection *connection,
-                           const gchar *name,
-                           const gchar *name_owner,
-                           EMailDisplayPopupTextHighlight *th_extension)
+set_document_uri (EMailDisplayPopupTextHighlight *extension,
+                  const gchar *document_uri)
 {
-       g_dbus_proxy_new (
-               connection,
-               G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START |
-               G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
-               G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
-               NULL,
-               name,
-               MODULE_TEXT_HIGHLIGHT_WEB_EXTENSION_OBJECT_PATH,
-               MODULE_TEXT_HIGHLIGHT_WEB_EXTENSION_INTERFACE,
-               NULL,
-               (GAsyncReadyCallback)web_extension_proxy_created_cb,
-               th_extension);
-}
-
-static void
-web_extension_vanished_cb (GDBusConnection *connection,
-                           const gchar *name,
-                           EMailDisplayPopupTextHighlight *th_extension)
-{
-       g_clear_object (&th_extension->web_extension);
-}
+       if (extension->document_uri == document_uri)
+               return;
 
-static void
-mail_display_popup_prefer_plain_watch_web_extension (EMailDisplayPopupTextHighlight *th_extension)
-{
-       th_extension->web_extension_watch_name_id =
-               g_bus_watch_name (
-                       G_BUS_TYPE_SESSION,
-                       MODULE_TEXT_HIGHLIGHT_WEB_EXTENSION_SERVICE_NAME,
-                       G_BUS_NAME_WATCHER_FLAGS_NONE,
-                       (GBusNameAppearedCallback) web_extension_appeared_cb,
-                       (GBusNameVanishedCallback) web_extension_vanished_cb,
-                       th_extension, NULL);
+       g_free (extension->document_uri);
+       extension->document_uri = g_strdup (document_uri);
 }
 
 static void
-reformat_get_document_uri_cb (GDBusProxy *web_extension,
-                              GAsyncResult *result,
-                              GtkAction *action)
+reformat (GtkAction *old,
+          GtkAction *action,
+          gpointer user_data)
 {
+       EMailDisplayPopupTextHighlight *th_extension;
        SoupURI *soup_uri;
        GHashTable *query;
        gchar *uri;
-       GVariant *result_variant;
 
-       result_variant = g_dbus_proxy_call_finish (web_extension, result, NULL);
-       if (result_variant) {
-               const gchar *document_uri;
+       th_extension = E_MAIL_DISPLAY_POPUP_TEXT_HIGHLIGHT (user_data);
 
-               g_variant_get (result_variant, "(&s)", &document_uri);
-               soup_uri = soup_uri_new (document_uri);
-               g_variant_unref (result_variant);
-       }
+       if (g_atomic_int_get (&th_extension->updating))
+               return;
+
+       if (th_extension->document_uri)
+               soup_uri = soup_uri_new (th_extension->document_uri);
+       else
+               soup_uri = NULL;
 
        if (!soup_uri)
                return;
@@ -203,44 +158,12 @@ reformat_get_document_uri_cb (GDBusProxy *web_extension,
        uri = soup_uri_to_string (soup_uri, FALSE);
        soup_uri_free (soup_uri);
 
-       /* Get frame's window and from the window the actual <iframe> element */
-       g_dbus_proxy_call (
-               web_extension,
-               "ChangeIFrameSource",
-               g_variant_new ("(s)", uri),
-               G_DBUS_CALL_FLAGS_NONE,
-               -1,
-               NULL,
-               NULL,
-               NULL);
+       e_web_view_set_document_iframe_src (E_WEB_VIEW (e_extension_get_extensible (E_EXTENSION 
(th_extension))),
+               th_extension->document_uri, uri);
 
        g_free (uri);
 }
 
-static void
-reformat (GtkAction *old,
-          GtkAction *action,
-          gpointer user_data)
-{
-       EMailDisplayPopupTextHighlight *th_extension;
-
-       th_extension = E_MAIL_DISPLAY_POPUP_TEXT_HIGHLIGHT (user_data);
-
-       if (!th_extension->web_extension)
-               return;
-
-       /* Get URI from saved document */
-       g_dbus_proxy_call (
-               th_extension->web_extension,
-               "GetDocumentURI",
-               NULL,
-               G_DBUS_CALL_FLAGS_NONE,
-               -1,
-               NULL,
-               (GAsyncReadyCallback) reformat_get_document_uri_cb,
-               action);
-}
-
 static GtkActionGroup *
 create_group (EMailDisplayPopupExtension *extension)
 {
@@ -295,10 +218,12 @@ create_group (EMailDisplayPopupExtension *extension)
                                NULL, NULL, action_index);
                action_index++;
                gtk_action_group_add_action (group, GTK_ACTION (action));
-               g_signal_connect (
-                       action, "changed",
-                       G_CALLBACK (reformat), extension);
-               gtk_radio_action_set_group (action, radio_group);
+               if (radio_group)
+                       gtk_radio_action_set_group (action, radio_group);
+               else
+                       g_signal_connect (
+                               action, "changed",
+                               G_CALLBACK (reformat), extension);
                radio_group = gtk_radio_action_get_group (action);
 
                g_object_unref (action);
@@ -326,11 +251,13 @@ create_group (EMailDisplayPopupExtension *extension)
                                NULL, NULL, action_index);
                action_index++;
                gtk_action_group_add_action (group, GTK_ACTION (action));
-               g_signal_connect (
-                       action, "changed",
-                       G_CALLBACK (reformat), extension);
 
-               gtk_radio_action_set_group (action, radio_group);
+               if (radio_group)
+                       gtk_radio_action_set_group (action, radio_group);
+               else
+                       g_signal_connect (
+                               action, "changed",
+                               G_CALLBACK (reformat), extension);
                radio_group = gtk_radio_action_get_group (action);
 
                g_object_unref (action);
@@ -352,28 +279,27 @@ create_group (EMailDisplayPopupExtension *extension)
 }
 
 static void
-get_document_uri_cb (GDBusProxy *web_extension,
-                     GAsyncResult *result,
-                     EMailDisplayPopupTextHighlight *th_extension)
+update_actions (EMailDisplayPopupExtension *extension,
+               const gchar *popup_document_uri)
 {
-       GVariant *result_variant;
-       gchar *document_uri;
+       EMailDisplayPopupTextHighlight *th_extension;
 
-       result_variant = g_dbus_proxy_call_finish (web_extension, result, NULL);
-       if (result_variant) {
-               g_variant_get (result_variant, "(s)", &document_uri);
-               g_variant_unref (result_variant);
-       }
+       th_extension = E_MAIL_DISPLAY_POPUP_TEXT_HIGHLIGHT (extension);
+
+       if (!th_extension->action_group)
+               th_extension->action_group = create_group (extension);
+
+       set_document_uri (th_extension, popup_document_uri);
 
        /* If the part below context menu was made by text-highlight formatter,
         * then try to check what formatter it's using at the moment and set
         * it as active in the popup menu */
-       if (document_uri && strstr (document_uri, ".text-highlight") != NULL) {
+       if (th_extension->document_uri && strstr (th_extension->document_uri, ".text-highlight") != NULL) {
                SoupURI *soup_uri;
                gtk_action_group_set_visible (
                        th_extension->action_group, TRUE);
 
-               soup_uri = soup_uri_new (document_uri);
+               soup_uri = soup_uri_new (th_extension->document_uri);
                if (soup_uri && soup_uri->query) {
                        GHashTable *query = soup_form_decode (soup_uri->query);
                        gchar *highlighter;
@@ -384,82 +310,23 @@ get_document_uri_cb (GDBusProxy *web_extension,
                                        th_extension->action_group, highlighter);
                                if (action) {
                                        gint value;
+                                       g_atomic_int_add (&th_extension->updating, 1);
                                        g_object_get (
                                                G_OBJECT (action), "value",
                                                &value, NULL);
                                        gtk_radio_action_set_current_value (
                                                GTK_RADIO_ACTION (action), value);
+                                       g_atomic_int_add (&th_extension->updating, -1);
                                }
                        }
                        g_hash_table_destroy (query);
                }
 
-               if (soup_uri) {
+               if (soup_uri)
                        soup_uri_free (soup_uri);
-               }
-
        } else {
-               gtk_action_group_set_visible (
-                       th_extension->action_group, FALSE);
+               gtk_action_group_set_visible (th_extension->action_group, FALSE);
        }
-
-
-       g_free (document_uri);
-}
-
-static void
-update_actions (EMailDisplayPopupExtension *extension)
-{
-       EMailDisplay *display;
-       EMailDisplayPopupTextHighlight *th_extension;
-       gint32 x, y;
-       GdkDeviceManager *device_manager;
-       GdkDevice *pointer;
-
-       display = E_MAIL_DISPLAY (e_extension_get_extensible (
-                       E_EXTENSION (extension)));
-
-       th_extension = E_MAIL_DISPLAY_POPUP_TEXT_HIGHLIGHT (extension);
-
-       if (th_extension->action_group == NULL) {
-               th_extension->action_group = create_group (extension);
-       }
-
-       /* In WK2 you can't get the node on what WebKitHitTest was performed,
-        * we have to use other way */
-       device_manager = gdk_display_get_device_manager (
-               gtk_widget_get_display (GTK_WIDGET(display)));
-       pointer = gdk_device_manager_get_client_pointer (device_manager);
-       gdk_window_get_device_position (
-               gtk_widget_get_window (GTK_WIDGET (display)), pointer, &x, &y, NULL);
-
-       if (!th_extension->web_extension)
-                       return;
-
-       g_dbus_proxy_call (
-               th_extension->web_extension,
-               "SaveDocumentFromPoint",
-               g_variant_new (
-                       "(tii)",
-                       webkit_web_view_get_page_id (
-                               WEBKIT_WEB_VIEW (display)),
-                       x, y),
-               G_DBUS_CALL_FLAGS_NONE,
-               -1,
-               NULL,
-               NULL,
-               NULL);
-
-       /* Get URI from saved document */
-       g_dbus_proxy_call (
-               th_extension->web_extension,
-               "GetDocumentURI",
-               NULL,
-               G_DBUS_CALL_FLAGS_NONE,
-               -1,
-               NULL,
-               (GAsyncReadyCallback) get_document_uri_cb,
-               th_extension);
 }
 
 void
@@ -488,13 +355,11 @@ e_mail_display_popup_extension_interface_init (EMailDisplayPopupExtensionInterfa
 void
 e_mail_display_popup_text_highlight_class_finalize (EMailDisplayPopupTextHighlightClass *klass)
 {
-
 }
 
 static void
 e_mail_display_popup_text_highlight_init (EMailDisplayPopupTextHighlight *extension)
 {
        extension->action_group = NULL;
-
-       mail_display_popup_prefer_plain_watch_web_extension (extension);
+       extension->document_uri = NULL;
 }
diff --git a/web-extensions/e-dom-utils.c b/web-extensions/e-dom-utils.c
index 96af6cf..35fbe0e 100644
--- a/web-extensions/e-dom-utils.c
+++ b/web-extensions/e-dom-utils.c
@@ -1924,3 +1924,62 @@ dom_get_node_inner_html (WebKitDOMNode *node)
 
        return inner_html;
 }
+
+WebKitDOMDocument *
+e_dom_utils_find_document_with_uri (WebKitDOMDocument *root_document,
+                                   const gchar *find_document_uri)
+{
+       WebKitDOMDocument *res_document = NULL;
+       GSList *todo;
+
+       g_return_val_if_fail (WEBKIT_DOM_IS_DOCUMENT (root_document), NULL);
+       g_return_val_if_fail (find_document_uri != NULL, NULL);
+
+       todo = g_slist_append (NULL, root_document);
+
+       while (todo) {
+               WebKitDOMDocument *document;
+               WebKitDOMHTMLCollection *frames;
+               gchar *document_uri;
+               gint ii, length;
+
+               document = todo->data;
+               todo = g_slist_remove (todo, document);
+
+               document_uri = webkit_dom_document_get_document_uri (document);
+               if (g_strcmp0 (document_uri, find_document_uri) == 0) {
+                       g_free (document_uri);
+                       res_document = document;
+                       break;
+               }
+
+               g_free (document_uri);
+
+               frames = webkit_dom_document_get_elements_by_tag_name_as_html_collection (document, "iframe");
+               length = webkit_dom_html_collection_get_length (frames);
+
+               /* Add rules to every sub document */
+               for (ii = 0; ii < length; ii++) {
+                       WebKitDOMDocument *content_document;
+                       WebKitDOMNode *node;
+
+                       node = webkit_dom_html_collection_item (frames, ii);
+                       content_document =
+                               webkit_dom_html_iframe_element_get_content_document (
+                                       WEBKIT_DOM_HTML_IFRAME_ELEMENT (node));
+
+                       if (!content_document)
+                               continue;
+
+                       todo = g_slist_prepend (todo, content_document);
+
+                       g_object_unref (node);
+               }
+
+               g_object_unref (frames);
+       }
+
+       g_slist_free (todo);
+
+       return res_document;
+}
diff --git a/web-extensions/e-dom-utils.h b/web-extensions/e-dom-utils.h
index 986d47e..f25f603 100644
--- a/web-extensions/e-dom-utils.h
+++ b/web-extensions/e-dom-utils.h
@@ -164,6 +164,11 @@ WebKitDOMElement *
 
 gchar *                dom_get_node_inner_html         (WebKitDOMNode *node);
 
+WebKitDOMDocument *
+               e_dom_utils_find_document_with_uri
+                                               (WebKitDOMDocument *root_document,
+                                                const gchar *find_document_uri);
+
 G_END_DECLS
 
 #endif /* E_DOM_UTILS_H */
diff --git a/web-extensions/e-web-extension.c b/web-extensions/e-web-extension.c
index d362072..d7db7c5 100644
--- a/web-extensions/e-web-extension.c
+++ b/web-extensions/e-web-extension.c
@@ -32,6 +32,9 @@
 #include "e-dom-utils.h"
 #include "e-web-extension-names.h"
 
+#define WEBKIT_DOM_USE_UNSTABLE_API
+#include <webkitdom/WebKitDOMDOMWindowUnstable.h>
+
 #define E_WEB_EXTENSION_GET_PRIVATE(obj) \
        (G_TYPE_INSTANCE_GET_PRIVATE \
        ((obj), E_TYPE_WEB_EXTENSION, EWebExtensionPrivate))
@@ -126,6 +129,17 @@ static const char introspection_xml[] =
 "      <arg type='s' name='button_id' direction='in'/>"
 "      <arg type='s' name='src' direction='in'/>"
 "    </method>"
+"    <method name='GetDocumentURIFromPoint'>"
+"      <arg type='t' name='page_id' direction='in'/>"
+"      <arg type='i' name='x' direction='in'/>"
+"      <arg type='i' name='y' direction='in'/>"
+"      <arg type='s' name='document_uri' direction='out'/>"
+"    </method>"
+"    <method name='SetDocumentIFrameSrc'>"
+"      <arg type='t' name='page_id' direction='in'/>"
+"      <arg type='s' name='document_uri' direction='in'/>"
+"      <arg type='s' name='new_iframe_src' direction='in'/>"
+"    </method>"
 "    <property type='b' name='NeedInput' access='readwrite'/>"
 "    <property type='b' name='ForceImageLoad' access='readwrite'/>"
 "  </interface>"
@@ -386,6 +400,49 @@ handle_method_call (GDBusConnection *connection,
                e_dom_utils_module_vcard_inline_set_iframe_src (document, button_id, src);
 
                g_dbus_method_invocation_return_value (invocation, NULL);
+       } else if (g_strcmp0 (method_name, "GetDocumentURIFromPoint") == 0) {
+               WebKitDOMDocument *document_at_point;
+               gchar *document_uri = NULL;
+               gint32 xx = 0, yy = 0;
+
+               g_variant_get (parameters, "(tii)", &page_id, &xx, &yy);
+               web_page = get_webkit_web_page_or_return_dbus_error (invocation, web_extension, page_id);
+               if (!web_page)
+                       return;
+
+               document = webkit_web_page_get_dom_document (web_page);
+               document_at_point = e_dom_utils_get_document_from_point (document, xx, yy);
+
+               if (document_at_point)
+                       document_uri = webkit_dom_document_get_document_uri (document_at_point);
+
+               g_dbus_method_invocation_return_value (
+                       invocation,
+                       g_variant_new ("(@s)", g_variant_new_take_string (document_uri ? document_uri : 
g_strdup (""))));
+       } else if (g_strcmp0 (method_name, "SetDocumentIFrameSrc") == 0) {
+               const gchar *document_uri = NULL, *new_iframe_src = NULL;
+               WebKitDOMDocument *iframe_document;
+
+               g_variant_get (parameters, "(t&s&s)", &page_id, &document_uri, &new_iframe_src);
+               web_page = get_webkit_web_page_or_return_dbus_error (invocation, web_extension, page_id);
+               if (!web_page)
+                       return;
+
+               document = webkit_web_page_get_dom_document (web_page);
+               iframe_document = e_dom_utils_find_document_with_uri (document, document_uri);
+
+               if (iframe_document) {
+                       WebKitDOMDOMWindow *window;
+                       WebKitDOMElement *frame_element;
+
+                       /* Get frame's window and from the window the actual <iframe> element */
+                       window = webkit_dom_document_get_default_view (iframe_document);
+                       frame_element = webkit_dom_dom_window_get_frame_element (window);
+                       webkit_dom_html_iframe_element_set_src (
+                               WEBKIT_DOM_HTML_IFRAME_ELEMENT (frame_element), new_iframe_src);
+               }
+
+               g_dbus_method_invocation_return_value (invocation, NULL);
        }
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]