win32: misc warning fixes
[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 "gproxyresolver.h"
33 #include "gproxy.h"
34 #include "gsettingsbackendinternal.h"
35 #include "gsocks4proxy.h"
36 #include "gsocks4aproxy.h"
37 #include "gsocks5proxy.h"
38 #include "gtlsbackend.h"
39 #include "gvfs.h"
40 #ifdef G_OS_WIN32
41 #include "gregistrysettingsbackend.h"
42 #endif
43 #include <glib/gstdio.h>
44
45 #ifdef G_OS_UNIX
46 #include "gdesktopappinfo.h"
47 #endif
48
49 /**
50  * SECTION:giomodule
51  * @short_description: Loadable GIO Modules
52  * @include: gio/gio.h
53  *
54  * Provides an interface and default functions for loading and unloading 
55  * modules. This is used internally to make GIO extensible, but can also
56  * be used by others to implement module loading.
57  * 
58  **/
59
60 /**
61  * SECTION:extensionpoints
62  * @short_description: Extension Points
63  * @include: gio.h
64  * @see_also: <link linkend="extending-gio">Extending GIO</link>
65  *
66  * #GIOExtensionPoint provides a mechanism for modules to extend the
67  * functionality of the library or application that loaded it in an 
68  * organized fashion.  
69  *
70  * An extension point is identified by a name, and it may optionally
71  * require that any implementation must by of a certain type (or derived
72  * thereof). Use g_io_extension_point_register() to register an
73  * extension point, and g_io_extension_point_set_required_type() to
74  * set a required type.
75  *
76  * A module can implement an extension point by specifying the #GType 
77  * that implements the functionality. Additionally, each implementation
78  * of an extension point has a name, and a priority. Use
79  * g_io_extension_point_implement() to implement an extension point.
80  * 
81  *  |[
82  *  GIOExtensionPoint *ep;
83  *
84  *  /&ast; Register an extension point &ast;/
85  *  ep = g_io_extension_point_register ("my-extension-point");
86  *  g_io_extension_point_set_required_type (ep, MY_TYPE_EXAMPLE);
87  *  ]|
88  *
89  *  |[
90  *  /&ast; Implement an extension point &ast;/
91  *  G_DEFINE_TYPE (MyExampleImpl, my_example_impl, MY_TYPE_EXAMPLE);
92  *  g_io_extension_point_implement ("my-extension-point",
93  *                                  my_example_impl_get_type (),
94  *                                  "my-example",
95  *                                  10);
96  *  ]|
97  *
98  *  It is up to the code that registered the extension point how
99  *  it uses the implementations that have been associated with it.
100  *  Depending on the use case, it may use all implementations, or
101  *  only the one with the highest priority, or pick a specific
102  *  one by name.
103  *
104  *  To avoid opening all modules just to find out what extension
105  *  points they implement, GIO makes use of a caching mechanism,
106  *  see <link linkend="gio-querymodules">gio-querymodules</link>.
107  *  You are expected to run this command after installing a
108  *  GIO module.
109  *
110  *  The <envar>GIO_EXTRA_MODULES</envar> environment variable can be
111  *  used to specify additional directories to automatically load modules
112  *  from. This environment variable has the same syntax as the
113  *  <envar>PATH</envar>. If two modules have the same base name in different
114  *  directories, then the latter one will be ignored. If additional
115  *  directories are specified GIO will load modules from the built-in
116  *  directory last.
117  */
118
119 /**
120  * GIOModuleScope:
121  *
122  * Represents a scope for loading IO modules. A scope can be used for blocking
123  * duplicate modules, or blocking a module you don't want to load.
124  *
125  * The scope can be used with g_io_modules_load_all_in_directory_with_scope()
126  * or g_io_modules_scan_all_in_directory_with_scope().
127  *
128  * Since: 2.30
129  */
130 struct _GIOModuleScope {
131   GIOModuleScopeFlags flags;
132   GHashTable *basenames;
133 };
134
135 /**
136  * g_io_module_scope_new:
137  * @flags: flags for the new scope
138  *
139  * Create a new scope for loading of IO modules. A scope can be used for
140  * blocking duplicate modules, or blocking a module you don't want to load.
141  *
142  * Specify the %G_IO_MODULE_SCOPE_BLOCK_DUPLICATES flag to block modules
143  * which have the same base name as a module that has already been seen
144  * in this scope.
145  *
146  * Returns: (transfer full): the new module scope
147  *
148  * Since: 2.30
149  */
150 GIOModuleScope *
151 g_io_module_scope_new (GIOModuleScopeFlags flags)
152 {
153   GIOModuleScope *scope = g_new0 (GIOModuleScope, 1);
154   scope->flags = flags;
155   scope->basenames = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
156   return scope;
157 }
158
159 /**
160  * g_io_module_scope_free:
161  * @scope: a module loading scope
162  *
163  * Free a module scope.
164  *
165  * Since: 2.30
166  */
167 void
168 g_io_module_scope_free (GIOModuleScope *scope)
169 {
170   if (!scope)
171     return;
172   g_hash_table_destroy (scope->basenames);
173   g_free (scope);
174 }
175
176 /**
177  * g_io_module_scope_block:
178  * @scope: a module loading scope
179  * @basename: the basename to block
180  *
181  * Block modules with the given @basename from being loaded when
182  * this scope is used with g_io_modules_scan_all_in_directory_with_scope()
183  * or g_io_modules_load_all_in_directory_with_scope().
184  *
185  * Since: 2.30
186  */
187 void
188 g_io_module_scope_block (GIOModuleScope *scope,
189                          const gchar    *basename)
190 {
191   gchar *key;
192
193   g_return_if_fail (scope != NULL);
194   g_return_if_fail (basename != NULL);
195
196   key = g_strdup (basename);
197   g_hash_table_insert (scope->basenames, key, key);
198 }
199
200 static gboolean
201 _g_io_module_scope_contains (GIOModuleScope *scope,
202                              const gchar    *basename)
203 {
204   return g_hash_table_lookup (scope->basenames, basename) ? TRUE : FALSE;
205 }
206
207 struct _GIOModule {
208   GTypeModule parent_instance;
209
210   gchar       *filename;
211   GModule     *library;
212   gboolean     initialized; /* The module was loaded at least once */
213
214   void (* load)   (GIOModule *module);
215   void (* unload) (GIOModule *module);
216 };
217
218 struct _GIOModuleClass
219 {
220   GTypeModuleClass parent_class;
221
222 };
223
224 static void      g_io_module_finalize      (GObject      *object);
225 static gboolean  g_io_module_load_module   (GTypeModule  *gmodule);
226 static void      g_io_module_unload_module (GTypeModule  *gmodule);
227
228 struct _GIOExtension {
229   char *name;
230   GType type;
231   gint priority;
232 };
233
234 struct _GIOExtensionPoint {
235   GType required_type;
236   char *name;
237   GList *extensions;
238   GList *lazy_load_modules;
239 };
240
241 static GHashTable *extension_points = NULL;
242 G_LOCK_DEFINE_STATIC(extension_points);
243
244 G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE);
245
246 static void
247 g_io_module_class_init (GIOModuleClass *class)
248 {
249   GObjectClass     *object_class      = G_OBJECT_CLASS (class);
250   GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
251
252   object_class->finalize     = g_io_module_finalize;
253
254   type_module_class->load    = g_io_module_load_module;
255   type_module_class->unload  = g_io_module_unload_module;
256 }
257
258 static void
259 g_io_module_init (GIOModule *module)
260 {
261 }
262
263 static void
264 g_io_module_finalize (GObject *object)
265 {
266   GIOModule *module = G_IO_MODULE (object);
267
268   g_free (module->filename);
269
270   G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
271 }
272
273 static gboolean
274 g_io_module_load_module (GTypeModule *gmodule)
275 {
276   GIOModule *module = G_IO_MODULE (gmodule);
277
278   if (!module->filename)
279     {
280       g_warning ("GIOModule path not set");
281       return FALSE;
282     }
283
284   module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
285
286   if (!module->library)
287     {
288       g_printerr ("%s\n", g_module_error ());
289       return FALSE;
290     }
291
292   /* Make sure that the loaded library contains the required methods */
293   if (! g_module_symbol (module->library,
294                          "g_io_module_load",
295                          (gpointer) &module->load) ||
296       ! g_module_symbol (module->library,
297                          "g_io_module_unload",
298                          (gpointer) &module->unload))
299     {
300       g_printerr ("%s\n", g_module_error ());
301       g_module_close (module->library);
302
303       return FALSE;
304     }
305
306   /* Initialize the loaded module */
307   module->load (module);
308   module->initialized = TRUE;
309
310   return TRUE;
311 }
312
313 static void
314 g_io_module_unload_module (GTypeModule *gmodule)
315 {
316   GIOModule *module = G_IO_MODULE (gmodule);
317
318   module->unload (module);
319
320   g_module_close (module->library);
321   module->library = NULL;
322
323   module->load   = NULL;
324   module->unload = NULL;
325 }
326
327 /**
328  * g_io_module_new:
329  * @filename: filename of the shared library module.
330  * 
331  * Creates a new GIOModule that will load the specific
332  * shared library when in use.
333  * 
334  * Returns: a #GIOModule from given @filename, 
335  * or %NULL on error.
336  **/
337 GIOModule *
338 g_io_module_new (const gchar *filename)
339 {
340   GIOModule *module;
341
342   g_return_val_if_fail (filename != NULL, NULL);
343
344   module = g_object_new (G_IO_TYPE_MODULE, NULL);
345   module->filename = g_strdup (filename);
346
347   return module;
348 }
349
350 static gboolean
351 is_valid_module_name (const gchar        *basename,
352                       GIOModuleScope     *scope)
353 {
354   gboolean result;
355
356 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
357   if (!g_str_has_prefix (basename, "lib") ||
358       !g_str_has_suffix (basename, ".so"))
359     return FALSE;
360 #else
361   if (!g_str_has_suffix (basename, ".dll"))
362     return FALSE;
363 #endif
364
365   result = TRUE;
366   if (scope)
367     {
368       result = _g_io_module_scope_contains (scope, basename) ? FALSE : TRUE;
369       if (result && (scope->flags & G_IO_MODULE_SCOPE_BLOCK_DUPLICATES))
370         g_io_module_scope_block (scope, basename);
371     }
372
373   return result;
374 }
375
376
377 /**
378  * g_io_modules_scan_all_in_directory_with_scope:
379  * @dirname: pathname for a directory containing modules to scan.
380  * @scope: a scope to use when scanning the modules
381  *
382  * Scans all the modules in the specified directory, ensuring that
383  * any extension point implemented by a module is registered.
384  *
385  * This may not actually load and initialize all the types in each
386  * module, some modules may be lazily loaded and initialized when
387  * an extension point it implementes is used with e.g.
388  * g_io_extension_point_get_extensions() or
389  * g_io_extension_point_get_extension_by_name().
390  *
391  * If you need to guarantee that all types are loaded in all the modules,
392  * use g_io_modules_load_all_in_directory().
393  *
394  * Since: 2.30
395  **/
396 void
397 g_io_modules_scan_all_in_directory_with_scope (const char     *dirname,
398                                                GIOModuleScope *scope)
399 {
400   const gchar *name;
401   char *filename;
402   GDir *dir;
403   GStatBuf statbuf;
404   char *data;
405   time_t cache_mtime;
406   GHashTable *cache;
407
408   if (!g_module_supported ())
409     return;
410
411   dir = g_dir_open (dirname, 0, NULL);
412   if (!dir)
413     return;
414
415   filename = g_build_filename (dirname, "giomodule.cache", NULL);
416
417   cache = g_hash_table_new_full (g_str_hash, g_str_equal,
418                                  g_free, (GDestroyNotify)g_strfreev);
419
420   cache_mtime = 0;
421   if (g_stat (filename, &statbuf) == 0 &&
422       g_file_get_contents (filename, &data, NULL, NULL))
423     {
424       char **lines;
425       int i;
426
427       /* Cache mtime is the time the cache file was created, any file
428        * that has a ctime before this was created then and not modified
429        * since then (userspace can't change ctime). Its possible to change
430        * the ctime forward without changing the file content, by e.g.
431        * chmoding the file, but this is uncommon and will only cause us
432        * to not use the cache so will not cause bugs.
433        */
434       cache_mtime = statbuf.st_mtime;
435
436       lines = g_strsplit (data, "\n", -1);
437       g_free (data);
438
439       for (i = 0;  lines[i] != NULL; i++)
440         {
441           char *line = lines[i];
442           char *file;
443           char *colon;
444           char **extension_points;
445
446           if (line[0] == '#')
447             continue;
448
449           colon = strchr (line, ':');
450           if (colon == NULL || line == colon)
451             continue; /* Invalid line, ignore */
452
453           *colon = 0; /* terminate filename */
454           file = g_strdup (line);
455           colon++; /* after colon */
456
457           while (g_ascii_isspace (*colon))
458             colon++;
459
460           extension_points = g_strsplit (colon, ",", -1);
461           g_hash_table_insert (cache, file, extension_points);
462         }
463       g_strfreev (lines);
464     }
465
466   while ((name = g_dir_read_name (dir)))
467     {
468       if (is_valid_module_name (name, scope))
469         {
470           GIOExtensionPoint *extension_point;
471           GIOModule *module;
472           gchar *path;
473           char **extension_points;
474           int i;
475
476           path = g_build_filename (dirname, name, NULL);
477           module = g_io_module_new (path);
478
479           extension_points = g_hash_table_lookup (cache, name);
480           if (extension_points != NULL &&
481               g_stat (path, &statbuf) == 0 &&
482               statbuf.st_ctime <= cache_mtime)
483             {
484               /* Lazy load/init the library when first required */
485               for (i = 0; extension_points[i] != NULL; i++)
486                 {
487                   extension_point =
488                     g_io_extension_point_register (extension_points[i]);
489                   extension_point->lazy_load_modules =
490                     g_list_prepend (extension_point->lazy_load_modules,
491                                     module);
492                 }
493             }
494           else
495             {
496               /* Try to load and init types */
497               if (g_type_module_use (G_TYPE_MODULE (module)))
498                 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
499               else
500                 { /* Failure to load */
501                   g_printerr ("Failed to load module: %s\n", path);
502                   g_object_unref (module);
503                   g_free (path);
504                   continue;
505                 }
506             }
507
508           g_free (path);
509         }
510     }
511
512   g_dir_close (dir);
513
514   g_hash_table_destroy (cache);
515
516   g_free (filename);
517 }
518
519 /**
520  * g_io_modules_scan_all_in_directory:
521  * @dirname: pathname for a directory containing modules to scan.
522  *
523  * Scans all the modules in the specified directory, ensuring that
524  * any extension point implemented by a module is registered.
525  *
526  * This may not actually load and initialize all the types in each
527  * module, some modules may be lazily loaded and initialized when
528  * an extension point it implementes is used with e.g.
529  * g_io_extension_point_get_extensions() or
530  * g_io_extension_point_get_extension_by_name().
531  *
532  * If you need to guarantee that all types are loaded in all the modules,
533  * use g_io_modules_load_all_in_directory().
534  *
535  * Since: 2.24
536  **/
537 void
538 g_io_modules_scan_all_in_directory (const char *dirname)
539 {
540   g_io_modules_scan_all_in_directory_with_scope (dirname, NULL);
541 }
542
543 /**
544  * g_io_modules_load_all_in_directory_with_scope:
545  * @dirname: pathname for a directory containing modules to load.
546  * @scope: a scope to use when scanning the modules.
547  *
548  * Loads all the modules in the specified directory.
549  *
550  * If don't require all modules to be initialized (and thus registering
551  * all gtypes) then you can use g_io_modules_scan_all_in_directory()
552  * which allows delayed/lazy loading of modules.
553  *
554  * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
555  *      from the directory,
556  *      All the modules are loaded into memory, if you want to
557  *      unload them (enabling on-demand loading) you must call
558  *      g_type_module_unuse() on all the modules. Free the list
559  *      with g_list_free().
560  *
561  * Since: 2.30
562  **/
563 GList *
564 g_io_modules_load_all_in_directory_with_scope (const char     *dirname,
565                                                GIOModuleScope *scope)
566 {
567   const gchar *name;
568   GDir        *dir;
569   GList *modules;
570
571   if (!g_module_supported ())
572     return NULL;
573
574   dir = g_dir_open (dirname, 0, NULL);
575   if (!dir)
576     return NULL;
577
578   modules = NULL;
579   while ((name = g_dir_read_name (dir)))
580     {
581       if (is_valid_module_name (name, scope))
582         {
583           GIOModule *module;
584           gchar     *path;
585
586           path = g_build_filename (dirname, name, NULL);
587           module = g_io_module_new (path);
588
589           if (!g_type_module_use (G_TYPE_MODULE (module)))
590             {
591               g_printerr ("Failed to load module: %s\n", path);
592               g_object_unref (module);
593               g_free (path);
594               continue;
595             }
596           
597           g_free (path);
598
599           modules = g_list_prepend (modules, module);
600         }
601     }
602   
603   g_dir_close (dir);
604
605   return modules;
606 }
607
608 /**
609  * g_io_modules_load_all_in_directory:
610  * @dirname: pathname for a directory containing modules to load.
611  *
612  * Loads all the modules in the specified directory.
613  *
614  * If don't require all modules to be initialized (and thus registering
615  * all gtypes) then you can use g_io_modules_scan_all_in_directory()
616  * which allows delayed/lazy loading of modules.
617  *
618  * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
619  *      from the directory,
620  *      All the modules are loaded into memory, if you want to
621  *      unload them (enabling on-demand loading) you must call
622  *      g_type_module_unuse() on all the modules. Free the list
623  *      with g_list_free().
624  **/
625 GList *
626 g_io_modules_load_all_in_directory (const char *dirname)
627 {
628   return g_io_modules_load_all_in_directory_with_scope (dirname, NULL);
629 }
630
631 GRecMutex default_modules_lock;
632 GHashTable *default_modules;
633
634 static gpointer
635 try_implementation (GIOExtension         *extension,
636                     GIOModuleVerifyFunc   verify_func)
637 {
638   GType type = g_io_extension_get_type (extension);
639   gpointer impl;
640
641   if (g_type_is_a (type, G_TYPE_INITABLE))
642     return g_initable_new (type, NULL, NULL, NULL);
643   else
644     {
645       impl = g_object_new (type, NULL);
646       if (!verify_func || verify_func (impl))
647         return impl;
648
649       g_object_unref (impl);
650       return NULL;
651     }
652 }
653
654 /**
655  * _g_io_module_get_default:
656  * @extension_point: the name of an extension point
657  * @envvar: (allow-none): the name of an environment variable to
658  *     override the default implementation.
659  * @verify_func: (allow-none): a function to call to verify that
660  *     a given implementation is usable in the current environment.
661  *
662  * Retrieves the default object implementing @extension_point.
663  *
664  * If @envvar is not %NULL, and the environment variable with that
665  * name is set, then the implementation it specifies will be tried
666  * first. After that, or if @envvar is not set, all other
667  * implementations will be tried in order of decreasing priority.
668  *
669  * If an extension point implementation implements #GInitable, then
670  * that implementation will only be used if it initializes
671  * successfully. Otherwise, if @verify_func is not %NULL, then it will
672  * be called on each candidate implementation after construction, to
673  * check if it is actually usable or not.
674  *
675  * The result is cached after it is generated the first time, and
676  * the function is thread-safe.
677  *
678  * Return value: (transfer none): an object implementing
679  *     @extension_point, or %NULL if there are no usable
680  *     implementations.
681  */
682 gpointer
683 _g_io_module_get_default (const gchar         *extension_point,
684                           const gchar         *envvar,
685                           GIOModuleVerifyFunc  verify_func)
686 {
687   const char *use_this;
688   GList *l;
689   GIOExtensionPoint *ep;
690   GIOExtension *extension, *preferred;
691   gpointer impl;
692
693   g_rec_mutex_lock (&default_modules_lock);
694   if (default_modules)
695     {
696       gpointer key;
697
698       if (g_hash_table_lookup_extended (default_modules, extension_point,
699                                         &key, &impl))
700         {
701           g_rec_mutex_unlock (&default_modules_lock);
702           return impl;
703         }
704     }
705   else
706     {
707       default_modules = g_hash_table_new (g_str_hash, g_str_equal);
708     }
709
710   _g_io_modules_ensure_loaded ();
711   ep = g_io_extension_point_lookup (extension_point);
712
713   if (!ep)
714     {
715       g_warn_if_reached ();
716       g_rec_mutex_unlock (&default_modules_lock);
717       return NULL;
718     }
719
720   use_this = envvar ? g_getenv (envvar) : NULL;
721   if (use_this)
722     {
723       preferred = g_io_extension_point_get_extension_by_name (ep, use_this);
724       if (preferred)
725         {
726           impl = try_implementation (preferred, verify_func);
727           if (impl)
728             goto done;
729         }
730       else
731         g_warning ("Can't find module '%s' specified in %s", use_this, envvar);
732     }
733   else
734     preferred = NULL;
735
736   for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
737     {
738       extension = l->data;
739       if (extension == preferred)
740         continue;
741
742       impl = try_implementation (extension, verify_func);
743       if (impl)
744         goto done;
745     }
746
747   impl = NULL;
748
749  done:
750   g_hash_table_insert (default_modules,
751                        g_strdup (extension_point),
752                        impl ? g_object_ref (impl) : NULL);
753   g_rec_mutex_unlock (&default_modules_lock);
754
755   return impl;
756 }
757
758 G_LOCK_DEFINE_STATIC (registered_extensions);
759 G_LOCK_DEFINE_STATIC (loaded_dirs);
760
761 extern GType _g_fen_directory_monitor_get_type (void);
762 extern GType _g_fen_file_monitor_get_type (void);
763 extern GType _g_inotify_directory_monitor_get_type (void);
764 extern GType _g_inotify_file_monitor_get_type (void);
765 extern GType _g_kqueue_directory_monitor_get_type (void);
766 extern GType _g_kqueue_file_monitor_get_type (void);
767 extern GType _g_unix_volume_monitor_get_type (void);
768 extern GType _g_local_vfs_get_type (void);
769
770 extern GType _g_win32_volume_monitor_get_type (void);
771 extern GType g_win32_directory_monitor_get_type (void);
772 extern GType _g_winhttp_vfs_get_type (void);
773
774 extern GType _g_dummy_proxy_resolver_get_type (void);
775 extern GType _g_dummy_tls_backend_get_type (void);
776 extern GType g_network_monitor_base_get_type (void);
777 #ifdef HAVE_NETLINK
778 extern GType _g_network_monitor_netlink_get_type (void);
779 #endif
780
781 #ifdef G_PLATFORM_WIN32
782
783 #include <windows.h>
784
785 static HMODULE gio_dll = NULL;
786
787 #ifdef DLL_EXPORT
788
789 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
790                      DWORD     fdwReason,
791                      LPVOID    lpvReserved);
792
793 BOOL WINAPI
794 DllMain (HINSTANCE hinstDLL,
795          DWORD     fdwReason,
796          LPVOID    lpvReserved)
797 {
798   if (fdwReason == DLL_PROCESS_ATTACH)
799       gio_dll = hinstDLL;
800
801   return TRUE;
802 }
803
804 void *
805 _g_io_win32_get_module (void)
806 {
807   return gio_dll;
808 }
809
810 #endif
811
812 #undef GIO_MODULE_DIR
813
814 /* GIO_MODULE_DIR is used only in code called just once,
815  * so no problem leaking this
816  */
817 #define GIO_MODULE_DIR \
818   g_build_filename (g_win32_get_package_installation_directory_of_module (gio_dll), \
819                     "lib/gio/modules", \
820                     NULL)
821
822 #endif
823
824 void
825 _g_io_modules_ensure_extension_points_registered (void)
826 {
827   static gboolean registered_extensions = FALSE;
828   GIOExtensionPoint *ep;
829
830   G_LOCK (registered_extensions);
831   
832   if (!registered_extensions)
833     {
834       registered_extensions = TRUE;
835       
836 #ifdef G_OS_UNIX
837 #if !GLIB_CHECK_VERSION (3, 0, 0)
838       ep = g_io_extension_point_register (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
839       G_GNUC_BEGIN_IGNORE_DEPRECATIONS
840       g_io_extension_point_set_required_type (ep, G_TYPE_DESKTOP_APP_INFO_LOOKUP);
841       G_GNUC_END_IGNORE_DEPRECATIONS
842 #endif
843 #endif
844       
845       ep = g_io_extension_point_register (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME);
846       g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_DIRECTORY_MONITOR);
847       
848       ep = g_io_extension_point_register (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
849       g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
850       
851       ep = g_io_extension_point_register (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
852       g_io_extension_point_set_required_type (ep, G_TYPE_VOLUME_MONITOR);
853       
854       ep = g_io_extension_point_register (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
855       g_io_extension_point_set_required_type (ep, G_TYPE_NATIVE_VOLUME_MONITOR);
856       
857       ep = g_io_extension_point_register (G_VFS_EXTENSION_POINT_NAME);
858       g_io_extension_point_set_required_type (ep, G_TYPE_VFS);
859
860       ep = g_io_extension_point_register ("gsettings-backend");
861       g_io_extension_point_set_required_type (ep, G_TYPE_OBJECT);
862
863       ep = g_io_extension_point_register (G_PROXY_RESOLVER_EXTENSION_POINT_NAME);
864       g_io_extension_point_set_required_type (ep, G_TYPE_PROXY_RESOLVER);
865
866       ep = g_io_extension_point_register (G_PROXY_EXTENSION_POINT_NAME);
867       g_io_extension_point_set_required_type (ep, G_TYPE_PROXY);
868
869       ep = g_io_extension_point_register (G_TLS_BACKEND_EXTENSION_POINT_NAME);
870       g_io_extension_point_set_required_type (ep, G_TYPE_TLS_BACKEND);
871
872       ep = g_io_extension_point_register (G_NETWORK_MONITOR_EXTENSION_POINT_NAME);
873       g_io_extension_point_set_required_type (ep, G_TYPE_NETWORK_MONITOR);
874     }
875   
876   G_UNLOCK (registered_extensions);
877 }
878
879 void
880 _g_io_modules_ensure_loaded (void)
881 {
882   static gboolean loaded_dirs = FALSE;
883   const char *module_path;
884   GIOModuleScope *scope;
885
886   _g_io_modules_ensure_extension_points_registered ();
887   
888   G_LOCK (loaded_dirs);
889
890   if (!loaded_dirs)
891     {
892       loaded_dirs = TRUE;
893       scope = g_io_module_scope_new (G_IO_MODULE_SCOPE_BLOCK_DUPLICATES);
894
895       /* First load any overrides, extras */
896       module_path = g_getenv ("GIO_EXTRA_MODULES");
897       if (module_path)
898         {
899           gchar **paths;
900           int i;
901
902           paths = g_strsplit (module_path, G_SEARCHPATH_SEPARATOR_S, 0);
903
904           for (i = 0; paths[i] != NULL; i++)
905             {
906               g_io_modules_scan_all_in_directory_with_scope (paths[i], scope);
907             }
908
909           g_strfreev (paths);
910         }
911
912       /* Then load the compiled in path */
913       g_io_modules_scan_all_in_directory_with_scope (GIO_MODULE_DIR, scope);
914
915       g_io_module_scope_free (scope);
916
917       /* Initialize types from built-in "modules" */
918       g_type_ensure (g_null_settings_backend_get_type ());
919       g_type_ensure (g_memory_settings_backend_get_type ());
920 #if defined(HAVE_SYS_INOTIFY_H) || defined(HAVE_LINUX_INOTIFY_H)
921       g_type_ensure (_g_inotify_directory_monitor_get_type ());
922       g_type_ensure (_g_inotify_file_monitor_get_type ());
923 #endif
924 #if defined(HAVE_KQUEUE)
925       g_type_ensure (_g_kqueue_directory_monitor_get_type ());
926       g_type_ensure (_g_kqueue_file_monitor_get_type ());
927 #endif
928 #if defined(HAVE_FEN)
929       g_type_ensure (_g_fen_directory_monitor_get_type ());
930       g_type_ensure (_g_fen_file_monitor_get_type ());
931 #endif
932 #ifdef G_OS_WIN32
933       g_type_ensure (_g_win32_volume_monitor_get_type ());
934       g_type_ensure (g_win32_directory_monitor_get_type ());
935       g_type_ensure (g_registry_backend_get_type ());
936 #endif
937 #ifdef HAVE_CARBON
938       g_nextstep_settings_backend_get_type ();
939 #endif
940 #ifdef G_OS_UNIX
941       g_type_ensure (_g_unix_volume_monitor_get_type ());
942 #endif
943 #ifdef G_OS_WIN32
944       g_type_ensure (_g_winhttp_vfs_get_type ());
945 #endif
946       g_type_ensure (_g_local_vfs_get_type ());
947       g_type_ensure (_g_dummy_proxy_resolver_get_type ());
948       g_type_ensure (_g_socks4a_proxy_get_type ());
949       g_type_ensure (_g_socks4_proxy_get_type ());
950       g_type_ensure (_g_socks5_proxy_get_type ());
951       g_type_ensure (_g_dummy_tls_backend_get_type ());
952       g_type_ensure (g_network_monitor_base_get_type ());
953 #ifdef HAVE_NETLINK
954       g_type_ensure (_g_network_monitor_netlink_get_type ());
955 #endif
956     }
957
958   G_UNLOCK (loaded_dirs);
959 }
960
961 static void
962 g_io_extension_point_free (GIOExtensionPoint *ep)
963 {
964   g_free (ep->name);
965   g_free (ep);
966 }
967
968 /**
969  * g_io_extension_point_register:
970  * @name: The name of the extension point
971  *
972  * Registers an extension point.
973  *
974  * Returns: (transfer none): the new #GIOExtensionPoint. This object is
975  *    owned by GIO and should not be freed.
976  */
977 GIOExtensionPoint *
978 g_io_extension_point_register (const char *name)
979 {
980   GIOExtensionPoint *ep;
981   
982   G_LOCK (extension_points);
983   if (extension_points == NULL)
984     extension_points = g_hash_table_new_full (g_str_hash,
985                                               g_str_equal,
986                                               NULL,
987                                               (GDestroyNotify)g_io_extension_point_free);
988
989   ep = g_hash_table_lookup (extension_points, name);
990   if (ep != NULL)
991     {
992       G_UNLOCK (extension_points);
993       return ep;
994     }
995
996   ep = g_new0 (GIOExtensionPoint, 1);
997   ep->name = g_strdup (name);
998   
999   g_hash_table_insert (extension_points, ep->name, ep);
1000   
1001   G_UNLOCK (extension_points);
1002
1003   return ep;
1004 }
1005
1006 /**
1007  * g_io_extension_point_lookup:
1008  * @name: the name of the extension point
1009  *
1010  * Looks up an existing extension point.
1011  *
1012  * Returns: (transfer none): the #GIOExtensionPoint, or %NULL if there
1013  *    is no registered extension point with the given name.
1014  */
1015 GIOExtensionPoint *
1016 g_io_extension_point_lookup (const char *name)
1017 {
1018   GIOExtensionPoint *ep;
1019
1020   G_LOCK (extension_points);
1021   ep = NULL;
1022   if (extension_points != NULL)
1023     ep = g_hash_table_lookup (extension_points, name);
1024   
1025   G_UNLOCK (extension_points);
1026
1027   return ep;
1028   
1029 }
1030
1031 /**
1032  * g_io_extension_point_set_required_type:
1033  * @extension_point: a #GIOExtensionPoint
1034  * @type: the #GType to require
1035  *
1036  * Sets the required type for @extension_point to @type. 
1037  * All implementations must henceforth have this type.
1038  */
1039 void
1040 g_io_extension_point_set_required_type (GIOExtensionPoint *extension_point,
1041                                         GType              type)
1042 {
1043   extension_point->required_type = type;
1044 }
1045
1046 /**
1047  * g_io_extension_point_get_required_type:
1048  * @extension_point: a #GIOExtensionPoint
1049  *
1050  * Gets the required type for @extension_point.
1051  *
1052  * Returns: the #GType that all implementations must have, 
1053  *     or #G_TYPE_INVALID if the extension point has no required type
1054  */
1055 GType
1056 g_io_extension_point_get_required_type (GIOExtensionPoint *extension_point)
1057 {
1058   return extension_point->required_type;
1059 }
1060
1061 static void
1062 lazy_load_modules (GIOExtensionPoint *extension_point)
1063 {
1064   GIOModule *module;
1065   GList *l;
1066
1067   for (l = extension_point->lazy_load_modules; l != NULL; l = l->next)
1068     {
1069       module = l->data;
1070
1071       if (!module->initialized)
1072         {
1073           if (g_type_module_use (G_TYPE_MODULE (module)))
1074             g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
1075           else
1076             g_printerr ("Failed to load module: %s\n",
1077                         module->filename);
1078         }
1079     }
1080 }
1081
1082 /**
1083  * g_io_extension_point_get_extensions:
1084  * @extension_point: a #GIOExtensionPoint
1085  *
1086  * Gets a list of all extensions that implement this extension point.
1087  * The list is sorted by priority, beginning with the highest priority.
1088  *
1089  * Returns: (element-type GIOExtension) (transfer none): a #GList of
1090  * #GIOExtension<!-- -->s. The list is owned by GIO and should not be
1091  * modified.
1092  */
1093 GList *
1094 g_io_extension_point_get_extensions (GIOExtensionPoint *extension_point)
1095 {
1096   lazy_load_modules (extension_point);
1097   return extension_point->extensions;
1098 }
1099
1100 /**
1101  * g_io_extension_point_get_extension_by_name:
1102  * @extension_point: a #GIOExtensionPoint
1103  * @name: the name of the extension to get
1104  *
1105  * Finds a #GIOExtension for an extension point by name.
1106  *
1107  * Returns: (transfer none): the #GIOExtension for @extension_point that has the
1108  *    given name, or %NULL if there is no extension with that name
1109  */
1110 GIOExtension *
1111 g_io_extension_point_get_extension_by_name (GIOExtensionPoint *extension_point,
1112                                             const char        *name)
1113 {
1114   GList *l;
1115
1116   lazy_load_modules (extension_point);
1117   for (l = extension_point->extensions; l != NULL; l = l->next)
1118     {
1119       GIOExtension *e = l->data;
1120
1121       if (e->name != NULL &&
1122           strcmp (e->name, name) == 0)
1123         return e;
1124     }
1125   
1126   return NULL;
1127 }
1128
1129 static gint
1130 extension_prio_compare (gconstpointer  a,
1131                         gconstpointer  b)
1132 {
1133   const GIOExtension *extension_a = a, *extension_b = b;
1134
1135   if (extension_a->priority > extension_b->priority)
1136     return -1;
1137
1138   if (extension_b->priority > extension_a->priority)
1139     return 1;
1140
1141   return 0;
1142 }
1143
1144 /**
1145  * g_io_extension_point_implement:
1146  * @extension_point_name: the name of the extension point
1147  * @type: the #GType to register as extension 
1148  * @extension_name: the name for the extension
1149  * @priority: the priority for the extension
1150  *
1151  * Registers @type as extension for the extension point with name
1152  * @extension_point_name. 
1153  *
1154  * If @type has already been registered as an extension for this 
1155  * extension point, the existing #GIOExtension object is returned.
1156  *
1157  * Returns: (transfer none): a #GIOExtension object for #GType
1158  */
1159 GIOExtension *
1160 g_io_extension_point_implement (const char *extension_point_name,
1161                                 GType       type,
1162                                 const char *extension_name,
1163                                 gint        priority)
1164 {
1165   GIOExtensionPoint *extension_point;
1166   GIOExtension *extension;
1167   GList *l;
1168
1169   g_return_val_if_fail (extension_point_name != NULL, NULL);
1170
1171   extension_point = g_io_extension_point_lookup (extension_point_name);
1172   if (extension_point == NULL)
1173     {
1174       g_warning ("Tried to implement non-registered extension point %s", extension_point_name);
1175       return NULL;
1176     }
1177   
1178   if (extension_point->required_type != 0 &&
1179       !g_type_is_a (type, extension_point->required_type))
1180     {
1181       g_warning ("Tried to register an extension of the type %s to extension point %s. "
1182                  "Expected type is %s.",
1183                  g_type_name (type),
1184                  extension_point_name, 
1185                  g_type_name (extension_point->required_type));
1186       return NULL;
1187     }      
1188
1189   /* It's safe to register the same type multiple times */
1190   for (l = extension_point->extensions; l != NULL; l = l->next)
1191     {
1192       extension = l->data;
1193       if (extension->type == type)
1194         return extension;
1195     }
1196   
1197   extension = g_slice_new0 (GIOExtension);
1198   extension->type = type;
1199   extension->name = g_strdup (extension_name);
1200   extension->priority = priority;
1201   
1202   extension_point->extensions = g_list_insert_sorted (extension_point->extensions,
1203                                                       extension, extension_prio_compare);
1204   
1205   return extension;
1206 }
1207
1208 /**
1209  * g_io_extension_ref_class:
1210  * @extension: a #GIOExtension
1211  *
1212  * Gets a reference to the class for the type that is 
1213  * associated with @extension.
1214  *
1215  * Returns: (transfer full): the #GTypeClass for the type of @extension
1216  */
1217 GTypeClass *
1218 g_io_extension_ref_class (GIOExtension *extension)
1219 {
1220   return g_type_class_ref (extension->type);
1221 }
1222
1223 /**
1224  * g_io_extension_get_type:
1225  * @extension: a #GIOExtension
1226  *
1227  * Gets the type associated with @extension.
1228  *
1229  * Returns: the type of @extension
1230  */
1231 GType
1232 g_io_extension_get_type (GIOExtension *extension)
1233 {
1234   return extension->type;
1235 }
1236
1237 /**
1238  * g_io_extension_get_name:
1239  * @extension: a #GIOExtension
1240  *
1241  * Gets the name under which @extension was registered.
1242  *
1243  * Note that the same type may be registered as extension
1244  * for multiple extension points, under different names.
1245  *
1246  * Returns: the name of @extension.
1247  */
1248 const char *
1249 g_io_extension_get_name (GIOExtension *extension)
1250 {
1251   return extension->name;
1252 }
1253
1254 /**
1255  * g_io_extension_get_priority:
1256  * @extension: a #GIOExtension
1257  *
1258  * Gets the priority with which @extension was registered.
1259  *
1260  * Returns: the priority of @extension
1261  */
1262 gint
1263 g_io_extension_get_priority (GIOExtension *extension)
1264 {
1265   return extension->priority;
1266 }