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