[Xfce4-commits] <midori:master> Move extension loading/ activating out of main for testing

Christian Dywan noreply at xfce.org
Tue Sep 25 02:04:01 CEST 2012


Updating branch refs/heads/master
         to 537ab2a9fc8ba69601af7c4e95e247e99246cb4d (commit)
       from 2eda29a136cbfd988d59b475c99bfbd5c5a1dae3 (commit)

commit 537ab2a9fc8ba69601af7c4e95e247e99246cb4d
Author: Christian Dywan <christian at twotoasts.de>
Date:   Tue Sep 25 01:57:40 2012 +0200

    Move extension loading/ activating out of main for testing
    
    The less code duplication, the better. This should also faciliate
    work towards extensions in non-normal modes.

 midori/main.c             |   79 +---------------------------------
 midori/midori-extension.c |  102 ++++++++++++++++++++++++++++++++++++++++++++-
 midori/midori-extension.h |   14 ++++++-
 tests/extensions.c        |   85 ++++++++++----------------------------
 4 files changed, 139 insertions(+), 141 deletions(-)

diff --git a/midori/main.c b/midori/main.c
index 4859e63..1f4a13a 100644
--- a/midori/main.c
+++ b/midori/main.c
@@ -628,13 +628,7 @@ midori_browser_show_preferences_cb (MidoriBrowser*    browser,
         {
             const gchar* filename;
             while ((filename = g_dir_read_name (extension_dir)))
-            {
-                /* Ignore files which don't have the correct suffix */
-                if (!g_str_has_suffix (filename, G_MODULE_SUFFIX))
-                    continue;
-
                 midori_load_module (app, extension_path, filename, FALSE);
-            }
             g_dir_close (extension_dir);
         }
         g_free (extension_path);
@@ -1207,83 +1201,16 @@ midori_load_soup_session_full (gpointer settings)
 }
 
 static void
-midori_load_extension (MidoriApp*       app,
-                       MidoriExtension* extension,
-                       const gchar*     filename)
-{
-    KatzeArray* extensions = katze_object_get_object (app, "extensions");
-    /* Signal that we want the extension to load and save */
-    g_object_set_data_full (G_OBJECT (extension), "filename",
-                            g_strdup (filename), g_free);
-    if (midori_extension_is_prepared (extension))
-        midori_extension_get_config_dir (extension);
-    katze_array_add_item (extensions, extension);
-    g_object_unref (extensions);
-}
-
-static void
 midori_load_module (MidoriApp*   app,
                     const gchar* extension_path,
                     const gchar* filename,
                     gboolean     activate)
 {
-    gchar* fullname;
-    GModule* module;
-    typedef GObject* (*extension_init_func)(void);
-    extension_init_func extension_init;
-    GObject* extension = NULL;
-    static GHashTable* modules = NULL;
-
-    if (strchr (filename, '/'))
-    {
-        gchar* clean = g_strndup (filename, strchr (filename, '/') - filename);
-        fullname = g_build_filename (extension_path, clean, NULL);
-        g_free (clean);
-    }
-    else
-        fullname = g_build_filename (extension_path, filename, NULL);
-
-    module = g_module_open (fullname, G_MODULE_BIND_LOCAL);
-    g_free (fullname);
-
-    /* GModule detects repeated loading but exposes no API to check it.
-       Skip any modules that were loaded before. */
-    if (modules == NULL)
-        modules = g_hash_table_new (g_direct_hash, g_direct_equal);
-    if (g_hash_table_lookup (modules, module))
+    GObject* extension = midori_extension_load_from_file (extension_path, filename, activate, FALSE);
+    if (extension == NULL)
         return;
-    g_hash_table_insert (modules, module, g_strdup (filename));
-
-    if (module && g_module_symbol (module, "extension_init",
-                                   (gpointer) &extension_init)
-        && (extension = extension_init ()))
-    {
-        if (MIDORI_IS_EXTENSION (extension))
-        {
-            midori_load_extension (app, MIDORI_EXTENSION (extension), filename);
-            if (activate)
-                g_signal_emit_by_name (extension, "activate", app);
-        }
-        else if (KATZE_IS_ARRAY (extension))
-        {
-            MidoriExtension* extension_item;
-            KATZE_ARRAY_FOREACH_ITEM (extension_item, KATZE_ARRAY (extension))
-                if (MIDORI_IS_EXTENSION (extension_item))
-                {
-                    gchar* key;
-
-                    midori_load_extension (app, extension_item, filename);
-                    if (activate)
-                    {
-                        key = katze_object_get_string (extension_item, "key");
-                        if (key && strstr (filename, key))
-                            g_signal_emit_by_name (extension_item, "activate", app);
-                        g_free (key);
-                    }
-                }
-        }
-    }
 
+    midori_extension_activate (extension, filename, activate, app);
     if (!extension  && g_module_error () != NULL)
     {
         KatzeArray* extensions = katze_object_get_object (app, "extensions");
diff --git a/midori/midori-extension.c b/midori/midori-extension.c
index 01f518c..663d88d 100644
--- a/midori/midori-extension.c
+++ b/midori/midori-extension.c
@@ -1,5 +1,5 @@
 /*
- Copyright (C) 2008-2009 Christian Dywan <christian at twotoasts.de>
+ Copyright (C) 2008-2012 Christian Dywan <christian at twotoasts.de>
  Copyright (C) 2009 Dale Whittaker <dayul at users.sf.net>
 
  This library is free software; you can redistribute it and/or
@@ -513,6 +513,106 @@ midori_extension_get_property (GObject*    object,
     }
 }
 
+GObject*
+midori_extension_load_from_file (const gchar* extension_path,
+                                 const gchar* filename,
+                                 gboolean     activate,
+                                 gboolean     test)
+{
+    gchar* fullname;
+    GModule* module;
+    typedef GObject* (*extension_init_func)(void);
+    extension_init_func extension_init;
+    static GHashTable* modules = NULL;
+
+    /* Ignore files which don't have the correct suffix */
+    if (!g_str_has_suffix (filename, G_MODULE_SUFFIX))
+        return NULL;
+
+    if (strchr (filename, '/'))
+    {
+        gchar* clean = g_strndup (filename, strchr (filename, '/') - filename);
+        fullname = g_build_filename (extension_path, clean, NULL);
+        g_free (clean);
+    }
+    else
+        fullname = g_build_filename (extension_path, filename, NULL);
+
+    module = g_module_open (fullname, G_MODULE_BIND_LOCAL);
+    g_free (fullname);
+
+    /* GModule detects repeated loading but exposes no API to check it.
+       Skip any modules that were loaded before. */
+    if (modules == NULL)
+        modules = g_hash_table_new (g_direct_hash, g_direct_equal);
+    if (g_hash_table_lookup (modules, module))
+        return NULL;
+
+    g_hash_table_insert (modules, module, g_strdup (filename));
+
+    if (module && g_module_symbol (module, "extension_init",
+                                   (gpointer) &extension_init))
+    {
+        typedef void (*extension_test_func)(void);
+        extension_test_func extension_test;
+        GObject* extension = extension_init ();
+        if (test && extension && g_module_symbol (module, "extension_test", (gpointer) &extension_test))
+            extension_test ();
+        return extension;
+    }
+
+    return NULL;
+}
+
+static void
+midori_load_extension (MidoriApp*       app,
+                       MidoriExtension* extension,
+                       const gchar*     filename)
+{
+    KatzeArray* extensions = katze_object_get_object (app, "extensions");
+    /* Signal that we want the extension to load and save */
+    g_object_set_data_full (G_OBJECT (extension), "filename",
+                            g_strdup (filename), g_free);
+    if (midori_extension_is_prepared (extension))
+        midori_extension_get_config_dir (extension);
+    katze_array_add_item (extensions, extension);
+    g_object_unref (extensions);
+}
+
+void
+midori_extension_activate (GObject*     extension,
+                           const gchar* filename,
+                           gboolean     activate,
+                           MidoriApp*   app)
+{
+    if (MIDORI_IS_EXTENSION (extension))
+    {
+        if (filename != NULL)
+            midori_load_extension (app, MIDORI_EXTENSION (extension), filename);
+        if (activate)
+            g_signal_emit_by_name (extension, "activate", app);
+    }
+    else if (KATZE_IS_ARRAY (extension))
+    {
+        MidoriExtension* extension_item;
+        KATZE_ARRAY_FOREACH_ITEM (extension_item, KATZE_ARRAY (extension))
+            if (MIDORI_IS_EXTENSION (extension_item))
+            {
+                gchar* key;
+
+                if (filename != NULL)
+                    midori_load_extension (app, extension_item, filename);
+                if (activate)
+                {
+                    key = katze_object_get_string (extension_item, "key");
+                    if (key && filename && strstr (filename, key))
+                        g_signal_emit_by_name (extension_item, "activate", app);
+                    g_free (key);
+                }
+            }
+    }
+}
+
 /**
  * midori_extension_is_prepared:
  * @extension: a #MidoriExtension
diff --git a/midori/midori-extension.h b/midori/midori-extension.h
index fe74d00..37dca80 100644
--- a/midori/midori-extension.h
+++ b/midori/midori-extension.h
@@ -1,5 +1,5 @@
 /*
- Copyright (C) 2008 Christian Dywan <christian at twotoasts.de>
+ Copyright (C) 2008-2012 Christian Dywan <christian at twotoasts.de>
 
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
@@ -48,6 +48,18 @@ struct _MidoriExtensionClass
 GType
 midori_extension_get_type            (void) G_GNUC_CONST;
 
+GObject*
+midori_extension_load_from_file      (const gchar* extension_path,
+                                      const gchar* filename,
+                                      gboolean     activate,
+                                      gboolean     test);
+
+void
+midori_extension_activate           (GObject*     extension,
+                                     const gchar* filename,
+                                     gboolean     activate,
+                                     MidoriApp*   app);
+
 gboolean
 midori_extension_is_prepared         (MidoriExtension* extension);
 
diff --git a/tests/extensions.c b/tests/extensions.c
index b3d5330..75251f0 100644
--- a/tests/extensions.c
+++ b/tests/extensions.c
@@ -166,77 +166,25 @@ extension_settings (void)
 static void
 extension_activate (gconstpointer data)
 {
-    MidoriExtension* extension;
     MidoriApp* app = midori_app_new ();
     g_object_set (app, "settings", midori_web_settings_new (), NULL);
-
-    if (MIDORI_IS_EXTENSION (data))
-    {
-       extension = MIDORI_EXTENSION (data);
-       g_signal_emit_by_name (extension, "activate", app);
-       midori_extension_deactivate (extension);
-    }
-    else if (KATZE_IS_ARRAY (data))
-    {
-       KATZE_ARRAY_FOREACH_ITEM (extension, KATZE_ARRAY (data))
-       {
-           g_signal_emit_by_name (extension, "activate", app);
-           midori_extension_deactivate (extension);
-       }
-    }
-
+    midori_extension_activate (G_OBJECT (data), NULL, TRUE, app);
     g_object_unref (app);
 }
 
 static void
-load_extensions (void)
+extension_load (const gchar* extension_path,
+                GDir*        extension_dir)
 {
-    if (g_module_supported ())
+    const gchar* filename;
+    while ((filename = g_dir_read_name (extension_dir)))
     {
-        GDir* extension_dir = g_dir_open (EXTENSION_PATH, 0, NULL);
-        if (extension_dir != NULL)
+        GObject* extension = midori_extension_load_from_file (extension_path, filename, FALSE, TRUE);
+        if (extension != NULL)
         {
-            const gchar* filename;
-
-            while ((filename = g_dir_read_name (extension_dir)))
-            {
-                gchar* fullname;
-                GModule* module;
-                typedef MidoriExtension* (*extension_init_func)(void);
-                extension_init_func extension_init;
-
-                /* Ignore files which don't have the correct suffix */
-                if (!g_str_has_suffix (filename, G_MODULE_SUFFIX))
-                    continue;
-
-                fullname = g_build_filename (EXTENSION_PATH, filename, NULL);
-                module = g_module_open (fullname, G_MODULE_BIND_LOCAL);
-                g_free (fullname);
-
-                if (module && g_module_symbol (module, "extension_init",
-                                               (gpointer) &extension_init))
-                {
-                    guint length;
-                    gchar* name;
-                    gchar* path;
-                    typedef MidoriExtension* (*extension_test_func)(const gchar* path);
-                    extension_test_func extension_test;
-
-                    if (g_str_has_prefix (filename, "lib"))
-                        filename = &filename[3];
-                    length = strlen (filename);
-                    name = g_strdup (filename);
-                    name[length - strlen (G_MODULE_SUFFIX) - 1] = '\0';
-                    path = g_strconcat ("/extensions/", name, "/activate", NULL);
-                    g_free (name);
-                    g_test_add_data_func (path, extension_init (), extension_activate);
-                    g_free (path);
-                    if (g_module_symbol (module, "extension_test",
-                                                 (gpointer) &extension_test))
-                        extension_test (path);
-                }
-            }
-            g_dir_close (extension_dir);
+            gchar* path = g_strconcat ("/extensions/", filename, NULL);
+            g_test_add_data_func (path, extension, extension_activate);
+            g_free (path);
         }
     }
 }
@@ -254,7 +202,18 @@ main (int    argc,
     g_test_add_func ("/extensions/create", extension_create);
     g_test_add_func ("/extensions/settings", extension_settings);
 
-    load_extensions ();
+    if (g_module_supported ())
+    {
+        const gchar* filename;
+        GDir* extension_dir = g_dir_open (EXTENSION_PATH, 0, NULL);
+        g_assert (extension_dir != NULL);
+
+        /* We require that extensions can be loaded repeatedly */
+        extension_load (EXTENSION_PATH, extension_dir);
+        extension_load (EXTENSION_PATH, extension_dir);
+
+        g_dir_close (extension_dir);
+    }
 
     return g_test_run ();
 }


More information about the Xfce4-commits mailing list