Do not expand autoptr macros when running introspection
[glib.git] / gio / giomodule.c
blob40476957c9d1e9ec8523e4d68f237f4a89968e3a
1 /* GIO - GLib Input, Output and Streaming Library
2 *
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.1 of the License, or (at your option) any later version.
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.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
21 #include "config.h"
23 #include <string.h>
25 #include "giomodule.h"
26 #include "giomodule-priv.h"
27 #include "glocalfilemonitor.h"
28 #include "gnativevolumemonitor.h"
29 #include "gproxyresolver.h"
30 #include "gproxy.h"
31 #include "gsettingsbackendinternal.h"
32 #include "ghttpproxy.h"
33 #include "gsocks4proxy.h"
34 #include "gsocks4aproxy.h"
35 #include "gsocks5proxy.h"
36 #include "gtlsbackend.h"
37 #include "gvfs.h"
38 #include "gnotificationbackend.h"
39 #include "ginitable.h"
40 #include "gnetworkmonitor.h"
41 #ifdef G_OS_WIN32
42 #include "gregistrysettingsbackend.h"
43 #endif
44 #include <glib/gstdio.h>
46 #if defined(G_OS_UNIX) && !defined(HAVE_COCOA)
47 #include "gdesktopappinfo.h"
48 #endif
49 #ifdef HAVE_COCOA
50 #include "gosxappinfo.h"
51 #endif
53 #ifdef HAVE_COCOA
54 #include <AvailabilityMacros.h>
55 #endif
57 /**
58 * SECTION:giomodule
59 * @short_description: Loadable GIO Modules
60 * @include: gio/gio.h
62 * Provides an interface and default functions for loading and unloading
63 * modules. This is used internally to make GIO extensible, but can also
64 * be used by others to implement module loading.
66 **/
68 /**
69 * SECTION:extensionpoints
70 * @short_description: Extension Points
71 * @include: gio.h
72 * @see_also: [Extending GIO][extending-gio]
74 * #GIOExtensionPoint provides a mechanism for modules to extend the
75 * functionality of the library or application that loaded it in an
76 * organized fashion.
78 * An extension point is identified by a name, and it may optionally
79 * require that any implementation must be of a certain type (or derived
80 * thereof). Use g_io_extension_point_register() to register an
81 * extension point, and g_io_extension_point_set_required_type() to
82 * set a required type.
84 * A module can implement an extension point by specifying the #GType
85 * that implements the functionality. Additionally, each implementation
86 * of an extension point has a name, and a priority. Use
87 * g_io_extension_point_implement() to implement an extension point.
89 * |[<!-- language="C" -->
90 * GIOExtensionPoint *ep;
92 * // Register an extension point
93 * ep = g_io_extension_point_register ("my-extension-point");
94 * g_io_extension_point_set_required_type (ep, MY_TYPE_EXAMPLE);
95 * ]|
97 * |[<!-- language="C" -->
98 * // Implement an extension point
99 * G_DEFINE_TYPE (MyExampleImpl, my_example_impl, MY_TYPE_EXAMPLE)
100 * g_io_extension_point_implement ("my-extension-point",
101 * my_example_impl_get_type (),
102 * "my-example",
103 * 10);
104 * ]|
106 * It is up to the code that registered the extension point how
107 * it uses the implementations that have been associated with it.
108 * Depending on the use case, it may use all implementations, or
109 * only the one with the highest priority, or pick a specific
110 * one by name.
112 * To avoid opening all modules just to find out what extension
113 * points they implement, GIO makes use of a caching mechanism,
114 * see [gio-querymodules][gio-querymodules].
115 * You are expected to run this command after installing a
116 * GIO module.
118 * The `GIO_EXTRA_MODULES` environment variable can be used to
119 * specify additional directories to automatically load modules
120 * from. This environment variable has the same syntax as the
121 * `PATH`. If two modules have the same base name in different
122 * directories, then the latter one will be ignored. If additional
123 * directories are specified GIO will load modules from the built-in
124 * directory last.
128 * GIOModuleScope:
130 * Represents a scope for loading IO modules. A scope can be used for blocking
131 * duplicate modules, or blocking a module you don't want to load.
133 * The scope can be used with g_io_modules_load_all_in_directory_with_scope()
134 * or g_io_modules_scan_all_in_directory_with_scope().
136 * Since: 2.30
138 struct _GIOModuleScope {
139 GIOModuleScopeFlags flags;
140 GHashTable *basenames;
144 * g_io_module_scope_new:
145 * @flags: flags for the new scope
147 * Create a new scope for loading of IO modules. A scope can be used for
148 * blocking duplicate modules, or blocking a module you don't want to load.
150 * Specify the %G_IO_MODULE_SCOPE_BLOCK_DUPLICATES flag to block modules
151 * which have the same base name as a module that has already been seen
152 * in this scope.
154 * Returns: (transfer full): the new module scope
156 * Since: 2.30
158 GIOModuleScope *
159 g_io_module_scope_new (GIOModuleScopeFlags flags)
161 GIOModuleScope *scope = g_new0 (GIOModuleScope, 1);
162 scope->flags = flags;
163 scope->basenames = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
164 return scope;
168 * g_io_module_scope_free:
169 * @scope: a module loading scope
171 * Free a module scope.
173 * Since: 2.30
175 void
176 g_io_module_scope_free (GIOModuleScope *scope)
178 if (!scope)
179 return;
180 g_hash_table_destroy (scope->basenames);
181 g_free (scope);
185 * g_io_module_scope_block:
186 * @scope: a module loading scope
187 * @basename: the basename to block
189 * Block modules with the given @basename from being loaded when
190 * this scope is used with g_io_modules_scan_all_in_directory_with_scope()
191 * or g_io_modules_load_all_in_directory_with_scope().
193 * Since: 2.30
195 void
196 g_io_module_scope_block (GIOModuleScope *scope,
197 const gchar *basename)
199 gchar *key;
201 g_return_if_fail (scope != NULL);
202 g_return_if_fail (basename != NULL);
204 key = g_strdup (basename);
205 g_hash_table_add (scope->basenames, key);
208 static gboolean
209 _g_io_module_scope_contains (GIOModuleScope *scope,
210 const gchar *basename)
212 return g_hash_table_contains (scope->basenames, basename);
215 struct _GIOModule {
216 GTypeModule parent_instance;
218 gchar *filename;
219 GModule *library;
220 gboolean initialized; /* The module was loaded at least once */
222 void (* load) (GIOModule *module);
223 void (* unload) (GIOModule *module);
226 struct _GIOModuleClass
228 GTypeModuleClass parent_class;
232 static void g_io_module_finalize (GObject *object);
233 static gboolean g_io_module_load_module (GTypeModule *gmodule);
234 static void g_io_module_unload_module (GTypeModule *gmodule);
237 * GIOExtension:
239 * #GIOExtension is an opaque data structure and can only be accessed
240 * using the following functions.
242 struct _GIOExtension {
243 char *name;
244 GType type;
245 gint priority;
249 * GIOExtensionPoint:
251 * #GIOExtensionPoint is an opaque data structure and can only be accessed
252 * using the following functions.
254 struct _GIOExtensionPoint {
255 GType required_type;
256 char *name;
257 GList *extensions;
258 GList *lazy_load_modules;
261 static GHashTable *extension_points = NULL;
262 G_LOCK_DEFINE_STATIC(extension_points);
264 G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE)
266 static void
267 g_io_module_class_init (GIOModuleClass *class)
269 GObjectClass *object_class = G_OBJECT_CLASS (class);
270 GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
272 object_class->finalize = g_io_module_finalize;
274 type_module_class->load = g_io_module_load_module;
275 type_module_class->unload = g_io_module_unload_module;
278 static void
279 g_io_module_init (GIOModule *module)
283 static void
284 g_io_module_finalize (GObject *object)
286 GIOModule *module = G_IO_MODULE (object);
288 g_free (module->filename);
290 G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
293 static gboolean
294 g_io_module_load_module (GTypeModule *gmodule)
296 GIOModule *module = G_IO_MODULE (gmodule);
298 if (!module->filename)
300 g_warning ("GIOModule path not set");
301 return FALSE;
304 module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
306 if (!module->library)
308 g_printerr ("%s\n", g_module_error ());
309 return FALSE;
312 /* Make sure that the loaded library contains the required methods */
313 if (! g_module_symbol (module->library,
314 "g_io_module_load",
315 (gpointer) &module->load) ||
316 ! g_module_symbol (module->library,
317 "g_io_module_unload",
318 (gpointer) &module->unload))
320 g_printerr ("%s\n", g_module_error ());
321 g_module_close (module->library);
323 return FALSE;
326 /* Initialize the loaded module */
327 module->load (module);
328 module->initialized = TRUE;
330 return TRUE;
333 static void
334 g_io_module_unload_module (GTypeModule *gmodule)
336 GIOModule *module = G_IO_MODULE (gmodule);
338 module->unload (module);
340 g_module_close (module->library);
341 module->library = NULL;
343 module->load = NULL;
344 module->unload = NULL;
348 * g_io_module_new:
349 * @filename: (type filename): filename of the shared library module.
351 * Creates a new GIOModule that will load the specific
352 * shared library when in use.
354 * Returns: a #GIOModule from given @filename,
355 * or %NULL on error.
357 GIOModule *
358 g_io_module_new (const gchar *filename)
360 GIOModule *module;
362 g_return_val_if_fail (filename != NULL, NULL);
364 module = g_object_new (G_IO_TYPE_MODULE, NULL);
365 module->filename = g_strdup (filename);
367 return module;
370 static gboolean
371 is_valid_module_name (const gchar *basename,
372 GIOModuleScope *scope)
374 gboolean result;
376 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
377 if (!g_str_has_prefix (basename, "lib") ||
378 !g_str_has_suffix (basename, ".so"))
379 return FALSE;
380 #else
381 if (!g_str_has_suffix (basename, ".dll"))
382 return FALSE;
383 #endif
385 result = TRUE;
386 if (scope)
388 result = _g_io_module_scope_contains (scope, basename) ? FALSE : TRUE;
389 if (result && (scope->flags & G_IO_MODULE_SCOPE_BLOCK_DUPLICATES))
390 g_io_module_scope_block (scope, basename);
393 return result;
398 * g_io_modules_scan_all_in_directory_with_scope:
399 * @dirname: (type filename): pathname for a directory containing modules
400 * to scan.
401 * @scope: a scope to use when scanning the modules
403 * Scans all the modules in the specified directory, ensuring that
404 * any extension point implemented by a module is registered.
406 * This may not actually load and initialize all the types in each
407 * module, some modules may be lazily loaded and initialized when
408 * an extension point it implementes is used with e.g.
409 * g_io_extension_point_get_extensions() or
410 * g_io_extension_point_get_extension_by_name().
412 * If you need to guarantee that all types are loaded in all the modules,
413 * use g_io_modules_load_all_in_directory().
415 * Since: 2.30
417 void
418 g_io_modules_scan_all_in_directory_with_scope (const char *dirname,
419 GIOModuleScope *scope)
421 const gchar *name;
422 char *filename;
423 GDir *dir;
424 GStatBuf statbuf;
425 char *data;
426 time_t cache_mtime;
427 GHashTable *cache;
429 if (!g_module_supported ())
430 return;
432 dir = g_dir_open (dirname, 0, NULL);
433 if (!dir)
434 return;
436 filename = g_build_filename (dirname, "giomodule.cache", NULL);
438 cache = g_hash_table_new_full (g_str_hash, g_str_equal,
439 g_free, (GDestroyNotify)g_strfreev);
441 cache_mtime = 0;
442 if (g_stat (filename, &statbuf) == 0 &&
443 g_file_get_contents (filename, &data, NULL, NULL))
445 char **lines;
446 int i;
448 /* Cache mtime is the time the cache file was created, any file
449 * that has a ctime before this was created then and not modified
450 * since then (userspace can't change ctime). Its possible to change
451 * the ctime forward without changing the file content, by e.g.
452 * chmoding the file, but this is uncommon and will only cause us
453 * to not use the cache so will not cause bugs.
455 cache_mtime = statbuf.st_mtime;
457 lines = g_strsplit (data, "\n", -1);
458 g_free (data);
460 for (i = 0; lines[i] != NULL; i++)
462 char *line = lines[i];
463 char *file;
464 char *colon;
465 char **extension_points;
467 if (line[0] == '#')
468 continue;
470 colon = strchr (line, ':');
471 if (colon == NULL || line == colon)
472 continue; /* Invalid line, ignore */
474 *colon = 0; /* terminate filename */
475 file = g_strdup (line);
476 colon++; /* after colon */
478 while (g_ascii_isspace (*colon))
479 colon++;
481 extension_points = g_strsplit (colon, ",", -1);
482 g_hash_table_insert (cache, file, extension_points);
484 g_strfreev (lines);
487 while ((name = g_dir_read_name (dir)))
489 if (is_valid_module_name (name, scope))
491 GIOExtensionPoint *extension_point;
492 GIOModule *module;
493 gchar *path;
494 char **extension_points;
495 int i;
497 path = g_build_filename (dirname, name, NULL);
498 module = g_io_module_new (path);
500 extension_points = g_hash_table_lookup (cache, name);
501 if (extension_points != NULL &&
502 g_stat (path, &statbuf) == 0 &&
503 statbuf.st_ctime <= cache_mtime)
505 /* Lazy load/init the library when first required */
506 for (i = 0; extension_points[i] != NULL; i++)
508 extension_point =
509 g_io_extension_point_register (extension_points[i]);
510 extension_point->lazy_load_modules =
511 g_list_prepend (extension_point->lazy_load_modules,
512 module);
515 else
517 /* Try to load and init types */
518 if (g_type_module_use (G_TYPE_MODULE (module)))
519 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
520 else
521 { /* Failure to load */
522 g_printerr ("Failed to load module: %s\n", path);
523 g_object_unref (module);
524 g_free (path);
525 continue;
529 g_free (path);
533 g_dir_close (dir);
535 g_hash_table_destroy (cache);
537 g_free (filename);
541 * g_io_modules_scan_all_in_directory:
542 * @dirname: (type filename): pathname for a directory containing modules
543 * to scan.
545 * Scans all the modules in the specified directory, ensuring that
546 * any extension point implemented by a module is registered.
548 * This may not actually load and initialize all the types in each
549 * module, some modules may be lazily loaded and initialized when
550 * an extension point it implementes is used with e.g.
551 * g_io_extension_point_get_extensions() or
552 * g_io_extension_point_get_extension_by_name().
554 * If you need to guarantee that all types are loaded in all the modules,
555 * use g_io_modules_load_all_in_directory().
557 * Since: 2.24
559 void
560 g_io_modules_scan_all_in_directory (const char *dirname)
562 g_io_modules_scan_all_in_directory_with_scope (dirname, NULL);
566 * g_io_modules_load_all_in_directory_with_scope:
567 * @dirname: (type filename): pathname for a directory containing modules
568 * to load.
569 * @scope: a scope to use when scanning the modules.
571 * Loads all the modules in the specified directory.
573 * If don't require all modules to be initialized (and thus registering
574 * all gtypes) then you can use g_io_modules_scan_all_in_directory()
575 * which allows delayed/lazy loading of modules.
577 * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
578 * from the directory,
579 * All the modules are loaded into memory, if you want to
580 * unload them (enabling on-demand loading) you must call
581 * g_type_module_unuse() on all the modules. Free the list
582 * with g_list_free().
584 * Since: 2.30
586 GList *
587 g_io_modules_load_all_in_directory_with_scope (const char *dirname,
588 GIOModuleScope *scope)
590 const gchar *name;
591 GDir *dir;
592 GList *modules;
594 if (!g_module_supported ())
595 return NULL;
597 dir = g_dir_open (dirname, 0, NULL);
598 if (!dir)
599 return NULL;
601 modules = NULL;
602 while ((name = g_dir_read_name (dir)))
604 if (is_valid_module_name (name, scope))
606 GIOModule *module;
607 gchar *path;
609 path = g_build_filename (dirname, name, NULL);
610 module = g_io_module_new (path);
612 if (!g_type_module_use (G_TYPE_MODULE (module)))
614 g_printerr ("Failed to load module: %s\n", path);
615 g_object_unref (module);
616 g_free (path);
617 continue;
620 g_free (path);
622 modules = g_list_prepend (modules, module);
626 g_dir_close (dir);
628 return modules;
632 * g_io_modules_load_all_in_directory:
633 * @dirname: (type filename): pathname for a directory containing modules
634 * to load.
636 * Loads all the modules in the specified directory.
638 * If don't require all modules to be initialized (and thus registering
639 * all gtypes) then you can use g_io_modules_scan_all_in_directory()
640 * which allows delayed/lazy loading of modules.
642 * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
643 * from the directory,
644 * All the modules are loaded into memory, if you want to
645 * unload them (enabling on-demand loading) you must call
646 * g_type_module_unuse() on all the modules. Free the list
647 * with g_list_free().
649 GList *
650 g_io_modules_load_all_in_directory (const char *dirname)
652 return g_io_modules_load_all_in_directory_with_scope (dirname, NULL);
655 static gpointer
656 try_class (GIOExtension *extension,
657 guint is_supported_offset)
659 GType type = g_io_extension_get_type (extension);
660 typedef gboolean (*verify_func) (void);
661 gpointer class;
663 class = g_type_class_ref (type);
664 if (!is_supported_offset || (* G_STRUCT_MEMBER(verify_func, class, is_supported_offset)) ())
665 return class;
667 g_type_class_unref (class);
668 return NULL;
672 * _g_io_module_get_default_type:
673 * @extension_point: the name of an extension point
674 * @envvar: (nullable): the name of an environment variable to
675 * override the default implementation.
676 * @is_supported_offset: a vtable offset, or zero
678 * Retrieves the default class implementing @extension_point.
680 * If @envvar is not %NULL, and the environment variable with that
681 * name is set, then the implementation it specifies will be tried
682 * first. After that, or if @envvar is not set, all other
683 * implementations will be tried in order of decreasing priority.
685 * If @is_supported_offset is non-zero, then it is the offset into the
686 * class vtable at which there is a function that takes no arguments and
687 * returns a boolean. This function will be called on each candidate
688 * implementation to check if it is actually usable or not.
690 * The result is cached after it is generated the first time, and
691 * the function is thread-safe.
693 * Returns: (transfer none): an object implementing
694 * @extension_point, or %NULL if there are no usable
695 * implementations.
697 GType
698 _g_io_module_get_default_type (const gchar *extension_point,
699 const gchar *envvar,
700 guint is_supported_offset)
702 static GRecMutex default_modules_lock;
703 static GHashTable *default_modules;
704 const char *use_this;
705 GList *l;
706 GIOExtensionPoint *ep;
707 GIOExtension *extension, *preferred;
708 gpointer impl;
710 g_rec_mutex_lock (&default_modules_lock);
711 if (default_modules)
713 gpointer key;
715 if (g_hash_table_lookup_extended (default_modules, extension_point, &key, &impl))
717 g_rec_mutex_unlock (&default_modules_lock);
718 return impl ? G_OBJECT_CLASS_TYPE (impl) : G_TYPE_INVALID;
721 else
723 default_modules = g_hash_table_new (g_str_hash, g_str_equal);
726 _g_io_modules_ensure_loaded ();
727 ep = g_io_extension_point_lookup (extension_point);
729 if (!ep)
731 g_warn_if_reached ();
732 g_rec_mutex_unlock (&default_modules_lock);
733 return G_TYPE_INVALID;
736 use_this = envvar ? g_getenv (envvar) : NULL;
737 if (use_this)
739 preferred = g_io_extension_point_get_extension_by_name (ep, use_this);
740 if (preferred)
742 impl = try_class (preferred, is_supported_offset);
743 if (impl)
744 goto done;
746 else
747 g_warning ("Can't find module '%s' specified in %s", use_this, envvar);
749 else
750 preferred = NULL;
752 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
754 extension = l->data;
755 if (extension == preferred)
756 continue;
758 impl = try_class (extension, is_supported_offset);
759 if (impl)
760 goto done;
763 impl = NULL;
765 done:
766 g_hash_table_insert (default_modules, g_strdup (extension_point), impl);
767 g_rec_mutex_unlock (&default_modules_lock);
769 return impl ? G_OBJECT_CLASS_TYPE (impl) : G_TYPE_INVALID;
772 static gpointer
773 try_implementation (GIOExtension *extension,
774 GIOModuleVerifyFunc verify_func)
776 GType type = g_io_extension_get_type (extension);
777 gpointer impl;
779 if (g_type_is_a (type, G_TYPE_INITABLE))
780 return g_initable_new (type, NULL, NULL, NULL);
781 else
783 impl = g_object_new (type, NULL);
784 if (!verify_func || verify_func (impl))
785 return impl;
787 g_object_unref (impl);
788 return NULL;
793 * _g_io_module_get_default:
794 * @extension_point: the name of an extension point
795 * @envvar: (nullable): the name of an environment variable to
796 * override the default implementation.
797 * @verify_func: (nullable): a function to call to verify that
798 * a given implementation is usable in the current environment.
800 * Retrieves the default object implementing @extension_point.
802 * If @envvar is not %NULL, and the environment variable with that
803 * name is set, then the implementation it specifies will be tried
804 * first. After that, or if @envvar is not set, all other
805 * implementations will be tried in order of decreasing priority.
807 * If an extension point implementation implements #GInitable, then
808 * that implementation will only be used if it initializes
809 * successfully. Otherwise, if @verify_func is not %NULL, then it will
810 * be called on each candidate implementation after construction, to
811 * check if it is actually usable or not.
813 * The result is cached after it is generated the first time, and
814 * the function is thread-safe.
816 * Returns: (transfer none): an object implementing
817 * @extension_point, or %NULL if there are no usable
818 * implementations.
820 gpointer
821 _g_io_module_get_default (const gchar *extension_point,
822 const gchar *envvar,
823 GIOModuleVerifyFunc verify_func)
825 static GRecMutex default_modules_lock;
826 static GHashTable *default_modules;
827 const char *use_this;
828 GList *l;
829 GIOExtensionPoint *ep;
830 GIOExtension *extension, *preferred;
831 gpointer impl;
833 g_rec_mutex_lock (&default_modules_lock);
834 if (default_modules)
836 gpointer key;
838 if (g_hash_table_lookup_extended (default_modules, extension_point,
839 &key, &impl))
841 g_rec_mutex_unlock (&default_modules_lock);
842 return impl;
845 else
847 default_modules = g_hash_table_new (g_str_hash, g_str_equal);
850 _g_io_modules_ensure_loaded ();
851 ep = g_io_extension_point_lookup (extension_point);
853 if (!ep)
855 g_warn_if_reached ();
856 g_rec_mutex_unlock (&default_modules_lock);
857 return NULL;
860 use_this = envvar ? g_getenv (envvar) : NULL;
861 if (use_this)
863 preferred = g_io_extension_point_get_extension_by_name (ep, use_this);
864 if (preferred)
866 impl = try_implementation (preferred, verify_func);
867 if (impl)
868 goto done;
870 else
871 g_warning ("Can't find module '%s' specified in %s", use_this, envvar);
873 else
874 preferred = NULL;
876 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
878 extension = l->data;
879 if (extension == preferred)
880 continue;
882 impl = try_implementation (extension, verify_func);
883 if (impl)
884 goto done;
887 impl = NULL;
889 done:
890 g_hash_table_insert (default_modules,
891 g_strdup (extension_point),
892 impl ? g_object_ref (impl) : NULL);
893 g_rec_mutex_unlock (&default_modules_lock);
895 return impl;
898 G_LOCK_DEFINE_STATIC (registered_extensions);
899 G_LOCK_DEFINE_STATIC (loaded_dirs);
901 extern GType g_fen_file_monitor_get_type (void);
902 extern GType g_inotify_file_monitor_get_type (void);
903 extern GType g_kqueue_file_monitor_get_type (void);
904 extern GType g_win32_file_monitor_get_type (void);
906 extern GType _g_unix_volume_monitor_get_type (void);
907 extern GType _g_local_vfs_get_type (void);
909 extern GType _g_win32_volume_monitor_get_type (void);
910 extern GType _g_winhttp_vfs_get_type (void);
912 extern GType _g_dummy_proxy_resolver_get_type (void);
913 extern GType _g_dummy_tls_backend_get_type (void);
914 extern GType g_network_monitor_base_get_type (void);
915 #ifdef HAVE_NETLINK
916 extern GType _g_network_monitor_netlink_get_type (void);
917 extern GType _g_network_monitor_nm_get_type (void);
918 #endif
920 #ifdef G_OS_UNIX
921 extern GType g_fdo_notification_backend_get_type (void);
922 extern GType g_gtk_notification_backend_get_type (void);
923 extern GType g_portal_notification_backend_get_type (void);
924 extern GType g_proxy_resolver_portal_get_type (void);
925 extern GType g_network_monitor_portal_get_type (void);
926 #endif
928 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
929 extern GType g_cocoa_notification_backend_get_type (void);
930 #endif
932 #ifdef G_PLATFORM_WIN32
934 #include <windows.h>
936 static HMODULE gio_dll = NULL;
938 #ifdef DLL_EXPORT
940 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
941 DWORD fdwReason,
942 LPVOID lpvReserved);
944 BOOL WINAPI
945 DllMain (HINSTANCE hinstDLL,
946 DWORD fdwReason,
947 LPVOID lpvReserved)
949 if (fdwReason == DLL_PROCESS_ATTACH)
950 gio_dll = hinstDLL;
952 return TRUE;
955 #endif
957 void *
958 _g_io_win32_get_module (void)
960 if (!gio_dll)
961 GetModuleHandleExA (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
962 (const char *) _g_io_win32_get_module,
963 &gio_dll);
964 return gio_dll;
967 #endif
969 void
970 _g_io_modules_ensure_extension_points_registered (void)
972 static gboolean registered_extensions = FALSE;
973 GIOExtensionPoint *ep;
975 G_LOCK (registered_extensions);
977 if (!registered_extensions)
979 registered_extensions = TRUE;
981 #if defined(G_OS_UNIX) && !defined(HAVE_COCOA)
982 #if !GLIB_CHECK_VERSION (3, 0, 0)
983 ep = g_io_extension_point_register (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
984 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
985 g_io_extension_point_set_required_type (ep, G_TYPE_DESKTOP_APP_INFO_LOOKUP);
986 G_GNUC_END_IGNORE_DEPRECATIONS
987 #endif
988 #endif
990 ep = g_io_extension_point_register (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
991 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
993 ep = g_io_extension_point_register (G_NFS_FILE_MONITOR_EXTENSION_POINT_NAME);
994 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
996 ep = g_io_extension_point_register (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
997 g_io_extension_point_set_required_type (ep, G_TYPE_VOLUME_MONITOR);
999 ep = g_io_extension_point_register (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
1000 g_io_extension_point_set_required_type (ep, G_TYPE_NATIVE_VOLUME_MONITOR);
1002 ep = g_io_extension_point_register (G_VFS_EXTENSION_POINT_NAME);
1003 g_io_extension_point_set_required_type (ep, G_TYPE_VFS);
1005 ep = g_io_extension_point_register ("gsettings-backend");
1006 g_io_extension_point_set_required_type (ep, G_TYPE_OBJECT);
1008 ep = g_io_extension_point_register (G_PROXY_RESOLVER_EXTENSION_POINT_NAME);
1009 g_io_extension_point_set_required_type (ep, G_TYPE_PROXY_RESOLVER);
1011 ep = g_io_extension_point_register (G_PROXY_EXTENSION_POINT_NAME);
1012 g_io_extension_point_set_required_type (ep, G_TYPE_PROXY);
1014 ep = g_io_extension_point_register (G_TLS_BACKEND_EXTENSION_POINT_NAME);
1015 g_io_extension_point_set_required_type (ep, G_TYPE_TLS_BACKEND);
1017 ep = g_io_extension_point_register (G_NETWORK_MONITOR_EXTENSION_POINT_NAME);
1018 g_io_extension_point_set_required_type (ep, G_TYPE_NETWORK_MONITOR);
1020 ep = g_io_extension_point_register (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME);
1021 g_io_extension_point_set_required_type (ep, G_TYPE_NOTIFICATION_BACKEND);
1024 G_UNLOCK (registered_extensions);
1027 static gchar *
1028 get_gio_module_dir (void)
1030 gchar *module_dir;
1032 module_dir = g_strdup (g_getenv ("GIO_MODULE_DIR"));
1033 if (module_dir == NULL)
1035 #ifdef G_OS_WIN32
1036 gchar *install_dir;
1038 install_dir = g_win32_get_package_installation_directory_of_module (gio_dll);
1039 #ifdef _MSC_VER
1040 /* On Visual Studio builds we have all the libraries and binaries in bin
1041 * so better load the gio modules from bin instead of lib
1043 module_dir = g_build_filename (install_dir,
1044 "bin", "gio", "modules",
1045 NULL);
1046 #else
1047 module_dir = g_build_filename (install_dir,
1048 "lib", "gio", "modules",
1049 NULL);
1050 #endif
1051 g_free (install_dir);
1052 #else
1053 module_dir = g_strdup (GIO_MODULE_DIR);
1054 #endif
1057 return module_dir;
1060 void
1061 _g_io_modules_ensure_loaded (void)
1063 static gboolean loaded_dirs = FALSE;
1064 const char *module_path;
1065 GIOModuleScope *scope;
1067 _g_io_modules_ensure_extension_points_registered ();
1069 G_LOCK (loaded_dirs);
1071 if (!loaded_dirs)
1073 gchar *module_dir;
1075 loaded_dirs = TRUE;
1076 scope = g_io_module_scope_new (G_IO_MODULE_SCOPE_BLOCK_DUPLICATES);
1078 /* First load any overrides, extras */
1079 module_path = g_getenv ("GIO_EXTRA_MODULES");
1080 if (module_path)
1082 gchar **paths;
1083 int i;
1085 paths = g_strsplit (module_path, G_SEARCHPATH_SEPARATOR_S, 0);
1087 for (i = 0; paths[i] != NULL; i++)
1089 g_io_modules_scan_all_in_directory_with_scope (paths[i], scope);
1092 g_strfreev (paths);
1095 /* Then load the compiled in path */
1096 module_dir = get_gio_module_dir ();
1098 g_io_modules_scan_all_in_directory_with_scope (module_dir, scope);
1099 g_free (module_dir);
1101 g_io_module_scope_free (scope);
1103 /* Initialize types from built-in "modules" */
1104 g_type_ensure (g_null_settings_backend_get_type ());
1105 g_type_ensure (g_memory_settings_backend_get_type ());
1106 #if defined(HAVE_INOTIFY_INIT1)
1107 g_type_ensure (g_inotify_file_monitor_get_type ());
1108 #endif
1109 #if defined(HAVE_KQUEUE)
1110 g_type_ensure (g_kqueue_file_monitor_get_type ());
1111 #endif
1112 #if defined(HAVE_FEN)
1113 g_type_ensure (g_fen_file_monitor_get_type ());
1114 #endif
1115 #ifdef G_OS_WIN32
1116 g_type_ensure (_g_win32_volume_monitor_get_type ());
1117 g_type_ensure (g_win32_file_monitor_get_type ());
1118 g_type_ensure (g_registry_backend_get_type ());
1119 #endif
1120 #ifdef HAVE_COCOA
1121 g_type_ensure (g_nextstep_settings_backend_get_type ());
1122 g_type_ensure (g_osx_app_info_get_type ());
1123 #endif
1124 #ifdef G_OS_UNIX
1125 g_type_ensure (_g_unix_volume_monitor_get_type ());
1126 g_type_ensure (g_fdo_notification_backend_get_type ());
1127 g_type_ensure (g_gtk_notification_backend_get_type ());
1128 g_type_ensure (g_portal_notification_backend_get_type ());
1129 g_type_ensure (g_network_monitor_portal_get_type ());
1130 g_type_ensure (g_proxy_resolver_portal_get_type ());
1131 #endif
1132 #if HAVE_MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
1133 g_type_ensure (g_cocoa_notification_backend_get_type ());
1134 #endif
1135 #ifdef G_OS_WIN32
1136 g_type_ensure (_g_winhttp_vfs_get_type ());
1137 #endif
1138 g_type_ensure (_g_local_vfs_get_type ());
1139 g_type_ensure (_g_dummy_proxy_resolver_get_type ());
1140 g_type_ensure (_g_http_proxy_get_type ());
1141 g_type_ensure (_g_https_proxy_get_type ());
1142 g_type_ensure (_g_socks4a_proxy_get_type ());
1143 g_type_ensure (_g_socks4_proxy_get_type ());
1144 g_type_ensure (_g_socks5_proxy_get_type ());
1145 g_type_ensure (_g_dummy_tls_backend_get_type ());
1146 g_type_ensure (g_network_monitor_base_get_type ());
1147 #ifdef HAVE_NETLINK
1148 g_type_ensure (_g_network_monitor_netlink_get_type ());
1149 g_type_ensure (_g_network_monitor_nm_get_type ());
1150 #endif
1153 G_UNLOCK (loaded_dirs);
1156 static void
1157 g_io_extension_point_free (GIOExtensionPoint *ep)
1159 g_free (ep->name);
1160 g_free (ep);
1164 * g_io_extension_point_register:
1165 * @name: The name of the extension point
1167 * Registers an extension point.
1169 * Returns: (transfer none): the new #GIOExtensionPoint. This object is
1170 * owned by GIO and should not be freed.
1172 GIOExtensionPoint *
1173 g_io_extension_point_register (const char *name)
1175 GIOExtensionPoint *ep;
1177 G_LOCK (extension_points);
1178 if (extension_points == NULL)
1179 extension_points = g_hash_table_new_full (g_str_hash,
1180 g_str_equal,
1181 NULL,
1182 (GDestroyNotify)g_io_extension_point_free);
1184 ep = g_hash_table_lookup (extension_points, name);
1185 if (ep != NULL)
1187 G_UNLOCK (extension_points);
1188 return ep;
1191 ep = g_new0 (GIOExtensionPoint, 1);
1192 ep->name = g_strdup (name);
1194 g_hash_table_insert (extension_points, ep->name, ep);
1196 G_UNLOCK (extension_points);
1198 return ep;
1202 * g_io_extension_point_lookup:
1203 * @name: the name of the extension point
1205 * Looks up an existing extension point.
1207 * Returns: (transfer none): the #GIOExtensionPoint, or %NULL if there
1208 * is no registered extension point with the given name.
1210 GIOExtensionPoint *
1211 g_io_extension_point_lookup (const char *name)
1213 GIOExtensionPoint *ep;
1215 G_LOCK (extension_points);
1216 ep = NULL;
1217 if (extension_points != NULL)
1218 ep = g_hash_table_lookup (extension_points, name);
1220 G_UNLOCK (extension_points);
1222 return ep;
1227 * g_io_extension_point_set_required_type:
1228 * @extension_point: a #GIOExtensionPoint
1229 * @type: the #GType to require
1231 * Sets the required type for @extension_point to @type.
1232 * All implementations must henceforth have this type.
1234 void
1235 g_io_extension_point_set_required_type (GIOExtensionPoint *extension_point,
1236 GType type)
1238 extension_point->required_type = type;
1242 * g_io_extension_point_get_required_type:
1243 * @extension_point: a #GIOExtensionPoint
1245 * Gets the required type for @extension_point.
1247 * Returns: the #GType that all implementations must have,
1248 * or #G_TYPE_INVALID if the extension point has no required type
1250 GType
1251 g_io_extension_point_get_required_type (GIOExtensionPoint *extension_point)
1253 return extension_point->required_type;
1256 static void
1257 lazy_load_modules (GIOExtensionPoint *extension_point)
1259 GIOModule *module;
1260 GList *l;
1262 for (l = extension_point->lazy_load_modules; l != NULL; l = l->next)
1264 module = l->data;
1266 if (!module->initialized)
1268 if (g_type_module_use (G_TYPE_MODULE (module)))
1269 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
1270 else
1271 g_printerr ("Failed to load module: %s\n",
1272 module->filename);
1278 * g_io_extension_point_get_extensions:
1279 * @extension_point: a #GIOExtensionPoint
1281 * Gets a list of all extensions that implement this extension point.
1282 * The list is sorted by priority, beginning with the highest priority.
1284 * Returns: (element-type GIOExtension) (transfer none): a #GList of
1285 * #GIOExtensions. The list is owned by GIO and should not be
1286 * modified.
1288 GList *
1289 g_io_extension_point_get_extensions (GIOExtensionPoint *extension_point)
1291 g_return_val_if_fail (extension_point != NULL, NULL);
1293 lazy_load_modules (extension_point);
1294 return extension_point->extensions;
1298 * g_io_extension_point_get_extension_by_name:
1299 * @extension_point: a #GIOExtensionPoint
1300 * @name: the name of the extension to get
1302 * Finds a #GIOExtension for an extension point by name.
1304 * Returns: (transfer none): the #GIOExtension for @extension_point that has the
1305 * given name, or %NULL if there is no extension with that name
1307 GIOExtension *
1308 g_io_extension_point_get_extension_by_name (GIOExtensionPoint *extension_point,
1309 const char *name)
1311 GList *l;
1313 g_return_val_if_fail (name != NULL, NULL);
1315 lazy_load_modules (extension_point);
1316 for (l = extension_point->extensions; l != NULL; l = l->next)
1318 GIOExtension *e = l->data;
1320 if (e->name != NULL &&
1321 strcmp (e->name, name) == 0)
1322 return e;
1325 return NULL;
1328 static gint
1329 extension_prio_compare (gconstpointer a,
1330 gconstpointer b)
1332 const GIOExtension *extension_a = a, *extension_b = b;
1334 if (extension_a->priority > extension_b->priority)
1335 return -1;
1337 if (extension_b->priority > extension_a->priority)
1338 return 1;
1340 return 0;
1344 * g_io_extension_point_implement:
1345 * @extension_point_name: the name of the extension point
1346 * @type: the #GType to register as extension
1347 * @extension_name: the name for the extension
1348 * @priority: the priority for the extension
1350 * Registers @type as extension for the extension point with name
1351 * @extension_point_name.
1353 * If @type has already been registered as an extension for this
1354 * extension point, the existing #GIOExtension object is returned.
1356 * Returns: (transfer none): a #GIOExtension object for #GType
1358 GIOExtension *
1359 g_io_extension_point_implement (const char *extension_point_name,
1360 GType type,
1361 const char *extension_name,
1362 gint priority)
1364 GIOExtensionPoint *extension_point;
1365 GIOExtension *extension;
1366 GList *l;
1368 g_return_val_if_fail (extension_point_name != NULL, NULL);
1370 extension_point = g_io_extension_point_lookup (extension_point_name);
1371 if (extension_point == NULL)
1373 g_warning ("Tried to implement non-registered extension point %s", extension_point_name);
1374 return NULL;
1377 if (extension_point->required_type != 0 &&
1378 !g_type_is_a (type, extension_point->required_type))
1380 g_warning ("Tried to register an extension of the type %s to extension point %s. "
1381 "Expected type is %s.",
1382 g_type_name (type),
1383 extension_point_name,
1384 g_type_name (extension_point->required_type));
1385 return NULL;
1388 /* It's safe to register the same type multiple times */
1389 for (l = extension_point->extensions; l != NULL; l = l->next)
1391 extension = l->data;
1392 if (extension->type == type)
1393 return extension;
1396 extension = g_slice_new0 (GIOExtension);
1397 extension->type = type;
1398 extension->name = g_strdup (extension_name);
1399 extension->priority = priority;
1401 extension_point->extensions = g_list_insert_sorted (extension_point->extensions,
1402 extension, extension_prio_compare);
1404 return extension;
1408 * g_io_extension_ref_class:
1409 * @extension: a #GIOExtension
1411 * Gets a reference to the class for the type that is
1412 * associated with @extension.
1414 * Returns: (transfer full): the #GTypeClass for the type of @extension
1416 GTypeClass *
1417 g_io_extension_ref_class (GIOExtension *extension)
1419 return g_type_class_ref (extension->type);
1423 * g_io_extension_get_type:
1424 * @extension: a #GIOExtension
1426 * Gets the type associated with @extension.
1428 * Returns: the type of @extension
1430 GType
1431 g_io_extension_get_type (GIOExtension *extension)
1433 return extension->type;
1437 * g_io_extension_get_name:
1438 * @extension: a #GIOExtension
1440 * Gets the name under which @extension was registered.
1442 * Note that the same type may be registered as extension
1443 * for multiple extension points, under different names.
1445 * Returns: the name of @extension.
1447 const char *
1448 g_io_extension_get_name (GIOExtension *extension)
1450 return extension->name;
1454 * g_io_extension_get_priority:
1455 * @extension: a #GIOExtension
1457 * Gets the priority with which @extension was registered.
1459 * Returns: the priority of @extension
1461 gint
1462 g_io_extension_get_priority (GIOExtension *extension)
1464 return extension->priority;