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