1 /* GIO - GLib Input, Output and Streaming Library
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>
25 #include "giomodule.h"
26 #include "giomodule-priv.h"
27 #include "glocalfilemonitor.h"
28 #include "gnativevolumemonitor.h"
29 #include "gproxyresolver.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"
38 #include "gnotificationbackend.h"
39 #include "ginitable.h"
40 #include "gnetworkmonitor.h"
42 #include "gregistrysettingsbackend.h"
44 #include <glib/gstdio.h>
47 #include "gdesktopappinfo.h"
52 * @short_description: Loadable GIO Modules
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.
62 * SECTION:extensionpoints
63 * @short_description: Extension Points
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
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);
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 (),
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
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
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
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().
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
147 * Returns: (transfer full): the new module scope
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
);
161 * g_io_module_scope_free:
162 * @scope: a module loading scope
164 * Free a module scope.
169 g_io_module_scope_free (GIOModuleScope
*scope
)
173 g_hash_table_destroy (scope
->basenames
);
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().
189 g_io_module_scope_block (GIOModuleScope
*scope
,
190 const gchar
*basename
)
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
);
202 _g_io_module_scope_contains (GIOModuleScope
*scope
,
203 const gchar
*basename
)
205 return g_hash_table_lookup (scope
->basenames
, basename
) ? TRUE
: FALSE
;
209 GTypeModule parent_instance
;
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
);
232 * #GIOExtension is an opaque data structure and can only be accessed
233 * using the following functions.
235 struct _GIOExtension
{
244 * #GIOExtensionPoint is an opaque data structure and can only be accessed
245 * using the following functions.
247 struct _GIOExtensionPoint
{
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
);
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
;
272 g_io_module_init (GIOModule
*module
)
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
);
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");
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 ());
305 /* Make sure that the loaded library contains the required methods */
306 if (! g_module_symbol (module
->library
,
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
);
319 /* Initialize the loaded module */
320 module
->load (module
);
321 module
->initialized
= TRUE
;
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
;
337 module
->unload
= NULL
;
342 * @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,
351 g_io_module_new (const gchar
*filename
)
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
);
364 is_valid_module_name (const gchar
*basename
,
365 GIOModuleScope
*scope
)
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"))
374 if (!g_str_has_suffix (basename
, ".dll"))
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
);
391 * g_io_modules_scan_all_in_directory_with_scope:
392 * @dirname: pathname for a directory containing modules to scan.
393 * @scope: a scope to use when scanning the modules
395 * Scans all the modules in the specified directory, ensuring that
396 * any extension point implemented by a module is registered.
398 * This may not actually load and initialize all the types in each
399 * module, some modules may be lazily loaded and initialized when
400 * an extension point it implementes is used with e.g.
401 * g_io_extension_point_get_extensions() or
402 * g_io_extension_point_get_extension_by_name().
404 * If you need to guarantee that all types are loaded in all the modules,
405 * use g_io_modules_load_all_in_directory().
410 g_io_modules_scan_all_in_directory_with_scope (const char *dirname
,
411 GIOModuleScope
*scope
)
421 if (!g_module_supported ())
424 dir
= g_dir_open (dirname
, 0, NULL
);
428 filename
= g_build_filename (dirname
, "giomodule.cache", NULL
);
430 cache
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
431 g_free
, (GDestroyNotify
)g_strfreev
);
434 if (g_stat (filename
, &statbuf
) == 0 &&
435 g_file_get_contents (filename
, &data
, NULL
, NULL
))
440 /* Cache mtime is the time the cache file was created, any file
441 * that has a ctime before this was created then and not modified
442 * since then (userspace can't change ctime). Its possible to change
443 * the ctime forward without changing the file content, by e.g.
444 * chmoding the file, but this is uncommon and will only cause us
445 * to not use the cache so will not cause bugs.
447 cache_mtime
= statbuf
.st_mtime
;
449 lines
= g_strsplit (data
, "\n", -1);
452 for (i
= 0; lines
[i
] != NULL
; i
++)
454 char *line
= lines
[i
];
457 char **extension_points
;
462 colon
= strchr (line
, ':');
463 if (colon
== NULL
|| line
== colon
)
464 continue; /* Invalid line, ignore */
466 *colon
= 0; /* terminate filename */
467 file
= g_strdup (line
);
468 colon
++; /* after colon */
470 while (g_ascii_isspace (*colon
))
473 extension_points
= g_strsplit (colon
, ",", -1);
474 g_hash_table_insert (cache
, file
, extension_points
);
479 while ((name
= g_dir_read_name (dir
)))
481 if (is_valid_module_name (name
, scope
))
483 GIOExtensionPoint
*extension_point
;
486 char **extension_points
;
489 path
= g_build_filename (dirname
, name
, NULL
);
490 module
= g_io_module_new (path
);
492 extension_points
= g_hash_table_lookup (cache
, name
);
493 if (extension_points
!= NULL
&&
494 g_stat (path
, &statbuf
) == 0 &&
495 statbuf
.st_ctime
<= cache_mtime
)
497 /* Lazy load/init the library when first required */
498 for (i
= 0; extension_points
[i
] != NULL
; i
++)
501 g_io_extension_point_register (extension_points
[i
]);
502 extension_point
->lazy_load_modules
=
503 g_list_prepend (extension_point
->lazy_load_modules
,
509 /* Try to load and init types */
510 if (g_type_module_use (G_TYPE_MODULE (module
)))
511 g_type_module_unuse (G_TYPE_MODULE (module
)); /* Unload */
513 { /* Failure to load */
514 g_printerr ("Failed to load module: %s\n", path
);
515 g_object_unref (module
);
527 g_hash_table_destroy (cache
);
533 * g_io_modules_scan_all_in_directory:
534 * @dirname: pathname for a directory containing modules to scan.
536 * Scans all the modules in the specified directory, ensuring that
537 * any extension point implemented by a module is registered.
539 * This may not actually load and initialize all the types in each
540 * module, some modules may be lazily loaded and initialized when
541 * an extension point it implementes is used with e.g.
542 * g_io_extension_point_get_extensions() or
543 * g_io_extension_point_get_extension_by_name().
545 * If you need to guarantee that all types are loaded in all the modules,
546 * use g_io_modules_load_all_in_directory().
551 g_io_modules_scan_all_in_directory (const char *dirname
)
553 g_io_modules_scan_all_in_directory_with_scope (dirname
, NULL
);
557 * g_io_modules_load_all_in_directory_with_scope:
558 * @dirname: pathname for a directory containing modules to load.
559 * @scope: a scope to use when scanning the modules.
561 * Loads all the modules in the specified directory.
563 * If don't require all modules to be initialized (and thus registering
564 * all gtypes) then you can use g_io_modules_scan_all_in_directory()
565 * which allows delayed/lazy loading of modules.
567 * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
568 * from the directory,
569 * All the modules are loaded into memory, if you want to
570 * unload them (enabling on-demand loading) you must call
571 * g_type_module_unuse() on all the modules. Free the list
572 * with g_list_free().
577 g_io_modules_load_all_in_directory_with_scope (const char *dirname
,
578 GIOModuleScope
*scope
)
584 if (!g_module_supported ())
587 dir
= g_dir_open (dirname
, 0, NULL
);
592 while ((name
= g_dir_read_name (dir
)))
594 if (is_valid_module_name (name
, scope
))
599 path
= g_build_filename (dirname
, name
, NULL
);
600 module
= g_io_module_new (path
);
602 if (!g_type_module_use (G_TYPE_MODULE (module
)))
604 g_printerr ("Failed to load module: %s\n", path
);
605 g_object_unref (module
);
612 modules
= g_list_prepend (modules
, module
);
622 * g_io_modules_load_all_in_directory:
623 * @dirname: pathname for a directory containing modules to load.
625 * Loads all the modules in the specified directory.
627 * If don't require all modules to be initialized (and thus registering
628 * all gtypes) then you can use g_io_modules_scan_all_in_directory()
629 * which allows delayed/lazy loading of modules.
631 * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
632 * from the directory,
633 * All the modules are loaded into memory, if you want to
634 * unload them (enabling on-demand loading) you must call
635 * g_type_module_unuse() on all the modules. Free the list
636 * with g_list_free().
639 g_io_modules_load_all_in_directory (const char *dirname
)
641 return g_io_modules_load_all_in_directory_with_scope (dirname
, NULL
);
645 try_class (GIOExtension
*extension
,
646 guint is_supported_offset
)
648 GType type
= g_io_extension_get_type (extension
);
649 typedef gboolean (*verify_func
) (void);
652 class = g_type_class_ref (type
);
653 if (!is_supported_offset
|| (* G_STRUCT_MEMBER(verify_func
, class, is_supported_offset
)) ())
656 g_type_class_unref (class);
661 * _g_io_module_get_default_type:
662 * @extension_point: the name of an extension point
663 * @envvar: (allow-none): the name of an environment variable to
664 * override the default implementation.
665 * @is_supported_offset: a vtable offset, or zero
667 * Retrieves the default class implementing @extension_point.
669 * If @envvar is not %NULL, and the environment variable with that
670 * name is set, then the implementation it specifies will be tried
671 * first. After that, or if @envvar is not set, all other
672 * implementations will be tried in order of decreasing priority.
674 * If @is_supported_offset is non-zero, then it is the offset into the
675 * class vtable at which there is a function that takes no arguments and
676 * returns a boolean. This function will be called on each candidate
677 * implementation to check if it is actually usable or not.
679 * The result is cached after it is generated the first time, and
680 * the function is thread-safe.
682 * Returns: (transfer none): an object implementing
683 * @extension_point, or %NULL if there are no usable
687 _g_io_module_get_default_type (const gchar
*extension_point
,
689 guint is_supported_offset
)
691 static GRecMutex default_modules_lock
;
692 static GHashTable
*default_modules
;
693 const char *use_this
;
695 GIOExtensionPoint
*ep
;
696 GIOExtension
*extension
, *preferred
;
699 g_rec_mutex_lock (&default_modules_lock
);
704 if (g_hash_table_lookup_extended (default_modules
, extension_point
, &key
, &impl
))
706 g_rec_mutex_unlock (&default_modules_lock
);
707 return impl
? G_OBJECT_CLASS_TYPE (impl
) : G_TYPE_INVALID
;
712 default_modules
= g_hash_table_new (g_str_hash
, g_str_equal
);
715 _g_io_modules_ensure_loaded ();
716 ep
= g_io_extension_point_lookup (extension_point
);
720 g_warn_if_reached ();
721 g_rec_mutex_unlock (&default_modules_lock
);
722 return G_TYPE_INVALID
;
725 use_this
= envvar
? g_getenv (envvar
) : NULL
;
728 preferred
= g_io_extension_point_get_extension_by_name (ep
, use_this
);
731 impl
= try_class (preferred
, is_supported_offset
);
736 g_warning ("Can't find module '%s' specified in %s", use_this
, envvar
);
741 for (l
= g_io_extension_point_get_extensions (ep
); l
!= NULL
; l
= l
->next
)
744 if (extension
== preferred
)
747 impl
= try_class (extension
, is_supported_offset
);
755 g_hash_table_insert (default_modules
, g_strdup (extension_point
), impl
);
756 g_rec_mutex_unlock (&default_modules_lock
);
758 return impl
? G_OBJECT_CLASS_TYPE (impl
) : G_TYPE_INVALID
;
762 try_implementation (GIOExtension
*extension
,
763 GIOModuleVerifyFunc verify_func
)
765 GType type
= g_io_extension_get_type (extension
);
768 if (g_type_is_a (type
, G_TYPE_INITABLE
))
769 return g_initable_new (type
, NULL
, NULL
, NULL
);
772 impl
= g_object_new (type
, NULL
);
773 if (!verify_func
|| verify_func (impl
))
776 g_object_unref (impl
);
782 * _g_io_module_get_default:
783 * @extension_point: the name of an extension point
784 * @envvar: (allow-none): the name of an environment variable to
785 * override the default implementation.
786 * @verify_func: (allow-none): a function to call to verify that
787 * a given implementation is usable in the current environment.
789 * Retrieves the default object implementing @extension_point.
791 * If @envvar is not %NULL, and the environment variable with that
792 * name is set, then the implementation it specifies will be tried
793 * first. After that, or if @envvar is not set, all other
794 * implementations will be tried in order of decreasing priority.
796 * If an extension point implementation implements #GInitable, then
797 * that implementation will only be used if it initializes
798 * successfully. Otherwise, if @verify_func is not %NULL, then it will
799 * be called on each candidate implementation after construction, to
800 * check if it is actually usable or not.
802 * The result is cached after it is generated the first time, and
803 * the function is thread-safe.
805 * Returns: (transfer none): an object implementing
806 * @extension_point, or %NULL if there are no usable
810 _g_io_module_get_default (const gchar
*extension_point
,
812 GIOModuleVerifyFunc verify_func
)
814 static GRecMutex default_modules_lock
;
815 static GHashTable
*default_modules
;
816 const char *use_this
;
818 GIOExtensionPoint
*ep
;
819 GIOExtension
*extension
, *preferred
;
822 g_rec_mutex_lock (&default_modules_lock
);
827 if (g_hash_table_lookup_extended (default_modules
, extension_point
,
830 g_rec_mutex_unlock (&default_modules_lock
);
836 default_modules
= g_hash_table_new (g_str_hash
, g_str_equal
);
839 _g_io_modules_ensure_loaded ();
840 ep
= g_io_extension_point_lookup (extension_point
);
844 g_warn_if_reached ();
845 g_rec_mutex_unlock (&default_modules_lock
);
849 use_this
= envvar
? g_getenv (envvar
) : NULL
;
852 preferred
= g_io_extension_point_get_extension_by_name (ep
, use_this
);
855 impl
= try_implementation (preferred
, verify_func
);
860 g_warning ("Can't find module '%s' specified in %s", use_this
, envvar
);
865 for (l
= g_io_extension_point_get_extensions (ep
); l
!= NULL
; l
= l
->next
)
868 if (extension
== preferred
)
871 impl
= try_implementation (extension
, verify_func
);
879 g_hash_table_insert (default_modules
,
880 g_strdup (extension_point
),
881 impl
? g_object_ref (impl
) : NULL
);
882 g_rec_mutex_unlock (&default_modules_lock
);
887 G_LOCK_DEFINE_STATIC (registered_extensions
);
888 G_LOCK_DEFINE_STATIC (loaded_dirs
);
890 extern GType
g_fen_file_monitor_get_type (void);
891 extern GType
g_inotify_file_monitor_get_type (void);
892 extern GType
g_kqueue_file_monitor_get_type (void);
893 extern GType
g_win32_file_monitor_get_type (void);
895 extern GType
_g_unix_volume_monitor_get_type (void);
896 extern GType
_g_local_vfs_get_type (void);
898 extern GType
_g_win32_volume_monitor_get_type (void);
899 extern GType
_g_winhttp_vfs_get_type (void);
901 extern GType
_g_dummy_proxy_resolver_get_type (void);
902 extern GType
_g_dummy_tls_backend_get_type (void);
903 extern GType
g_network_monitor_base_get_type (void);
905 extern GType
_g_network_monitor_netlink_get_type (void);
906 extern GType
_g_network_monitor_nm_get_type (void);
910 extern GType
g_fdo_notification_backend_get_type (void);
911 extern GType
g_gtk_notification_backend_get_type (void);
915 extern GType
g_cocoa_notification_backend_get_type (void);
918 #ifdef G_PLATFORM_WIN32
922 static HMODULE gio_dll
= NULL
;
926 BOOL WINAPI
DllMain (HINSTANCE hinstDLL
,
931 DllMain (HINSTANCE hinstDLL
,
935 if (fdwReason
== DLL_PROCESS_ATTACH
)
944 _g_io_win32_get_module (void)
947 GetModuleHandleExA (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
,
948 (const char *) _g_io_win32_get_module
,
956 _g_io_modules_ensure_extension_points_registered (void)
958 static gboolean registered_extensions
= FALSE
;
959 GIOExtensionPoint
*ep
;
961 G_LOCK (registered_extensions
);
963 if (!registered_extensions
)
965 registered_extensions
= TRUE
;
968 #if !GLIB_CHECK_VERSION (3, 0, 0)
969 ep
= g_io_extension_point_register (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME
);
970 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
971 g_io_extension_point_set_required_type (ep
, G_TYPE_DESKTOP_APP_INFO_LOOKUP
);
972 G_GNUC_END_IGNORE_DEPRECATIONS
976 ep
= g_io_extension_point_register (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME
);
977 g_io_extension_point_set_required_type (ep
, G_TYPE_LOCAL_FILE_MONITOR
);
979 ep
= g_io_extension_point_register (G_NFS_FILE_MONITOR_EXTENSION_POINT_NAME
);
980 g_io_extension_point_set_required_type (ep
, G_TYPE_LOCAL_FILE_MONITOR
);
982 ep
= g_io_extension_point_register (G_VOLUME_MONITOR_EXTENSION_POINT_NAME
);
983 g_io_extension_point_set_required_type (ep
, G_TYPE_VOLUME_MONITOR
);
985 ep
= g_io_extension_point_register (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME
);
986 g_io_extension_point_set_required_type (ep
, G_TYPE_NATIVE_VOLUME_MONITOR
);
988 ep
= g_io_extension_point_register (G_VFS_EXTENSION_POINT_NAME
);
989 g_io_extension_point_set_required_type (ep
, G_TYPE_VFS
);
991 ep
= g_io_extension_point_register ("gsettings-backend");
992 g_io_extension_point_set_required_type (ep
, G_TYPE_OBJECT
);
994 ep
= g_io_extension_point_register (G_PROXY_RESOLVER_EXTENSION_POINT_NAME
);
995 g_io_extension_point_set_required_type (ep
, G_TYPE_PROXY_RESOLVER
);
997 ep
= g_io_extension_point_register (G_PROXY_EXTENSION_POINT_NAME
);
998 g_io_extension_point_set_required_type (ep
, G_TYPE_PROXY
);
1000 ep
= g_io_extension_point_register (G_TLS_BACKEND_EXTENSION_POINT_NAME
);
1001 g_io_extension_point_set_required_type (ep
, G_TYPE_TLS_BACKEND
);
1003 ep
= g_io_extension_point_register (G_NETWORK_MONITOR_EXTENSION_POINT_NAME
);
1004 g_io_extension_point_set_required_type (ep
, G_TYPE_NETWORK_MONITOR
);
1006 ep
= g_io_extension_point_register (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME
);
1007 g_io_extension_point_set_required_type (ep
, G_TYPE_NOTIFICATION_BACKEND
);
1010 G_UNLOCK (registered_extensions
);
1014 get_gio_module_dir (void)
1018 module_dir
= g_strdup (g_getenv ("GIO_MODULE_DIR"));
1019 if (module_dir
== NULL
)
1024 install_dir
= g_win32_get_package_installation_directory_of_module (gio_dll
);
1026 /* On Visual Studio builds we have all the libraries and binaries in bin
1027 * so better load the gio modules from bin instead of lib
1029 module_dir
= g_build_filename (install_dir
,
1030 "bin", "gio", "modules",
1033 module_dir
= g_build_filename (install_dir
,
1034 "lib", "gio", "modules",
1037 g_free (install_dir
);
1039 module_dir
= g_strdup (GIO_MODULE_DIR
);
1047 _g_io_modules_ensure_loaded (void)
1049 static gboolean loaded_dirs
= FALSE
;
1050 const char *module_path
;
1051 GIOModuleScope
*scope
;
1053 _g_io_modules_ensure_extension_points_registered ();
1055 G_LOCK (loaded_dirs
);
1062 scope
= g_io_module_scope_new (G_IO_MODULE_SCOPE_BLOCK_DUPLICATES
);
1064 /* First load any overrides, extras */
1065 module_path
= g_getenv ("GIO_EXTRA_MODULES");
1071 paths
= g_strsplit (module_path
, G_SEARCHPATH_SEPARATOR_S
, 0);
1073 for (i
= 0; paths
[i
] != NULL
; i
++)
1075 g_io_modules_scan_all_in_directory_with_scope (paths
[i
], scope
);
1081 /* Then load the compiled in path */
1082 module_dir
= get_gio_module_dir ();
1084 g_io_modules_scan_all_in_directory_with_scope (module_dir
, scope
);
1085 g_free (module_dir
);
1087 g_io_module_scope_free (scope
);
1089 /* Initialize types from built-in "modules" */
1090 g_type_ensure (g_null_settings_backend_get_type ());
1091 g_type_ensure (g_memory_settings_backend_get_type ());
1092 #if defined(HAVE_INOTIFY_INIT1)
1093 g_type_ensure (g_inotify_file_monitor_get_type ());
1095 #if defined(HAVE_KQUEUE)
1096 g_type_ensure (g_kqueue_file_monitor_get_type ());
1098 #if defined(HAVE_FEN)
1099 g_type_ensure (g_fen_file_monitor_get_type ());
1102 g_type_ensure (_g_win32_volume_monitor_get_type ());
1103 g_type_ensure (g_win32_file_monitor_get_type ());
1104 g_type_ensure (g_registry_backend_get_type ());
1107 g_nextstep_settings_backend_get_type ();
1110 g_type_ensure (_g_unix_volume_monitor_get_type ());
1111 g_type_ensure (g_fdo_notification_backend_get_type ());
1112 g_type_ensure (g_gtk_notification_backend_get_type ());
1115 g_type_ensure (g_cocoa_notification_backend_get_type ());
1118 g_type_ensure (_g_winhttp_vfs_get_type ());
1120 g_type_ensure (_g_local_vfs_get_type ());
1121 g_type_ensure (_g_dummy_proxy_resolver_get_type ());
1122 g_type_ensure (_g_http_proxy_get_type ());
1123 g_type_ensure (_g_https_proxy_get_type ());
1124 g_type_ensure (_g_socks4a_proxy_get_type ());
1125 g_type_ensure (_g_socks4_proxy_get_type ());
1126 g_type_ensure (_g_socks5_proxy_get_type ());
1127 g_type_ensure (_g_dummy_tls_backend_get_type ());
1128 g_type_ensure (g_network_monitor_base_get_type ());
1130 g_type_ensure (_g_network_monitor_netlink_get_type ());
1131 g_type_ensure (_g_network_monitor_nm_get_type ());
1135 G_UNLOCK (loaded_dirs
);
1139 g_io_extension_point_free (GIOExtensionPoint
*ep
)
1146 * g_io_extension_point_register:
1147 * @name: The name of the extension point
1149 * Registers an extension point.
1151 * Returns: (transfer none): the new #GIOExtensionPoint. This object is
1152 * owned by GIO and should not be freed.
1155 g_io_extension_point_register (const char *name
)
1157 GIOExtensionPoint
*ep
;
1159 G_LOCK (extension_points
);
1160 if (extension_points
== NULL
)
1161 extension_points
= g_hash_table_new_full (g_str_hash
,
1164 (GDestroyNotify
)g_io_extension_point_free
);
1166 ep
= g_hash_table_lookup (extension_points
, name
);
1169 G_UNLOCK (extension_points
);
1173 ep
= g_new0 (GIOExtensionPoint
, 1);
1174 ep
->name
= g_strdup (name
);
1176 g_hash_table_insert (extension_points
, ep
->name
, ep
);
1178 G_UNLOCK (extension_points
);
1184 * g_io_extension_point_lookup:
1185 * @name: the name of the extension point
1187 * Looks up an existing extension point.
1189 * Returns: (transfer none): the #GIOExtensionPoint, or %NULL if there
1190 * is no registered extension point with the given name.
1193 g_io_extension_point_lookup (const char *name
)
1195 GIOExtensionPoint
*ep
;
1197 G_LOCK (extension_points
);
1199 if (extension_points
!= NULL
)
1200 ep
= g_hash_table_lookup (extension_points
, name
);
1202 G_UNLOCK (extension_points
);
1209 * g_io_extension_point_set_required_type:
1210 * @extension_point: a #GIOExtensionPoint
1211 * @type: the #GType to require
1213 * Sets the required type for @extension_point to @type.
1214 * All implementations must henceforth have this type.
1217 g_io_extension_point_set_required_type (GIOExtensionPoint
*extension_point
,
1220 extension_point
->required_type
= type
;
1224 * g_io_extension_point_get_required_type:
1225 * @extension_point: a #GIOExtensionPoint
1227 * Gets the required type for @extension_point.
1229 * Returns: the #GType that all implementations must have,
1230 * or #G_TYPE_INVALID if the extension point has no required type
1233 g_io_extension_point_get_required_type (GIOExtensionPoint
*extension_point
)
1235 return extension_point
->required_type
;
1239 lazy_load_modules (GIOExtensionPoint
*extension_point
)
1244 for (l
= extension_point
->lazy_load_modules
; l
!= NULL
; l
= l
->next
)
1248 if (!module
->initialized
)
1250 if (g_type_module_use (G_TYPE_MODULE (module
)))
1251 g_type_module_unuse (G_TYPE_MODULE (module
)); /* Unload */
1253 g_printerr ("Failed to load module: %s\n",
1260 * g_io_extension_point_get_extensions:
1261 * @extension_point: a #GIOExtensionPoint
1263 * Gets a list of all extensions that implement this extension point.
1264 * The list is sorted by priority, beginning with the highest priority.
1266 * Returns: (element-type GIOExtension) (transfer none): a #GList of
1267 * #GIOExtensions. The list is owned by GIO and should not be
1271 g_io_extension_point_get_extensions (GIOExtensionPoint
*extension_point
)
1273 lazy_load_modules (extension_point
);
1274 return extension_point
->extensions
;
1278 * g_io_extension_point_get_extension_by_name:
1279 * @extension_point: a #GIOExtensionPoint
1280 * @name: the name of the extension to get
1282 * Finds a #GIOExtension for an extension point by name.
1284 * Returns: (transfer none): the #GIOExtension for @extension_point that has the
1285 * given name, or %NULL if there is no extension with that name
1288 g_io_extension_point_get_extension_by_name (GIOExtensionPoint
*extension_point
,
1293 g_return_val_if_fail (name
!= NULL
, NULL
);
1295 lazy_load_modules (extension_point
);
1296 for (l
= extension_point
->extensions
; l
!= NULL
; l
= l
->next
)
1298 GIOExtension
*e
= l
->data
;
1300 if (e
->name
!= NULL
&&
1301 strcmp (e
->name
, name
) == 0)
1309 extension_prio_compare (gconstpointer a
,
1312 const GIOExtension
*extension_a
= a
, *extension_b
= b
;
1314 if (extension_a
->priority
> extension_b
->priority
)
1317 if (extension_b
->priority
> extension_a
->priority
)
1324 * g_io_extension_point_implement:
1325 * @extension_point_name: the name of the extension point
1326 * @type: the #GType to register as extension
1327 * @extension_name: the name for the extension
1328 * @priority: the priority for the extension
1330 * Registers @type as extension for the extension point with name
1331 * @extension_point_name.
1333 * If @type has already been registered as an extension for this
1334 * extension point, the existing #GIOExtension object is returned.
1336 * Returns: (transfer none): a #GIOExtension object for #GType
1339 g_io_extension_point_implement (const char *extension_point_name
,
1341 const char *extension_name
,
1344 GIOExtensionPoint
*extension_point
;
1345 GIOExtension
*extension
;
1348 g_return_val_if_fail (extension_point_name
!= NULL
, NULL
);
1350 extension_point
= g_io_extension_point_lookup (extension_point_name
);
1351 if (extension_point
== NULL
)
1353 g_warning ("Tried to implement non-registered extension point %s", extension_point_name
);
1357 if (extension_point
->required_type
!= 0 &&
1358 !g_type_is_a (type
, extension_point
->required_type
))
1360 g_warning ("Tried to register an extension of the type %s to extension point %s. "
1361 "Expected type is %s.",
1363 extension_point_name
,
1364 g_type_name (extension_point
->required_type
));
1368 /* It's safe to register the same type multiple times */
1369 for (l
= extension_point
->extensions
; l
!= NULL
; l
= l
->next
)
1371 extension
= l
->data
;
1372 if (extension
->type
== type
)
1376 extension
= g_slice_new0 (GIOExtension
);
1377 extension
->type
= type
;
1378 extension
->name
= g_strdup (extension_name
);
1379 extension
->priority
= priority
;
1381 extension_point
->extensions
= g_list_insert_sorted (extension_point
->extensions
,
1382 extension
, extension_prio_compare
);
1388 * g_io_extension_ref_class:
1389 * @extension: a #GIOExtension
1391 * Gets a reference to the class for the type that is
1392 * associated with @extension.
1394 * Returns: (transfer full): the #GTypeClass for the type of @extension
1397 g_io_extension_ref_class (GIOExtension
*extension
)
1399 return g_type_class_ref (extension
->type
);
1403 * g_io_extension_get_type:
1404 * @extension: a #GIOExtension
1406 * Gets the type associated with @extension.
1408 * Returns: the type of @extension
1411 g_io_extension_get_type (GIOExtension
*extension
)
1413 return extension
->type
;
1417 * g_io_extension_get_name:
1418 * @extension: a #GIOExtension
1420 * Gets the name under which @extension was registered.
1422 * Note that the same type may be registered as extension
1423 * for multiple extension points, under different names.
1425 * Returns: the name of @extension.
1428 g_io_extension_get_name (GIOExtension
*extension
)
1430 return extension
->name
;
1434 * g_io_extension_get_priority:
1435 * @extension: a #GIOExtension
1437 * Gets the priority with which @extension was registered.
1439 * Returns: the priority of @extension
1442 g_io_extension_get_priority (GIOExtension
*extension
)
1444 return extension
->priority
;