Debian Codegraph @

liblomiri-app-launch/second-exec-core.c

raw
1 /*
2 * Copyright 2013 Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 3, as published
6 * by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranties of
10 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 * PURPOSE. See the GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authors:
17 * Ted Gould <[email protected]>
18 */
19
20 #include <gio/gio.h>
21 #include "liblomiri-app-launch/lomiri-app-launch.h"
22 #include "utils.h"
23 #include "second-exec-core.h"
24 #include "lomiri-app-launch-trace.h"
25 #include "ual-tracepoint.h"
26
27 typedef struct {
28 GDBusConnection * bus;
29 gchar * appid;
30 gchar * instanceid;
31 gchar ** input_uris;
32 GPid app_pid;
33 guint connections_open;
34 GVariant * app_data;
35 gchar * dbus_path;
36 guint64 lomiri_starttime;
37 GSource * timer;
38 guint signal;
39 } second_exec_t;
40
41 static void second_exec_complete (second_exec_t * data);
42
43 static GSource *
44 thread_default_timeout (guint interval, GSourceFunc func, gpointer data)
45 {
46 GSource * src = g_timeout_source_new(interval);
47 GMainContext * context = g_main_context_get_thread_default();
48
49 g_source_set_callback(src, func, data, NULL);
50
51 g_source_attach(src, context);
52
53 return src;
54 }
55
56 /* Lomiri didn't respond in time, continue on */
57 static gboolean
58 timer_cb (gpointer user_data)
59 {
60 ual_tracepoint(second_exec_resume_timeout, ((second_exec_t *)user_data)->appid);
61 g_warning("Lomiri didn't respond in 500ms to resume the app");
62
63 second_exec_complete(user_data);
64 return G_SOURCE_REMOVE;
65 }
66
67 /* Lower the connection count and process if it gets to zero */
68 static void
69 connection_count_dec (second_exec_t * data)
70 {
71 ual_tracepoint(second_exec_connection_complete, data->appid);
72 data->connections_open--;
73 if (data->connections_open == 0) {
74 g_debug("Finished finding connections");
75 /* Check time here, either we've already heard from
76 Lomiri and we should send the data to the app (quit) or
77 we should wait some more */
78 guint64 timespent = g_get_monotonic_time() - data->lomiri_starttime;
79 if (timespent > 500 /* ms */ * 1000 /* ms to us */) {
80 second_exec_complete(data);
81 } else {
82 g_debug("Timer Set");
83 data->timer = thread_default_timeout(500 - (timespent / 1000), timer_cb, data);
84 }
85 }
86 return;
87 }
88
89 /* Called when Lomiri is done unfreezing the application, if we're
90 done determining the PID, we can send signals */
91 static void
92 lomiri_resume_cb (GDBusConnection * connection, const gchar * sender, const gchar * path, const gchar * interface, const gchar * signal, GVariant * params, gpointer user_data)
93 {
94 second_exec_t * data = (second_exec_t *)user_data;
95 g_debug("Lomiri Completed Resume");
96 ual_tracepoint(second_exec_resume_complete, data->appid);
97
98 if (data->timer != NULL) {
99 g_source_destroy(data->timer);
100 g_source_unref(data->timer);
101 data->timer = NULL;
102 }
103
104 if (data->connections_open == 0) {
105 second_exec_complete(data);
106 } else {
107 /* Make it look like we started *forever* ago */
108 data->lomiri_starttime = 0;
109 }
110
111 return;
112 }
113
114 /* Turn the input string into something we can send to apps */
115 static void
116 parse_uris (second_exec_t * data)
117 {
118 if (data->app_data != NULL) {
119 /* Already done */
120 return;
121 }
122
123 GVariant * uris = NULL;
124
125 if (data->input_uris == NULL) {
126 uris = g_variant_new_array(G_VARIANT_TYPE_STRING, NULL, 0);
127 } else {
128 GVariantBuilder builder;
129 g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY);
130
131 int i;
132 for (i = 0; data->input_uris[i] != NULL; i++) {
133 g_variant_builder_add_value(&builder, g_variant_new_string(data->input_uris[i]));
134 }
135
136 uris = g_variant_builder_end(&builder);
137 }
138
139 GVariant * platform = g_variant_new_array(G_VARIANT_TYPE("{sv}"), NULL, 0);
140
141 GVariantBuilder tuple;
142 g_variant_builder_init(&tuple, G_VARIANT_TYPE_TUPLE);
143 g_variant_builder_add_value(&tuple, uris);
144 g_variant_builder_add_value(&tuple, platform);
145
146 data->app_data = g_variant_builder_end(&tuple);
147 g_variant_ref_sink(data->app_data);
148
149 return;
150 }
151
152 /* Finds us our dbus path to use. Basically this is the name
153 of the application with dots replaced by / and a / tacted on
154 the front. This is recommended here:
155
156 http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#dbus
157 */
158 static void
159 app_id_to_dbus_path (second_exec_t * data)
160 {
161 if (data->dbus_path != NULL) {
162 return;
163 }
164
165 GString * str = g_string_sized_new(strlen(data->appid) + 2); /* base case, we just need a / and a null */
166 g_string_append_c(str, '/');
167
168 int i;
169 for (i = 0; data->appid[i] != '\0'; i++) {
170 if ((data->appid[i] >= 'a' && data->appid[i] <= 'z') ||
171 (data->appid[i] >= 'A' && data->appid[i] <= 'Z') ||
172 (data->appid[i] >= '0' && data->appid[i] <= '9' && i != 0)) {
173 g_string_append_c(str, data->appid[i]);
174 continue;
175 }
176
177 g_string_append_printf(str, "_%2x", data->appid[i]);
178 }
179
180 data->dbus_path = g_string_free(str, FALSE);
181 g_debug("DBus Path: %s", data->dbus_path);
182
183 return;
184 }
185
186 /* Finish the send and decrement the counter */
187 static void
188 send_open_cb (GObject * object, GAsyncResult * res, gpointer user_data)
189 {
190 GError * error = NULL;
191
192 ual_tracepoint(second_exec_app_contacted, ((second_exec_t *)user_data)->appid);
193
194 g_dbus_connection_call_finish(G_DBUS_CONNECTION(object), res, &error);
195
196 if (error != NULL) {
197 ual_tracepoint(second_exec_app_error, ((second_exec_t *)user_data)->appid);
198 /* Mostly just to free the error, but printing for debugging */
199 g_debug("Unable to send Open: %s", error->message);
200 g_error_free(error);
201 }
202
203 connection_count_dec(user_data);
204 return;
205 }
206
207 /* Sends the Open message to the connection with the URIs we were given */
208 static void
209 contact_app (GDBusConnection * bus, const gchar * dbus_name, second_exec_t * data)
210 {
211 ual_tracepoint(second_exec_contact_app, data->appid, dbus_name);
212
213 parse_uris(data);
214 app_id_to_dbus_path(data);
215
216 /* Using the FD.o Application interface */
217 g_dbus_connection_call(bus,
218 dbus_name,
219 data->dbus_path,
220 "org.freedesktop.Application",
221 "Open",
222 data->app_data,
223 NULL,
224 G_DBUS_CALL_FLAGS_NONE,
225 -1,
226 NULL,
227 send_open_cb, data);
228
229 g_debug("Sending Open request to: %s", dbus_name);
230
231 return;
232 }
233
234 typedef struct {
235 gchar * name;
236 second_exec_t * data;
237 } get_pid_t;
238
239 /* Gets the PID for a connection, and if it matches the one we're looking
240 for then it tries to send a message to that connection */
241 static void
242 get_pid_cb (GObject * object, GAsyncResult * res, gpointer user_data)
243 {
244 get_pid_t * data = (get_pid_t *)user_data;
245 GError * error = NULL;
246 GVariant * vpid = NULL;
247
248 ual_tracepoint(second_exec_got_pid, data->data->appid, data->name);
249
250 vpid = g_dbus_connection_call_finish(G_DBUS_CONNECTION(object), res, &error);
251
252 if (error != NULL) {
253 g_warning("Unable to query PID for dbus name '%s': %s", data->name, error->message);
254 g_error_free(error);
255
256 /* Lowering the connection count, this one is terminal, even if in error */
257 connection_count_dec(data->data);
258
259 g_free(data->name);
260 g_free(data);
261
262 return;
263 }
264
265 guint pid = 0;
266 g_variant_get(vpid, "(u)", &pid);
267 g_variant_unref(vpid);
268
269 if (pid == data->data->app_pid) {
270 /* Trying to send a message to the connection */
271 contact_app(G_DBUS_CONNECTION(object), data->name, data->data);
272 } else {
273 /* See if we can quit now */
274 connection_count_dec(data->data);
275 }
276
277 g_free(data->name);
278 g_free(data);
279
280 return;
281 }
282
283 /* Starts to look for the PID and the connections for that PID */
284 static void
285 find_appid_pid (GDBusConnection * session, second_exec_t * data)
286 {
287 GError * error = NULL;
288
289 /* List all the connections on dbus. This sucks that we have to do
290 this, but in the future we should add DBus API to do this lookup
291 instead of having to do it with a bunch of requests */
292 GVariant * listnames = g_dbus_connection_call_sync(session,
293 "org.freedesktop.DBus",
294 "/",
295 "org.freedesktop.DBus",
296 "ListNames",
297 NULL,
298 G_VARIANT_TYPE("(as)"),
299 G_DBUS_CALL_FLAGS_NONE,
300 -1,
301 NULL,
302 &error);
303
304 if (error != NULL) {
305 g_warning("Unable to get list of names from DBus: %s", error->message);
306 g_error_free(error);
307 return;
308 }
309
310 g_debug("Got bus names");
311 ual_tracepoint(second_exec_got_dbus_names, data->appid);
312
313 g_debug("Primary PID: %d", data->app_pid);
314 ual_tracepoint(second_exec_got_primary_pid, data->appid);
315
316 /* Get the names */
317 GVariant * names = g_variant_get_child_value(listnames, 0);
318 GVariantIter iter;
319 g_variant_iter_init(&iter, names);
320 gchar * name = NULL;
321
322 while (g_variant_iter_loop(&iter, "s", &name)) {
323 /* We only want to ask each connection once, this makes that so */
324 if (!g_dbus_is_unique_name(name)) {
325 continue;
326 }
327
328 get_pid_t * pid_data = g_new0(get_pid_t, 1);
329 pid_data->data = data;
330 pid_data->name = g_strdup(name);
331
332 ual_tracepoint(second_exec_request_pid, data->appid, pid_data->name);
333
334 /* Get the PIDs */
335 g_dbus_connection_call(session,
336 "org.freedesktop.DBus",
337 "/",
338 "org.freedesktop.DBus",
339 "GetConnectionUnixProcessID",
340 g_variant_new("(s)", name),
341 G_VARIANT_TYPE("(u)"),
342 G_DBUS_CALL_FLAGS_NONE,
343 -1,
344 NULL,
345 get_pid_cb, pid_data);
346
347 data->connections_open++;
348 }
349
350 g_variant_unref(names);
351 g_variant_unref(listnames);
352
353 return;
354 }
355
356 gboolean
357 second_exec (GDBusConnection * session, GCancellable * cancel, GPid pid, const gchar * app_id, const gchar * instance_id, gchar ** appuris)
358 {
359 ual_tracepoint(second_exec_start, app_id);
360 GError * error = NULL;
361
362 /* Setup our continuation data */
363 second_exec_t * data = g_new0(second_exec_t, 1);
364 data->appid = g_strdup(app_id);
365 data->instanceid = g_strdup(instance_id);
366 data->input_uris = g_strdupv(appuris);
367 data->bus = g_object_ref(session);
368 data->app_pid = pid;
369
370 /* Set up listening for the unfrozen signal from Lomiri */
371 data->signal = g_dbus_connection_signal_subscribe(session,
372 NULL, /* sender */
373 "com.lomiri.LomiriAppLaunch", /* interface */
374 "LomiriResumeResponse", /* signal */
375 "/", /* path */
376 app_id, /* arg0 */
377 G_DBUS_SIGNAL_FLAGS_NONE,
378 lomiri_resume_cb, data,
379 NULL); /* user data destroy */
380
381 g_debug("Sending resume request");
382 ual_tracepoint(second_exec_emit_resume, app_id);
383
384 /* Send unfreeze to to Lomiri */
385 g_dbus_connection_emit_signal(session,
386 NULL, /* destination */
387 "/", /* path */
388 "com.lomiri.LomiriAppLaunch", /* interface */
389 "LomiriResumeRequest", /* signal */
390 g_variant_new("(ss)", app_id, instance_id),
391 &error);
392
393 /* Now we start a race, we try to get to the point of knowing who
394 to send things to, and Lomiri is unfrezing it. When both are
395 done we can send something to the app */
396 data->lomiri_starttime = g_get_monotonic_time();
397
398 if (error != NULL) {
399 /* On error let's not wait for Lomiri */
400 g_warning("Unable to signal Lomiri: %s", error->message);
401 g_error_free(error);
402 error = NULL;
403 data->lomiri_starttime = 0;
404 }
405
406 /* If we've got something to give out, start looking for how */
407 if (data->input_uris != NULL) {
408 find_appid_pid(session, data);
409 } else {
410 g_debug("No URIs to send");
411 }
412
413 /* Loop and wait for everything to align */
414 if (data->connections_open == 0) {
415 if (data->lomiri_starttime == 0) {
416 second_exec_complete(data);
417 } else {
418 data->timer = thread_default_timeout(500, timer_cb, data);
419 }
420 }
421
422 return TRUE;
423 }
424
425 static void
426 second_exec_complete (second_exec_t * data)
427 {
428 GError * error = NULL;
429 ual_tracepoint(second_exec_emit_focus, data->appid);
430
431 /* Now that we're done sending the info to the app, we can ask
432 Lomiri to focus the application. */
433 g_dbus_connection_emit_signal(data->bus,
434 NULL, /* destination */
435 "/", /* path */
436 "com.lomiri.LomiriAppLaunch", /* interface */
437 "LomiriFocusRequest", /* signal */
438 g_variant_new("(ss)", data->appid, data->instanceid),
439 &error);
440
441 if (error != NULL) {
442 g_warning("Unable to request focus to Lomiri: %s", error->message);
443 g_error_free(error);
444 error = NULL;
445 }
446
447 /* Make sure the signal hits the bus */
448 g_dbus_connection_flush_sync(data->bus, NULL, &error);
449 if (error != NULL) {
450 g_warning("Unable to flush session bus: %s", error->message);
451 g_error_free(error);
452 error = NULL;
453 }
454
455 ual_tracepoint(second_exec_finish, data->appid);
456 g_debug("Second Exec complete");
457
458 /* Clean up */
459 if (data->signal != 0)
460 g_dbus_connection_signal_unsubscribe(data->bus, data->signal);
461
462 if (data->timer != NULL) {
463 g_source_destroy(data->timer);
464 g_source_unref(data->timer);
465 }
466 g_object_unref(data->bus);
467 if (data->app_data != NULL)
468 g_variant_unref(data->app_data);
469 g_free(data->appid);
470 g_free(data->instanceid);
471 g_strfreev(data->input_uris);
472 g_free(data->dbus_path);
473 g_free(data);
474
475 return;
476 }