[grilo/0.1.x] core: Allow restricting the list of plugins to use



commit a3a51aea472e87459efd59b7cc1de9225ae2a1f4
Author: Juan A. Suarez Romero <jasuarez igalia com>
Date:   Fri Sep 2 22:14:14 2011 +0000

    core: Allow restricting the list of plugins to use
    
    Sometimes it is useful to restrict the list of plugins to a subset of those
    installed.
    
    This patch provides an environment variable (GRL_PLUGIN_LIST) 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/Makefile.am                |    3 +-
 src/grilo.c                    |   27 ++++++++++++++++++-----
 src/grl-plugin-registry-priv.h |   32 ++++++++++++++++++++++++++++
 src/grl-plugin-registry.c      |   45 +++++++++++++++++++++++++++++++++++++++-
 src/grl-plugin-registry.h      |    1 +
 5 files changed, 100 insertions(+), 8 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index b96f64a..54d1d0a 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -37,7 +37,7 @@ lib GRL_NAME@_la_LIBADD =	\
 
 lib GRL_NAME@_la_SOURCES =					\
 	grl-media-plugin.c grl-media-plugin-priv.h		\
-	grl-plugin-registry.c					\
+	grl-plugin-registry.c grl-plugin-registry-priv.h	\
 	grl-metadata-key.c grl-metadata-key-priv.h		\
 	grl-metadata-source.c grl-metadata-source-priv.h	\
 	grl-operation.c grl-operation.h				\
@@ -91,6 +91,7 @@ data_h_headers =		\
 lib GRL_NAME@inc_HEADERS += $(data_h_headers)
 
 noinst_HEADERS =			\
+	grl-plugin-registry-priv.h	\
 	grl-media-plugin-priv.h		\
 	grl-metadata-source-priv.h	\
 	grl-metadata-key-priv.h		\
diff --git a/src/grilo.c b/src/grilo.c
index 8b3f80b..d68750e 100644
--- a/src/grilo.c
+++ b/src/grilo.c
@@ -36,6 +36,7 @@
 #include "grilo.h"
 #include "grl-metadata-key-priv.h"
 #include "grl-operation-priv.h"
+#include "grl-plugin-registry-priv.h"
 #include "grl-log-priv.h"
 #include "config.h"
 
@@ -43,6 +44,7 @@
 
 static gboolean grl_initialized = FALSE;
 static const gchar *plugin_path = NULL;
+static const gchar *plugin_list = NULL;
 
 /**
  * grl_init:
@@ -60,8 +62,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 +110,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 +151,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,
+      "Colon-separated list of plugins to use", NULL },
     { NULL }
   };
 
diff --git a/src/grl-plugin-registry-priv.h b/src/grl-plugin-registry-priv.h
new file mode 100644
index 0000000..c9844bc
--- /dev/null
+++ b/src/grl-plugin-registry-priv.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2011 Igalia S.L.
+ *
+ * Contact: Iago Toral Quiroga <itoral igalia com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+ */
+
+#ifndef _GRL_PLUGIN_REGISTRY_PRIV_H_
+#define _GRL_PLUGIN_REGISTRY_PRIV_H_
+
+#include <grl-plugin-registry.h>
+
+void
+grl_plugin_registry_restrict_plugins (GrlPluginRegistry *registry,
+                                      gchar **plugins);
+
+#endif /* _GRL_PLUGIN_REGISTRY_PRIV_H_ */
diff --git a/src/grl-plugin-registry.c b/src/grl-plugin-registry.c
index 704889a..628d6b3 100644
--- a/src/grl-plugin-registry.c
+++ b/src/grl-plugin-registry.c
@@ -39,6 +39,7 @@
  */
 
 #include "grl-plugin-registry.h"
+#include "grl-plugin-registry-priv.h"
 #include "grl-media-plugin-priv.h"
 #include "grl-log.h"
 #include "grl-error.h"
@@ -70,6 +71,7 @@ struct _GrlPluginRegistryPrivate {
   GParamSpecPool *system_keys;
   GHashTable *ranks;
   GSList *plugins_dir;
+  GSList *allowed_plugins;
   gboolean all_plugin_info_loaded;
 };
 
@@ -366,6 +368,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);
@@ -414,7 +424,40 @@ grl_plugin_registry_load_list (GrlPluginRegistry *registry,
   return loaded_one;
 }
 
-/* ================ API ================ */
+/* ================ PRIVATE API ================ */
+
+/**
+ * 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++;
+  }
+}
+
+/* ================ PUBLIC API ================ */
 
 /**
  * grl_plugin_registry_get_default:
diff --git a/src/grl-plugin-registry.h b/src/grl-plugin-registry.h
index 8479c80..d961fb5 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_LIST"
 #define GRL_PLUGIN_RANKS_VAR "GRL_PLUGIN_RANKS"
 
 /* Macros */



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