Merge branch 'test-ip_mreq_source-android-only' into 'master'
[glib.git] / gio / giomodule.c
blob36c0cefed212fe87337e783c2bc3045c6b420985
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 load_symbols (GIOModule *module)
296 gchar *name;
297 gchar *load_symname;
298 gchar *unload_symname;
299 gboolean ret;
301 name = _g_io_module_extract_name (module->filename);
302 load_symname = g_strconcat ("g_io_", name, "_load", NULL);
303 unload_symname = g_strconcat ("g_io_", name, "_unload", NULL);
305 ret = g_module_symbol (module->library,
306 load_symname,
307 (gpointer) &module->load) &&
308 g_module_symbol (module->library,
309 unload_symname,
310 (gpointer) &module->unload);
312 if (!ret)
314 /* Fallback to old names */
315 ret = g_module_symbol (module->library,
316 "g_io_module_load",
317 (gpointer) &module->load) &&
318 g_module_symbol (module->library,
319 "g_io_module_unload",
320 (gpointer) &module->unload);
323 g_free (name);
324 g_free (load_symname);
325 g_free (unload_symname);
327 return ret;
330 static gboolean
331 g_io_module_load_module (GTypeModule *gmodule)
333 GIOModule *module = G_IO_MODULE (gmodule);
335 if (!module->filename)
337 g_warning ("GIOModule path not set");
338 return FALSE;
341 module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
343 if (!module->library)
345 g_printerr ("%s\n", g_module_error ());
346 return FALSE;
349 /* Make sure that the loaded library contains the required methods */
350 if (!load_symbols (module))
352 g_printerr ("%s\n", g_module_error ());
353 g_module_close (module->library);
355 return FALSE;
358 /* Initialize the loaded module */
359 module->load (module);
360 module->initialized = TRUE;
362 return TRUE;
365 static void
366 g_io_module_unload_module (GTypeModule *gmodule)
368 GIOModule *module = G_IO_MODULE (gmodule);
370 module->unload (module);
372 g_module_close (module->library);
373 module->library = NULL;
375 module->load = NULL;
376 module->unload = NULL;
380 * g_io_module_new:
381 * @filename: (type filename): filename of the shared library module.
383 * Creates a new GIOModule that will load the specific
384 * shared library when in use.
386 * Returns: a #GIOModule from given @filename,
387 * or %NULL on error.
389 GIOModule *
390 g_io_module_new (const gchar *filename)
392 GIOModule *module;
394 g_return_val_if_fail (filename != NULL, NULL);
396 module = g_object_new (G_IO_TYPE_MODULE, NULL);
397 module->filename = g_strdup (filename);
399 return module;
402 static gboolean
403 is_valid_module_name (const gchar *basename,
404 GIOModuleScope *scope)
406 gboolean result;
408 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
409 if (!g_str_has_prefix (basename, "lib") ||
410 !g_str_has_suffix (basename, ".so"))
411 return FALSE;
412 #else
413 if (!g_str_has_suffix (basename, ".dll"))
414 return FALSE;
415 #endif
417 result = TRUE;
418 if (scope)
420 result = _g_io_module_scope_contains (scope, basename) ? FALSE : TRUE;
421 if (result && (scope->flags & G_IO_MODULE_SCOPE_BLOCK_DUPLICATES))
422 g_io_module_scope_block (scope, basename);
425 return result;
430 * g_io_modules_scan_all_in_directory_with_scope:
431 * @dirname: (type filename): pathname for a directory containing modules
432 * to scan.
433 * @scope: a scope to use when scanning the modules
435 * Scans all the modules in the specified directory, ensuring that
436 * any extension point implemented by a module is registered.
438 * This may not actually load and initialize all the types in each
439 * module, some modules may be lazily loaded and initialized when
440 * an extension point it implementes is used with e.g.
441 * g_io_extension_point_get_extensions() or
442 * g_io_extension_point_get_extension_by_name().
444 * If you need to guarantee that all types are loaded in all the modules,
445 * use g_io_modules_load_all_in_directory().
447 * Since: 2.30
449 void
450 g_io_modules_scan_all_in_directory_with_scope (const char *dirname,
451 GIOModuleScope *scope)
453 const gchar *name;
454 char *filename;
455 GDir *dir;
456 GStatBuf statbuf;
457 char *data;
458 time_t cache_mtime;
459 GHashTable *cache;
461 if (!g_module_supported ())
462 return;
464 dir = g_dir_open (dirname, 0, NULL);
465 if (!dir)
466 return;
468 filename = g_build_filename (dirname, "giomodule.cache", NULL);
470 cache = g_hash_table_new_full (g_str_hash, g_str_equal,
471 g_free, (GDestroyNotify)g_strfreev);
473 cache_mtime = 0;
474 if (g_stat (filename, &statbuf) == 0 &&
475 g_file_get_contents (filename, &data, NULL, NULL))
477 char **lines;
478 int i;
480 /* Cache mtime is the time the cache file was created, any file
481 * that has a ctime before this was created then and not modified
482 * since then (userspace can't change ctime). Its possible to change
483 * the ctime forward without changing the file content, by e.g.
484 * chmoding the file, but this is uncommon and will only cause us
485 * to not use the cache so will not cause bugs.
487 cache_mtime = statbuf.st_mtime;
489 lines = g_strsplit (data, "\n", -1);
490 g_free (data);
492 for (i = 0; lines[i] != NULL; i++)
494 char *line = lines[i];
495 char *file;
496 char *colon;
497 char **extension_points;
499 if (line[0] == '#')
500 continue;
502 colon = strchr (line, ':');
503 if (colon == NULL || line == colon)
504 continue; /* Invalid line, ignore */
506 *colon = 0; /* terminate filename */
507 file = g_strdup (line);
508 colon++; /* after colon */
510 while (g_ascii_isspace (*colon))
511 colon++;
513 extension_points = g_strsplit (colon, ",", -1);
514 g_hash_table_insert (cache, file, extension_points);
516 g_strfreev (lines);
519 while ((name = g_dir_read_name (dir)))
521 if (is_valid_module_name (name, scope))
523 GIOExtensionPoint *extension_point;
524 GIOModule *module;
525 gchar *path;
526 char **extension_points;
527 int i;
529 path = g_build_filename (dirname, name, NULL);
530 module = g_io_module_new (path);
532 extension_points = g_hash_table_lookup (cache, name);
533 if (extension_points != NULL &&
534 g_stat (path, &statbuf) == 0 &&
535 statbuf.st_ctime <= cache_mtime)
537 /* Lazy load/init the library when first required */
538 for (i = 0; extension_points[i] != NULL; i++)
540 extension_point =
541 g_io_extension_point_register (extension_points[i]);
542 extension_point->lazy_load_modules =
543 g_list_prepend (extension_point->lazy_load_modules,
544 module);
547 else
549 /* Try to load and init types */
550 if (g_type_module_use (G_TYPE_MODULE (module)))
551 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
552 else
553 { /* Failure to load */
554 g_printerr ("Failed to load module: %s\n", path);
555 g_object_unref (module);
556 g_free (path);
557 continue;
561 g_free (path);
565 g_dir_close (dir);
567 g_hash_table_destroy (cache);
569 g_free (filename);
573 * g_io_modules_scan_all_in_directory:
574 * @dirname: (type filename): pathname for a directory containing modules
575 * to scan.
577 * Scans all the modules in the specified directory, ensuring that
578 * any extension point implemented by a module is registered.
580 * This may not actually load and initialize all the types in each
581 * module, some modules may be lazily loaded and initialized when
582 * an extension point it implementes is used with e.g.
583 * g_io_extension_point_get_extensions() or
584 * g_io_extension_point_get_extension_by_name().
586 * If you need to guarantee that all types are loaded in all the modules,
587 * use g_io_modules_load_all_in_directory().
589 * Since: 2.24
591 void
592 g_io_modules_scan_all_in_directory (const char *dirname)
594 g_io_modules_scan_all_in_directory_with_scope (dirname, NULL);
598 * g_io_modules_load_all_in_directory_with_scope:
599 * @dirname: (type filename): pathname for a directory containing modules
600 * to load.
601 * @scope: a scope to use when scanning the modules.
603 * Loads all the modules in the specified directory.
605 * If don't require all modules to be initialized (and thus registering
606 * all gtypes) then you can use g_io_modules_scan_all_in_directory()
607 * which allows delayed/lazy loading of modules.
609 * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
610 * from the directory,
611 * All the modules are loaded into memory, if you want to
612 * unload them (enabling on-demand loading) you must call
613 * g_type_module_unuse() on all the modules. Free the list
614 * with g_list_free().
616 * Since: 2.30
618 GList *
619 g_io_modules_load_all_in_directory_with_scope (const char *dirname,
620 GIOModuleScope *scope)
622 const gchar *name;
623 GDir *dir;
624 GList *modules;
626 if (!g_module_supported ())
627 return NULL;
629 dir = g_dir_open (dirname, 0, NULL);
630 if (!dir)
631 return NULL;
633 modules = NULL;
634 while ((name = g_dir_read_name (dir)))
636 if (is_valid_module_name (name, scope))
638 GIOModule *module;
639 gchar *path;
641 path = g_build_filename (dirname, name, NULL);
642 module = g_io_module_new (path);
644 if (!g_type_module_use (G_TYPE_MODULE (module)))
646 g_printerr ("Failed to load module: %s\n", path);
647 g_object_unref (module);
648 g_free (path);
649 continue;
652 g_free (path);
654 modules = g_list_prepend (modules, module);
658 g_dir_close (dir);
660 return modules;
664 * g_io_modules_load_all_in_directory:
665 * @dirname: (type filename): pathname for a directory containing modules
666 * to load.
668 * Loads all the modules in the specified directory.
670 * If don't require all modules to be initialized (and thus registering
671 * all gtypes) then you can use g_io_modules_scan_all_in_directory()
672 * which allows delayed/lazy loading of modules.
674 * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
675 * from the directory,
676 * All the modules are loaded into memory, if you want to
677 * unload them (enabling on-demand loading) you must call
678 * g_type_module_unuse() on all the modules. Free the list
679 * with g_list_free().
681 GList *
682 g_io_modules_load_all_in_directory (const char *dirname)
684 return g_io_modules_load_all_in_directory_with_scope (dirname, NULL);
687 static gpointer
688 try_class (GIOExtension *extension,
689 guint is_supported_offset)
691 GType type = g_io_extension_get_type (extension);
692 typedef gboolean (*verify_func) (void);
693 gpointer class;
695 class = g_type_class_ref (type);
696 if (!is_supported_offset || (* G_STRUCT_MEMBER(verify_func, class, is_supported_offset)) ())
697 return class;
699 g_type_class_unref (class);
700 return NULL;
704 * _g_io_module_get_default_type:
705 * @extension_point: the name of an extension point
706 * @envvar: (nullable): the name of an environment variable to
707 * override the default implementation.
708 * @is_supported_offset: a vtable offset, or zero
710 * Retrieves the default class implementing @extension_point.
712 * If @envvar is not %NULL, and the environment variable with that
713 * name is set, then the implementation it specifies will be tried
714 * first. After that, or if @envvar is not set, all other
715 * implementations will be tried in order of decreasing priority.
717 * If @is_supported_offset is non-zero, then it is the offset into the
718 * class vtable at which there is a function that takes no arguments and
719 * returns a boolean. This function will be called on each candidate
720 * implementation to check if it is actually usable or not.
722 * The result is cached after it is generated the first time, and
723 * the function is thread-safe.
725 * Returns: (transfer none): the type to instantiate to implement
726 * @extension_point, or %G_TYPE_INVALID if there are no usable
727 * implementations.
729 GType
730 _g_io_module_get_default_type (const gchar *extension_point,
731 const gchar *envvar,
732 guint is_supported_offset)
734 static GRecMutex default_modules_lock;
735 static GHashTable *default_modules;
736 const char *use_this;
737 GList *l;
738 GIOExtensionPoint *ep;
739 GIOExtension *extension, *preferred;
740 gpointer impl;
742 g_rec_mutex_lock (&default_modules_lock);
743 if (default_modules)
745 gpointer key;
747 if (g_hash_table_lookup_extended (default_modules, extension_point, &key, &impl))
749 g_rec_mutex_unlock (&default_modules_lock);
750 return impl ? G_OBJECT_CLASS_TYPE (impl) : G_TYPE_INVALID;
753 else
755 default_modules = g_hash_table_new (g_str_hash, g_str_equal);
758 _g_io_modules_ensure_loaded ();
759 ep = g_io_extension_point_lookup (extension_point);
761 if (!ep)
763 g_warn_if_reached ();
764 g_rec_mutex_unlock (&default_modules_lock);
765 return G_TYPE_INVALID;
768 use_this = envvar ? g_getenv (envvar) : NULL;
769 if (use_this)
771 preferred = g_io_extension_point_get_extension_by_name (ep, use_this);
772 if (preferred)
774 impl = try_class (preferred, is_supported_offset);
775 if (impl)
776 goto done;
778 else
779 g_warning ("Can't find module '%s' specified in %s", use_this, envvar);
781 else
782 preferred = NULL;
784 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
786 extension = l->data;
787 if (extension == preferred)
788 continue;
790 impl = try_class (extension, is_supported_offset);
791 if (impl)
792 goto done;
795 impl = NULL;
797 done:
798 g_hash_table_insert (default_modules, g_strdup (extension_point), impl);
799 g_rec_mutex_unlock (&default_modules_lock);
801 return impl ? G_OBJECT_CLASS_TYPE (impl) : G_TYPE_INVALID;
804 static gpointer
805 try_implementation (const char *extension_point,
806 GIOExtension *extension,
807 GIOModuleVerifyFunc verify_func)
809 GType type = g_io_extension_get_type (extension);
810 gpointer impl;
812 if (g_type_is_a (type, G_TYPE_INITABLE))
814 GError *error = NULL;
816 impl = g_initable_new (type, NULL, &error, NULL);
817 if (impl)
818 return impl;
820 g_debug ("Failed to initialize %s (%s) for %s: %s",
821 g_io_extension_get_name (extension),
822 g_type_name (type),
823 extension_point,
824 error ? error->message : "");
825 g_clear_error (&error);
826 return NULL;
828 else
830 impl = g_object_new (type, NULL);
831 if (!verify_func || verify_func (impl))
832 return impl;
834 g_object_unref (impl);
835 return NULL;
840 * _g_io_module_get_default:
841 * @extension_point: the name of an extension point
842 * @envvar: (nullable): the name of an environment variable to
843 * override the default implementation.
844 * @verify_func: (nullable): a function to call to verify that
845 * a given implementation is usable in the current environment.
847 * Retrieves the default object implementing @extension_point.
849 * If @envvar is not %NULL, and the environment variable with that
850 * name is set, then the implementation it specifies will be tried
851 * first. After that, or if @envvar is not set, all other
852 * implementations will be tried in order of decreasing priority.
854 * If an extension point implementation implements #GInitable, then
855 * that implementation will only be used if it initializes
856 * successfully. Otherwise, if @verify_func is not %NULL, then it will
857 * be called on each candidate implementation after construction, to
858 * check if it is actually usable or not.
860 * The result is cached after it is generated the first time, and
861 * the function is thread-safe.
863 * Returns: (transfer none): an object implementing
864 * @extension_point, or %NULL if there are no usable
865 * implementations.
867 gpointer
868 _g_io_module_get_default (const gchar *extension_point,
869 const gchar *envvar,
870 GIOModuleVerifyFunc verify_func)
872 static GRecMutex default_modules_lock;
873 static GHashTable *default_modules;
874 const char *use_this;
875 GList *l;
876 GIOExtensionPoint *ep;
877 GIOExtension *extension, *preferred;
878 gpointer impl;
880 g_rec_mutex_lock (&default_modules_lock);
881 if (default_modules)
883 gpointer key;
885 if (g_hash_table_lookup_extended (default_modules, extension_point,
886 &key, &impl))
888 g_rec_mutex_unlock (&default_modules_lock);
889 return impl;
892 else
894 default_modules = g_hash_table_new (g_str_hash, g_str_equal);
897 _g_io_modules_ensure_loaded ();
898 ep = g_io_extension_point_lookup (extension_point);
900 if (!ep)
902 g_warn_if_reached ();
903 g_rec_mutex_unlock (&default_modules_lock);
904 return NULL;
907 use_this = envvar ? g_getenv (envvar) : NULL;
908 if (use_this)
910 preferred = g_io_extension_point_get_extension_by_name (ep, use_this);
911 if (preferred)
913 impl = try_implementation (extension_point, preferred, verify_func);
914 if (impl)
915 goto done;
917 else
918 g_warning ("Can't find module '%s' specified in %s", use_this, envvar);
920 else
921 preferred = NULL;
923 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
925 extension = l->data;
926 if (extension == preferred)
927 continue;
929 impl = try_implementation (extension_point, extension, verify_func);
930 if (impl)
931 goto done;
934 impl = NULL;
936 done:
937 g_hash_table_insert (default_modules,
938 g_strdup (extension_point),
939 impl ? g_object_ref (impl) : NULL);
940 g_rec_mutex_unlock (&default_modules_lock);
942 return impl;
945 G_LOCK_DEFINE_STATIC (registered_extensions);
946 G_LOCK_DEFINE_STATIC (loaded_dirs);
948 extern GType g_fen_file_monitor_get_type (void);
949 extern GType g_inotify_file_monitor_get_type (void);
950 extern GType g_kqueue_file_monitor_get_type (void);
951 extern GType g_win32_file_monitor_get_type (void);
953 extern GType _g_unix_volume_monitor_get_type (void);
954 extern GType _g_local_vfs_get_type (void);
956 extern GType _g_win32_volume_monitor_get_type (void);
957 extern GType _g_winhttp_vfs_get_type (void);
959 extern GType _g_dummy_proxy_resolver_get_type (void);
960 extern GType _g_dummy_tls_backend_get_type (void);
961 extern GType g_network_monitor_base_get_type (void);
962 #ifdef HAVE_NETLINK
963 extern GType _g_network_monitor_netlink_get_type (void);
964 extern GType _g_network_monitor_nm_get_type (void);
965 #endif
967 #ifdef G_OS_UNIX
968 extern GType g_fdo_notification_backend_get_type (void);
969 extern GType g_gtk_notification_backend_get_type (void);
970 extern GType g_portal_notification_backend_get_type (void);
971 extern GType g_proxy_resolver_portal_get_type (void);
972 extern GType g_network_monitor_portal_get_type (void);
973 #endif
975 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
976 extern GType g_cocoa_notification_backend_get_type (void);
977 #endif
979 #ifdef G_PLATFORM_WIN32
980 extern GType g_win32_notification_backend_get_type (void);
982 #include <windows.h>
983 extern GType _g_win32_network_monitor_get_type (void);
985 static HMODULE gio_dll = NULL;
987 #ifdef DLL_EXPORT
989 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
990 DWORD fdwReason,
991 LPVOID lpvReserved);
993 BOOL WINAPI
994 DllMain (HINSTANCE hinstDLL,
995 DWORD fdwReason,
996 LPVOID lpvReserved)
998 if (fdwReason == DLL_PROCESS_ATTACH)
999 gio_dll = hinstDLL;
1001 return TRUE;
1004 #endif
1006 void *
1007 _g_io_win32_get_module (void)
1009 if (!gio_dll)
1010 GetModuleHandleExA (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
1011 (const char *) _g_io_win32_get_module,
1012 &gio_dll);
1013 return gio_dll;
1016 #endif
1018 void
1019 _g_io_modules_ensure_extension_points_registered (void)
1021 static gboolean registered_extensions = FALSE;
1022 GIOExtensionPoint *ep;
1024 G_LOCK (registered_extensions);
1026 if (!registered_extensions)
1028 registered_extensions = TRUE;
1030 #if defined(G_OS_UNIX) && !defined(HAVE_COCOA)
1031 #if !GLIB_CHECK_VERSION (3, 0, 0)
1032 ep = g_io_extension_point_register (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
1033 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
1034 g_io_extension_point_set_required_type (ep, G_TYPE_DESKTOP_APP_INFO_LOOKUP);
1035 G_GNUC_END_IGNORE_DEPRECATIONS
1036 #endif
1037 #endif
1039 ep = g_io_extension_point_register (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
1040 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
1042 ep = g_io_extension_point_register (G_NFS_FILE_MONITOR_EXTENSION_POINT_NAME);
1043 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
1045 ep = g_io_extension_point_register (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
1046 g_io_extension_point_set_required_type (ep, G_TYPE_VOLUME_MONITOR);
1048 ep = g_io_extension_point_register (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
1049 g_io_extension_point_set_required_type (ep, G_TYPE_NATIVE_VOLUME_MONITOR);
1051 ep = g_io_extension_point_register (G_VFS_EXTENSION_POINT_NAME);
1052 g_io_extension_point_set_required_type (ep, G_TYPE_VFS);
1054 ep = g_io_extension_point_register ("gsettings-backend");
1055 g_io_extension_point_set_required_type (ep, G_TYPE_OBJECT);
1057 ep = g_io_extension_point_register (G_PROXY_RESOLVER_EXTENSION_POINT_NAME);
1058 g_io_extension_point_set_required_type (ep, G_TYPE_PROXY_RESOLVER);
1060 ep = g_io_extension_point_register (G_PROXY_EXTENSION_POINT_NAME);
1061 g_io_extension_point_set_required_type (ep, G_TYPE_PROXY);
1063 ep = g_io_extension_point_register (G_TLS_BACKEND_EXTENSION_POINT_NAME);
1064 g_io_extension_point_set_required_type (ep, G_TYPE_TLS_BACKEND);
1066 ep = g_io_extension_point_register (G_NETWORK_MONITOR_EXTENSION_POINT_NAME);
1067 g_io_extension_point_set_required_type (ep, G_TYPE_NETWORK_MONITOR);
1069 ep = g_io_extension_point_register (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME);
1070 g_io_extension_point_set_required_type (ep, G_TYPE_NOTIFICATION_BACKEND);
1073 G_UNLOCK (registered_extensions);
1076 static gchar *
1077 get_gio_module_dir (void)
1079 gchar *module_dir;
1081 module_dir = g_strdup (g_getenv ("GIO_MODULE_DIR"));
1082 if (module_dir == NULL)
1084 #ifdef G_OS_WIN32
1085 gchar *install_dir;
1087 install_dir = g_win32_get_package_installation_directory_of_module (gio_dll);
1088 #ifdef _MSC_VER
1089 /* On Visual Studio builds we have all the libraries and binaries in bin
1090 * so better load the gio modules from bin instead of lib
1092 module_dir = g_build_filename (install_dir,
1093 "bin", "gio", "modules",
1094 NULL);
1095 #else
1096 module_dir = g_build_filename (install_dir,
1097 "lib", "gio", "modules",
1098 NULL);
1099 #endif
1100 g_free (install_dir);
1101 #else
1102 module_dir = g_strdup (GIO_MODULE_DIR);
1103 #endif
1106 return module_dir;
1109 void
1110 _g_io_modules_ensure_loaded (void)
1112 static gboolean loaded_dirs = FALSE;
1113 const char *module_path;
1114 GIOModuleScope *scope;
1116 _g_io_modules_ensure_extension_points_registered ();
1118 G_LOCK (loaded_dirs);
1120 if (!loaded_dirs)
1122 gchar *module_dir;
1124 loaded_dirs = TRUE;
1125 scope = g_io_module_scope_new (G_IO_MODULE_SCOPE_BLOCK_DUPLICATES);
1127 /* First load any overrides, extras */
1128 module_path = g_getenv ("GIO_EXTRA_MODULES");
1129 if (module_path)
1131 gchar **paths;
1132 int i;
1134 paths = g_strsplit (module_path, G_SEARCHPATH_SEPARATOR_S, 0);
1136 for (i = 0; paths[i] != NULL; i++)
1138 g_io_modules_scan_all_in_directory_with_scope (paths[i], scope);
1141 g_strfreev (paths);
1144 /* Then load the compiled in path */
1145 module_dir = get_gio_module_dir ();
1147 g_io_modules_scan_all_in_directory_with_scope (module_dir, scope);
1148 g_free (module_dir);
1150 g_io_module_scope_free (scope);
1152 /* Initialize types from built-in "modules" */
1153 g_type_ensure (g_null_settings_backend_get_type ());
1154 g_type_ensure (g_memory_settings_backend_get_type ());
1155 #if defined(HAVE_INOTIFY_INIT1)
1156 g_type_ensure (g_inotify_file_monitor_get_type ());
1157 #endif
1158 #if defined(HAVE_KQUEUE)
1159 g_type_ensure (g_kqueue_file_monitor_get_type ());
1160 #endif
1161 #if defined(HAVE_FEN)
1162 g_type_ensure (g_fen_file_monitor_get_type ());
1163 #endif
1164 #ifdef G_OS_WIN32
1165 g_type_ensure (_g_win32_volume_monitor_get_type ());
1166 g_type_ensure (g_win32_file_monitor_get_type ());
1167 g_type_ensure (g_registry_backend_get_type ());
1168 #endif
1169 #ifdef HAVE_COCOA
1170 g_type_ensure (g_nextstep_settings_backend_get_type ());
1171 g_type_ensure (g_osx_app_info_get_type ());
1172 #endif
1173 #ifdef G_OS_UNIX
1174 g_type_ensure (_g_unix_volume_monitor_get_type ());
1175 g_type_ensure (g_fdo_notification_backend_get_type ());
1176 g_type_ensure (g_gtk_notification_backend_get_type ());
1177 g_type_ensure (g_portal_notification_backend_get_type ());
1178 g_type_ensure (g_network_monitor_portal_get_type ());
1179 g_type_ensure (g_proxy_resolver_portal_get_type ());
1180 #endif
1181 #if HAVE_MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
1182 g_type_ensure (g_cocoa_notification_backend_get_type ());
1183 #endif
1184 #ifdef G_OS_WIN32
1185 g_type_ensure (g_win32_notification_backend_get_type ());
1186 g_type_ensure (_g_winhttp_vfs_get_type ());
1187 #endif
1188 g_type_ensure (_g_local_vfs_get_type ());
1189 g_type_ensure (_g_dummy_proxy_resolver_get_type ());
1190 g_type_ensure (_g_http_proxy_get_type ());
1191 g_type_ensure (_g_https_proxy_get_type ());
1192 g_type_ensure (_g_socks4a_proxy_get_type ());
1193 g_type_ensure (_g_socks4_proxy_get_type ());
1194 g_type_ensure (_g_socks5_proxy_get_type ());
1195 g_type_ensure (_g_dummy_tls_backend_get_type ());
1196 g_type_ensure (g_network_monitor_base_get_type ());
1197 #ifdef HAVE_NETLINK
1198 g_type_ensure (_g_network_monitor_netlink_get_type ());
1199 g_type_ensure (_g_network_monitor_nm_get_type ());
1200 #endif
1201 #ifdef G_OS_WIN32
1202 g_type_ensure (_g_win32_network_monitor_get_type ());
1203 #endif
1206 G_UNLOCK (loaded_dirs);
1209 static void
1210 g_io_extension_point_free (GIOExtensionPoint *ep)
1212 g_free (ep->name);
1213 g_free (ep);
1217 * g_io_extension_point_register:
1218 * @name: The name of the extension point
1220 * Registers an extension point.
1222 * Returns: (transfer none): the new #GIOExtensionPoint. This object is
1223 * owned by GIO and should not be freed.
1225 GIOExtensionPoint *
1226 g_io_extension_point_register (const char *name)
1228 GIOExtensionPoint *ep;
1230 G_LOCK (extension_points);
1231 if (extension_points == NULL)
1232 extension_points = g_hash_table_new_full (g_str_hash,
1233 g_str_equal,
1234 NULL,
1235 (GDestroyNotify)g_io_extension_point_free);
1237 ep = g_hash_table_lookup (extension_points, name);
1238 if (ep != NULL)
1240 G_UNLOCK (extension_points);
1241 return ep;
1244 ep = g_new0 (GIOExtensionPoint, 1);
1245 ep->name = g_strdup (name);
1247 g_hash_table_insert (extension_points, ep->name, ep);
1249 G_UNLOCK (extension_points);
1251 return ep;
1255 * g_io_extension_point_lookup:
1256 * @name: the name of the extension point
1258 * Looks up an existing extension point.
1260 * Returns: (transfer none): the #GIOExtensionPoint, or %NULL if there
1261 * is no registered extension point with the given name.
1263 GIOExtensionPoint *
1264 g_io_extension_point_lookup (const char *name)
1266 GIOExtensionPoint *ep;
1268 G_LOCK (extension_points);
1269 ep = NULL;
1270 if (extension_points != NULL)
1271 ep = g_hash_table_lookup (extension_points, name);
1273 G_UNLOCK (extension_points);
1275 return ep;
1280 * g_io_extension_point_set_required_type:
1281 * @extension_point: a #GIOExtensionPoint
1282 * @type: the #GType to require
1284 * Sets the required type for @extension_point to @type.
1285 * All implementations must henceforth have this type.
1287 void
1288 g_io_extension_point_set_required_type (GIOExtensionPoint *extension_point,
1289 GType type)
1291 extension_point->required_type = type;
1295 * g_io_extension_point_get_required_type:
1296 * @extension_point: a #GIOExtensionPoint
1298 * Gets the required type for @extension_point.
1300 * Returns: the #GType that all implementations must have,
1301 * or #G_TYPE_INVALID if the extension point has no required type
1303 GType
1304 g_io_extension_point_get_required_type (GIOExtensionPoint *extension_point)
1306 return extension_point->required_type;
1309 static void
1310 lazy_load_modules (GIOExtensionPoint *extension_point)
1312 GIOModule *module;
1313 GList *l;
1315 for (l = extension_point->lazy_load_modules; l != NULL; l = l->next)
1317 module = l->data;
1319 if (!module->initialized)
1321 if (g_type_module_use (G_TYPE_MODULE (module)))
1322 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
1323 else
1324 g_printerr ("Failed to load module: %s\n",
1325 module->filename);
1331 * g_io_extension_point_get_extensions:
1332 * @extension_point: a #GIOExtensionPoint
1334 * Gets a list of all extensions that implement this extension point.
1335 * The list is sorted by priority, beginning with the highest priority.
1337 * Returns: (element-type GIOExtension) (transfer none): a #GList of
1338 * #GIOExtensions. The list is owned by GIO and should not be
1339 * modified.
1341 GList *
1342 g_io_extension_point_get_extensions (GIOExtensionPoint *extension_point)
1344 g_return_val_if_fail (extension_point != NULL, NULL);
1346 lazy_load_modules (extension_point);
1347 return extension_point->extensions;
1351 * g_io_extension_point_get_extension_by_name:
1352 * @extension_point: a #GIOExtensionPoint
1353 * @name: the name of the extension to get
1355 * Finds a #GIOExtension for an extension point by name.
1357 * Returns: (transfer none): the #GIOExtension for @extension_point that has the
1358 * given name, or %NULL if there is no extension with that name
1360 GIOExtension *
1361 g_io_extension_point_get_extension_by_name (GIOExtensionPoint *extension_point,
1362 const char *name)
1364 GList *l;
1366 g_return_val_if_fail (name != NULL, NULL);
1368 lazy_load_modules (extension_point);
1369 for (l = extension_point->extensions; l != NULL; l = l->next)
1371 GIOExtension *e = l->data;
1373 if (e->name != NULL &&
1374 strcmp (e->name, name) == 0)
1375 return e;
1378 return NULL;
1381 static gint
1382 extension_prio_compare (gconstpointer a,
1383 gconstpointer b)
1385 const GIOExtension *extension_a = a, *extension_b = b;
1387 if (extension_a->priority > extension_b->priority)
1388 return -1;
1390 if (extension_b->priority > extension_a->priority)
1391 return 1;
1393 return 0;
1397 * g_io_extension_point_implement:
1398 * @extension_point_name: the name of the extension point
1399 * @type: the #GType to register as extension
1400 * @extension_name: the name for the extension
1401 * @priority: the priority for the extension
1403 * Registers @type as extension for the extension point with name
1404 * @extension_point_name.
1406 * If @type has already been registered as an extension for this
1407 * extension point, the existing #GIOExtension object is returned.
1409 * Returns: (transfer none): a #GIOExtension object for #GType
1411 GIOExtension *
1412 g_io_extension_point_implement (const char *extension_point_name,
1413 GType type,
1414 const char *extension_name,
1415 gint priority)
1417 GIOExtensionPoint *extension_point;
1418 GIOExtension *extension;
1419 GList *l;
1421 g_return_val_if_fail (extension_point_name != NULL, NULL);
1423 extension_point = g_io_extension_point_lookup (extension_point_name);
1424 if (extension_point == NULL)
1426 g_warning ("Tried to implement non-registered extension point %s", extension_point_name);
1427 return NULL;
1430 if (extension_point->required_type != 0 &&
1431 !g_type_is_a (type, extension_point->required_type))
1433 g_warning ("Tried to register an extension of the type %s to extension point %s. "
1434 "Expected type is %s.",
1435 g_type_name (type),
1436 extension_point_name,
1437 g_type_name (extension_point->required_type));
1438 return NULL;
1441 /* It's safe to register the same type multiple times */
1442 for (l = extension_point->extensions; l != NULL; l = l->next)
1444 extension = l->data;
1445 if (extension->type == type)
1446 return extension;
1449 extension = g_slice_new0 (GIOExtension);
1450 extension->type = type;
1451 extension->name = g_strdup (extension_name);
1452 extension->priority = priority;
1454 extension_point->extensions = g_list_insert_sorted (extension_point->extensions,
1455 extension, extension_prio_compare);
1457 return extension;
1461 * g_io_extension_ref_class:
1462 * @extension: a #GIOExtension
1464 * Gets a reference to the class for the type that is
1465 * associated with @extension.
1467 * Returns: (transfer full): the #GTypeClass for the type of @extension
1469 GTypeClass *
1470 g_io_extension_ref_class (GIOExtension *extension)
1472 return g_type_class_ref (extension->type);
1476 * g_io_extension_get_type:
1477 * @extension: a #GIOExtension
1479 * Gets the type associated with @extension.
1481 * Returns: the type of @extension
1483 GType
1484 g_io_extension_get_type (GIOExtension *extension)
1486 return extension->type;
1490 * g_io_extension_get_name:
1491 * @extension: a #GIOExtension
1493 * Gets the name under which @extension was registered.
1495 * Note that the same type may be registered as extension
1496 * for multiple extension points, under different names.
1498 * Returns: the name of @extension.
1500 const char *
1501 g_io_extension_get_name (GIOExtension *extension)
1503 return extension->name;
1507 * g_io_extension_get_priority:
1508 * @extension: a #GIOExtension
1510 * Gets the priority with which @extension was registered.
1512 * Returns: the priority of @extension
1514 gint
1515 g_io_extension_get_priority (GIOExtension *extension)
1517 return extension->priority;