Upstream version 11.39.264.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / tools / linux / xwalk_launcher_main.cc
index c4798a4..8ea14b7 100644 (file)
 // Copyright (c) 2013 Intel Corporation. All rights reserved.
+// Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#include <sys/types.h>
-#include <stdlib.h>
-#include <stdbool.h>
-#include <string.h>
-#include <stdio.h>
-#include <pwd.h>
-#include <libgen.h>
-
 #include <glib.h>
-#include <gio/gio.h>
-
-#include "xwalk/application/tools/linux/dbus_connection.h"
-#include "xwalk/application/tools/linux/xwalk_tizen_user.h"
-#include "xwalk/application/tools/linux/xwalk_launcher_tizen.h"
-
-static const char* xwalk_service_name = "org.crosswalkproject.Runtime1";
-static const char* xwalk_running_path = "/running1";
-static const char* xwalk_running_manager_iface =
-    "org.crosswalkproject.Running.Manager1";
-static const char* xwalk_running_app_iface =
-    "org.crosswalkproject.Running.Application1";
-
-static char* application_object_path;
-
-static GMainLoop* mainloop;
-
-static void object_removed(GDBusObjectManager* manager, GDBusObject* object,
-                           gpointer user_data) {
-  const char* path = g_dbus_object_get_object_path(object);
-
-  if (g_strcmp0(path, application_object_path))
-    return;
-
-  fprintf(stderr, "Application '%s' disappeared, exiting.\n", path);
-
-  g_main_loop_quit(mainloop);
-}
-
-static void on_app_properties_changed(GDBusProxy* proxy,
-                                      GVariant* changed_properties,
-                                      GStrv invalidated_properties,
-                                      gpointer user_data) {
-  const char* interface = g_dbus_proxy_get_interface_name(proxy);
-
-  fprintf(stderr, "properties changed %s\n", interface);
 
-  if (g_variant_n_children(changed_properties) == 0)
-    return;
+#include <memory>
 
-  if (g_strcmp0(interface, xwalk_running_app_iface))
-    return;
+#include "base/message_loop/message_loop.h"
+#include "base/message_loop/message_pump.h"
+#include "base/message_loop/message_pump_glib.h"
+#include "base/run_loop.h"
 
-  GVariantIter* iter;
-  const gchar* key;
-  GVariant* value;
-
-  g_variant_get(changed_properties, "a{sv}", &iter);
-
-  while (g_variant_iter_loop(iter, "{&sv}", &key, &value)) {
-    if (g_strcmp0(key, "State"))
-      continue;
-
-    const gchar* state = g_variant_get_string(value, NULL);
+#include "xwalk/application/tools/linux/xwalk_launcher.h"
+#if defined(OS_TIZEN)
+#include "xwalk/application/tools/linux/xwalk_launcher_tizen.h"
+#endif
 
-    fprintf(stderr, "Application state %s\n", state);
-  }
-}
+namespace {
+
+int g_argc;
+char** g_argv;
+gboolean query_running = FALSE;
+gboolean fullscreen = FALSE;
+gboolean remote_debugging = FALSE;
+gchar** cmd_appid_or_url;
+char* application_object_path;
+
+}  // namespace
+
+static const GOptionEntry entries[] {
+  { "running", 'r', 0, G_OPTION_ARG_NONE, &query_running,
+    "Check whether the application is running", nullptr },
+  { "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &fullscreen,
+    "Run the application as fullscreen", nullptr },
+  { "debugging_port", 'd', 0, G_OPTION_ARG_NONE, &remote_debugging,
+    "Enable remote debugging for the application", nullptr },
+  { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY,
+    &cmd_appid_or_url,
+    "ID of the application to be launched or URL to open", nullptr },
+  { nullptr }
+};
 
 int main(int argc, char** argv) {
-  GError* error = NULL;
-  const char* appid;
-
 #if !GLIB_CHECK_VERSION(2, 36, 0)
-  // g_type_init() is deprecated on GLib since 2.36, Tizen has 2.32.
+  // g_type_init() is deprecated on GLib since 2.36.
   g_type_init();
 #endif
 
-  if (xwalk_tizen_set_home_for_user_app())
+#if defined(OS_TIZEN)
+  if (xwalk_tizen_check_group_users())
+    return 1;
+#endif
+  base::MessageLoop msg_loop(
+      make_scoped_ptr<base::MessagePump>(new base::MessagePumpGlib()));
+
+  g_argc = argc;
+  g_argv = argv;
+
+  GError* error = nullptr;
+  GOptionContext* context =
+      g_option_context_new("- Crosswalk Application Launcher");
+  g_option_context_add_main_entries(context, entries, nullptr);
+  if (!g_option_context_parse(context, &argc, &argv, &error)) {
+    LOG(ERROR) << "Option parsing failed: " << error->message;
     exit(1);
+  }
 
-  if (argc >= 2) {
-    appid = argv[1];
-  } else {
-    if (!strcmp(basename(argv[0]), "xwalk-launcher")) {
-      fprintf(stderr, "No AppID informed, nothing to do\n");
+  std::string appid_or_url;
+  if (!strcmp(basename(g_argv[0]), "xwalk-launcher")) {
+    if (!cmd_appid_or_url) {
+      LOG(ERROR) << "No AppID informed, nothing to do.";
       exit(1);
     }
-
-    // We assume that we are running from a link to the xwalk-launcher binary.
-    appid = basename(argv[0]);
-  }
-
-  GDBusConnection* connection = get_session_bus_connection(&error);
-  if (!connection) {
-    fprintf(stderr, "Couldn't get the session bus connection: %s\n",
-            error->message);
-    exit(1);
-  }
-
-  GDBusObjectManager* running_apps_om = g_dbus_object_manager_client_new_sync(
-      connection, G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
-      xwalk_service_name, xwalk_running_path,
-      NULL, NULL, NULL, NULL, &error);
-  if (!running_apps_om) {
-    fprintf(stderr, "Service '%s' does could not be reached: %s\n",
-            xwalk_service_name, error->message);
-    exit(1);
-  }
-
-  g_signal_connect(running_apps_om, "object-removed",
-                   G_CALLBACK(object_removed), NULL);
-
-  GDBusProxy* running_proxy = g_dbus_proxy_new_sync(
-      connection,
-      G_DBUS_PROXY_FLAGS_NONE, NULL, xwalk_service_name,
-      xwalk_running_path, xwalk_running_manager_iface, NULL, &error);
-  if (!running_proxy) {
-    g_print("Couldn't create proxy for '%s': %s\n", xwalk_running_manager_iface,
-            error->message);
-    g_error_free(error);
-    exit(1);
-  }
-
-  GVariant* result = g_dbus_proxy_call_sync(running_proxy, "Launch",
-                                            g_variant_new("(s)", appid),
-                                            G_DBUS_CALL_FLAGS_NONE,
-                                            -1, NULL, &error);
-  if (!result) {
-    fprintf(stderr, "Couldn't call 'Launch' method: %s\n", error->message);
-    exit(1);
-  }
-
-  g_variant_get(result, "(o)", &application_object_path);
-  fprintf(stderr, "Application launched with path '%s'\n",
-          application_object_path);
-
-  GDBusProxy* app_proxy = g_dbus_proxy_new_sync(
-      connection,
-      G_DBUS_PROXY_FLAGS_NONE, NULL, xwalk_service_name,
-      application_object_path, xwalk_running_app_iface, NULL, &error);
-  if (!app_proxy) {
-    g_print("Couldn't create proxy for '%s': %s\n", xwalk_running_app_iface,
-            error->message);
-    g_error_free(error);
-    exit(1);
+    appid_or_url = std::string(cmd_appid_or_url[0]);
+  } else {
+    appid_or_url = std::string(basename(g_argv[0]));
   }
 
-  g_signal_connect(app_proxy, "g-properties-changed",
-                   G_CALLBACK(on_app_properties_changed), NULL);
-
-  mainloop = g_main_loop_new(NULL, FALSE);
-
-#if defined(OS_TIZEN_MOBILE)
-  char name[128];
-  snprintf(name, sizeof(name), "xwalk-%s", appid);
-
-  if (xwalk_appcore_init(argc, argv, name)) {
-    fprintf(stderr, "Failed to initialize appcore");
-    exit(1);
-  }
+  std::unique_ptr<XWalkLauncher> launcher;
+#if defined(OS_TIZEN)
+  launcher.reset(new XWalkLauncherTizen(query_running, &msg_loop));
+#else
+  launcher.reset(new XWalkLauncher(query_running, &msg_loop));
 #endif
-
-  g_main_loop_run(mainloop);
-
-  return 0;
+  int result = launcher->Launch(appid_or_url, fullscreen, remote_debugging,
+                                argc, argv);
+  if (!result)
+    msg_loop.Run();
+  return result;
 }