Assure proper termination using context session
[platform/framework/web/crosswalk-tizen.git] / runtime / browser / runtime_process.cc
index d5b143c..83d7645 100755 (executable)
  *    limitations under the License.
  */
 
-#include <ewk_chromium.h>
+#include <EWebKit.h>
+#include <EWebKit_internal.h>
 
 #include <Elementary.h>
 
+#ifdef WATCH_FACE_FEATURE_SUPPORT
+#include <bundle_internal.h>
+#include <Ecore_Wayland.h>
+#endif  // WATCH_FACE_FEATURE_SUPPORT
+
 #include "common/application_data.h"
 #include "common/command_line.h"
 #include "common/logger.h"
 #include "common/profiler.h"
+#include "extensions/renderer/xwalk_extension_renderer_controller.h"
 #include "runtime/browser/runtime.h"
 #include "runtime/common/constants.h"
 #include "runtime/browser/prelauncher.h"
 #include "runtime/browser/preload_manager.h"
 
+#include "runtime/browser/ui_runtime.h"
+
+using namespace extensions;
 bool g_prelaunch = false;
 
+#ifdef WATCH_FACE_FEATURE_SUPPORT
+static int setWatchEnv(int argc, char **argv) {
+  bundle *kb = NULL;
+  char *wayland_display = NULL;
+  char *xdg_runtime_dir = NULL;
+  char *width_str = NULL;
+  char *height_str = NULL;
+
+  if (argc <= 0 || argv == NULL) {
+    errno = EINVAL;
+    LOGGER(ERROR) << "argument are invalid";
+    return -1;
+  }
+
+  kb = bundle_import_from_argv(argc, argv);
+  if (kb) {
+    bundle_get_str(kb, "XDG_RUNTIME_DIR", &xdg_runtime_dir);
+    bundle_get_str(kb, "WAYLAND_DISPLAY", &wayland_display);
+    bundle_get_str(kb, "WATCH_WIDTH", &width_str);
+    bundle_get_str(kb, "WATCH_HEIGHT", &height_str);
+
+    if (xdg_runtime_dir) {
+      LOGGER(DEBUG) << "senenv: " << xdg_runtime_dir;
+      setenv("XDG_RUNTIME_DIR", xdg_runtime_dir, 1);
+    } else {
+      LOGGER(ERROR) << "failed to get xdgruntimedir";
+    }
+
+    if (wayland_display) {
+      LOGGER(DEBUG) << "setenv: " << wayland_display;
+      setenv("WAYLAND_DISPLAY", wayland_display, 1);
+    } else {
+      LOGGER(ERROR) << "failed to get waylanddisplay";
+    }
+    bundle_free(kb);
+  } else {
+    LOGGER(ERROR) << "failed to get launch argv";
+  }
+  return 0;
+}
+#endif  // WATCH_FACE_FEATURE_SUPPORT
+
 int real_main(int argc, char* argv[]) {
   STEP_PROFILE_START("Start -> Launch Completed");
   STEP_PROFILE_START("Start -> OnCreate");
@@ -45,6 +97,12 @@ int real_main(int argc, char* argv[]) {
     return false;
   }
 
+#ifdef WATCH_FACE_FEATURE_SUPPORT
+  if (appdata->app_type() == common::ApplicationData::WATCH) {
+    setWatchEnv(argc, argv);
+  }
+#endif  // WATCH_FACE_FEATURE_SUPPORT
+
   // Default behavior, run as runtime.
   LOGGER(INFO) << "Runtime process has been created.";
   if (!g_prelaunch) {
@@ -60,6 +118,14 @@ int real_main(int argc, char* argv[]) {
     const int chromium_arg_cnt =
         sizeof(chromium_arg_options) / sizeof(chromium_arg_options[0]);
     ewk_set_arguments(chromium_arg_cnt, chromium_arg_options);
+#ifdef WATCH_FACE_FEATURE_SUPPORT
+  } else {
+    if (appdata->app_type() == common::ApplicationData::WATCH) {
+      // Below code will be enabled after testing
+      //ecore_wl_shutdown();
+      //ecore_wl_init(NULL);
+    }
+#endif  // WATCH_FACE_FEATURE_SUPPORT
   }
 
   int ret = 0;
@@ -68,17 +134,30 @@ int real_main(int argc, char* argv[]) {
     std::unique_ptr<runtime::Runtime> runtime =
         runtime::Runtime::MakeRuntime(appdata);
     ret = runtime->Exec(argc, argv);
+    if (ret)
+      LOGGER(ERROR) << "Exec returns non zero.";
+    LOGGER(DEBUG) << "plugin_session_count : " <<
+        XWalkExtensionRendererController::plugin_session_count;
+    if (XWalkExtensionRendererController::plugin_session_count > 0) {
+      LOGGER(DEBUG) << "Defer termination of main loop";
+      ecore_main_loop_begin();
+    }
     runtime.reset();
   }
+  LOGGER(DEBUG) << "ewk_shutdown";
   ewk_shutdown();
-  exit(ret);
+  elm_shutdown();
+  elm_exit();
 
+  LOGGER(DEBUG) << "EXIT_SUCCESS";
   return EXIT_SUCCESS;
 }
 
+__attribute__((visibility("default")))
 int main(int argc, char* argv[]) {
   if (strcmp(argv[0], "/usr/bin/wrt-loader") == 0) {
     elm_init(argc, argv);
+    elm_config_cache_flush_enabled_set(EINA_TRUE);
     auto preload = [argv](void) {
       g_prelaunch = true;
       ewk_init();