[PATCH 3/3] core: Allow restricting the list of plugins to use



From: "Juan A. Suarez Romero" <jasuarez igalia com>

Sometimes it is useful to restrict the list of plugins to a subset of those
installed.

This patch provides a new function in registry, as well as an environment
variable (GRL_PLUGIN_USE) and a parameter to Grilo-based applications to
restrict the plugins to use.

Applications will only see those plugins that are in the specified list, no
matter if there are other plugins also in the same location.

Signed-off-by: Juan A. Suarez Romero <jasuarez igalia com>
---
 src/grilo.c               |   26 ++++++++++++++++++++------
 src/grl-plugin-registry.c |   40 ++++++++++++++++++++++++++++++++++++++++
 src/grl-plugin-registry.h |    4 ++++
 3 files changed, 64 insertions(+), 6 deletions(-)

diff --git a/src/grilo.c b/src/grilo.c
index 8b3f80b..4e41459 100644
--- a/src/grilo.c
+++ b/src/grilo.c
@@ -43,6 +43,7 @@
 
 static gboolean grl_initialized = FALSE;
 static const gchar *plugin_path = NULL;
+static const gchar *plugin_list = NULL;
 
 /**
  * grl_init:
@@ -60,8 +61,8 @@ grl_init (gint *argc,
   GOptionContext *ctx;
   GOptionGroup *group;
   GrlPluginRegistry *registry;
-  gchar **plugin_dir;
-  gchar **plugin_dirs_split;
+  gchar **split_element;
+  gchar **split_list;
 
   if (grl_initialized) {
     GRL_DEBUG ("already initialized grl");
@@ -108,11 +109,22 @@ grl_init (gint *argc,
     plugin_path = GRL_PLUGIN_PATH_DEFAULT;
   }
 
-  plugin_dirs_split = g_strsplit (plugin_path, ":", 0);
-  for (plugin_dir = plugin_dirs_split; *plugin_dir; plugin_dir++) {
-    grl_plugin_registry_add_directory (registry, *plugin_dir);
+  split_list = g_strsplit (plugin_path, ":", 0);
+  for (split_element = split_list; *split_element; split_element++) {
+    grl_plugin_registry_add_directory (registry, *split_element);
+  }
+  g_strfreev (split_list);
+
+  /* Restrict plugins to load */
+  if (!plugin_list) {
+    plugin_list = g_getenv (GRL_PLUGIN_LIST_VAR);
+  }
+
+  if (plugin_list) {
+    split_list = g_strsplit (plugin_list, ",", 0);
+    grl_plugin_registry_restrict_plugins (registry, split_list);
+    g_strfreev (split_list);
   }
-  g_strfreev (plugin_dirs_split);
 
   grl_initialized = TRUE;
 }
@@ -138,6 +150,8 @@ grl_init_get_option_group (void)
   static const GOptionEntry grl_args[] = {
     { "grl-plugin-path", 0, 0, G_OPTION_ARG_STRING, &plugin_path,
       "Colon-separated paths containing plugins", NULL },
+    { "grl-plugin-use", 0, 0, G_OPTION_ARG_STRING, &plugin_list,
+      "Comma-separated list of plugins to use", NULL },
     { NULL }
   };
 
diff --git a/src/grl-plugin-registry.c b/src/grl-plugin-registry.c
index cf8dfd1..a54a89c 100644
--- a/src/grl-plugin-registry.c
+++ b/src/grl-plugin-registry.c
@@ -77,6 +77,7 @@ struct _GrlPluginRegistryPrivate {
   GParamSpecPool *system_keys;
   GHashTable *ranks;
   GSList *plugins_dir;
+  GSList *allowed_plugins;
   gboolean all_plugin_info_loaded;
   struct KeyIDHandler key_id_handler;
 };
@@ -387,6 +388,14 @@ grl_plugin_registry_load_plugin_info_directory (GrlPluginRegistry *registry,
         g_free (file);
         continue;
       }
+      /* Check if plugin is allowed or not */
+      if (registry->priv->allowed_plugins &&
+          !g_slist_find_custom (registry->priv->allowed_plugins,
+                                id,
+                                (GCompareFunc) g_strcmp0)) {
+        GRL_DEBUG ("'%s' plugin not allowed; skipping", id);
+        continue;
+      }
       plugin_info = grl_plugin_registry_load_plugin_info (registry, id, file);
       g_free (id);
       g_free (file);
@@ -661,6 +670,37 @@ grl_plugin_registry_add_directory (GrlPluginRegistry *registry,
 }
 
 /**
+ * grl_plugin_registry_restrict_plugins:
+ * @registry: the registry instance
+ * @plugins: a @NULL-terminated array of plugins identifiers
+ *
+ * Restrict the plugins that application sees to this list.
+ *
+ * Other plugins will not be available for the application, unless it uses
+ * directly #grl_plugin_registry_load() function.
+ **/
+void
+grl_plugin_registry_restrict_plugins (GrlPluginRegistry *registry,
+                                      gchar **plugins)
+{
+  g_return_if_fail (GRL_IS_PLUGIN_REGISTRY (registry));
+  g_return_if_fail (plugins);
+
+  /* Free previous list */
+  if (registry->priv->allowed_plugins) {
+    g_slist_foreach (registry->priv->allowed_plugins, (GFunc) g_free, NULL);
+    g_slist_free (registry->priv->allowed_plugins);
+    registry->priv->allowed_plugins = NULL;
+  }
+
+  while (*plugins) {
+    registry->priv->allowed_plugins = g_slist_prepend (registry->priv->allowed_plugins,
+                                                       g_strdup (*plugins));
+    plugins++;
+  }
+}
+
+/**
  * grl_plugin_registry_load:
  * @registry: the registry instance
  * @library_filename: the path to the so file
diff --git a/src/grl-plugin-registry.h b/src/grl-plugin-registry.h
index 22ae36f..1df0742 100644
--- a/src/grl-plugin-registry.h
+++ b/src/grl-plugin-registry.h
@@ -37,6 +37,7 @@
 #include <grl-definitions.h>
 
 #define GRL_PLUGIN_PATH_VAR "GRL_PLUGIN_PATH"
+#define GRL_PLUGIN_LIST_VAR "GRL_PLUGIN_USE"
 #define GRL_PLUGIN_RANKS_VAR "GRL_PLUGIN_RANKS"
 
 /* Macros */
@@ -205,6 +206,9 @@ GrlPluginRegistry *grl_plugin_registry_get_default (void);
 void grl_plugin_registry_add_directory (GrlPluginRegistry *registry,
                                         const gchar *path);
 
+void grl_plugin_registry_restrict_plugins (GrlPluginRegistry *registry,
+                                           gchar **plugins);
+
 gboolean grl_plugin_registry_load (GrlPluginRegistry *registry,
                                    const gchar *library_filename,
                                    GError **error);
-- 
1.7.5.4



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