gdbusintrospection.c 65.9 KB
Newer Older
1 2
/* GDBus - GLib D-Bus Library
 *
3
 * Copyright (C) 2008-2010 Red Hat, Inc.
4 5 6 7
 *
 * 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
8
 * version 2.1 of the License, or (at your option) any later version.
9 10 11 12 13 14 15
 *
 * 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 21 22 23
 *
 * Author: David Zeuthen <davidz@redhat.com>
 */

#include "config.h"

#include <stdlib.h>
David Zeuthen's avatar
David Zeuthen committed
24
#include <string.h>
25 26 27

#include "gdbusintrospection.h"

David Zeuthen's avatar
David Zeuthen committed
28 29
#include "glibintl.h"

30 31
/**
 * SECTION:gdbusintrospection
32 33
 * @title: D-Bus Introspection Data
 * @short_description: Node and interface description data structures
David Zeuthen's avatar
David Zeuthen committed
34
 * @include: gio/gio.h
35 36
 *
 * Various data structures and convenience routines to parse and
37 38 39
 * generate D-Bus introspection XML. Introspection information is
 * 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)
42 43 44 45
 */

/* ---------------------------------------------------------------------------------------------------- */

46 47
#define _MY_DEFINE_BOXED_TYPE(TypeName, type_name) \
  G_DEFINE_BOXED_TYPE (TypeName, type_name, type_name##_ref, type_name##_unref)
48

49 50 51 52 53 54 55 56 57
_MY_DEFINE_BOXED_TYPE (GDBusNodeInfo,       g_dbus_node_info)
_MY_DEFINE_BOXED_TYPE (GDBusInterfaceInfo,  g_dbus_interface_info)
_MY_DEFINE_BOXED_TYPE (GDBusMethodInfo,     g_dbus_method_info)
_MY_DEFINE_BOXED_TYPE (GDBusSignalInfo,     g_dbus_signal_info)
_MY_DEFINE_BOXED_TYPE (GDBusPropertyInfo,   g_dbus_property_info)
_MY_DEFINE_BOXED_TYPE (GDBusArgInfo,        g_dbus_arg_info)
_MY_DEFINE_BOXED_TYPE (GDBusAnnotationInfo, g_dbus_annotation_info)

#undef _MY_DEFINE_BOXED_TYPE
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

/* ---------------------------------------------------------------------------------------------------- */

typedef struct
{
  /* stuff we are currently collecting */
  GPtrArray *args;
  GPtrArray *out_args;
  GPtrArray *methods;
  GPtrArray *signals;
  GPtrArray *properties;
  GPtrArray *interfaces;
  GPtrArray *nodes;
  GPtrArray *annotations;

  /* A list of GPtrArray's containing annotations */
  GSList *annotations_stack;

  /* A list of GPtrArray's containing interfaces */
  GSList *interfaces_stack;

  /* A list of GPtrArray's containing nodes */
  GSList *nodes_stack;

  /* Whether the direction was "in" for last parsed arg */
  gboolean last_arg_was_in;

  /* Number of args currently being collected; used for assigning
   * names to args without a "name" attribute
   */
  guint num_args;

} ParseData;

/* ---------------------------------------------------------------------------------------------------- */

/**
 * g_dbus_node_info_ref:
 * @info: A #GDBusNodeInfo
 *
 * If @info is statically allocated does nothing. Otherwise increases
 * the reference count.
 *
 * Returns: The same @info.
102 103
 *
 * Since: 2.26
104 105 106 107
 */
GDBusNodeInfo *
g_dbus_node_info_ref (GDBusNodeInfo *info)
{
108
  if (g_atomic_int_get (&info->ref_count) == -1)
109 110 111 112 113 114 115 116 117 118 119 120 121
    return info;
  g_atomic_int_inc (&info->ref_count);
  return info;
}

/**
 * g_dbus_interface_info_ref:
 * @info: A #GDBusInterfaceInfo
 *
 * If @info is statically allocated does nothing. Otherwise increases
 * the reference count.
 *
 * Returns: The same @info.
122 123
 *
 * Since: 2.26
124 125 126 127
 */
GDBusInterfaceInfo *
g_dbus_interface_info_ref (GDBusInterfaceInfo *info)
{
128
  if (g_atomic_int_get (&info->ref_count) == -1)
129 130 131 132 133 134 135 136 137 138 139 140 141
    return info;
  g_atomic_int_inc (&info->ref_count);
  return info;
}

/**
 * g_dbus_method_info_ref:
 * @info: A #GDBusMethodInfo
 *
 * If @info is statically allocated does nothing. Otherwise increases
 * the reference count.
 *
 * Returns: The same @info.
142 143
 *
 * Since: 2.26
144 145 146 147
 */
GDBusMethodInfo *
g_dbus_method_info_ref (GDBusMethodInfo *info)
{
148
  if (g_atomic_int_get (&info->ref_count) == -1)
149 150 151 152 153 154 155 156 157 158 159 160 161
    return info;
  g_atomic_int_inc (&info->ref_count);
  return info;
}

/**
 * g_dbus_signal_info_ref:
 * @info: A #GDBusSignalInfo
 *
 * If @info is statically allocated does nothing. Otherwise increases
 * the reference count.
 *
 * Returns: The same @info.
162 163
 *
 * Since: 2.26
164 165 166 167
 */
GDBusSignalInfo *
g_dbus_signal_info_ref (GDBusSignalInfo *info)
{
168
  if (g_atomic_int_get (&info->ref_count) == -1)
169 170 171 172 173 174 175 176 177 178 179 180 181
    return info;
  g_atomic_int_inc (&info->ref_count);
  return info;
}

/**
 * g_dbus_property_info_ref:
 * @info: A #GDBusPropertyInfo
 *
 * If @info is statically allocated does nothing. Otherwise increases
 * the reference count.
 *
 * Returns: The same @info.
182 183
 *
 * Since: 2.26
184 185 186 187
 */
GDBusPropertyInfo *
g_dbus_property_info_ref (GDBusPropertyInfo *info)
{
188
  if (g_atomic_int_get (&info->ref_count) == -1)
189 190 191 192 193 194 195 196 197 198 199 200 201
    return info;
  g_atomic_int_inc (&info->ref_count);
  return info;
}

/**
 * g_dbus_arg_info_ref:
 * @info: A #GDBusArgInfo
 *
 * If @info is statically allocated does nothing. Otherwise increases
 * the reference count.
 *
 * Returns: The same @info.
202 203
 *
 * Since: 2.26
204 205 206 207
 */
GDBusArgInfo *
g_dbus_arg_info_ref (GDBusArgInfo *info)
{
208
  if (g_atomic_int_get (&info->ref_count) == -1)
209 210 211 212 213 214
    return info;
  g_atomic_int_inc (&info->ref_count);
  return info;
}

/**
215
 * g_dbus_annotation_info_ref:
216 217 218 219 220 221
 * @info: A #GDBusNodeInfo
 *
 * If @info is statically allocated does nothing. Otherwise increases
 * the reference count.
 *
 * Returns: The same @info.
222 223
 *
 * Since: 2.26
224 225 226 227
 */
GDBusAnnotationInfo *
g_dbus_annotation_info_ref (GDBusAnnotationInfo *info)
{
228
  if (g_atomic_int_get (&info->ref_count) == -1)
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    return info;
  g_atomic_int_inc (&info->ref_count);
  return info;
}

/* ---------------------------------------------------------------------------------------------------- */

static void
free_null_terminated_array (gpointer array, GDestroyNotify unref_func)
{
  guint n;
  gpointer *p = array;
  if (p == NULL)
    return;
  for (n = 0; p[n] != NULL; n++)
    unref_func (p[n]);
  g_free (p);
}

/**
 * g_dbus_annotation_info_unref:
 * @info: A #GDBusAnnotationInfo.
 *
 * If @info is statically allocated, does nothing. Otherwise decreases
 * the reference count of @info. When its reference count drops to 0,
 * the memory used is freed.
255 256
 *
 * Since: 2.26
257 258 259 260
 */
void
g_dbus_annotation_info_unref (GDBusAnnotationInfo *info)
{
261
  if (g_atomic_int_get (&info->ref_count) == -1)
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
    return;
  if (g_atomic_int_dec_and_test (&info->ref_count))
    {
      g_free (info->key);
      g_free (info->value);
      free_null_terminated_array (info->annotations, (GDestroyNotify) g_dbus_annotation_info_unref);
      g_free (info);
    }
}

/**
 * g_dbus_arg_info_unref:
 * @info: A #GDBusArgInfo.
 *
 * If @info is statically allocated, does nothing. Otherwise decreases
 * the reference count of @info. When its reference count drops to 0,
 * the memory used is freed.
279 280
 *
 * Since: 2.26
281 282 283 284
 */
void
g_dbus_arg_info_unref (GDBusArgInfo *info)
{
285
  if (g_atomic_int_get (&info->ref_count) == -1)
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
    return;
  if (g_atomic_int_dec_and_test (&info->ref_count))
    {
      g_free (info->name);
      g_free (info->signature);
      free_null_terminated_array (info->annotations, (GDestroyNotify) g_dbus_annotation_info_unref);
      g_free (info);
    }
}

/**
 * g_dbus_method_info_unref:
 * @info: A #GDBusMethodInfo.
 *
 * If @info is statically allocated, does nothing. Otherwise decreases
 * the reference count of @info. When its reference count drops to 0,
 * the memory used is freed.
303 304
 *
 * Since: 2.26
305 306 307 308
 */
void
g_dbus_method_info_unref (GDBusMethodInfo *info)
{
309
  if (g_atomic_int_get (&info->ref_count) == -1)
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
    return;
  if (g_atomic_int_dec_and_test (&info->ref_count))
    {
      g_free (info->name);
      free_null_terminated_array (info->in_args, (GDestroyNotify) g_dbus_arg_info_unref);
      free_null_terminated_array (info->out_args, (GDestroyNotify) g_dbus_arg_info_unref);
      free_null_terminated_array (info->annotations, (GDestroyNotify) g_dbus_annotation_info_unref);
      g_free (info);
    }
}

/**
 * g_dbus_signal_info_unref:
 * @info: A #GDBusSignalInfo.
 *
 * If @info is statically allocated, does nothing. Otherwise decreases
 * the reference count of @info. When its reference count drops to 0,
 * the memory used is freed.
328 329
 *
 * Since: 2.26
330 331 332 333
 */
void
g_dbus_signal_info_unref (GDBusSignalInfo *info)
{
334
  if (g_atomic_int_get (&info->ref_count) == -1)
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
    return;
  if (g_atomic_int_dec_and_test (&info->ref_count))
    {
      g_free (info->name);
      free_null_terminated_array (info->args, (GDestroyNotify) g_dbus_arg_info_unref);
      free_null_terminated_array (info->annotations, (GDestroyNotify) g_dbus_annotation_info_unref);
      g_free (info);
    }
}

/**
 * g_dbus_property_info_unref:
 * @info: A #GDBusPropertyInfo.
 *
 * If @info is statically allocated, does nothing. Otherwise decreases
 * the reference count of @info. When its reference count drops to 0,
 * the memory used is freed.
352 353
 *
 * Since: 2.26
354 355 356 357
 */
void
g_dbus_property_info_unref (GDBusPropertyInfo *info)
{
358
  if (g_atomic_int_get (&info->ref_count) == -1)
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
    return;
  if (g_atomic_int_dec_and_test (&info->ref_count))
    {
      g_free (info->name);
      g_free (info->signature);
      free_null_terminated_array (info->annotations, (GDestroyNotify) g_dbus_annotation_info_unref);
      g_free (info);
    }
}

/**
 * g_dbus_interface_info_unref:
 * @info: A #GDBusInterfaceInfo.
 *
 * If @info is statically allocated, does nothing. Otherwise decreases
 * the reference count of @info. When its reference count drops to 0,
 * the memory used is freed.
376 377
 *
 * Since: 2.26
378 379 380 381
 */
void
g_dbus_interface_info_unref (GDBusInterfaceInfo *info)
{
382
  if (g_atomic_int_get (&info->ref_count) == -1)
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
    return;
  if (g_atomic_int_dec_and_test (&info->ref_count))
    {
      g_free (info->name);
      free_null_terminated_array (info->methods, (GDestroyNotify) g_dbus_method_info_unref);
      free_null_terminated_array (info->signals, (GDestroyNotify) g_dbus_signal_info_unref);
      free_null_terminated_array (info->properties, (GDestroyNotify) g_dbus_property_info_unref);
      free_null_terminated_array (info->annotations, (GDestroyNotify) g_dbus_annotation_info_unref);
      g_free (info);
    }
}

/**
 * g_dbus_node_info_unref:
 * @info: A #GDBusNodeInfo.
 *
 * If @info is statically allocated, does nothing. Otherwise decreases
 * the reference count of @info. When its reference count drops to 0,
 * the memory used is freed.
402 403
 *
 * Since: 2.26
404 405 406 407
 */
void
g_dbus_node_info_unref (GDBusNodeInfo *info)
{
408
  if (g_atomic_int_get (&info->ref_count) == -1)
409 410 411 412 413 414 415 416 417 418 419 420 421 422
    return;
  if (g_atomic_int_dec_and_test (&info->ref_count))
    {
      g_free (info->path);
      free_null_terminated_array (info->interfaces, (GDestroyNotify) g_dbus_interface_info_unref);
      free_null_terminated_array (info->nodes, (GDestroyNotify) g_dbus_node_info_unref);
      free_null_terminated_array (info->annotations, (GDestroyNotify) g_dbus_annotation_info_unref);
      g_free (info);
    }
}

/* ---------------------------------------------------------------------------------------------------- */

static void
423 424 425 426
g_dbus_annotation_info_set (ParseData            *data,
                            GDBusAnnotationInfo  *info,
                            const gchar          *key,
                            const gchar          *value,
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 467 468 469 470 471 472 473
                            GDBusAnnotationInfo **embedded_annotations)
{
  info->ref_count = 1;

  if (key != NULL)
    info->key = g_strdup (key);

  if (value != NULL)
    info->value = g_strdup (value);

  if (embedded_annotations != NULL)
    info->annotations = embedded_annotations;
}

static void
g_dbus_arg_info_set (ParseData            *data,
                     GDBusArgInfo         *info,
                     const gchar          *name,
                     const gchar          *signature,
                     GDBusAnnotationInfo **annotations)
{
  info->ref_count = 1;

  /* name may be NULL - TODO: compute name? */
  if (name != NULL)
    info->name = g_strdup (name);

  if (signature != NULL)
    info->signature = g_strdup (signature);

  if (annotations != NULL)
    info->annotations = annotations;
}

static void
g_dbus_method_info_set (ParseData            *data,
                        GDBusMethodInfo      *info,
                        const gchar          *name,
                        GDBusArgInfo        **in_args,
                        GDBusArgInfo        **out_args,
                        GDBusAnnotationInfo **annotations)
{
  info->ref_count = 1;

  if (name != NULL)
    info->name = g_strdup (name);

474 475
  if (in_args != NULL)
    info->in_args = in_args;
476

477 478
  if (out_args != NULL)
    info->out_args = out_args;
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495

  if (annotations != NULL)
    info->annotations = annotations;
}

static void
g_dbus_signal_info_set (ParseData            *data,
                        GDBusSignalInfo      *info,
                        const gchar          *name,
                        GDBusArgInfo        **args,
                        GDBusAnnotationInfo **annotations)
{
  info->ref_count = 1;

  if (name != NULL)
    info->name = g_strdup (name);

496 497
  if (args != NULL)
    info->args = args;
498 499

  if (annotations != NULL)
500
    info->annotations = annotations;
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
}

static void
g_dbus_property_info_set (ParseData               *data,
                          GDBusPropertyInfo       *info,
                          const gchar             *name,
                          const gchar             *signature,
                          GDBusPropertyInfoFlags   flags,
                          GDBusAnnotationInfo    **annotations)
{
  info->ref_count = 1;

  if (name != NULL)
    info->name = g_strdup (name);

  if (flags != G_DBUS_PROPERTY_INFO_FLAGS_NONE)
    info->flags = flags;

  if (signature != NULL)
520
    info->signature = g_strdup (signature);
521 522

  if (annotations != NULL)
523
    info->annotations = annotations;
524 525 526 527 528 529 530 531 532 533 534 535 536 537
}

static void
g_dbus_interface_info_set (ParseData            *data,
                           GDBusInterfaceInfo   *info,
                           const gchar          *name,
                           GDBusMethodInfo     **methods,
                           GDBusSignalInfo     **signals,
                           GDBusPropertyInfo   **properties,
                           GDBusAnnotationInfo **annotations)
{
  info->ref_count = 1;

  if (name != NULL)
538
    info->name = g_strdup (name);
539

540 541
  if (methods != NULL)
    info->methods = methods;
542

543 544
  if (signals != NULL)
    info->signals = signals;
545

546 547
  if (properties != NULL)
    info->properties = properties;
548 549

  if (annotations != NULL)
550
    info->annotations = annotations;
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
}

static void
g_dbus_node_info_set (ParseData            *data,
                      GDBusNodeInfo        *info,
                      const gchar          *path,
                      GDBusInterfaceInfo  **interfaces,
                      GDBusNodeInfo       **nodes,
                      GDBusAnnotationInfo **annotations)
{
  info->ref_count = 1;

  if (path != NULL)
    {
      info->path = g_strdup (path);
      /* TODO: relative / absolute path snafu */
    }

569 570
  if (interfaces != NULL)
    info->interfaces = interfaces;
571

572 573
  if (nodes != NULL)
    info->nodes = nodes;
574 575

  if (annotations != NULL)
576
    info->annotations = annotations;
577 578 579 580 581
}

/* ---------------------------------------------------------------------------------------------------- */

static void
582 583 584
g_dbus_annotation_info_generate_xml (GDBusAnnotationInfo *info,
                                     guint                indent,
                                     GString             *string_builder)
585
{
586
  gchar *tmp;
587 588
  guint n;

589 590 591 592 593 594
  tmp = g_markup_printf_escaped ("%*s<annotation name=\"%s\" value=\"%s\"",
                                 indent, "",
                                 info->key,
                                 info->value);
  g_string_append (string_builder, tmp);
  g_free (tmp);
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615

  if (info->annotations == NULL)
    {
      g_string_append (string_builder, "/>\n");
    }
  else
    {
      g_string_append (string_builder, ">\n");

      for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
        g_dbus_annotation_info_generate_xml (info->annotations[n],
                                             indent + 2,
                                             string_builder);

      g_string_append_printf (string_builder, "%*s</annotation>\n",
                              indent, "");
    }

}

static void
616 617 618 619
g_dbus_arg_info_generate_xml (GDBusArgInfo *info,
                              guint         indent,
                              const gchar  *extra_attributes,
                              GString      *string_builder)
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
{
  guint n;

  g_string_append_printf (string_builder, "%*s<arg type=\"%s\"",
                          indent, "",
                          info->signature);

  if (info->name != NULL)
    g_string_append_printf (string_builder, " name=\"%s\"", info->name);

  if (extra_attributes != NULL)
    g_string_append_printf (string_builder, " %s", extra_attributes);

  if (info->annotations == NULL)
    {
      g_string_append (string_builder, "/>\n");
    }
  else
    {
      g_string_append (string_builder, ">\n");

      for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
        g_dbus_annotation_info_generate_xml (info->annotations[n],
                                             indent + 2,
                                             string_builder);

      g_string_append_printf (string_builder, "%*s</arg>\n", indent, "");
    }

}

static void
652 653 654
g_dbus_method_info_generate_xml (GDBusMethodInfo *info,
                                 guint            indent,
                                 GString         *string_builder)
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 682 683 684 685 686 687 688 689 690 691
{
  guint n;

  g_string_append_printf (string_builder, "%*s<method name=\"%s\"",
                          indent, "",
                          info->name);

  if (info->annotations == NULL && info->in_args == NULL && info->out_args == NULL)
    {
      g_string_append (string_builder, "/>\n");
    }
  else
    {
      g_string_append (string_builder, ">\n");

      for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
        g_dbus_annotation_info_generate_xml (info->annotations[n],
                                             indent + 2,
                                             string_builder);

      for (n = 0; info->in_args != NULL && info->in_args[n] != NULL; n++)
        g_dbus_arg_info_generate_xml (info->in_args[n],
                                      indent + 2,
                                      "direction=\"in\"",
                                      string_builder);

      for (n = 0; info->out_args != NULL && info->out_args[n] != NULL; n++)
        g_dbus_arg_info_generate_xml (info->out_args[n],
                                      indent + 2,
                                      "direction=\"out\"",
                                      string_builder);

      g_string_append_printf (string_builder, "%*s</method>\n", indent, "");
    }
}

static void
692 693 694
g_dbus_signal_info_generate_xml (GDBusSignalInfo *info,
                                 guint            indent,
                                 GString         *string_builder)
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
{
  guint n;

  g_string_append_printf (string_builder, "%*s<signal name=\"%s\"",
                          indent, "",
                          info->name);

  if (info->annotations == NULL && info->args == NULL)
    {
      g_string_append (string_builder, "/>\n");
    }
  else
    {
      g_string_append (string_builder, ">\n");

      for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
        g_dbus_annotation_info_generate_xml (info->annotations[n],
                                             indent + 2,
                                             string_builder);

      for (n = 0; info->args != NULL && info->args[n] != NULL; n++)
        g_dbus_arg_info_generate_xml (info->args[n],
                                      indent + 2,
                                      NULL,
                                      string_builder);

      g_string_append_printf (string_builder, "%*s</signal>\n", indent, "");
    }
}

static void
726 727 728
g_dbus_property_info_generate_xml (GDBusPropertyInfo *info,
                                   guint              indent,
                                   GString           *string_builder)
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 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
{
  guint n;
  const gchar *access_string;

  if ((info->flags & G_DBUS_PROPERTY_INFO_FLAGS_READABLE) &&
      (info->flags & G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE))
    {
      access_string = "readwrite";
    }
  else if (info->flags & G_DBUS_PROPERTY_INFO_FLAGS_READABLE)
    {
      access_string = "read";
    }
  else if (info->flags & G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE)
    {
      access_string = "write";
    }
  else
    {
      g_assert_not_reached ();
    }

  g_string_append_printf (string_builder, "%*s<property type=\"%s\" name=\"%s\" access=\"%s\"",
                          indent, "",
                          info->signature,
                          info->name,
                          access_string);

  if (info->annotations == NULL)
    {
      g_string_append (string_builder, "/>\n");
    }
  else
    {
      g_string_append (string_builder, ">\n");

      for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
        g_dbus_annotation_info_generate_xml (info->annotations[n],
                                               indent + 2,
                                               string_builder);

      g_string_append_printf (string_builder, "%*s</property>\n", indent, "");
    }

}

/**
 * g_dbus_interface_info_generate_xml:
 * @info: A #GDBusNodeInfo
 * @indent: Indentation level.
779
 * @string_builder: A #GString to to append XML data to.
780 781 782 783 784
 *
 * Appends an XML representation of @info (and its children) to @string_builder.
 *
 * This function is typically used for generating introspection XML
 * documents at run-time for handling the
785
 * `org.freedesktop.DBus.Introspectable.Introspect`
786
 * method.
787 788
 *
 * Since: 2.26
789 790
 */
void
791 792 793
g_dbus_interface_info_generate_xml (GDBusInterfaceInfo *info,
                                    guint               indent,
                                    GString            *string_builder)
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
{
  guint n;

  g_string_append_printf (string_builder, "%*s<interface name=\"%s\">\n",
                          indent, "",
                          info->name);

  for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
    g_dbus_annotation_info_generate_xml (info->annotations[n],
                                         indent + 2,
                                         string_builder);

  for (n = 0; info->methods != NULL && info->methods[n] != NULL; n++)
    g_dbus_method_info_generate_xml (info->methods[n],
                                     indent + 2,
                                     string_builder);

  for (n = 0; info->signals != NULL && info->signals[n] != NULL; n++)
    g_dbus_signal_info_generate_xml (info->signals[n],
                                     indent + 2,
                                     string_builder);

  for (n = 0; info->properties != NULL && info->properties[n] != NULL; n++)
    g_dbus_property_info_generate_xml (info->properties[n],
                                       indent + 2,
                                       string_builder);

  g_string_append_printf (string_builder, "%*s</interface>\n", indent, "");
}

/**
 * g_dbus_node_info_generate_xml:
826
 * @info: A #GDBusNodeInfo.
827
 * @indent: Indentation level.
828
 * @string_builder: A #GString to to append XML data to.
829
 *
830
 * Appends an XML representation of @info (and its children) to @string_builder.
831 832
 *
 * This function is typically used for generating introspection XML documents at run-time for
833
 * handling the `org.freedesktop.DBus.Introspectable.Introspect`  method.
834 835
 *
 * Since: 2.26
836 837
 */
void
838 839 840
g_dbus_node_info_generate_xml (GDBusNodeInfo *info,
                               guint          indent,
                               GString       *string_builder)
841 842 843 844
{
  guint n;

  g_string_append_printf (string_builder, "%*s<node", indent, "");
845 846
  if (info->path != NULL)
    g_string_append_printf (string_builder, " name=\"%s\"", info->path);
847

848
  if (info->interfaces == NULL && info->nodes == NULL && info->annotations == NULL)
849 850 851 852 853 854 855
    {
      g_string_append (string_builder, "/>\n");
    }
  else
    {
      g_string_append (string_builder, ">\n");

856 857
      for (n = 0; info->annotations != NULL && info->annotations[n] != NULL; n++)
        g_dbus_annotation_info_generate_xml (info->annotations[n],
858 859 860
                                             indent + 2,
                                             string_builder);

861 862
      for (n = 0; info->interfaces != NULL && info->interfaces[n] != NULL; n++)
        g_dbus_interface_info_generate_xml (info->interfaces[n],
863 864 865
                                            indent + 2,
                                            string_builder);

866 867
      for (n = 0; info->nodes != NULL && info->nodes[n] != NULL; n++)
        g_dbus_node_info_generate_xml (info->nodes[n],
868 869 870 871 872 873 874 875 876 877
                                       indent + 2,
                                       string_builder);

      g_string_append_printf (string_builder, "%*s</node>\n", indent, "");
    }
}

/* ---------------------------------------------------------------------------------------------------- */

static GDBusAnnotationInfo **
878 879
parse_data_steal_annotations (ParseData *data,
                              guint     *out_num_elements)
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
{
  GDBusAnnotationInfo **ret;
  if (out_num_elements != NULL)
    *out_num_elements = data->annotations->len;
  if (data->annotations == NULL)
    ret = NULL;
  else
    {
      g_ptr_array_add (data->annotations, NULL);
      ret = (GDBusAnnotationInfo **) g_ptr_array_free (data->annotations, FALSE);
    }
  data->annotations = g_ptr_array_new ();
  return ret;
}

static GDBusArgInfo **
896 897
parse_data_steal_args (ParseData *data,
                       guint     *out_num_elements)
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
{
  GDBusArgInfo **ret;
  if (out_num_elements != NULL)
    *out_num_elements = data->args->len;
  if (data->args == NULL)
    ret = NULL;
  else
    {
      g_ptr_array_add (data->args, NULL);
      ret = (GDBusArgInfo **) g_ptr_array_free (data->args, FALSE);
    }
  data->args = g_ptr_array_new ();
  return ret;
}

static GDBusArgInfo **
914 915
parse_data_steal_out_args (ParseData *data,
                           guint     *out_num_elements)
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
{
  GDBusArgInfo **ret;
  if (out_num_elements != NULL)
    *out_num_elements = data->out_args->len;
  if (data->out_args == NULL)
    ret = NULL;
  else
    {
      g_ptr_array_add (data->out_args, NULL);
      ret = (GDBusArgInfo **) g_ptr_array_free (data->out_args, FALSE);
    }
  data->out_args = g_ptr_array_new ();
  return ret;
}

static GDBusMethodInfo **
932 933
parse_data_steal_methods (ParseData *data,
                          guint     *out_num_elements)
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
{
  GDBusMethodInfo **ret;
  if (out_num_elements != NULL)
    *out_num_elements = data->methods->len;
  if (data->methods == NULL)
    ret = NULL;
  else
    {
      g_ptr_array_add (data->methods, NULL);
      ret = (GDBusMethodInfo **) g_ptr_array_free (data->methods, FALSE);
    }
  data->methods = g_ptr_array_new ();
  return ret;
}

static GDBusSignalInfo **
950 951
parse_data_steal_signals (ParseData *data,
                          guint     *out_num_elements)
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
{
  GDBusSignalInfo **ret;
  if (out_num_elements != NULL)
    *out_num_elements = data->signals->len;
  if (data->signals == NULL)
    ret = NULL;
  else
    {
      g_ptr_array_add (data->signals, NULL);
      ret = (GDBusSignalInfo **) g_ptr_array_free (data->signals, FALSE);
    }
  data->signals = g_ptr_array_new ();
  return ret;
}

static GDBusPropertyInfo **
968 969
parse_data_steal_properties (ParseData *data,
                             guint     *out_num_elements)
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
{
  GDBusPropertyInfo **ret;
  if (out_num_elements != NULL)
    *out_num_elements = data->properties->len;
  if (data->properties == NULL)
    ret = NULL;
  else
    {
      g_ptr_array_add (data->properties, NULL);
      ret = (GDBusPropertyInfo **) g_ptr_array_free (data->properties, FALSE);
    }
  data->properties = g_ptr_array_new ();
  return ret;
}

static GDBusInterfaceInfo **
986 987
parse_data_steal_interfaces (ParseData *data,
                             guint     *out_num_elements)
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
{
  GDBusInterfaceInfo **ret;
  if (out_num_elements != NULL)
    *out_num_elements = data->interfaces->len;
  if (data->interfaces == NULL)
    ret = NULL;
  else
    {
      g_ptr_array_add (data->interfaces, NULL);
      ret = (GDBusInterfaceInfo **) g_ptr_array_free (data->interfaces, FALSE);
    }
  data->interfaces = g_ptr_array_new ();
  return ret;
}

static GDBusNodeInfo **
1004 1005
parse_data_steal_nodes (ParseData *data,
                        guint     *out_num_elements)
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
{
  GDBusNodeInfo **ret;
  if (out_num_elements != NULL)
    *out_num_elements = data->nodes->len;
  if (data->nodes == NULL)
    ret = NULL;
  else
    {
      g_ptr_array_add (data->nodes, NULL);
      ret = (GDBusNodeInfo **) g_ptr_array_free (data->nodes, FALSE);
    }
  data->nodes = g_ptr_array_new ();
  return ret;
}

/* ---------------------------------------------------------------------------------------------------- */

static void
parse_data_free_annotations (ParseData *data)
{
  if (data->annotations == NULL)
    return;
  g_ptr_array_foreach (data->annotations, (GFunc) g_dbus_annotation_info_unref, NULL);
  g_ptr_array_free (data->annotations, TRUE);
  data->annotations = NULL;
}

static void
parse_data_free_args (ParseData *data)
{
  if (data->args == NULL)
    return;
  g_ptr_array_foreach (data->args, (GFunc) g_dbus_arg_info_unref, NULL);
  g_ptr_array_free (data->args, TRUE);
  data->args = NULL;
}

static void
parse_data_free_out_args (ParseData *data)
{
  if (data->out_args == NULL)
    return;
  g_ptr_array_foreach (data->out_args, (GFunc) g_dbus_arg_info_unref, NULL);
  g_ptr_array_free (data->out_args, TRUE);
  data->out_args = NULL;
}

static void
parse_data_free_methods (ParseData *data)
{
  if (data->methods == NULL)
    return;
  g_ptr_array_foreach (data->methods, (GFunc) g_dbus_method_info_unref, NULL);
  g_ptr_array_free (data->methods, TRUE);
  data->methods = NULL;
}

static void
parse_data_free_signals (ParseData *data)
{
  if (data->signals == NULL)
    return;
  g_ptr_array_foreach (data->signals, (GFunc) g_dbus_signal_info_unref, NULL);
  g_ptr_array_free (data->signals, TRUE);
  data->signals = NULL;
}

static void
parse_data_free_properties (ParseData *data)
{
  if (data->properties == NULL)
    return;
  g_ptr_array_foreach (data->properties, (GFunc) g_dbus_property_info_unref, NULL);
  g_ptr_array_free (data->properties, TRUE);
  data->properties = NULL;
}

static void
parse_data_free_interfaces (ParseData *data)
{
  if (data->interfaces == NULL)
    return;
  g_ptr_array_foreach (data->interfaces, (GFunc) g_dbus_interface_info_unref, NULL);
  g_ptr_array_free (data->interfaces, TRUE);
  data->interfaces = NULL;
}

static void
parse_data_free_nodes (ParseData *data)
{
  if (data->nodes == NULL)
    return;
  g_ptr_array_foreach (data->nodes, (GFunc) g_dbus_node_info_unref, NULL);
  g_ptr_array_free (data->nodes, TRUE);
  data->nodes = NULL;
}

/* ---------------------------------------------------------------------------------------------------- */

static GDBusAnnotationInfo *
1106 1107
parse_data_get_annotation (ParseData *data,
                           gboolean   create_new)
1108 1109 1110 1111 1112 1113 1114
{
  if (create_new)
    g_ptr_array_add (data->annotations, g_new0 (GDBusAnnotationInfo, 1));
  return data->annotations->pdata[data->annotations->len - 1];
}

static GDBusArgInfo *
1115 1116
parse_data_get_arg (ParseData *data,
                    gboolean   create_new)
1117 1118 1119 1120 1121 1122 1123
{
  if (create_new)
    g_ptr_array_add (data->args, g_new0 (GDBusArgInfo, 1));
  return data->args->pdata[data->args->len - 1];
}

static GDBusArgInfo *
1124 1125
parse_data_get_out_arg (ParseData *data,
                        gboolean   create_new)
1126 1127 1128 1129 1130 1131 1132
{
  if (create_new)
    g_ptr_array_add (data->out_args, g_new0 (GDBusArgInfo, 1));
  return data->out_args->pdata[data->out_args->len - 1];
}

static GDBusMethodInfo *
1133 1134
parse_data_get_method (ParseData *data,
                       gboolean   create_new)
1135 1136 1137 1138 1139 1140 1141
{
  if (create_new)
    g_ptr_array_add (data->methods, g_new0 (GDBusMethodInfo, 1));
  return data->methods->pdata[data->methods->len - 1];
}

static GDBusSignalInfo *
1142 1143
parse_data_get_signal (ParseData *data,
                       gboolean   create_new)
1144 1145 1146 1147 1148 1149 1150
{
  if (create_new)
    g_ptr_array_add (data->signals, g_new0 (GDBusSignalInfo, 1));
  return data->signals->pdata[data->signals->len - 1];
}

static GDBusPropertyInfo *
1151 1152
parse_data_get_property (ParseData *data,
                         gboolean   create_new)
1153 1154 1155 1156 1157 1158 1159
{
  if (create_new)
    g_ptr_array_add (data->properties, g_new0 (GDBusPropertyInfo, 1));
  return data->properties->pdata[data->properties->len - 1];
}

static GDBusInterfaceInfo *
1160 1161
parse_data_get_interface (ParseData *data,
                          gboolean   create_new)
1162 1163 1164 1165 1166 1167 1168
{
  if (create_new)
    g_ptr_array_add (data->interfaces, g_new0 (GDBusInterfaceInfo, 1));
  return data->interfaces->pdata[data->interfaces->len - 1];
}

static GDBusNodeInfo *
1169 1170
parse_data_get_node (ParseData *data,
                     gboolean   create_new)
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
{
  if (create_new)
    g_ptr_array_add (data->nodes, g_new0 (GDBusNodeInfo, 1));
  return data->nodes->pdata[data->nodes->len - 1];
}

/* ---------------------------------------------------------------------------------------------------- */

static ParseData *
parse_data_new (void)
{
  ParseData *data;

  data = g_new0 (ParseData, 1);

  /* initialize arrays */
  parse_data_steal_annotations (data, NULL);
  parse_data_steal_args (data, NULL);
  parse_data_steal_out_args (data, NULL);
  parse_data_steal_methods (data, NULL);
  parse_data_steal_signals (data, NULL);
  parse_data_steal_properties (data, NULL);
  parse_data_steal_interfaces (data, NULL);
  parse_data_steal_nodes (data, NULL);

  return data;
}

static void
parse_data_free (ParseData *data)
{
  GSList *l;

  /* free stack of annotation arrays */
  for (l = data->annotations_stack; l != NULL; l = l->next)
    {
      GPtrArray *annotations = l->data;
      g_ptr_array_foreach (annotations, (GFunc) g_dbus_annotation_info_unref, NULL);
      g_ptr_array_free (annotations, TRUE);
    }
  g_slist_free (data->annotations_stack);

  /* free stack of interface arrays */
  for (l = data->interfaces_stack; l != NULL; l = l->next)
    {
      GPtrArray *interfaces = l->data;
      g_ptr_array_foreach (interfaces, (GFunc) g_dbus_interface_info_unref, NULL);
      g_ptr_array_free (interfaces, TRUE);
    }
  g_slist_free (data->interfaces_stack);

  /* free stack of node arrays */
  for (l = data->nodes_stack; l != NULL; l = l->next)
    {
      GPtrArray *nodes = l->data;
      g_ptr_array_foreach (nodes, (GFunc) g_dbus_node_info_unref, NULL);
      g_ptr_array_free (nodes, TRUE);
    }
  g_slist_free (data->nodes_stack);

  /* free arrays (data->annotations, data->interfaces and data->nodes have been freed above) */
  parse_data_free_args (data);
  parse_data_free_out_args (data);
  parse_data_free_methods (data);
  parse_data_free_signals (data);
  parse_data_free_properties (data);
1237 1238 1239
  parse_data_free_interfaces (data);
  parse_data_free_annotations (data);
  parse_data_free_nodes (data);
1240 1241 1242 1243 1244 1245 1246

  g_free (data);
}

/* ---------------------------------------------------------------------------------------------------- */

static void
1247 1248 1249 1250 1251 1252
parser_start_element (GMarkupParseContext  *context,
                      const gchar          *element_name,
                      const gchar         **attribute_names,
                      const gchar         **attribute_values,
                      gpointer              user_data,
                      GError              **error)
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
{
  ParseData *data = user_data;
  GSList *stack;
  const gchar *name;
  const gchar *type;
  const gchar *access;
  const gchar *direction;
  const gchar *value;

  name = NULL;
  type = NULL;
  access = NULL;
  direction = NULL;
  value = NULL;

  stack = (GSList *) g_markup_parse_context_get_element_stack (context);

  /* ---------------------------------------------------------------------------------------------------- */
  if (strcmp (element_name, "node") == 0)
    {
      if (!(g_slist_length (stack) >= 1 || strcmp (stack->next->data, "node") != 0))
        {
          g_set_error_literal (error,
                               G_MARKUP_ERROR,
                               G_MARKUP_ERROR_INVALID_CONTENT,
                               "<node> elements can only be top-level or embedded in other <node> elements");
          goto out;
        }

1282 1283 1284 1285 1286 1287 1288 1289
      if (!g_markup_collect_attributes (element_name,
                                        attribute_names,
                                        attribute_values,
                                        error,
                                        G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL, "name", &name,
                                        /* some hand-written introspection XML documents use this */
                                        G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL, "xmlns:doc", NULL,
                                        G_MARKUP_COLLECT_INVALID))
1290 1291 1292 1293 1294
        goto out;

      g_dbus_node_info_set (data,
                            parse_data_get_node (data, TRUE),
                            name,
1295 1296
                            NULL,
                            NULL,
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
                            NULL);

      /* push the currently retrieved interfaces and nodes on the stack and prepare new arrays */
      data->interfaces_stack = g_slist_prepend (data->interfaces_stack, data->interfaces);
      data->interfaces = NULL;
      parse_data_steal_interfaces (data, NULL);

      data->nodes_stack = g_slist_prepend (data->nodes_stack, data->nodes);
      data->nodes = NULL;
      parse_data_steal_nodes (data, NULL);

    }
  /* ---------------------------------------------------------------------------------------------------- */
  else if (strcmp (element_name, "interface") == 0)
    {
      if (g_slist_length (stack) < 2 || strcmp (stack->next->data, "node") != 0)
        {
          g_set_error_literal (error,
                               G_MARKUP_ERROR,
                               G_MARKUP_ERROR_INVALID_CONTENT,
                               "<interface> elements can only be embedded in <node> elements");
          goto out;
        }

1321 1322 1323 1324 1325 1326 1327 1328
      if (!g_markup_collect_attributes (element_name,
                                        attribute_names,
                                        attribute_values,
                                        error,
                                        G_MARKUP_COLLECT_STRING, "name", &name,
                                        /* seen in the wild */
                                        G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL, "version", NULL,
                                        G_MARKUP_COLLECT_INVALID))
1329 1330 1331 1332 1333
        goto out;

      g_dbus_interface_info_set (data,
                                 parse_data_get_interface (data, TRUE),
                                 name,
1334 1335 1336
                                 NULL,
                                 NULL,
                                 NULL,
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
                                 NULL);

    }
  /* ---------------------------------------------------------------------------------------------------- */
  else if (strcmp (element_name, "method") == 0)
    {
      if (g_slist_length (stack) < 2 || strcmp (stack->next->data, "interface") != 0)
        {
          g_set_error_literal (error,
                               G_MARKUP_ERROR,
                               G_MARKUP_ERROR_INVALID_CONTENT,
                               "<method> elements can only be embedded in <interface> elements");
          goto out;
        }

1352 1353 1354 1355 1356 1357 1358 1359
      if (!g_markup_collect_attributes (element_name,
                                        attribute_names,
                                        attribute_values,
                                        error,
                                        G_MARKUP_COLLECT_STRING, "name", &name,
                                        /* seen in the wild */
                                        G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL, "version", NULL,
                                        G_MARKUP_COLLECT_INVALID))
1360 1361 1362 1363 1364
        goto out;

      g_dbus_method_info_set (data,
                              parse_data_get_method (data, TRUE),
                              name,
1365 1366
                              NULL,
                              NULL,
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
                              NULL);

      data->num_args = 0;

    }
  /* ---------------------------------------------------------------------------------------------------- */
  else if (strcmp (element_name, "signal") == 0)
    {
      if (g_slist_length (stack) < 2 || strcmp (stack->next->data, "interface") != 0)
        {
          g_set_error_literal (error,
                               G_MARKUP_ERROR,
                               G_MARKUP_ERROR_INVALID_CONTENT,
                               "<signal> elements can only be embedded in <interface> elements");
          goto out;
        }

1384 1385 1386 1387 1388 1389
      if (!g_markup_collect_attributes (element_name,
                                        attribute_names,
                                        attribute_values,
                                        error,
                                        G_MARKUP_COLLECT_STRING, "name", &name,
                                        G_MARKUP_COLLECT_INVALID))
1390 1391 1392 1393 1394
        goto out;

      g_dbus_signal_info_set (data,
                              parse_data_get_signal (data, TRUE),
                              name,
1395
                              NULL,
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
                              NULL);

      data->num_args = 0;

    }
  /* ---------------------------------------------------------------------------------------------------- */
  else if (strcmp (element_name, "property") == 0)
    {
      GDBusPropertyInfoFlags flags;

      if (g_slist_length (stack) < 2 || strcmp (stack->next->data, "interface") != 0)
        {
          g_set_error_literal (error,
                               G_MARKUP_ERROR,
                               G_MARKUP_ERROR_INVALID_CONTENT,
                               "<property> elements can only be embedded in <interface> elements");
          goto out;
        }

1415 1416 1417 1418 1419 1420 1421 1422
      if (!g_markup_collect_attributes (element_name,
                                        attribute_names,
                                        attribute_values,
                                        error,
                                        G_MARKUP_COLLECT_STRING, "name", &name,
                                        G_MARKUP_COLLECT_STRING, "type", &type,
                                        G_MARKUP_COLLECT_STRING, "access", &access,
                                        G_MARKUP_COLLECT_INVALID))
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
        goto out;

      if (strcmp (access, "read") == 0)
        flags = G_DBUS_PROPERTY_INFO_FLAGS_READABLE;
      else if (strcmp (access, "write") == 0)
        flags = G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE;
      else if (strcmp (access, "readwrite") == 0)
        flags = G_DBUS_PROPERTY_INFO_FLAGS_READABLE | G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE;
      else
        {
          g_set_error (error,
                       G_MARKUP_ERROR,
                       G_MARKUP_ERROR_INVALID_CONTENT,
                       "Unknown value '%s' of access attribute for element <property>",
                       access);
          goto out;
        }

      g_dbus_property_info_set (data,
                                parse_data_get_property (data, TRUE),
                                name,
                                type,
                                flags,
                                NULL);

    }
  /* ---------------------------------------------------------------------------------------------------- */
  else if (strcmp (element_name, "arg") == 0)
    {
      gboolean is_in;
      gchar *name_to_use;

      if (g_slist_length (stack) < 2 ||
          (strcmp (stack->next->data, "method") != 0 &&
           strcmp (stack->next->data, "signal") != 0))
        {
          g_set_error_literal (error,
                               G_MARKUP_ERROR,
                               G_MARKUP_ERROR_INVALID_CONTENT,
                               "<arg> elements can only be embedded in <method> or <signal> elements");
          goto out;
        }

1466 1467 1468 1469 1470 1471 1472 1473
      if (!g_markup_collect_attributes (element_name,
                                        attribute_names,
                                        attribute_values,
                                        error,
                                        G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL, "name", &name,
                                        G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL, "direction", &direction,
                                        G_MARKUP_COLLECT_STRING, "type", &type,
                                        G_MARKUP_COLLECT_INVALID))
1474 1475
        goto out;

1476 1477 1478 1479
      if (strcmp (stack->next->data, "method") == 0)
        is_in = TRUE;
      else
        is_in = FALSE;
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
      if (direction != NULL)
        {
          if (strcmp (direction, "in") == 0)
            is_in = TRUE;
          else if (strcmp (direction, "out") == 0)
            is_in = FALSE;
          else
            {
              g_set_error (error,
                           G_MARKUP_ERROR,
                           G_MARKUP_ERROR_INVALID_CONTENT,
                           "Unknown value '%s' of direction attribute",
                           direction);
              goto out;
            }
        }

      if (is_in && strcmp (stack->next->data, "signal") == 0)
        {
          g_set_error_literal (error,
                               G_MARKUP_ERROR,
                               G_MARKUP_ERROR_INVALID_CONTENT,
                               "Only direction 'out' is allowed for <arg> elements embedded in <signal>");
          goto out;
        }

      if (name == NULL)
        name_to_use = g_strdup_printf ("arg_%d", data->num_args);
      else
        name_to_use = g_strdup (name);
      data->num_args++;

      if (is_in)
        {
          g_dbus_arg_info_set (data,
                               parse_data_get_arg (data, TRUE),
                               name_to_use,
                               type,
                               NULL);
          data->last_arg_was_in = TRUE;
        }
      else
        {
          g_dbus_arg_info_set (data,
                               parse_data_get_out_arg (data, TRUE),
                               name_to_use,
                               type,
                               NULL);
          data->last_arg_was_in = FALSE;

        }

      g_free (name_to_use);
    }
  /* ---------------------------------------------------------------------------------------------------- */
  else if (strcmp (element_name, "annotation") == 0)
    {
      if (g_slist_length (stack) < 2 ||
          (strcmp (stack->next->data, "node") != 0 &&
           strcmp (stack->next->data, "interface") != 0 &&
           strcmp (stack->next->data, "signal") != 0 &&
           strcmp (stack->next->data, "method") != 0 &&
           strcmp (stack->next->data, "property") != 0 &&
           strcmp (stack->next->data, "arg") != 0 &&
           strcmp (stack->next->data, "annotation") != 0))
        {
          g_set_error_literal (error,
                               G_MARKUP_ERROR,
                               G_MARKUP_ERROR_INVALID_CONTENT,
                               "<annotation> elements can only be embedded in <node>, <interface>, <signal>, <method>, <property>, <arg> or <annotation> elements");
          goto out;
        }

1553 1554 1555 1556 1557 1558 1559
      if (!g_markup_collect_attributes (element_name,
                                        attribute_names,
                                        attribute_values,
                                        error,
                                        G_MARKUP_COLLECT_STRING, "name", &name,
                                        G_MARKUP_COLLECT_STRING, "value", &value,
                                        G_MARKUP_COLLECT_INVALID))
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593
        goto out;

      g_dbus_annotation_info_set (data,
                                  parse_data_get_annotation (data, TRUE),
                                  name,
                                  value,
                                  NULL);
    }
  /* ---------------------------------------------------------------------------------------------------- */
  else
    {
      /* don't bail on unknown elements; just ignore them */
    }
  /* ---------------------------------------------------------------------------------------------------- */

  /* push the currently retrieved annotations on the stack and prepare a new one */
  data->annotations_stack = g_slist_prepend (data->annotations_stack, data->annotations);
  data->annotations = NULL;
  parse_data_steal_annotations (data, NULL);

 out:
  ;
}

/* ---------------------------------------------------------------------------------------------------- */

static GDBusAnnotationInfo **
steal_annotations (ParseData *data)
{
  return parse_data_steal_annotations (data, NULL);
}


static void
1594 1595 1596 1597
parser_end_element (GMarkupParseContext  *context,
                    const gchar          *element_name,
                    gpointer              user_data,
                    GError              **error)
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760
{
  ParseData *data = user_data;
  gboolean have_popped_annotations;

  have_popped_annotations = FALSE;

  if (strcmp (element_name, "node") == 0)
    {
      guint num_nodes;
      guint num_interfaces;
      GDBusNodeInfo **nodes;
      GDBusInterfaceInfo **interfaces;

      nodes = parse_data_steal_nodes (data, &num_nodes);
      interfaces = parse_data_steal_interfaces (data, &num_interfaces);

      /* destroy the nodes, interfaces for scope we're exiting and and pop the nodes, interfaces from the
       * scope we're reentering
       */
      parse_data_free_interfaces (data);
      data->interfaces = (GPtrArray *) data->interfaces_stack->data;
      data->interfaces_stack = g_slist_remove (data->interfaces_stack, data->interfaces_stack->data);

      parse_data_free_nodes (data);
      data->nodes = (GPtrArray *) data->nodes_stack->data;
      data->nodes_stack = g_slist_remove (data->nodes_stack, data->nodes_stack->data);

      g_dbus_node_info_set (data,
                            parse_data_get_node (data, FALSE),
                            NULL,
                            interfaces,
                            nodes,
                            steal_annotations (data));

    }
  else if (strcmp (element_name, "interface") == 0)
    {
      guint num_methods;
      guint num_signals;
      guint num_properties;
      GDBusMethodInfo **methods;
      GDBusSignalInfo **signals;
      GDBusPropertyInfo **properties;

      methods    = parse_data_steal_methods    (data, &num_methods);
      signals    = parse_data_steal_signals    (data, &num_signals);
      properties = parse_data_steal_properties (data, &num_properties);

      g_dbus_interface_info_set (data,
                                 parse_data_get_interface (data, FALSE),
                                 NULL,
                                 methods,
                                 signals,
                                 properties,
                                 steal_annotations (data));

    }
  else if (strcmp (element_name, "method") == 0)
    {
      guint in_num_args;
      guint out_num_args;
      GDBusArgInfo **in_args;
      GDBusArgInfo **out_args;

      in_args  = parse_data_steal_args     (data, &in_num_args);
      out_args = parse_data_steal_out_args (data, &out_num_args);

      g_dbus_method_info_set (data,
                              parse_data_get_method (data, FALSE),
                              NULL,
                              in_args,
                              out_args,
                              steal_annotations (data));
    }
  else if (strcmp (element_name, "signal") == 0)
    {
      guint num_args;
      GDBusArgInfo **args;

      args = parse_data_steal_out_args (data, &num_args);

      g_dbus_signal_info_set (data,
                              parse_data_get_signal (data, FALSE),
                              NULL,
                              args,
                              steal_annotations (data));
    }
  else if (strcmp (element_name, "property") == 0)
    {
      g_dbus_property_info_set (data,
                                parse_data_get_property (data, FALSE),
                                NULL,
                                NULL,
                                G_DBUS_PROPERTY_INFO_FLAGS_NONE,
                                steal_annotations (data));
    }
  else if (strcmp (element_name, "arg") == 0)
    {
      g_dbus_arg_info_set (data,
                           data->last_arg_was_in ? parse_data_get_arg (data, FALSE) : parse_data_get_out_arg (data, FALSE),
                           NULL,
                           NULL,
                           steal_annotations (data));
    }
  else if (strcmp (element_name, "annotation") == 0)
    {
      GDBusAnnotationInfo **embedded_annotations;

      embedded_annotations = steal_annotations (data);

      /* destroy the annotations for scope we're exiting and and pop the annotations from the scope we're reentering */
      parse_data_free_annotations (data);
      data->annotations = (GPtrArray *) data->annotations_stack->data;
      data->annotations_stack = g_slist_remove (data->annotations_stack, data->annotations_stack->data);

      have_popped_annotations = TRUE;

      g_dbus_annotation_info_set (data,
                                  parse_data_get_annotation (data, FALSE),
                                  NULL,
                                  NULL,
                                  embedded_annotations);
    }
  else
    {
      /* don't bail on unknown elements; just ignore them */
    }

  if (!have_popped_annotations)
    {
      /* destroy the annotations for scope we're exiting and and pop the annotations from the scope we're reentering */
      parse_data_free_annotations (data);
      data->annotations = (GPtrArray *) data->annotations_stack->data;
      data->annotations_stack = g_slist_remove (data->annotations_stack, data->annotations_stack->data);
    }
}

/* ---------------------------------------------------------------------------------------------------- */

static void
parser_error (GMarkupParseContext *context,
              GError              *error,
              gpointer             user_data)
{
  gint line_number;
  gint char_number;

  g_markup_parse_context_get_position (context, &line_number, &char_number);

  g_prefix_error (&error, "%d:%d: ",
                  line_number,
                  char_number);
}

/* ---------------------------------------------------------------------------------------------------- */

/**
 * g_dbus_node_info_new_for_xml:
 * @xml_data: Valid D-Bus introspection XML.
 * @error: Return location for error.
 *
 * Parses @xml_data and returns a #GDBusNodeInfo representing the data.
 *
1761
 * The introspection XML must contain exactly one top-level
1762
 * <node> element.
1763
 *
1764
 * Note that this routine is using a
1765
 * [GMarkup][glib-Simple-XML-Subset-Parser.description]-based
1766 1767
 * parser that only accepts a subset of valid XML documents.
 *
1768 1769
 * Returns: A #GDBusNodeInfo structure or %NULL if @error is set. Free
 * with g_dbus_node_info_unref().
1770 1771
 *
 * Since: 2.26
1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
 */
GDBusNodeInfo *
g_dbus_node_info_new_for_xml (const gchar  *xml_data,
                              GError      **error)
{
  GDBusNodeInfo *ret;
  GMarkupParseContext *context;
  GMarkupParser *parser;
  guint num_nodes;
  ParseData *data;
1782
  GDBusNodeInfo **ughret;
1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794

  ret = NULL;
  parser = NULL;
  context = NULL;

  parser = g_new0 (GMarkupParser, 1);
  parser->start_element = parser_start_element;
  parser->end_element   = parser_end_element;
  parser->error         = parser_error;

  data = parse_data_new ();
  context = g_markup_parse_context_new (parser,
1795
                                        G_MARKUP_IGNORE_QUALIFIED,
1796 1797 1798 1799 1800 1801 1802 1803 1804
                                        data,
                                        (GDestroyNotify) parse_data_free);

  if (!g_markup_parse_context_parse (context,
                                     xml_data,
                                     strlen (xml_data),
                                     error))
    goto out;

1805 1806 1807
  if (!g_markup_parse_context_end_parse (context, error))
    goto out;

1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822
  ughret = parse_data_steal_nodes (data, &num_nodes);

  if (num_nodes != 1)
    {
      guint n;

      g_set_error (error,
                   G_MARKUP_ERROR,
                   G_MARKUP_ERROR_INVALID_CONTENT,
                   "Expected a single node in introspection XML, found %d",
                   num_nodes);

      /* clean up */
      for (n = 0; n < num_nodes; n++)
        {
1823 1824
          g_dbus_node_info_unref (ughret[n]);
          ughret[n] = NULL;
1825 1826 1827 1828 1829 1830 1831
        }
    }

  ret = ughret[0];
  g_free (ughret);

 out:
1832
  g_free (parser);
1833 1834 1835 1836 1837 1838 1839 1840 1841 1842
  if (context != NULL)
    g_markup_parse_context_free (context);

  return ret;
}

/* ---------------------------------------------------------------------------------------------------- */

/**
 * g_dbus_annotation_info_lookup:
1843
 * @annotations: (array zero-terminated=1) (nullable): A %NULL-terminated array of annotations or %NULL.
1844 1845 1846 1847
 * @name: The name of the annotation to look up.
 *
 * Looks up the value of an annotation.
 *
1848
 * The cost of this function is O(n) in number of annotations.
1849 1850
 *
 * Returns: The value or %NULL if not found. Do not free, it is owned by @annotations.
1851 1852
 *
 * Since: 2.26
1853 1854
 */
const gchar *
1855 1856
g_dbus_annotation_info_lookup (GDBusAnnotationInfo **annotations,
                               const gchar          *name)
1857 1858 1859 1860 1861
{
  guint n;
  const gchar *ret;

  ret = NULL;
1862
  for (n = 0; annotations != NULL && annotations[n] != NULL; n++)
1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876
    {
      if (g_strcmp0 (annotations[n]->key, name) == 0)
        {
          ret = annotations[n]->value;
          goto out;
        }
    }

 out:
  return ret;
}

/* ---------------------------------------------------------------------------------------------------- */

1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907
G_LOCK_DEFINE_STATIC (info_cache_lock);

typedef struct
{
  gint use_count;

  /* gchar* -> GDBusMethodInfo* */
  GHashTable *method_name_to_data;

  /* gchar* -> GDBusMethodInfo* */
  GHashTable *signal_name_to_data;

  /* gchar* -> GDBusMethodInfo* */
  GHashTable *property_name_to_data;
} InfoCacheEntry;

static void
info_cache_free (InfoCacheEntry *cache)
{
  g_assert (cache->use_count == 0);
  g_hash_table_unref (cache->method_name_to_data);
  g_hash_table_unref (cache->signal_name_to_data);
  g_hash_table_unref (cache->property_name_to_data);
  g_slice_free (InfoCacheEntry, cache);
}

/* maps from GDBusInterfaceInfo* to InfoCacheEntry* */
static GHashTable *info_cache = NULL;

/* ---------------------------------------------------------------------------------------------------- */

1908 1909 1910 1911 1912 1913 1914
/**
 * g_dbus_interface_info_lookup_method:
 * @info: A #GDBusInterfaceInfo.
 * @name: A D-Bus method name (typically in CamelCase)
 *
 * Looks up information about a method.
 *
1915
 * The cost of this function is O(n) in number of methods unless
1916
 * g_dbus_interface_info_cache_build() has been used on @info.
1917
 *
1918
 * Returns: (transfer none): A #GDBusMethodInfo or %NULL if not found. Do not free, it is owned by @info.
1919 1920 1921
 *
 * Since: 2.26
 */
1922 1923 1924
GDBusMethodInfo *
g_dbus_interface_info_lookup_method (GDBusInterfaceInfo *info,
                                     const gchar        *name)
1925 1926
{
  guint n;
1927
  GDBusMethodInfo *result;
1928

1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942
  G_LOCK (info_cache_lock);
  if (G_LIKELY (info_cache != NULL))
    {
      InfoCacheEntry *cache;
      cache = g_hash_table_lookup (info_cache, info);
      if (G_LIKELY (cache != NULL))
        {
          result = g_hash_table_lookup (cache->method_name_to_data, name);
          G_UNLOCK (info_cache_lock);
          goto out;
        }
    }
  G_UNLOCK (info_cache_lock);

1943 1944
  for (n = 0; info->methods != NULL && info->methods[n] != NULL; n++)
    {
1945
      GDBusMethodInfo *i = info->methods[n];
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968

      if (g_strcmp0 (i->name, name) == 0)
        {
          result = i;
          goto out;
        }
    }

  result = NULL;

 out:
  return result;
}

/* ---------------------------------------------------------------------------------------------------- */

/**
 * g_dbus_interface_info_lookup_signal:
 * @info: A #GDBusInterfaceInfo.
 * @name: A D-Bus signal name (typically in CamelCase)
 *
 * Looks up information about a signal.
 *
1969
 * The cost of this function is O(n) in number of signals unless
1970
 * g_dbus_interface_info_cache_build() has been used on @info.
1971
 *
1972
 * Returns: (transfer none): A #GDBusSignalInfo or %NULL if not found. Do not free, it is owned by @info.
1973 1974 1975
 *
 * Since: 2.26
 */
1976 1977 1978
GDBusSignalInfo *
g_dbus_interface_info_lookup_signal (GDBusInterfaceInfo *info,
                                     const gchar        *name)
1979 1980
{
  guint n;
1981
  GDBusSignalInfo *result;
1982

1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996
  G_LOCK (info_cache_lock);
  if (G_LIKELY (info_cache != NULL))
    {
      InfoCacheEntry *cache;
      cache = g_hash_table_lookup (info_cache, info);
      if (G_LIKELY (cache != NULL))
        {
          result = g_hash_table_lookup (cache->signal_name_to_data, name);
          G_UNLOCK (info_cache_lock);
          goto out;
        }
    }
  G_UNLOCK (info_cache_lock);

1997 1998
  for (n = 0; info->signals != NULL && info->signals[n] != NULL; n++)
    {
1999
      GDBusSignalInfo *i = info->signals[n];
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022

      if (g_strcmp0 (i->name, name) == 0)
        {
          result = i;
          goto out;
        }
    }

  result = NULL;

 out:
  return result;
}

/* ---------------------------------------------------------------------------------------------------- */

/**
 * g_dbus_interface_info_lookup_property:
 * @info: A #GDBusInterfaceInfo.
 * @name: A D-Bus property name (typically in CamelCase).
 *
 * Looks up information about a property.
 *
2023
 * The cost of this function is O(n) in number of properties unless
2024
 * g_dbus_interface_info_cache_build() has been used on @info.
2025
 *
2026
 * Returns: (transfer none): A #GDBusPropertyInfo or %NULL if not found. Do not free, it is owned by @info.
2027 2028 2029
 *
 * Since: 2.26
 */
2030 2031 2032
GDBusPropertyInfo *
g_dbus_interface_info_lookup_property (GDBusInterfaceInfo *info,
                                       const gchar        *name)
2033 2034
{
  guint n;
2035
  GDBusPropertyInfo *result;
2036

2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050
  G_LOCK (info_cache_lock);
  if (G_LIKELY (info_cache != NULL))
    {
      InfoCacheEntry *cache;
      cache = g_hash_table_lookup (info_cache, info);
      if (G_LIKELY (cache != NULL))
        {
          result = g_hash_table_lookup (cache->property_name_to_data, name);
          G_UNLOCK (info_cache_lock);
          goto out;
        }
    }
  G_UNLOCK (info_cache_lock);

2051 2052
  for (n = 0; info->properties != NULL && info->properties[n] != NULL; n++)
    {
2053
      GDBusPropertyInfo *i = info->properties[n];
2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067

      if (g_strcmp0 (i->name, name) == 0)
        {
          result = i;
          goto out;
        }
    }

  result = NULL;

 out:
  return result;
}

2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156
/* ---------------------------------------------------------------------------------------------------- */

/**
 * g_dbus_interface_info_cache_build:
 * @info: A #GDBusInterfaceInfo.
 *
 * Builds a lookup-cache to speed up
 * g_dbus_interface_info_lookup_method(),
 * g_dbus_interface_info_lookup_signal() and
 * g_dbus_interface_info_lookup_property().
 *
 * If this has already been called with @info, the existing cache is
 * used and its use count is increased.
 *
 * Note that @info cannot be modified until
 * g_dbus_interface_info_cache_release() is called.
 *
 * Since: 2.30
 */
void
g_dbus_interface_info_cache_build (GDBusInterfaceInfo *info)
{
  InfoCacheEntry *cache;
  guint n;

  G_LOCK (info_cache_lock);
  if (info_cache == NULL)
    info_cache = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, (GDestroyNotify) info_cache_free);
  cache = g_hash_table_lookup (info_cache, info);
  if (cache != NULL)
    {
      cache->use_count += 1;
      goto out;
    }
  cache = g_slice_new0 (InfoCacheEntry);
  cache->use_count = 1;
  cache->method_name_to_data = g_hash_table_new (g_str_hash, g_str_equal);
  cache->signal_name_to_data = g_hash_table_new (g_str_hash, g_str_equal);
  cache->property_name_to_data = g_hash_table_new (g_str_hash, g_str_equal);
  for (n = 0; info->methods != NULL && info->methods[n] != NULL; n++)
    g_hash_table_insert (cache->method_name_to_data, info->methods[n]->name, info->methods[n]);
  for (n = 0; info->signals != NULL && info->signals[n] != NULL; n++)
    g_hash_table_insert (cache->signal_name_to_data, info->signals[n]->name, info->signals[n]);
  for (n = 0; info->properties != NULL && info->properties[n] != NULL; n++)
    g_hash_table_insert (cache->property_name_to_data, info->properties[n]->name, info->properties[n]);
  g_hash_table_insert (info_cache, info, cache);
 out:
  G_UNLOCK (info_cache_lock);
}

/**
 * g_dbus_interface_info_cache_release:
 * @info: A GDBusInterfaceInfo
 *
 * Decrements the usage count for the cache for @info built by
 * g_dbus_interface_info_cache_build() (if any) and frees the
 * resources used by the cache if the usage count drops to zero.
 *
 * Since: 2.30
 */
void
g_dbus_interface_info_cache_release (GDBusInterfaceInfo *info)
{
  InfoCacheEntry *cache;

  G_LOCK (info_cache_lock);
  if (G_UNLIKELY (info_cache == NULL))
    {
      g_warning ("%s called for interface %s but there is no cache", info->name, G_STRFUNC);
      goto out;
    }

  cache = g_hash_table_lookup (info_cache, info);
  if (G_UNLIKELY (cache == NULL))
    {
      g_warning ("%s called for interface %s but there is no cache entry", info->name, G_STRFUNC);
      goto out;
    }
  cache->use_count -= 1;
  if (cache->use_count == 0)
    {
      g_hash_table_remove (info_cache, info);
      /* could nuke info_cache itself if empty */
    }
 out:
  G_UNLOCK (info_cache_lock);
}


2157 2158 2159 2160
/* ---------------------------------------------------------------------------------------------------- */

/**
 * g_dbus_node_info_lookup_interface:
2161
 * @info: A #GDBusNodeInfo.
2162 2163 2164 2165
 * @name: A D-Bus interface name.
 *
 * Looks up information about an interface.
 *
2166
 * The cost of this function is O(n) in number of interfaces.
2167
 *
2168
 * Returns: (transfer none): A #GDBusInterfaceInfo or %NULL if not found. Do not free, it is owned by @info.
2169 2170 2171
 *
 * Since: 2.26
 */
2172 2173 2174
GDBusInterfaceInfo *
g_dbus_node_info_lookup_interface (GDBusNodeInfo *info,
                                   const gchar   *name)
2175 2176
{
  guint n;
2177
  GDBusInterfaceInfo *result;
2178

2179
  for (n = 0; info->interfaces != NULL && info->interfaces[n] != NULL; n++)
2180
    {