Pass in NULL instead of g_cclosure_marshal_generic
[platform/upstream/glib.git] / gio / gdbusinterfaceskeleton.c
1 /* GDBus - GLib D-Bus Library
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: David Zeuthen <davidz@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include "gdbusinterface.h"
26 #include "gdbusinterfaceskeleton.h"
27 #include "gdbusobjectskeleton.h"
28 #include "gioenumtypes.h"
29 #include "gdbusprivate.h"
30 #include "gdbusmethodinvocation.h"
31 #include "gdbusconnection.h"
32 #include "gioscheduler.h"
33 #include "gioerror.h"
34
35 #include "glibintl.h"
36
37 /**
38  * SECTION:gdbusinterfaceskeleton
39  * @short_description: Service-side D-Bus interface
40  * @include: gio/gio.h
41  *
42  * Abstract base class for D-Bus interfaces on the service side.
43  */
44
45 struct _GDBusInterfaceSkeletonPrivate
46 {
47   GDBusObject *object;
48   GDBusInterfaceSkeletonFlags flags;
49   guint registration_id;
50
51   GDBusConnection *connection;
52   gchar *object_path;
53   GDBusInterfaceVTable *hooked_vtable;
54 };
55
56 enum
57 {
58   G_AUTHORIZE_METHOD_SIGNAL,
59   LAST_SIGNAL
60 };
61
62 enum
63 {
64   PROP_0,
65   PROP_G_FLAGS
66 };
67
68 static guint signals[LAST_SIGNAL] = {0};
69
70 static void dbus_interface_interface_init (GDBusInterfaceIface *iface);
71
72 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GDBusInterfaceSkeleton, g_dbus_interface_skeleton, G_TYPE_OBJECT,
73                                   G_IMPLEMENT_INTERFACE (G_TYPE_DBUS_INTERFACE, dbus_interface_interface_init));
74
75 static void
76 g_dbus_interface_skeleton_finalize (GObject *object)
77 {
78   GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object);
79   /* unexport if already exported */
80   if (interface->priv->registration_id > 0)
81     g_dbus_interface_skeleton_unexport (interface);
82
83   g_assert (interface->priv->connection == NULL);
84   g_assert (interface->priv->object_path == NULL);
85   g_assert (interface->priv->hooked_vtable == NULL);
86
87   if (interface->priv->object != NULL)
88     g_object_remove_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object);
89   G_OBJECT_CLASS (g_dbus_interface_skeleton_parent_class)->finalize (object);
90 }
91
92 static void
93 g_dbus_interface_skeleton_get_property (GObject      *object,
94                                         guint         prop_id,
95                                         GValue       *value,
96                                         GParamSpec   *pspec)
97 {
98   GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object);
99
100   switch (prop_id)
101     {
102     case PROP_G_FLAGS:
103       g_value_set_flags (value, g_dbus_interface_skeleton_get_flags (interface));
104       break;
105
106     default:
107       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
108       break;
109     }
110 }
111
112 static void
113 g_dbus_interface_skeleton_set_property (GObject      *object,
114                                         guint         prop_id,
115                                         const GValue *value,
116                                         GParamSpec   *pspec)
117 {
118   GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (object);
119
120   switch (prop_id)
121     {
122     case PROP_G_FLAGS:
123       g_dbus_interface_skeleton_set_flags (interface, g_value_get_flags (value));
124       break;
125
126     default:
127       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
128       break;
129     }
130 }
131
132 static gboolean
133 g_dbus_interface_skeleton_g_authorize_method_default (GDBusInterfaceSkeleton    *interface,
134                                                       GDBusMethodInvocation *invocation)
135 {
136   return TRUE;
137 }
138
139 static void
140 g_dbus_interface_skeleton_class_init (GDBusInterfaceSkeletonClass *klass)
141 {
142   GObjectClass *gobject_class;
143
144   gobject_class = G_OBJECT_CLASS (klass);
145   gobject_class->finalize     = g_dbus_interface_skeleton_finalize;
146   gobject_class->set_property = g_dbus_interface_skeleton_set_property;
147   gobject_class->get_property = g_dbus_interface_skeleton_get_property;
148
149   klass->g_authorize_method = g_dbus_interface_skeleton_g_authorize_method_default;
150
151   /**
152    * GDBusInterfaceSkeleton:g-flags:
153    *
154    * Flags from the #GDBusInterfaceSkeletonFlags enumeration.
155    *
156    * Since: 2.30
157    */
158   g_object_class_install_property (gobject_class,
159                                    PROP_G_FLAGS,
160                                    g_param_spec_flags ("g-flags",
161                                                        "g-flags",
162                                                        "Flags for the interface skeleton",
163                                                        G_TYPE_DBUS_INTERFACE_SKELETON_FLAGS,
164                                                        G_DBUS_INTERFACE_SKELETON_FLAGS_NONE,
165                                                        G_PARAM_READABLE |
166                                                        G_PARAM_WRITABLE |
167                                                        G_PARAM_STATIC_STRINGS));
168
169   /**
170    * GDBusInterfaceSkeleton::g-authorize-method:
171    * @interface: The #GDBusInterfaceSkeleton emitting the signal.
172    * @invocation: A #GDBusMethodInvocation.
173    *
174    * Emitted when a method is invoked by a remote caller and used to
175    * determine if the method call is authorized.
176    *
177    * Note that this signal is emitted in a thread dedicated to
178    * handling the method call so handlers are allowed to perform
179    * blocking IO. This means that it is appropriate to call
180    * e.g. <ulink
181    * url="http://hal.freedesktop.org/docs/polkit/PolkitAuthority.html#polkit-authority-check-authorization-sync">polkit_authority_check_authorization_sync()</ulink>
182    * with the <ulink
183    * url="http://hal.freedesktop.org/docs/polkit/PolkitAuthority.html#POLKIT-CHECK-AUTHORIZATION-FLAGS-ALLOW-USER-INTERACTION:CAPS">POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION</ulink> flag set.
184    *
185    * If %FALSE is returned then no further handlers are run and the
186    * signal handler must take a reference to @invocation and finish
187    * handling the call (e.g. return an error via
188    * g_dbus_method_invocation_return_error()).
189    *
190    * Otherwise, if %TRUE is returned, signal emission continues. If no
191    * handlers return %FALSE, then the method is dispatched. If
192    * @interface has an enclosing #GDBusObjectSkeleton, then the
193    * #GDBusObjectSkeleton::authorize-method signal handlers run before
194    * the handlers for this signal.
195    *
196    * The default class handler just returns %TRUE.
197    *
198    * Please note that the common case is optimized: if no signals
199    * handlers are connected and the default class handler isn't
200    * overridden (for both @interface and the enclosing
201    * #GDBusObjectSkeleton, if any) and #GDBusInterfaceSkeleton:g-flags does
202    * not have the
203    * %G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD
204    * flags set, no dedicated thread is ever used and the call will be
205    * handled in the same thread as the object that @interface belongs
206    * to was exported in.
207    *
208    * Returns: %TRUE if the call is authorized, %FALSE otherwise.
209    *
210    * Since: 2.30
211    */
212   signals[G_AUTHORIZE_METHOD_SIGNAL] =
213     g_signal_new ("g-authorize-method",
214                   G_TYPE_DBUS_INTERFACE_SKELETON,
215                   G_SIGNAL_RUN_LAST,
216                   G_STRUCT_OFFSET (GDBusInterfaceSkeletonClass, g_authorize_method),
217                   _g_signal_accumulator_false_handled,
218                   NULL,
219                   NULL,
220                   G_TYPE_BOOLEAN,
221                   1,
222                   G_TYPE_DBUS_METHOD_INVOCATION);
223
224   g_type_class_add_private (klass, sizeof (GDBusInterfaceSkeletonPrivate));
225 }
226
227 static void
228 g_dbus_interface_skeleton_init (GDBusInterfaceSkeleton *interface)
229 {
230   interface->priv = G_TYPE_INSTANCE_GET_PRIVATE (interface, G_TYPE_DBUS_INTERFACE_SKELETON, GDBusInterfaceSkeletonPrivate);
231 }
232
233 /* ---------------------------------------------------------------------------------------------------- */
234
235 /**
236  * g_dbus_interface_skeleton_get_flags:
237  * @interface_: A #GDBusInterfaceSkeleton.
238  *
239  * Gets the #GDBusInterfaceSkeletonFlags that describes what the behavior
240  * of @interface_
241  *
242  * Returns: One or more flags from the #GDBusInterfaceSkeletonFlags enumeration.
243  *
244  * Since: 2.30
245  */
246 GDBusInterfaceSkeletonFlags
247 g_dbus_interface_skeleton_get_flags (GDBusInterfaceSkeleton  *interface_)
248 {
249   g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), G_DBUS_INTERFACE_SKELETON_FLAGS_NONE);
250   return interface_->priv->flags;
251 }
252
253 /**
254  * g_dbus_interface_skeleton_set_flags:
255  * @interface_: A #GDBusInterfaceSkeleton.
256  * @flags: Flags from the #GDBusInterfaceSkeletonFlags enumeration.
257  *
258  * Sets flags describing what the behavior of @skeleton should be.
259  *
260  * Since: 2.30
261  */
262 void
263 g_dbus_interface_skeleton_set_flags (GDBusInterfaceSkeleton      *interface_,
264                                      GDBusInterfaceSkeletonFlags  flags)
265 {
266   g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
267   if (interface_->priv->flags != flags)
268     {
269       interface_->priv->flags = flags;
270       g_object_notify (G_OBJECT (interface_), "g-flags");
271     }
272 }
273
274 /**
275  * g_dbus_interface_skeleton_get_info:
276  * @interface_: A #GDBusInterfaceSkeleton.
277  *
278  * Gets D-Bus introspection information for the D-Bus interface
279  * implemented by @interface_.
280  *
281  * Returns: (transfer none): A #GDBusInterfaceInfo (never %NULL). Do not free.
282  *
283  * Since: 2.30
284  */
285 GDBusInterfaceInfo *
286 g_dbus_interface_skeleton_get_info (GDBusInterfaceSkeleton *interface_)
287 {
288   GDBusInterfaceInfo *ret;
289   g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
290   ret = G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->get_info (interface_);
291   g_warn_if_fail (ret != NULL);
292   return ret;
293 }
294
295 /**
296  * g_dbus_interface_skeleton_get_vtable: (skip)
297  * @interface_: A #GDBusInterfaceSkeleton.
298  *
299  * Gets the interface vtable for the D-Bus interface implemented by
300  * @interface_. The returned function pointers should expect @interface_
301  * itself to be passed as @user_data.
302  *
303  * Returns: A #GDBusInterfaceVTable (never %NULL).
304  *
305  * Since: 2.30
306  */
307 GDBusInterfaceVTable *
308 g_dbus_interface_skeleton_get_vtable (GDBusInterfaceSkeleton *interface_)
309 {
310   GDBusInterfaceVTable *ret;
311   g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
312   ret = G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->get_vtable (interface_);
313   g_warn_if_fail (ret != NULL);
314   return ret;
315 }
316
317 /**
318  * g_dbus_interface_skeleton_get_properties:
319  * @interface_: A #GDBusInterfaceSkeleton.
320  *
321  * Gets all D-Bus properties for @interface_.
322  *
323  * Returns: (transfer full): A #GVariant of type <link linkend="G-VARIANT-TYPE-VARDICT:CAPS">'a{sv}'</link>. Free with g_variant_unref().
324  *
325  * Since: 2.30
326  */
327 GVariant *
328 g_dbus_interface_skeleton_get_properties (GDBusInterfaceSkeleton *interface_)
329 {
330   GVariant *ret;
331   g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
332   ret = G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->get_properties (interface_);
333   return g_variant_take_ref (ret);
334 }
335
336 /**
337  * g_dbus_interface_skeleton_flush:
338  * @interface_: A #GDBusInterfaceSkeleton.
339  *
340  * If @interface_ has outstanding changes, request for these changes to be
341  * emitted immediately.
342  *
343  * For example, an exported D-Bus interface may queue up property
344  * changes and emit the
345  * <literal>org.freedesktop.DBus.Properties::PropertiesChanged</literal>
346  * signal later (e.g. in an idle handler). This technique is useful
347  * for collapsing multiple property changes into one.
348  *
349  * Since: 2.30
350  */
351 void
352 g_dbus_interface_skeleton_flush (GDBusInterfaceSkeleton *interface_)
353 {
354   g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
355   G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface_)->flush (interface_);
356 }
357
358 /* ---------------------------------------------------------------------------------------------------- */
359
360 static GDBusInterfaceInfo *
361 _g_dbus_interface_skeleton_get_info (GDBusInterface *interface_)
362 {
363   GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
364   return g_dbus_interface_skeleton_get_info (interface);
365 }
366
367 static GDBusObject *
368 g_dbus_interface_skeleton_get_object (GDBusInterface *interface_)
369 {
370   GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
371   return interface->priv->object;
372 }
373
374 static void
375 g_dbus_interface_skeleton_set_object (GDBusInterface *interface_,
376                                       GDBusObject    *object)
377 {
378   GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (interface_);
379   if (interface->priv->object != NULL)
380     g_object_remove_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object);
381   interface->priv->object = object;
382   if (object != NULL)
383     g_object_add_weak_pointer (G_OBJECT (interface->priv->object), (gpointer *) &interface->priv->object);
384 }
385
386 static void
387 dbus_interface_interface_init (GDBusInterfaceIface *iface)
388 {
389   iface->get_info    = _g_dbus_interface_skeleton_get_info;
390   iface->get_object  = g_dbus_interface_skeleton_get_object;
391   iface->set_object  = g_dbus_interface_skeleton_set_object;
392 }
393
394 /* ---------------------------------------------------------------------------------------------------- */
395
396 typedef struct
397 {
398   volatile gint ref_count;
399   GDBusInterfaceSkeleton       *interface;
400   GDBusInterfaceMethodCallFunc  method_call_func;
401   GDBusMethodInvocation        *invocation;
402   GMainContext                 *context;
403 } DispatchData;
404
405 static void
406 dispatch_data_unref (DispatchData *data)
407 {
408   if (g_atomic_int_dec_and_test (&data->ref_count))
409     {
410       if (data->context != NULL)
411         g_main_context_unref (data->context);
412       g_free (data);
413     }
414 }
415
416 static DispatchData *
417 dispatch_data_ref (DispatchData *data)
418 {
419   g_atomic_int_inc (&data->ref_count);
420   return data;
421 }
422
423 static gboolean
424 dispatch_invoke_in_context_func (gpointer user_data)
425 {
426   DispatchData *data = user_data;
427   data->method_call_func (g_dbus_method_invocation_get_connection (data->invocation),
428                           g_dbus_method_invocation_get_sender (data->invocation),
429                           g_dbus_method_invocation_get_object_path (data->invocation),
430                           g_dbus_method_invocation_get_interface_name (data->invocation),
431                           g_dbus_method_invocation_get_method_name (data->invocation),
432                           g_dbus_method_invocation_get_parameters (data->invocation),
433                           data->invocation,
434                           g_dbus_method_invocation_get_user_data (data->invocation));
435   return FALSE;
436 }
437
438 static gboolean
439 dispatch_in_thread_func (GIOSchedulerJob *job,
440                          GCancellable    *cancellable,
441                          gpointer         user_data)
442 {
443   DispatchData *data = user_data;
444   gboolean authorized;
445
446   /* first check on the enclosing object (if any), then the interface */
447   authorized = TRUE;
448   if (data->interface->priv->object != NULL)
449     {
450       g_signal_emit_by_name (data->interface->priv->object,
451                              "authorize-method",
452                              data->interface,
453                              data->invocation,
454                              &authorized);
455     }
456   if (authorized)
457     {
458       g_signal_emit (data->interface,
459                      signals[G_AUTHORIZE_METHOD_SIGNAL],
460                      0,
461                      data->invocation,
462                      &authorized);
463     }
464
465   if (authorized)
466     {
467       gboolean run_in_thread;
468       run_in_thread = (data->interface->priv->flags & G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
469       if (run_in_thread)
470         {
471           /* might as well just re-use the existing thread */
472           data->method_call_func (g_dbus_method_invocation_get_connection (data->invocation),
473                                   g_dbus_method_invocation_get_sender (data->invocation),
474                                   g_dbus_method_invocation_get_object_path (data->invocation),
475                                   g_dbus_method_invocation_get_interface_name (data->invocation),
476                                   g_dbus_method_invocation_get_method_name (data->invocation),
477                                   g_dbus_method_invocation_get_parameters (data->invocation),
478                                   data->invocation,
479                                   g_dbus_method_invocation_get_user_data (data->invocation));
480         }
481       else
482         {
483           /* bah, back to original context */
484           g_main_context_invoke_full (data->context,
485                                       G_PRIORITY_DEFAULT,
486                                       dispatch_invoke_in_context_func,
487                                       dispatch_data_ref (data),
488                                       (GDestroyNotify) dispatch_data_unref);
489         }
490     }
491   else
492     {
493       /* do nothing */
494     }
495
496   return FALSE;
497 }
498
499 static void
500 g_dbus_interface_method_dispatch_helper (GDBusInterfaceSkeleton       *interface,
501                                          GDBusInterfaceMethodCallFunc  method_call_func,
502                                          GDBusMethodInvocation        *invocation)
503 {
504   gboolean has_handlers;
505   gboolean has_default_class_handler;
506   gboolean emit_authorized_signal;
507   gboolean run_in_thread;
508
509   g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface));
510   g_return_if_fail (method_call_func != NULL);
511   g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
512
513   /* optimization for the common case where
514    *
515    *  a) no handler is connected and class handler is not overridden (both interface and object); and
516    *  b) method calls are not dispatched in a thread
517    */
518   has_handlers = g_signal_has_handler_pending (interface,
519                                                signals[G_AUTHORIZE_METHOD_SIGNAL],
520                                                0,
521                                                TRUE);
522   has_default_class_handler = (G_DBUS_INTERFACE_SKELETON_GET_CLASS (interface)->g_authorize_method ==
523                                g_dbus_interface_skeleton_g_authorize_method_default);
524
525   emit_authorized_signal = (has_handlers || !has_default_class_handler);
526   if (!emit_authorized_signal)
527     {
528       if (interface->priv->object != NULL)
529         emit_authorized_signal = _g_dbus_object_skeleton_has_authorize_method_handlers (G_DBUS_OBJECT_SKELETON (interface->priv->object));
530     }
531
532   run_in_thread = (interface->priv->flags & G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
533   if (!emit_authorized_signal && !run_in_thread)
534     {
535       method_call_func (g_dbus_method_invocation_get_connection (invocation),
536                         g_dbus_method_invocation_get_sender (invocation),
537                         g_dbus_method_invocation_get_object_path (invocation),
538                         g_dbus_method_invocation_get_interface_name (invocation),
539                         g_dbus_method_invocation_get_method_name (invocation),
540                         g_dbus_method_invocation_get_parameters (invocation),
541                         invocation,
542                         g_dbus_method_invocation_get_user_data (invocation));
543     }
544   else
545     {
546       DispatchData *data;
547       data = g_new0 (DispatchData, 1);
548       data->interface = interface;
549       data->method_call_func = method_call_func;
550       data->invocation = invocation;
551       data->context = g_main_context_get_thread_default ();
552       data->ref_count = 1;
553       if (data->context != NULL)
554         g_main_context_ref (data->context);
555       g_io_scheduler_push_job (dispatch_in_thread_func,
556                                data,
557                                (GDestroyNotify) dispatch_data_unref,
558                                G_PRIORITY_DEFAULT,
559                                NULL); /* GCancellable* */
560     }
561 }
562
563 static void
564 skeleton_intercept_handle_method_call (GDBusConnection       *connection,
565                                        const gchar           *sender,
566                                        const gchar           *object_path,
567                                        const gchar           *interface_name,
568                                        const gchar           *method_name,
569                                        GVariant              *parameters,
570                                        GDBusMethodInvocation *invocation,
571                                        gpointer               user_data)
572 {
573   GDBusInterfaceSkeleton *interface = G_DBUS_INTERFACE_SKELETON (user_data);
574   g_dbus_interface_method_dispatch_helper (interface,
575                                            g_dbus_interface_skeleton_get_vtable (interface)->method_call,
576                                            invocation);
577 }
578
579 /* ---------------------------------------------------------------------------------------------------- */
580
581 /**
582  * g_dbus_interface_skeleton_get_connection:
583  * @interface_: A #GDBusInterfaceSkeleton.
584  *
585  * Gets the connection that @interface_ is exported on, if any.
586  *
587  * Returns: (transfer none): A #GDBusConnection or %NULL if @interface_ is
588  * not exported anywhere. Do not free, the object belongs to @interface_.
589  *
590  * Since: 2.30
591  */
592 GDBusConnection *
593 g_dbus_interface_skeleton_get_connection (GDBusInterfaceSkeleton *interface_)
594 {
595   g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
596   return interface_->priv->connection;
597 }
598
599 /**
600  * g_dbus_interface_skeleton_get_object_path:
601  * @interface_: A #GDBusInterfaceSkeleton.
602  *
603  * Gets the object path that @interface_ is exported on, if any.
604  *
605  * Returns: A string owned by @interface_ or %NULL if @interface_ is not exported
606  * anywhere. Do not free, the string belongs to @interface_.
607  *
608  * Since: 2.30
609  */
610 const gchar *
611 g_dbus_interface_skeleton_get_object_path (GDBusInterfaceSkeleton *interface_)
612 {
613   g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), NULL);
614   return interface_->priv->object_path;
615 }
616
617 /**
618  * g_dbus_interface_skeleton_export:
619  * @interface_: The D-Bus interface to export.
620  * @connection: A #GDBusConnection to export @interface_ on.
621  * @object_path: The path to export the interface at.
622  * @error: Return location for error or %NULL.
623  *
624  * Exports @interface_ at @object_path on @connection.
625  *
626  * Use g_dbus_interface_skeleton_unexport() to unexport the object.
627  *
628  * Returns: %TRUE if the interface was exported, other %FALSE with
629  * @error set.
630  *
631  * Since: 2.30
632  */
633 gboolean
634 g_dbus_interface_skeleton_export (GDBusInterfaceSkeleton  *interface_,
635                                   GDBusConnection         *connection,
636                                   const gchar             *object_path,
637                                   GError                 **error)
638 {
639   gboolean ret;
640
641   g_return_val_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_), 0);
642   g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
643   g_return_val_if_fail (g_variant_is_object_path (object_path), 0);
644   g_return_val_if_fail (error == NULL || *error == NULL, 0);
645
646   ret = FALSE;
647   if (interface_->priv->registration_id > 0)
648     {
649       g_set_error_literal (error,
650                            G_IO_ERROR,
651                            G_IO_ERROR_FAILED, /* TODO: new error code */
652                            "The object is already exported");
653       goto out;
654     }
655
656   g_assert (interface_->priv->connection == NULL);
657   g_assert (interface_->priv->object_path == NULL);
658   g_assert (interface_->priv->hooked_vtable == NULL);
659
660   /* Hook the vtable since we need to intercept method calls for
661    * ::g-authorize-method and for dispatching in thread vs
662    * context
663    */
664   interface_->priv->hooked_vtable = g_memdup (g_dbus_interface_skeleton_get_vtable (interface_), sizeof (GDBusInterfaceVTable));
665   interface_->priv->hooked_vtable->method_call = skeleton_intercept_handle_method_call;
666
667   interface_->priv->connection = g_object_ref (connection);
668   interface_->priv->object_path = g_strdup (object_path);
669   interface_->priv->registration_id = g_dbus_connection_register_object (connection,
670                                                                          object_path,
671                                                                          g_dbus_interface_skeleton_get_info (interface_),
672                                                                          interface_->priv->hooked_vtable,
673                                                                          interface_,
674                                                                          NULL, /* user_data_free_func */
675                                                                          error);
676   if (interface_->priv->registration_id == 0)
677     goto out;
678
679   ret = TRUE;
680
681  out:
682   return ret;
683 }
684
685 /**
686  * g_dbus_interface_skeleton_unexport:
687  * @interface_: A #GDBusInterfaceSkeleton.
688  *
689  * Stops exporting an interface previously exported with
690  * g_dbus_interface_skeleton_export().
691  *
692  * Since: 2.30
693  */
694 void
695 g_dbus_interface_skeleton_unexport (GDBusInterfaceSkeleton *interface_)
696 {
697   g_return_if_fail (G_IS_DBUS_INTERFACE_SKELETON (interface_));
698   g_return_if_fail (interface_->priv->registration_id > 0);
699
700   g_assert (interface_->priv->connection != NULL);
701   g_assert (interface_->priv->object_path != NULL);
702   g_assert (interface_->priv->hooked_vtable != NULL);
703
704   g_warn_if_fail (g_dbus_connection_unregister_object (interface_->priv->connection,
705                                                        interface_->priv->registration_id));
706
707   g_object_unref (interface_->priv->connection);
708   g_free (interface_->priv->object_path);
709   interface_->priv->connection = NULL;
710   interface_->priv->object_path = NULL;
711   interface_->priv->hooked_vtable = NULL;
712   interface_->priv->registration_id = 0;
713 }
714
715 /* ---------------------------------------------------------------------------------------------------- */