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