giomodule.c 38.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* GIO - GLib Input, Output and Streaming Library
 * 
 * Copyright (C) 2006-2007 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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 19 20
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 */

21
#include "config.h"
22

23 24
#include <string.h>

25
#include "giomodule.h"
Alexander Larsson's avatar
Alexander Larsson committed
26
#include "giomodule-priv.h"
27 28
#include "glocalfilemonitor.h"
#include "gnativevolumemonitor.h"
29
#include "gproxyresolver.h"
30
#include "gproxy.h"
31
#include "gsettingsbackendinternal.h"
Paolo Borelli's avatar
Paolo Borelli committed
32
#include "ghttpproxy.h"
33 34
#include "gsocks4proxy.h"
#include "gsocks4aproxy.h"
35
#include "gsocks5proxy.h"
36
#include "gtlsbackend.h"
37
#include "gvfs.h"
Lars Uebernickel's avatar
Lars Uebernickel committed
38
#include "gnotificationbackend.h"
39 40
#include "ginitable.h"
#include "gnetworkmonitor.h"
41 42 43
#ifdef G_OS_WIN32
#include "gregistrysettingsbackend.h"
#endif
44
#include <glib/gstdio.h>
45

46 47 48 49
#ifdef G_OS_UNIX
#include "gdesktopappinfo.h"
#endif

50 51 52
/**
 * SECTION:giomodule
 * @short_description: Loadable GIO Modules
Matthias Clasen's avatar
Matthias Clasen committed
53
 * @include: gio/gio.h
54 55
 *
 * Provides an interface and default functions for loading and unloading 
56 57
 * modules. This is used internally to make GIO extensible, but can also
 * be used by others to implement module loading.
58 59 60
 * 
 **/

61 62 63 64
/**
 * SECTION:extensionpoints
 * @short_description: Extension Points
 * @include: gio.h
65
 * @see_also: [Extending GIO][extending-gio]
66 67 68 69 70 71
 *
 * #GIOExtensionPoint provides a mechanism for modules to extend the
 * functionality of the library or application that loaded it in an 
 * organized fashion.  
 *
 * 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 74 75 76 77 78 79 80 81
 * thereof). Use g_io_extension_point_register() to register an
 * extension point, and g_io_extension_point_set_required_type() to
 * set a required type.
 *
 * A module can implement an extension point by specifying the #GType 
 * that implements the functionality. Additionally, each implementation
 * of an extension point has a name, and a priority. Use
 * g_io_extension_point_implement() to implement an extension point.
 * 
82
 *  |[<!-- language="C" -->
83 84
 *  GIOExtensionPoint *ep;
 *
Matthias Clasen's avatar
Matthias Clasen committed
85
 *  // Register an extension point
86 87 88 89
 *  ep = g_io_extension_point_register ("my-extension-point");
 *  g_io_extension_point_set_required_type (ep, MY_TYPE_EXAMPLE);
 *  ]|
 *
90
 *  |[<!-- language="C" -->
Matthias Clasen's avatar
Matthias Clasen committed
91
 *  // Implement an extension point
92 93 94 95 96 97 98 99 100 101 102
 *  G_DEFINE_TYPE (MyExampleImpl, my_example_impl, MY_TYPE_EXAMPLE);
 *  g_io_extension_point_implement ("my-extension-point",
 *                                  my_example_impl_get_type (),
 *                                  "my-example",
 *                                  10);
 *  ]|
 *
 *  It is up to the code that registered the extension point how
 *  it uses the implementations that have been associated with it.
 *  Depending on the use case, it may use all implementations, or
 *  only the one with the highest priority, or pick a specific
103 104 105 106
 *  one by name.
 *
 *  To avoid opening all modules just to find out what extension
 *  points they implement, GIO makes use of a caching mechanism,
107
 *  see [gio-querymodules][gio-querymodules].
108 109
 *  You are expected to run this command after installing a
 *  GIO module.
110
 *
111 112
 *  The `GIO_EXTRA_MODULES` environment variable can be used to
 *  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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
 *  directories, then the latter one will be ignored. If additional
 *  directories are specified GIO will load modules from the built-in
 *  directory last.
 */

/**
 * GIOModuleScope:
 *
 * Represents a scope for loading IO modules. A scope can be used for blocking
 * duplicate modules, or blocking a module you don't want to load.
 *
 * The scope can be used with g_io_modules_load_all_in_directory_with_scope()
 * or g_io_modules_scan_all_in_directory_with_scope().
 *
 * Since: 2.30
 */
struct _GIOModuleScope {
  GIOModuleScopeFlags flags;
  GHashTable *basenames;
};

/**
 * g_io_module_scope_new:
 * @flags: flags for the new scope
 *
 * Create a new scope for loading of IO modules. A scope can be used for
 * 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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
 * which have the same base name as a module that has already been seen
 * in this scope.
 *
 * Returns: (transfer full): the new module scope
 *
 * Since: 2.30
 */
GIOModuleScope *
g_io_module_scope_new (GIOModuleScopeFlags flags)
{
  GIOModuleScope *scope = g_new0 (GIOModuleScope, 1);
  scope->flags = flags;
  scope->basenames = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
  return scope;
}

/**
 * g_io_module_scope_free:
 * @scope: a module loading scope
 *
 * Free a module scope.
 *
 * Since: 2.30
 */
void
g_io_module_scope_free (GIOModuleScope *scope)
{
  if (!scope)
    return;
  g_hash_table_destroy (scope->basenames);
  g_free (scope);
}

/**
 * g_io_module_scope_block:
 * @scope: a module loading scope
Matthias Clasen's avatar
Matthias Clasen committed
180
 * @basename: the basename to block
181
 *
Matthias Clasen's avatar
Matthias Clasen committed
182 183 184
 * Block modules with the given @basename from being loaded when
 * this scope is used with g_io_modules_scan_all_in_directory_with_scope()
 * or g_io_modules_load_all_in_directory_with_scope().
185 186
 *
 * Since: 2.30
187
 */
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
void
g_io_module_scope_block (GIOModuleScope *scope,
                         const gchar    *basename)
{
  gchar *key;

  g_return_if_fail (scope != NULL);
  g_return_if_fail (basename != NULL);

  key = g_strdup (basename);
  g_hash_table_insert (scope->basenames, key, key);
}

static gboolean
_g_io_module_scope_contains (GIOModuleScope *scope,
                             const gchar    *basename)
{
  return g_hash_table_lookup (scope->basenames, basename) ? TRUE : FALSE;
}

208 209
struct _GIOModule {
  GTypeModule parent_instance;
210

211 212
  gchar       *filename;
  GModule     *library;
213 214
  gboolean     initialized; /* The module was loaded at least once */

215 216 217 218 219 220 221 222 223 224 225 226 227 228
  void (* load)   (GIOModule *module);
  void (* unload) (GIOModule *module);
};

struct _GIOModuleClass
{
  GTypeModuleClass parent_class;

};

static void      g_io_module_finalize      (GObject      *object);
static gboolean  g_io_module_load_module   (GTypeModule  *gmodule);
static void      g_io_module_unload_module (GTypeModule  *gmodule);

229 230 231 232 233 234
/**
 * GIOExtension:
 *
 * #GIOExtension is an opaque data structure and can only be accessed
 * using the following functions.
 */
235 236 237 238 239 240
struct _GIOExtension {
  char *name;
  GType type;
  gint priority;
};

241 242 243 244 245 246
/**
 * GIOExtensionPoint:
 *
 * #GIOExtensionPoint is an opaque data structure and can only be accessed
 * using the following functions.
 */
247 248 249 250 251 252 253 254 255 256
struct _GIOExtensionPoint {
  GType required_type;
  char *name;
  GList *extensions;
  GList *lazy_load_modules;
};

static GHashTable *extension_points = NULL;
G_LOCK_DEFINE_STATIC(extension_points);

257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
G_DEFINE_TYPE (GIOModule, g_io_module, G_TYPE_TYPE_MODULE);

static void
g_io_module_class_init (GIOModuleClass *class)
{
  GObjectClass     *object_class      = G_OBJECT_CLASS (class);
  GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);

  object_class->finalize     = g_io_module_finalize;

  type_module_class->load    = g_io_module_load_module;
  type_module_class->unload  = g_io_module_unload_module;
}

static void
g_io_module_init (GIOModule *module)
{
}

static void
g_io_module_finalize (GObject *object)
{
  GIOModule *module = G_IO_MODULE (object);

  g_free (module->filename);

  G_OBJECT_CLASS (g_io_module_parent_class)->finalize (object);
}

static gboolean
g_io_module_load_module (GTypeModule *gmodule)
{
  GIOModule *module = G_IO_MODULE (gmodule);

  if (!module->filename)
    {
      g_warning ("GIOModule path not set");
      return FALSE;
    }

  module->library = g_module_open (module->filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);

  if (!module->library)
    {
      g_printerr ("%s\n", g_module_error ());
      return FALSE;
    }

  /* Make sure that the loaded library contains the required methods */
  if (! g_module_symbol (module->library,
                         "g_io_module_load",
Alexander Larsson's avatar
Alexander Larsson committed
308
                         (gpointer) &module->load) ||
309 310
      ! g_module_symbol (module->library,
                         "g_io_module_unload",
Alexander Larsson's avatar
Alexander Larsson committed
311
                         (gpointer) &module->unload))
312 313 314 315 316 317 318 319 320
    {
      g_printerr ("%s\n", g_module_error ());
      g_module_close (module->library);

      return FALSE;
    }

  /* Initialize the loaded module */
  module->load (module);
321
  module->initialized = TRUE;
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341

  return TRUE;
}

static void
g_io_module_unload_module (GTypeModule *gmodule)
{
  GIOModule *module = G_IO_MODULE (gmodule);

  module->unload (module);

  g_module_close (module->library);
  module->library = NULL;

  module->load   = NULL;
  module->unload = NULL;
}

/**
 * g_io_module_new:
Alexander Larsson's avatar
Alexander Larsson committed
342
 * @filename: filename of the shared library module.
343
 * 
Alexander Larsson's avatar
Alexander Larsson committed
344 345
 * Creates a new GIOModule that will load the specific
 * shared library when in use.
346 347
 * 
 * Returns: a #GIOModule from given @filename, 
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
 * or %NULL on error.
 **/
GIOModule *
g_io_module_new (const gchar *filename)
{
  GIOModule *module;

  g_return_val_if_fail (filename != NULL, NULL);

  module = g_object_new (G_IO_TYPE_MODULE, NULL);
  module->filename = g_strdup (filename);

  return module;
}

static gboolean
364 365
is_valid_module_name (const gchar        *basename,
                      GIOModuleScope     *scope)
366
{
367 368
  gboolean result;

369
#if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
370 371 372
  if (!g_str_has_prefix (basename, "lib") ||
      !g_str_has_suffix (basename, ".so"))
    return FALSE;
373
#else
374 375
  if (!g_str_has_suffix (basename, ".dll"))
    return FALSE;
376
#endif
377 378 379 380 381 382 383 384 385 386

  result = TRUE;
  if (scope)
    {
      result = _g_io_module_scope_contains (scope, basename) ? FALSE : TRUE;
      if (result && (scope->flags & G_IO_MODULE_SCOPE_BLOCK_DUPLICATES))
        g_io_module_scope_block (scope, basename);
    }

  return result;
387 388
}

389

390
/**
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
394 395 396 397 398 399 400 401 402 403 404
 *
 * Scans all the modules in the specified directory, ensuring that
 * any extension point implemented by a module is registered.
 *
 * This may not actually load and initialize all the types in each
 * module, some modules may be lazily loaded and initialized when
 * an extension point it implementes is used with e.g.
 * g_io_extension_point_get_extensions() or
 * g_io_extension_point_get_extension_by_name().
 *
 * If you need to guarantee that all types are loaded in all the modules,
405
 * use g_io_modules_load_all_in_directory().
406
 *
407
 * Since: 2.30
408 409
 **/
void
410 411
g_io_modules_scan_all_in_directory_with_scope (const char     *dirname,
                                               GIOModuleScope *scope)
412 413 414 415
{
  const gchar *name;
  char *filename;
  GDir *dir;
416
  GStatBuf statbuf;
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
  char *data;
  time_t cache_mtime;
  GHashTable *cache;

  if (!g_module_supported ())
    return;

  dir = g_dir_open (dirname, 0, NULL);
  if (!dir)
    return;

  filename = g_build_filename (dirname, "giomodule.cache", NULL);

  cache = g_hash_table_new_full (g_str_hash, g_str_equal,
				 g_free, (GDestroyNotify)g_strfreev);

  cache_mtime = 0;
  if (g_stat (filename, &statbuf) == 0 &&
      g_file_get_contents (filename, &data, NULL, NULL))
    {
      char **lines;
      int i;

      /* Cache mtime is the time the cache file was created, any file
       * that has a ctime before this was created then and not modified
       * since then (userspace can't change ctime). Its possible to change
       * the ctime forward without changing the file content, by e.g.
       * chmoding the file, but this is uncommon and will only cause us
       * to not use the cache so will not cause bugs.
       */
      cache_mtime = statbuf.st_mtime;

      lines = g_strsplit (data, "\n", -1);
      g_free (data);

      for (i = 0;  lines[i] != NULL; i++)
	{
	  char *line = lines[i];
	  char *file;
	  char *colon;
	  char **extension_points;

	  if (line[0] == '#')
	    continue;

	  colon = strchr (line, ':');
	  if (colon == NULL || line == colon)
	    continue; /* Invalid line, ignore */

	  *colon = 0; /* terminate filename */
467
	  file = g_strdup (line);
468 469 470 471 472 473
	  colon++; /* after colon */

	  while (g_ascii_isspace (*colon))
	    colon++;

	  extension_points = g_strsplit (colon, ",", -1);
474
	  g_hash_table_insert (cache, file, extension_points);
475 476 477 478 479 480
	}
      g_strfreev (lines);
    }

  while ((name = g_dir_read_name (dir)))
    {
481
      if (is_valid_module_name (name, scope))
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
	{
	  GIOExtensionPoint *extension_point;
	  GIOModule *module;
	  gchar *path;
	  char **extension_points;
	  int i;

	  path = g_build_filename (dirname, name, NULL);
	  module = g_io_module_new (path);

	  extension_points = g_hash_table_lookup (cache, name);
	  if (extension_points != NULL &&
	      g_stat (path, &statbuf) == 0 &&
	      statbuf.st_ctime <= cache_mtime)
	    {
	      /* Lazy load/init the library when first required */
	      for (i = 0; extension_points[i] != NULL; i++)
		{
		  extension_point =
		    g_io_extension_point_register (extension_points[i]);
		  extension_point->lazy_load_modules =
		    g_list_prepend (extension_point->lazy_load_modules,
504
				    module);
505 506 507 508 509 510 511
		}
	    }
	  else
	    {
	      /* Try to load and init types */
	      if (g_type_module_use (G_TYPE_MODULE (module)))
		g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
512 513 514 515 516 517 518
	      else
		{ /* Failure to load */
		  g_printerr ("Failed to load module: %s\n", path);
		  g_object_unref (module);
		  g_free (path);
		  continue;
		}
519 520 521 522 523 524 525 526 527
	    }

	  g_free (path);
	}
    }

  g_dir_close (dir);

  g_hash_table_destroy (cache);
528 529

  g_free (filename);
530 531
}

532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
/**
 * g_io_modules_scan_all_in_directory:
 * @dirname: pathname for a directory containing modules to scan.
 *
 * Scans all the modules in the specified directory, ensuring that
 * any extension point implemented by a module is registered.
 *
 * This may not actually load and initialize all the types in each
 * module, some modules may be lazily loaded and initialized when
 * an extension point it implementes is used with e.g.
 * g_io_extension_point_get_extensions() or
 * g_io_extension_point_get_extension_by_name().
 *
 * If you need to guarantee that all types are loaded in all the modules,
 * use g_io_modules_load_all_in_directory().
 *
 * Since: 2.24
 **/
void
g_io_modules_scan_all_in_directory (const char *dirname)
{
  g_io_modules_scan_all_in_directory_with_scope (dirname, NULL);
}
555

Alexander Larsson's avatar
Alexander Larsson committed
556
/**
557
 * g_io_modules_load_all_in_directory_with_scope:
Alexander Larsson's avatar
Alexander Larsson committed
558
 * @dirname: pathname for a directory containing modules to load.
559
 * @scope: a scope to use when scanning the modules.
560 561 562
 *
 * Loads all the modules in the specified directory.
 *
563 564 565 566
 * If don't require all modules to be initialized (and thus registering
 * all gtypes) then you can use g_io_modules_scan_all_in_directory()
 * which allows delayed/lazy loading of modules.
 *
567 568
 * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
 *      from the directory,
569 570 571 572
 *      All the modules are loaded into memory, if you want to
 *      unload them (enabling on-demand loading) you must call
 *      g_type_module_unuse() on all the modules. Free the list
 *      with g_list_free().
573 574
 *
 * Since: 2.30
Alexander Larsson's avatar
Alexander Larsson committed
575 576
 **/
GList *
577 578
g_io_modules_load_all_in_directory_with_scope (const char     *dirname,
                                               GIOModuleScope *scope)
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
{
  const gchar *name;
  GDir        *dir;
  GList *modules;

  if (!g_module_supported ())
    return NULL;

  dir = g_dir_open (dirname, 0, NULL);
  if (!dir)
    return NULL;

  modules = NULL;
  while ((name = g_dir_read_name (dir)))
    {
594
      if (is_valid_module_name (name, scope))
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
        {
          GIOModule *module;
          gchar     *path;

          path = g_build_filename (dirname, name, NULL);
          module = g_io_module_new (path);

          if (!g_type_module_use (G_TYPE_MODULE (module)))
            {
              g_printerr ("Failed to load module: %s\n", path);
              g_object_unref (module);
              g_free (path);
              continue;
            }
	  
          g_free (path);

          modules = g_list_prepend (modules, module);
        }
    }
  
  g_dir_close (dir);

  return modules;
}

621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
/**
 * g_io_modules_load_all_in_directory:
 * @dirname: pathname for a directory containing modules to load.
 *
 * Loads all the modules in the specified directory.
 *
 * If don't require all modules to be initialized (and thus registering
 * all gtypes) then you can use g_io_modules_scan_all_in_directory()
 * which allows delayed/lazy loading of modules.
 *
 * Returns: (element-type GIOModule) (transfer full): a list of #GIOModules loaded
 *      from the directory,
 *      All the modules are loaded into memory, if you want to
 *      unload them (enabling on-demand loading) you must call
 *      g_type_module_unuse() on all the modules. Free the list
 *      with g_list_free().
 **/
GList *
g_io_modules_load_all_in_directory (const char *dirname)
{
  return g_io_modules_load_all_in_directory_with_scope (dirname, NULL);
}

644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
static gpointer
try_class (GIOExtension *extension,
           guint         is_supported_offset)
{
  GType type = g_io_extension_get_type (extension);
  typedef gboolean (*verify_func) (void);
  gpointer class;

  class = g_type_class_ref (type);
  if (!is_supported_offset || (* G_STRUCT_MEMBER(verify_func, class, is_supported_offset)) ())
    return class;

  g_type_class_unref (class);
  return NULL;
}

/**
 * _g_io_module_get_default_type:
 * @extension_point: the name of an extension point
 * @envvar: (allow-none): the name of an environment variable to
 *     override the default implementation.
 * @is_supported_offset: a vtable offset, or zero
 *
 * Retrieves the default class implementing @extension_point.
 *
 * If @envvar is not %NULL, and the environment variable with that
 * name is set, then the implementation it specifies will be tried
 * first. After that, or if @envvar is not set, all other
 * implementations will be tried in order of decreasing priority.
 *
 * If @is_supported_offset is non-zero, then it is the offset into the
 * class vtable at which there is a function that takes no arguments and
 * returns a boolean.  This function will be called on each candidate
 * implementation to check if it is actually usable or not.
 *
 * The result is cached after it is generated the first time, and
 * the function is thread-safe.
 *
682
 * Returns: (transfer none): an object implementing
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
 *     @extension_point, or %NULL if there are no usable
 *     implementations.
 */
GType
_g_io_module_get_default_type (const gchar *extension_point,
                               const gchar *envvar,
                               guint        is_supported_offset)
{
  static GRecMutex default_modules_lock;
  static GHashTable *default_modules;
  const char *use_this;
  GList *l;
  GIOExtensionPoint *ep;
  GIOExtension *extension, *preferred;
  gpointer impl;

  g_rec_mutex_lock (&default_modules_lock);
  if (default_modules)
    {
      gpointer key;

      if (g_hash_table_lookup_extended (default_modules, extension_point, &key, &impl))
        {
          g_rec_mutex_unlock (&default_modules_lock);
          return impl ? G_OBJECT_CLASS_TYPE (impl) : G_TYPE_INVALID;
        }
    }
  else
    {
      default_modules = g_hash_table_new (g_str_hash, g_str_equal);
    }

  _g_io_modules_ensure_loaded ();
  ep = g_io_extension_point_lookup (extension_point);

  if (!ep)
    {
      g_warn_if_reached ();
      g_rec_mutex_unlock (&default_modules_lock);
      return G_TYPE_INVALID;
    }

  use_this = envvar ? g_getenv (envvar) : NULL;
  if (use_this)
    {
      preferred = g_io_extension_point_get_extension_by_name (ep, use_this);
      if (preferred)
        {
          impl = try_class (preferred, is_supported_offset);
          if (impl)
            goto done;
        }
      else
        g_warning ("Can't find module '%s' specified in %s", use_this, envvar);
    }
  else
    preferred = NULL;

  for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
    {
      extension = l->data;
      if (extension == preferred)
        continue;

      impl = try_class (extension, is_supported_offset);
      if (impl)
        goto done;
    }

  impl = NULL;

 done:
  g_hash_table_insert (default_modules, g_strdup (extension_point), impl);
  g_rec_mutex_unlock (&default_modules_lock);

  return impl ? G_OBJECT_CLASS_TYPE (impl) : G_TYPE_INVALID;
}
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804

static gpointer
try_implementation (GIOExtension         *extension,
		    GIOModuleVerifyFunc   verify_func)
{
  GType type = g_io_extension_get_type (extension);
  gpointer impl;

  if (g_type_is_a (type, G_TYPE_INITABLE))
    return g_initable_new (type, NULL, NULL, NULL);
  else
    {
      impl = g_object_new (type, NULL);
      if (!verify_func || verify_func (impl))
	return impl;

      g_object_unref (impl);
      return NULL;
    }
}

/**
 * _g_io_module_get_default:
 * @extension_point: the name of an extension point
 * @envvar: (allow-none): the name of an environment variable to
 *     override the default implementation.
 * @verify_func: (allow-none): a function to call to verify that
 *     a given implementation is usable in the current environment.
 *
 * Retrieves the default object implementing @extension_point.
 *
 * If @envvar is not %NULL, and the environment variable with that
 * name is set, then the implementation it specifies will be tried
 * first. After that, or if @envvar is not set, all other
 * implementations will be tried in order of decreasing priority.
 *
 * If an extension point implementation implements #GInitable, then
 * that implementation will only be used if it initializes
 * successfully. Otherwise, if @verify_func is not %NULL, then it will
 * be called on each candidate implementation after construction, to
 * check if it is actually usable or not.
 *
 * The result is cached after it is generated the first time, and
 * the function is thread-safe.
 *
805
 * Returns: (transfer none): an object implementing
806 807 808 809 810 811 812 813
 *     @extension_point, or %NULL if there are no usable
 *     implementations.
 */
gpointer
_g_io_module_get_default (const gchar         *extension_point,
			  const gchar         *envvar,
			  GIOModuleVerifyFunc  verify_func)
{
814 815
  static GRecMutex default_modules_lock;
  static GHashTable *default_modules;
816 817 818 819 820 821
  const char *use_this;
  GList *l;
  GIOExtensionPoint *ep;
  GIOExtension *extension, *preferred;
  gpointer impl;

822
  g_rec_mutex_lock (&default_modules_lock);
823 824 825 826 827 828 829
  if (default_modules)
    {
      gpointer key;

      if (g_hash_table_lookup_extended (default_modules, extension_point,
					&key, &impl))
	{
830
	  g_rec_mutex_unlock (&default_modules_lock);
831 832 833 834 835 836 837 838 839 840 841 842 843 844
	  return impl;
	}
    }
  else
    {
      default_modules = g_hash_table_new (g_str_hash, g_str_equal);
    }

  _g_io_modules_ensure_loaded ();
  ep = g_io_extension_point_lookup (extension_point);

  if (!ep)
    {
      g_warn_if_reached ();
845
      g_rec_mutex_unlock (&default_modules_lock);
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
      return NULL;
    }

  use_this = envvar ? g_getenv (envvar) : NULL;
  if (use_this)
    {
      preferred = g_io_extension_point_get_extension_by_name (ep, use_this);
      if (preferred)
	{
	  impl = try_implementation (preferred, verify_func);
	  if (impl)
	    goto done;
	}
      else
	g_warning ("Can't find module '%s' specified in %s", use_this, envvar);
    }
  else
    preferred = NULL;

  for (l = g_io_extension_point_get_extensions (ep); l != NULL; l = l->next)
    {
      extension = l->data;
      if (extension == preferred)
	continue;

      impl = try_implementation (extension, verify_func);
      if (impl)
	goto done;
    }

  impl = NULL;

 done:
  g_hash_table_insert (default_modules,
880 881
		       g_strdup (extension_point),
		       impl ? g_object_ref (impl) : NULL);
882
  g_rec_mutex_unlock (&default_modules_lock);
883 884 885 886

  return impl;
}

887
G_LOCK_DEFINE_STATIC (registered_extensions);
888 889
G_LOCK_DEFINE_STATIC (loaded_dirs);

890 891 892 893 894
extern GType g_fen_file_monitor_get_type (void);
extern GType g_inotify_file_monitor_get_type (void);
extern GType g_kqueue_file_monitor_get_type (void);
extern GType g_win32_file_monitor_get_type (void);

895 896 897
extern GType _g_unix_volume_monitor_get_type (void);
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);
900

901
extern GType _g_dummy_proxy_resolver_get_type (void);
902
extern GType _g_dummy_tls_backend_get_type (void);
903 904 905
extern GType g_network_monitor_base_get_type (void);
#ifdef HAVE_NETLINK
extern GType _g_network_monitor_netlink_get_type (void);
906
extern GType _g_network_monitor_nm_get_type (void);
907
#endif
908

Lars Uebernickel's avatar
Lars Uebernickel committed
909 910
#ifdef G_OS_UNIX
extern GType g_fdo_notification_backend_get_type (void);
911
extern GType g_gtk_notification_backend_get_type (void);
Lars Uebernickel's avatar
Lars Uebernickel committed
912 913
#endif

914 915 916 917
#ifdef HAVE_COCOA
extern GType g_cocoa_notification_backend_get_type (void);
#endif

918 919 920 921 922 923 924 925
#ifdef G_PLATFORM_WIN32

#include <windows.h>

static HMODULE gio_dll = NULL;

#ifdef DLL_EXPORT

Dan Winship's avatar
Dan Winship committed
926 927 928 929
BOOL WINAPI DllMain (HINSTANCE hinstDLL,
                     DWORD     fdwReason,
                     LPVOID    lpvReserved);

930 931 932 933 934 935 936 937 938 939 940
BOOL WINAPI
DllMain (HINSTANCE hinstDLL,
	 DWORD     fdwReason,
	 LPVOID    lpvReserved)
{
  if (fdwReason == DLL_PROCESS_ATTACH)
      gio_dll = hinstDLL;

  return TRUE;
}

941 942
#endif

943 944 945
void *
_g_io_win32_get_module (void)
{
946 947 948 949
  if (!gio_dll)
    GetModuleHandleExA (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
                        (const char *) _g_io_win32_get_module,
                        &gio_dll);
950 951 952
  return gio_dll;
}

953 954
#endif

955
void
956
_g_io_modules_ensure_extension_points_registered (void)
957
{
958
  static gboolean registered_extensions = FALSE;
959
  GIOExtensionPoint *ep;
960

961
  G_LOCK (registered_extensions);
962 963
  
  if (!registered_extensions)
964
    {
965 966
      registered_extensions = TRUE;
      
967 968 969
#ifdef G_OS_UNIX
#if !GLIB_CHECK_VERSION (3, 0, 0)
      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
973 974
#endif
#endif
975

976 977
      ep = g_io_extension_point_register (G_LOCAL_FILE_MONITOR_EXTENSION_POINT_NAME);
      g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);
978 979 980 981

      ep = g_io_extension_point_register (G_NFS_FILE_MONITOR_EXTENSION_POINT_NAME);
      g_io_extension_point_set_required_type (ep, G_TYPE_LOCAL_FILE_MONITOR);

982 983 984 985 986 987 988 989
      ep = g_io_extension_point_register (G_VOLUME_MONITOR_EXTENSION_POINT_NAME);
      g_io_extension_point_set_required_type (ep, G_TYPE_VOLUME_MONITOR);
      
      ep = g_io_extension_point_register (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME);
      g_io_extension_point_set_required_type (ep, G_TYPE_NATIVE_VOLUME_MONITOR);
      
      ep = g_io_extension_point_register (G_VFS_EXTENSION_POINT_NAME);
      g_io_extension_point_set_required_type (ep, G_TYPE_VFS);
990 991 992

      ep = g_io_extension_point_register ("gsettings-backend");
      g_io_extension_point_set_required_type (ep, G_TYPE_OBJECT);
993 994 995

      ep = g_io_extension_point_register (G_PROXY_RESOLVER_EXTENSION_POINT_NAME);
      g_io_extension_point_set_required_type (ep, G_TYPE_PROXY_RESOLVER);
996 997 998

      ep = g_io_extension_point_register (G_PROXY_EXTENSION_POINT_NAME);
      g_io_extension_point_set_required_type (ep, G_TYPE_PROXY);
999 1000 1001

      ep = g_io_extension_point_register (G_TLS_BACKEND_EXTENSION_POINT_NAME);
      g_io_extension_point_set_required_type (ep, G_TYPE_TLS_BACKEND);
1002 1003 1004

      ep = g_io_extension_point_register (G_NETWORK_MONITOR_EXTENSION_POINT_NAME);
      g_io_extension_point_set_required_type (ep, G_TYPE_NETWORK_MONITOR);
Lars Uebernickel's avatar
Lars Uebernickel committed
1005 1006 1007

      ep = g_io_extension_point_register (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME);
      g_io_extension_point_set_required_type (ep, G_TYPE_NOTIFICATION_BACKEND);
1008 1009
    }
  
1010
  G_UNLOCK (registered_extensions);
1011
}
1012

1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
static gchar *
get_gio_module_dir (void)
{
  gchar *module_dir;

  module_dir = g_strdup (g_getenv ("GIO_MODULE_DIR"));
  if (module_dir == NULL)
    {
#ifdef G_OS_WIN32
      gchar *install_dir;

      install_dir = g_win32_get_package_installation_directory_of_module (gio_dll);
1025 1026 1027 1028 1029 1030 1031 1032
#ifdef _MSC_VER
      /* On Visual Studio builds we have all the libraries and binaries in bin
       * so better load the gio modules from bin instead of lib
       */
      module_dir = g_build_filename (install_dir,
                                     "bin", "gio", "modules",
                                     NULL);
#else
1033 1034 1035
      module_dir = g_build_filename (install_dir,
                                     "lib", "gio", "modules",
                                     NULL);
1036
#endif
1037 1038
      g_free (install_dir);
#else
1039
      module_dir = g_strdup (GIO_MODULE_DIR);
1040 1041 1042 1043 1044 1045
#endif
    }

  return module_dir;
}

1046 1047 1048 1049 1050
void
_g_io_modules_ensure_loaded (void)
{
  static gboolean loaded_dirs = FALSE;
  const char *module_path;
1051
  GIOModuleScope *scope;
1052 1053 1054 1055 1056 1057 1058

  _g_io_modules_ensure_extension_points_registered ();
  
  G_LOCK (loaded_dirs);

  if (!loaded_dirs)
    {
1059 1060
      gchar *module_dir;

1061
      loaded_dirs = TRUE;
1062
      scope = g_io_module_scope_new (G_IO_MODULE_SCOPE_BLOCK_DUPLICATES);
1063

1064
      /* First load any overrides, extras */
1065 1066
      module_path = g_getenv ("GIO_EXTRA_MODULES");
      if (module_path)
1067 1068 1069
	{
	  gchar **paths;
	  int i;
1070

1071
	  paths = g_strsplit (module_path, G_SEARCHPATH_SEPARATOR_S, 0);
1072 1073

	  for (i = 0; paths[i] != NULL; i++)
1074 1075 1076
	    {
	      g_io_modules_scan_all_in_directory_with_scope (paths[i], scope);
	    }
1077 1078 1079

	  g_strfreev (paths);
	}
1080

1081
      /* Then load the compiled in path */
1082
      module_dir = get_gio_module_dir ();
1083 1084

      g_io_modules_scan_all_in_directory_with_scope (module_dir, scope);
1085
      g_free (module_dir);
1086 1087 1088

      g_io_module_scope_free (scope);

1089
      /* Initialize types from built-in "modules" */
1090 1091
      g_type_ensure (g_null_settings_backend_get_type ());
      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 ());
Alexander Larsson's avatar
Alexander Larsson committed
1094
#endif
1095
#if defined(HAVE_KQUEUE)
1096
      g_type_ensure (g_kqueue_file_monitor_get_type ());
1097
#endif
Alexander Larsson's avatar
Alexander Larsson committed
1098
#if defined(HAVE_FEN)
1099
      g_type_ensure (g_fen_file_monitor_get_type ());
1100
#endif
Alexander Larsson's avatar
Alexander Larsson committed
1101
#ifdef G_OS_WIN32
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 ());
Alexander Larsson's avatar
Alexander Larsson committed
1105
#endif
1106
#ifdef HAVE_COCOA
1107 1108
      g_nextstep_settings_backend_get_type ();
#endif
1109
#ifdef G_OS_UNIX
1110
      g_type_ensure (_g_unix_volume_monitor_get_type ());
Lars Uebernickel's avatar
Lars Uebernickel committed
1111
      g_type_ensure (g_fdo_notification_backend_get_type ());
1112
      g_type_ensure (g_gtk_notification_backend_get_type ());
1113
#endif
1114 1115 1116
#ifdef HAVE_COCOA
      g_type_ensure (g_cocoa_notification_backend_get_type ());
#endif
1117
#ifdef G_OS_WIN32
1118
      g_type_ensure (_g_winhttp_vfs_get_type ());
1119
#endif
1120 1121
      g_type_ensure (_g_local_vfs_get_type ());
      g_type_ensure (_g_dummy_proxy_resolver_get_type ());
Paolo Borelli's avatar
Paolo Borelli committed
1122 1123
      g_type_ensure (_g_http_proxy_get_type ());
      g_type_ensure (_g_https_proxy_get_type ());
1124 1125 1126 1127 1128
      g_type_ensure (_g_socks4a_proxy_get_type ());
      g_type_ensure (_g_socks4_proxy_get_type ());
      g_type_ensure (_g_socks5_proxy_get_type ());
      g_type_ensure (_g_dummy_tls_backend_get_type ());
      g_type_ensure (g_network_monitor_base_get_type ());
1129
#ifdef HAVE_NETLINK
1130
      g_type_ensure (_g_network_monitor_netlink_get_type ());
1131
      g_type_ensure (_g_network_monitor_nm_get_type ());
1132
#endif
1133
    }
1134

1135 1136
  G_UNLOCK (loaded_dirs);
}
1137

1138 1139 1140 1141 1142 1143 1144
static void
g_io_extension_point_free (GIOExtensionPoint *ep)
{
  g_free (ep->name);
  g_free (ep);
}

1145 1146 1147 1148 1149 1150
/**
 * g_io_extension_point_register:
 * @name: The name of the extension point
 *
 * Registers an extension point.
 *
1151 1152
 * Returns: (transfer none): the new #GIOExtensionPoint. This object is
 *    owned by GIO and should not be freed.
1153
 */
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
GIOExtensionPoint *
g_io_extension_point_register (const char *name)
{
  GIOExtensionPoint *ep;
  
  G_LOCK (extension_points);
  if (extension_points == NULL)
    extension_points = g_hash_table_new_full (g_str_hash,
					      g_str_equal,
					      NULL,
					      (GDestroyNotify)g_io_extension_point_free);

1166 1167
  ep = g_hash_table_lookup (extension_points, name);
  if (ep != NULL)
1168 1169
    {
      G_UNLOCK (extension_points);
1170
      return ep;
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
    }

  ep = g_new0 (GIOExtensionPoint, 1);
  ep->name = g_strdup (name);
  
  g_hash_table_insert (extension_points, ep->name, ep);
  
  G_UNLOCK (extension_points);

  return ep;
}

1183 1184 1185 1186 1187 1188
/**
 * g_io_extension_point_lookup:
 * @name: the name of the extension point
 *
 * Looks up an existing extension point.
 *
1189 1190
 * Returns: (transfer none): the #GIOExtensionPoint, or %NULL if there
 *    is no registered extension point with the given name.
1191
 */
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
GIOExtensionPoint *
g_io_extension_point_lookup (const char *name)
{
  GIOExtensionPoint *ep;

  G_LOCK (extension_points);
  ep = NULL;
  if (extension_points != NULL)
    ep = g_hash_table_lookup (extension_points, name);
  
  G_UNLOCK (extension_points);

  return ep;
  
}

1208 1209 1210 1211 1212 1213 1214 1215
/**
 * g_io_extension_point_set_required_type:
 * @extension_point: a #GIOExtensionPoint
 * @type: the #GType to require
 *
 * Sets the required type for @extension_point to @type. 
 * All implementations must henceforth have this type.
 */
1216 1217 1218 1219 1220 1221 1222
void
g_io_extension_point_set_required_type (GIOExtensionPoint *extension_point,
					GType              type)
{
  extension_point->required_type = type;
}

1223 1224 1225 1226 1227 1228 1229 1230 1231
/**
 * g_io_extension_point_get_required_type:
 * @extension_point: a #GIOExtensionPoint
 *
 * Gets the required type for @extension_point.
 *
 * Returns: the #GType that all implementations must have, 
 *     or #G_TYPE_INVALID if the extension point has no required type
 */
1232 1233 1234 1235 1236 1237
GType
g_io_extension_point_get_required_type (GIOExtensionPoint *extension_point)
{
  return extension_point->required_type;
}

Allison Karlitskaya's avatar
Allison Karlitskaya committed
1238
static void
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
lazy_load_modules (GIOExtensionPoint *extension_point)
{
  GIOModule *module;
  GList *l;

  for (l = extension_point->lazy_load_modules; l != NULL; l = l->next)
    {
      module = l->data;

      if (!module->initialized)
	{
	  if (g_type_module_use (G_TYPE_MODULE (module)))
	    g_type_module_unuse (G_TYPE_MODULE (module)); /* Unload */
	  else
	    g_printerr ("Failed to load module: %s\n",
			module->filename);
	}
    }
}

1259 1260 1261 1262 1263 1264
/**
 * g_io_extension_point_get_extensions:
 * @extension_point: a #GIOExtensionPoint
 *
 * Gets a list of all extensions that implement this extension point.
 * The list is sorted by priority, beginning with the highest priority.
1265 1266
 *
 * Returns: (element-type GIOExtension) (transfer none): a #GList of
1267 1268
 *     #GIOExtensions. The list is owned by GIO and should not be
 *     modified.
1269
 */
1270 1271 1272
GList *
g_io_extension_point_get_extensions (GIOExtensionPoint *extension_point)
{
1273
  lazy_load_modules (extension_point);
1274 1275 1276
  return extension_point->extensions;
}

1277 1278 1279 1280 1281 1282 1283
/**
 * g_io_extension_point_get_extension_by_name:
 * @extension_point: a #GIOExtensionPoint
 * @name: the name of the extension to get
 *
 * Finds a #GIOExtension for an extension point by name.
 *
1284
 * Returns: (transfer none): the #GIOExtension for @extension_point that has the
1285 1286
 *    given name, or %NULL if there is no extension with that name
 */
1287 1288 1289 1290 1291 1292
GIOExtension *
g_io_extension_point_get_extension_by_name (GIOExtensionPoint *extension_point,
					    const char        *name)
{
  GList *l;

1293 1294
  g_return_val_if_fail (name != NULL, NULL);

1295
  lazy_load_modules (extension_point);
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
  for (l = extension_point->extensions; l != NULL; l = l->next)
    {
      GIOExtension *e = l->data;

      if (e->name != NULL &&
	  strcmp (e->name, name) == 0)
	return e;
    }
  
  return NULL;
}

static gint
extension_prio_compare (gconstpointer  a,
			gconstpointer  b)
{
  const GIOExtension *extension_a = a, *extension_b = b;

1314 1315 1316 1317 1318 1319 1320
  if (extension_a->priority > extension_b->priority)
    return -1;

  if (extension_b->priority > extension_a->priority)
    return 1;

  return 0;
1321 1322
}

1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
/**
 * g_io_extension_point_implement:
 * @extension_point_name: the name of the extension point
 * @type: the #GType to register as extension 
 * @extension_name: the name for the extension
 * @priority: the priority for the extension
 *
 * Registers @type as extension for the extension point with name
 * @extension_point_name. 
 *
 * If @type has already been registered as an extension for this 
 * extension point, the existing #GIOExtension object is returned.
 *
1336
 * Returns: (transfer none): a #GIOExtension object for #GType
1337
 */
1338 1339
GIOExtension *
g_io_extension_point_implement (const char *extension_point_name,
1340
				GType       type,
1341
				const char *extension_name,
1342
				gint        priority)
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
{
  GIOExtensionPoint *extension_point;
  GIOExtension *extension;
  GList *l;

  g_return_val_if_fail (extension_point_name != NULL, NULL);

  extension_point = g_io_extension_point_lookup (extension_point_name);
  if (extension_point == NULL)
    {
      g_warning ("Tried to implement non-registered extension point %s", extension_point_name);
      return NULL;
    }
  
  if (extension_point->required_type != 0 &&
      !g_type_is_a (type, extension_point->required_type))
    {
      g_warning ("Tried to register an extension of the type %s to extension point %s. "
		 "Expected type is %s.",
		 g_type_name (type),
		 extension_point_name, 
		 g_type_name (extension_point->required_type));
      return NULL;
    }      

1368
  /* It's safe to register the same type multiple times */
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
  for (l = extension_point->extensions; l != NULL; l = l->next)
    {
      extension = l->data;
      if (extension->type == type)
	return extension;
    }
  
  extension = g_slice_new0 (GIOExtension);
  extension->type = type;
  extension->name = g_strdup (extension_name);
  extension->priority = priority;
  
  extension_point->extensions = g_list_insert_sorted (extension_point->extensions,
						      extension, extension_prio_compare);
  
  return extension;
}

1387 1388 1389 1390 1391 1392 1393
/**
 * g_io_extension_ref_class:
 * @extension: a #GIOExtension
 *
 * Gets a reference to the class for the type that is 
 * associated with @extension.
 *
1394
 * Returns: (transfer full): the #GTypeClass for the type of @extension
1395
 */
1396 1397 1398 1399 1400 1401
GTypeClass *
g_io_extension_ref_class (GIOExtension *extension)
{
  return g_type_class_ref (extension->type);
}

1402 1403 1404 1405 1406 1407 1408 1409
/**
 * g_io_extension_get_type:
 * @extension: a #GIOExtension
 *
 * Gets the type associated with @extension.
 *
 * Returns: the type of @extension
 */
1410 1411 1412 1413 1414 1415
GType
g_io_extension_get_type (GIOExtension *extension)
{
  return extension->type;
}

1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
/**
 * g_io_extension_get_name:
 * @extension: a #GIOExtension
 *
 * Gets the name under which @extension was registered.
 *
 * Note that the same type may be registered as extension
 * for multiple extension points, under different names.
 *
 * Returns: the name of @extension.
 */
1427 1428 1429 1430 1431 1432
const char *
g_io_extension_get_name (GIOExtension *extension)
{
  return extension->name;
}

1433 1434 1435 1436 1437 1438 1439 1440
/**
 * g_io_extension_get_priority:
 * @extension: a #GIOExtension
 *
 * Gets the priority with which @extension was registered.
 *
 * Returns: the priority of @extension
 */
1441 1442 1443 1444 1445
gint
g_io_extension_get_priority (GIOExtension *extension)
{
  return extension->priority;
}