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.
16 #include <gio/gunixfdlist.h>
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"
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";
30 static char* application_object_path;
32 static GMainLoop* mainloop;
33 static GDBusConnection* g_connection;
34 static XWalkExtensionProcessLauncher* ep_launcher = NULL;
38 static gboolean query_running = FALSE;
39 static gboolean fullscreen = FALSE;
40 static gchar** cmd_appid;
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 },
52 static void object_removed(GDBusObjectManager* manager, GDBusObject* object,
54 const char* path = g_dbus_object_get_object_path(object);
56 if (g_strcmp0(path, application_object_path))
59 fprintf(stderr, "Application '%s' disappeared, exiting.\n", path);
62 g_main_loop_quit(mainloop);
65 static void on_app_properties_changed(GDBusProxy* proxy,
66 GVariant* changed_properties,
67 GStrv invalidated_properties,
69 const char* interface = g_dbus_proxy_get_interface_name(proxy);
71 fprintf(stderr, "properties changed %s\n", interface);
73 if (g_variant_n_children(changed_properties) == 0)
76 if (g_strcmp0(interface, xwalk_running_app_iface))
83 g_variant_get(changed_properties, "a{sv}", &iter);
85 while (g_variant_iter_loop(iter, "{&sv}", &key, &value)) {
86 if (g_strcmp0(key, "State"))
89 const gchar* state = g_variant_get_string(value, NULL);
91 fprintf(stderr, "Application state %s\n", state);
95 static gboolean init_extension_process_channel(GDBusProxy* app_proxy) {
96 if (ep_launcher->is_started())
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)
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))
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);
117 ep_launcher->Launch(channel_id, client_fd);
121 static void on_app_signal(GDBusProxy* proxy,
124 GVariant* parameters,
125 gpointer user_data) {
126 if (!strcmp(signal_name, "EPChannelCreated")) {
127 init_extension_process_channel(proxy);
129 fprintf(stderr, "Unkown signal received: %s\n", signal_name);
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);
137 bool is_running = FALSE;
139 for (it = objects; it; it = it->next) {
140 GDBusObject* object = reinterpret_cast<GDBusObject*>(it->data);
141 GDBusInterface* iface = g_dbus_object_get_interface(
143 xwalk_running_app_iface);
147 GDBusProxy* proxy = G_DBUS_PROXY(iface);
148 GVariant* id_variant;
149 id_variant = g_dbus_proxy_get_cached_property(proxy, "AppID");
151 g_object_unref(iface);
156 g_variant_get(id_variant, "s", &id);
157 if (!strcmp(app_id, id)) {
162 g_object_unref(iface);
164 const char* str = is_running ? "running" : "not running";
165 g_print("Application %s is %s.\n", app_id, str);
167 g_list_free_full(objects, g_object_unref);
170 static void launch_application(GDBusObjectManager* running_apps_manager,
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);
178 GDBusProxy* running_proxy = g_dbus_proxy_new_sync(
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,
189 unsigned int launcher_pid = getpid();
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);
195 fprintf(stderr, "Couldn't call 'Launch' method: %s\n", error->message);
199 g_variant_get(result, "(o)", &application_object_path);
200 fprintf(stderr, "Application launched with path '%s'\n",
201 application_object_path);
203 GDBusProxy* app_proxy = g_dbus_proxy_new_sync(
205 G_DBUS_PROXY_FLAGS_NONE, NULL, xwalk_service_name,
206 application_object_path, xwalk_running_app_iface, NULL, &error);
208 g_print("Couldn't create proxy for '%s': %s\n", xwalk_running_app_iface,
214 g_signal_connect(app_proxy, "g-properties-changed",
215 G_CALLBACK(on_app_properties_changed), NULL);
217 mainloop = g_main_loop_new(NULL, FALSE);
218 g_signal_connect(app_proxy, "g-signal", G_CALLBACK(on_app_signal), NULL);
220 #if defined(OS_TIZEN)
222 snprintf(name, sizeof(name), "xwalk-%s", appid);
224 if (xwalk_appcore_init(g_argc, g_argv, name)) {
225 fprintf(stderr, "Failed to initialize appcore");
230 init_extension_process_channel(app_proxy);
231 g_main_loop_run(mainloop);
234 int main(int argc, char** argv) {
235 GError* error = NULL;
241 #if !GLIB_CHECK_VERSION(2, 36, 0)
242 // g_type_init() is deprecated on GLib since 2.36.
246 if (xwalk_tizen_check_user_app())
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);
257 if (!strcmp(basename(argv[0]), "xwalk-launcher")) {
258 if (cmd_appid == NULL) {
259 fprintf(stderr, "No AppID informed, nothing to do.\n");
262 appid = strdup(cmd_appid[0]);
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".
269 tokens = g_strsplit(appid, ".", 2);
270 if (g_strv_length(tokens) != 2) {
271 fprintf(stderr, "Invalid Tizen AppID, fallback to Crosswalk AppID.\n");
274 appid = strdup(tokens[1]);
280 g_connection = get_session_bus_connection(&error);
282 fprintf(stderr, "Couldn't get the session bus connection: %s\n",
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);
299 query_application_running(running_apps_manager, appid);
301 launch_application(running_apps_manager, appid, fullscreen);