Upstream version 11.39.264.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / tools / linux / xwalk_launcher_tizen.cc
index a26b053..8aa67b7 100644 (file)
@@ -1,40 +1,31 @@
 // 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 <glib.h>
-#include <gio/gio.h>
+#include "xwalk/application/tools/linux/xwalk_launcher_tizen.h"
+
+#include <unistd.h>
+#include <pkgmgr-info.h>
 #include <stdio.h>
-#include <string.h>
 #include <stdlib.h>
-#include <appcore/appcore-common.h>
-#include <pkgmgr-info.h>
+#include <string.h>
 
-#include "xwalk/application/common/id_util.h"
-#include "xwalk/application/tools/linux/xwalk_launcher_tizen.h"
+#include <string>
 
-enum app_event {
-  AE_UNKNOWN,
-  AE_CREATE,
-  AE_TERMINATE,
-  AE_PAUSE,
-  AE_RESUME,
-  AE_RESET,
-  AE_LOWMEM_POST,
-  AE_MEM_FLUSH,
-  AE_MAX
-};
+#include "base/logging.h"
+#include "url/gurl.h"
 
 // Private struct from appcore-internal, necessary to get events from
 // the system.
 struct ui_ops {
   void* data;
-  void (*cb_app)(enum app_event evnt, void* data, bundle* b);
+  void (*cb_app)(app_event evnt, void* data, bundle* b);
 };
 
-static struct ui_ops appcore_ops;
+namespace {
 
-static const char* event2str(enum app_event event) {
+const char* Event2Str(app_event event) {
   switch (event) {
     case AE_UNKNOWN:
       return "AE_UNKNOWN";
@@ -59,33 +50,68 @@ static const char* event2str(enum app_event event) {
   return "INVALID EVENT";
 }
 
-static void application_event_cb(enum app_event event, void* data, bundle* b) {
-  fprintf(stderr, "event '%s'\n", event2str(event));
-  GDBusProxy* app_proxy = reinterpret_cast<GDBusProxy*>(data);
+ui_ops app_ops;
+
+}  // namespace
 
-  if (!app_proxy) {
-    fprintf(stderr, "Invalid DBus proxy.");
-    return;
+XWalkLauncherTizen::XWalkLauncherTizen(bool query_running,
+    base::MessageLoop* main_loop)
+    : XWalkLauncher(query_running, main_loop) {
+}
+
+int XWalkLauncherTizen::Launch(const std::string& appid_or_url, bool fullscreen,
+                               bool remote_debugging, int argc, char* argv[]) {
+  appid_or_url_ = appid_or_url;
+  fullscreen_ = fullscreen;
+  remote_debugging_ = remote_debugging;
+  // Query app.
+  if (query_running_) {
+    return dbus_object_manager_->IsApplicationRunning(appid_or_url_);
   }
+  std::string name = "xwalk-" + appid_or_url_;
+
+  if (XwalkAppcoreInit(name, argc, argv)) {
+    LOG(ERROR) << "Failed to initialize appcore.";
+    return 1;
+  }
+  if (GURL(appid_or_url_).spec().empty()
+      && XwalkChangeCmdline(appid_or_url_, argc, argv))
+    return 1;
+  return 0;
+}
+
+bool XWalkLauncherTizen::Suspend() {
+  return dbus_object_manager_->Suspend();
+}
+
+bool XWalkLauncherTizen::Resume() {
+  return dbus_object_manager_->Resume();
+}
+
+void XWalkLauncherTizen::application_event_cb(app_event event,
+                                              void* data, bundle* b) {
+  XWalkLauncherTizen* xwalk_launcher = static_cast<XWalkLauncherTizen*>(data);
+  LOG(INFO) << "event '" << Event2Str(event) << "'";
 
   switch (event) {
     case AE_UNKNOWN:
     case AE_CREATE:
       break;
     case AE_TERMINATE:
-      exit(0);
+      xwalk_launcher->main_loop_->QuitNow();
       break;
     case AE_PAUSE:
-      g_dbus_proxy_call(
-          app_proxy, "Suspend", NULL,
-          G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
+      if (!xwalk_launcher->Suspend())
+        LOG(ERROR) << "Suspending application failed";
       break;
     case AE_RESUME:
-      g_dbus_proxy_call(
-          app_proxy, "Resume", NULL,
-          G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
+      if (!xwalk_launcher->Resume())
+        LOG(ERROR) << "Resuming application failed";
       break;
     case AE_RESET:
+      if (!xwalk_launcher->LaunchApplication())
+        xwalk_launcher->main_loop_->QuitNow();
+      break;
     case AE_LOWMEM_POST:
     case AE_MEM_FLUSH:
     case AE_MAX:
@@ -93,34 +119,37 @@ static void application_event_cb(enum app_event event, void* data, bundle* b) {
   }
 }
 
-int xwalk_appcore_init(
-    int argc, char** argv, const char* name, GDBusProxy* app_proxy) {
-  appcore_ops.cb_app = application_event_cb;
-  appcore_ops.data = app_proxy;
-
-  return appcore_init(name, &appcore_ops, argc, argv);
+int XWalkLauncherTizen::XwalkAppcoreInit(const std::string& name,
+                                         int argc, char* argv[]) {
+  app_ops.cb_app = application_event_cb;
+  app_ops.data = this;
+  return appcore_init(name.c_str(), &app_ops, argc, argv);
 }
 
-int xwalk_change_cmdline(int argc, char** argv, const char* app_id) {
+int XWalkLauncherTizen::XwalkChangeCmdline(const std::string& app_id,
+                                           int argc, char* argv[]) {
   // Change /proc/<pid>/cmdline to app exec path. See XWALK-1722 for details.
   pkgmgrinfo_appinfo_h handle;
-  char* exec_path = NULL;
+  char* exec_path = nullptr;
   // todo : add is_admin
-  if (pkgmgrinfo_appinfo_get_usr_appinfo(app_id,
+  if (pkgmgrinfo_appinfo_get_usr_appinfo(app_id.c_str(),
       getuid(), &handle) != PMINFO_R_OK ||
       pkgmgrinfo_appinfo_get_exec(handle, &exec_path) != PMINFO_R_OK ||
       !exec_path) {
-    if (pkgmgrinfo_appinfo_get_appinfo(app_id, &handle) != PMINFO_R_OK ||
+    if (pkgmgrinfo_appinfo_get_appinfo(app_id.c_str(), &handle) !=
+        PMINFO_R_OK ||
         pkgmgrinfo_appinfo_get_exec(handle, &exec_path) != PMINFO_R_OK ||
         !exec_path) {
-      fprintf(stderr, "Couldn't find exec path for application: %s\n", app_id);
+      LOG(ERROR) << "Couldn't find exec path for application: " << app_id;
       return -1;
     }
   }
 
+  // zeros g_argv_
   for (int i = 0; i < argc; ++i)
     memset(argv[i], 0, strlen(argv[i]));
-  strncpy(argv[0], exec_path, strlen(exec_path)+1);
+
+  strncpy(argv[0], exec_path, strlen(exec_path) + 1);
   pkgmgrinfo_appinfo_destroy_appinfo(handle);
   return 0;
 }