Stop using glib-genmarshal at build time
[platform/upstream/glib.git] / gio / gdbusnameowning.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 <stdlib.h>
26
27 #include "gdbusutils.h"
28 #include "gdbusnameowning.h"
29 #include "gdbuserror.h"
30 #include "gdbusprivate.h"
31 #include "gdbusconnection.h"
32
33 #include "glibintl.h"
34
35 /**
36  * SECTION:gdbusnameowning
37  * @title: Owning Bus Names
38  * @short_description: Simple API for owning bus names
39  * @include: gio/gio.h
40  *
41  * Convenience API for owning bus names.
42  *
43  * <example id="gdbus-owning-names"><title>Simple application owning a name</title><programlisting><xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gdbus-example-own-name.c"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include></programlisting></example>
44  */
45
46 G_LOCK_DEFINE_STATIC (lock);
47
48 /* ---------------------------------------------------------------------------------------------------- */
49
50 typedef enum
51 {
52   PREVIOUS_CALL_NONE = 0,
53   PREVIOUS_CALL_ACQUIRED,
54   PREVIOUS_CALL_LOST,
55 } PreviousCall;
56
57 typedef struct
58 {
59   volatile gint             ref_count;
60   guint                     id;
61   GBusNameOwnerFlags        flags;
62   gchar                    *name;
63   GBusAcquiredCallback      bus_acquired_handler;
64   GBusNameAcquiredCallback  name_acquired_handler;
65   GBusNameLostCallback      name_lost_handler;
66   gpointer                  user_data;
67   GDestroyNotify            user_data_free_func;
68   GMainContext             *main_context;
69
70   PreviousCall              previous_call;
71
72   GDBusConnection          *connection;
73   gulong                    disconnected_signal_handler_id;
74   guint                     name_acquired_subscription_id;
75   guint                     name_lost_subscription_id;
76
77   gboolean                  cancelled;
78
79   gboolean                  needs_release;
80 } Client;
81
82 static guint next_global_id = 1;
83 static GHashTable *map_id_to_client = NULL;
84
85
86 static Client *
87 client_ref (Client *client)
88 {
89   g_atomic_int_inc (&client->ref_count);
90   return client;
91 }
92
93 static void
94 client_unref (Client *client)
95 {
96   if (g_atomic_int_dec_and_test (&client->ref_count))
97     {
98       if (client->connection != NULL)
99         {
100           if (client->disconnected_signal_handler_id > 0)
101             g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
102           if (client->name_acquired_subscription_id > 0)
103             g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
104           if (client->name_lost_subscription_id > 0)
105             g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
106           g_object_unref (client->connection);
107         }
108       if (client->main_context != NULL)
109         g_main_context_unref (client->main_context);
110       g_free (client->name);
111       if (client->user_data_free_func != NULL)
112         client->user_data_free_func (client->user_data);
113       g_free (client);
114     }
115 }
116
117 /* ---------------------------------------------------------------------------------------------------- */
118
119
120 typedef enum
121 {
122   CALL_TYPE_NAME_ACQUIRED,
123   CALL_TYPE_NAME_LOST
124 } CallType;
125
126 typedef struct
127 {
128   Client *client;
129
130   /* keep this separate because client->connection may
131    * be set to NULL after scheduling the call
132    */
133   GDBusConnection *connection;
134
135   /* set to TRUE to call acquired */
136   CallType call_type;
137 } CallHandlerData;
138
139 static void
140 call_handler_data_free (CallHandlerData *data)
141 {
142   if (data->connection != NULL)
143     g_object_unref (data->connection);
144   client_unref (data->client);
145   g_free (data);
146 }
147
148 static void
149 actually_do_call (Client *client, GDBusConnection *connection, CallType call_type)
150 {
151   switch (call_type)
152     {
153     case CALL_TYPE_NAME_ACQUIRED:
154       if (client->name_acquired_handler != NULL)
155         {
156           client->name_acquired_handler (connection,
157                                          client->name,
158                                          client->user_data);
159         }
160       break;
161
162     case CALL_TYPE_NAME_LOST:
163       if (client->name_lost_handler != NULL)
164         {
165           client->name_lost_handler (connection,
166                                      client->name,
167                                      client->user_data);
168         }
169       break;
170
171     default:
172       g_assert_not_reached ();
173       break;
174     }
175 }
176
177 static gboolean
178 call_in_idle_cb (gpointer _data)
179 {
180   CallHandlerData *data = _data;
181   actually_do_call (data->client, data->connection, data->call_type);
182   return FALSE;
183 }
184
185 static void
186 schedule_call_in_idle (Client *client, CallType  call_type)
187 {
188   CallHandlerData *data;
189   GSource *idle_source;
190
191   data = g_new0 (CallHandlerData, 1);
192   data->client = client_ref (client);
193   data->connection = client->connection != NULL ? g_object_ref (client->connection) : NULL;
194   data->call_type = call_type;
195
196   idle_source = g_idle_source_new ();
197   g_source_set_priority (idle_source, G_PRIORITY_HIGH);
198   g_source_set_callback (idle_source,
199                          call_in_idle_cb,
200                          data,
201                          (GDestroyNotify) call_handler_data_free);
202   g_source_attach (idle_source, client->main_context);
203   g_source_unref (idle_source);
204 }
205
206 static void
207 do_call (Client *client, CallType call_type)
208 {
209   /* only schedule in idle if we're not in the right thread */
210   if (g_main_context_get_thread_default () != client->main_context)
211     schedule_call_in_idle (client, call_type);
212   else
213     actually_do_call (client, client->connection, call_type);
214 }
215
216 static void
217 call_acquired_handler (Client *client)
218 {
219   if (client->previous_call != PREVIOUS_CALL_ACQUIRED)
220     {
221       client->previous_call = PREVIOUS_CALL_ACQUIRED;
222       if (!client->cancelled)
223         {
224           do_call (client, CALL_TYPE_NAME_ACQUIRED);
225         }
226     }
227 }
228
229 static void
230 call_lost_handler (Client  *client)
231 {
232   if (client->previous_call != PREVIOUS_CALL_LOST)
233     {
234       client->previous_call = PREVIOUS_CALL_LOST;
235       if (!client->cancelled)
236         {
237           do_call (client, CALL_TYPE_NAME_LOST);
238         }
239     }
240 }
241
242 /* ---------------------------------------------------------------------------------------------------- */
243
244 static void
245 on_name_lost_or_acquired (GDBusConnection  *connection,
246                           const gchar      *sender_name,
247                           const gchar      *object_path,
248                           const gchar      *interface_name,
249                           const gchar      *signal_name,
250                           GVariant         *parameters,
251                           gpointer          user_data)
252 {
253   Client *client = user_data;
254   const gchar *name;
255
256   if (g_strcmp0 (object_path, "/org/freedesktop/DBus") != 0 ||
257       g_strcmp0 (interface_name, "org.freedesktop.DBus") != 0 ||
258       g_strcmp0 (sender_name, "org.freedesktop.DBus") != 0)
259     goto out;
260
261   if (g_strcmp0 (signal_name, "NameLost") == 0)
262     {
263       g_variant_get (parameters, "(&s)", &name);
264       if (g_strcmp0 (name, client->name) == 0)
265         {
266           call_lost_handler (client);
267         }
268     }
269   else if (g_strcmp0 (signal_name, "NameAcquired") == 0)
270     {
271       g_variant_get (parameters, "(&s)", &name);
272       if (g_strcmp0 (name, client->name) == 0)
273         {
274           call_acquired_handler (client);
275         }
276     }
277  out:
278   ;
279 }
280
281 /* ---------------------------------------------------------------------------------------------------- */
282
283 static void
284 request_name_cb (GObject      *source_object,
285                  GAsyncResult *res,
286                  gpointer      user_data)
287 {
288   Client *client = user_data;
289   GVariant *result;
290   guint32 request_name_reply;
291   gboolean subscribe;
292
293   request_name_reply = 0;
294   result = NULL;
295
296   result = g_dbus_connection_call_finish (client->connection,
297                                           res,
298                                           NULL);
299   if (result != NULL)
300     {
301       g_variant_get (result, "(u)", &request_name_reply);
302       g_variant_unref (result);
303     }
304
305   subscribe = FALSE;
306
307   switch (request_name_reply)
308     {
309     case 1: /* DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER */
310       /* We got the name - now listen for NameLost and NameAcquired */
311       call_acquired_handler (client);
312       subscribe = TRUE;
313       client->needs_release = TRUE;
314       break;
315
316     case 2: /* DBUS_REQUEST_NAME_REPLY_IN_QUEUE */
317       /* Waiting in line - listen for NameLost and NameAcquired */
318       call_lost_handler (client);
319       subscribe = TRUE;
320       client->needs_release = TRUE;
321       break;
322
323     default:
324       /* assume we couldn't get the name - explicit fallthrough */
325     case 3: /* DBUS_REQUEST_NAME_REPLY_EXISTS */
326     case 4: /* DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER */
327       /* Some other part of the process is already owning the name */
328       call_lost_handler (client);
329       break;
330     }
331
332   if (subscribe)
333     {
334       /* start listening to NameLost and NameAcquired messages */
335       client->name_lost_subscription_id =
336         g_dbus_connection_signal_subscribe (client->connection,
337                                             "org.freedesktop.DBus",
338                                             "org.freedesktop.DBus",
339                                             "NameLost",
340                                             "/org/freedesktop/DBus",
341                                             client->name,
342                                             G_DBUS_SIGNAL_FLAGS_NONE,
343                                             on_name_lost_or_acquired,
344                                             client,
345                                             NULL);
346       client->name_acquired_subscription_id =
347         g_dbus_connection_signal_subscribe (client->connection,
348                                             "org.freedesktop.DBus",
349                                             "org.freedesktop.DBus",
350                                             "NameAcquired",
351                                             "/org/freedesktop/DBus",
352                                             client->name,
353                                             G_DBUS_SIGNAL_FLAGS_NONE,
354                                             on_name_lost_or_acquired,
355                                             client,
356                                             NULL);
357     }
358
359   client_unref (client);
360 }
361
362 /* ---------------------------------------------------------------------------------------------------- */
363
364 static void
365 on_connection_disconnected (GDBusConnection *connection,
366                             gboolean         remote_peer_vanished,
367                             GError          *error,
368                             gpointer         user_data)
369 {
370   Client *client = user_data;
371
372   if (client->disconnected_signal_handler_id > 0)
373     g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
374   if (client->name_acquired_subscription_id > 0)
375     g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
376   if (client->name_lost_subscription_id > 0)
377     g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
378   g_object_unref (client->connection);
379   client->disconnected_signal_handler_id = 0;
380   client->name_acquired_subscription_id = 0;
381   client->name_lost_subscription_id = 0;
382   client->connection = NULL;
383
384   call_lost_handler (client);
385 }
386
387 /* ---------------------------------------------------------------------------------------------------- */
388
389 static void
390 has_connection (Client *client)
391 {
392   /* listen for disconnection */
393   client->disconnected_signal_handler_id = g_signal_connect (client->connection,
394                                                              "closed",
395                                                              G_CALLBACK (on_connection_disconnected),
396                                                              client);
397
398   /* attempt to acquire the name */
399   g_dbus_connection_call (client->connection,
400                           "org.freedesktop.DBus",  /* bus name */
401                           "/org/freedesktop/DBus", /* object path */
402                           "org.freedesktop.DBus",  /* interface name */
403                           "RequestName",           /* method name */
404                           g_variant_new ("(su)",
405                                          client->name,
406                                          client->flags),
407                           G_VARIANT_TYPE ("(u)"),
408                           G_DBUS_CALL_FLAGS_NONE,
409                           -1,
410                           NULL,
411                           (GAsyncReadyCallback) request_name_cb,
412                           client_ref (client));
413 }
414
415
416 static void
417 connection_get_cb (GObject      *source_object,
418                    GAsyncResult *res,
419                    gpointer      user_data)
420 {
421   Client *client = user_data;
422
423   client->connection = g_bus_get_finish (res, NULL);
424   if (client->connection == NULL)
425     {
426       call_lost_handler (client);
427       goto out;
428     }
429
430   /* No need to schedule this in idle as we're already in the thread
431    * that the user called g_bus_own_name() from. This is because
432    * g_bus_get() guarantees that.
433    *
434    * Also, we need to ensure that the handler is invoked *before*
435    * we call RequestName(). Otherwise there is a race.
436    */
437   if (client->bus_acquired_handler != NULL)
438     {
439       client->bus_acquired_handler (client->connection,
440                                     client->name,
441                                     client->user_data);
442     }
443
444   has_connection (client);
445
446  out:
447   client_unref (client);
448 }
449
450 /* ---------------------------------------------------------------------------------------------------- */
451
452 /**
453  * g_bus_own_name_on_connection:
454  * @connection: A #GDBusConnection.
455  * @name: The well-known name to own.
456  * @flags: A set of flags from the #GBusNameOwnerFlags enumeration.
457  * @name_acquired_handler: Handler to invoke when @name is acquired or %NULL.
458  * @name_lost_handler: Handler to invoke when @name is lost or %NULL.
459  * @user_data: User data to pass to handlers.
460  * @user_data_free_func: Function for freeing @user_data or %NULL.
461  *
462  * Like g_bus_own_name() but takes a #GDBusConnection instead of a
463  * #GBusType.
464  *
465  * Returns: An identifier (never 0) that an be used with
466  * g_bus_unown_name() to stop owning the name.
467  *
468  * Since: 2.26
469  */
470 guint
471 g_bus_own_name_on_connection (GDBusConnection          *connection,
472                               const gchar              *name,
473                               GBusNameOwnerFlags        flags,
474                               GBusNameAcquiredCallback  name_acquired_handler,
475                               GBusNameLostCallback      name_lost_handler,
476                               gpointer                  user_data,
477                               GDestroyNotify            user_data_free_func)
478 {
479   Client *client;
480
481   g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
482   g_return_val_if_fail (g_dbus_is_name (name) && !g_dbus_is_unique_name (name), 0);
483
484   G_LOCK (lock);
485
486   client = g_new0 (Client, 1);
487   client->ref_count = 1;
488   client->id = next_global_id++; /* TODO: uh oh, handle overflow */
489   client->name = g_strdup (name);
490   client->flags = flags;
491   client->name_acquired_handler = name_acquired_handler;
492   client->name_lost_handler = name_lost_handler;
493   client->user_data = user_data;
494   client->user_data_free_func = user_data_free_func;
495   client->main_context = g_main_context_get_thread_default ();
496   if (client->main_context != NULL)
497     g_main_context_ref (client->main_context);
498
499   client->connection = g_object_ref (connection);
500
501   if (map_id_to_client == NULL)
502     {
503       map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
504     }
505   g_hash_table_insert (map_id_to_client,
506                        GUINT_TO_POINTER (client->id),
507                        client);
508
509   G_UNLOCK (lock);
510
511   has_connection (client);
512
513   return client->id;
514 }
515
516 /**
517  * g_bus_own_name:
518  * @bus_type: The type of bus to own a name on.
519  * @name: The well-known name to own.
520  * @flags: A set of flags from the #GBusNameOwnerFlags enumeration.
521  * @bus_acquired_handler: Handler to invoke when connected to the bus of type @bus_type or %NULL.
522  * @name_acquired_handler: Handler to invoke when @name is acquired or %NULL.
523  * @name_lost_handler: Handler to invoke when @name is lost or %NULL.
524  * @user_data: User data to pass to handlers.
525  * @user_data_free_func: Function for freeing @user_data or %NULL.
526  *
527  * Starts acquiring @name on the bus specified by @bus_type and calls
528  * @name_acquired_handler and @name_lost_handler when the name is
529  * acquired respectively lost. Callbacks will be invoked in the <link
530  * linkend="g-main-context-push-thread-default">thread-default main
531  * loop</link> of the thread you are calling this function from.
532  *
533  * You are guaranteed that one of the @name_acquired_handler and @name_lost_handler
534  * callbacks will be invoked after calling this function - there are three
535  * possible cases:
536  * <itemizedlist>
537  *   <listitem><para>
538  *     @name_lost_handler with a %NULL connection (if a connection to the bus can't be made).
539  *   </para></listitem>
540  *   <listitem><para>
541  *     @bus_acquired_handler then @name_lost_handler (if the name can't be obtained)
542  *   </para></listitem>
543  *   <listitem><para>
544  *     @bus_acquired_handler then @name_acquired_handler (if the name was obtained).
545  *   </para></listitem>
546  * </itemizedlist>
547  * When you are done owning the name, just call g_bus_unown_name()
548  * with the owner id this function returns.
549  *
550  * If the name is acquired or lost (for example another application
551  * could acquire the name if you allow replacement or the application
552  * currently owning the name exits), the handlers are also invoked. If the
553  * #GDBusConnection that is used for attempting to own the name
554  * closes, then @name_lost_handler is invoked since it is no
555  * longer possible for other processes to access the process.
556  *
557  * You cannot use g_bus_own_name() several times for the same name (unless
558  * interleaved with calls to g_bus_unown_name()) - only the first call
559  * will work.
560  *
561  * Another guarantee is that invocations of @name_acquired_handler
562  * and @name_lost_handler are guaranteed to alternate; that
563  * is, if @name_acquired_handler is invoked then you are
564  * guaranteed that the next time one of the handlers is invoked, it
565  * will be @name_lost_handler. The reverse is also true.
566  *
567  * If you plan on exporting objects (using e.g.
568  * g_dbus_connection_register_object()), note that it is generally too late
569  * to export the objects in @name_acquired_handler. Instead, you can do this
570  * in @bus_acquired_handler since you are guaranteed that this will run
571  * before @name is requested from the bus.
572  *
573  * This behavior makes it very simple to write applications that wants
574  * to own names and export objects, see <xref linkend="gdbus-owning-names"/>.
575  * Simply register objects to be exported in @bus_acquired_handler and
576  * unregister the objects (if any) in @name_lost_handler.
577  *
578  * Returns: An identifier (never 0) that an be used with
579  * g_bus_unown_name() to stop owning the name.
580  *
581  * Since: 2.26
582  */
583 guint
584 g_bus_own_name (GBusType                  bus_type,
585                 const gchar              *name,
586                 GBusNameOwnerFlags        flags,
587                 GBusAcquiredCallback      bus_acquired_handler,
588                 GBusNameAcquiredCallback  name_acquired_handler,
589                 GBusNameLostCallback      name_lost_handler,
590                 gpointer                  user_data,
591                 GDestroyNotify            user_data_free_func)
592 {
593   Client *client;
594
595   g_return_val_if_fail (g_dbus_is_name (name) && !g_dbus_is_unique_name (name), 0);
596
597   G_LOCK (lock);
598
599   client = g_new0 (Client, 1);
600   client->ref_count = 1;
601   client->id = next_global_id++; /* TODO: uh oh, handle overflow */
602   client->name = g_strdup (name);
603   client->flags = flags;
604   client->bus_acquired_handler = bus_acquired_handler;
605   client->name_acquired_handler = name_acquired_handler;
606   client->name_lost_handler = name_lost_handler;
607   client->user_data = user_data;
608   client->user_data_free_func = user_data_free_func;
609   client->main_context = g_main_context_get_thread_default ();
610   if (client->main_context != NULL)
611     g_main_context_ref (client->main_context);
612
613   if (map_id_to_client == NULL)
614     {
615       map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
616     }
617   g_hash_table_insert (map_id_to_client,
618                        GUINT_TO_POINTER (client->id),
619                        client);
620
621   g_bus_get (bus_type,
622              NULL,
623              connection_get_cb,
624              client_ref (client));
625
626   G_UNLOCK (lock);
627
628   return client->id;
629 }
630
631 typedef struct {
632   GClosure *bus_acquired_closure;
633   GClosure *name_acquired_closure;
634   GClosure *name_lost_closure;
635 } OwnNameData;
636
637 static OwnNameData *
638 own_name_data_new (GClosure *bus_acquired_closure,
639                    GClosure *name_acquired_closure,
640                    GClosure *name_lost_closure)
641 {
642   OwnNameData *data;
643
644   data = g_new0 (OwnNameData, 1);
645
646   if (bus_acquired_closure != NULL)
647     {
648       data->bus_acquired_closure = g_closure_ref (bus_acquired_closure);
649       g_closure_sink (bus_acquired_closure);
650       if (G_CLOSURE_NEEDS_MARSHAL (bus_acquired_closure))
651         g_closure_set_marshal (bus_acquired_closure, g_cclosure_marshal_generic);
652     }
653
654   if (name_acquired_closure != NULL)
655     {
656       data->name_acquired_closure = g_closure_ref (name_acquired_closure);
657       g_closure_sink (name_acquired_closure);
658       if (G_CLOSURE_NEEDS_MARSHAL (name_acquired_closure))
659         g_closure_set_marshal (name_acquired_closure, g_cclosure_marshal_generic);
660     }
661
662   if (name_lost_closure != NULL)
663     {
664       data->name_lost_closure = g_closure_ref (name_lost_closure);
665       g_closure_sink (name_lost_closure);
666       if (G_CLOSURE_NEEDS_MARSHAL (name_lost_closure))
667         g_closure_set_marshal (name_lost_closure, g_cclosure_marshal_generic);
668     }
669
670   return data;
671 }
672
673 static void
674 own_with_closures_on_bus_acquired (GDBusConnection *connection,
675                                    const gchar     *name,
676                                    gpointer         user_data)
677 {
678   OwnNameData *data = user_data;
679   GValue params[2] = { { 0, }, { 0, } };
680
681   g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
682   g_value_set_object (&params[0], connection);
683
684   g_value_init (&params[1], G_TYPE_STRING);
685   g_value_set_string (&params[1], name);
686
687   g_closure_invoke (data->bus_acquired_closure, NULL, 2, params, NULL);
688
689   g_value_unset (params + 0);
690   g_value_unset (params + 1);
691 }
692
693 static void
694 own_with_closures_on_name_acquired (GDBusConnection *connection,
695                                     const gchar     *name,
696                                     gpointer         user_data)
697 {
698   OwnNameData *data = user_data;
699   GValue params[2] = { { 0, }, { 0, } };
700
701   g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
702   g_value_set_object (&params[0], connection);
703
704   g_value_init (&params[1], G_TYPE_STRING);
705   g_value_set_string (&params[1], name);
706
707   g_closure_invoke (data->name_acquired_closure, NULL, 2, params, NULL);
708
709   g_value_unset (params + 0);
710   g_value_unset (params + 1);
711 }
712
713 static void
714 own_with_closures_on_name_lost (GDBusConnection *connection,
715                                 const gchar     *name,
716                                 gpointer         user_data)
717 {
718   OwnNameData *data = user_data;
719   GValue params[2] = { { 0, }, { 0, } };
720
721   g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
722   g_value_set_object (&params[0], connection);
723
724   g_value_init (&params[1], G_TYPE_STRING);
725   g_value_set_string (&params[1], name);
726
727   g_closure_invoke (data->name_lost_closure, NULL, 2, params, NULL);
728
729   g_value_unset (params + 0);
730   g_value_unset (params + 1);
731 }
732
733 static void
734 bus_own_name_free_func (gpointer user_data)
735 {
736   OwnNameData *data = user_data;
737
738   if (data->bus_acquired_closure != NULL)
739     g_closure_unref (data->bus_acquired_closure);
740
741   if (data->name_acquired_closure != NULL)
742     g_closure_unref (data->name_acquired_closure);
743
744   if (data->name_lost_closure != NULL)
745     g_closure_unref (data->name_lost_closure);
746
747   g_free (data);
748 }
749
750 /**
751  * g_bus_own_name_with_closures:
752  * @bus_type: The type of bus to own a name on.
753  * @name: The well-known name to own.
754  * @flags: A set of flags from the #GBusNameOwnerFlags enumeration.
755  * @bus_acquired_closure: (allow-none): #GClosure to invoke when connected to
756  * the bus of type @bus_type or %NULL.
757  * @name_acquired_closure: (allow-none): #GClosure to invoke when @name is
758  * acquired or %NULL.
759  * @name_lost_closure: (allow-none): #GClosure to invoke when @name is lost or
760  * %NULL.
761  *
762  * Version of g_bus_own_name() using closures instead of callbacks for
763  * easier binding in other languages.
764  *
765  * Returns: An identifier (never 0) that an be used with
766  * g_bus_unown_name() to stop owning the name.
767  *
768  * Rename to: g_bus_own_name
769  *
770  * Since: 2.26
771  */
772 guint
773 g_bus_own_name_with_closures (GBusType                  bus_type,
774                               const gchar              *name,
775                               GBusNameOwnerFlags        flags,
776                               GClosure                 *bus_acquired_closure,
777                               GClosure                 *name_acquired_closure,
778                               GClosure                 *name_lost_closure)
779 {
780   return g_bus_own_name (bus_type,
781           name,
782           flags,
783           bus_acquired_closure != NULL ? own_with_closures_on_bus_acquired : NULL,
784           name_acquired_closure != NULL ? own_with_closures_on_name_acquired : NULL,
785           name_lost_closure != NULL ? own_with_closures_on_name_lost : NULL,
786           own_name_data_new (bus_acquired_closure,
787                              name_acquired_closure,
788                              name_lost_closure),
789           bus_own_name_free_func);
790 }
791
792 /**
793  * g_bus_own_name_on_connection_with_closures:
794  * @connection: A #GDBusConnection.
795  * @name: The well-known name to own.
796  * @flags: A set of flags from the #GBusNameOwnerFlags enumeration.
797  * @name_acquired_closure: (allow-none): #GClosure to invoke when @name is
798  * acquired or %NULL.
799  * @name_lost_closure: (allow-none): #GClosure to invoke when @name is lost or
800  * %NULL.
801  *
802  * Version of g_bus_own_name_on_connection() using closures instead of callbacks for
803  * easier binding in other languages.
804  *
805  * Returns: An identifier (never 0) that an be used with
806  * g_bus_unown_name() to stop owning the name.
807  *
808  * Rename to: g_bus_own_name_on_connection
809  *
810  * Since: 2.26
811  */
812 guint
813 g_bus_own_name_on_connection_with_closures (GDBusConnection          *connection,
814                                             const gchar              *name,
815                                             GBusNameOwnerFlags        flags,
816                                             GClosure                 *name_acquired_closure,
817                                             GClosure                 *name_lost_closure)
818 {
819   return g_bus_own_name_on_connection (connection,
820           name,
821           flags,
822           name_acquired_closure != NULL ? own_with_closures_on_name_acquired : NULL,
823           name_lost_closure != NULL ? own_with_closures_on_name_lost : NULL,
824           own_name_data_new (NULL,
825                              name_acquired_closure,
826                              name_lost_closure),
827           bus_own_name_free_func);
828 }
829
830 /**
831  * g_bus_unown_name:
832  * @owner_id: An identifier obtained from g_bus_own_name()
833  *
834  * Stops owning a name.
835  *
836  * Since: 2.26
837  */
838 void
839 g_bus_unown_name (guint owner_id)
840 {
841   Client *client;
842
843   g_return_if_fail (owner_id > 0);
844
845   client = NULL;
846
847   G_LOCK (lock);
848   if (owner_id == 0 || map_id_to_client == NULL ||
849       (client = g_hash_table_lookup (map_id_to_client, GUINT_TO_POINTER (owner_id))) == NULL)
850     {
851       g_warning ("Invalid id %d passed to g_bus_unown_name()", owner_id);
852       goto out;
853     }
854
855   client->cancelled = TRUE;
856   g_warn_if_fail (g_hash_table_remove (map_id_to_client, GUINT_TO_POINTER (owner_id)));
857
858  out:
859   G_UNLOCK (lock);
860
861   /* do callback without holding lock */
862   if (client != NULL)
863     {
864       /* Release the name if needed */
865       if (client->needs_release && client->connection != NULL)
866         {
867           GVariant *result;
868           GError *error;
869           guint32 release_name_reply;
870
871           /* TODO: it kinda sucks having to do a sync call to release the name - but if
872            * we don't, then a subsequent grab of the name will make the bus daemon return
873            * IN_QUEUE which will trigger name_lost().
874            *
875            * I believe this is a bug in the bus daemon.
876            */
877           error = NULL;
878           result = g_dbus_connection_call_sync (client->connection,
879                                                 "org.freedesktop.DBus",  /* bus name */
880                                                 "/org/freedesktop/DBus", /* object path */
881                                                 "org.freedesktop.DBus",  /* interface name */
882                                                 "ReleaseName",           /* method name */
883                                                 g_variant_new ("(s)", client->name),
884                                                 G_VARIANT_TYPE ("(u)"),
885                                                 G_DBUS_CALL_FLAGS_NONE,
886                                                 -1,
887                                                 NULL,
888                                                 &error);
889           if (result == NULL)
890             {
891               g_warning ("Error releasing name %s: %s", client->name, error->message);
892               g_error_free (error);
893             }
894           else
895             {
896               g_variant_get (result, "(u)", &release_name_reply);
897               if (release_name_reply != 1 /* DBUS_RELEASE_NAME_REPLY_RELEASED */)
898                 {
899                   g_warning ("Unexpected reply %d when releasing name %s", release_name_reply, client->name);
900                 }
901               g_variant_unref (result);
902             }
903         }
904
905       if (client->disconnected_signal_handler_id > 0)
906         g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
907       if (client->name_acquired_subscription_id > 0)
908         g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
909       if (client->name_lost_subscription_id > 0)
910         g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
911       client->disconnected_signal_handler_id = 0;
912       client->name_acquired_subscription_id = 0;
913       client->name_lost_subscription_id = 0;
914       if (client->connection != NULL)
915         {
916           g_object_unref (client->connection);
917           client->connection = NULL;
918         }
919
920       client_unref (client);
921     }
922 }