39a38ff5397e5cf07594d49276838c2d9733f04b
[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: a list of #GIOModules loaded from the directory,
415  *      All the modules are loaded into memory, if you want to
416  *      unload them (enabling on-demand loading) you must call
417  *      g_type_module_unuse() on all the modules. Free the list
418  *      with g_list_free().
419  **/
420 GList *
421 g_io_modules_load_all_in_directory (const char *dirname)
422 {
423   const gchar *name;
424   GDir        *dir;
425   GList *modules;
426
427   if (!g_module_supported ())
428     return NULL;
429
430   dir = g_dir_open (dirname, 0, NULL);
431   if (!dir)
432     return NULL;
433
434   modules = NULL;
435   while ((name = g_dir_read_name (dir)))
436     {
437       if (is_valid_module_name (name))
438         {
439           GIOModule *module;
440           gchar     *path;
441
442           path = g_build_filename (dirname, name, NULL);
443           module = g_io_module_new (path);
444
445           if (!g_type_module_use (G_TYPE_MODULE (module)))
446             {
447               g_printerr ("Failed to load module: %s\n", path);
448               g_object_unref (module);
449               g_free (path);
450               continue;
451             }
452           
453           g_free (path);
454
455           modules = g_list_prepend (modules, module);
456         }
457     }
458   
459   g_dir_close (dir);
460
461   return modules;
462 }
463
464 G_LOCK_DEFINE_STATIC (registered_extensions);
465 G_LOCK_DEFINE_STATIC (loaded_dirs);
466
467 extern GType _g_fen_directory_monitor_get_type (void);
468 extern GType _g_fen_file_monitor_get_type (void);
469 extern GType _g_inotify_directory_monitor_get_type (void);
470 extern GType _g_inotify_file_monitor_get_type (void);
471 extern GType _g_unix_volume_monitor_get_type (void);
472 extern GType _g_local_vfs_get_type (void);
473
474 extern GType _g_win32_volume_monitor_get_type (void);
475 extern GType g_win32_directory_monitor_get_type (void);
476 extern GType _g_winhttp_vfs_get_type (void);
477
478 extern GType _g_dummy_proxy_resolver_get_type (void);
479
480 #ifdef G_PLATFORM_WIN32
481
482 #include <windows.h>
483
484 static HMODULE gio_dll = NULL;
485
486 #ifdef DLL_EXPORT
487
488 BOOL WINAPI
489 DllMain (HINSTANCE hinstDLL,
490          DWORD     fdwReason,
491          LPVOID    lpvReserved)
492 {
493   if (fdwReason == DLL_PROCESS_ATTACH)
494       gio_dll = hinstDLL;
495
496   return TRUE;
497 }
498
499 #endif
500
501 #undef GIO_MODULE_DIR
502
503 /* GIO_MODULE_DIR is used only in code called just once,
504  * so no problem leaking this
505  */
506 #define GIO_MODULE_DIR \
507   g_build_filename (g_win32_get_package_installation_directory_of_module (gio_dll), \
508                     "lib/gio/modules", \
509                     NULL)
510
511 #endif
512
513 void
514 _g_io_modules_ensure_extension_points_registered (void)
515 {
516   static gboolean registered_extensions = FALSE;
517   GIOExtensionPoint *ep;
518
519   G_LOCK (registered_extensions);
520   
521   if (!registered_extensions)
522     {
523       registered_extensions = TRUE;
524       
525 #ifdef G_OS_UNIX
526       ep = g_io_extension_point_register (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
527       g_io_extension_point_set_required_type (ep, G_TYPE_DESKTOP_APP_INFO_LOOKUP);
528 #endif
529       
530       ep = g_io_extension_point_register (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME);
531       g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_DIRECTORY_MONITOR);
532       
533       ep = g_io_extension_point_register (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
534       g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
535       
536       ep = g_io_extension_point_register (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
537       g_io_extension_point_set_required_type (ep, G_TYPE_VOLUME_MONITOR);
538       
539       ep = g_io_extension_point_register (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
540       g_io_extension_point_set_required_type (ep, G_TYPE_NATIVE_VOLUME_MONITOR);
541       
542       ep = g_io_extension_point_register (G_VFS_EXTENSION_POINT_NAME);
543       g_io_extension_point_set_required_type (ep, G_TYPE_VFS);
544
545       ep = g_io_extension_point_register ("gsettings-backend");
546       g_io_extension_point_set_required_type (ep, G_TYPE_OBJECT);
547
548       ep = g_io_extension_point_register (G_PROXY_RESOLVER_EXTENSION_POINT_NAME);
549       g_io_extension_point_set_required_type (ep, G_TYPE_PROXY_RESOLVER);
550
551       ep = g_io_extension_point_register (G_PROXY_EXTENSION_POINT_NAME);
552       g_io_extension_point_set_required_type (ep, G_TYPE_PROXY);
553     }
554   
555   G_UNLOCK (registered_extensions);
556  }
557
558 void
559 _g_io_modules_ensure_loaded (void)
560 {
561   static gboolean loaded_dirs = FALSE;
562   const char *module_path;
563
564   _g_io_modules_ensure_extension_points_registered ();
565   
566   G_LOCK (loaded_dirs);
567
568   if (!loaded_dirs)
569     {
570       loaded_dirs = TRUE;
571
572       g_io_modules_scan_all_in_directory (GIO_MODULE_DIR);
573
574       module_path = g_getenv ("GIO_EXTRA_MODULES");
575
576       if (module_path)
577         {
578           gchar **paths;
579           int i;
580
581           paths = g_strsplit (module_path, ":", 0);
582
583           for (i = 0; paths[i] != NULL; i++)
584             g_io_modules_scan_all_in_directory (paths[i]);
585
586           g_strfreev (paths);
587         }
588
589       /* Initialize types from built-in "modules" */
590       g_memory_settings_backend_get_type ();
591 #if defined(HAVE_SYS_INOTIFY_H) || defined(HAVE_LINUX_INOTIFY_H)
592       _g_inotify_directory_monitor_get_type ();
593       _g_inotify_file_monitor_get_type ();
594 #endif
595 #if defined(HAVE_FEN)
596       _g_fen_directory_monitor_get_type ();
597       _g_fen_file_monitor_get_type ();
598 #endif
599 #ifdef G_OS_WIN32
600       _g_win32_volume_monitor_get_type ();
601       g_win32_directory_monitor_get_type ();
602       g_registry_backend_get_type ();
603 #endif
604 #ifdef G_OS_UNIX
605       _g_unix_volume_monitor_get_type ();
606 #endif
607 #ifdef G_OS_WIN32
608       _g_winhttp_vfs_get_type ();
609 #endif
610       _g_local_vfs_get_type ();
611       _g_dummy_proxy_resolver_get_type ();
612       _g_socks4a_proxy_get_type ();
613       _g_socks4_proxy_get_type ();
614       _g_socks5_proxy_get_type ();
615     }
616
617   G_UNLOCK (loaded_dirs);
618 }
619
620 static void
621 g_io_extension_point_free (GIOExtensionPoint *ep)
622 {
623   g_free (ep->name);
624   g_free (ep);
625 }
626
627 /**
628  * g_io_extension_point_register:
629  * @name: The name of the extension point
630  *
631  * Registers an extension point.
632  *
633  * Returns: the new #GIOExtensionPoint. This object is owned by GIO
634  *    and should not be freed
635  */
636 GIOExtensionPoint *
637 g_io_extension_point_register (const char *name)
638 {
639   GIOExtensionPoint *ep;
640   
641   G_LOCK (extension_points);
642   if (extension_points == NULL)
643     extension_points = g_hash_table_new_full (g_str_hash,
644                                               g_str_equal,
645                                               NULL,
646                                               (GDestroyNotify)g_io_extension_point_free);
647
648   ep = g_hash_table_lookup (extension_points, name);
649   if (ep != NULL)
650     {
651       G_UNLOCK (extension_points);
652       return ep;
653     }
654
655   ep = g_new0 (GIOExtensionPoint, 1);
656   ep->name = g_strdup (name);
657   
658   g_hash_table_insert (extension_points, ep->name, ep);
659   
660   G_UNLOCK (extension_points);
661
662   return ep;
663 }
664
665 /**
666  * g_io_extension_point_lookup:
667  * @name: the name of the extension point
668  *
669  * Looks up an existing extension point.
670  *
671  * Returns: the #GIOExtensionPoint, or %NULL if there is no
672  *    registered extension point with the given name
673  */
674 GIOExtensionPoint *
675 g_io_extension_point_lookup (const char *name)
676 {
677   GIOExtensionPoint *ep;
678
679   G_LOCK (extension_points);
680   ep = NULL;
681   if (extension_points != NULL)
682     ep = g_hash_table_lookup (extension_points, name);
683   
684   G_UNLOCK (extension_points);
685
686   return ep;
687   
688 }
689
690 /**
691  * g_io_extension_point_set_required_type:
692  * @extension_point: a #GIOExtensionPoint
693  * @type: the #GType to require
694  *
695  * Sets the required type for @extension_point to @type. 
696  * All implementations must henceforth have this type.
697  */
698 void
699 g_io_extension_point_set_required_type (GIOExtensionPoint *extension_point,
700                                         GType              type)
701 {
702   extension_point->required_type = type;
703 }
704
705 /**
706  * g_io_extension_point_get_required_type:
707  * @extension_point: a #GIOExtensionPoint
708  *
709  * Gets the required type for @extension_point.
710  *
711  * Returns: the #GType that all implementations must have, 
712  *     or #G_TYPE_INVALID if the extension point has no required type
713  */
714 GType
715 g_io_extension_point_get_required_type (GIOExtensionPoint *extension_point)
716 {
717   return extension_point->required_type;
718 }
719
720 void
721 lazy_load_modules (GIOExtensionPoint *extension_point)
722 {
723   GIOModule *module;
724   GList *l;
725
726   for (l = extension_point->lazy_load_modules; l != NULL; l = l->next)
727     {
728       module = l->data;
729
730       if (!module->initialized)
731         {
732           if (g_type_module_use (G_TYPE_MODULE (module)))
733             g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
734           else
735             g_printerr ("Failed to load module: %s\n",
736                         module->filename);
737         }
738     }
739 }
740
741 /**
742  * g_io_extension_point_get_extensions:
743  * @extension_point: a #GIOExtensionPoint
744  *
745  * Gets a list of all extensions that implement this extension point.
746  * The list is sorted by priority, beginning with the highest priority.
747  *
748  * Returns: (element-type GIOExtension) (transfer none): a #GList of
749  * #GIOExtension<!-- -->s. The list is owned by GIO and should not be
750  * modified.
751  */
752 GList *
753 g_io_extension_point_get_extensions (GIOExtensionPoint *extension_point)
754 {
755   lazy_load_modules (extension_point);
756   return extension_point->extensions;
757 }
758
759 /**
760  * g_io_extension_point_get_extension_by_name:
761  * @extension_point: a #GIOExtensionPoint
762  * @name: the name of the extension to get
763  *
764  * Finds a #GIOExtension for an extension point by name.
765  *
766  * Returns: the #GIOExtension for @extension_point that has the
767  *    given name, or %NULL if there is no extension with that name
768  */
769 GIOExtension *
770 g_io_extension_point_get_extension_by_name (GIOExtensionPoint *extension_point,
771                                             const char        *name)
772 {
773   GList *l;
774
775   lazy_load_modules (extension_point);
776   for (l = extension_point->extensions; l != NULL; l = l->next)
777     {
778       GIOExtension *e = l->data;
779
780       if (e->name != NULL &&
781           strcmp (e->name, name) == 0)
782         return e;
783     }
784   
785   return NULL;
786 }
787
788 static gint
789 extension_prio_compare (gconstpointer  a,
790                         gconstpointer  b)
791 {
792   const GIOExtension *extension_a = a, *extension_b = b;
793
794   return extension_b->priority - extension_a->priority;
795 }
796
797 /**
798  * g_io_extension_point_implement:
799  * @extension_point_name: the name of the extension point
800  * @type: the #GType to register as extension 
801  * @extension_name: the name for the extension
802  * @priority: the priority for the extension
803  *
804  * Registers @type as extension for the extension point with name
805  * @extension_point_name. 
806  *
807  * If @type has already been registered as an extension for this 
808  * extension point, the existing #GIOExtension object is returned.
809  *
810  * Returns: a #GIOExtension object for #GType
811  */
812 GIOExtension *
813 g_io_extension_point_implement (const char *extension_point_name,
814                                 GType       type,
815                                 const char *extension_name,
816                                 gint        priority)
817 {
818   GIOExtensionPoint *extension_point;
819   GIOExtension *extension;
820   GList *l;
821
822   g_return_val_if_fail (extension_point_name != NULL, NULL);
823
824   extension_point = g_io_extension_point_lookup (extension_point_name);
825   if (extension_point == NULL)
826     {
827       g_warning ("Tried to implement non-registered extension point %s", extension_point_name);
828       return NULL;
829     }
830   
831   if (extension_point->required_type != 0 &&
832       !g_type_is_a (type, extension_point->required_type))
833     {
834       g_warning ("Tried to register an extension of the type %s to extension point %s. "
835                  "Expected type is %s.",
836                  g_type_name (type),
837                  extension_point_name, 
838                  g_type_name (extension_point->required_type));
839       return NULL;
840     }      
841
842   /* It's safe to register the same type multiple times */
843   for (l = extension_point->extensions; l != NULL; l = l->next)
844     {
845       extension = l->data;
846       if (extension->type == type)
847         return extension;
848     }
849   
850   extension = g_slice_new0 (GIOExtension);
851   extension->type = type;
852   extension->name = g_strdup (extension_name);
853   extension->priority = priority;
854   
855   extension_point->extensions = g_list_insert_sorted (extension_point->extensions,
856                                                       extension, extension_prio_compare);
857   
858   return extension;
859 }
860
861 /**
862  * g_io_extension_ref_class:
863  * @extension: a #GIOExtension
864  *
865  * Gets a reference to the class for the type that is 
866  * associated with @extension.
867  *
868  * Returns: the #GTypeClass for the type of @extension
869  */
870 GTypeClass *
871 g_io_extension_ref_class (GIOExtension *extension)
872 {
873   return g_type_class_ref (extension->type);
874 }
875
876 /**
877  * g_io_extension_get_type:
878  * @extension: a #GIOExtension
879  *
880  * Gets the type associated with @extension.
881  *
882  * Returns: the type of @extension
883  */
884 GType
885 g_io_extension_get_type (GIOExtension *extension)
886 {
887   return extension->type;
888 }
889
890 /**
891  * g_io_extension_get_name:
892  * @extension: a #GIOExtension
893  *
894  * Gets the name under which @extension was registered.
895  *
896  * Note that the same type may be registered as extension
897  * for multiple extension points, under different names.
898  *
899  * Returns: the name of @extension.
900  */
901 const char *
902 g_io_extension_get_name (GIOExtension *extension)
903 {
904   return extension->name;
905 }
906
907 /**
908  * g_io_extension_get_priority:
909  * @extension: a #GIOExtension
910  *
911  * Gets the priority with which @extension was registered.
912  *
913  * Returns: the priority of @extension
914  */
915 gint
916 g_io_extension_get_priority (GIOExtension *extension)
917 {
918   return extension->priority;
919 }