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