2c221e4e0f23a1ddb96b98449da5c436251cf64b
[platform/upstream/glib.git] / gio / giomodule.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include <string.h>
26
27 #include "giomodule.h"
28 #include "giomodule-priv.h"
29 #include "gmemorysettingsbackend.h"
30 #include "glocalfilemonitor.h"
31 #include "glocaldirectorymonitor.h"
32 #include "gnativevolumemonitor.h"
33 #include "gproxyresolver.h"
34 #include "gproxy.h"
35 #include "gsocks4proxy.h"
36 #include "gsocks4aproxy.h"
37 #include "gsocks5proxy.h"
38 #include "gvfs.h"
39 #ifdef G_OS_UNIX
40 #include "gdesktopappinfo.h"
41 #endif
42 #ifdef G_OS_WIN32
43 #include "gregistrysettingsbackend.h"
44 #endif
45 #include <glib/gstdio.h>
46
47 /**
48  * SECTION:giomodule
49  * @short_description: Loadable GIO Modules
50  * @include: gio/gio.h
51  *
52  * Provides an interface and default functions for loading and unloading 
53  * modules. This is used internally to make GIO extensible, but can also
54  * be used by others to implement module loading.
55  * 
56  **/
57
58 /**
59  * SECTION:extensionpoints
60  * @short_description: Extension Points
61  * @include: gio.h
62  * @see_also: <link linkend="extending-gio">Extending GIO</link>
63  *
64  * #GIOExtensionPoint provides a mechanism for modules to extend the
65  * functionality of the library or application that loaded it in an 
66  * organized fashion.  
67  *
68  * An extension point is identified by a name, and it may optionally
69  * require that any implementation must by of a certain type (or derived
70  * thereof). Use g_io_extension_point_register() to register an
71  * extension point, and g_io_extension_point_set_required_type() to
72  * set a required type.
73  *
74  * A module can implement an extension point by specifying the #GType 
75  * that implements the functionality. Additionally, each implementation
76  * of an extension point has a name, and a priority. Use
77  * g_io_extension_point_implement() to implement an extension point.
78  * 
79  *  |[
80  *  GIOExtensionPoint *ep;
81  *
82  *  /&ast; Register an extension point &ast;/
83  *  ep = g_io_extension_point_register ("my-extension-point");
84  *  g_io_extension_point_set_required_type (ep, MY_TYPE_EXAMPLE);
85  *  ]|
86  *
87  *  |[
88  *  /&ast; Implement an extension point &ast;/
89  *  G_DEFINE_TYPE (MyExampleImpl, my_example_impl, MY_TYPE_EXAMPLE);
90  *  g_io_extension_point_implement ("my-extension-point",
91  *                                  my_example_impl_get_type (),
92  *                                  "my-example",
93  *                                  10);
94  *  ]|
95  *
96  *  It is up to the code that registered the extension point how
97  *  it uses the implementations that have been associated with it.
98  *  Depending on the use case, it may use all implementations, or
99  *  only the one with the highest priority, or pick a specific
100  *  one by name.
101  *
102  *  To avoid opening all modules just to find out what extension
103  *  points they implement, GIO makes use of a caching mechanism,
104  *  see <link linkend="gio-querymodules">gio-querymodules</link>.
105  *  You are expected to run this command after installing a
106  *  GIO module.
107  */
108 struct _GIOModule {
109   GTypeModule parent_instance;
110
111   gchar       *filename;
112   GModule     *library;
113   gboolean     initialized; /* The module was loaded at least once */
114
115   void (* load)   (GIOModule *module);
116   void (* unload) (GIOModule *module);
117 };
118
119 struct _GIOModuleClass
120 {
121   GTypeModuleClass parent_class;
122
123 };
124
125 static void      g_io_module_finalize      (GObject      *object);
126 static gboolean  g_io_module_load_module   (GTypeModule  *gmodule);
127 static void      g_io_module_unload_module (GTypeModule  *gmodule);
128
129 struct _GIOExtension {
130   char *name;
131   GType type;
132   gint priority;
133 };
134
135 struct _GIOExtensionPoint {
136   GType required_type;
137   char *name;
138   GList *extensions;
139   GList *lazy_load_modules;
140 };
141
142 static GHashTable *extension_points = NULL;
143 G_LOCK_DEFINE_STATIC(extension_points);
144
145 G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE);
146
147 static void
148 g_io_module_class_init (GIOModuleClass *class)
149 {
150   GObjectClass     *object_class      = G_OBJECT_CLASS (class);
151   GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
152
153   object_class->finalize     = g_io_module_finalize;
154
155   type_module_class->load    = g_io_module_load_module;
156   type_module_class->unload  = g_io_module_unload_module;
157 }
158
159 static void
160 g_io_module_init (GIOModule *module)
161 {
162 }
163
164 static void
165 g_io_module_finalize (GObject *object)
166 {
167   GIOModule *module = G_IO_MODULE (object);
168
169   g_free (module->filename);
170
171   G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
172 }
173
174 static gboolean
175 g_io_module_load_module (GTypeModule *gmodule)
176 {
177   GIOModule *module = G_IO_MODULE (gmodule);
178
179   if (!module->filename)
180     {
181       g_warning ("GIOModule path not set");
182       return FALSE;
183     }
184
185   module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
186
187   if (!module->library)
188     {
189       g_printerr ("%s\n", g_module_error ());
190       return FALSE;
191     }
192
193   /* Make sure that the loaded library contains the required methods */
194   if (! g_module_symbol (module->library,
195                          "g_io_module_load",
196                          (gpointer) &module->load) ||
197       ! g_module_symbol (module->library,
198                          "g_io_module_unload",
199                          (gpointer) &module->unload))
200     {
201       g_printerr ("%s\n", g_module_error ());
202       g_module_close (module->library);
203
204       return FALSE;
205     }
206
207   /* Initialize the loaded module */
208   module->load (module);
209   module->initialized = TRUE;
210
211   return TRUE;
212 }
213
214 static void
215 g_io_module_unload_module (GTypeModule *gmodule)
216 {
217   GIOModule *module = G_IO_MODULE (gmodule);
218
219   module->unload (module);
220
221   g_module_close (module->library);
222   module->library = NULL;
223
224   module->load   = NULL;
225   module->unload = NULL;
226 }
227
228 /**
229  * g_io_module_new:
230  * @filename: filename of the shared library module.
231  * 
232  * Creates a new GIOModule that will load the specific
233  * shared library when in use.
234  * 
235  * Returns: a #GIOModule from given @filename, 
236  * or %NULL on error.
237  **/
238 GIOModule *
239 g_io_module_new (const gchar *filename)
240 {
241   GIOModule *module;
242
243   g_return_val_if_fail (filename != NULL, NULL);
244
245   module = g_object_new (G_IO_TYPE_MODULE, NULL);
246   module->filename = g_strdup (filename);
247
248   return module;
249 }
250
251 static gboolean
252 is_valid_module_name (const gchar *basename)
253 {
254 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
255   return
256     g_str_has_prefix (basename, "lib") &&
257     g_str_has_suffix (basename, ".so");
258 #else
259   return g_str_has_suffix (basename, ".dll");
260 #endif
261 }
262
263 /**
264  * g_io_modules_scan_all_in_directory:
265  * @dirname: pathname for a directory containing modules to scan.
266  *
267  * Scans all the modules in the specified directory, ensuring that
268  * any extension point implemented by a module is registered.
269  *
270  * This may not actually load and initialize all the types in each
271  * module, some modules may be lazily loaded and initialized when
272  * an extension point it implementes is used with e.g.
273  * g_io_extension_point_get_extensions() or
274  * g_io_extension_point_get_extension_by_name().
275  *
276  * If you need to guarantee that all types are loaded in all the modules,
277  * use g_io_modules_scan_all_in_directory().
278  *
279  * Since: 2.24
280  **/
281 void
282 g_io_modules_scan_all_in_directory (const char *dirname)
283 {
284   const gchar *name;
285   char *filename;
286   GDir *dir;
287   GStatBuf statbuf;
288   char *data;
289   time_t cache_mtime;
290   GHashTable *cache;
291
292   if (!g_module_supported ())
293     return;
294
295   dir = g_dir_open (dirname, 0, NULL);
296   if (!dir)
297     return;
298
299   filename = g_build_filename (dirname, "giomodule.cache", NULL);
300
301   cache = g_hash_table_new_full (g_str_hash, g_str_equal,
302                                  g_free, (GDestroyNotify)g_strfreev);
303
304   cache_mtime = 0;
305   if (g_stat (filename, &statbuf) == 0 &&
306       g_file_get_contents (filename, &data, NULL, NULL))
307     {
308       char **lines;
309       int i;
310
311       /* Cache mtime is the time the cache file was created, any file
312        * that has a ctime before this was created then and not modified
313        * since then (userspace can't change ctime). Its possible to change
314        * the ctime forward without changing the file content, by e.g.
315        * chmoding the file, but this is uncommon and will only cause us
316        * to not use the cache so will not cause bugs.
317        */
318       cache_mtime = statbuf.st_mtime;
319
320       lines = g_strsplit (data, "\n", -1);
321       g_free (data);
322
323       for (i = 0;  lines[i] != NULL; i++)
324         {
325           char *line = lines[i];
326           char *file;
327           char *colon;
328           char **extension_points;
329
330           if (line[0] == '#')
331             continue;
332
333           colon = strchr (line, ':');
334           if (colon == NULL || line == colon)
335             continue; /* Invalid line, ignore */
336
337           *colon = 0; /* terminate filename */
338           file = g_strdup (line);
339           colon++; /* after colon */
340
341           while (g_ascii_isspace (*colon))
342             colon++;
343
344           extension_points = g_strsplit (colon, ",", -1);
345           g_hash_table_insert (cache, file, extension_points);
346         }
347       g_strfreev (lines);
348     }
349
350   while ((name = g_dir_read_name (dir)))
351     {
352       if (is_valid_module_name (name))
353         {
354           GIOExtensionPoint *extension_point;
355           GIOModule *module;
356           gchar *path;
357           char **extension_points;
358           int i;
359
360           path = g_build_filename (dirname, name, NULL);
361           module = g_io_module_new (path);
362
363           extension_points = g_hash_table_lookup (cache, name);
364           if (extension_points != NULL &&
365               g_stat (path, &statbuf) == 0 &&
366               statbuf.st_ctime <= cache_mtime)
367             {
368               /* Lazy load/init the library when first required */
369               for (i = 0; extension_points[i] != NULL; i++)
370                 {
371                   extension_point =
372                     g_io_extension_point_register (extension_points[i]);
373                   extension_point->lazy_load_modules =
374                     g_list_prepend (extension_point->lazy_load_modules,
375                                     module);
376                 }
377             }
378           else
379             {
380               /* Try to load and init types */
381               if (g_type_module_use (G_TYPE_MODULE (module)))
382                 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
383               else
384                 { /* Failure to load */
385                   g_printerr ("Failed to load module: %s\n", path);
386                   g_object_unref (module);
387                   g_free (path);
388                   continue;
389                 }
390             }
391
392           g_free (path);
393         }
394     }
395
396   g_dir_close (dir);
397
398   g_hash_table_destroy (cache);
399
400   g_free (filename);
401 }
402
403
404 /**
405  * g_io_modules_load_all_in_directory:
406  * @dirname: pathname for a directory containing modules to load.
407  *
408  * Loads all the modules in the specified directory.
409  *
410  * If don't require all modules to be initialized (and thus registering
411  * all gtypes) then you can use g_io_modules_scan_all_in_directory()
412  * which allows delayed/lazy loading of modules.
413  *
414  * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
415  *      from the directory,
416  *      All the modules are loaded into memory, if you want to
417  *      unload them (enabling on-demand loading) you must call
418  *      g_type_module_unuse() on all the modules. Free the list
419  *      with g_list_free().
420  **/
421 GList *
422 g_io_modules_load_all_in_directory (const char *dirname)
423 {
424   const gchar *name;
425   GDir        *dir;
426   GList *modules;
427
428   if (!g_module_supported ())
429     return NULL;
430
431   dir = g_dir_open (dirname, 0, NULL);
432   if (!dir)
433     return NULL;
434
435   modules = NULL;
436   while ((name = g_dir_read_name (dir)))
437     {
438       if (is_valid_module_name (name))
439         {
440           GIOModule *module;
441           gchar     *path;
442
443           path = g_build_filename (dirname, name, NULL);
444           module = g_io_module_new (path);
445
446           if (!g_type_module_use (G_TYPE_MODULE (module)))
447             {
448               g_printerr ("Failed to load module: %s\n", path);
449               g_object_unref (module);
450               g_free (path);
451               continue;
452             }
453           
454           g_free (path);
455
456           modules = g_list_prepend (modules, module);
457         }
458     }
459   
460   g_dir_close (dir);
461
462   return modules;
463 }
464
465 G_LOCK_DEFINE_STATIC (registered_extensions);
466 G_LOCK_DEFINE_STATIC (loaded_dirs);
467
468 extern GType _g_fen_directory_monitor_get_type (void);
469 extern GType _g_fen_file_monitor_get_type (void);
470 extern GType _g_inotify_directory_monitor_get_type (void);
471 extern GType _g_inotify_file_monitor_get_type (void);
472 extern GType _g_unix_volume_monitor_get_type (void);
473 extern GType _g_local_vfs_get_type (void);
474
475 extern GType _g_win32_volume_monitor_get_type (void);
476 extern GType g_win32_directory_monitor_get_type (void);
477 extern GType _g_winhttp_vfs_get_type (void);
478
479 extern GType _g_dummy_proxy_resolver_get_type (void);
480
481 #ifdef G_PLATFORM_WIN32
482
483 #include <windows.h>
484
485 static HMODULE gio_dll = NULL;
486
487 #ifdef DLL_EXPORT
488
489 BOOL WINAPI
490 DllMain (HINSTANCE hinstDLL,
491          DWORD     fdwReason,
492          LPVOID    lpvReserved)
493 {
494   if (fdwReason == DLL_PROCESS_ATTACH)
495       gio_dll = hinstDLL;
496
497   return TRUE;
498 }
499
500 #endif
501
502 #undef GIO_MODULE_DIR
503
504 /* GIO_MODULE_DIR is used only in code called just once,
505  * so no problem leaking this
506  */
507 #define GIO_MODULE_DIR \
508   g_build_filename (g_win32_get_package_installation_directory_of_module (gio_dll), \
509                     "lib/gio/modules", \
510                     NULL)
511
512 #endif
513
514 void
515 _g_io_modules_ensure_extension_points_registered (void)
516 {
517   static gboolean registered_extensions = FALSE;
518   GIOExtensionPoint *ep;
519
520   G_LOCK (registered_extensions);
521   
522   if (!registered_extensions)
523     {
524       registered_extensions = TRUE;
525       
526 #ifdef G_OS_UNIX
527       ep = g_io_extension_point_register (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
528       g_io_extension_point_set_required_type (ep, G_TYPE_DESKTOP_APP_INFO_LOOKUP);
529 #endif
530       
531       ep = g_io_extension_point_register (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME);
532       g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_DIRECTORY_MONITOR);
533       
534       ep = g_io_extension_point_register (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
535       g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
536       
537       ep = g_io_extension_point_register (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
538       g_io_extension_point_set_required_type (ep, G_TYPE_VOLUME_MONITOR);
539       
540       ep = g_io_extension_point_register (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
541       g_io_extension_point_set_required_type (ep, G_TYPE_NATIVE_VOLUME_MONITOR);
542       
543       ep = g_io_extension_point_register (G_VFS_EXTENSION_POINT_NAME);
544       g_io_extension_point_set_required_type (ep, G_TYPE_VFS);
545
546       ep = g_io_extension_point_register ("gsettings-backend");
547       g_io_extension_point_set_required_type (ep, G_TYPE_OBJECT);
548
549       ep = g_io_extension_point_register (G_PROXY_RESOLVER_EXTENSION_POINT_NAME);
550       g_io_extension_point_set_required_type (ep, G_TYPE_PROXY_RESOLVER);
551
552       ep = g_io_extension_point_register (G_PROXY_EXTENSION_POINT_NAME);
553       g_io_extension_point_set_required_type (ep, G_TYPE_PROXY);
554     }
555   
556   G_UNLOCK (registered_extensions);
557  }
558
559 void
560 _g_io_modules_ensure_loaded (void)
561 {
562   static gboolean loaded_dirs = FALSE;
563   const char *module_path;
564
565   _g_io_modules_ensure_extension_points_registered ();
566   
567   G_LOCK (loaded_dirs);
568
569   if (!loaded_dirs)
570     {
571       loaded_dirs = TRUE;
572
573       g_io_modules_scan_all_in_directory (GIO_MODULE_DIR);
574
575       module_path = g_getenv ("GIO_EXTRA_MODULES");
576
577       if (module_path)
578         {
579           gchar **paths;
580           int i;
581
582           paths = g_strsplit (module_path, ":", 0);
583
584           for (i = 0; paths[i] != NULL; i++)
585             g_io_modules_scan_all_in_directory (paths[i]);
586
587           g_strfreev (paths);
588         }
589
590       /* Initialize types from built-in "modules" */
591       g_memory_settings_backend_get_type ();
592 #if defined(HAVE_SYS_INOTIFY_H) || defined(HAVE_LINUX_INOTIFY_H)
593       _g_inotify_directory_monitor_get_type ();
594       _g_inotify_file_monitor_get_type ();
595 #endif
596 #if defined(HAVE_FEN)
597       _g_fen_directory_monitor_get_type ();
598       _g_fen_file_monitor_get_type ();
599 #endif
600 #ifdef G_OS_WIN32
601       _g_win32_volume_monitor_get_type ();
602       g_win32_directory_monitor_get_type ();
603       g_registry_backend_get_type ();
604 #endif
605 #ifdef G_OS_UNIX
606       _g_unix_volume_monitor_get_type ();
607 #endif
608 #ifdef G_OS_WIN32
609       _g_winhttp_vfs_get_type ();
610 #endif
611       _g_local_vfs_get_type ();
612       _g_dummy_proxy_resolver_get_type ();
613       _g_socks4a_proxy_get_type ();
614       _g_socks4_proxy_get_type ();
615       _g_socks5_proxy_get_type ();
616     }
617
618   G_UNLOCK (loaded_dirs);
619 }
620
621 static void
622 g_io_extension_point_free (GIOExtensionPoint *ep)
623 {
624   g_free (ep->name);
625   g_free (ep);
626 }
627
628 /**
629  * g_io_extension_point_register:
630  * @name: The name of the extension point
631  *
632  * Registers an extension point.
633  *
634  * Returns: the new #GIOExtensionPoint. This object is owned by GIO
635  *    and should not be freed
636  */
637 GIOExtensionPoint *
638 g_io_extension_point_register (const char *name)
639 {
640   GIOExtensionPoint *ep;
641   
642   G_LOCK (extension_points);
643   if (extension_points == NULL)
644     extension_points = g_hash_table_new_full (g_str_hash,
645                                               g_str_equal,
646                                               NULL,
647                                               (GDestroyNotify)g_io_extension_point_free);
648
649   ep = g_hash_table_lookup (extension_points, name);
650   if (ep != NULL)
651     {
652       G_UNLOCK (extension_points);
653       return ep;
654     }
655
656   ep = g_new0 (GIOExtensionPoint, 1);
657   ep->name = g_strdup (name);
658   
659   g_hash_table_insert (extension_points, ep->name, ep);
660   
661   G_UNLOCK (extension_points);
662
663   return ep;
664 }
665
666 /**
667  * g_io_extension_point_lookup:
668  * @name: the name of the extension point
669  *
670  * Looks up an existing extension point.
671  *
672  * Returns: the #GIOExtensionPoint, or %NULL if there is no
673  *    registered extension point with the given name
674  */
675 GIOExtensionPoint *
676 g_io_extension_point_lookup (const char *name)
677 {
678   GIOExtensionPoint *ep;
679
680   G_LOCK (extension_points);
681   ep = NULL;
682   if (extension_points != NULL)
683     ep = g_hash_table_lookup (extension_points, name);
684   
685   G_UNLOCK (extension_points);
686
687   return ep;
688   
689 }
690
691 /**
692  * g_io_extension_point_set_required_type:
693  * @extension_point: a #GIOExtensionPoint
694  * @type: the #GType to require
695  *
696  * Sets the required type for @extension_point to @type. 
697  * All implementations must henceforth have this type.
698  */
699 void
700 g_io_extension_point_set_required_type (GIOExtensionPoint *extension_point,
701                                         GType              type)
702 {
703   extension_point->required_type = type;
704 }
705
706 /**
707  * g_io_extension_point_get_required_type:
708  * @extension_point: a #GIOExtensionPoint
709  *
710  * Gets the required type for @extension_point.
711  *
712  * Returns: the #GType that all implementations must have, 
713  *     or #G_TYPE_INVALID if the extension point has no required type
714  */
715 GType
716 g_io_extension_point_get_required_type (GIOExtensionPoint *extension_point)
717 {
718   return extension_point->required_type;
719 }
720
721 void
722 lazy_load_modules (GIOExtensionPoint *extension_point)
723 {
724   GIOModule *module;
725   GList *l;
726
727   for (l = extension_point->lazy_load_modules; l != NULL; l = l->next)
728     {
729       module = l->data;
730
731       if (!module->initialized)
732         {
733           if (g_type_module_use (G_TYPE_MODULE (module)))
734             g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
735           else
736             g_printerr ("Failed to load module: %s\n",
737                         module->filename);
738         }
739     }
740 }
741
742 /**
743  * g_io_extension_point_get_extensions:
744  * @extension_point: a #GIOExtensionPoint
745  *
746  * Gets a list of all extensions that implement this extension point.
747  * The list is sorted by priority, beginning with the highest priority.
748  *
749  * Returns: (element-type GIOExtension) (transfer none): a #GList of
750  * #GIOExtension<!-- -->s. The list is owned by GIO and should not be
751  * modified.
752  */
753 GList *
754 g_io_extension_point_get_extensions (GIOExtensionPoint *extension_point)
755 {
756   lazy_load_modules (extension_point);
757   return extension_point->extensions;
758 }
759
760 /**
761  * g_io_extension_point_get_extension_by_name:
762  * @extension_point: a #GIOExtensionPoint
763  * @name: the name of the extension to get
764  *
765  * Finds a #GIOExtension for an extension point by name.
766  *
767  * Returns: (transfer none): the #GIOExtension for @extension_point that has the
768  *    given name, or %NULL if there is no extension with that name
769  */
770 GIOExtension *
771 g_io_extension_point_get_extension_by_name (GIOExtensionPoint *extension_point,
772                                             const char        *name)
773 {
774   GList *l;
775
776   lazy_load_modules (extension_point);
777   for (l = extension_point->extensions; l != NULL; l = l->next)
778     {
779       GIOExtension *e = l->data;
780
781       if (e->name != NULL &&
782           strcmp (e->name, name) == 0)
783         return e;
784     }
785   
786   return NULL;
787 }
788
789 static gint
790 extension_prio_compare (gconstpointer  a,
791                         gconstpointer  b)
792 {
793   const GIOExtension *extension_a = a, *extension_b = b;
794
795   if (extension_a->priority > extension_b->priority)
796     return -1;
797
798   if (extension_b->priority > extension_a->priority)
799     return 1;
800
801   return 0;
802 }
803
804 /**
805  * g_io_extension_point_implement:
806  * @extension_point_name: the name of the extension point
807  * @type: the #GType to register as extension 
808  * @extension_name: the name for the extension
809  * @priority: the priority for the extension
810  *
811  * Registers @type as extension for the extension point with name
812  * @extension_point_name. 
813  *
814  * If @type has already been registered as an extension for this 
815  * extension point, the existing #GIOExtension object is returned.
816  *
817  * Returns: a #GIOExtension object for #GType
818  */
819 GIOExtension *
820 g_io_extension_point_implement (const char *extension_point_name,
821                                 GType       type,
822                                 const char *extension_name,
823                                 gint        priority)
824 {
825   GIOExtensionPoint *extension_point;
826   GIOExtension *extension;
827   GList *l;
828
829   g_return_val_if_fail (extension_point_name != NULL, NULL);
830
831   extension_point = g_io_extension_point_lookup (extension_point_name);
832   if (extension_point == NULL)
833     {
834       g_warning ("Tried to implement non-registered extension point %s", extension_point_name);
835       return NULL;
836     }
837   
838   if (extension_point->required_type != 0 &&
839       !g_type_is_a (type, extension_point->required_type))
840     {
841       g_warning ("Tried to register an extension of the type %s to extension point %s. "
842                  "Expected type is %s.",
843                  g_type_name (type),
844                  extension_point_name, 
845                  g_type_name (extension_point->required_type));
846       return NULL;
847     }      
848
849   /* It's safe to register the same type multiple times */
850   for (l = extension_point->extensions; l != NULL; l = l->next)
851     {
852       extension = l->data;
853       if (extension->type == type)
854         return extension;
855     }
856   
857   extension = g_slice_new0 (GIOExtension);
858   extension->type = type;
859   extension->name = g_strdup (extension_name);
860   extension->priority = priority;
861   
862   extension_point->extensions = g_list_insert_sorted (extension_point->extensions,
863                                                       extension, extension_prio_compare);
864   
865   return extension;
866 }
867
868 /**
869  * g_io_extension_ref_class:
870  * @extension: a #GIOExtension
871  *
872  * Gets a reference to the class for the type that is 
873  * associated with @extension.
874  *
875  * Returns: (transfer full): the #GTypeClass for the type of @extension
876  */
877 GTypeClass *
878 g_io_extension_ref_class (GIOExtension *extension)
879 {
880   return g_type_class_ref (extension->type);
881 }
882
883 /**
884  * g_io_extension_get_type:
885  * @extension: a #GIOExtension
886  *
887  * Gets the type associated with @extension.
888  *
889  * Returns: the type of @extension
890  */
891 GType
892 g_io_extension_get_type (GIOExtension *extension)
893 {
894   return extension->type;
895 }
896
897 /**
898  * g_io_extension_get_name:
899  * @extension: a #GIOExtension
900  *
901  * Gets the name under which @extension was registered.
902  *
903  * Note that the same type may be registered as extension
904  * for multiple extension points, under different names.
905  *
906  * Returns: the name of @extension.
907  */
908 const char *
909 g_io_extension_get_name (GIOExtension *extension)
910 {
911   return extension->name;
912 }
913
914 /**
915  * g_io_extension_get_priority:
916  * @extension: a #GIOExtension
917  *
918  * Gets the priority with which @extension was registered.
919  *
920  * Returns: the priority of @extension
921  */
922 gint
923 g_io_extension_get_priority (GIOExtension *extension)
924 {
925   return extension->priority;
926 }