• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: Alexander Larsson <alexl@redhat.com>
19  */
20 
21 #include "config.h"
22 
23 /* For the #GDesktopAppInfoLookup macros; since macro deprecation is implemented
24  * in the preprocessor, we need to define this before including glib.h*/
25 #define GLIB_DISABLE_DEPRECATION_WARNINGS
26 
27 #include <string.h>
28 
29 #include "giomodule.h"
30 #include "giomodule-priv.h"
31 #include "glocalfilemonitor.h"
32 #include "gnativevolumemonitor.h"
33 #include "gproxyresolver.h"
34 #include "gproxy.h"
35 #include "gsettingsbackendinternal.h"
36 #include "ghttpproxy.h"
37 #include "gsocks4proxy.h"
38 #include "gsocks4aproxy.h"
39 #include "gsocks5proxy.h"
40 #include "gtlsbackend.h"
41 #include "gvfs.h"
42 #include "gnotificationbackend.h"
43 #include "ginitable.h"
44 #include "gnetworkmonitor.h"
45 #ifdef G_OS_WIN32
46 #include "gregistrysettingsbackend.h"
47 #endif
48 #include <glib/gstdio.h>
49 
50 #if defined(G_OS_UNIX) && !defined(HAVE_COCOA)
51 #include "gdesktopappinfo.h"
52 #endif
53 #ifdef HAVE_COCOA
54 #include "gosxappinfo.h"
55 #endif
56 
57 #ifdef HAVE_COCOA
58 #include <AvailabilityMacros.h>
59 #endif
60 
61 /**
62  * SECTION:giomodule
63  * @short_description: Loadable GIO Modules
64  * @include: gio/gio.h
65  *
66  * Provides an interface and default functions for loading and unloading
67  * modules. This is used internally to make GIO extensible, but can also
68  * be used by others to implement module loading.
69  *
70  **/
71 
72 /**
73  * SECTION:extensionpoints
74  * @short_description: Extension Points
75  * @include: gio.h
76  * @see_also: [Extending GIO][extending-gio]
77  *
78  * #GIOExtensionPoint provides a mechanism for modules to extend the
79  * functionality of the library or application that loaded it in an
80  * organized fashion.
81  *
82  * An extension point is identified by a name, and it may optionally
83  * require that any implementation must be of a certain type (or derived
84  * thereof). Use g_io_extension_point_register() to register an
85  * extension point, and g_io_extension_point_set_required_type() to
86  * set a required type.
87  *
88  * A module can implement an extension point by specifying the #GType
89  * that implements the functionality. Additionally, each implementation
90  * of an extension point has a name, and a priority. Use
91  * g_io_extension_point_implement() to implement an extension point.
92  *
93  *  |[<!-- language="C" -->
94  *  GIOExtensionPoint *ep;
95  *
96  *  // Register an extension point
97  *  ep = g_io_extension_point_register ("my-extension-point");
98  *  g_io_extension_point_set_required_type (ep, MY_TYPE_EXAMPLE);
99  *  ]|
100  *
101  *  |[<!-- language="C" -->
102  *  // Implement an extension point
103  *  G_DEFINE_TYPE (MyExampleImpl, my_example_impl, MY_TYPE_EXAMPLE)
104  *  g_io_extension_point_implement ("my-extension-point",
105  *                                  my_example_impl_get_type (),
106  *                                  "my-example",
107  *                                  10);
108  *  ]|
109  *
110  *  It is up to the code that registered the extension point how
111  *  it uses the implementations that have been associated with it.
112  *  Depending on the use case, it may use all implementations, or
113  *  only the one with the highest priority, or pick a specific
114  *  one by name.
115  *
116  *  To avoid opening all modules just to find out what extension
117  *  points they implement, GIO makes use of a caching mechanism,
118  *  see [gio-querymodules][gio-querymodules].
119  *  You are expected to run this command after installing a
120  *  GIO module.
121  *
122  *  The `GIO_EXTRA_MODULES` environment variable can be used to
123  *  specify additional directories to automatically load modules
124  *  from. This environment variable has the same syntax as the
125  *  `PATH`. If two modules have the same base name in different
126  *  directories, then the latter one will be ignored. If additional
127  *  directories are specified GIO will load modules from the built-in
128  *  directory last.
129  */
130 
131 /**
132  * GIOModuleScope:
133  *
134  * Represents a scope for loading IO modules. A scope can be used for blocking
135  * duplicate modules, or blocking a module you don't want to load.
136  *
137  * The scope can be used with g_io_modules_load_all_in_directory_with_scope()
138  * or g_io_modules_scan_all_in_directory_with_scope().
139  *
140  * Since: 2.30
141  */
142 struct _GIOModuleScope {
143   GIOModuleScopeFlags flags;
144   GHashTable *basenames;
145 };
146 
147 /**
148  * g_io_module_scope_new:
149  * @flags: flags for the new scope
150  *
151  * Create a new scope for loading of IO modules. A scope can be used for
152  * blocking duplicate modules, or blocking a module you don't want to load.
153  *
154  * Specify the %G_IO_MODULE_SCOPE_BLOCK_DUPLICATES flag to block modules
155  * which have the same base name as a module that has already been seen
156  * in this scope.
157  *
158  * Returns: (transfer full): the new module scope
159  *
160  * Since: 2.30
161  */
162 GIOModuleScope *
g_io_module_scope_new(GIOModuleScopeFlags flags)163 g_io_module_scope_new (GIOModuleScopeFlags flags)
164 {
165   GIOModuleScope *scope = g_new0 (GIOModuleScope, 1);
166   scope->flags = flags;
167   scope->basenames = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
168   return scope;
169 }
170 
171 /**
172  * g_io_module_scope_free:
173  * @scope: a module loading scope
174  *
175  * Free a module scope.
176  *
177  * Since: 2.30
178  */
179 void
g_io_module_scope_free(GIOModuleScope * scope)180 g_io_module_scope_free (GIOModuleScope *scope)
181 {
182   if (!scope)
183     return;
184   g_hash_table_destroy (scope->basenames);
185   g_free (scope);
186 }
187 
188 /**
189  * g_io_module_scope_block:
190  * @scope: a module loading scope
191  * @basename: the basename to block
192  *
193  * Block modules with the given @basename from being loaded when
194  * this scope is used with g_io_modules_scan_all_in_directory_with_scope()
195  * or g_io_modules_load_all_in_directory_with_scope().
196  *
197  * Since: 2.30
198  */
199 void
g_io_module_scope_block(GIOModuleScope * scope,const gchar * basename)200 g_io_module_scope_block (GIOModuleScope *scope,
201                          const gchar    *basename)
202 {
203   gchar *key;
204 
205   g_return_if_fail (scope != NULL);
206   g_return_if_fail (basename != NULL);
207 
208   key = g_strdup (basename);
209   g_hash_table_add (scope->basenames, key);
210 }
211 
212 static gboolean
_g_io_module_scope_contains(GIOModuleScope * scope,const gchar * basename)213 _g_io_module_scope_contains (GIOModuleScope *scope,
214                              const gchar    *basename)
215 {
216   return g_hash_table_contains (scope->basenames, basename);
217 }
218 
219 struct _GIOModule {
220   GTypeModule parent_instance;
221 
222   gchar       *filename;
223   GModule     *library;
224   gboolean     initialized; /* The module was loaded at least once */
225 
226   void (* load)   (GIOModule *module);
227   void (* unload) (GIOModule *module);
228 };
229 
230 struct _GIOModuleClass
231 {
232   GTypeModuleClass parent_class;
233 
234 };
235 
236 static void      g_io_module_finalize      (GObject      *object);
237 static gboolean  g_io_module_load_module   (GTypeModule  *gmodule);
238 static void      g_io_module_unload_module (GTypeModule  *gmodule);
239 
240 /**
241  * GIOExtension:
242  *
243  * #GIOExtension is an opaque data structure and can only be accessed
244  * using the following functions.
245  */
246 struct _GIOExtension {
247   char *name;
248   GType type;
249   gint priority;
250 };
251 
252 /**
253  * GIOExtensionPoint:
254  *
255  * #GIOExtensionPoint is an opaque data structure and can only be accessed
256  * using the following functions.
257  */
258 struct _GIOExtensionPoint {
259   GType required_type;
260   char *name;
261   GList *extensions;
262   GList *lazy_load_modules;
263 };
264 
265 static GHashTable *extension_points = NULL;
266 G_LOCK_DEFINE_STATIC(extension_points);
267 
G_DEFINE_TYPE(GIOModule,g_io_module,G_TYPE_TYPE_MODULE)268 G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE)
269 
270 static void
271 g_io_module_class_init (GIOModuleClass *class)
272 {
273   GObjectClass     *object_class      = G_OBJECT_CLASS (class);
274   GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
275 
276   object_class->finalize     = g_io_module_finalize;
277 
278   type_module_class->load    = g_io_module_load_module;
279   type_module_class->unload  = g_io_module_unload_module;
280 }
281 
282 static void
g_io_module_init(GIOModule * module)283 g_io_module_init (GIOModule *module)
284 {
285 }
286 
287 static void
g_io_module_finalize(GObject * object)288 g_io_module_finalize (GObject *object)
289 {
290   GIOModule *module = G_IO_MODULE (object);
291 
292   g_free (module->filename);
293 
294   G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
295 }
296 
297 static gboolean
load_symbols(GIOModule * module)298 load_symbols (GIOModule *module)
299 {
300   gchar *name;
301   gchar *load_symname;
302   gchar *unload_symname;
303   gboolean ret;
304 
305   name = _g_io_module_extract_name (module->filename);
306   load_symname = g_strconcat ("g_io_", name, "_load", NULL);
307   unload_symname = g_strconcat ("g_io_", name, "_unload", NULL);
308 
309   ret = g_module_symbol (module->library,
310                          load_symname,
311                          (gpointer) &module->load) &&
312         g_module_symbol (module->library,
313                          unload_symname,
314                          (gpointer) &module->unload);
315 
316   if (!ret)
317     {
318       /* Fallback to old names */
319       ret = g_module_symbol (module->library,
320                              "g_io_module_load",
321                              (gpointer) &module->load) &&
322             g_module_symbol (module->library,
323                              "g_io_module_unload",
324                              (gpointer) &module->unload);
325     }
326 
327   g_free (name);
328   g_free (load_symname);
329   g_free (unload_symname);
330 
331   return ret;
332 }
333 
334 static gboolean
g_io_module_load_module(GTypeModule * gmodule)335 g_io_module_load_module (GTypeModule *gmodule)
336 {
337   GIOModule *module = G_IO_MODULE (gmodule);
338 
339   if (!module->filename)
340     {
341       g_warning ("GIOModule path not set");
342       return FALSE;
343     }
344 
345   module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
346 
347   if (!module->library)
348     {
349       g_printerr ("%s\n", g_module_error ());
350       return FALSE;
351     }
352 
353   /* Make sure that the loaded library contains the required methods */
354   if (!load_symbols (module))
355     {
356       g_printerr ("%s\n", g_module_error ());
357       g_module_close (module->library);
358 
359       return FALSE;
360     }
361 
362   /* Initialize the loaded module */
363   module->load (module);
364   module->initialized = TRUE;
365 
366   return TRUE;
367 }
368 
369 static void
g_io_module_unload_module(GTypeModule * gmodule)370 g_io_module_unload_module (GTypeModule *gmodule)
371 {
372   GIOModule *module = G_IO_MODULE (gmodule);
373 
374   module->unload (module);
375 
376   g_module_close (module->library);
377   module->library = NULL;
378 
379   module->load   = NULL;
380   module->unload = NULL;
381 }
382 
383 /**
384  * g_io_module_new:
385  * @filename: (type filename): filename of the shared library module.
386  *
387  * Creates a new GIOModule that will load the specific
388  * shared library when in use.
389  *
390  * Returns: a #GIOModule from given @filename,
391  * or %NULL on error.
392  **/
393 GIOModule *
g_io_module_new(const gchar * filename)394 g_io_module_new (const gchar *filename)
395 {
396   GIOModule *module;
397 
398   g_return_val_if_fail (filename != NULL, NULL);
399 
400   module = g_object_new (G_IO_TYPE_MODULE, NULL);
401   module->filename = g_strdup (filename);
402 
403   return module;
404 }
405 
406 static gboolean
is_valid_module_name(const gchar * basename,GIOModuleScope * scope)407 is_valid_module_name (const gchar        *basename,
408                       GIOModuleScope     *scope)
409 {
410   gboolean result;
411 
412 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
413   if (!g_str_has_prefix (basename, "lib") ||
414       !g_str_has_suffix (basename, ".so"))
415     return FALSE;
416 #else
417   if (!g_str_has_suffix (basename, ".dll"))
418     return FALSE;
419 #endif
420 
421   result = TRUE;
422   if (scope)
423     {
424       result = _g_io_module_scope_contains (scope, basename) ? FALSE : TRUE;
425       if (result && (scope->flags & G_IO_MODULE_SCOPE_BLOCK_DUPLICATES))
426         g_io_module_scope_block (scope, basename);
427     }
428 
429   return result;
430 }
431 
432 
433 /**
434  * g_io_modules_scan_all_in_directory_with_scope:
435  * @dirname: (type filename): pathname for a directory containing modules
436  *     to scan.
437  * @scope: a scope to use when scanning the modules
438  *
439  * Scans all the modules in the specified directory, ensuring that
440  * any extension point implemented by a module is registered.
441  *
442  * This may not actually load and initialize all the types in each
443  * module, some modules may be lazily loaded and initialized when
444  * an extension point it implementes is used with e.g.
445  * g_io_extension_point_get_extensions() or
446  * g_io_extension_point_get_extension_by_name().
447  *
448  * If you need to guarantee that all types are loaded in all the modules,
449  * use g_io_modules_load_all_in_directory().
450  *
451  * Since: 2.30
452  **/
453 void
g_io_modules_scan_all_in_directory_with_scope(const char * dirname,GIOModuleScope * scope)454 g_io_modules_scan_all_in_directory_with_scope (const char     *dirname,
455                                                GIOModuleScope *scope)
456 {
457   const gchar *name;
458   char *filename;
459   GDir *dir;
460   GStatBuf statbuf;
461   char *data;
462   time_t cache_mtime;
463   GHashTable *cache;
464 
465   if (!g_module_supported ())
466     return;
467 
468   dir = g_dir_open (dirname, 0, NULL);
469   if (!dir)
470     return;
471 
472   filename = g_build_filename (dirname, "giomodule.cache", NULL);
473 
474   cache = g_hash_table_new_full (g_str_hash, g_str_equal,
475 				 g_free, (GDestroyNotify)g_strfreev);
476 
477   cache_mtime = 0;
478   if (g_stat (filename, &statbuf) == 0 &&
479       g_file_get_contents (filename, &data, NULL, NULL))
480     {
481       char **lines;
482       int i;
483 
484       /* Cache mtime is the time the cache file was created, any file
485        * that has a ctime before this was created then and not modified
486        * since then (userspace can't change ctime). Its possible to change
487        * the ctime forward without changing the file content, by e.g.
488        * chmoding the file, but this is uncommon and will only cause us
489        * to not use the cache so will not cause bugs.
490        */
491       cache_mtime = statbuf.st_mtime;
492 
493       lines = g_strsplit (data, "\n", -1);
494       g_free (data);
495 
496       for (i = 0;  lines[i] != NULL; i++)
497 	{
498 	  char *line = lines[i];
499 	  char *file;
500 	  char *colon;
501 	  char **extension_points;
502 
503 	  if (line[0] == '#')
504 	    continue;
505 
506 	  colon = strchr (line, ':');
507 	  if (colon == NULL || line == colon)
508 	    continue; /* Invalid line, ignore */
509 
510 	  *colon = 0; /* terminate filename */
511 	  file = g_strdup (line);
512 	  colon++; /* after colon */
513 
514 	  while (g_ascii_isspace (*colon))
515 	    colon++;
516 
517 	  extension_points = g_strsplit (colon, ",", -1);
518 	  g_hash_table_insert (cache, file, extension_points);
519 	}
520       g_strfreev (lines);
521     }
522 
523   while ((name = g_dir_read_name (dir)))
524     {
525       if (is_valid_module_name (name, scope))
526 	{
527 	  GIOExtensionPoint *extension_point;
528 	  GIOModule *module;
529 	  gchar *path;
530 	  char **extension_points;
531 	  int i;
532 
533 	  path = g_build_filename (dirname, name, NULL);
534 	  module = g_io_module_new (path);
535 
536 	  extension_points = g_hash_table_lookup (cache, name);
537 	  if (extension_points != NULL &&
538 	      g_stat (path, &statbuf) == 0 &&
539 	      statbuf.st_ctime <= cache_mtime)
540 	    {
541 	      /* Lazy load/init the library when first required */
542 	      for (i = 0; extension_points[i] != NULL; i++)
543 		{
544 		  extension_point =
545 		    g_io_extension_point_register (extension_points[i]);
546 		  extension_point->lazy_load_modules =
547 		    g_list_prepend (extension_point->lazy_load_modules,
548 				    module);
549 		}
550 	    }
551 	  else
552 	    {
553 	      /* Try to load and init types */
554 	      if (g_type_module_use (G_TYPE_MODULE (module)))
555 		g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
556 	      else
557 		{ /* Failure to load */
558 		  g_printerr ("Failed to load module: %s\n", path);
559 		  g_object_unref (module);
560 		  g_free (path);
561 		  continue;
562 		}
563 	    }
564 
565 	  g_free (path);
566 	}
567     }
568 
569   g_dir_close (dir);
570 
571   g_hash_table_destroy (cache);
572 
573   g_free (filename);
574 }
575 
576 /**
577  * g_io_modules_scan_all_in_directory:
578  * @dirname: (type filename): pathname for a directory containing modules
579  *     to scan.
580  *
581  * Scans all the modules in the specified directory, ensuring that
582  * any extension point implemented by a module is registered.
583  *
584  * This may not actually load and initialize all the types in each
585  * module, some modules may be lazily loaded and initialized when
586  * an extension point it implementes is used with e.g.
587  * g_io_extension_point_get_extensions() or
588  * g_io_extension_point_get_extension_by_name().
589  *
590  * If you need to guarantee that all types are loaded in all the modules,
591  * use g_io_modules_load_all_in_directory().
592  *
593  * Since: 2.24
594  **/
595 void
g_io_modules_scan_all_in_directory(const char * dirname)596 g_io_modules_scan_all_in_directory (const char *dirname)
597 {
598   g_io_modules_scan_all_in_directory_with_scope (dirname, NULL);
599 }
600 
601 /**
602  * g_io_modules_load_all_in_directory_with_scope:
603  * @dirname: (type filename): pathname for a directory containing modules
604  *     to load.
605  * @scope: a scope to use when scanning the modules.
606  *
607  * Loads all the modules in the specified directory.
608  *
609  * If don't require all modules to be initialized (and thus registering
610  * all gtypes) then you can use g_io_modules_scan_all_in_directory()
611  * which allows delayed/lazy loading of modules.
612  *
613  * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
614  *      from the directory,
615  *      All the modules are loaded into memory, if you want to
616  *      unload them (enabling on-demand loading) you must call
617  *      g_type_module_unuse() on all the modules. Free the list
618  *      with g_list_free().
619  *
620  * Since: 2.30
621  **/
622 GList *
g_io_modules_load_all_in_directory_with_scope(const char * dirname,GIOModuleScope * scope)623 g_io_modules_load_all_in_directory_with_scope (const char     *dirname,
624                                                GIOModuleScope *scope)
625 {
626   const gchar *name;
627   GDir        *dir;
628   GList *modules;
629 
630   if (!g_module_supported ())
631     return NULL;
632 
633   dir = g_dir_open (dirname, 0, NULL);
634   if (!dir)
635     return NULL;
636 
637   modules = NULL;
638   while ((name = g_dir_read_name (dir)))
639     {
640       if (is_valid_module_name (name, scope))
641         {
642           GIOModule *module;
643           gchar     *path;
644 
645           path = g_build_filename (dirname, name, NULL);
646           module = g_io_module_new (path);
647 
648           if (!g_type_module_use (G_TYPE_MODULE (module)))
649             {
650               g_printerr ("Failed to load module: %s\n", path);
651               g_object_unref (module);
652               g_free (path);
653               continue;
654             }
655 
656           g_free (path);
657 
658           modules = g_list_prepend (modules, module);
659         }
660     }
661 
662   g_dir_close (dir);
663 
664   return modules;
665 }
666 
667 /**
668  * g_io_modules_load_all_in_directory:
669  * @dirname: (type filename): pathname for a directory containing modules
670  *     to load.
671  *
672  * Loads all the modules in the specified directory.
673  *
674  * If don't require all modules to be initialized (and thus registering
675  * all gtypes) then you can use g_io_modules_scan_all_in_directory()
676  * which allows delayed/lazy loading of modules.
677  *
678  * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
679  *      from the directory,
680  *      All the modules are loaded into memory, if you want to
681  *      unload them (enabling on-demand loading) you must call
682  *      g_type_module_unuse() on all the modules. Free the list
683  *      with g_list_free().
684  **/
685 GList *
g_io_modules_load_all_in_directory(const char * dirname)686 g_io_modules_load_all_in_directory (const char *dirname)
687 {
688   return g_io_modules_load_all_in_directory_with_scope (dirname, NULL);
689 }
690 
691 static gpointer
try_class(GIOExtension * extension,guint is_supported_offset)692 try_class (GIOExtension *extension,
693            guint         is_supported_offset)
694 {
695   GType type = g_io_extension_get_type (extension);
696   typedef gboolean (*verify_func) (void);
697   gpointer class;
698 
699   class = g_type_class_ref (type);
700   if (!is_supported_offset || (* G_STRUCT_MEMBER(verify_func, class, is_supported_offset)) ())
701     return class;
702 
703   g_type_class_unref (class);
704   return NULL;
705 }
706 
707 static void
print_help(const char * envvar,GIOExtensionPoint * ep)708 print_help (const char        *envvar,
709             GIOExtensionPoint *ep)
710 {
711   g_print ("Supported arguments for %s environment variable:\n", envvar);
712 
713   if (g_io_extension_point_get_extensions (ep) == NULL)
714     g_print (" (none)\n");
715   else
716     {
717       GList *l;
718       GIOExtension *extension;
719       int width = 0;
720 
721       for (l = g_io_extension_point_get_extensions (ep); l; l = l->next)
722         {
723           extension = l->data;
724           width = MAX (width, strlen (g_io_extension_get_name (extension)));
725         }
726 
727       for (l = g_io_extension_point_get_extensions (ep); l; l = l->next)
728         {
729           extension = l->data;
730 
731           g_print (" %*s - %d\n", width, g_io_extension_get_name (extension), g_io_extension_get_priority (extension));
732         }
733     }
734 }
735 
736 /**
737  * _g_io_module_get_default_type:
738  * @extension_point: the name of an extension point
739  * @envvar: (nullable): the name of an environment variable to
740  *     override the default implementation.
741  * @is_supported_offset: a vtable offset, or zero
742  *
743  * Retrieves the default class implementing @extension_point.
744  *
745  * If @envvar is not %NULL, and the environment variable with that
746  * name is set, then the implementation it specifies will be tried
747  * first. After that, or if @envvar is not set, all other
748  * implementations will be tried in order of decreasing priority.
749  *
750  * If @is_supported_offset is non-zero, then it is the offset into the
751  * class vtable at which there is a function that takes no arguments and
752  * returns a boolean.  This function will be called on each candidate
753  * implementation to check if it is actually usable or not.
754  *
755  * The result is cached after it is generated the first time, and
756  * the function is thread-safe.
757  *
758  * Returns: (transfer none): the type to instantiate to implement
759  *     @extension_point, or %G_TYPE_INVALID if there are no usable
760  *     implementations.
761  */
762 GType
_g_io_module_get_default_type(const gchar * extension_point,const gchar * envvar,guint is_supported_offset)763 _g_io_module_get_default_type (const gchar *extension_point,
764                                const gchar *envvar,
765                                guint        is_supported_offset)
766 {
767   static GRecMutex default_modules_lock;
768   static GHashTable *default_modules;
769   const char *use_this;
770   GList *l;
771   GIOExtensionPoint *ep;
772   GIOExtension *extension, *preferred;
773   gpointer impl;
774 
775   g_rec_mutex_lock (&default_modules_lock);
776   if (default_modules)
777     {
778       gpointer key;
779 
780       if (g_hash_table_lookup_extended (default_modules, extension_point, &key, &impl))
781         {
782           g_rec_mutex_unlock (&default_modules_lock);
783           return impl ? G_OBJECT_CLASS_TYPE (impl) : G_TYPE_INVALID;
784         }
785     }
786   else
787     {
788       default_modules = g_hash_table_new (g_str_hash, g_str_equal);
789     }
790 
791   _g_io_modules_ensure_loaded ();
792   ep = g_io_extension_point_lookup (extension_point);
793 
794   if (!ep)
795     {
796       g_warn_if_reached ();
797       g_rec_mutex_unlock (&default_modules_lock);
798       return G_TYPE_INVALID;
799     }
800 
801   use_this = envvar ? g_getenv (envvar) : NULL;
802   if (g_strcmp0 (use_this, "help") == 0)
803     {
804       print_help (envvar, ep);
805       use_this = NULL;
806     }
807 
808   if (use_this)
809     {
810       preferred = g_io_extension_point_get_extension_by_name (ep, use_this);
811       if (preferred)
812         {
813           impl = try_class (preferred, is_supported_offset);
814           if (impl)
815             goto done;
816         }
817       else
818         g_warning ("Can't find module '%s' specified in %s", use_this, envvar);
819     }
820   else
821     preferred = NULL;
822 
823   for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
824     {
825       extension = l->data;
826       if (extension == preferred)
827         continue;
828 
829       impl = try_class (extension, is_supported_offset);
830       if (impl)
831         goto done;
832     }
833 
834   impl = NULL;
835 
836  done:
837   g_hash_table_insert (default_modules, g_strdup (extension_point), impl);
838   g_rec_mutex_unlock (&default_modules_lock);
839 
840   return impl ? G_OBJECT_CLASS_TYPE (impl) : G_TYPE_INVALID;
841 }
842 
843 static gpointer
try_implementation(const char * extension_point,GIOExtension * extension,GIOModuleVerifyFunc verify_func)844 try_implementation (const char           *extension_point,
845                     GIOExtension         *extension,
846 		    GIOModuleVerifyFunc   verify_func)
847 {
848   GType type = g_io_extension_get_type (extension);
849   gpointer impl;
850 
851   if (g_type_is_a (type, G_TYPE_INITABLE))
852     {
853       GError *error = NULL;
854 
855       impl = g_initable_new (type, NULL, &error, NULL);
856       if (impl)
857         return impl;
858 
859       g_debug ("Failed to initialize %s (%s) for %s: %s",
860                g_io_extension_get_name (extension),
861                g_type_name (type),
862                extension_point,
863                error ? error->message : "");
864       g_clear_error (&error);
865       return NULL;
866     }
867   else
868     {
869       impl = g_object_new (type, NULL);
870       if (!verify_func || verify_func (impl))
871 	return impl;
872 
873       g_object_unref (impl);
874       return NULL;
875     }
876 }
877 
878 /**
879  * _g_io_module_get_default:
880  * @extension_point: the name of an extension point
881  * @envvar: (nullable): the name of an environment variable to
882  *     override the default implementation.
883  * @verify_func: (nullable): a function to call to verify that
884  *     a given implementation is usable in the current environment.
885  *
886  * Retrieves the default object implementing @extension_point.
887  *
888  * If @envvar is not %NULL, and the environment variable with that
889  * name is set, then the implementation it specifies will be tried
890  * first. After that, or if @envvar is not set, all other
891  * implementations will be tried in order of decreasing priority.
892  *
893  * If an extension point implementation implements #GInitable, then
894  * that implementation will only be used if it initializes
895  * successfully. Otherwise, if @verify_func is not %NULL, then it will
896  * be called on each candidate implementation after construction, to
897  * check if it is actually usable or not.
898  *
899  * The result is cached after it is generated the first time, and
900  * the function is thread-safe.
901  *
902  * Returns: (transfer none): an object implementing
903  *     @extension_point, or %NULL if there are no usable
904  *     implementations.
905  */
906 gpointer
_g_io_module_get_default(const gchar * extension_point,const gchar * envvar,GIOModuleVerifyFunc verify_func)907 _g_io_module_get_default (const gchar         *extension_point,
908 			  const gchar         *envvar,
909 			  GIOModuleVerifyFunc  verify_func)
910 {
911   static GRecMutex default_modules_lock;
912   static GHashTable *default_modules;
913   const char *use_this;
914   GList *l;
915   GIOExtensionPoint *ep;
916   GIOExtension *extension = NULL, *preferred;
917   gpointer impl;
918 
919   g_rec_mutex_lock (&default_modules_lock);
920   if (default_modules)
921     {
922       gpointer key;
923 
924       if (g_hash_table_lookup_extended (default_modules, extension_point,
925 					&key, &impl))
926 	{
927           /* Don’t debug here, since we’re returning a cached object which was
928            * already printed earlier. */
929 	  g_rec_mutex_unlock (&default_modules_lock);
930 	  return impl;
931 	}
932     }
933   else
934     {
935       default_modules = g_hash_table_new (g_str_hash, g_str_equal);
936     }
937 
938   _g_io_modules_ensure_loaded ();
939   ep = g_io_extension_point_lookup (extension_point);
940 
941   if (!ep)
942     {
943       g_debug ("%s: Failed to find extension point ‘%s’",
944                G_STRFUNC, extension_point);
945       g_warn_if_reached ();
946       g_rec_mutex_unlock (&default_modules_lock);
947       return NULL;
948     }
949 
950   use_this = envvar ? g_getenv (envvar) : NULL;
951   if (g_strcmp0 (use_this, "help") == 0)
952     {
953       print_help (envvar, ep);
954       use_this = NULL;
955     }
956 
957   if (use_this)
958     {
959       preferred = g_io_extension_point_get_extension_by_name (ep, use_this);
960       if (preferred)
961 	{
962 	  impl = try_implementation (extension_point, preferred, verify_func);
963 	  extension = preferred;
964 	  if (impl)
965 	    goto done;
966 	}
967       else
968         g_warning ("Can't find module '%s' specified in %s", use_this, envvar);
969     }
970   else
971     preferred = NULL;
972 
973   for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
974     {
975       extension = l->data;
976       if (extension == preferred)
977 	continue;
978 
979       impl = try_implementation (extension_point, extension, verify_func);
980       if (impl)
981 	goto done;
982     }
983 
984   impl = NULL;
985 
986  done:
987   g_hash_table_insert (default_modules,
988 		       g_strdup (extension_point),
989 		       impl ? g_object_ref (impl) : NULL);
990   g_rec_mutex_unlock (&default_modules_lock);
991 
992   if (impl != NULL)
993     {
994       g_assert (extension != NULL);
995       g_debug ("%s: Found default implementation %s (%s) for ‘%s’",
996                G_STRFUNC, g_io_extension_get_name (extension),
997                G_OBJECT_TYPE_NAME (impl), extension_point);
998     }
999   else
1000     g_debug ("%s: Failed to find default implementation for ‘%s’",
1001              G_STRFUNC, extension_point);
1002 
1003   return impl;
1004 }
1005 
1006 G_LOCK_DEFINE_STATIC (registered_extensions);
1007 G_LOCK_DEFINE_STATIC (loaded_dirs);
1008 
1009 extern GType g_fen_file_monitor_get_type (void);
1010 extern GType g_inotify_file_monitor_get_type (void);
1011 extern GType g_kqueue_file_monitor_get_type (void);
1012 extern GType g_win32_file_monitor_get_type (void);
1013 
1014 extern GType _g_unix_volume_monitor_get_type (void);
1015 extern GType _g_local_vfs_get_type (void);
1016 
1017 extern GType _g_win32_volume_monitor_get_type (void);
1018 extern GType _g_winhttp_vfs_get_type (void);
1019 
1020 extern GType _g_dummy_proxy_resolver_get_type (void);
1021 extern GType _g_dummy_tls_backend_get_type (void);
1022 extern GType g_network_monitor_base_get_type (void);
1023 #ifdef HAVE_NETLINK
1024 extern GType _g_network_monitor_netlink_get_type (void);
1025 extern GType _g_network_monitor_nm_get_type (void);
1026 #endif
1027 
1028 #ifdef G_OS_UNIX
1029 extern GType g_fdo_notification_backend_get_type (void);
1030 extern GType g_gtk_notification_backend_get_type (void);
1031 extern GType g_portal_notification_backend_get_type (void);
1032 extern GType g_proxy_resolver_portal_get_type (void);
1033 extern GType g_network_monitor_portal_get_type (void);
1034 #endif
1035 
1036 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
1037 extern GType g_cocoa_notification_backend_get_type (void);
1038 #endif
1039 
1040 #ifdef G_PLATFORM_WIN32
1041 extern GType g_win32_notification_backend_get_type (void);
1042 
1043 #include <windows.h>
1044 extern GType _g_win32_network_monitor_get_type (void);
1045 
1046 static HMODULE gio_dll = NULL;
1047 
1048 #ifdef DLL_EXPORT
1049 
1050 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
1051                      DWORD     fdwReason,
1052                      LPVOID    lpvReserved);
1053 
1054 BOOL WINAPI
DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)1055 DllMain (HINSTANCE hinstDLL,
1056 	 DWORD     fdwReason,
1057 	 LPVOID    lpvReserved)
1058 {
1059   if (fdwReason == DLL_PROCESS_ATTACH)
1060       gio_dll = hinstDLL;
1061 
1062   return TRUE;
1063 }
1064 
1065 #endif
1066 
1067 void *
_g_io_win32_get_module(void)1068 _g_io_win32_get_module (void)
1069 {
1070   if (!gio_dll)
1071     GetModuleHandleExA (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
1072                         (const char *) _g_io_win32_get_module,
1073                         &gio_dll);
1074   return gio_dll;
1075 }
1076 
1077 #endif
1078 
1079 void
_g_io_modules_ensure_extension_points_registered(void)1080 _g_io_modules_ensure_extension_points_registered (void)
1081 {
1082   static gboolean registered_extensions = FALSE;
1083   GIOExtensionPoint *ep;
1084 
1085   G_LOCK (registered_extensions);
1086 
1087   if (!registered_extensions)
1088     {
1089       registered_extensions = TRUE;
1090 
1091 #if defined(G_OS_UNIX) && !defined(HAVE_COCOA)
1092 #if !GLIB_CHECK_VERSION (3, 0, 0)
1093       ep = g_io_extension_point_register (G_DESKTOP_APP_INFO_LOOKUP_EXTENSION_POINT_NAME);
1094       g_io_extension_point_set_required_type (ep, G_TYPE_DESKTOP_APP_INFO_LOOKUP);
1095 #endif
1096 #endif
1097 
1098       ep = g_io_extension_point_register (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
1099       g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
1100 
1101       ep = g_io_extension_point_register (G_NFS_FILE_MONITOR_EXTENSION_POINT_NAME);
1102       g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
1103 
1104       ep = g_io_extension_point_register (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
1105       g_io_extension_point_set_required_type (ep, G_TYPE_VOLUME_MONITOR);
1106 
1107       ep = g_io_extension_point_register (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
1108       g_io_extension_point_set_required_type (ep, G_TYPE_NATIVE_VOLUME_MONITOR);
1109 
1110       ep = g_io_extension_point_register (G_VFS_EXTENSION_POINT_NAME);
1111       g_io_extension_point_set_required_type (ep, G_TYPE_VFS);
1112 
1113       ep = g_io_extension_point_register ("gsettings-backend");
1114       g_io_extension_point_set_required_type (ep, G_TYPE_OBJECT);
1115 
1116       ep = g_io_extension_point_register (G_PROXY_RESOLVER_EXTENSION_POINT_NAME);
1117       g_io_extension_point_set_required_type (ep, G_TYPE_PROXY_RESOLVER);
1118 
1119       ep = g_io_extension_point_register (G_PROXY_EXTENSION_POINT_NAME);
1120       g_io_extension_point_set_required_type (ep, G_TYPE_PROXY);
1121 
1122       ep = g_io_extension_point_register (G_TLS_BACKEND_EXTENSION_POINT_NAME);
1123       g_io_extension_point_set_required_type (ep, G_TYPE_TLS_BACKEND);
1124 
1125       ep = g_io_extension_point_register (G_NETWORK_MONITOR_EXTENSION_POINT_NAME);
1126       g_io_extension_point_set_required_type (ep, G_TYPE_NETWORK_MONITOR);
1127 
1128       ep = g_io_extension_point_register (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME);
1129       g_io_extension_point_set_required_type (ep, G_TYPE_NOTIFICATION_BACKEND);
1130     }
1131 
1132   G_UNLOCK (registered_extensions);
1133 }
1134 
1135 static gchar *
get_gio_module_dir(void)1136 get_gio_module_dir (void)
1137 {
1138   gchar *module_dir;
1139 
1140   module_dir = g_strdup (g_getenv ("GIO_MODULE_DIR"));
1141   if (module_dir == NULL)
1142     {
1143 #ifdef G_OS_WIN32
1144       gchar *install_dir;
1145 
1146       install_dir = g_win32_get_package_installation_directory_of_module (gio_dll);
1147 #ifdef _MSC_VER
1148       /* On Visual Studio builds we have all the libraries and binaries in bin
1149        * so better load the gio modules from bin instead of lib
1150        */
1151       module_dir = g_build_filename (install_dir,
1152                                      "bin", "gio", "modules",
1153                                      NULL);
1154 #else
1155       module_dir = g_build_filename (install_dir,
1156                                      "lib", "gio", "modules",
1157                                      NULL);
1158 #endif
1159       g_free (install_dir);
1160 #else
1161       module_dir = g_strdup (GIO_MODULE_DIR);
1162 #endif
1163     }
1164 
1165   return module_dir;
1166 }
1167 
1168 void
_g_io_modules_ensure_loaded(void)1169 _g_io_modules_ensure_loaded (void)
1170 {
1171   static gboolean loaded_dirs = FALSE;
1172   const char *module_path;
1173   GIOModuleScope *scope;
1174 
1175   _g_io_modules_ensure_extension_points_registered ();
1176 
1177   G_LOCK (loaded_dirs);
1178 
1179   if (!loaded_dirs)
1180     {
1181       gchar *module_dir;
1182 
1183       loaded_dirs = TRUE;
1184       scope = g_io_module_scope_new (G_IO_MODULE_SCOPE_BLOCK_DUPLICATES);
1185 
1186       /* First load any overrides, extras */
1187       module_path = g_getenv ("GIO_EXTRA_MODULES");
1188       if (module_path)
1189 	{
1190 	  gchar **paths;
1191 	  int i;
1192 
1193 	  paths = g_strsplit (module_path, G_SEARCHPATH_SEPARATOR_S, 0);
1194 
1195 	  for (i = 0; paths[i] != NULL; i++)
1196 	    {
1197 	      g_io_modules_scan_all_in_directory_with_scope (paths[i], scope);
1198 	    }
1199 
1200 	  g_strfreev (paths);
1201 	}
1202 
1203       /* Then load the compiled in path */
1204       module_dir = get_gio_module_dir ();
1205 
1206       g_io_modules_scan_all_in_directory_with_scope (module_dir, scope);
1207       g_free (module_dir);
1208 
1209       g_io_module_scope_free (scope);
1210 
1211       /* Initialize types from built-in "modules" */
1212       g_type_ensure (g_null_settings_backend_get_type ());
1213       g_type_ensure (g_memory_settings_backend_get_type ());
1214       g_type_ensure (g_keyfile_settings_backend_get_type ());
1215 #if defined(HAVE_INOTIFY_INIT1)
1216       g_type_ensure (g_inotify_file_monitor_get_type ());
1217 #endif
1218 #if defined(HAVE_KQUEUE)
1219       g_type_ensure (g_kqueue_file_monitor_get_type ());
1220 #endif
1221 #if defined(HAVE_FEN)
1222       g_type_ensure (g_fen_file_monitor_get_type ());
1223 #endif
1224 #ifdef G_OS_WIN32
1225       g_type_ensure (_g_win32_volume_monitor_get_type ());
1226       g_type_ensure (g_win32_file_monitor_get_type ());
1227       g_type_ensure (g_registry_backend_get_type ());
1228 #endif
1229 #ifdef HAVE_COCOA
1230       g_type_ensure (g_nextstep_settings_backend_get_type ());
1231       g_type_ensure (g_osx_app_info_get_type ());
1232 #endif
1233 #ifdef G_OS_UNIX
1234       g_type_ensure (_g_unix_volume_monitor_get_type ());
1235       g_type_ensure (g_fdo_notification_backend_get_type ());
1236       g_type_ensure (g_gtk_notification_backend_get_type ());
1237       g_type_ensure (g_portal_notification_backend_get_type ());
1238       g_type_ensure (g_network_monitor_portal_get_type ());
1239       g_type_ensure (g_proxy_resolver_portal_get_type ());
1240 #endif
1241 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
1242       g_type_ensure (g_cocoa_notification_backend_get_type ());
1243 #endif
1244 #ifdef G_OS_WIN32
1245       g_type_ensure (g_win32_notification_backend_get_type ());
1246       g_type_ensure (_g_winhttp_vfs_get_type ());
1247 #endif
1248       g_type_ensure (_g_local_vfs_get_type ());
1249       g_type_ensure (_g_dummy_proxy_resolver_get_type ());
1250       g_type_ensure (_g_http_proxy_get_type ());
1251       g_type_ensure (_g_https_proxy_get_type ());
1252       g_type_ensure (_g_socks4a_proxy_get_type ());
1253       g_type_ensure (_g_socks4_proxy_get_type ());
1254       g_type_ensure (_g_socks5_proxy_get_type ());
1255       g_type_ensure (_g_dummy_tls_backend_get_type ());
1256       g_type_ensure (g_network_monitor_base_get_type ());
1257 #ifdef HAVE_NETLINK
1258       g_type_ensure (_g_network_monitor_netlink_get_type ());
1259       g_type_ensure (_g_network_monitor_nm_get_type ());
1260 #endif
1261 #ifdef G_OS_WIN32
1262       g_type_ensure (_g_win32_network_monitor_get_type ());
1263 #endif
1264     }
1265 
1266   G_UNLOCK (loaded_dirs);
1267 }
1268 
1269 static void
g_io_extension_point_free(GIOExtensionPoint * ep)1270 g_io_extension_point_free (GIOExtensionPoint *ep)
1271 {
1272   g_free (ep->name);
1273   g_free (ep);
1274 }
1275 
1276 /**
1277  * g_io_extension_point_register:
1278  * @name: The name of the extension point
1279  *
1280  * Registers an extension point.
1281  *
1282  * Returns: (transfer none): the new #GIOExtensionPoint. This object is
1283  *    owned by GIO and should not be freed.
1284  */
1285 GIOExtensionPoint *
g_io_extension_point_register(const char * name)1286 g_io_extension_point_register (const char *name)
1287 {
1288   GIOExtensionPoint *ep;
1289 
1290   G_LOCK (extension_points);
1291   if (extension_points == NULL)
1292     extension_points = g_hash_table_new_full (g_str_hash,
1293 					      g_str_equal,
1294 					      NULL,
1295 					      (GDestroyNotify)g_io_extension_point_free);
1296 
1297   ep = g_hash_table_lookup (extension_points, name);
1298   if (ep != NULL)
1299     {
1300       G_UNLOCK (extension_points);
1301       return ep;
1302     }
1303 
1304   ep = g_new0 (GIOExtensionPoint, 1);
1305   ep->name = g_strdup (name);
1306 
1307   g_hash_table_insert (extension_points, ep->name, ep);
1308 
1309   G_UNLOCK (extension_points);
1310 
1311   return ep;
1312 }
1313 
1314 /**
1315  * g_io_extension_point_lookup:
1316  * @name: the name of the extension point
1317  *
1318  * Looks up an existing extension point.
1319  *
1320  * Returns: (transfer none): the #GIOExtensionPoint, or %NULL if there
1321  *    is no registered extension point with the given name.
1322  */
1323 GIOExtensionPoint *
g_io_extension_point_lookup(const char * name)1324 g_io_extension_point_lookup (const char *name)
1325 {
1326   GIOExtensionPoint *ep;
1327 
1328   G_LOCK (extension_points);
1329   ep = NULL;
1330   if (extension_points != NULL)
1331     ep = g_hash_table_lookup (extension_points, name);
1332 
1333   G_UNLOCK (extension_points);
1334 
1335   return ep;
1336 
1337 }
1338 
1339 /**
1340  * g_io_extension_point_set_required_type:
1341  * @extension_point: a #GIOExtensionPoint
1342  * @type: the #GType to require
1343  *
1344  * Sets the required type for @extension_point to @type.
1345  * All implementations must henceforth have this type.
1346  */
1347 void
g_io_extension_point_set_required_type(GIOExtensionPoint * extension_point,GType type)1348 g_io_extension_point_set_required_type (GIOExtensionPoint *extension_point,
1349 					GType              type)
1350 {
1351   extension_point->required_type = type;
1352 }
1353 
1354 /**
1355  * g_io_extension_point_get_required_type:
1356  * @extension_point: a #GIOExtensionPoint
1357  *
1358  * Gets the required type for @extension_point.
1359  *
1360  * Returns: the #GType that all implementations must have,
1361  *     or #G_TYPE_INVALID if the extension point has no required type
1362  */
1363 GType
g_io_extension_point_get_required_type(GIOExtensionPoint * extension_point)1364 g_io_extension_point_get_required_type (GIOExtensionPoint *extension_point)
1365 {
1366   return extension_point->required_type;
1367 }
1368 
1369 static void
lazy_load_modules(GIOExtensionPoint * extension_point)1370 lazy_load_modules (GIOExtensionPoint *extension_point)
1371 {
1372   GIOModule *module;
1373   GList *l;
1374 
1375   for (l = extension_point->lazy_load_modules; l != NULL; l = l->next)
1376     {
1377       module = l->data;
1378 
1379       if (!module->initialized)
1380 	{
1381 	  if (g_type_module_use (G_TYPE_MODULE (module)))
1382 	    g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
1383 	  else
1384 	    g_printerr ("Failed to load module: %s\n",
1385 			module->filename);
1386 	}
1387     }
1388 }
1389 
1390 /**
1391  * g_io_extension_point_get_extensions:
1392  * @extension_point: a #GIOExtensionPoint
1393  *
1394  * Gets a list of all extensions that implement this extension point.
1395  * The list is sorted by priority, beginning with the highest priority.
1396  *
1397  * Returns: (element-type GIOExtension) (transfer none): a #GList of
1398  *     #GIOExtensions. The list is owned by GIO and should not be
1399  *     modified.
1400  */
1401 GList *
g_io_extension_point_get_extensions(GIOExtensionPoint * extension_point)1402 g_io_extension_point_get_extensions (GIOExtensionPoint *extension_point)
1403 {
1404   g_return_val_if_fail (extension_point != NULL, NULL);
1405 
1406   lazy_load_modules (extension_point);
1407   return extension_point->extensions;
1408 }
1409 
1410 /**
1411  * g_io_extension_point_get_extension_by_name:
1412  * @extension_point: a #GIOExtensionPoint
1413  * @name: the name of the extension to get
1414  *
1415  * Finds a #GIOExtension for an extension point by name.
1416  *
1417  * Returns: (transfer none): the #GIOExtension for @extension_point that has the
1418  *    given name, or %NULL if there is no extension with that name
1419  */
1420 GIOExtension *
g_io_extension_point_get_extension_by_name(GIOExtensionPoint * extension_point,const char * name)1421 g_io_extension_point_get_extension_by_name (GIOExtensionPoint *extension_point,
1422 					    const char        *name)
1423 {
1424   GList *l;
1425 
1426   g_return_val_if_fail (name != NULL, NULL);
1427 
1428   lazy_load_modules (extension_point);
1429   for (l = extension_point->extensions; l != NULL; l = l->next)
1430     {
1431       GIOExtension *e = l->data;
1432 
1433       if (e->name != NULL &&
1434 	  strcmp (e->name, name) == 0)
1435 	return e;
1436     }
1437 
1438   return NULL;
1439 }
1440 
1441 static gint
extension_prio_compare(gconstpointer a,gconstpointer b)1442 extension_prio_compare (gconstpointer  a,
1443 			gconstpointer  b)
1444 {
1445   const GIOExtension *extension_a = a, *extension_b = b;
1446 
1447   if (extension_a->priority > extension_b->priority)
1448     return -1;
1449 
1450   if (extension_b->priority > extension_a->priority)
1451     return 1;
1452 
1453   return 0;
1454 }
1455 
1456 /**
1457  * g_io_extension_point_implement:
1458  * @extension_point_name: the name of the extension point
1459  * @type: the #GType to register as extension
1460  * @extension_name: the name for the extension
1461  * @priority: the priority for the extension
1462  *
1463  * Registers @type as extension for the extension point with name
1464  * @extension_point_name.
1465  *
1466  * If @type has already been registered as an extension for this
1467  * extension point, the existing #GIOExtension object is returned.
1468  *
1469  * Returns: (transfer none): a #GIOExtension object for #GType
1470  */
1471 GIOExtension *
g_io_extension_point_implement(const char * extension_point_name,GType type,const char * extension_name,gint priority)1472 g_io_extension_point_implement (const char *extension_point_name,
1473 				GType       type,
1474 				const char *extension_name,
1475 				gint        priority)
1476 {
1477   GIOExtensionPoint *extension_point;
1478   GIOExtension *extension;
1479   GList *l;
1480 
1481   g_return_val_if_fail (extension_point_name != NULL, NULL);
1482 
1483   extension_point = g_io_extension_point_lookup (extension_point_name);
1484   if (extension_point == NULL)
1485     {
1486       g_warning ("Tried to implement non-registered extension point %s", extension_point_name);
1487       return NULL;
1488     }
1489 
1490   if (extension_point->required_type != 0 &&
1491       !g_type_is_a (type, extension_point->required_type))
1492     {
1493       g_warning ("Tried to register an extension of the type %s to extension point %s. "
1494 		 "Expected type is %s.",
1495 		 g_type_name (type),
1496 		 extension_point_name,
1497 		 g_type_name (extension_point->required_type));
1498       return NULL;
1499     }
1500 
1501   /* It's safe to register the same type multiple times */
1502   for (l = extension_point->extensions; l != NULL; l = l->next)
1503     {
1504       extension = l->data;
1505       if (extension->type == type)
1506 	return extension;
1507     }
1508 
1509   extension = g_slice_new0 (GIOExtension);
1510   extension->type = type;
1511   extension->name = g_strdup (extension_name);
1512   extension->priority = priority;
1513 
1514   extension_point->extensions = g_list_insert_sorted (extension_point->extensions,
1515 						      extension, extension_prio_compare);
1516 
1517   return extension;
1518 }
1519 
1520 /**
1521  * g_io_extension_ref_class:
1522  * @extension: a #GIOExtension
1523  *
1524  * Gets a reference to the class for the type that is
1525  * associated with @extension.
1526  *
1527  * Returns: (transfer full): the #GTypeClass for the type of @extension
1528  */
1529 GTypeClass *
g_io_extension_ref_class(GIOExtension * extension)1530 g_io_extension_ref_class (GIOExtension *extension)
1531 {
1532   return g_type_class_ref (extension->type);
1533 }
1534 
1535 /**
1536  * g_io_extension_get_type:
1537  * @extension: a #GIOExtension
1538  *
1539  * Gets the type associated with @extension.
1540  *
1541  * Returns: the type of @extension
1542  */
1543 GType
g_io_extension_get_type(GIOExtension * extension)1544 g_io_extension_get_type (GIOExtension *extension)
1545 {
1546   return extension->type;
1547 }
1548 
1549 /**
1550  * g_io_extension_get_name:
1551  * @extension: a #GIOExtension
1552  *
1553  * Gets the name under which @extension was registered.
1554  *
1555  * Note that the same type may be registered as extension
1556  * for multiple extension points, under different names.
1557  *
1558  * Returns: the name of @extension.
1559  */
1560 const char *
g_io_extension_get_name(GIOExtension * extension)1561 g_io_extension_get_name (GIOExtension *extension)
1562 {
1563   return extension->name;
1564 }
1565 
1566 /**
1567  * g_io_extension_get_priority:
1568  * @extension: a #GIOExtension
1569  *
1570  * Gets the priority with which @extension was registered.
1571  *
1572  * Returns: the priority of @extension
1573  */
1574 gint
g_io_extension_get_priority(GIOExtension * extension)1575 g_io_extension_get_priority (GIOExtension *extension)
1576 {
1577   return extension->priority;
1578 }
1579