[epiphany] Use user a script message instead of a DBus signal for RemoveItemFromOverview



commit cc89a4362495bf7d102b3d325e6fa1098a2d29b4
Author: Carlos Garcia Campos <cgarcia igalia com>
Date:   Fri Nov 21 14:58:23 2014 +0100

    Use user a script message instead of a DBus signal for RemoveItemFromOverview

 embed/ephy-embed-shell.c                 |   46 ++++++----------
 embed/ephy-embed-utils.c                 |   22 +++++++
 embed/ephy-embed-utils.h                 |    2 +
 embed/web-extension/ephy-web-extension.c |   48 ----------------
 embed/web-extension/ephy-web-overview.c  |   91 ------------------------------
 src/resources/overview.html              |    2 +-
 6 files changed, 42 insertions(+), 169 deletions(-)
---
diff --git a/embed/ephy-embed-shell.c b/embed/ephy-embed-shell.c
index 360bffd..a047d42 100644
--- a/embed/ephy-embed-shell.c
+++ b/embed/ephy-embed-shell.c
@@ -62,7 +62,6 @@ struct _EphyEmbedShellPrivate
   GList *web_extensions;
   guint web_extensions_page_created_signal_id;
   guint web_extensions_form_auth_save_signal_id;
-  guint web_extensions_remove_from_overview_signal_id;
   guint web_extensions_allow_tls_certificate_signal_id;
 };
 
@@ -246,23 +245,20 @@ history_set_url_hidden_cb (EphyHistoryService *service,
 }
 
 static void
-web_extension_remove_from_overview (GDBusConnection *connection,
-                                    const char *sender_name,
-                                    const char *object_path,
-                                    const char *interface_name,
-                                    const char *signal_name,
-                                    GVariant *parameters,
-                                    EphyEmbedShell *shell)
+web_extension_overview_message_received_cb (WebKitUserContentManager *manager,
+                                            WebKitJavascriptResult *message,
+                                            EphyEmbedShell *shell)
 {
-  const char *url_to_remove;
+  char *url_to_remove;
 
-  g_variant_get (parameters, "(&s)", &url_to_remove);
+  url_to_remove = ephy_embed_utils_get_js_result_as_string (message);
 
   shell->priv->hiding_overview_item++;
   ephy_history_service_set_url_hidden (shell->priv->global_history_service,
                                        url_to_remove, TRUE, NULL,
                                        (EphyHistoryJobCallback) history_set_url_hidden_cb,
                                        shell);
+  g_free (url_to_remove);
 
   if (shell->priv->update_overview_timeout_id > 0)
     g_source_remove (shell->priv->update_overview_timeout_id);
@@ -556,17 +552,6 @@ ephy_embed_shell_setup_web_extensions_connection (EphyEmbedShell *shell)
                                         (GDBusSignalCallback)web_extension_form_auth_save_requested,
                                         shell,
                                         NULL);
-  shell->priv->web_extensions_remove_from_overview_signal_id =
-    g_dbus_connection_signal_subscribe (shell->priv->bus,
-                                        NULL,
-                                        EPHY_WEB_EXTENSION_INTERFACE,
-                                        "RemoveItemFromOverview",
-                                        EPHY_WEB_EXTENSION_OBJECT_PATH,
-                                        NULL,
-                                        G_DBUS_SIGNAL_FLAGS_NONE,
-                                        (GDBusSignalCallback)web_extension_remove_from_overview,
-                                        shell,
-                                        NULL);
   shell->priv->web_extensions_allow_tls_certificate_signal_id =
     g_dbus_connection_signal_subscribe (shell->priv->bus,
                                         NULL,
@@ -619,6 +604,15 @@ ephy_embed_shell_startup (GApplication* application)
 
   ephy_embed_shell_setup_web_extensions_connection (shell);
 
+  /* User content manager */
+  shell->priv->user_content = webkit_user_content_manager_new ();
+
+  webkit_user_content_manager_register_script_message_handler (shell->priv->user_content,
+                                                               "overview");
+  g_signal_connect (shell->priv->user_content, "script-message-received::overview",
+                    G_CALLBACK (web_extension_overview_message_received_cb),
+                    shell);
+
   web_context = webkit_web_context_get_default ();
   ephy_embed_shell_setup_process_model (shell, web_context);
   g_signal_connect (web_context, "initialize-web-extensions",
@@ -668,6 +662,8 @@ ephy_embed_shell_shutdown (GApplication* application)
 
   G_APPLICATION_CLASS (ephy_embed_shell_parent_class)->shutdown (application);
 
+  webkit_user_content_manager_unregister_script_message_handler (priv->user_content, "overview");
+
   if (priv->web_extensions_page_created_signal_id > 0) {
     g_dbus_connection_signal_unsubscribe (priv->bus, priv->web_extensions_page_created_signal_id);
     priv->web_extensions_page_created_signal_id = 0;
@@ -678,11 +674,6 @@ ephy_embed_shell_shutdown (GApplication* application)
     priv->web_extensions_form_auth_save_signal_id = 0;
   }
 
-  if (priv->web_extensions_remove_from_overview_signal_id > 0) {
-    g_dbus_connection_signal_unsubscribe (priv->bus, priv->web_extensions_remove_from_overview_signal_id);
-    priv->web_extensions_remove_from_overview_signal_id = 0;
-  }
-
   if (priv->web_extensions_allow_tls_certificate_signal_id > 0) {
     g_dbus_connection_signal_unsubscribe (priv->bus, priv->web_extensions_allow_tls_certificate_signal_id);
     priv->web_extensions_allow_tls_certificate_signal_id = 0;
@@ -1046,8 +1037,5 @@ ephy_embed_shell_clear_cache (EphyEmbedShell *shell)
 WebKitUserContentManager *
 ephy_embed_shell_get_user_content_manager (EphyEmbedShell *shell)
 {
-  if (!shell->priv->user_content)
-    shell->priv->user_content = webkit_user_content_manager_new ();
-
   return shell->priv->user_content;
 }
diff --git a/embed/ephy-embed-utils.c b/embed/ephy-embed-utils.c
index 4d50777..8672ea5 100644
--- a/embed/ephy-embed-utils.c
+++ b/embed/ephy-embed-utils.c
@@ -32,6 +32,7 @@
 #include <string.h>
 #include <glib/gi18n.h>
 #include <libsoup/soup.h>
+#include <JavaScriptCore/JavaScript.h>
 
 static GRegex *non_search_regex;
 static GRegex *domain_regex;
@@ -324,6 +325,27 @@ ephy_embed_utils_urls_have_same_origin (const char *a_url,
   return retval;
 }
 
+char *
+ephy_embed_utils_get_js_result_as_string (WebKitJavascriptResult *js_result)
+{
+  JSValueRef js_value;
+  JSStringRef js_string;
+  size_t max_size;
+  char *retval = NULL;
+
+  js_value = webkit_javascript_result_get_value (js_result);
+  js_string = JSValueToStringCopy (webkit_javascript_result_get_global_context (js_result),
+                                   js_value, NULL);
+  max_size = JSStringGetMaximumUTF8CStringSize (js_string);
+  if (max_size) {
+    retval = g_malloc (max_size);
+    JSStringGetUTF8CString (js_string, retval, max_size);
+  }
+  JSStringRelease (js_string);
+
+  return retval;
+}
+
 void
 ephy_embed_utils_shutdown (void)
 {
diff --git a/embed/ephy-embed-utils.h b/embed/ephy-embed-utils.h
index 68b468a..ae8b02b 100644
--- a/embed/ephy-embed-utils.h
+++ b/embed/ephy-embed-utils.h
@@ -29,6 +29,7 @@
 #define EPHY_EMBED_UTILS_H
 
 #include "ephy-web-view.h"
+#include <webkit2/webkit2.h>
 
 G_BEGIN_DECLS
 
@@ -48,6 +49,7 @@ gboolean ephy_embed_utils_is_no_show_address                    (const char *add
 char    *ephy_embed_utils_get_title_from_address                (const char *address);
 gboolean ephy_embed_utils_urls_have_same_origin                 (const char *a_url,
                                                                  const char *b_url);
+char    *ephy_embed_utils_get_js_result_as_string               (WebKitJavascriptResult *js_result);
 void     ephy_embed_utils_shutdown                              (void);
 
 G_END_DECLS
diff --git a/embed/web-extension/ephy-web-extension.c b/embed/web-extension/ephy-web-extension.c
index 6705104..057faad 100644
--- a/embed/web-extension/ephy-web-extension.c
+++ b/embed/web-extension/ephy-web-extension.c
@@ -82,9 +82,6 @@ static const char introspection_xml[] =
   "   <arg type='s' name='hostname' direction='out'/>"
   "   <arg type='s' name='username' direction='out'/>"
   "  </signal>"
-  "  <signal name='RemoveItemFromOverview'>"
-  "   <arg type='s' name='url' direction='out'/>"
-  "  </signal>"
   "  <method name='FormAuthDataSaveConfirmationResponse'>"
   "   <arg type='u' name='request_id' direction='in'/>"
   "   <arg type='b' name='should_store' direction='in'/>"
@@ -281,29 +278,6 @@ request_decision_on_storing (EphyEmbedFormAuth *form_auth)
 }
 
 static void
-overview_item_removed (EphyWebOverview *overview,
-                       const char *url,
-                       EphyWebExtension *extension)
-{
-  GError *error = NULL;
-
-  if (!extension->priv->dbus_connection)
-    return;
-
-  g_dbus_connection_emit_signal (extension->priv->dbus_connection,
-                                 NULL,
-                                 EPHY_WEB_EXTENSION_OBJECT_PATH,
-                                 EPHY_WEB_EXTENSION_INTERFACE,
-                                 "RemoveItemFromOverview",
-                                 g_variant_new ("(s)", url),
-                                 &error);
-  if (error) {
-    g_debug ("Error emitting signal RemoveItemFromOverview: %s\n", error->message);
-    g_error_free (error);
-  }
-}
-
-static void
 should_store_cb (const char *username,
                  const char *password,
                  gpointer user_data)
@@ -1335,23 +1309,6 @@ prepare_certificate_exception_js (WebKitScriptWorld *world,
 }
 
 static void
-prepare_overview (WebKitScriptWorld *world,
-                  WebKitWebPage *web_page,
-                  WebKitFrame *frame,
-                  EphyWebExtension *extension)
-{
-  EphyWebOverview *overview;
-  JSGlobalContextRef context;
-
-  overview = ephy_web_overview_new (web_page, extension->priv->overview_model);
-  g_signal_connect (overview, "item-removed",
-                    G_CALLBACK (overview_item_removed),
-                    extension);
-  context = webkit_frame_get_javascript_context_for_script_world (frame, world);
-  ephy_web_overview_init_js (overview, context);
-}
-
-static void
 window_object_cleared_cb (WebKitScriptWorld *world,
                           WebKitWebPage     *web_page,
                           WebKitFrame       *frame,
@@ -1360,11 +1317,6 @@ window_object_cleared_cb (WebKitScriptWorld *world,
   WebKitDOMDocument *dom_document;
   char *dom_url;
 
-  if (g_strcmp0 (webkit_web_page_get_uri (web_page), "ephy-about:overview") == 0) {
-    prepare_overview (world, web_page, frame, extension);
-    return;
-  }
-
   dom_document = webkit_web_page_get_dom_document (web_page);
   dom_url = webkit_dom_document_get_url (dom_document);
 
diff --git a/embed/web-extension/ephy-web-overview.c b/embed/web-extension/ephy-web-overview.c
index 878fb5f..fe0c6b8 100644
--- a/embed/web-extension/ephy-web-overview.c
+++ b/embed/web-extension/ephy-web-overview.c
@@ -41,15 +41,6 @@ enum
   PROP_MODEL,
 };
 
-enum
-{
-  ITEM_REMOVED,
-
-  LAST_SIGNAL
-};
-
-static guint signals[LAST_SIGNAL];
-
 G_DEFINE_TYPE (EphyWebOverview, ephy_web_overview, G_TYPE_OBJECT)
 
 typedef struct {
@@ -462,15 +453,6 @@ ephy_web_overview_class_init (EphyWebOverviewClass *klass)
                                                         G_PARAM_CONSTRUCT_ONLY |
                                                         G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | 
G_PARAM_STATIC_BLURB));
 
-  signals[ITEM_REMOVED] =
-    g_signal_new ("item-removed",
-                  EPHY_TYPE_WEB_OVERVIEW,
-                  G_SIGNAL_RUN_LAST,
-                  0, NULL, NULL,
-                  g_cclosure_marshal_VOID__STRING,
-                  G_TYPE_NONE, 1,
-                  G_TYPE_STRING);
-
   g_type_class_add_private (object_class, sizeof(EphyWebOverviewPrivate));
 }
 
@@ -492,76 +474,3 @@ ephy_web_overview_new (WebKitWebPage *web_page,
                        "model", model,
                        NULL);
 }
-
-static JSValueRef
-remove_item_from_overview_cb (JSContextRef context,
-                              JSObjectRef function,
-                              JSObjectRef this_object,
-                              size_t argument_count,
-                              const JSValueRef arguments[],
-                              JSValueRef *exception)
-{
-  EphyWebOverview *overview;
-  JSStringRef result_string_js;
-  size_t max_size;
-  char *result_string;
-
-  overview = EPHY_WEB_OVERVIEW (JSObjectGetPrivate (this_object));
-
-  result_string_js = JSValueToStringCopy (context, arguments[0], NULL);
-  max_size = JSStringGetMaximumUTF8CStringSize (result_string_js);
-
-  result_string = g_malloc (max_size);
-  JSStringGetUTF8CString (result_string_js, result_string, max_size);
-  g_signal_emit (overview, signals[ITEM_REMOVED], 0, result_string);
-  JSStringRelease (result_string_js);
-  g_free (result_string);
-
-  return JSValueMakeUndefined (context);
-}
-
-static const JSStaticFunction overview_static_funcs[] =
-{
-  { "removeItemFromOverview", remove_item_from_overview_cb, kJSPropertyAttributeReadOnly | 
kJSPropertyAttributeDontDelete },
-  { NULL, NULL, 0 }
-};
-
-static const JSClassDefinition overview_class_def =
-{
-  0,                     /* version */
-  kJSClassAttributeNone, /* attributes */
-  "Overview",            /* className */
-  NULL,                  /* parentClass */
-  NULL,                  /* staticValues */
-  overview_static_funcs, /* staticFunctions */
-  NULL,                  /* initialize */
-  NULL,                  /* finalize */
-  NULL,                  /* hasProperty */
-  NULL,                  /* getProperty */
-  NULL,                  /* setProperty */
-  NULL,                  /* deleteProperty */
-  NULL,                  /* getPropertyNames */
-  NULL,                  /* callAsFunction */
-  NULL,                  /* callAsConstructor */
-  NULL,                  /* hasInstance */
-  NULL                   /* convertToType */
-};
-
-void
-ephy_web_overview_init_js (EphyWebOverview *overview,
-                           JSGlobalContextRef context)
-{
-  JSObjectRef global_object;
-  JSClassRef class_def;
-  JSObjectRef class_object;
-  JSStringRef str;
-
-  global_object = JSContextGetGlobalObject (context);
-
-  class_def = JSClassCreate (&overview_class_def);
-  class_object = JSObjectMake (context, class_def, overview);
-  str = JSStringCreateWithUTF8CString ("Overview");
-  JSObjectSetProperty (context, global_object, str, class_object, kJSPropertyAttributeNone, NULL);
-  JSStringRelease (str);
-  JSClassRelease (class_def);
-}
diff --git a/src/resources/overview.html b/src/resources/overview.html
index c22bb41..bf6ccd1 100755
--- a/src/resources/overview.html
+++ b/src/resources/overview.html
@@ -183,7 +183,7 @@
     var listItemNode = elem.parentElement;
     event.preventDefault();
     listItemNode.className +=" removed ";
-    Overview.removeItemFromOverview(elem.href);
+    window.webkit.messageHandlers.overview.postMessage(elem.href);
   }
   </script>
 


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