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