Revert "GIOScheduler: Avoid constant iteration over pending job list"
[platform/upstream/glib.git] / gio / gapplicationimpl-dbus.c
1 /*
2  * Copyright © 2010 Codethink Limited
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General
15  * Public License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17  * Boston, MA 02111-1307, USA.
18  *
19  * Authors: Ryan Lortie <desrt@desrt.ca>
20  */
21
22 #include "gapplicationimpl.h"
23
24 #include "gactiongroup.h"
25 #include "gactiongroupexporter.h"
26 #include "gremoteactiongroup.h"
27 #include "gdbusactiongroup.h"
28 #include "gapplication.h"
29 #include "gfile.h"
30 #include "gdbusconnection.h"
31 #include "gdbusintrospection.h"
32 #include "gdbuserror.h"
33
34 #include <string.h>
35 #include <stdio.h>
36
37 #include "gapplicationcommandline.h"
38 #include "gdbusmethodinvocation.h"
39
40 G_GNUC_INTERNAL gboolean
41 g_dbus_action_group_sync (GDBusActionGroup  *group,
42                           GCancellable      *cancellable,
43                           GError           **error);
44
45
46 /* DBus Interface definition {{{1 */
47
48 /* For documentation of these interfaces, see
49  * http://live.gnome.org/GTK+/GApplication-dbus-apis
50  */
51 static const gchar org_gtk_Application_xml[] =
52   "<node>"
53     "<interface name='org.gtk.Application'>"
54       "<method name='Activate'>"
55         "<arg type='a{sv}' name='platform-data' direction='in'/>"
56       "</method>"
57       "<method name='Open'>"
58         "<arg type='as' name='uris' direction='in'/>"
59         "<arg type='s' name='hint' direction='in'/>"
60         "<arg type='a{sv}' name='platform-data' direction='in'/>"
61       "</method>"
62       "<method name='CommandLine'>"
63         "<arg type='o' name='path' direction='in'/>"
64         "<arg type='aay' name='arguments' direction='in'/>"
65         "<arg type='a{sv}' name='platform-data' direction='in'/>"
66         "<arg type='i' name='exit-status' direction='out'/>"
67       "</method>"
68     "</interface>"
69   "</node>";
70
71 static GDBusInterfaceInfo *org_gtk_Application;
72
73 static const gchar org_gtk_private_CommandLine_xml[] =
74   "<node>"
75     "<interface name='org.gtk.private.CommandLine'>"
76       "<method name='Print'>"
77         "<arg type='s' name='message' direction='in'/>"
78       "</method>"
79       "<method name='PrintError'>"
80         "<arg type='s' name='message' direction='in'/>"
81       "</method>"
82     "</interface>"
83   "</node>";
84
85 static GDBusInterfaceInfo *org_gtk_private_CommandLine;
86
87 /* GApplication implementation {{{1 */
88 struct _GApplicationImpl
89 {
90   GDBusConnection *session_bus;
91   GActionGroup    *exported_actions;
92   const gchar     *bus_name;
93
94   gchar           *object_path;
95   guint            object_id;
96   guint            actions_id;
97
98   gboolean         properties_live;
99   gboolean         primary;
100   GApplication    *app;
101 };
102
103
104 static GApplicationCommandLine *
105 g_dbus_command_line_new (GDBusMethodInvocation *invocation);
106
107
108 static void
109 g_application_impl_method_call (GDBusConnection       *connection,
110                                 const gchar           *sender,
111                                 const gchar           *object_path,
112                                 const gchar           *interface_name,
113                                 const gchar           *method_name,
114                                 GVariant              *parameters,
115                                 GDBusMethodInvocation *invocation,
116                                 gpointer               user_data)
117 {
118   GApplicationImpl *impl = user_data;
119   GApplicationClass *class;
120
121   class = G_APPLICATION_GET_CLASS (impl->app);
122
123   if (strcmp (method_name, "Activate") == 0)
124     {
125       GVariant *platform_data;
126
127       g_variant_get (parameters, "(@a{sv})", &platform_data);
128       class->before_emit (impl->app, platform_data);
129       g_signal_emit_by_name (impl->app, "activate");
130       class->after_emit (impl->app, platform_data);
131       g_variant_unref (platform_data);
132
133       g_dbus_method_invocation_return_value (invocation, NULL);
134     }
135
136   else if (strcmp (method_name, "Open") == 0)
137     {
138       GVariant *platform_data;
139       const gchar *hint;
140       GVariant *array;
141       GFile **files;
142       gint n, i;
143
144       g_variant_get (parameters, "(@ass@a{sv})",
145                      &array, &hint, &platform_data);
146
147       n = g_variant_n_children (array);
148       files = g_new (GFile *, n + 1);
149
150       for (i = 0; i < n; i++)
151         {
152           const gchar *uri;
153
154           g_variant_get_child (array, i, "&s", &uri);
155           files[i] = g_file_new_for_uri (uri);
156         }
157       g_variant_unref (array);
158       files[n] = NULL;
159
160       class->before_emit (impl->app, platform_data);
161       g_signal_emit_by_name (impl->app, "open", files, n, hint);
162       class->after_emit (impl->app, platform_data);
163
164       g_variant_unref (platform_data);
165
166       for (i = 0; i < n; i++)
167         g_object_unref (files[i]);
168       g_free (files);
169
170       g_dbus_method_invocation_return_value (invocation, NULL);
171     }
172
173   else if (strcmp (method_name, "CommandLine") == 0)
174     {
175       GApplicationCommandLine *cmdline;
176       GVariant *platform_data;
177       int status;
178
179       cmdline = g_dbus_command_line_new (invocation);
180       platform_data = g_variant_get_child_value (parameters, 2);
181       class->before_emit (impl->app, platform_data);
182       g_signal_emit_by_name (impl->app, "command-line", cmdline, &status);
183       g_application_command_line_set_exit_status (cmdline, status);
184       class->after_emit (impl->app, platform_data);
185       g_variant_unref (platform_data);
186       g_object_unref (cmdline);
187     }
188   else
189     g_assert_not_reached ();
190 }
191
192 static gchar *
193 application_path_from_appid (const gchar *appid)
194 {
195   gchar *appid_path, *iter;
196
197   if (appid == NULL)
198     /* this is a private implementation detail */
199     return g_strdup ("/org/gtk/Application/anonymous");
200
201   appid_path = g_strconcat ("/", appid, NULL);
202   for (iter = appid_path; *iter; iter++)
203     {
204       if (*iter == '.')
205         *iter = '/';
206
207       if (*iter == '-')
208         *iter = '_';
209     }
210
211   return appid_path;
212 }
213
214 /* Attempt to become the primary instance.
215  *
216  * Returns %TRUE if everything went OK, regardless of if we became the
217  * primary instance or not.  %FALSE is reserved for when something went
218  * seriously wrong (and @error will be set too, in that case).
219  *
220  * After a %TRUE return, impl->primary will be TRUE if we were
221  * successful.
222  */
223 static gboolean
224 g_application_impl_attempt_primary (GApplicationImpl  *impl,
225                                     GCancellable      *cancellable,
226                                     GError           **error)
227 {
228   const static GDBusInterfaceVTable vtable = {
229     g_application_impl_method_call,
230   };
231   GApplicationClass *app_class = G_APPLICATION_GET_CLASS (impl->app);
232   GVariant *reply;
233   guint32 rval;
234
235   if (org_gtk_Application == NULL)
236     {
237       GError *error = NULL;
238       GDBusNodeInfo *info;
239
240       info = g_dbus_node_info_new_for_xml (org_gtk_Application_xml, &error);
241       if G_UNLIKELY (info == NULL)
242         g_error ("%s", error->message);
243       org_gtk_Application = g_dbus_node_info_lookup_interface (info, "org.gtk.Application");
244       g_assert (org_gtk_Application != NULL);
245       g_dbus_interface_info_ref (org_gtk_Application);
246       g_dbus_node_info_unref (info);
247     }
248
249   /* We could possibly have been D-Bus activated as a result of incoming
250    * requests on either the application or actiongroup interfaces.
251    * Because of how GDBus dispatches messages, we need to ensure that
252    * both of those things are registered before we attempt to request
253    * our name.
254    *
255    * The action group need not be populated yet, as long as it happens
256    * before we return to the mainloop.  The reason for that is because
257    * GDBus does the check to make sure the object exists from the worker
258    * thread but doesn't actually dispatch the action invocation until we
259    * hit the mainloop in this thread.  There is also no danger of
260    * receiving 'activate' or 'open' signals until after 'startup' runs,
261    * for the same reason.
262    */
263   impl->object_id = g_dbus_connection_register_object (impl->session_bus, impl->object_path,
264                                                        org_gtk_Application, &vtable, impl, NULL, error);
265
266   if (impl->object_id == 0)
267     return FALSE;
268
269   impl->actions_id = g_dbus_connection_export_action_group (impl->session_bus, impl->object_path,
270                                                             impl->exported_actions, error);
271
272   if (impl->actions_id == 0)
273     return FALSE;
274
275   if (!app_class->dbus_register (impl->app,
276                                  impl->session_bus,
277                                  impl->object_path,
278                                  error))
279     return FALSE;
280
281   if (impl->bus_name == NULL)
282     {
283       /* If this is a non-unique application then it is sufficient to
284        * have our object paths registered. We can return now.
285        *
286        * Note: non-unique applications always act as primary-instance.
287        */
288       impl->primary = TRUE;
289       return TRUE;
290     }
291
292   /* If this is a unique application then we need to attempt to own
293    * the well-known name and fall back to remote mode (!is_primary)
294    * in the case that we can't do that.
295    */
296   /* DBUS_NAME_FLAG_DO_NOT_QUEUE: 0x4 */
297   reply = g_dbus_connection_call_sync (impl->session_bus, "org.freedesktop.DBus", "/org/freedesktop/DBus",
298                                        "org.freedesktop.DBus", "RequestName",
299                                        g_variant_new ("(su)", impl->bus_name, 0x4), G_VARIANT_TYPE ("(u)"),
300                                        0, -1, cancellable, error);
301
302   if (reply == NULL)
303     return FALSE;
304
305   g_variant_get (reply, "(u)", &rval);
306   g_variant_unref (reply);
307
308   /* DBUS_REQUEST_NAME_REPLY_EXISTS: 3 */
309   impl->primary = (rval != 3);
310
311   return TRUE;
312 }
313
314 /* Stop doing the things that the primary instance does.
315  *
316  * This should be called if attempting to become the primary instance
317  * failed (in order to clean up any partial success) and should also
318  * be called when freeing the GApplication.
319  *
320  * It is safe to call this multiple times.
321  */
322 static void
323 g_application_impl_stop_primary (GApplicationImpl *impl)
324 {
325   GApplicationClass *app_class = G_APPLICATION_GET_CLASS (impl->app);
326
327   app_class->dbus_unregister (impl->app,
328                               impl->session_bus,
329                               impl->object_path);
330
331   if (impl->object_id)
332     {
333       g_dbus_connection_unregister_object (impl->session_bus, impl->object_id);
334       impl->object_id = 0;
335     }
336
337   if (impl->actions_id)
338     {
339       g_dbus_connection_unexport_action_group (impl->session_bus, impl->actions_id);
340       impl->actions_id = 0;
341     }
342
343   if (impl->primary && impl->bus_name)
344     {
345       g_dbus_connection_call (impl->session_bus, "org.freedesktop.DBus",
346                               "/org/freedesktop/DBus", "org.freedesktop.DBus",
347                               "ReleaseName", g_variant_new ("(s)", impl->bus_name),
348                               NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
349       impl->primary = FALSE;
350     }
351 }
352
353 void
354 g_application_impl_destroy (GApplicationImpl *impl)
355 {
356   g_application_impl_stop_primary (impl);
357
358   if (impl->session_bus)
359     g_object_unref (impl->session_bus);
360
361   g_free (impl->object_path);
362
363   g_slice_free (GApplicationImpl, impl);
364 }
365
366 GApplicationImpl *
367 g_application_impl_register (GApplication        *application,
368                              const gchar         *appid,
369                              GApplicationFlags    flags,
370                              GActionGroup        *exported_actions,
371                              GRemoteActionGroup **remote_actions,
372                              GCancellable        *cancellable,
373                              GError             **error)
374 {
375   GDBusActionGroup *actions;
376   GApplicationImpl *impl;
377
378   g_assert ((flags & G_APPLICATION_NON_UNIQUE) || appid != NULL);
379
380   impl = g_slice_new0 (GApplicationImpl);
381
382   impl->app = application;
383   impl->exported_actions = exported_actions;
384
385   /* non-unique applications do not attempt to acquire a bus name */
386   if (~flags & G_APPLICATION_NON_UNIQUE)
387     impl->bus_name = appid;
388
389   impl->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, cancellable, NULL);
390
391   if (impl->session_bus == NULL)
392     {
393       /* If we can't connect to the session bus, proceed as a normal
394        * non-unique application.
395        */
396       *remote_actions = NULL;
397       return impl;
398     }
399
400   impl->object_path = application_path_from_appid (appid);
401
402   /* Only try to be the primary instance if
403    * G_APPLICATION_IS_LAUNCHER was not specified.
404    */
405   if (~flags & G_APPLICATION_IS_LAUNCHER)
406     {
407       if (!g_application_impl_attempt_primary (impl, cancellable, error))
408         {
409           g_application_impl_destroy (impl);
410           return NULL;
411         }
412
413       if (impl->primary)
414         return impl;
415
416       /* We didn't make it.  Drop our service-side stuff. */
417       g_application_impl_stop_primary (impl);
418
419       if (flags & G_APPLICATION_IS_SERVICE)
420         {
421           g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
422                        "Unable to acquire bus name `%s'", appid);
423           g_application_impl_destroy (impl);
424
425           return NULL;
426         }
427     }
428
429   /* We are non-primary.  Try to get the primary's list of actions.
430    * This also serves as a mechanism to ensure that the primary exists
431    * (ie: DBus service files installed correctly, etc).
432    */
433   actions = g_dbus_action_group_get (impl->session_bus, impl->bus_name, impl->object_path);
434   if (!g_dbus_action_group_sync (actions, cancellable, error))
435     {
436       /* The primary appears not to exist.  Fail the registration. */
437       g_application_impl_destroy (impl);
438       g_object_unref (actions);
439
440       return NULL;
441     }
442
443   *remote_actions = G_REMOTE_ACTION_GROUP (actions);
444
445   return impl;
446 }
447
448 void
449 g_application_impl_activate (GApplicationImpl *impl,
450                              GVariant         *platform_data)
451 {
452   g_dbus_connection_call (impl->session_bus,
453                           impl->bus_name,
454                           impl->object_path,
455                           "org.gtk.Application",
456                           "Activate",
457                           g_variant_new ("(@a{sv})", platform_data),
458                           NULL, 0, -1, NULL, NULL, NULL);
459 }
460
461 void
462 g_application_impl_open (GApplicationImpl  *impl,
463                          GFile            **files,
464                          gint               n_files,
465                          const gchar       *hint,
466                          GVariant          *platform_data)
467 {
468   GVariantBuilder builder;
469   gint i;
470
471   g_variant_builder_init (&builder, G_VARIANT_TYPE ("(assa{sv})"));
472   g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
473   for (i = 0; i < n_files; i++)
474     {
475       gchar *uri = g_file_get_uri (files[i]);
476       g_variant_builder_add (&builder, "s", uri);
477       g_free (uri);
478     }
479   g_variant_builder_close (&builder);
480   g_variant_builder_add (&builder, "s", hint);
481   g_variant_builder_add_value (&builder, platform_data);
482
483   g_dbus_connection_call (impl->session_bus,
484                           impl->bus_name,
485                           impl->object_path,
486                           "org.gtk.Application",
487                           "Open",
488                           g_variant_builder_end (&builder),
489                           NULL, 0, -1, NULL, NULL, NULL);
490 }
491
492 static void
493 g_application_impl_cmdline_method_call (GDBusConnection       *connection,
494                                         const gchar           *sender,
495                                         const gchar           *object_path,
496                                         const gchar           *interface_name,
497                                         const gchar           *method_name,
498                                         GVariant              *parameters,
499                                         GDBusMethodInvocation *invocation,
500                                         gpointer               user_data)
501 {
502   const gchar *message;
503
504   g_variant_get_child (parameters, 0, "&s", &message);
505
506   if (strcmp (method_name, "Print") == 0)
507     g_print ("%s", message);
508   else if (strcmp (method_name, "PrintError") == 0)
509     g_printerr ("%s", message);
510   else
511     g_assert_not_reached ();
512
513   g_dbus_method_invocation_return_value (invocation, NULL);
514 }
515
516 typedef struct
517 {
518   GMainLoop *loop;
519   int status;
520 } CommandLineData;
521
522 static void
523 g_application_impl_cmdline_done (GObject      *source,
524                                  GAsyncResult *result,
525                                  gpointer      user_data)
526 {
527   CommandLineData *data = user_data;
528   GError *error = NULL;
529   GVariant *reply;
530
531   reply = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source),
532                                          result, &error);
533
534   if (reply != NULL)
535     {
536       g_variant_get (reply, "(i)", &data->status);
537       g_variant_unref (reply);
538     }
539
540   else
541     {
542       g_printerr ("%s\n", error->message);
543       g_error_free (error);
544       data->status = 1;
545     }
546
547   g_main_loop_quit (data->loop);
548 }
549
550 int
551 g_application_impl_command_line (GApplicationImpl  *impl,
552                                  gchar            **arguments,
553                                  GVariant          *platform_data)
554 {
555   const static GDBusInterfaceVTable vtable = {
556     g_application_impl_cmdline_method_call
557   };
558   const gchar *object_path = "/org/gtk/Application/CommandLine";
559   GMainContext *context;
560   CommandLineData data;
561   guint object_id;
562
563   context = g_main_context_new ();
564   data.loop = g_main_loop_new (context, FALSE);
565   g_main_context_push_thread_default (context);
566
567   if (org_gtk_private_CommandLine == NULL)
568     {
569       GError *error = NULL;
570       GDBusNodeInfo *info;
571
572       info = g_dbus_node_info_new_for_xml (org_gtk_private_CommandLine_xml, &error);
573       if G_UNLIKELY (info == NULL)
574         g_error ("%s", error->message);
575       org_gtk_private_CommandLine = g_dbus_node_info_lookup_interface (info, "org.gtk.private.CommandLine");
576       g_assert (org_gtk_private_CommandLine != NULL);
577       g_dbus_interface_info_ref (org_gtk_private_CommandLine);
578       g_dbus_node_info_unref (info);
579     }
580
581   object_id = g_dbus_connection_register_object (impl->session_bus, object_path,
582                                                  org_gtk_private_CommandLine,
583                                                  &vtable, &data, NULL, NULL);
584   /* In theory we should try other paths... */
585   g_assert (object_id != 0);
586
587   g_dbus_connection_call (impl->session_bus,
588                           impl->bus_name,
589                           impl->object_path,
590                           "org.gtk.Application",
591                           "CommandLine",
592                           g_variant_new ("(o^aay@a{sv})", object_path,
593                                          arguments, platform_data),
594                           G_VARIANT_TYPE ("(i)"), 0, G_MAXINT, NULL,
595                           g_application_impl_cmdline_done, &data);
596
597   g_main_loop_run (data.loop);
598
599   g_main_context_pop_thread_default (context);
600   g_main_context_unref (context);
601   g_main_loop_unref (data.loop);
602
603   return data.status;
604 }
605
606 void
607 g_application_impl_flush (GApplicationImpl *impl)
608 {
609   if (impl->session_bus)
610     g_dbus_connection_flush_sync (impl->session_bus, NULL, NULL);
611 }
612
613 GDBusConnection *
614 g_application_impl_get_dbus_connection (GApplicationImpl *impl)
615 {
616   return impl->session_bus;
617 }
618
619 const gchar *
620 g_application_impl_get_dbus_object_path (GApplicationImpl *impl)
621 {
622   return impl->object_path;
623 }
624
625 /* GDBusCommandLine implementation {{{1 */
626
627 typedef GApplicationCommandLineClass GDBusCommandLineClass;
628 static GType g_dbus_command_line_get_type (void);
629 typedef struct
630 {
631   GApplicationCommandLine  parent_instance;
632   GDBusMethodInvocation   *invocation;
633
634   GDBusConnection *connection;
635   const gchar     *bus_name;
636   const gchar     *object_path;
637 } GDBusCommandLine;
638
639
640 G_DEFINE_TYPE (GDBusCommandLine,
641                g_dbus_command_line,
642                G_TYPE_APPLICATION_COMMAND_LINE)
643
644 static void
645 g_dbus_command_line_print_literal (GApplicationCommandLine *cmdline,
646                                    const gchar             *message)
647 {
648   GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
649
650   g_dbus_connection_call (gdbcl->connection,
651                           gdbcl->bus_name,
652                           gdbcl->object_path,
653                           "org.gtk.private.CommandLine", "Print",
654                           g_variant_new ("(s)", message),
655                           NULL, 0, -1, NULL, NULL, NULL);
656 }
657
658 static void
659 g_dbus_command_line_printerr_literal (GApplicationCommandLine *cmdline,
660                                       const gchar             *message)
661 {
662   GDBusCommandLine *gdbcl = (GDBusCommandLine *) cmdline;
663
664   g_dbus_connection_call (gdbcl->connection,
665                           gdbcl->bus_name,
666                           gdbcl->object_path,
667                           "org.gtk.private.CommandLine", "PrintError",
668                           g_variant_new ("(s)", message),
669                           NULL, 0, -1, NULL, NULL, NULL);
670 }
671
672 static void
673 g_dbus_command_line_finalize (GObject *object)
674 {
675   GApplicationCommandLine *cmdline = G_APPLICATION_COMMAND_LINE (object);
676   GDBusCommandLine *gdbcl = (GDBusCommandLine *) object;
677   gint status;
678
679   status = g_application_command_line_get_exit_status (cmdline);
680
681   g_dbus_method_invocation_return_value (gdbcl->invocation,
682                                          g_variant_new ("(i)", status));
683   g_object_unref (gdbcl->invocation);
684
685   G_OBJECT_CLASS (g_dbus_command_line_parent_class)
686     ->finalize (object);
687 }
688
689 static void
690 g_dbus_command_line_init (GDBusCommandLine *gdbcl)
691 {
692 }
693
694 static void
695 g_dbus_command_line_class_init (GApplicationCommandLineClass *class)
696 {
697   GObjectClass *object_class = G_OBJECT_CLASS (class);
698
699   object_class->finalize = g_dbus_command_line_finalize;
700   class->printerr_literal = g_dbus_command_line_printerr_literal;
701   class->print_literal = g_dbus_command_line_print_literal;
702 }
703
704 static GApplicationCommandLine *
705 g_dbus_command_line_new (GDBusMethodInvocation *invocation)
706 {
707   GDBusCommandLine *gdbcl;
708   GVariant *args;
709
710   args = g_dbus_method_invocation_get_parameters (invocation);
711
712   gdbcl = g_object_new (g_dbus_command_line_get_type (),
713                         "arguments", g_variant_get_child_value (args, 1),
714                         "platform-data", g_variant_get_child_value (args, 2),
715                         NULL);
716   gdbcl->connection = g_dbus_method_invocation_get_connection (invocation);
717   gdbcl->bus_name = g_dbus_method_invocation_get_sender (invocation);
718   g_variant_get_child (args, 0, "&o", &gdbcl->object_path);
719   gdbcl->invocation = g_object_ref (invocation);
720
721   return G_APPLICATION_COMMAND_LINE (gdbcl);
722 }
723
724 /* Epilogue {{{1 */
725
726 /* vim:set foldmethod=marker: */