dd5e09ac8868cac6a8bcb460a585fe8a81b06556
[platform/framework/web/crosswalk.git] / src / xwalk / application / tools / linux / xwalk_launcher_main.cc
1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <sys/types.h>
6 #include <stdlib.h>
7 #include <stdbool.h>
8 #include <string.h>
9 #include <stdio.h>
10 #include <unistd.h>
11 #include <pwd.h>
12 #include <libgen.h>
13
14 #include <glib.h>
15 #include <gio/gio.h>
16
17 #include "xwalk/application/tools/linux/dbus_connection.h"
18 #include "xwalk/application/tools/linux/xwalk_tizen_user.h"
19 #include "xwalk/application/tools/linux/xwalk_launcher_tizen.h"
20
21 static const char* xwalk_service_name = "org.crosswalkproject.Runtime1";
22 static const char* xwalk_running_path = "/running1";
23 static const char* xwalk_running_manager_iface =
24     "org.crosswalkproject.Running.Manager1";
25 static const char* xwalk_running_app_iface =
26     "org.crosswalkproject.Running.Application1";
27
28 static const char cmd_line_fullscreen_arg[] = "--fullscreen";
29
30 static char* application_object_path;
31
32 static GMainLoop* mainloop;
33
34 static void object_removed(GDBusObjectManager* manager, GDBusObject* object,
35                            gpointer user_data) {
36   const char* path = g_dbus_object_get_object_path(object);
37
38   if (g_strcmp0(path, application_object_path))
39     return;
40
41   fprintf(stderr, "Application '%s' disappeared, exiting.\n", path);
42
43   g_main_loop_quit(mainloop);
44 }
45
46 static void on_app_properties_changed(GDBusProxy* proxy,
47                                       GVariant* changed_properties,
48                                       GStrv invalidated_properties,
49                                       gpointer user_data) {
50   const char* interface = g_dbus_proxy_get_interface_name(proxy);
51
52   fprintf(stderr, "properties changed %s\n", interface);
53
54   if (g_variant_n_children(changed_properties) == 0)
55     return;
56
57   if (g_strcmp0(interface, xwalk_running_app_iface))
58     return;
59
60   GVariantIter* iter;
61   const gchar* key;
62   GVariant* value;
63
64   g_variant_get(changed_properties, "a{sv}", &iter);
65
66   while (g_variant_iter_loop(iter, "{&sv}", &key, &value)) {
67     if (g_strcmp0(key, "State"))
68       continue;
69
70     const gchar* state = g_variant_get_string(value, NULL);
71
72     fprintf(stderr, "Application state %s\n", state);
73   }
74 }
75
76 int main(int argc, char** argv) {
77   GError* error = NULL;
78   char* appid;
79   gboolean fullscreen = FALSE;
80
81
82 #if !GLIB_CHECK_VERSION(2, 36, 0)
83   // g_type_init() is deprecated on GLib since 2.36, Tizen has 2.32.
84   g_type_init();
85 #endif
86
87   if (xwalk_tizen_set_home_for_user_app())
88     exit(1);
89
90   if (!strcmp(basename(argv[0]), "xwalk-launcher")) {
91     if (argc < 2) {
92       fprintf(stderr, "No AppID informed, nothing to do\n");
93       exit(1);
94     }
95
96     appid = argv[1];
97
98     if (argc > 2) {
99       if (!strcmp(basename(argv[2]), cmd_line_fullscreen_arg))
100         fullscreen = TRUE;
101     }
102
103   } else {
104     appid = strdup(basename(argv[0]));
105
106     if (argc > 1) {
107       if (!strcmp(basename(argv[1]), cmd_line_fullscreen_arg))
108         fullscreen = TRUE;
109     }
110   }
111
112   GDBusConnection* connection = get_session_bus_connection(&error);
113   if (!connection) {
114     fprintf(stderr, "Couldn't get the session bus connection: %s\n",
115             error->message);
116     exit(1);
117   }
118
119   GDBusObjectManager* running_apps_om = g_dbus_object_manager_client_new_sync(
120       connection, G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
121       xwalk_service_name, xwalk_running_path,
122       NULL, NULL, NULL, NULL, &error);
123   if (!running_apps_om) {
124     fprintf(stderr, "Service '%s' does could not be reached: %s\n",
125             xwalk_service_name, error->message);
126     exit(1);
127   }
128
129   g_signal_connect(running_apps_om, "object-removed",
130                    G_CALLBACK(object_removed), NULL);
131
132   GDBusProxy* running_proxy = g_dbus_proxy_new_sync(
133       connection,
134       G_DBUS_PROXY_FLAGS_NONE, NULL, xwalk_service_name,
135       xwalk_running_path, xwalk_running_manager_iface, NULL, &error);
136   if (!running_proxy) {
137     g_print("Couldn't create proxy for '%s': %s\n", xwalk_running_manager_iface,
138             error->message);
139     g_error_free(error);
140     exit(1);
141   }
142
143   unsigned int launcher_pid = getpid();
144
145   GVariant* result = g_dbus_proxy_call_sync(running_proxy, "Launch",
146                                             g_variant_new("(sub)", appid,
147                                                           launcher_pid,
148                                                           fullscreen),
149                                             G_DBUS_CALL_FLAGS_NONE,
150                                             -1, NULL, &error);
151   if (!result) {
152     fprintf(stderr, "Couldn't call 'Launch' method: %s\n", error->message);
153     exit(1);
154   }
155
156   g_variant_get(result, "(o)", &application_object_path);
157   fprintf(stderr, "Application launched with path '%s'\n",
158           application_object_path);
159
160   GDBusProxy* app_proxy = g_dbus_proxy_new_sync(
161       connection,
162       G_DBUS_PROXY_FLAGS_NONE, NULL, xwalk_service_name,
163       application_object_path, xwalk_running_app_iface, NULL, &error);
164   if (!app_proxy) {
165     g_print("Couldn't create proxy for '%s': %s\n", xwalk_running_app_iface,
166             error->message);
167     g_error_free(error);
168     exit(1);
169   }
170
171   g_signal_connect(app_proxy, "g-properties-changed",
172                    G_CALLBACK(on_app_properties_changed), NULL);
173
174   mainloop = g_main_loop_new(NULL, FALSE);
175
176 #if defined(OS_TIZEN)
177   char name[128];
178   snprintf(name, sizeof(name), "xwalk-%s", appid);
179
180   if (xwalk_appcore_init(argc, argv, name)) {
181     fprintf(stderr, "Failed to initialize appcore");
182     exit(1);
183   }
184 #endif
185
186   g_main_loop_run(mainloop);
187
188   return 0;
189 }