[folks] BackendStore: provide an easier way to limit backends via environment



commit 1fd8b55222f820b65338f9587a328316a35bd22a
Author: Simon McVittie <simon mcvittie collabora co uk>
Date:   Fri Mar 8 15:02:25 2013 +0000

    BackendStore: provide an easier way to limit backends via environment
    
    If FOLKS_BACKENDS_ALLOWED is set, it's a space-, comma- or
    colon-separated list of backends to allow, or "all". If unset, the
    default is "all". Backends not in the list are disabled, even if the
    keyfile says they ought to be enabled.
    
    If FOLKS_BACKENDS_DISABLED is set, it's a space-, comma- or
    colon-separated list of additional backends to disable.
    
    Bug: https://bugzilla.gnome.org/show_bug.cgi?id=695381
    Signed-off-by: Simon McVittie <simon mcvittie collabora co uk>
    Reviewed-by: Philip Withnall <philip tecnocode co uk>

 HACKING                  |   17 ++++++++++
 folks/backend-store.vala |   78 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 95 insertions(+), 0 deletions(-)
---
diff --git a/HACKING b/HACKING
index b41bf66..263e548 100644
--- a/HACKING
+++ b/HACKING
@@ -174,3 +174,20 @@ This is based on Federico Mena Quintero’s plot-timeline.py, described on:
 http://people.gnome.org/~federico/news-2006-03.html#timeline-tools. The Python
 script itself can be downloaded from
 http://gitorious.org/projects/performance-scripts.
+
+Environment variables
+=====================
+
+FOLKS_BACKEND_STORE_KEY_FILE_PATH sets the keyfile used to control the set
+of enabled backends. The default is g_get_user_data_dir()/folks/backends.ini,
+and if it is empty, all backends are enabled.
+
+If FOLKS_BACKENDS_ALLOWED is set, it's a space-, comma- or
+colon-separated list of backends to allow, or "all". If unset, the
+default is equivalent to "all". Backends not in the list are disallowed,
+even if enabled in the keyfile or with enable_backend().
+
+If FOLKS_BACKENDS_DISABLED is set, it's a space-, comma- or
+colon-separated list of backends to disallow, or "all". If unset, the
+default is equivalent to "all". Backends in the list are disallowed,
+even if enabled in the keyfile or with enable_backend().
diff --git a/folks/backend-store.vala b/folks/backend-store.vala
index 4a47aa5..d80eceb 100644
--- a/folks/backend-store.vala
+++ b/folks/backend-store.vala
@@ -43,6 +43,10 @@ public class Folks.BackendStore : Object {
 
   /* this contains all backends, regardless of enabled or prepared state */
   private HashMap<string,Backend> _backend_hash;
+  /* if null, all backends are allowed */
+  private HashSet<string>? _backends_allowed;
+  /* if null, no backends are disabled */
+  private HashSet<string>? _backends_disabled;
   private HashMap<string, Backend> _prepared_backends;
   private Map<string, Backend> _prepared_backends_ro;
   private File _config_file;
@@ -462,6 +466,15 @@ public class Folks.BackendStore : Object {
   private bool _backend_is_enabled (string name)
     {
       var all_others_enabled = true;
+
+      if (this._backends_allowed != null &&
+          !(name in (!) this._backends_allowed))
+        return false;
+
+      if (this._backends_disabled != null &&
+          name in (!) this._backends_disabled)
+        return false;
+
       try
         {
           all_others_enabled = this._backends_key_file.get_boolean (
@@ -552,6 +565,11 @@ public class Folks.BackendStore : Object {
    * asynchronous call may begin after a previous asynchronous call for the same
    * backend name has begun and before it has finished).
    *
+   * If the backend is disallowed by the FOLKS_BACKENDS_ALLOWED
+   * and/or FOLKS_BACKENDS_DISABLED environment variables, this method
+   * will store the fact that it should be enabled in future, but will
+   * not enable it during this application run.
+   *
    * @param name the name of the backend to enable
    * @since 0.3.2
    */
@@ -756,6 +774,66 @@ public class Folks.BackendStore : Object {
   /* This method is safe to call multiple times concurrently. */
   private async void _load_disabled_backend_names ()
     {
+      /* If set, this is a list of allowed backends. No others can be enabled,
+       * even if the keyfile says they ought to be.
+       * The default is equivalent to "all", which allows any backend.
+       *
+       * Regression tests and benchmarks can use this to restrict themselves
+       * to a small set of backends for which they have done the necessary
+       * setup/configuration/sandboxing. */
+      var envvar = Environment.get_variable ("FOLKS_BACKENDS_ALLOWED");
+
+      if (envvar != null)
+        {
+          /* Allow space, comma or colon separation, consistent with
+           * g_parse_debug_string(). */
+          var tokens = envvar.split_set (" ,:");
+
+          this._backends_allowed = new HashSet<string> ();
+
+          foreach (unowned string s in tokens)
+            {
+              if (s == "all")
+                {
+                  this._backends_allowed = null;
+                  break;
+                }
+
+              if (s != "")
+                this._backends_allowed.add (s);
+            }
+
+          if (this._backends_allowed != null)
+            {
+              debug ("Backends limited by FOLKS_BACKENDS_ALLOWED:");
+
+              foreach (unowned string s in tokens)
+                debug ("Backend '%s' is allowed", s);
+
+              debug ("All other backends disabled by FOLKS_BACKENDS_ALLOWED");
+            }
+        }
+
+      /* If set, this is a list of disallowed backends.
+       * They are not enabled, even if the keyfile says they ought to be. */
+      envvar = Environment.get_variable ("FOLKS_BACKENDS_DISABLED");
+
+      if (envvar != null)
+        {
+          var tokens = envvar.split_set (" ,:");
+
+          this._backends_disabled = new HashSet<string> ();
+
+          foreach (unowned string s in tokens)
+            {
+              if (s != "")
+                {
+                  debug ("Backend '%s' disabled by FOLKS_BACKENDS_DISABLED", s);
+                  this._backends_disabled.add (s);
+                }
+            }
+        }
+
       File file;
       unowned string? path = Environment.get_variable (
           "FOLKS_BACKEND_STORE_KEY_FILE_PATH");


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