Upstream version 7.35.139.0
[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 #include <gio/gunixfdlist.h>
17
18 #include "xwalk/application/tools/linux/dbus_connection.h"
19 #include "xwalk/application/tools/linux/xwalk_extension_process_launcher.h"
20 #include "xwalk/application/tools/linux/xwalk_launcher_tizen.h"
21 #include "xwalk/application/tools/linux/xwalk_tizen_user.h"
22
23 static const char* xwalk_service_name = "org.crosswalkproject.Runtime1";
24 static const char* xwalk_running_path = "/running1";
25 static const char* xwalk_running_manager_iface =
26     "org.crosswalkproject.Running.Manager1";
27 static const char* xwalk_running_app_iface =
28     "org.crosswalkproject.Running.Application1";
29
30 static char* application_object_path;
31
32 static GMainLoop* mainloop;
33 static GDBusConnection* g_connection;
34 static XWalkExtensionProcessLauncher* ep_launcher = NULL;
35
36 static int g_argc;
37 static char** g_argv;
38 static gboolean query_running = FALSE;
39 static gboolean fullscreen = FALSE;
40 static gchar** cmd_appid;
41
42 static GOptionEntry entries[] = {
43   { "running", 'r', 0, G_OPTION_ARG_NONE, &query_running,
44     "Check whether the application is running", NULL },
45   { "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &fullscreen,
46     "Run the application as fullscreen", NULL },
47   { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &cmd_appid,
48     "ID of the application to be launched", NULL },
49   { NULL }
50 };
51
52 static void object_removed(GDBusObjectManager* manager, GDBusObject* object,
53                            gpointer user_data) {
54   const char* path = g_dbus_object_get_object_path(object);
55
56   if (g_strcmp0(path, application_object_path))
57     return;
58
59   fprintf(stderr, "Application '%s' disappeared, exiting.\n", path);
60
61   delete ep_launcher;
62   g_main_loop_quit(mainloop);
63 }
64
65 static void on_app_properties_changed(GDBusProxy* proxy,
66                                       GVariant* changed_properties,
67                                       GStrv invalidated_properties,
68                                       gpointer user_data) {
69   const char* interface = g_dbus_proxy_get_interface_name(proxy);
70
71   fprintf(stderr, "properties changed %s\n", interface);
72
73   if (g_variant_n_children(changed_properties) == 0)
74     return;
75
76   if (g_strcmp0(interface, xwalk_running_app_iface))
77     return;
78
79   GVariantIter* iter;
80   const gchar* key;
81   GVariant* value;
82
83   g_variant_get(changed_properties, "a{sv}", &iter);
84
85   while (g_variant_iter_loop(iter, "{&sv}", &key, &value)) {
86     if (g_strcmp0(key, "State"))
87       continue;
88
89     const gchar* state = g_variant_get_string(value, NULL);
90
91     fprintf(stderr, "Application state %s\n", state);
92   }
93 }
94
95 static gboolean init_extension_process_channel(GDBusProxy* app_proxy) {
96   if (ep_launcher->is_started())
97     return FALSE;
98
99   // Get the client socket file descriptor from fd_list. The reply will
100   // contains an index to the list.
101   GUnixFDList* fd_list;
102   GVariant* res = g_dbus_proxy_call_with_unix_fd_list_sync(
103       app_proxy, "GetEPChannel", NULL, G_DBUS_CALL_FLAGS_NONE,
104       -1, NULL, &fd_list, NULL, NULL);
105   if (!res || g_variant_n_children(res) != 2)
106     return FALSE;
107
108   const gchar* channel_id =
109       g_variant_get_string(g_variant_get_child_value(res, 0), NULL);
110   if (!channel_id || !strlen(channel_id))
111     return FALSE;
112
113   gint32 client_fd_idx =
114       g_variant_get_handle(g_variant_get_child_value(res, 1));
115   int client_fd = g_unix_fd_list_get(fd_list, client_fd_idx, NULL);
116
117   ep_launcher->Launch(channel_id, client_fd);
118   return TRUE;
119 }
120
121 static void on_app_signal(GDBusProxy* proxy,
122                           gchar* sender_name,
123                           gchar* signal_name,
124                           GVariant* parameters,
125                           gpointer user_data) {
126   if (!strcmp(signal_name, "EPChannelCreated")) {
127     init_extension_process_channel(proxy);
128   } else {
129     fprintf(stderr, "Unkown signal received: %s\n", signal_name);
130   }
131 }
132
133 static void query_application_running(GDBusObjectManager* running_om,
134                                       const char* app_id) {
135   GList* objects = g_dbus_object_manager_get_objects(running_om);
136   GList* it;
137   bool is_running = FALSE;
138
139   for (it = objects; it; it = it->next) {
140     GDBusObject* object = reinterpret_cast<GDBusObject*>(it->data);
141     GDBusInterface* iface = g_dbus_object_get_interface(
142         object,
143         xwalk_running_app_iface);
144     if (!iface)
145       continue;
146
147     GDBusProxy* proxy = G_DBUS_PROXY(iface);
148     GVariant* id_variant;
149     id_variant = g_dbus_proxy_get_cached_property(proxy, "AppID");
150     if (!id_variant) {
151       g_object_unref(iface);
152       continue;
153     }
154
155     const gchar* id;
156     g_variant_get(id_variant, "s", &id);
157     if (!strcmp(app_id, id)) {
158       is_running = TRUE;
159       break;
160     }
161
162     g_object_unref(iface);
163   }
164   const char* str = is_running ? "running" : "not running";
165   g_print("Application %s is %s.\n", app_id, str);
166
167   g_list_free_full(objects, g_object_unref);
168 }
169
170 static void launch_application(GDBusObjectManager* running_apps_manager,
171                                const char* appid,
172                                gboolean fullscreen) {
173   ep_launcher = new XWalkExtensionProcessLauncher();
174   GError* error = NULL;
175   g_signal_connect(running_apps_manager, "object-removed",
176                    G_CALLBACK(object_removed), NULL);
177
178   GDBusProxy* running_proxy = g_dbus_proxy_new_sync(
179       g_connection,
180       G_DBUS_PROXY_FLAGS_NONE, NULL, xwalk_service_name,
181       xwalk_running_path, xwalk_running_manager_iface, NULL, &error);
182   if (!running_proxy) {
183     g_print("Couldn't create proxy for '%s': %s\n", xwalk_running_manager_iface,
184             error->message);
185     g_error_free(error);
186     exit(1);
187   }
188
189   unsigned int launcher_pid = getpid();
190
191   GVariant* result  = g_dbus_proxy_call_sync(running_proxy, "Launch",
192       g_variant_new("(sub)", appid, launcher_pid, fullscreen),
193       G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
194   if (!result) {
195     fprintf(stderr, "Couldn't call 'Launch' method: %s\n", error->message);
196     exit(1);
197   }
198
199   g_variant_get(result, "(o)", &application_object_path);
200   fprintf(stderr, "Application launched with path '%s'\n",
201           application_object_path);
202
203   GDBusProxy* app_proxy = g_dbus_proxy_new_sync(
204       g_connection,
205       G_DBUS_PROXY_FLAGS_NONE, NULL, xwalk_service_name,
206       application_object_path, xwalk_running_app_iface, NULL, &error);
207   if (!app_proxy) {
208     g_print("Couldn't create proxy for '%s': %s\n", xwalk_running_app_iface,
209             error->message);
210     g_error_free(error);
211     exit(1);
212   }
213
214   g_signal_connect(app_proxy, "g-properties-changed",
215                    G_CALLBACK(on_app_properties_changed), NULL);
216
217   mainloop = g_main_loop_new(NULL, FALSE);
218   g_signal_connect(app_proxy, "g-signal", G_CALLBACK(on_app_signal), NULL);
219
220 #if defined(OS_TIZEN)
221   char name[128];
222   snprintf(name, sizeof(name), "xwalk-%s", appid);
223
224   if (xwalk_appcore_init(g_argc, g_argv, name)) {
225     fprintf(stderr, "Failed to initialize appcore");
226     exit(1);
227   }
228 #endif
229
230   init_extension_process_channel(app_proxy);
231   g_main_loop_run(mainloop);
232 }
233
234 int main(int argc, char** argv) {
235   GError* error = NULL;
236   char* appid;
237
238   g_argc = argc;
239   g_argv = argv;
240
241 #if !GLIB_CHECK_VERSION(2, 36, 0)
242   // g_type_init() is deprecated on GLib since 2.36.
243   g_type_init();
244 #endif
245
246   if (xwalk_tizen_check_user_app())
247     exit(1);
248
249   GOptionContext* context =
250       g_option_context_new("- Crosswalk Application Launcher");
251   g_option_context_add_main_entries(context, entries, NULL);
252   if (!g_option_context_parse(context, &argc, &argv, &error)) {
253     fprintf(stderr, "Option parsing failed: %s\n", error->message);
254     exit(1);
255   }
256
257   if (!strcmp(basename(argv[0]), "xwalk-launcher")) {
258     if (cmd_appid == NULL) {
259       fprintf(stderr, "No AppID informed, nothing to do.\n");
260       return 0;
261     }
262     appid = strdup(cmd_appid[0]);
263   } else {
264     appid = strdup(basename(argv[0]));
265 #if defined(OS_TIZEN)
266     // The Tizen application ID will have a format like
267     // "xwalk.crosswalk_32bytes_app_id".
268     gchar** tokens;
269     tokens = g_strsplit(appid, ".", 2);
270     if (g_strv_length(tokens) != 2) {
271       fprintf(stderr, "Invalid Tizen AppID, fallback to Crosswalk AppID.\n");
272     } else {
273       free(appid);
274       appid = strdup(tokens[1]);
275     }
276     g_strfreev(tokens);
277 #endif
278   }
279
280   g_connection = get_session_bus_connection(&error);
281   if (!g_connection) {
282     fprintf(stderr, "Couldn't get the session bus connection: %s\n",
283             error->message);
284     exit(1);
285   }
286
287   GDBusObjectManager* running_apps_manager =
288       g_dbus_object_manager_client_new_sync(
289           g_connection, G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
290           xwalk_service_name, xwalk_running_path,
291           NULL, NULL, NULL, NULL, &error);
292   if (!running_apps_manager) {
293     fprintf(stderr, "Service '%s' does could not be reached: %s\n",
294             xwalk_service_name, error->message);
295     exit(1);
296   }
297
298   if (query_running) {
299     query_application_running(running_apps_manager, appid);
300   } else {
301     launch_application(running_apps_manager, appid, fullscreen);
302   }
303
304   free(appid);
305   return 0;
306 }