[gnome-software] Allow plugins to be enabled and disabled at runtime



commit 52e50faab854805773f8211c3b16e5366b2fb337
Author: Richard Hughes <richard hughsie com>
Date:   Wed Jun 8 18:00:24 2016 +0100

    Allow plugins to be enabled and disabled at runtime
    
    This makes debugging easier. To use in gnome-software-cmd there are new command
    line options, but for gnome-software you have to use the environment variables
    GNOME_SOFTWARE_PLUGINS_BLACKLIST and GNOME_SOFTWARE_PLUGINS_WHITELIST.
    
    For instance:
    
    GNOME_SOFTWARE_PLUGINS_BLACKLIST=packagekit,dpkg,steam,odrs ./gnome-software
    
    Based on a patch from Niv Sardi <xaiki endlessm com>, many thanks.

 src/gs-application.c   |   16 +++++++++++++++-
 src/gs-cmd.c           |   23 ++++++++++++++++-------
 src/gs-plugin-loader.c |   23 +++++++++++++++++++++++
 src/gs-plugin-loader.h |    1 +
 src/gs-self-test.c     |    2 +-
 5 files changed, 56 insertions(+), 9 deletions(-)
---
diff --git a/src/gs-application.c b/src/gs-application.c
index e46cb4e..9be8a6b 100644
--- a/src/gs-application.c
+++ b/src/gs-application.c
@@ -198,16 +198,30 @@ static void
 gs_application_initialize_plugins (GsApplication *app)
 {
        static gboolean initialized = FALSE;
+       g_auto(GStrv) plugin_blacklist = NULL;
+       g_auto(GStrv) plugin_whitelist = NULL;
        g_autoptr(GError) error = NULL;
+       const gchar *tmp;
 
        if (initialized)
                return;
 
        initialized = TRUE;
 
+       /* allow for debugging */
+       tmp = g_getenv ("GNOME_SOFTWARE_PLUGINS_BLACKLIST");
+       if (tmp != NULL)
+               plugin_blacklist = g_strsplit (tmp, ",", -1);
+       tmp = g_getenv ("GNOME_SOFTWARE_PLUGINS_WHITELIST");
+       if (tmp != NULL)
+               plugin_whitelist = g_strsplit (tmp, ",", -1);
+
        app->plugin_loader = gs_plugin_loader_new ();
        gs_plugin_loader_set_location (app->plugin_loader, NULL);
-       if (!gs_plugin_loader_setup (app->plugin_loader, NULL, &error)) {
+       if (!gs_plugin_loader_setup (app->plugin_loader,
+                                    plugin_whitelist,
+                                    plugin_blacklist,
+                                    &error)) {
                g_warning ("Failed to setup plugins: %s", error->message);
                exit (1);
        }
diff --git a/src/gs-cmd.c b/src/gs-cmd.c
index 60244a8..d69790d 100644
--- a/src/gs-cmd.c
+++ b/src/gs-cmd.c
@@ -209,12 +209,14 @@ main (int argc, char **argv)
        gint cache_age = 0;
        gint repeat = 1;
        int status = 0;
-       g_auto(GStrv) plugin_names = NULL;
+       g_auto(GStrv) plugin_blacklist = NULL;
+       g_auto(GStrv) plugin_whitelist = NULL;
        g_autoptr(GError) error = NULL;
        g_autoptr(GsAppList) list = NULL;
        g_autoptr(GPtrArray) categories = NULL;
        g_autoptr(GsDebug) debug = gs_debug_new ();
-       g_autofree gchar *plugin_names_str = NULL;
+       g_autofree gchar *plugin_blacklist_str = NULL;
+       g_autofree gchar *plugin_whitelist_str = NULL;
        g_autofree gchar *refine_flags_str = NULL;
        g_autoptr(GsApp) app = NULL;
        g_autoptr(GFile) file = NULL;
@@ -232,8 +234,10 @@ main (int argc, char **argv)
                  "Use this maximum cache age in seconds", NULL },
                { "prefer-local", '\0', 0, G_OPTION_ARG_NONE, &prefer_local,
                  "Prefer local file sources to AppStream", NULL },
-               { "plugin-names", '\0', 0, G_OPTION_ARG_STRING, &plugin_names_str,
-                 "Whitelist only these plugin names", NULL },
+               { "plugin-blacklist", '\0', 0, G_OPTION_ARG_STRING, &plugin_blacklist_str,
+                 "Do not load specific plugins", NULL },
+               { "plugin-whitelist", '\0', 0, G_OPTION_ARG_STRING, &plugin_whitelist_str,
+                 "Only load specific plugins", NULL },
                { "verbose", '\0', 0, G_OPTION_ARG_NONE, &verbose,
                  "Show verbose debugging information", NULL },
                { NULL}
@@ -277,9 +281,14 @@ main (int argc, char **argv)
        /* load plugins */
        plugin_loader = gs_plugin_loader_new ();
        gs_plugin_loader_set_location (plugin_loader, "./plugins/.libs");
-       if (plugin_names_str != NULL)
-               plugin_names = g_strsplit (plugin_names_str, ",", -1);
-       ret = gs_plugin_loader_setup (plugin_loader, plugin_names, &error);
+       if (plugin_whitelist_str != NULL)
+               plugin_whitelist = g_strsplit (plugin_whitelist_str, ",", -1);
+       if (plugin_blacklist_str != NULL)
+               plugin_blacklist = g_strsplit (plugin_blacklist_str, ",", -1);
+       ret = gs_plugin_loader_setup (plugin_loader,
+                                     plugin_whitelist,
+                                     plugin_blacklist,
+                                     &error);
        if (!ret) {
                g_print ("Failed to setup plugins: %s\n", error->message);
                goto out;
diff --git a/src/gs-plugin-loader.c b/src/gs-plugin-loader.c
index 9394b55..835f174 100644
--- a/src/gs-plugin-loader.c
+++ b/src/gs-plugin-loader.c
@@ -3349,10 +3349,19 @@ gs_plugin_loader_plugin_sort_fn (gconstpointer a, gconstpointer b)
 
 /**
  * gs_plugin_loader_setup:
+ * @plugin_loader: a #GsPluginLoader
+ * @whitelist: list of plugin names, or %NULL
+ * @blacklist: list of plugin names, or %NULL
+ * @error: A #GError, or %NULL
+ *
+ * Sets up the plugin loader ready for use.
+ *
+ * Returns: %TRUE for success
  */
 gboolean
 gs_plugin_loader_setup (GsPluginLoader *plugin_loader,
                        gchar **whitelist,
+                       gchar **blacklist,
                        GError **error)
 {
        GsPluginLoaderPrivate *priv = gs_plugin_loader_get_instance_private (plugin_loader);
@@ -3404,6 +3413,20 @@ gs_plugin_loader_setup (GsPluginLoader *plugin_loader,
                }
        }
 
+       /* optional blacklist */
+       if (blacklist != NULL) {
+               for (i = 0; i < priv->plugins->len; i++) {
+                       gboolean ret;
+                       plugin = g_ptr_array_index (priv->plugins, i);
+                       if (!gs_plugin_get_enabled (plugin))
+                               continue;
+                       ret = g_strv_contains ((const gchar * const *) blacklist,
+                                              gs_plugin_get_name (plugin));
+                       if (ret)
+                               gs_plugin_set_enabled (plugin, FALSE);
+               }
+       }
+
        /* run the plugins */
        gs_plugin_loader_run (plugin_loader, "gs_plugin_initialize");
 
diff --git a/src/gs-plugin-loader.h b/src/gs-plugin-loader.h
index f14e50c..572416c 100644
--- a/src/gs-plugin-loader.h
+++ b/src/gs-plugin-loader.h
@@ -183,6 +183,7 @@ gboolean     gs_plugin_loader_update_finish         (GsPluginLoader *plugin_loader,
                                                         GError         **error);
 gboolean        gs_plugin_loader_setup                 (GsPluginLoader *plugin_loader,
                                                         gchar          **whitelist,
+                                                        gchar          **blacklist,
                                                         GError         **error);
 void            gs_plugin_loader_dump_state            (GsPluginLoader *plugin_loader);
 gboolean        gs_plugin_loader_get_enabled           (GsPluginLoader *plugin_loader,
diff --git a/src/gs-self-test.c b/src/gs-self-test.c
index 996794f..f8c1f6e 100644
--- a/src/gs-self-test.c
+++ b/src/gs-self-test.c
@@ -975,7 +975,7 @@ main (int argc, char **argv)
        g_signal_connect (plugin_loader, "status-changed",
                          G_CALLBACK (gs_plugin_loader_status_changed_cb), NULL);
        gs_plugin_loader_set_location (plugin_loader, "./plugins/.libs");
-       ret = gs_plugin_loader_setup (plugin_loader, (gchar**) whitelist, &error);
+       ret = gs_plugin_loader_setup (plugin_loader, (gchar**) whitelist, NULL, &error);
        g_assert_no_error (error);
        g_assert (ret);
        g_assert (!gs_plugin_loader_get_enabled (plugin_loader, "notgoingtoexist"));


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