From 4a74819eb273463bb457fa71f055a7ae276fc639 Mon Sep 17 00:00:00 2001
From: Cosimo Cecchi <cosimo@endlessm.com>
Date: Mon, 20 Jun 2016 11:42:56 -0700
Subject: [PATCH] vfs: add g_vfs_register_uri_scheme()

Add a new API to allow clients to register a custom GFile implementation
handling a particular URI scheme.
This can be useful for tests, but also for cases where a different URI
scheme is desired to be used with another custom GFile backend.

As an additional cleanup, we can use this to register the "resource" URI
scheme too.

Based on a patch by Jasper St. Pierre <jstpierre@mecheye.net>.

https://bugzilla.gnome.org/show_bug.cgi?id=767887
---
 docs/reference/gio/gio-sections.txt |   2 +
 gio/gvfs.c                          | 133 +++++++++++++++++++++++++++++++++---
 gio/gvfs.h                          |  24 +++++++
 gio/tests/vfs.c                     |  35 ++++++++++
 4 files changed, 183 insertions(+), 11 deletions(-)

diff --git a/docs/reference/gio/gio-sections.txt b/docs/reference/gio/gio-sections.txt
index 756b390..aaf2067 100644
--- a/docs/reference/gio/gio-sections.txt
+++ b/docs/reference/gio/gio-sections.txt
@@ -42,6 +42,7 @@ g_async_initable_get_type
 <FILE>gvfs</FILE>
 <TITLE>GVfs</TITLE>
 GVfs
+GVfsURILookupFunc
 G_VFS_EXTENSION_POINT_NAME
 g_vfs_get_file_for_path
 g_vfs_get_file_for_uri
@@ -50,6 +51,7 @@ g_vfs_get_default
 g_vfs_get_local
 g_vfs_is_active
 g_vfs_get_supported_uri_schemes
+g_vfs_register_uri_scheme
 <SUBSECTION Standard>
 GVfsClass
 G_VFS
diff --git a/gio/gvfs.c b/gio/gvfs.c
index 2390b7c..31dfaaa 100644
--- a/gio/gvfs.c
+++ b/gio/gvfs.c
@@ -37,16 +37,47 @@
  *
  */
 
-G_DEFINE_TYPE (GVfs, g_vfs, G_TYPE_OBJECT);
+typedef struct _GVfsPrivate {
+  GHashTable *additional_schemes;
+  char const **supported_schemes;
+} GVfsPrivate;
+
+G_DEFINE_TYPE_WITH_PRIVATE (GVfs, g_vfs, G_TYPE_OBJECT);
+
+static void
+g_vfs_dispose (GObject *object)
+{
+  GVfs *vfs = G_VFS (object);
+  GVfsPrivate *priv = g_vfs_get_instance_private (vfs);
+
+  g_clear_pointer (&priv->additional_schemes, g_hash_table_destroy);
+  g_clear_pointer (&priv->supported_schemes, g_free);
+
+  G_OBJECT_CLASS (g_vfs_parent_class)->dispose (object);
+}
 
 static void
 g_vfs_class_init (GVfsClass *klass)
 {
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  object_class->dispose = g_vfs_dispose;
+}
+
+static GFile *
+resource_get_file_for_uri (GVfs       *vfs,
+                           const char *uri)
+{
+  return _g_resource_file_new (uri);
 }
 
 static void
 g_vfs_init (GVfs *vfs)
 {
+  GVfsPrivate *priv = g_vfs_get_instance_private (vfs);
+  priv->additional_schemes = g_hash_table_new_full (g_str_hash, g_str_equal,
+                                                    g_free, NULL);
+
+  g_vfs_register_uri_scheme (vfs, "resource", resource_get_file_for_uri);
 }
 
 /**
@@ -95,6 +126,27 @@ g_vfs_get_file_for_path (GVfs       *vfs,
   return (* class->get_file_for_path) (vfs, path);
 }
 
+static GFile *
+get_file_for_uri_internal (GVfs       *vfs,
+                           const char *uri)
+{
+  GVfsPrivate *priv = g_vfs_get_instance_private (vfs);
+  GFile *ret = NULL;
+  char *scheme;
+  GVfsURILookupFunc func;
+
+  scheme = g_uri_parse_scheme (uri);
+  if (scheme == NULL)
+    return NULL;
+
+  func = g_hash_table_lookup (priv->additional_schemes, scheme);
+  if (func)
+    ret = func (vfs, uri);
+
+  g_free (scheme);
+  return ret;
+}
+
 /**
  * g_vfs_get_file_for_uri:
  * @vfs: a#GVfs.
@@ -114,19 +166,16 @@ g_vfs_get_file_for_uri (GVfs       *vfs,
                         const char *uri)
 {
   GVfsClass *class;
+  GFile *ret;
  
   g_return_val_if_fail (G_IS_VFS (vfs), NULL);
   g_return_val_if_fail (uri != NULL, NULL);
 
   class = G_VFS_GET_CLASS (vfs);
 
-  /* This is an unfortunate placement, but we really
-   * need to check this before chaining to the vfs,
-   * because we want to support resource uris for
-   * all vfs:es, even those that predate resources.
-   */
-  if (g_str_has_prefix (uri, "resource:"))
-    return _g_resource_file_new (uri);
+  ret = get_file_for_uri_internal (vfs, uri);
+  if (ret)
+    return ret;
 
   return (* class->get_file_for_uri) (vfs, uri);
 }
@@ -144,13 +193,39 @@ g_vfs_get_file_for_uri (GVfs       *vfs,
 const gchar * const *
 g_vfs_get_supported_uri_schemes (GVfs *vfs)
 {
-  GVfsClass *class;
+  GVfsPrivate *priv;
 
   g_return_val_if_fail (G_IS_VFS (vfs), NULL);
 
-  class = G_VFS_GET_CLASS (vfs);
+  priv = g_vfs_get_instance_private (vfs);
 
-  return (* class->get_supported_uri_schemes) (vfs);
+  if (!priv->supported_schemes)
+    {
+      GVfsClass *class;
+      const char * const *default_schemes;
+      const char *additional_scheme;
+      GPtrArray *supported_schemes;
+      GHashTableIter iter;
+      int idx;
+
+      class = G_VFS_GET_CLASS (vfs);
+
+      default_schemes = (* class->get_supported_uri_schemes) (vfs);
+      supported_schemes = g_ptr_array_new ();
+
+      for (idx = 0; idx < G_N_ELEMENTS (default_schemes); idx++)
+        g_ptr_array_add (supported_schemes, (gpointer) default_schemes[idx]);
+
+      g_hash_table_iter_init (&iter, priv->additional_schemes);
+
+      while (g_hash_table_iter_next (&iter, (gpointer *) &additional_scheme, NULL))
+        g_ptr_array_add (supported_schemes, (gpointer) additional_scheme);
+
+      g_free (priv->supported_schemes);
+      priv->supported_schemes = (char const **) g_ptr_array_free (supported_schemes, FALSE);
+    }
+
+  return priv->supported_schemes;
 }
 
 /**
@@ -216,3 +291,39 @@ g_vfs_get_local (void)
 
   return G_VFS (vfs);
 }
+
+/**
+ * g_vfs_register_uri_scheme:
+ * @scheme: an URI scheme, e.g. "http"
+ * @func: (nullable): a #GVfsURILookupFunc
+ *
+ * Registers @func as the lookup function for URIs with a scheme matching
+ * @scheme.
+ * When a #GFile is requested with an URI containing @scheme (e.g. through
+ * g_file_new_for_uri()), @func will be called to allow a custom constructor.
+ *
+ * If this function is called twice with the same scheme, @func will be
+ * replaced. It's possible to call this function with a %NULL @func to
+ * unregister handling of @scheme.
+ *
+ * Since: 2.50
+ */
+void
+g_vfs_register_uri_scheme (GVfs              *vfs,
+                           const char        *scheme,
+                           GVfsURILookupFunc  func)
+{
+  GVfsPrivate *priv;
+
+  g_return_if_fail (G_IS_VFS (vfs));
+  g_return_if_fail (scheme != NULL);
+
+  priv = g_vfs_get_instance_private (vfs);
+
+  if (func != NULL)
+    g_hash_table_replace (priv->additional_schemes, g_strdup (scheme), func);
+  else
+    g_hash_table_remove (priv->additional_schemes, scheme);
+
+  g_clear_pointer (&priv->supported_schemes, g_free);
+}
diff --git a/gio/gvfs.h b/gio/gvfs.h
index e67e2d9..509baaa 100644
--- a/gio/gvfs.h
+++ b/gio/gvfs.h
@@ -37,6 +37,25 @@ G_BEGIN_DECLS
 #define G_IS_VFS_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), G_TYPE_VFS))
 
 /**
+ * GVfsURILookupFunc:
+ * @vfs: a #GVfs
+ * @uri: the URI to lookup a #GFile for
+ *
+ * This function type is used by g_vfs_register_uri_scheme() to make it
+ * possible for a client to associate an URI scheme to a different #GFile
+ * implementation.
+ *
+ * The client should return a reference to the new file that has been
+ * created for @uri, or %NULL to continue with the default implementation.
+ *
+ * Returns: (transfer full): a #GFile for @uri.
+ *
+ * Since: 2.50
+ */
+typedef GFile * (* GVfsURILookupFunc) (GVfs       *vfs,
+                                       const char *uri);
+
+/**
  * G_VFS_EXTENSION_POINT_NAME:
  *
  * Extension point for #GVfs functionality.
@@ -127,6 +146,11 @@ GVfs *                g_vfs_get_default               (void);
 GLIB_AVAILABLE_IN_ALL
 GVfs *                g_vfs_get_local                 (void);
 
+GLIB_AVAILABLE_IN_2_50
+void                  g_vfs_register_uri_scheme       (GVfs              *vfs,
+                                                       const char        *scheme,
+                                                       GVfsURILookupFunc  func);
+
 G_END_DECLS
 
 #endif /* __G_VFS_H__ */
diff --git a/gio/tests/vfs.c b/gio/tests/vfs.c
index 47ba3c0..2e4bb5a 100644
--- a/gio/tests/vfs.c
+++ b/gio/tests/vfs.c
@@ -23,6 +23,40 @@
 
 #include <gio/gio.h>
 
+static GFile *
+test_vfs_lookup (GVfs       *vfs,
+                 const char *uri)
+{
+  GFile *file;
+  file = g_file_new_for_path (g_get_home_dir ());
+  g_object_set_data (G_OBJECT (file), "testfile", GINT_TO_POINTER (1));
+
+  return file;
+}
+
+static void
+test_register_scheme (void)
+{
+  GVfs *vfs;
+  GFile *file;
+
+  vfs = g_vfs_get_default ();
+  g_assert_nonnull (vfs);
+  g_assert_true (g_vfs_is_active (vfs));
+
+  g_vfs_register_uri_scheme (vfs, "test", test_vfs_lookup);
+
+  file = g_file_new_for_uri ("test:///foo");
+  g_assert_cmpint (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (file), "testfile")), ==, 1);
+  g_object_unref (file);
+
+  g_vfs_register_uri_scheme (vfs, "test", NULL);
+
+  file = g_file_new_for_uri ("test:///foo");
+  g_assert_null (g_object_get_data (G_OBJECT (file), "testfile"));
+  g_object_unref (file);
+}
+
 static void
 test_local (void)
 {
@@ -49,6 +83,7 @@ main (int argc, char *argv[])
   g_test_init (&argc, &argv, NULL);
 
   g_test_add_func ("/gvfs/local", test_local);
+  g_test_add_func ("/gvfs/register-scheme", test_register_scheme);
 
   return g_test_run ();
 }
-- 
2.1.4