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