gdbusinterfaceskeleton.c 34.1 KB
Newer Older
1 2 3 4 5 6 7
/* GDBus - GLib D-Bus Library
 *
 * Copyright (C) 2008-2010 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
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 "gdbusinterface.h"
24 25
#include "gdbusinterfaceskeleton.h"
#include "gdbusobjectskeleton.h"
26 27 28 29
#include "gioenumtypes.h"
#include "gdbusprivate.h"
#include "gdbusmethodinvocation.h"
#include "gdbusconnection.h"
30
#include "gtask.h"
31 32 33 34 35
#include "gioerror.h"

#include "glibintl.h"

/**
36
 * SECTION:gdbusinterfaceskeleton
37 38 39 40 41 42
 * @short_description: Service-side D-Bus interface
 * @include: gio/gio.h
 *
 * Abstract base class for D-Bus interfaces on the service side.
 */

43
struct _GDBusInterfaceSkeletonPrivate
44
{
45
  GMutex                      lock;
46

47
  GDBusObject                *object;
48
  GDBusInterfaceSkeletonFlags flags;
49

50 51 52
  GSList                     *connections;   /* List of ConnectionData */
  gchar                      *object_path;   /* The object path for this skeleton */
  GDBusInterfaceVTable       *hooked_vtable;
53 54
};

55 56 57 58 59 60
typedef struct
{
  GDBusConnection *connection;
  guint            registration_id;
} ConnectionData;

61 62 63 64 65 66 67 68 69 70 71 72 73 74
enum
{
  G_AUTHORIZE_METHOD_SIGNAL,
  LAST_SIGNAL
};

enum
{
  PROP_0,
  PROP_G_FLAGS
};

static guint signals[LAST_SIGNAL] = {0};

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
static void     dbus_interface_interface_init                      (GDBusInterfaceIface    *iface);

static void     set_object_path_locked                             (GDBusInterfaceSkeleton *interface_,
                                                                    const gchar            *object_path);
static void     remove_connection_locked                           (GDBusInterfaceSkeleton *interface_,
                                                                    GDBusConnection        *connection);
static void     skeleton_intercept_handle_method_call              (GDBusConnection        *connection,
                                                                    const gchar            *sender,
                                                                    const gchar            *object_path,
                                                                    const gchar            *interface_name,
                                                                    const gchar            *method_name,
                                                                    GVariant               *parameters,
                                                                    GDBusMethodInvocation  *invocation,
                                                                    gpointer                user_data);

90

91
G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GDBusInterfaceSkeleton, g_dbus_interface_skeleton, G_TYPE_OBJECT,
92 93
                                  G_ADD_PRIVATE (GDBusInterfaceSkeleton)
                                  G_IMPLEMENT_INTERFACE (G_TYPE_DBUS_INTERFACE, dbus_interface_interface_init))
94 95

static void
96
g_dbus_interface_skeleton_finalize (GObject *object)
97
{
98
  GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object);
99

100
  /* Hold the lock just in case any code we call verifies that the lock is held */
101 102 103 104 105 106 107 108 109 110 111 112 113 114
  g_mutex_lock (&interface->priv->lock);

  /* unexport from all connections if we're exported anywhere */
  while (interface->priv->connections != NULL)
    {
      ConnectionData *data = interface->priv->connections->data;
      remove_connection_locked (interface, data->connection);
    }

  set_object_path_locked (interface, NULL);

  g_mutex_unlock (&interface->priv->lock);

  g_free (interface->priv->hooked_vtable);
115

116 117
  if (interface->priv->object != NULL)
    g_object_remove_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object);
118

119
  g_mutex_clear (&interface->priv->lock);
120

121
  G_OBJECT_CLASS (g_dbus_interface_skeleton_parent_class)->finalize (object);
122 123 124
}

static void
125 126 127 128
g_dbus_interface_skeleton_get_property (GObject      *object,
                                        guint         prop_id,
                                        GValue       *value,
                                        GParamSpec   *pspec)
129
{
130
  GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object);
131 132 133 134

  switch (prop_id)
    {
    case PROP_G_FLAGS:
135
      g_value_set_flags (value, g_dbus_interface_skeleton_get_flags (interface));
136 137 138 139 140 141 142 143 144
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
145 146 147 148
g_dbus_interface_skeleton_set_property (GObject      *object,
                                        guint         prop_id,
                                        const GValue *value,
                                        GParamSpec   *pspec)
149
{
150
  GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object);
151 152 153 154

  switch (prop_id)
    {
    case PROP_G_FLAGS:
155
      g_dbus_interface_skeleton_set_flags (interface, g_value_get_flags (value));
156 157 158 159 160 161 162 163 164
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static gboolean
165 166
g_dbus_interface_skeleton_g_authorize_method_default (GDBusInterfaceSkeleton    *interface,
                                                      GDBusMethodInvocation *invocation)
167 168 169 170 171
{
  return TRUE;
}

static void
172
g_dbus_interface_skeleton_class_init (GDBusInterfaceSkeletonClass *klass)
173 174 175 176
{
  GObjectClass *gobject_class;

  gobject_class = G_OBJECT_CLASS (klass);
177 178 179
  gobject_class->finalize     = g_dbus_interface_skeleton_finalize;
  gobject_class->set_property = g_dbus_interface_skeleton_set_property;
  gobject_class->get_property = g_dbus_interface_skeleton_get_property;
180

181
  klass->g_authorize_method = g_dbus_interface_skeleton_g_authorize_method_default;
182 183

  /**
184
   * GDBusInterfaceSkeleton:g-flags:
185
   *
186
   * Flags from the #GDBusInterfaceSkeletonFlags enumeration.
187 188
   *
   * Since: 2.30
189 190 191 192 193
   */
  g_object_class_install_property (gobject_class,
                                   PROP_G_FLAGS,
                                   g_param_spec_flags ("g-flags",
                                                       "g-flags",
194 195 196
                                                       "Flags for the interface skeleton",
                                                       G_TYPE_DBUS_INTERFACE_SKELETON_FLAGS,
                                                       G_DBUS_INTERFACE_SKELETON_FLAGS_NONE,
197 198 199 200 201
                                                       G_PARAM_READABLE |
                                                       G_PARAM_WRITABLE |
                                                       G_PARAM_STATIC_STRINGS));

  /**
202 203
   * GDBusInterfaceSkeleton::g-authorize-method:
   * @interface: The #GDBusInterfaceSkeleton emitting the signal.
204 205 206 207 208 209 210
   * @invocation: A #GDBusMethodInvocation.
   *
   * Emitted when a method is invoked by a remote caller and used to
   * determine if the method call is authorized.
   *
   * Note that this signal is emitted in a thread dedicated to
   * handling the method call so handlers are allowed to perform
211 212 213 214 215
   * blocking IO. This means that it is appropriate to call e.g.
   * [polkit_authority_check_authorization_sync()](http://hal.freedesktop.org/docs/polkit/PolkitAuthority.html#polkit-authority-check-authorization-sync)
   * with the
   * [POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION](http://hal.freedesktop.org/docs/polkit/PolkitAuthority.html#POLKIT-CHECK-AUTHORIZATION-FLAGS-ALLOW-USER-INTERACTION:CAPS)
   * flag set.
216 217
   *
   * If %FALSE is returned then no further handlers are run and the
218
   * signal handler must take a reference to @invocation and finish
219 220 221 222 223
   * handling the call (e.g. return an error via
   * g_dbus_method_invocation_return_error()).
   *
   * Otherwise, if %TRUE is returned, signal emission continues. If no
   * handlers return %FALSE, then the method is dispatched. If
224 225 226
   * @interface has an enclosing #GDBusObjectSkeleton, then the
   * #GDBusObjectSkeleton::authorize-method signal handlers run before
   * the handlers for this signal.
227 228 229 230 231 232
   *
   * The default class handler just returns %TRUE.
   *
   * Please note that the common case is optimized: if no signals
   * handlers are connected and the default class handler isn't
   * overridden (for both @interface and the enclosing
233
   * #GDBusObjectSkeleton, if any) and #GDBusInterfaceSkeleton:g-flags does
234
   * not have the
235
   * %G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD
236 237 238 239 240
   * flags set, no dedicated thread is ever used and the call will be
   * handled in the same thread as the object that @interface belongs
   * to was exported in.
   *
   * Returns: %TRUE if the call is authorized, %FALSE otherwise.
241 242
   *
   * Since: 2.30
243 244
   */
  signals[G_AUTHORIZE_METHOD_SIGNAL] =
245
    g_signal_new (I_("g-authorize-method"),
246
                  G_TYPE_DBUS_INTERFACE_SKELETON,
247
                  G_SIGNAL_RUN_LAST,
248
                  G_STRUCT_OFFSET (GDBusInterfaceSkeletonClass, g_authorize_method),
249 250
                  _g_signal_accumulator_false_handled,
                  NULL,
251
                  NULL,
252 253 254 255 256 257
                  G_TYPE_BOOLEAN,
                  1,
                  G_TYPE_DBUS_METHOD_INVOCATION);
}

static void
258
g_dbus_interface_skeleton_init (GDBusInterfaceSkeleton *interface)
259
{
260
  interface->priv = g_dbus_interface_skeleton_get_instance_private (interface);
261
  g_mutex_init (&interface->priv->lock);
262 263 264 265 266
}

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

/**
267 268
 * g_dbus_interface_skeleton_get_flags:
 * @interface_: A #GDBusInterfaceSkeleton.
269
 *
270
 * Gets the #GDBusInterfaceSkeletonFlags that describes what the behavior
271
 * of @interface_
272
 *
273
 * Returns: One or more flags from the #GDBusInterfaceSkeletonFlags enumeration.
274 275
 *
 * Since: 2.30
276
 */
277 278
GDBusInterfaceSkeletonFlags
g_dbus_interface_skeleton_get_flags (GDBusInterfaceSkeleton  *interface_)
279
{
280
  g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), G_DBUS_INTERFACE_SKELETON_FLAGS_NONE);
281
  return interface_->priv->flags;
282 283 284
}

/**
285 286 287
 * g_dbus_interface_skeleton_set_flags:
 * @interface_: A #GDBusInterfaceSkeleton.
 * @flags: Flags from the #GDBusInterfaceSkeletonFlags enumeration.
288
 *
289
 * Sets flags describing what the behavior of @skeleton should be.
290 291
 *
 * Since: 2.30
292 293
 */
void
294 295
g_dbus_interface_skeleton_set_flags (GDBusInterfaceSkeleton      *interface_,
                                     GDBusInterfaceSkeletonFlags  flags)
296
{
297
  g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
298
  g_mutex_lock (&interface_->priv->lock);
299
  if (interface_->priv->flags != flags)
300
    {
301
      interface_->priv->flags = flags;
302
      g_mutex_unlock (&interface_->priv->lock);
303
      g_object_notify (G_OBJECT (interface_), "g-flags");
304
    }
305 306
  else
    {
307
      g_mutex_unlock (&interface_->priv->lock);
308
    }
309 310 311
}

/**
312 313
 * g_dbus_interface_skeleton_get_info:
 * @interface_: A #GDBusInterfaceSkeleton.
314 315
 *
 * Gets D-Bus introspection information for the D-Bus interface
316
 * implemented by @interface_.
317 318
 *
 * Returns: (transfer none): A #GDBusInterfaceInfo (never %NULL). Do not free.
319 320
 *
 * Since: 2.30
321 322
 */
GDBusInterfaceInfo *
323
g_dbus_interface_skeleton_get_info (GDBusInterfaceSkeleton *interface_)
324 325
{
  GDBusInterfaceInfo *ret;
326 327
  g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
  ret = G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->get_info (interface_);
328 329 330 331 332
  g_warn_if_fail (ret != NULL);
  return ret;
}

/**
333
 * g_dbus_interface_skeleton_get_vtable: (skip)
334
 * @interface_: A #GDBusInterfaceSkeleton.
335 336
 *
 * Gets the interface vtable for the D-Bus interface implemented by
337
 * @interface_. The returned function pointers should expect @interface_
338 339 340
 * itself to be passed as @user_data.
 *
 * Returns: A #GDBusInterfaceVTable (never %NULL).
341 342
 *
 * Since: 2.30
343 344
 */
GDBusInterfaceVTable *
345
g_dbus_interface_skeleton_get_vtable (GDBusInterfaceSkeleton *interface_)
346 347
{
  GDBusInterfaceVTable *ret;
348 349
  g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
  ret = G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->get_vtable (interface_);
350 351 352 353 354
  g_warn_if_fail (ret != NULL);
  return ret;
}

/**
355 356
 * g_dbus_interface_skeleton_get_properties:
 * @interface_: A #GDBusInterfaceSkeleton.
357
 *
358
 * Gets all D-Bus properties for @interface_.
359
 *
360 361 362
 * Returns: (transfer full): A #GVariant of type
 * ['a{sv}'][G-VARIANT-TYPE-VARDICT:CAPS].
 * Free with g_variant_unref().
363 364
 *
 * Since: 2.30
365 366
 */
GVariant *
367
g_dbus_interface_skeleton_get_properties (GDBusInterfaceSkeleton *interface_)
368 369
{
  GVariant *ret;
370 371
  g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
  ret = G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->get_properties (interface_);
372
  return g_variant_take_ref (ret);
373 374 375
}

/**
376 377
 * g_dbus_interface_skeleton_flush:
 * @interface_: A #GDBusInterfaceSkeleton.
378
 *
379
 * If @interface_ has outstanding changes, request for these changes to be
380 381 382 383
 * emitted immediately.
 *
 * For example, an exported D-Bus interface may queue up property
 * changes and emit the
384
 * `org.freedesktop.DBus.Properties.PropertiesChanged`
385 386
 * signal later (e.g. in an idle handler). This technique is useful
 * for collapsing multiple property changes into one.
387 388
 *
 * Since: 2.30
389 390
 */
void
391
g_dbus_interface_skeleton_flush (GDBusInterfaceSkeleton *interface_)
392
{
393 394
  g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
  G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->flush (interface_);
395 396 397 398 399
}

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

static GDBusInterfaceInfo *
400
_g_dbus_interface_skeleton_get_info (GDBusInterface *interface_)
401
{
402 403
  GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
  return g_dbus_interface_skeleton_get_info (interface);
404 405 406
}

static GDBusObject *
407
g_dbus_interface_skeleton_get_object (GDBusInterface *interface_)
408
{
409
  GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
410
  GDBusObject *ret;
411
  g_mutex_lock (&interface->priv->lock);
412
  ret = interface->priv->object;
413
  g_mutex_unlock (&interface->priv->lock);
414
  return ret;
415 416
}

417 418 419 420 421 422 423 424 425 426 427 428 429
static GDBusObject *
g_dbus_interface_skeleton_dup_object (GDBusInterface *interface_)
{
  GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
  GDBusObject *ret;
  g_mutex_lock (&interface->priv->lock);
  ret = interface->priv->object;
  if (ret != NULL)
    g_object_ref (ret);
  g_mutex_unlock (&interface->priv->lock);
  return ret;
}

430
static void
431 432
g_dbus_interface_skeleton_set_object (GDBusInterface *interface_,
                                      GDBusObject    *object)
433
{
434
  GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
435
  g_mutex_lock (&interface->priv->lock);
436 437 438
  if (interface->priv->object != NULL)
    g_object_remove_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object);
  interface->priv->object = object;
439
  if (object != NULL)
440
    g_object_add_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object);
441
  g_mutex_unlock (&interface->priv->lock);
442 443 444 445 446
}

static void
dbus_interface_interface_init (GDBusInterfaceIface *iface)
{
447 448
  iface->get_info    = _g_dbus_interface_skeleton_get_info;
  iface->get_object  = g_dbus_interface_skeleton_get_object;
449
  iface->dup_object  = g_dbus_interface_skeleton_dup_object;
450
  iface->set_object  = g_dbus_interface_skeleton_set_object;
451 452 453 454 455 456 457
}

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

typedef struct
{
  volatile gint ref_count;
458
  GDBusInterfaceSkeleton       *interface;
459 460 461 462 463 464 465 466
  GDBusInterfaceMethodCallFunc  method_call_func;
  GDBusMethodInvocation        *invocation;
} DispatchData;

static void
dispatch_data_unref (DispatchData *data)
{
  if (g_atomic_int_dec_and_test (&data->ref_count))
467
    g_slice_free (DispatchData, data);
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
}

static DispatchData *
dispatch_data_ref (DispatchData *data)
{
  g_atomic_int_inc (&data->ref_count);
  return data;
}

static gboolean
dispatch_invoke_in_context_func (gpointer user_data)
{
  DispatchData *data = user_data;
  data->method_call_func (g_dbus_method_invocation_get_connection (data->invocation),
                          g_dbus_method_invocation_get_sender (data->invocation),
                          g_dbus_method_invocation_get_object_path (data->invocation),
                          g_dbus_method_invocation_get_interface_name (data->invocation),
                          g_dbus_method_invocation_get_method_name (data->invocation),
                          g_dbus_method_invocation_get_parameters (data->invocation),
                          data->invocation,
                          g_dbus_method_invocation_get_user_data (data->invocation));
  return FALSE;
}

492 493 494 495 496
static void
dispatch_in_thread_func (GTask        *task,
                         gpointer      source_object,
                         gpointer      task_data,
                         GCancellable *cancellable)
497
{
498
  DispatchData *data = task_data;
499 500
  GDBusInterfaceSkeletonFlags flags;
  GDBusObject *object;
501 502
  gboolean authorized;

503
  g_mutex_lock (&data->interface->priv->lock);
504 505 506 507
  flags = data->interface->priv->flags;
  object = data->interface->priv->object;
  if (object != NULL)
    g_object_ref (object);
508
  g_mutex_unlock (&data->interface->priv->lock);
509

510 511
  /* first check on the enclosing object (if any), then the interface */
  authorized = TRUE;
512
  if (object != NULL)
513
    {
514
      g_signal_emit_by_name (object,
515
                             "authorize-method",
516
                             data->interface,
517 518 519 520 521
                             data->invocation,
                             &authorized);
    }
  if (authorized)
    {
522
      g_signal_emit (data->interface,
523 524 525 526 527 528 529 530 531
                     signals[G_AUTHORIZE_METHOD_SIGNAL],
                     0,
                     data->invocation,
                     &authorized);
    }

  if (authorized)
    {
      gboolean run_in_thread;
532
      run_in_thread = (flags & G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
      if (run_in_thread)
        {
          /* might as well just re-use the existing thread */
          data->method_call_func (g_dbus_method_invocation_get_connection (data->invocation),
                                  g_dbus_method_invocation_get_sender (data->invocation),
                                  g_dbus_method_invocation_get_object_path (data->invocation),
                                  g_dbus_method_invocation_get_interface_name (data->invocation),
                                  g_dbus_method_invocation_get_method_name (data->invocation),
                                  g_dbus_method_invocation_get_parameters (data->invocation),
                                  data->invocation,
                                  g_dbus_method_invocation_get_user_data (data->invocation));
        }
      else
        {
          /* bah, back to original context */
548 549
          g_main_context_invoke_full (g_task_get_context (task),
                                      g_task_get_priority (task),
550 551 552 553 554 555 556 557 558 559
                                      dispatch_invoke_in_context_func,
                                      dispatch_data_ref (data),
                                      (GDestroyNotify) dispatch_data_unref);
        }
    }
  else
    {
      /* do nothing */
    }

560 561
  if (object != NULL)
    g_object_unref (object);
562 563 564
}

static void
565
g_dbus_interface_method_dispatch_helper (GDBusInterfaceSkeleton       *interface,
566 567 568 569 570 571 572
                                         GDBusInterfaceMethodCallFunc  method_call_func,
                                         GDBusMethodInvocation        *invocation)
{
  gboolean has_handlers;
  gboolean has_default_class_handler;
  gboolean emit_authorized_signal;
  gboolean run_in_thread;
573 574
  GDBusInterfaceSkeletonFlags flags;
  GDBusObject *object;
575

576
  g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface));
577 578 579
  g_return_if_fail (method_call_func != NULL);
  g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));

580
  g_mutex_lock (&interface->priv->lock);
581 582 583 584
  flags = interface->priv->flags;
  object = interface->priv->object;
  if (object != NULL)
    g_object_ref (object);
585
  g_mutex_unlock (&interface->priv->lock);
586

587 588 589 590 591
  /* optimization for the common case where
   *
   *  a) no handler is connected and class handler is not overridden (both interface and object); and
   *  b) method calls are not dispatched in a thread
   */
592
  has_handlers = g_signal_has_handler_pending (interface,
593 594 595
                                               signals[G_AUTHORIZE_METHOD_SIGNAL],
                                               0,
                                               TRUE);
596 597
  has_default_class_handler = (G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface)->g_authorize_method ==
                               g_dbus_interface_skeleton_g_authorize_method_default);
598 599 600 601

  emit_authorized_signal = (has_handlers || !has_default_class_handler);
  if (!emit_authorized_signal)
    {
602 603
      if (object != NULL)
        emit_authorized_signal = _g_dbus_object_skeleton_has_authorize_method_handlers (G_DBUS_OBJECT_SKELETON (object));
604 605
    }

606
  run_in_thread = (flags & G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
607 608 609 610 611 612 613 614 615 616 617 618 619
  if (!emit_authorized_signal && !run_in_thread)
    {
      method_call_func (g_dbus_method_invocation_get_connection (invocation),
                        g_dbus_method_invocation_get_sender (invocation),
                        g_dbus_method_invocation_get_object_path (invocation),
                        g_dbus_method_invocation_get_interface_name (invocation),
                        g_dbus_method_invocation_get_method_name (invocation),
                        g_dbus_method_invocation_get_parameters (invocation),
                        invocation,
                        g_dbus_method_invocation_get_user_data (invocation));
    }
  else
    {
620
      GTask *task;
621
      DispatchData *data;
622 623

      data = g_slice_new0 (DispatchData);
624
      data->interface = interface;
625 626 627
      data->method_call_func = method_call_func;
      data->invocation = invocation;
      data->ref_count = 1;
628 629

      task = g_task_new (interface, NULL, NULL, NULL);
630
      g_task_set_source_tag (task, g_dbus_interface_method_dispatch_helper);
631 632 633
      g_task_set_task_data (task, data, (GDestroyNotify) dispatch_data_unref);
      g_task_run_in_thread (task, dispatch_in_thread_func);
      g_object_unref (task);
634
    }
635 636 637

  if (object != NULL)
    g_object_unref (object);
638 639 640
}

static void
641 642 643 644 645 646 647 648
skeleton_intercept_handle_method_call (GDBusConnection       *connection,
                                       const gchar           *sender,
                                       const gchar           *object_path,
                                       const gchar           *interface_name,
                                       const gchar           *method_name,
                                       GVariant              *parameters,
                                       GDBusMethodInvocation *invocation,
                                       gpointer               user_data)
649
{
650
  GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (user_data);
651
  g_dbus_interface_method_dispatch_helper (interface,
652
                                           g_dbus_interface_skeleton_get_vtable (interface)->method_call,
653 654 655 656 657
                                           invocation);
}

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

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 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
static ConnectionData *
new_connection (GDBusConnection *connection,
                guint            registration_id)
{
  ConnectionData *data;

  data = g_slice_new0 (ConnectionData);
  data->connection      = g_object_ref (connection);
  data->registration_id = registration_id;

  return data;
}

static void
free_connection (ConnectionData *data)
{
  if (data != NULL)
    {
      g_object_unref (data->connection);
      g_slice_free (ConnectionData, data);
    }
}

static gboolean
add_connection_locked (GDBusInterfaceSkeleton *interface_,
                       GDBusConnection        *connection,
                       GError                **error)
{
  ConnectionData *data;
  guint registration_id;
  gboolean ret = FALSE;

  if (interface_->priv->hooked_vtable == NULL)
    {
      /* Hook the vtable since we need to intercept method calls for
       * ::g-authorize-method and for dispatching in thread vs
       * context
       *
       * We need to wait until subclasses have had time to initialize
       * properly before building the hooked_vtable, so we create it
       * once at the last minute.
       */
      interface_->priv->hooked_vtable = g_memdup (g_dbus_interface_skeleton_get_vtable (interface_), sizeof (GDBusInterfaceVTable));
      interface_->priv->hooked_vtable->method_call = skeleton_intercept_handle_method_call;
    }

  registration_id = g_dbus_connection_register_object (connection,
                                                       interface_->priv->object_path,
                                                       g_dbus_interface_skeleton_get_info (interface_),
                                                       interface_->priv->hooked_vtable,
                                                       interface_,
                                                       NULL, /* user_data_free_func */
                                                       error);

  if (registration_id > 0)
    {
      data = new_connection (connection, registration_id);
      interface_->priv->connections = g_slist_append (interface_->priv->connections, data);
      ret = TRUE;
    }

  return ret;
}

static void
remove_connection_locked (GDBusInterfaceSkeleton *interface_,
                          GDBusConnection        *connection)
{
  ConnectionData *data;
  GSList *l;

  /* Get the connection in the list and unregister ... */
  for (l = interface_->priv->connections; l != NULL; l = l->next)
    {
      data = l->data;
      if (data->connection == connection)
        {
          g_warn_if_fail (g_dbus_connection_unregister_object (data->connection, data->registration_id));
          free_connection (data);
          interface_->priv->connections = g_slist_delete_link (interface_->priv->connections, l);
          /* we are guaranteed that the connection is only added once, so bail out early */
          goto out;
        }
    }
 out:
  ;
}

static void
set_object_path_locked (GDBusInterfaceSkeleton *interface_,
                        const gchar            *object_path)
{
  if (g_strcmp0 (interface_->priv->object_path, object_path) != 0)
    {
      g_free (interface_->priv->object_path);
      interface_->priv->object_path = g_strdup (object_path);
    }
}

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

759
/**
760 761
 * g_dbus_interface_skeleton_get_connection:
 * @interface_: A #GDBusInterfaceSkeleton.
762
 *
763
 * Gets the first connection that @interface_ is exported on, if any.
764
 *
765 766
 * Returns: (transfer none): A #GDBusConnection or %NULL if @interface_ is
 * not exported anywhere. Do not free, the object belongs to @interface_.
767 768
 *
 * Since: 2.30
769 770
 */
GDBusConnection *
771
g_dbus_interface_skeleton_get_connection (GDBusInterfaceSkeleton *interface_)
772
{
773
  ConnectionData  *data;
774
  GDBusConnection *ret;
775

776
  g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
777
  g_mutex_lock (&interface_->priv->lock);
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834

  ret = NULL;
  if (interface_->priv->connections != NULL)
    {
      data = interface_->priv->connections->data;
      if (data != NULL)
        ret = data->connection;
    }

  g_mutex_unlock (&interface_->priv->lock);

  return ret;
}

/**
 * g_dbus_interface_skeleton_get_connections:
 * @interface_: A #GDBusInterfaceSkeleton.
 *
 * Gets a list of the connections that @interface_ is exported on.
 *
 * Returns: (element-type GDBusConnection) (transfer full): A list of
 *   all the connections that @interface_ is exported on. The returned
 *   list should be freed with g_list_free() after each element has
 *   been freed with g_object_unref().
 *
 * Since: 2.32
 */
GList *
g_dbus_interface_skeleton_get_connections (GDBusInterfaceSkeleton *interface_)
{
  GList           *connections;
  GSList          *l;
  ConnectionData  *data;

  g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);

  g_mutex_lock (&interface_->priv->lock);
  connections = NULL;

  for (l = interface_->priv->connections; l != NULL; l = l->next)
    {
      data        = l->data;
      connections = g_list_prepend (connections,
                                    /* Return a reference to each connection */
                                    g_object_ref (data->connection));
    }

  g_mutex_unlock (&interface_->priv->lock);

  return g_list_reverse (connections);
}

/**
 * g_dbus_interface_skeleton_has_connection:
 * @interface_: A #GDBusInterfaceSkeleton.
 * @connection: A #GDBusConnection.
 *
835
 * Checks if @interface_ is exported on @connection.
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
 *
 * Returns: %TRUE if @interface_ is exported on @connection, %FALSE otherwise.
 *
 * Since: 2.32
 */
gboolean
g_dbus_interface_skeleton_has_connection (GDBusInterfaceSkeleton     *interface_,
                                          GDBusConnection            *connection)
{
  GSList *l;
  gboolean ret = FALSE;

  g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), FALSE);
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);

  g_mutex_lock (&interface_->priv->lock);

  for (l = interface_->priv->connections; l != NULL; l = l->next)
    {
      ConnectionData *data = l->data;
      if (data->connection == connection)
        {
          ret = TRUE;
          goto out;
        }
    }

 out:
864
  g_mutex_unlock (&interface_->priv->lock);
865
  return ret;
866 867 868
}

/**
869 870
 * g_dbus_interface_skeleton_get_object_path:
 * @interface_: A #GDBusInterfaceSkeleton.
871
 *
872
 * Gets the object path that @interface_ is exported on, if any.
873
 *
874 875
 * Returns: A string owned by @interface_ or %NULL if @interface_ is not exported
 * anywhere. Do not free, the string belongs to @interface_.
876 877
 *
 * Since: 2.30
878 879
 */
const gchar *
880
g_dbus_interface_skeleton_get_object_path (GDBusInterfaceSkeleton *interface_)
881
{
882
  const gchar *ret;
883
  g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
884
  g_mutex_lock (&interface_->priv->lock);
885
  ret = interface_->priv->object_path;
886
  g_mutex_unlock (&interface_->priv->lock);
887
  return ret;
888 889 890
}

/**
891
 * g_dbus_interface_skeleton_export:
892 893
 * @interface_: The D-Bus interface to export.
 * @connection: A #GDBusConnection to export @interface_ on.
894 895 896
 * @object_path: The path to export the interface at.
 * @error: Return location for error or %NULL.
 *
897
 * Exports @interface_ at @object_path on @connection.
898
 *
899 900 901 902
 * This can be called multiple times to export the same @interface_
 * onto multiple connections however the @object_path provided must be
 * the same for all connections.
 *
903
 * Use g_dbus_interface_skeleton_unexport() to unexport the object.
904
 *
905
 * Returns: %TRUE if the interface was exported on @connection, otherwise %FALSE with
906
 * @error set.
907 908
 *
 * Since: 2.30
909 910
 */
gboolean
911 912 913 914
g_dbus_interface_skeleton_export (GDBusInterfaceSkeleton  *interface_,
                                  GDBusConnection         *connection,
                                  const gchar             *object_path,
                                  GError                 **error)
915
{
916
  gboolean ret = FALSE;
917

918 919 920 921
  g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), FALSE);
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
  g_return_val_if_fail (g_variant_is_object_path (object_path), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
922

923 924 925
  /* Assert that the object path is the same for multiple connections here */
  g_return_val_if_fail (interface_->priv->object_path == NULL ||
                        g_strcmp0 (interface_->priv->object_path, object_path) == 0, FALSE);
926

927
  g_mutex_lock (&interface_->priv->lock);
928

929 930
  /* Set the object path */
  set_object_path_locked (interface_, object_path);
931

932 933
  /* Add the connection */
  ret = add_connection_locked (interface_, connection, error);
934

935
  g_mutex_unlock (&interface_->priv->lock);
936 937 938 939
  return ret;
}

/**
940 941
 * g_dbus_interface_skeleton_unexport:
 * @interface_: A #GDBusInterfaceSkeleton.
942
 *
943 944 945
 * Stops exporting @interface_ on all connections it is exported on.
 *
 * To unexport @interface_ from only a single connection, use
946
 * g_dbus_interface_skeleton_unexport_from_connection()
947 948
 *
 * Since: 2.30
949 950
 */
void
951
g_dbus_interface_skeleton_unexport (GDBusInterfaceSkeleton *interface_)
952
{
953
  g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
  g_return_if_fail (interface_->priv->connections != NULL);

  g_mutex_lock (&interface_->priv->lock);

  g_assert (interface_->priv->object_path != NULL);
  g_assert (interface_->priv->hooked_vtable != NULL);

  /* Remove all connections */
  while (interface_->priv->connections != NULL)
    {
      ConnectionData *data = interface_->priv->connections->data;
      remove_connection_locked (interface_, data->connection);
    }

  /* Unset the object path since there are no connections left */
  set_object_path_locked (interface_, NULL);

  g_mutex_unlock (&interface_->priv->lock);
}


/**
 * g_dbus_interface_skeleton_unexport_from_connection:
 * @interface_: A #GDBusInterfaceSkeleton.
 * @connection: A #GDBusConnection.
 *
 * Stops exporting @interface_ on @connection.
 *
 * To stop exporting on all connections the interface is exported on,
 * use g_dbus_interface_skeleton_unexport().
 *
 * Since: 2.32
 */
void
g_dbus_interface_skeleton_unexport_from_connection (GDBusInterfaceSkeleton *interface_,
                                                    GDBusConnection        *connection)
{
  g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
  g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
  g_return_if_fail (interface_->priv->connections != NULL);
994

995
  g_mutex_lock (&interface_->priv->lock);
996

997 998 999
  g_assert (interface_->priv->object_path != NULL);
  g_assert (interface_->priv->hooked_vtable != NULL);

1000
  remove_connection_locked (interface_, connection);
1001

1002 1003 1004
  /* Reset the object path if we removed the last connection */
  if (interface_->priv->connections == NULL)
    set_object_path_locked (interface_, NULL);
1005

1006
  g_mutex_unlock (&interface_->priv->lock);
1007 1008 1009
}

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