Add some more cases to the app-id unit tests
[glib.git] / gio / giomodule.c
blob6bb8203c2feaa5479ea8d37e4443146c53ee0956
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 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 #ifdef G_OS_UNIX
47 #include "gdesktopappinfo.h"
48 #endif
50 /**
51 * SECTION:giomodule
52 * @short_description: Loadable GIO Modules
53 * @include: gio/gio.h
55 * Provides an interface and default functions for loading and unloading
56 * modules. This is used internally to make GIO extensible, but can also
57 * be used by others to implement module loading.
59 **/
61 /**
62 * SECTION:extensionpoints
63 * @short_description: Extension Points
64 * @include: gio.h
65 * @see_also: [Extending GIO][extending-gio]
67 * #GIOExtensionPoint provides a mechanism for modules to extend the
68 * functionality of the library or application that loaded it in an
69 * organized fashion.
71 * An extension point is identified by a name, and it may optionally
72 * require that any implementation must be of a certain type (or derived
73 * thereof). Use g_io_extension_point_register() to register an
74 * extension point, and g_io_extension_point_set_required_type() to
75 * set a required type.
77 * A module can implement an extension point by specifying the #GType
78 * that implements the functionality. Additionally, each implementation
79 * of an extension point has a name, and a priority. Use
80 * g_io_extension_point_implement() to implement an extension point.
82 * |[<!-- language="C" -->
83 * GIOExtensionPoint *ep;
85 * // Register an extension point
86 * ep = g_io_extension_point_register ("my-extension-point");
87 * g_io_extension_point_set_required_type (ep, MY_TYPE_EXAMPLE);
88 * ]|
90 * |[<!-- language="C" -->
91 * // Implement an extension point
92 * G_DEFINE_TYPE (MyExampleImpl, my_example_impl, MY_TYPE_EXAMPLE);
93 * g_io_extension_point_implement ("my-extension-point",
94 * my_example_impl_get_type (),
95 * "my-example",
96 * 10);
97 * ]|
99 * It is up to the code that registered the extension point how
100 * it uses the implementations that have been associated with it.
101 * Depending on the use case, it may use all implementations, or
102 * only the one with the highest priority, or pick a specific
103 * one by name.
105 * To avoid opening all modules just to find out what extension
106 * points they implement, GIO makes use of a caching mechanism,
107 * see [gio-querymodules][gio-querymodules].
108 * You are expected to run this command after installing a
109 * GIO module.
111 * The `GIO_EXTRA_MODULES` environment variable can be used to
112 * specify additional directories to automatically load modules
113 * from. This environment variable has the same syntax as the
114 * `PATH`. If two modules have the same base name in different
115 * directories, then the latter one will be ignored. If additional
116 * directories are specified GIO will load modules from the built-in
117 * directory last.
121 * GIOModuleScope:
123 * Represents a scope for loading IO modules. A scope can be used for blocking
124 * duplicate modules, or blocking a module you don't want to load.
126 * The scope can be used with g_io_modules_load_all_in_directory_with_scope()
127 * or g_io_modules_scan_all_in_directory_with_scope().
129 * Since: 2.30
131 struct _GIOModuleScope {
132 GIOModuleScopeFlags flags;
133 GHashTable *basenames;
137 * g_io_module_scope_new:
138 * @flags: flags for the new scope
140 * Create a new scope for loading of IO modules. A scope can be used for
141 * blocking duplicate modules, or blocking a module you don't want to load.
143 * Specify the %G_IO_MODULE_SCOPE_BLOCK_DUPLICATES flag to block modules
144 * which have the same base name as a module that has already been seen
145 * in this scope.
147 * Returns: (transfer full): the new module scope
149 * Since: 2.30
151 GIOModuleScope *
152 g_io_module_scope_new (GIOModuleScopeFlags flags)
154 GIOModuleScope *scope = g_new0 (GIOModuleScope, 1);
155 scope->flags = flags;
156 scope->basenames = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
157 return scope;
161 * g_io_module_scope_free:
162 * @scope: a module loading scope
164 * Free a module scope.
166 * Since: 2.30
168 void
169 g_io_module_scope_free (GIOModuleScope *scope)
171 if (!scope)
172 return;
173 g_hash_table_destroy (scope->basenames);
174 g_free (scope);
178 * g_io_module_scope_block:
179 * @scope: a module loading scope
180 * @basename: the basename to block
182 * Block modules with the given @basename from being loaded when
183 * this scope is used with g_io_modules_scan_all_in_directory_with_scope()
184 * or g_io_modules_load_all_in_directory_with_scope().
186 * Since: 2.30
188 void
189 g_io_module_scope_block (GIOModuleScope *scope,
190 const gchar *basename)
192 gchar *key;
194 g_return_if_fail (scope != NULL);
195 g_return_if_fail (basename != NULL);
197 key = g_strdup (basename);
198 g_hash_table_insert (scope->basenames, key, key);
201 static gboolean
202 _g_io_module_scope_contains (GIOModuleScope *scope,
203 const gchar *basename)
205 return g_hash_table_lookup (scope->basenames, basename) ? TRUE : FALSE;
208 struct _GIOModule {
209 GTypeModule parent_instance;
211 gchar *filename;
212 GModule *library;
213 gboolean initialized; /* The module was loaded at least once */
215 void (* load) (GIOModule *module);
216 void (* unload) (GIOModule *module);
219 struct _GIOModuleClass
221 GTypeModuleClass parent_class;
225 static void g_io_module_finalize (GObject *object);
226 static gboolean g_io_module_load_module (GTypeModule *gmodule);
227 static void g_io_module_unload_module (GTypeModule *gmodule);
230 * GIOExtension:
232 * #GIOExtension is an opaque data structure and can only be accessed
233 * using the following functions.
235 struct _GIOExtension {
236 char *name;
237 GType type;
238 gint priority;
242 * GIOExtensionPoint:
244 * #GIOExtensionPoint is an opaque data structure and can only be accessed
245 * using the following functions.
247 struct _GIOExtensionPoint {
248 GType required_type;
249 char *name;
250 GList *extensions;
251 GList *lazy_load_modules;
254 static GHashTable *extension_points = NULL;
255 G_LOCK_DEFINE_STATIC(extension_points);
257 G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE);
259 static void
260 g_io_module_class_init (GIOModuleClass *class)
262 GObjectClass *object_class = G_OBJECT_CLASS (class);
263 GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
265 object_class->finalize = g_io_module_finalize;
267 type_module_class->load = g_io_module_load_module;
268 type_module_class->unload = g_io_module_unload_module;
271 static void
272 g_io_module_init (GIOModule *module)
276 static void
277 g_io_module_finalize (GObject *object)
279 GIOModule *module = G_IO_MODULE (object);
281 g_free (module->filename);
283 G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
286 static gboolean
287 g_io_module_load_module (GTypeModule *gmodule)
289 GIOModule *module = G_IO_MODULE (gmodule);
291 if (!module->filename)
293 g_warning ("GIOModule path not set");
294 return FALSE;
297 module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
299 if (!module->library)
301 g_printerr ("%s\n", g_module_error ());
302 return FALSE;
305 /* Make sure that the loaded library contains the required methods */
306 if (! g_module_symbol (module->library,
307 "g_io_module_load",
308 (gpointer) &module->load) ||
309 ! g_module_symbol (module->library,
310 "g_io_module_unload",
311 (gpointer) &module->unload))
313 g_printerr ("%s\n", g_module_error ());
314 g_module_close (module->library);
316 return FALSE;
319 /* Initialize the loaded module */
320 module->load (module);
321 module->initialized = TRUE;
323 return TRUE;
326 static void
327 g_io_module_unload_module (GTypeModule *gmodule)
329 GIOModule *module = G_IO_MODULE (gmodule);
331 module->unload (module);
333 g_module_close (module->library);
334 module->library = NULL;
336 module->load = NULL;
337 module->unload = NULL;
341 * g_io_module_new:
342 * @filename: (type filename): filename of the shared library module.
344 * Creates a new GIOModule that will load the specific
345 * shared library when in use.
347 * Returns: a #GIOModule from given @filename,
348 * or %NULL on error.
350 GIOModule *
351 g_io_module_new (const gchar *filename)
353 GIOModule *module;
355 g_return_val_if_fail (filename != NULL, NULL);
357 module = g_object_new (G_IO_TYPE_MODULE, NULL);
358 module->filename = g_strdup (filename);
360 return module;
363 static gboolean
364 is_valid_module_name (const gchar *basename,
365 GIOModuleScope *scope)
367 gboolean result;
369 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
370 if (!g_str_has_prefix (basename, "lib") ||
371 !g_str_has_suffix (basename, ".so"))
372 return FALSE;
373 #else
374 if (!g_str_has_suffix (basename, ".dll"))
375 return FALSE;
376 #endif
378 result = TRUE;
379 if (scope)
381 result = _g_io_module_scope_contains (scope, basename) ? FALSE : TRUE;
382 if (result && (scope->flags & G_IO_MODULE_SCOPE_BLOCK_DUPLICATES))
383 g_io_module_scope_block (scope, basename);
386 return result;
391 * g_io_modules_scan_all_in_directory_with_scope:
392 * @dirname: (type filename): pathname for a directory containing modules
393 * to scan.
394 * @scope: a scope to use when scanning the modules
396 * Scans all the modules in the specified directory, ensuring that
397 * any extension point implemented by a module is registered.
399 * This may not actually load and initialize all the types in each
400 * module, some modules may be lazily loaded and initialized when
401 * an extension point it implementes is used with e.g.
402 * g_io_extension_point_get_extensions() or
403 * g_io_extension_point_get_extension_by_name().
405 * If you need to guarantee that all types are loaded in all the modules,
406 * use g_io_modules_load_all_in_directory().
408 * Since: 2.30
410 void
411 g_io_modules_scan_all_in_directory_with_scope (const char *dirname,
412 GIOModuleScope *scope)
414 const gchar *name;
415 char *filename;
416 GDir *dir;
417 GStatBuf statbuf;
418 char *data;
419 time_t cache_mtime;
420 GHashTable *cache;
422 if (!g_module_supported ())
423 return;
425 dir = g_dir_open (dirname, 0, NULL);
426 if (!dir)
427 return;
429 filename = g_build_filename (dirname, "giomodule.cache", NULL);
431 cache = g_hash_table_new_full (g_str_hash, g_str_equal,
432 g_free, (GDestroyNotify)g_strfreev);
434 cache_mtime = 0;
435 if (g_stat (filename, &statbuf) == 0 &&
436 g_file_get_contents (filename, &data, NULL, NULL))
438 char **lines;
439 int i;
441 /* Cache mtime is the time the cache file was created, any file
442 * that has a ctime before this was created then and not modified
443 * since then (userspace can't change ctime). Its possible to change
444 * the ctime forward without changing the file content, by e.g.
445 * chmoding the file, but this is uncommon and will only cause us
446 * to not use the cache so will not cause bugs.
448 cache_mtime = statbuf.st_mtime;
450 lines = g_strsplit (data, "\n", -1);
451 g_free (data);
453 for (i = 0; lines[i] != NULL; i++)
455 char *line = lines[i];
456 char *file;
457 char *colon;
458 char **extension_points;
460 if (line[0] == '#')
461 continue;
463 colon = strchr (line, ':');
464 if (colon == NULL || line == colon)
465 continue; /* Invalid line, ignore */
467 *colon = 0; /* terminate filename */
468 file = g_strdup (line);
469 colon++; /* after colon */
471 while (g_ascii_isspace (*colon))
472 colon++;
474 extension_points = g_strsplit (colon, ",", -1);
475 g_hash_table_insert (cache, file, extension_points);
477 g_strfreev (lines);
480 while ((name = g_dir_read_name (dir)))
482 if (is_valid_module_name (name, scope))
484 GIOExtensionPoint *extension_point;
485 GIOModule *module;
486 gchar *path;
487 char **extension_points;
488 int i;
490 path = g_build_filename (dirname, name, NULL);
491 module = g_io_module_new (path);
493 extension_points = g_hash_table_lookup (cache, name);
494 if (extension_points != NULL &&
495 g_stat (path, &statbuf) == 0 &&
496 statbuf.st_ctime <= cache_mtime)
498 /* Lazy load/init the library when first required */
499 for (i = 0; extension_points[i] != NULL; i++)
501 extension_point =
502 g_io_extension_point_register (extension_points[i]);
503 extension_point->lazy_load_modules =
504 g_list_prepend (extension_point->lazy_load_modules,
505 module);
508 else
510 /* Try to load and init types */
511 if (g_type_module_use (G_TYPE_MODULE (module)))
512 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
513 else
514 { /* Failure to load */
515 g_printerr ("Failed to load module: %s\n", path);
516 g_object_unref (module);
517 g_free (path);
518 continue;
522 g_free (path);
526 g_dir_close (dir);
528 g_hash_table_destroy (cache);
530 g_free (filename);
534 * g_io_modules_scan_all_in_directory:
535 * @dirname: (type filename): pathname for a directory containing modules
536 * to scan.
538 * Scans all the modules in the specified directory, ensuring that
539 * any extension point implemented by a module is registered.
541 * This may not actually load and initialize all the types in each
542 * module, some modules may be lazily loaded and initialized when
543 * an extension point it implementes is used with e.g.
544 * g_io_extension_point_get_extensions() or
545 * g_io_extension_point_get_extension_by_name().
547 * If you need to guarantee that all types are loaded in all the modules,
548 * use g_io_modules_load_all_in_directory().
550 * Since: 2.24
552 void
553 g_io_modules_scan_all_in_directory (const char *dirname)
555 g_io_modules_scan_all_in_directory_with_scope (dirname, NULL);
559 * g_io_modules_load_all_in_directory_with_scope:
560 * @dirname: (type filename): pathname for a directory containing modules
561 * to load.
562 * @scope: a scope to use when scanning the modules.
564 * Loads all the modules in the specified directory.
566 * If don't require all modules to be initialized (and thus registering
567 * all gtypes) then you can use g_io_modules_scan_all_in_directory()
568 * which allows delayed/lazy loading of modules.
570 * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
571 * from the directory,
572 * All the modules are loaded into memory, if you want to
573 * unload them (enabling on-demand loading) you must call
574 * g_type_module_unuse() on all the modules. Free the list
575 * with g_list_free().
577 * Since: 2.30
579 GList *
580 g_io_modules_load_all_in_directory_with_scope (const char *dirname,
581 GIOModuleScope *scope)
583 const gchar *name;
584 GDir *dir;
585 GList *modules;
587 if (!g_module_supported ())
588 return NULL;
590 dir = g_dir_open (dirname, 0, NULL);
591 if (!dir)
592 return NULL;
594 modules = NULL;
595 while ((name = g_dir_read_name (dir)))
597 if (is_valid_module_name (name, scope))
599 GIOModule *module;
600 gchar *path;
602 path = g_build_filename (dirname, name, NULL);
603 module = g_io_module_new (path);
605 if (!g_type_module_use (G_TYPE_MODULE (module)))
607 g_printerr ("Failed to load module: %s\n", path);
608 g_object_unref (module);
609 g_free (path);
610 continue;
613 g_free (path);
615 modules = g_list_prepend (modules, module);
619 g_dir_close (dir);
621 return modules;
625 * g_io_modules_load_all_in_directory:
626 * @dirname: (type filename): pathname for a directory containing modules
627 * to load.
629 * Loads all the modules in the specified directory.
631 * If don't require all modules to be initialized (and thus registering
632 * all gtypes) then you can use g_io_modules_scan_all_in_directory()
633 * which allows delayed/lazy loading of modules.
635 * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
636 * from the directory,
637 * All the modules are loaded into memory, if you want to
638 * unload them (enabling on-demand loading) you must call
639 * g_type_module_unuse() on all the modules. Free the list
640 * with g_list_free().
642 GList *
643 g_io_modules_load_all_in_directory (const char *dirname)
645 return g_io_modules_load_all_in_directory_with_scope (dirname, NULL);
648 static gpointer
649 try_class (GIOExtension *extension,
650 guint is_supported_offset)
652 GType type = g_io_extension_get_type (extension);
653 typedef gboolean (*verify_func) (void);
654 gpointer class;
656 class = g_type_class_ref (type);
657 if (!is_supported_offset || (* G_STRUCT_MEMBER(verify_func, class, is_supported_offset)) ())
658 return class;
660 g_type_class_unref (class);
661 return NULL;
665 * _g_io_module_get_default_type:
666 * @extension_point: the name of an extension point
667 * @envvar: (nullable): the name of an environment variable to
668 * override the default implementation.
669 * @is_supported_offset: a vtable offset, or zero
671 * Retrieves the default class implementing @extension_point.
673 * If @envvar is not %NULL, and the environment variable with that
674 * name is set, then the implementation it specifies will be tried
675 * first. After that, or if @envvar is not set, all other
676 * implementations will be tried in order of decreasing priority.
678 * If @is_supported_offset is non-zero, then it is the offset into the
679 * class vtable at which there is a function that takes no arguments and
680 * returns a boolean. This function will be called on each candidate
681 * implementation to check if it is actually usable or not.
683 * The result is cached after it is generated the first time, and
684 * the function is thread-safe.
686 * Returns: (transfer none): an object implementing
687 * @extension_point, or %NULL if there are no usable
688 * implementations.
690 GType
691 _g_io_module_get_default_type (const gchar *extension_point,
692 const gchar *envvar,
693 guint is_supported_offset)
695 static GRecMutex default_modules_lock;
696 static GHashTable *default_modules;
697 const char *use_this;
698 GList *l;
699 GIOExtensionPoint *ep;
700 GIOExtension *extension, *preferred;
701 gpointer impl;
703 g_rec_mutex_lock (&default_modules_lock);
704 if (default_modules)
706 gpointer key;
708 if (g_hash_table_lookup_extended (default_modules, extension_point, &key, &impl))
710 g_rec_mutex_unlock (&default_modules_lock);
711 return impl ? G_OBJECT_CLASS_TYPE (impl) : G_TYPE_INVALID;
714 else
716 default_modules = g_hash_table_new (g_str_hash, g_str_equal);
719 _g_io_modules_ensure_loaded ();
720 ep = g_io_extension_point_lookup (extension_point);
722 if (!ep)
724 g_warn_if_reached ();
725 g_rec_mutex_unlock (&default_modules_lock);
726 return G_TYPE_INVALID;
729 use_this = envvar ? g_getenv (envvar) : NULL;
730 if (use_this)
732 preferred = g_io_extension_point_get_extension_by_name (ep, use_this);
733 if (preferred)
735 impl = try_class (preferred, is_supported_offset);
736 if (impl)
737 goto done;
739 else
740 g_warning ("Can't find module '%s' specified in %s", use_this, envvar);
742 else
743 preferred = NULL;
745 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
747 extension = l->data;
748 if (extension == preferred)
749 continue;
751 impl = try_class (extension, is_supported_offset);
752 if (impl)
753 goto done;
756 impl = NULL;
758 done:
759 g_hash_table_insert (default_modules, g_strdup (extension_point), impl);
760 g_rec_mutex_unlock (&default_modules_lock);
762 return impl ? G_OBJECT_CLASS_TYPE (impl) : G_TYPE_INVALID;
765 static gpointer
766 try_implementation (GIOExtension *extension,
767 GIOModuleVerifyFunc verify_func)
769 GType type = g_io_extension_get_type (extension);
770 gpointer impl;
772 if (g_type_is_a (type, G_TYPE_INITABLE))
773 return g_initable_new (type, NULL, NULL, NULL);
774 else
776 impl = g_object_new (type, NULL);
777 if (!verify_func || verify_func (impl))
778 return impl;
780 g_object_unref (impl);
781 return NULL;
786 * _g_io_module_get_default:
787 * @extension_point: the name of an extension point
788 * @envvar: (nullable): the name of an environment variable to
789 * override the default implementation.
790 * @verify_func: (nullable): a function to call to verify that
791 * a given implementation is usable in the current environment.
793 * Retrieves the default object implementing @extension_point.
795 * If @envvar is not %NULL, and the environment variable with that
796 * name is set, then the implementation it specifies will be tried
797 * first. After that, or if @envvar is not set, all other
798 * implementations will be tried in order of decreasing priority.
800 * If an extension point implementation implements #GInitable, then
801 * that implementation will only be used if it initializes
802 * successfully. Otherwise, if @verify_func is not %NULL, then it will
803 * be called on each candidate implementation after construction, to
804 * check if it is actually usable or not.
806 * The result is cached after it is generated the first time, and
807 * the function is thread-safe.
809 * Returns: (transfer none): an object implementing
810 * @extension_point, or %NULL if there are no usable
811 * implementations.
813 gpointer
814 _g_io_module_get_default (const gchar *extension_point,
815 const gchar *envvar,
816 GIOModuleVerifyFunc verify_func)
818 static GRecMutex default_modules_lock;
819 static GHashTable *default_modules;
820 const char *use_this;
821 GList *l;
822 GIOExtensionPoint *ep;
823 GIOExtension *extension, *preferred;
824 gpointer impl;
826 g_rec_mutex_lock (&default_modules_lock);
827 if (default_modules)
829 gpointer key;
831 if (g_hash_table_lookup_extended (default_modules, extension_point,
832 &key, &impl))
834 g_rec_mutex_unlock (&default_modules_lock);
835 return impl;
838 else
840 default_modules = g_hash_table_new (g_str_hash, g_str_equal);
843 _g_io_modules_ensure_loaded ();
844 ep = g_io_extension_point_lookup (extension_point);
846 if (!ep)
848 g_warn_if_reached ();
849 g_rec_mutex_unlock (&default_modules_lock);
850 return NULL;
853 use_this = envvar ? g_getenv (envvar) : NULL;
854 if (use_this)
856 preferred = g_io_extension_point_get_extension_by_name (ep, use_this);
857 if (preferred)
859 impl = try_implementation (preferred, verify_func);
860 if (impl)
861 goto done;
863 else
864 g_warning ("Can't find module '%s' specified in %s", use_this, envvar);
866 else
867 preferred = NULL;
869 for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
871 extension = l->data;
872 if (extension == preferred)
873 continue;
875 impl = try_implementation (extension, verify_func);
876 if (impl)
877 goto done;
880 impl = NULL;
882 done:
883 g_hash_table_insert (default_modules,
884 g_strdup (extension_point),
885 impl ? g_object_ref (impl) : NULL);
886 g_rec_mutex_unlock (&default_modules_lock);
888 return impl;
891 G_LOCK_DEFINE_STATIC (registered_extensions);
892 G_LOCK_DEFINE_STATIC (loaded_dirs);
894 extern GType g_fen_file_monitor_get_type (void);
895 extern GType g_inotify_file_monitor_get_type (void);
896 extern GType g_kqueue_file_monitor_get_type (void);
897 extern GType g_win32_file_monitor_get_type (void);
899 extern GType _g_unix_volume_monitor_get_type (void);
900 extern GType _g_local_vfs_get_type (void);
902 extern GType _g_win32_volume_monitor_get_type (void);
903 extern GType _g_winhttp_vfs_get_type (void);
905 extern GType _g_dummy_proxy_resolver_get_type (void);
906 extern GType _g_dummy_tls_backend_get_type (void);
907 extern GType g_network_monitor_base_get_type (void);
908 #ifdef HAVE_NETLINK
909 extern GType _g_network_monitor_netlink_get_type (void);
910 extern GType _g_network_monitor_nm_get_type (void);
911 #endif
913 #ifdef G_OS_UNIX
914 extern GType g_fdo_notification_backend_get_type (void);
915 extern GType g_gtk_notification_backend_get_type (void);
916 extern GType g_portal_notification_backend_get_type (void);
917 extern GType g_proxy_resolver_portal_get_type (void);
918 extern GType g_network_monitor_portal_get_type (void);
919 #endif
921 #ifdef HAVE_COCOA
922 extern GType g_cocoa_notification_backend_get_type (void);
923 #endif
925 #ifdef G_PLATFORM_WIN32
927 #include <windows.h>
929 static HMODULE gio_dll = NULL;
931 #ifdef DLL_EXPORT
933 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
934 DWORD fdwReason,
935 LPVOID lpvReserved);
937 BOOL WINAPI
938 DllMain (HINSTANCE hinstDLL,
939 DWORD fdwReason,
940 LPVOID lpvReserved)
942 if (fdwReason == DLL_PROCESS_ATTACH)
943 gio_dll = hinstDLL;
945 return TRUE;
948 #endif
950 void *
951 _g_io_win32_get_module (void)
953 if (!gio_dll)
954 GetModuleHandleExA (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
955 (const char *) _g_io_win32_get_module,
956 &gio_dll);
957 return gio_dll;
960 #endif
962 void
963 _g_io_modules_ensure_extension_points_registered (void)
965 static gboolean registered_extensions = FALSE;
966 GIOExtensionPoint *ep;
968 G_LOCK (registered_extensions);
970 if (!registered_extensions)
972 registered_extensions = TRUE;
974 #ifdef G_OS_UNIX
975 #if !GLIB_CHECK_VERSION (3, 0, 0)
976 ep = g_io_extension_point_register (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
977 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
978 g_io_extension_point_set_required_type (ep, G_TYPE_DESKTOP_APP_INFO_LOOKUP);
979 G_GNUC_END_IGNORE_DEPRECATIONS
980 #endif
981 #endif
983 ep = g_io_extension_point_register (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
984 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
986 ep = g_io_extension_point_register (G_NFS_FILE_MONITOR_EXTENSION_POINT_NAME);
987 g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
989 ep = g_io_extension_point_register (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
990 g_io_extension_point_set_required_type (ep, G_TYPE_VOLUME_MONITOR);
992 ep = g_io_extension_point_register (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
993 g_io_extension_point_set_required_type (ep, G_TYPE_NATIVE_VOLUME_MONITOR);
995 ep = g_io_extension_point_register (G_VFS_EXTENSION_POINT_NAME);
996 g_io_extension_point_set_required_type (ep, G_TYPE_VFS);
998 ep = g_io_extension_point_register ("gsettings-backend");
999 g_io_extension_point_set_required_type (ep, G_TYPE_OBJECT);
1001 ep = g_io_extension_point_register (G_PROXY_RESOLVER_EXTENSION_POINT_NAME);
1002 g_io_extension_point_set_required_type (ep, G_TYPE_PROXY_RESOLVER);
1004 ep = g_io_extension_point_register (G_PROXY_EXTENSION_POINT_NAME);
1005 g_io_extension_point_set_required_type (ep, G_TYPE_PROXY);
1007 ep = g_io_extension_point_register (G_TLS_BACKEND_EXTENSION_POINT_NAME);
1008 g_io_extension_point_set_required_type (ep, G_TYPE_TLS_BACKEND);
1010 ep = g_io_extension_point_register (G_NETWORK_MONITOR_EXTENSION_POINT_NAME);
1011 g_io_extension_point_set_required_type (ep, G_TYPE_NETWORK_MONITOR);
1013 ep = g_io_extension_point_register (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME);
1014 g_io_extension_point_set_required_type (ep, G_TYPE_NOTIFICATION_BACKEND);
1017 G_UNLOCK (registered_extensions);
1020 static gchar *
1021 get_gio_module_dir (void)
1023 gchar *module_dir;
1025 module_dir = g_strdup (g_getenv ("GIO_MODULE_DIR"));
1026 if (module_dir == NULL)
1028 #ifdef G_OS_WIN32
1029 gchar *install_dir;
1031 install_dir = g_win32_get_package_installation_directory_of_module (gio_dll);
1032 #ifdef _MSC_VER
1033 /* On Visual Studio builds we have all the libraries and binaries in bin
1034 * so better load the gio modules from bin instead of lib
1036 module_dir = g_build_filename (install_dir,
1037 "bin", "gio", "modules",
1038 NULL);
1039 #else
1040 module_dir = g_build_filename (install_dir,
1041 "lib", "gio", "modules",
1042 NULL);
1043 #endif
1044 g_free (install_dir);
1045 #else
1046 module_dir = g_strdup (GIO_MODULE_DIR);
1047 #endif
1050 return module_dir;
1053 void
1054 _g_io_modules_ensure_loaded (void)
1056 static gboolean loaded_dirs = FALSE;
1057 const char *module_path;
1058 GIOModuleScope *scope;
1060 _g_io_modules_ensure_extension_points_registered ();
1062 G_LOCK (loaded_dirs);
1064 if (!loaded_dirs)
1066 gchar *module_dir;
1068 loaded_dirs = TRUE;
1069 scope = g_io_module_scope_new (G_IO_MODULE_SCOPE_BLOCK_DUPLICATES);
1071 /* First load any overrides, extras */
1072 module_path = g_getenv ("GIO_EXTRA_MODULES");
1073 if (module_path)
1075 gchar **paths;
1076 int i;
1078 paths = g_strsplit (module_path, G_SEARCHPATH_SEPARATOR_S, 0);
1080 for (i = 0; paths[i] != NULL; i++)
1082 g_io_modules_scan_all_in_directory_with_scope (paths[i], scope);
1085 g_strfreev (paths);
1088 /* Then load the compiled in path */
1089 module_dir = get_gio_module_dir ();
1091 g_io_modules_scan_all_in_directory_with_scope (module_dir, scope);
1092 g_free (module_dir);
1094 g_io_module_scope_free (scope);
1096 /* Initialize types from built-in "modules" */
1097 g_type_ensure (g_null_settings_backend_get_type ());
1098 g_type_ensure (g_memory_settings_backend_get_type ());
1099 #if defined(HAVE_INOTIFY_INIT1)
1100 g_type_ensure (g_inotify_file_monitor_get_type ());
1101 #endif
1102 #if defined(HAVE_KQUEUE)
1103 g_type_ensure (g_kqueue_file_monitor_get_type ());
1104 #endif
1105 #if defined(HAVE_FEN)
1106 g_type_ensure (g_fen_file_monitor_get_type ());
1107 #endif
1108 #ifdef G_OS_WIN32
1109 g_type_ensure (_g_win32_volume_monitor_get_type ());
1110 g_type_ensure (g_win32_file_monitor_get_type ());
1111 g_type_ensure (g_registry_backend_get_type ());
1112 #endif
1113 #ifdef HAVE_COCOA
1114 g_nextstep_settings_backend_get_type ();
1115 #endif
1116 #ifdef G_OS_UNIX
1117 g_type_ensure (_g_unix_volume_monitor_get_type ());
1118 g_type_ensure (g_fdo_notification_backend_get_type ());
1119 g_type_ensure (g_gtk_notification_backend_get_type ());
1120 g_type_ensure (g_portal_notification_backend_get_type ());
1121 g_type_ensure (g_network_monitor_portal_get_type ());
1122 g_type_ensure (g_proxy_resolver_portal_get_type ());
1123 #endif
1124 #ifdef HAVE_COCOA
1125 g_type_ensure (g_cocoa_notification_backend_get_type ());
1126 #endif
1127 #ifdef G_OS_WIN32
1128 g_type_ensure (_g_winhttp_vfs_get_type ());
1129 #endif
1130 g_type_ensure (_g_local_vfs_get_type ());
1131 g_type_ensure (_g_dummy_proxy_resolver_get_type ());
1132 g_type_ensure (_g_http_proxy_get_type ());
1133 g_type_ensure (_g_https_proxy_get_type ());
1134 g_type_ensure (_g_socks4a_proxy_get_type ());
1135 g_type_ensure (_g_socks4_proxy_get_type ());
1136 g_type_ensure (_g_socks5_proxy_get_type ());
1137 g_type_ensure (_g_dummy_tls_backend_get_type ());
1138 g_type_ensure (g_network_monitor_base_get_type ());
1139 #ifdef HAVE_NETLINK
1140 g_type_ensure (_g_network_monitor_netlink_get_type ());
1141 g_type_ensure (_g_network_monitor_nm_get_type ());
1142 #endif
1145 G_UNLOCK (loaded_dirs);
1148 static void
1149 g_io_extension_point_free (GIOExtensionPoint *ep)
1151 g_free (ep->name);
1152 g_free (ep);
1156 * g_io_extension_point_register:
1157 * @name: The name of the extension point
1159 * Registers an extension point.
1161 * Returns: (transfer none): the new #GIOExtensionPoint. This object is
1162 * owned by GIO and should not be freed.
1164 GIOExtensionPoint *
1165 g_io_extension_point_register (const char *name)
1167 GIOExtensionPoint *ep;
1169 G_LOCK (extension_points);
1170 if (extension_points == NULL)
1171 extension_points = g_hash_table_new_full (g_str_hash,
1172 g_str_equal,
1173 NULL,
1174 (GDestroyNotify)g_io_extension_point_free);
1176 ep = g_hash_table_lookup (extension_points, name);
1177 if (ep != NULL)
1179 G_UNLOCK (extension_points);
1180 return ep;
1183 ep = g_new0 (GIOExtensionPoint, 1);
1184 ep->name = g_strdup (name);
1186 g_hash_table_insert (extension_points, ep->name, ep);
1188 G_UNLOCK (extension_points);
1190 return ep;
1194 * g_io_extension_point_lookup:
1195 * @name: the name of the extension point
1197 * Looks up an existing extension point.
1199 * Returns: (transfer none): the #GIOExtensionPoint, or %NULL if there
1200 * is no registered extension point with the given name.
1202 GIOExtensionPoint *
1203 g_io_extension_point_lookup (const char *name)
1205 GIOExtensionPoint *ep;
1207 G_LOCK (extension_points);
1208 ep = NULL;
1209 if (extension_points != NULL)
1210 ep = g_hash_table_lookup (extension_points, name);
1212 G_UNLOCK (extension_points);
1214 return ep;
1219 * g_io_extension_point_set_required_type:
1220 * @extension_point: a #GIOExtensionPoint
1221 * @type: the #GType to require
1223 * Sets the required type for @extension_point to @type.
1224 * All implementations must henceforth have this type.
1226 void
1227 g_io_extension_point_set_required_type (GIOExtensionPoint *extension_point,
1228 GType type)
1230 extension_point->required_type = type;
1234 * g_io_extension_point_get_required_type:
1235 * @extension_point: a #GIOExtensionPoint
1237 * Gets the required type for @extension_point.
1239 * Returns: the #GType that all implementations must have,
1240 * or #G_TYPE_INVALID if the extension point has no required type
1242 GType
1243 g_io_extension_point_get_required_type (GIOExtensionPoint *extension_point)
1245 return extension_point->required_type;
1248 static void
1249 lazy_load_modules (GIOExtensionPoint *extension_point)
1251 GIOModule *module;
1252 GList *l;
1254 for (l = extension_point->lazy_load_modules; l != NULL; l = l->next)
1256 module = l->data;
1258 if (!module->initialized)
1260 if (g_type_module_use (G_TYPE_MODULE (module)))
1261 g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
1262 else
1263 g_printerr ("Failed to load module: %s\n",
1264 module->filename);
1270 * g_io_extension_point_get_extensions:
1271 * @extension_point: a #GIOExtensionPoint
1273 * Gets a list of all extensions that implement this extension point.
1274 * The list is sorted by priority, beginning with the highest priority.
1276 * Returns: (element-type GIOExtension) (transfer none): a #GList of
1277 * #GIOExtensions. The list is owned by GIO and should not be
1278 * modified.
1280 GList *
1281 g_io_extension_point_get_extensions (GIOExtensionPoint *extension_point)
1283 g_return_val_if_fail (extension_point != NULL, NULL);
1285 lazy_load_modules (extension_point);
1286 return extension_point->extensions;
1290 * g_io_extension_point_get_extension_by_name:
1291 * @extension_point: a #GIOExtensionPoint
1292 * @name: the name of the extension to get
1294 * Finds a #GIOExtension for an extension point by name.
1296 * Returns: (transfer none): the #GIOExtension for @extension_point that has the
1297 * given name, or %NULL if there is no extension with that name
1299 GIOExtension *
1300 g_io_extension_point_get_extension_by_name (GIOExtensionPoint *extension_point,
1301 const char *name)
1303 GList *l;
1305 g_return_val_if_fail (name != NULL, NULL);
1307 lazy_load_modules (extension_point);
1308 for (l = extension_point->extensions; l != NULL; l = l->next)
1310 GIOExtension *e = l->data;
1312 if (e->name != NULL &&
1313 strcmp (e->name, name) == 0)
1314 return e;
1317 return NULL;
1320 static gint
1321 extension_prio_compare (gconstpointer a,
1322 gconstpointer b)
1324 const GIOExtension *extension_a = a, *extension_b = b;
1326 if (extension_a->priority > extension_b->priority)
1327 return -1;
1329 if (extension_b->priority > extension_a->priority)
1330 return 1;
1332 return 0;
1336 * g_io_extension_point_implement:
1337 * @extension_point_name: the name of the extension point
1338 * @type: the #GType to register as extension
1339 * @extension_name: the name for the extension
1340 * @priority: the priority for the extension
1342 * Registers @type as extension for the extension point with name
1343 * @extension_point_name.
1345 * If @type has already been registered as an extension for this
1346 * extension point, the existing #GIOExtension object is returned.
1348 * Returns: (transfer none): a #GIOExtension object for #GType
1350 GIOExtension *
1351 g_io_extension_point_implement (const char *extension_point_name,
1352 GType type,
1353 const char *extension_name,
1354 gint priority)
1356 GIOExtensionPoint *extension_point;
1357 GIOExtension *extension;
1358 GList *l;
1360 g_return_val_if_fail (extension_point_name != NULL, NULL);
1362 extension_point = g_io_extension_point_lookup (extension_point_name);
1363 if (extension_point == NULL)
1365 g_warning ("Tried to implement non-registered extension point %s", extension_point_name);
1366 return NULL;
1369 if (extension_point->required_type != 0 &&
1370 !g_type_is_a (type, extension_point->required_type))
1372 g_warning ("Tried to register an extension of the type %s to extension point %s. "
1373 "Expected type is %s.",
1374 g_type_name (type),
1375 extension_point_name,
1376 g_type_name (extension_point->required_type));
1377 return NULL;
1380 /* It's safe to register the same type multiple times */
1381 for (l = extension_point->extensions; l != NULL; l = l->next)
1383 extension = l->data;
1384 if (extension->type == type)
1385 return extension;
1388 extension = g_slice_new0 (GIOExtension);
1389 extension->type = type;
1390 extension->name = g_strdup (extension_name);
1391 extension->priority = priority;
1393 extension_point->extensions = g_list_insert_sorted (extension_point->extensions,
1394 extension, extension_prio_compare);
1396 return extension;
1400 * g_io_extension_ref_class:
1401 * @extension: a #GIOExtension
1403 * Gets a reference to the class for the type that is
1404 * associated with @extension.
1406 * Returns: (transfer full): the #GTypeClass for the type of @extension
1408 GTypeClass *
1409 g_io_extension_ref_class (GIOExtension *extension)
1411 return g_type_class_ref (extension->type);
1415 * g_io_extension_get_type:
1416 * @extension: a #GIOExtension
1418 * Gets the type associated with @extension.
1420 * Returns: the type of @extension
1422 GType
1423 g_io_extension_get_type (GIOExtension *extension)
1425 return extension->type;
1429 * g_io_extension_get_name:
1430 * @extension: a #GIOExtension
1432 * Gets the name under which @extension was registered.
1434 * Note that the same type may be registered as extension
1435 * for multiple extension points, under different names.
1437 * Returns: the name of @extension.
1439 const char *
1440 g_io_extension_get_name (GIOExtension *extension)
1442 return extension->name;
1446 * g_io_extension_get_priority:
1447 * @extension: a #GIOExtension
1449 * Gets the priority with which @extension was registered.
1451 * Returns: the priority of @extension
1453 gint
1454 g_io_extension_get_priority (GIOExtension *extension)
1456 return extension->priority;