1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
26 #include "gdbusintrospection.h"
31 * SECTION:gdbusintrospection
32 * @title: D-Bus Introspection Data
33 * @short_description: Node and interface description data structures
36 * Various data structures and convenience routines to parse and
37 * generate D-Bus introspection XML. Introspection information is
38 * used when registering objects with g_dbus_connection_register_object().
40 * The format of D-Bus introspection XML is specified in the
41 * [D-Bus specification](http://dbus.freedesktop.org/doc/dbus-specification.html#introspection-format)
44 /* ---------------------------------------------------------------------------------------------------- */
46 #define _MY_DEFINE_BOXED_TYPE(TypeName, type_name) \
47 G_DEFINE_BOXED_TYPE (TypeName, type_name, type_name##_ref, type_name##_unref)
49 _MY_DEFINE_BOXED_TYPE (GDBusNodeInfo, g_dbus_node_info);
50 _MY_DEFINE_BOXED_TYPE (GDBusInterfaceInfo, g_dbus_interface_info);
51 _MY_DEFINE_BOXED_TYPE (GDBusMethodInfo, g_dbus_method_info);
52 _MY_DEFINE_BOXED_TYPE (GDBusSignalInfo, g_dbus_signal_info);
53 _MY_DEFINE_BOXED_TYPE (GDBusPropertyInfo, g_dbus_property_info);
54 _MY_DEFINE_BOXED_TYPE (GDBusArgInfo, g_dbus_arg_info);
55 _MY_DEFINE_BOXED_TYPE (GDBusAnnotationInfo, g_dbus_annotation_info);
57 /* ---------------------------------------------------------------------------------------------------- */
61 /* stuff we are currently collecting */
66 GPtrArray *properties;
67 GPtrArray *interfaces;
69 GPtrArray *annotations;
71 /* A list of GPtrArray's containing annotations */
72 GSList *annotations_stack;
74 /* A list of GPtrArray's containing interfaces */
75 GSList *interfaces_stack;
77 /* A list of GPtrArray's containing nodes */
80 /* Whether the direction was "in" for last parsed arg */
81 gboolean last_arg_was_in;
83 /* Number of args currently being collected; used for assigning
84 * names to args without a "name" attribute
90 /* ---------------------------------------------------------------------------------------------------- */
93 * g_dbus_node_info_ref:
94 * @info: A #GDBusNodeInfo
96 * If @info is statically allocated does nothing. Otherwise increases
97 * the reference count.
99 * Returns: The same @info.
104 g_dbus_node_info_ref (GDBusNodeInfo *info)
106 if (info->ref_count == -1)
108 g_atomic_int_inc (&info->ref_count);
113 * g_dbus_interface_info_ref:
114 * @info: A #GDBusInterfaceInfo
116 * If @info is statically allocated does nothing. Otherwise increases
117 * the reference count.
119 * Returns: The same @info.
124 g_dbus_interface_info_ref (GDBusInterfaceInfo *info)
126 if (info->ref_count == -1)
128 g_atomic_int_inc (&info->ref_count);
133 * g_dbus_method_info_ref:
134 * @info: A #GDBusMethodInfo
136 * If @info is statically allocated does nothing. Otherwise increases
137 * the reference count.
139 * Returns: The same @info.
144 g_dbus_method_info_ref (GDBusMethodInfo *info)
146 if (info->ref_count == -1)
148 g_atomic_int_inc (&info->ref_count);
153 * g_dbus_signal_info_ref:
154 * @info: A #GDBusSignalInfo
156 * If @info is statically allocated does nothing. Otherwise increases
157 * the reference count.
159 * Returns: The same @info.
164 g_dbus_signal_info_ref (GDBusSignalInfo *info)
166 if (info->ref_count == -1)
168 g_atomic_int_inc (&info->ref_count);
173 * g_dbus_property_info_ref:
174 * @info: A #GDBusPropertyInfo
176 * If @info is statically allocated does nothing. Otherwise increases
177 * the reference count.
179 * Returns: The same @info.
184 g_dbus_property_info_ref (GDBusPropertyInfo *info)
186 if (info->ref_count == -1)
188 g_atomic_int_inc (&info->ref_count);
193 * g_dbus_arg_info_ref:
194 * @info: A #GDBusArgInfo
196 * If @info is statically allocated does nothing. Otherwise increases
197 * the reference count.
199 * Returns: The same @info.
204 g_dbus_arg_info_ref (GDBusArgInfo *info)
206 if (info->ref_count == -1)
208 g_atomic_int_inc (&info->ref_count);
213 * g_dbus_annotation_info_ref:
214 * @info: A #GDBusNodeInfo
216 * If @info is statically allocated does nothing. Otherwise increases
217 * the reference count.
219 * Returns: The same @info.
223 GDBusAnnotationInfo *
224 g_dbus_annotation_info_ref (GDBusAnnotationInfo *info)
226 if (info->ref_count == -1)
228 g_atomic_int_inc (&info->ref_count);
232 /* ---------------------------------------------------------------------------------------------------- */
235 free_null_terminated_array (gpointer array, GDestroyNotify unref_func)
241 for (n = 0; p[n] != NULL; n++)
247 * g_dbus_annotation_info_unref:
248 * @info: A #GDBusAnnotationInfo.
250 * If @info is statically allocated, does nothing. Otherwise decreases
251 * the reference count of @info. When its reference count drops to 0,
252 * the memory used is freed.
257 g_dbus_annotation_info_unref (GDBusAnnotationInfo *info)
259 if (info->ref_count == -1)
261 if (g_atomic_int_dec_and_test (&info->ref_count))
264 g_free (info->value);
265 free_null_terminated_array (info->annotations, (GDestroyNotify) g_dbus_annotation_info_unref);
271 * g_dbus_arg_info_unref:
272 * @info: A #GDBusArgInfo.
274 * If @info is statically allocated, does nothing. Otherwise decreases
275 * the reference count of @info. When its reference count drops to 0,
276 * the memory used is freed.
281 g_dbus_arg_info_unref (GDBusArgInfo *info)
283 if (info->ref_count == -1)
285 if (g_atomic_int_dec_and_test (&info->ref_count))
288 g_free (info->signature);
289 free_null_terminated_array (info->annotations, (GDestroyNotify) g_dbus_annotation_info_unref);
295 * g_dbus_method_info_unref:
296 * @info: A #GDBusMethodInfo.
298 * If @info is statically allocated, does nothing. Otherwise decreases
299 * the reference count of @info. When its reference count drops to 0,
300 * the memory used is freed.
305 g_dbus_method_info_unref (GDBusMethodInfo *info)
307 if (info->ref_count == -1)
309 if (g_atomic_int_dec_and_test (&info->ref_count))
312 free_null_terminated_array (info->in_args, (GDestroyNotify) g_dbus_arg_info_unref);
313 free_null_terminated_array (info->out_args, (GDestroyNotify) g_dbus_arg_info_unref);
314 free_null_terminated_array (info->annotations, (GDestroyNotify) g_dbus_annotation_info_unref);
320 * g_dbus_signal_info_unref:
321 * @info: A #GDBusSignalInfo.
323 * If @info is statically allocated, does nothing. Otherwise decreases
324 * the reference count of @info. When its reference count drops to 0,
325 * the memory used is freed.
330 g_dbus_signal_info_unref (GDBusSignalInfo *info)
332 if (info->ref_count == -1)
334 if (g_atomic_int_dec_and_test (&info->ref_count))
337 free_null_terminated_array (info->args, (GDestroyNotify) g_dbus_arg_info_unref);
338 free_null_terminated_array (info->annotations, (GDestroyNotify) g_dbus_annotation_info_unref);
344 * g_dbus_property_info_unref:
345 * @info: A #GDBusPropertyInfo.
347 * If @info is statically allocated, does nothing. Otherwise decreases
348 * the reference count of @info. When its reference count drops to 0,
349 * the memory used is freed.
354 g_dbus_property_info_unref (GDBusPropertyInfo *info)
356 if (info->ref_count == -1)
358 if (g_atomic_int_dec_and_test (&info->ref_count))
361 g_free (info->signature);
362 free_null_terminated_array (info->annotations, (GDestroyNotify) g_dbus_annotation_info_unref);
368 * g_dbus_interface_info_unref:
369 * @info: A #GDBusInterfaceInfo.
371 * If @info is statically allocated, does nothing. Otherwise decreases
372 * the reference count of @info. When its reference count drops to 0,
373 * the memory used is freed.
378 g_dbus_interface_info_unref (GDBusInterfaceInfo *info)
380 if (info->ref_count == -1)
382 if (g_atomic_int_dec_and_test (&info->ref_count))
385 free_null_terminated_array (info->methods, (GDestroyNotify) g_dbus_method_info_unref);
386 free_null_terminated_array (info->signals, (GDestroyNotify) g_dbus_signal_info_unref);
387 free_null_terminated_array (info->properties, (GDestroyNotify) g_dbus_property_info_unref);
388 free_null_terminated_array (info->annotations, (GDestroyNotify) g_dbus_annotation_info_unref);
394 * g_dbus_node_info_unref:
395 * @info: A #GDBusNodeInfo.
397 * If @info is statically allocated, does nothing. Otherwise decreases
398 * the reference count of @info. When its reference count drops to 0,
399 * the memory used is freed.
404 g_dbus_node_info_unref (GDBusNodeInfo *info)
406 if (info->ref_count == -1)
408 if (g_atomic_int_dec_and_test (&info->ref_count))
411 free_null_terminated_array (info->interfaces, (GDestroyNotify) g_dbus_interface_info_unref);
412 free_null_terminated_array (info->nodes, (GDestroyNotify) g_dbus_node_info_unref);
413 free_null_terminated_array (info->annotations, (GDestroyNotify) g_dbus_annotation_info_unref);
418 /* ---------------------------------------------------------------------------------------------------- */
421 g_dbus_annotation_info_set (ParseData *data,
422 GDBusAnnotationInfo *info,
425 GDBusAnnotationInfo **embedded_annotations)
430 info->key = g_strdup (key);
433 info->value = g_strdup (value);
435 if (embedded_annotations != NULL)
436 info->annotations = embedded_annotations;
440 g_dbus_arg_info_set (ParseData *data,
443 const gchar *signature,
444 GDBusAnnotationInfo **annotations)
448 /* name may be NULL - TODO: compute name? */
450 info->name = g_strdup (name);
452 if (signature != NULL)
453 info->signature = g_strdup (signature);
455 if (annotations != NULL)
456 info->annotations = annotations;
460 g_dbus_method_info_set (ParseData *data,
461 GDBusMethodInfo *info,
463 GDBusArgInfo **in_args,
464 GDBusArgInfo **out_args,
465 GDBusAnnotationInfo **annotations)
470 info->name = g_strdup (name);
473 info->in_args = in_args;
475 if (out_args != NULL)
476 info->out_args = out_args;
478 if (annotations != NULL)
479 info->annotations = annotations;
483 g_dbus_signal_info_set (ParseData *data,
484 GDBusSignalInfo *info,
487 GDBusAnnotationInfo **annotations)
492 info->name = g_strdup (name);
497 if (annotations != NULL)
498 info->annotations = annotations;
502 g_dbus_property_info_set (ParseData *data,
503 GDBusPropertyInfo *info,
505 const gchar *signature,
506 GDBusPropertyInfoFlags flags,
507 GDBusAnnotationInfo **annotations)
512 info->name = g_strdup (name);
514 if (flags != G_DBUS_PROPERTY_INFO_FLAGS_NONE)
517 if (signature != NULL)
518 info->signature = g_strdup (signature);
520 if (annotations != NULL)
521 info->annotations = annotations;
525 g_dbus_interface_info_set (ParseData *data,
526 GDBusInterfaceInfo *info,
528 GDBusMethodInfo **methods,
529 GDBusSignalInfo **signals,
530 GDBusPropertyInfo **properties,
531 GDBusAnnotationInfo **annotations)
536 info->name = g_strdup (name);
539 info->methods = methods;
542 info->signals = signals;
544 if (properties != NULL)
545 info->properties = properties;
547 if (annotations != NULL)
548 info->annotations = annotations;
552 g_dbus_node_info_set (ParseData *data,
555 GDBusInterfaceInfo **interfaces,
556 GDBusNodeInfo **nodes,
557 GDBusAnnotationInfo **annotations)
563 info->path = g_strdup (path);
564 /* TODO: relative / absolute path snafu */
567 if (interfaces != NULL)
568 info->interfaces = interfaces;
573 if (annotations != NULL)
574 info->annotations = annotations;
577 /* ---------------------------------------------------------------------------------------------------- */
580 g_dbus_annotation_info_generate_xml (GDBusAnnotationInfo *info,
582 GString *string_builder)
587 tmp = g_markup_printf_escaped ("%*s<annotation name=\"%s\" value=\"%s\"",
591 g_string_append (string_builder, tmp);
594 if (info->annotations == NULL)
596 g_string_append (string_builder, "/>\n");
600 g_string_append (string_builder, ">\n");
602 for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
603 g_dbus_annotation_info_generate_xml (info->annotations[n],
607 g_string_append_printf (string_builder, "%*s</annotation>\n",
614 g_dbus_arg_info_generate_xml (GDBusArgInfo *info,
616 const gchar *extra_attributes,
617 GString *string_builder)
621 g_string_append_printf (string_builder, "%*s<arg type=\"%s\"",
625 if (info->name != NULL)
626 g_string_append_printf (string_builder, " name=\"%s\"", info->name);
628 if (extra_attributes != NULL)
629 g_string_append_printf (string_builder, " %s", extra_attributes);
631 if (info->annotations == NULL)
633 g_string_append (string_builder, "/>\n");
637 g_string_append (string_builder, ">\n");
639 for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
640 g_dbus_annotation_info_generate_xml (info->annotations[n],
644 g_string_append_printf (string_builder, "%*s</arg>\n", indent, "");
650 g_dbus_method_info_generate_xml (GDBusMethodInfo *info,
652 GString *string_builder)
656 g_string_append_printf (string_builder, "%*s<method name=\"%s\"",
660 if (info->annotations == NULL && info->in_args == NULL && info->out_args == NULL)
662 g_string_append (string_builder, "/>\n");
666 g_string_append (string_builder, ">\n");
668 for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
669 g_dbus_annotation_info_generate_xml (info->annotations[n],
673 for (n = 0; info->in_args != NULL && info->in_args[n] != NULL; n++)
674 g_dbus_arg_info_generate_xml (info->in_args[n],
679 for (n = 0; info->out_args != NULL && info->out_args[n] != NULL; n++)
680 g_dbus_arg_info_generate_xml (info->out_args[n],
685 g_string_append_printf (string_builder, "%*s</method>\n", indent, "");
690 g_dbus_signal_info_generate_xml (GDBusSignalInfo *info,
692 GString *string_builder)
696 g_string_append_printf (string_builder, "%*s<signal name=\"%s\"",
700 if (info->annotations == NULL && info->args == NULL)
702 g_string_append (string_builder, "/>\n");
706 g_string_append (string_builder, ">\n");
708 for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
709 g_dbus_annotation_info_generate_xml (info->annotations[n],
713 for (n = 0; info->args != NULL && info->args[n] != NULL; n++)
714 g_dbus_arg_info_generate_xml (info->args[n],
719 g_string_append_printf (string_builder, "%*s</signal>\n", indent, "");
724 g_dbus_property_info_generate_xml (GDBusPropertyInfo *info,
726 GString *string_builder)
729 const gchar *access_string;
731 if ((info->flags & G_DBUS_PROPERTY_INFO_FLAGS_READABLE) &&
732 (info->flags & G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE))
734 access_string = "readwrite";
736 else if (info->flags & G_DBUS_PROPERTY_INFO_FLAGS_READABLE)
738 access_string = "read";
740 else if (info->flags & G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE)
742 access_string = "write";
746 g_assert_not_reached ();
749 g_string_append_printf (string_builder, "%*s<property type=\"%s\" name=\"%s\" access=\"%s\"",
755 if (info->annotations == NULL)
757 g_string_append (string_builder, "/>\n");
761 g_string_append (string_builder, ">\n");
763 for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
764 g_dbus_annotation_info_generate_xml (info->annotations[n],
768 g_string_append_printf (string_builder, "%*s</property>\n", indent, "");
774 * g_dbus_interface_info_generate_xml:
775 * @info: A #GDBusNodeInfo
776 * @indent: Indentation level.
777 * @string_builder: (out): A #GString to to append XML data to.
779 * Appends an XML representation of @info (and its children) to @string_builder.
781 * This function is typically used for generating introspection XML
782 * documents at run-time for handling the
783 * `org.freedesktop.DBus.Introspectable.Introspect`
789 g_dbus_interface_info_generate_xml (GDBusInterfaceInfo *info,
791 GString *string_builder)
795 g_string_append_printf (string_builder, "%*s<interface name=\"%s\">\n",
799 for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
800 g_dbus_annotation_info_generate_xml (info->annotations[n],
804 for (n = 0; info->methods != NULL && info->methods[n] != NULL; n++)
805 g_dbus_method_info_generate_xml (info->methods[n],
809 for (n = 0; info->signals != NULL && info->signals[n] != NULL; n++)
810 g_dbus_signal_info_generate_xml (info->signals[n],
814 for (n = 0; info->properties != NULL && info->properties[n] != NULL; n++)
815 g_dbus_property_info_generate_xml (info->properties[n],
819 g_string_append_printf (string_builder, "%*s</interface>\n", indent, "");
823 * g_dbus_node_info_generate_xml:
824 * @info: A #GDBusNodeInfo.
825 * @indent: Indentation level.
826 * @string_builder: (out): A #GString to to append XML data to.
828 * Appends an XML representation of @info (and its children) to @string_builder.
830 * This function is typically used for generating introspection XML documents at run-time for
831 * handling the `org.freedesktop.DBus.Introspectable.Introspect` method.
836 g_dbus_node_info_generate_xml (GDBusNodeInfo *info,
838 GString *string_builder)
842 g_string_append_printf (string_builder, "%*s<node", indent, "");
843 if (info->path != NULL)
844 g_string_append_printf (string_builder, " name=\"%s\"", info->path);
846 if (info->interfaces == NULL && info->nodes == NULL && info->annotations == NULL)
848 g_string_append (string_builder, "/>\n");
852 g_string_append (string_builder, ">\n");
854 for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
855 g_dbus_annotation_info_generate_xml (info->annotations[n],
859 for (n = 0; info->interfaces != NULL && info->interfaces[n] != NULL; n++)
860 g_dbus_interface_info_generate_xml (info->interfaces[n],
864 for (n = 0; info->nodes != NULL && info->nodes[n] != NULL; n++)
865 g_dbus_node_info_generate_xml (info->nodes[n],
869 g_string_append_printf (string_builder, "%*s</node>\n", indent, "");
873 /* ---------------------------------------------------------------------------------------------------- */
875 static GDBusAnnotationInfo **
876 parse_data_steal_annotations (ParseData *data,
877 guint *out_num_elements)
879 GDBusAnnotationInfo **ret;
880 if (out_num_elements != NULL)
881 *out_num_elements = data->annotations->len;
882 if (data->annotations == NULL)
886 g_ptr_array_add (data->annotations, NULL);
887 ret = (GDBusAnnotationInfo **) g_ptr_array_free (data->annotations, FALSE);
889 data->annotations = g_ptr_array_new ();
893 static GDBusArgInfo **
894 parse_data_steal_args (ParseData *data,
895 guint *out_num_elements)
898 if (out_num_elements != NULL)
899 *out_num_elements = data->args->len;
900 if (data->args == NULL)
904 g_ptr_array_add (data->args, NULL);
905 ret = (GDBusArgInfo **) g_ptr_array_free (data->args, FALSE);
907 data->args = g_ptr_array_new ();
911 static GDBusArgInfo **
912 parse_data_steal_out_args (ParseData *data,
913 guint *out_num_elements)
916 if (out_num_elements != NULL)
917 *out_num_elements = data->out_args->len;
918 if (data->out_args == NULL)
922 g_ptr_array_add (data->out_args, NULL);
923 ret = (GDBusArgInfo **) g_ptr_array_free (data->out_args, FALSE);
925 data->out_args = g_ptr_array_new ();
929 static GDBusMethodInfo **
930 parse_data_steal_methods (ParseData *data,
931 guint *out_num_elements)
933 GDBusMethodInfo **ret;
934 if (out_num_elements != NULL)
935 *out_num_elements = data->methods->len;
936 if (data->methods == NULL)
940 g_ptr_array_add (data->methods, NULL);
941 ret = (GDBusMethodInfo **) g_ptr_array_free (data->methods, FALSE);
943 data->methods = g_ptr_array_new ();
947 static GDBusSignalInfo **
948 parse_data_steal_signals (ParseData *data,
949 guint *out_num_elements)
951 GDBusSignalInfo **ret;
952 if (out_num_elements != NULL)
953 *out_num_elements = data->signals->len;
954 if (data->signals == NULL)
958 g_ptr_array_add (data->signals, NULL);
959 ret = (GDBusSignalInfo **) g_ptr_array_free (data->signals, FALSE);
961 data->signals = g_ptr_array_new ();
965 static GDBusPropertyInfo **
966 parse_data_steal_properties (ParseData *data,
967 guint *out_num_elements)
969 GDBusPropertyInfo **ret;
970 if (out_num_elements != NULL)
971 *out_num_elements = data->properties->len;
972 if (data->properties == NULL)
976 g_ptr_array_add (data->properties, NULL);
977 ret = (GDBusPropertyInfo **) g_ptr_array_free (data->properties, FALSE);
979 data->properties = g_ptr_array_new ();
983 static GDBusInterfaceInfo **
984 parse_data_steal_interfaces (ParseData *data,
985 guint *out_num_elements)
987 GDBusInterfaceInfo **ret;
988 if (out_num_elements != NULL)
989 *out_num_elements = data->interfaces->len;
990 if (data->interfaces == NULL)
994 g_ptr_array_add (data->interfaces, NULL);
995 ret = (GDBusInterfaceInfo **) g_ptr_array_free (data->interfaces, FALSE);
997 data->interfaces = g_ptr_array_new ();
1001 static GDBusNodeInfo **
1002 parse_data_steal_nodes (ParseData *data,
1003 guint *out_num_elements)
1005 GDBusNodeInfo **ret;
1006 if (out_num_elements != NULL)
1007 *out_num_elements = data->nodes->len;
1008 if (data->nodes == NULL)
1012 g_ptr_array_add (data->nodes, NULL);
1013 ret = (GDBusNodeInfo **) g_ptr_array_free (data->nodes, FALSE);
1015 data->nodes = g_ptr_array_new ();
1019 /* ---------------------------------------------------------------------------------------------------- */
1022 parse_data_free_annotations (ParseData *data)
1024 if (data->annotations == NULL)
1026 g_ptr_array_foreach (data->annotations, (GFunc) g_dbus_annotation_info_unref, NULL);
1027 g_ptr_array_free (data->annotations, TRUE);
1028 data->annotations = NULL;
1032 parse_data_free_args (ParseData *data)
1034 if (data->args == NULL)
1036 g_ptr_array_foreach (data->args, (GFunc) g_dbus_arg_info_unref, NULL);
1037 g_ptr_array_free (data->args, TRUE);
1042 parse_data_free_out_args (ParseData *data)
1044 if (data->out_args == NULL)
1046 g_ptr_array_foreach (data->out_args, (GFunc) g_dbus_arg_info_unref, NULL);
1047 g_ptr_array_free (data->out_args, TRUE);
1048 data->out_args = NULL;
1052 parse_data_free_methods (ParseData *data)
1054 if (data->methods == NULL)
1056 g_ptr_array_foreach (data->methods, (GFunc) g_dbus_method_info_unref, NULL);
1057 g_ptr_array_free (data->methods, TRUE);
1058 data->methods = NULL;
1062 parse_data_free_signals (ParseData *data)
1064 if (data->signals == NULL)
1066 g_ptr_array_foreach (data->signals, (GFunc) g_dbus_signal_info_unref, NULL);
1067 g_ptr_array_free (data->signals, TRUE);
1068 data->signals = NULL;
1072 parse_data_free_properties (ParseData *data)
1074 if (data->properties == NULL)
1076 g_ptr_array_foreach (data->properties, (GFunc) g_dbus_property_info_unref, NULL);
1077 g_ptr_array_free (data->properties, TRUE);
1078 data->properties = NULL;
1082 parse_data_free_interfaces (ParseData *data)
1084 if (data->interfaces == NULL)
1086 g_ptr_array_foreach (data->interfaces, (GFunc) g_dbus_interface_info_unref, NULL);
1087 g_ptr_array_free (data->interfaces, TRUE);
1088 data->interfaces = NULL;
1092 parse_data_free_nodes (ParseData *data)
1094 if (data->nodes == NULL)
1096 g_ptr_array_foreach (data->nodes, (GFunc) g_dbus_node_info_unref, NULL);
1097 g_ptr_array_free (data->nodes, TRUE);
1101 /* ---------------------------------------------------------------------------------------------------- */
1103 static GDBusAnnotationInfo *
1104 parse_data_get_annotation (ParseData *data,
1105 gboolean create_new)
1108 g_ptr_array_add (data->annotations, g_new0 (GDBusAnnotationInfo, 1));
1109 return data->annotations->pdata[data->annotations->len - 1];
1112 static GDBusArgInfo *
1113 parse_data_get_arg (ParseData *data,
1114 gboolean create_new)
1117 g_ptr_array_add (data->args, g_new0 (GDBusArgInfo, 1));
1118 return data->args->pdata[data->args->len - 1];
1121 static GDBusArgInfo *
1122 parse_data_get_out_arg (ParseData *data,
1123 gboolean create_new)
1126 g_ptr_array_add (data->out_args, g_new0 (GDBusArgInfo, 1));
1127 return data->out_args->pdata[data->out_args->len - 1];
1130 static GDBusMethodInfo *
1131 parse_data_get_method (ParseData *data,
1132 gboolean create_new)
1135 g_ptr_array_add (data->methods, g_new0 (GDBusMethodInfo, 1));
1136 return data->methods->pdata[data->methods->len - 1];
1139 static GDBusSignalInfo *
1140 parse_data_get_signal (ParseData *data,
1141 gboolean create_new)
1144 g_ptr_array_add (data->signals, g_new0 (GDBusSignalInfo, 1));
1145 return data->signals->pdata[data->signals->len - 1];
1148 static GDBusPropertyInfo *
1149 parse_data_get_property (ParseData *data,
1150 gboolean create_new)
1153 g_ptr_array_add (data->properties, g_new0 (GDBusPropertyInfo, 1));
1154 return data->properties->pdata[data->properties->len - 1];
1157 static GDBusInterfaceInfo *
1158 parse_data_get_interface (ParseData *data,
1159 gboolean create_new)
1162 g_ptr_array_add (data->interfaces, g_new0 (GDBusInterfaceInfo, 1));
1163 return data->interfaces->pdata[data->interfaces->len - 1];
1166 static GDBusNodeInfo *
1167 parse_data_get_node (ParseData *data,
1168 gboolean create_new)
1171 g_ptr_array_add (data->nodes, g_new0 (GDBusNodeInfo, 1));
1172 return data->nodes->pdata[data->nodes->len - 1];
1175 /* ---------------------------------------------------------------------------------------------------- */
1178 parse_data_new (void)
1182 data = g_new0 (ParseData, 1);
1184 /* initialize arrays */
1185 parse_data_steal_annotations (data, NULL);
1186 parse_data_steal_args (data, NULL);
1187 parse_data_steal_out_args (data, NULL);
1188 parse_data_steal_methods (data, NULL);
1189 parse_data_steal_signals (data, NULL);
1190 parse_data_steal_properties (data, NULL);
1191 parse_data_steal_interfaces (data, NULL);
1192 parse_data_steal_nodes (data, NULL);
1198 parse_data_free (ParseData *data)
1202 /* free stack of annotation arrays */
1203 for (l = data->annotations_stack; l != NULL; l = l->next)
1205 GPtrArray *annotations = l->data;
1206 g_ptr_array_foreach (annotations, (GFunc) g_dbus_annotation_info_unref, NULL);
1207 g_ptr_array_free (annotations, TRUE);
1209 g_slist_free (data->annotations_stack);
1211 /* free stack of interface arrays */
1212 for (l = data->interfaces_stack; l != NULL; l = l->next)
1214 GPtrArray *interfaces = l->data;
1215 g_ptr_array_foreach (interfaces, (GFunc) g_dbus_interface_info_unref, NULL);
1216 g_ptr_array_free (interfaces, TRUE);
1218 g_slist_free (data->interfaces_stack);
1220 /* free stack of node arrays */
1221 for (l = data->nodes_stack; l != NULL; l = l->next)
1223 GPtrArray *nodes = l->data;
1224 g_ptr_array_foreach (nodes, (GFunc) g_dbus_node_info_unref, NULL);
1225 g_ptr_array_free (nodes, TRUE);
1227 g_slist_free (data->nodes_stack);
1229 /* free arrays (data->annotations, data->interfaces and data->nodes have been freed above) */
1230 parse_data_free_args (data);
1231 parse_data_free_out_args (data);
1232 parse_data_free_methods (data);
1233 parse_data_free_signals (data);
1234 parse_data_free_properties (data);
1235 parse_data_free_interfaces (data);
1236 parse_data_free_annotations (data);
1237 parse_data_free_nodes (data);
1242 /* ---------------------------------------------------------------------------------------------------- */
1245 parser_start_element (GMarkupParseContext *context,
1246 const gchar *element_name,
1247 const gchar **attribute_names,
1248 const gchar **attribute_values,
1252 ParseData *data = user_data;
1256 const gchar *access;
1257 const gchar *direction;
1266 stack = (GSList *) g_markup_parse_context_get_element_stack (context);
1268 /* ---------------------------------------------------------------------------------------------------- */
1269 if (strcmp (element_name, "node") == 0)
1271 if (!(g_slist_length (stack) >= 1 || strcmp (stack->next->data, "node") != 0))
1273 g_set_error_literal (error,
1275 G_MARKUP_ERROR_INVALID_CONTENT,
1276 "<node> elements can only be top-level or embedded in other <node> elements");
1280 if (!g_markup_collect_attributes (element_name,
1284 G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL, "name", &name,
1285 /* some hand-written introspection XML documents use this */
1286 G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL, "xmlns:doc", NULL,
1287 G_MARKUP_COLLECT_INVALID))
1290 g_dbus_node_info_set (data,
1291 parse_data_get_node (data, TRUE),
1297 /* push the currently retrieved interfaces and nodes on the stack and prepare new arrays */
1298 data->interfaces_stack = g_slist_prepend (data->interfaces_stack, data->interfaces);
1299 data->interfaces = NULL;
1300 parse_data_steal_interfaces (data, NULL);
1302 data->nodes_stack = g_slist_prepend (data->nodes_stack, data->nodes);
1304 parse_data_steal_nodes (data, NULL);
1307 /* ---------------------------------------------------------------------------------------------------- */
1308 else if (strcmp (element_name, "interface") == 0)
1310 if (g_slist_length (stack) < 2 || strcmp (stack->next->data, "node") != 0)
1312 g_set_error_literal (error,
1314 G_MARKUP_ERROR_INVALID_CONTENT,
1315 "<interface> elements can only be embedded in <node> elements");
1319 if (!g_markup_collect_attributes (element_name,
1323 G_MARKUP_COLLECT_STRING, "name", &name,
1324 /* seen in the wild */
1325 G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL, "version", NULL,
1326 G_MARKUP_COLLECT_INVALID))
1329 g_dbus_interface_info_set (data,
1330 parse_data_get_interface (data, TRUE),
1338 /* ---------------------------------------------------------------------------------------------------- */
1339 else if (strcmp (element_name, "method") == 0)
1341 if (g_slist_length (stack) < 2 || strcmp (stack->next->data, "interface") != 0)
1343 g_set_error_literal (error,
1345 G_MARKUP_ERROR_INVALID_CONTENT,
1346 "<method> elements can only be embedded in <interface> elements");
1350 if (!g_markup_collect_attributes (element_name,
1354 G_MARKUP_COLLECT_STRING, "name", &name,
1355 /* seen in the wild */
1356 G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL, "version", NULL,
1357 G_MARKUP_COLLECT_INVALID))
1360 g_dbus_method_info_set (data,
1361 parse_data_get_method (data, TRUE),
1370 /* ---------------------------------------------------------------------------------------------------- */
1371 else if (strcmp (element_name, "signal") == 0)
1373 if (g_slist_length (stack) < 2 || strcmp (stack->next->data, "interface") != 0)
1375 g_set_error_literal (error,
1377 G_MARKUP_ERROR_INVALID_CONTENT,
1378 "<signal> elements can only be embedded in <interface> elements");
1382 if (!g_markup_collect_attributes (element_name,
1386 G_MARKUP_COLLECT_STRING, "name", &name,
1387 G_MARKUP_COLLECT_INVALID))
1390 g_dbus_signal_info_set (data,
1391 parse_data_get_signal (data, TRUE),
1399 /* ---------------------------------------------------------------------------------------------------- */
1400 else if (strcmp (element_name, "property") == 0)
1402 GDBusPropertyInfoFlags flags;
1404 if (g_slist_length (stack) < 2 || strcmp (stack->next->data, "interface") != 0)
1406 g_set_error_literal (error,
1408 G_MARKUP_ERROR_INVALID_CONTENT,
1409 "<property> elements can only be embedded in <interface> elements");
1413 if (!g_markup_collect_attributes (element_name,
1417 G_MARKUP_COLLECT_STRING, "name", &name,
1418 G_MARKUP_COLLECT_STRING, "type", &type,
1419 G_MARKUP_COLLECT_STRING, "access", &access,
1420 G_MARKUP_COLLECT_INVALID))
1423 if (strcmp (access, "read") == 0)
1424 flags = G_DBUS_PROPERTY_INFO_FLAGS_READABLE;
1425 else if (strcmp (access, "write") == 0)
1426 flags = G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE;
1427 else if (strcmp (access, "readwrite") == 0)
1428 flags = G_DBUS_PROPERTY_INFO_FLAGS_READABLE | G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE;
1433 G_MARKUP_ERROR_INVALID_CONTENT,
1434 "Unknown value '%s' of access attribute for element <property>",
1439 g_dbus_property_info_set (data,
1440 parse_data_get_property (data, TRUE),
1447 /* ---------------------------------------------------------------------------------------------------- */
1448 else if (strcmp (element_name, "arg") == 0)
1453 if (g_slist_length (stack) < 2 ||
1454 (strcmp (stack->next->data, "method") != 0 &&
1455 strcmp (stack->next->data, "signal") != 0))
1457 g_set_error_literal (error,
1459 G_MARKUP_ERROR_INVALID_CONTENT,
1460 "<arg> elements can only be embedded in <method> or <signal> elements");
1464 if (!g_markup_collect_attributes (element_name,
1468 G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL, "name", &name,
1469 G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL, "direction", &direction,
1470 G_MARKUP_COLLECT_STRING, "type", &type,
1471 G_MARKUP_COLLECT_INVALID))
1474 if (strcmp (stack->next->data, "method") == 0)
1478 if (direction != NULL)
1480 if (strcmp (direction, "in") == 0)
1482 else if (strcmp (direction, "out") == 0)
1488 G_MARKUP_ERROR_INVALID_CONTENT,
1489 "Unknown value '%s' of direction attribute",
1495 if (is_in && strcmp (stack->next->data, "signal") == 0)
1497 g_set_error_literal (error,
1499 G_MARKUP_ERROR_INVALID_CONTENT,
1500 "Only direction 'out' is allowed for <arg> elements embedded in <signal>");
1505 name_to_use = g_strdup_printf ("arg_%d", data->num_args);
1507 name_to_use = g_strdup (name);
1512 g_dbus_arg_info_set (data,
1513 parse_data_get_arg (data, TRUE),
1517 data->last_arg_was_in = TRUE;
1521 g_dbus_arg_info_set (data,
1522 parse_data_get_out_arg (data, TRUE),
1526 data->last_arg_was_in = FALSE;
1530 g_free (name_to_use);
1532 /* ---------------------------------------------------------------------------------------------------- */
1533 else if (strcmp (element_name, "annotation") == 0)
1535 if (g_slist_length (stack) < 2 ||
1536 (strcmp (stack->next->data, "node") != 0 &&
1537 strcmp (stack->next->data, "interface") != 0 &&
1538 strcmp (stack->next->data, "signal") != 0 &&
1539 strcmp (stack->next->data, "method") != 0 &&
1540 strcmp (stack->next->data, "property") != 0 &&
1541 strcmp (stack->next->data, "arg") != 0 &&
1542 strcmp (stack->next->data, "annotation") != 0))
1544 g_set_error_literal (error,
1546 G_MARKUP_ERROR_INVALID_CONTENT,
1547 "<annotation> elements can only be embedded in <node>, <interface>, <signal>, <method>, <property>, <arg> or <annotation> elements");
1551 if (!g_markup_collect_attributes (element_name,
1555 G_MARKUP_COLLECT_STRING, "name", &name,
1556 G_MARKUP_COLLECT_STRING, "value", &value,
1557 G_MARKUP_COLLECT_INVALID))
1560 g_dbus_annotation_info_set (data,
1561 parse_data_get_annotation (data, TRUE),
1566 /* ---------------------------------------------------------------------------------------------------- */
1569 /* don't bail on unknown elements; just ignore them */
1571 /* ---------------------------------------------------------------------------------------------------- */
1573 /* push the currently retrieved annotations on the stack and prepare a new one */
1574 data->annotations_stack = g_slist_prepend (data->annotations_stack, data->annotations);
1575 data->annotations = NULL;
1576 parse_data_steal_annotations (data, NULL);
1582 /* ---------------------------------------------------------------------------------------------------- */
1584 static GDBusAnnotationInfo **
1585 steal_annotations (ParseData *data)
1587 return parse_data_steal_annotations (data, NULL);
1592 parser_end_element (GMarkupParseContext *context,
1593 const gchar *element_name,
1597 ParseData *data = user_data;
1598 gboolean have_popped_annotations;
1600 have_popped_annotations = FALSE;
1602 if (strcmp (element_name, "node") == 0)
1605 guint num_interfaces;
1606 GDBusNodeInfo **nodes;
1607 GDBusInterfaceInfo **interfaces;
1609 nodes = parse_data_steal_nodes (data, &num_nodes);
1610 interfaces = parse_data_steal_interfaces (data, &num_interfaces);
1612 /* destroy the nodes, interfaces for scope we're exiting and and pop the nodes, interfaces from the
1613 * scope we're reentering
1615 parse_data_free_interfaces (data);
1616 data->interfaces = (GPtrArray *) data->interfaces_stack->data;
1617 data->interfaces_stack = g_slist_remove (data->interfaces_stack, data->interfaces_stack->data);
1619 parse_data_free_nodes (data);
1620 data->nodes = (GPtrArray *) data->nodes_stack->data;
1621 data->nodes_stack = g_slist_remove (data->nodes_stack, data->nodes_stack->data);
1623 g_dbus_node_info_set (data,
1624 parse_data_get_node (data, FALSE),
1628 steal_annotations (data));
1631 else if (strcmp (element_name, "interface") == 0)
1635 guint num_properties;
1636 GDBusMethodInfo **methods;
1637 GDBusSignalInfo **signals;
1638 GDBusPropertyInfo **properties;
1640 methods = parse_data_steal_methods (data, &num_methods);
1641 signals = parse_data_steal_signals (data, &num_signals);
1642 properties = parse_data_steal_properties (data, &num_properties);
1644 g_dbus_interface_info_set (data,
1645 parse_data_get_interface (data, FALSE),
1650 steal_annotations (data));
1653 else if (strcmp (element_name, "method") == 0)
1657 GDBusArgInfo **in_args;
1658 GDBusArgInfo **out_args;
1660 in_args = parse_data_steal_args (data, &in_num_args);
1661 out_args = parse_data_steal_out_args (data, &out_num_args);
1663 g_dbus_method_info_set (data,
1664 parse_data_get_method (data, FALSE),
1668 steal_annotations (data));
1670 else if (strcmp (element_name, "signal") == 0)
1673 GDBusArgInfo **args;
1675 args = parse_data_steal_out_args (data, &num_args);
1677 g_dbus_signal_info_set (data,
1678 parse_data_get_signal (data, FALSE),
1681 steal_annotations (data));
1683 else if (strcmp (element_name, "property") == 0)
1685 g_dbus_property_info_set (data,
1686 parse_data_get_property (data, FALSE),
1689 G_DBUS_PROPERTY_INFO_FLAGS_NONE,
1690 steal_annotations (data));
1692 else if (strcmp (element_name, "arg") == 0)
1694 g_dbus_arg_info_set (data,
1695 data->last_arg_was_in ? parse_data_get_arg (data, FALSE) : parse_data_get_out_arg (data, FALSE),
1698 steal_annotations (data));
1700 else if (strcmp (element_name, "annotation") == 0)
1702 GDBusAnnotationInfo **embedded_annotations;
1704 embedded_annotations = steal_annotations (data);
1706 /* destroy the annotations for scope we're exiting and and pop the annotations from the scope we're reentering */
1707 parse_data_free_annotations (data);
1708 data->annotations = (GPtrArray *) data->annotations_stack->data;
1709 data->annotations_stack = g_slist_remove (data->annotations_stack, data->annotations_stack->data);
1711 have_popped_annotations = TRUE;
1713 g_dbus_annotation_info_set (data,
1714 parse_data_get_annotation (data, FALSE),
1717 embedded_annotations);
1721 /* don't bail on unknown elements; just ignore them */
1724 if (!have_popped_annotations)
1726 /* destroy the annotations for scope we're exiting and and pop the annotations from the scope we're reentering */
1727 parse_data_free_annotations (data);
1728 data->annotations = (GPtrArray *) data->annotations_stack->data;
1729 data->annotations_stack = g_slist_remove (data->annotations_stack, data->annotations_stack->data);
1733 /* ---------------------------------------------------------------------------------------------------- */
1736 parser_error (GMarkupParseContext *context,
1743 g_markup_parse_context_get_position (context, &line_number, &char_number);
1745 g_prefix_error (&error, "%d:%d: ",
1750 /* ---------------------------------------------------------------------------------------------------- */
1753 * g_dbus_node_info_new_for_xml:
1754 * @xml_data: Valid D-Bus introspection XML.
1755 * @error: Return location for error.
1757 * Parses @xml_data and returns a #GDBusNodeInfo representing the data.
1759 * The introspection XML must contain exactly one top-level
1762 * Note that this routine is using a
1763 * [GMarkup][glib-Simple-XML-Subset-Parser.description]-based
1764 * parser that only accepts a subset of valid XML documents.
1766 * Returns: A #GDBusNodeInfo structure or %NULL if @error is set. Free
1767 * with g_dbus_node_info_unref().
1772 g_dbus_node_info_new_for_xml (const gchar *xml_data,
1776 GMarkupParseContext *context;
1777 GMarkupParser *parser;
1780 GDBusNodeInfo **ughret;
1786 parser = g_new0 (GMarkupParser, 1);
1787 parser->start_element = parser_start_element;
1788 parser->end_element = parser_end_element;
1789 parser->error = parser_error;
1791 data = parse_data_new ();
1792 context = g_markup_parse_context_new (parser,
1793 G_MARKUP_IGNORE_QUALIFIED,
1795 (GDestroyNotify) parse_data_free);
1797 if (!g_markup_parse_context_parse (context,
1803 if (!g_markup_parse_context_end_parse (context, error))
1806 ughret = parse_data_steal_nodes (data, &num_nodes);
1814 G_MARKUP_ERROR_INVALID_CONTENT,
1815 "Expected a single node in introspection XML, found %d",
1819 for (n = 0; n < num_nodes; n++)
1821 g_dbus_node_info_unref (ughret[n]);
1831 if (context != NULL)
1832 g_markup_parse_context_free (context);
1837 /* ---------------------------------------------------------------------------------------------------- */
1840 * g_dbus_annotation_info_lookup:
1841 * @annotations: (array zero-terminated=1) (allow-none): A %NULL-terminated array of annotations or %NULL.
1842 * @name: The name of the annotation to look up.
1844 * Looks up the value of an annotation.
1846 * The cost of this function is O(n) in number of annotations.
1848 * Returns: The value or %NULL if not found. Do not free, it is owned by @annotations.
1853 g_dbus_annotation_info_lookup (GDBusAnnotationInfo **annotations,
1860 for (n = 0; annotations != NULL && annotations[n] != NULL; n++)
1862 if (g_strcmp0 (annotations[n]->key, name) == 0)
1864 ret = annotations[n]->value;
1873 /* ---------------------------------------------------------------------------------------------------- */
1875 G_LOCK_DEFINE_STATIC (info_cache_lock);
1881 /* gchar* -> GDBusMethodInfo* */
1882 GHashTable *method_name_to_data;
1884 /* gchar* -> GDBusMethodInfo* */
1885 GHashTable *signal_name_to_data;
1887 /* gchar* -> GDBusMethodInfo* */
1888 GHashTable *property_name_to_data;
1892 info_cache_free (InfoCacheEntry *cache)
1894 g_assert (cache->use_count == 0);
1895 g_hash_table_unref (cache->method_name_to_data);
1896 g_hash_table_unref (cache->signal_name_to_data);
1897 g_hash_table_unref (cache->property_name_to_data);
1898 g_slice_free (InfoCacheEntry, cache);
1901 /* maps from GDBusInterfaceInfo* to InfoCacheEntry* */
1902 static GHashTable *info_cache = NULL;
1904 /* ---------------------------------------------------------------------------------------------------- */
1907 * g_dbus_interface_info_lookup_method:
1908 * @info: A #GDBusInterfaceInfo.
1909 * @name: A D-Bus method name (typically in CamelCase)
1911 * Looks up information about a method.
1913 * The cost of this function is O(n) in number of methods unless
1914 * g_dbus_interface_info_cache_build() has been used on @info.
1916 * Returns: (transfer none): A #GDBusMethodInfo or %NULL if not found. Do not free, it is owned by @info.
1921 g_dbus_interface_info_lookup_method (GDBusInterfaceInfo *info,
1925 GDBusMethodInfo *result;
1927 G_LOCK (info_cache_lock);
1928 if (G_LIKELY (info_cache != NULL))
1930 InfoCacheEntry *cache;
1931 cache = g_hash_table_lookup (info_cache, info);
1932 if (G_LIKELY (cache != NULL))
1934 result = g_hash_table_lookup (cache->method_name_to_data, name);
1935 G_UNLOCK (info_cache_lock);
1939 G_UNLOCK (info_cache_lock);
1941 for (n = 0; info->methods != NULL && info->methods[n] != NULL; n++)
1943 GDBusMethodInfo *i = info->methods[n];
1945 if (g_strcmp0 (i->name, name) == 0)
1958 /* ---------------------------------------------------------------------------------------------------- */
1961 * g_dbus_interface_info_lookup_signal:
1962 * @info: A #GDBusInterfaceInfo.
1963 * @name: A D-Bus signal name (typically in CamelCase)
1965 * Looks up information about a signal.
1967 * The cost of this function is O(n) in number of signals unless
1968 * g_dbus_interface_info_cache_build() has been used on @info.
1970 * Returns: (transfer none): A #GDBusSignalInfo or %NULL if not found. Do not free, it is owned by @info.
1975 g_dbus_interface_info_lookup_signal (GDBusInterfaceInfo *info,
1979 GDBusSignalInfo *result;
1981 G_LOCK (info_cache_lock);
1982 if (G_LIKELY (info_cache != NULL))
1984 InfoCacheEntry *cache;
1985 cache = g_hash_table_lookup (info_cache, info);
1986 if (G_LIKELY (cache != NULL))
1988 result = g_hash_table_lookup (cache->signal_name_to_data, name);
1989 G_UNLOCK (info_cache_lock);
1993 G_UNLOCK (info_cache_lock);
1995 for (n = 0; info->signals != NULL && info->signals[n] != NULL; n++)
1997 GDBusSignalInfo *i = info->signals[n];
1999 if (g_strcmp0 (i->name, name) == 0)
2012 /* ---------------------------------------------------------------------------------------------------- */
2015 * g_dbus_interface_info_lookup_property:
2016 * @info: A #GDBusInterfaceInfo.
2017 * @name: A D-Bus property name (typically in CamelCase).
2019 * Looks up information about a property.
2021 * The cost of this function is O(n) in number of properties unless
2022 * g_dbus_interface_info_cache_build() has been used on @info.
2024 * Returns: (transfer none): A #GDBusPropertyInfo or %NULL if not found. Do not free, it is owned by @info.
2029 g_dbus_interface_info_lookup_property (GDBusInterfaceInfo *info,
2033 GDBusPropertyInfo *result;
2035 G_LOCK (info_cache_lock);
2036 if (G_LIKELY (info_cache != NULL))
2038 InfoCacheEntry *cache;
2039 cache = g_hash_table_lookup (info_cache, info);
2040 if (G_LIKELY (cache != NULL))
2042 result = g_hash_table_lookup (cache->property_name_to_data, name);
2043 G_UNLOCK (info_cache_lock);
2047 G_UNLOCK (info_cache_lock);
2049 for (n = 0; info->properties != NULL && info->properties[n] != NULL; n++)
2051 GDBusPropertyInfo *i = info->properties[n];
2053 if (g_strcmp0 (i->name, name) == 0)
2066 /* ---------------------------------------------------------------------------------------------------- */
2069 * g_dbus_interface_info_cache_build:
2070 * @info: A #GDBusInterfaceInfo.
2072 * Builds a lookup-cache to speed up
2073 * g_dbus_interface_info_lookup_method(),
2074 * g_dbus_interface_info_lookup_signal() and
2075 * g_dbus_interface_info_lookup_property().
2077 * If this has already been called with @info, the existing cache is
2078 * used and its use count is increased.
2080 * Note that @info cannot be modified until
2081 * g_dbus_interface_info_cache_release() is called.
2086 g_dbus_interface_info_cache_build (GDBusInterfaceInfo *info)
2088 InfoCacheEntry *cache;
2091 G_LOCK (info_cache_lock);
2092 if (info_cache == NULL)
2093 info_cache = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, (GDestroyNotify) info_cache_free);
2094 cache = g_hash_table_lookup (info_cache, info);
2097 cache->use_count += 1;
2100 cache = g_slice_new0 (InfoCacheEntry);
2101 cache->use_count = 1;
2102 cache->method_name_to_data = g_hash_table_new (g_str_hash, g_str_equal);
2103 cache->signal_name_to_data = g_hash_table_new (g_str_hash, g_str_equal);
2104 cache->property_name_to_data = g_hash_table_new (g_str_hash, g_str_equal);
2105 for (n = 0; info->methods != NULL && info->methods[n] != NULL; n++)
2106 g_hash_table_insert (cache->method_name_to_data, info->methods[n]->name, info->methods[n]);
2107 for (n = 0; info->signals != NULL && info->signals[n] != NULL; n++)
2108 g_hash_table_insert (cache->signal_name_to_data, info->signals[n]->name, info->signals[n]);
2109 for (n = 0; info->properties != NULL && info->properties[n] != NULL; n++)
2110 g_hash_table_insert (cache->property_name_to_data, info->properties[n]->name, info->properties[n]);
2111 g_hash_table_insert (info_cache, info, cache);
2113 G_UNLOCK (info_cache_lock);
2117 * g_dbus_interface_info_cache_release:
2118 * @info: A GDBusInterfaceInfo
2120 * Decrements the usage count for the cache for @info built by
2121 * g_dbus_interface_info_cache_build() (if any) and frees the
2122 * resources used by the cache if the usage count drops to zero.
2127 g_dbus_interface_info_cache_release (GDBusInterfaceInfo *info)
2129 InfoCacheEntry *cache;
2131 G_LOCK (info_cache_lock);
2132 if (G_UNLIKELY (info_cache == NULL))
2134 g_warning ("%s called for interface %s but there is no cache", info->name, G_STRFUNC);
2138 cache = g_hash_table_lookup (info_cache, info);
2139 if (G_UNLIKELY (cache == NULL))
2141 g_warning ("%s called for interface %s but there is no cache entry", info->name, G_STRFUNC);
2144 cache->use_count -= 1;
2145 if (cache->use_count == 0)
2147 g_hash_table_remove (info_cache, info);
2148 /* could nuke info_cache itself if empty */
2151 G_UNLOCK (info_cache_lock);
2155 /* ---------------------------------------------------------------------------------------------------- */
2158 * g_dbus_node_info_lookup_interface:
2159 * @info: A #GDBusNodeInfo.
2160 * @name: A D-Bus interface name.
2162 * Looks up information about an interface.
2164 * The cost of this function is O(n) in number of interfaces.
2166 * Returns: (transfer none): A #GDBusInterfaceInfo or %NULL if not found. Do not free, it is owned by @info.
2170 GDBusInterfaceInfo *
2171 g_dbus_node_info_lookup_interface (GDBusNodeInfo *info,
2175 GDBusInterfaceInfo *result;
2177 for (n = 0; info->interfaces != NULL && info->interfaces[n] != NULL; n++)
2179 GDBusInterfaceInfo *i = info->interfaces[n];
2181 if (g_strcmp0 (i->name, name) == 0)