Assure proper termination using context session
[platform/framework/web/crosswalk-tizen.git] / runtime / browser / runtime_process.cc
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 #include <EWebKit.h>
18 #include <EWebKit_internal.h>
19
20 #include <Elementary.h>
21
22 #ifdef WATCH_FACE_FEATURE_SUPPORT
23 #include <bundle_internal.h>
24 #include <Ecore_Wayland.h>
25 #endif  // WATCH_FACE_FEATURE_SUPPORT
26
27 #include "common/application_data.h"
28 #include "common/command_line.h"
29 #include "common/logger.h"
30 #include "common/profiler.h"
31 #include "extensions/renderer/xwalk_extension_renderer_controller.h"
32 #include "runtime/browser/runtime.h"
33 #include "runtime/common/constants.h"
34 #include "runtime/browser/prelauncher.h"
35 #include "runtime/browser/preload_manager.h"
36
37 #include "runtime/browser/ui_runtime.h"
38
39 using namespace extensions;
40 bool g_prelaunch = false;
41
42 #ifdef WATCH_FACE_FEATURE_SUPPORT
43 static int setWatchEnv(int argc, char **argv) {
44   bundle *kb = NULL;
45   char *wayland_display = NULL;
46   char *xdg_runtime_dir = NULL;
47   char *width_str = NULL;
48   char *height_str = NULL;
49
50   if (argc <= 0 || argv == NULL) {
51     errno = EINVAL;
52     LOGGER(ERROR) << "argument are invalid";
53     return -1;
54   }
55
56   kb = bundle_import_from_argv(argc, argv);
57   if (kb) {
58     bundle_get_str(kb, "XDG_RUNTIME_DIR", &xdg_runtime_dir);
59     bundle_get_str(kb, "WAYLAND_DISPLAY", &wayland_display);
60     bundle_get_str(kb, "WATCH_WIDTH", &width_str);
61     bundle_get_str(kb, "WATCH_HEIGHT", &height_str);
62
63     if (xdg_runtime_dir) {
64       LOGGER(DEBUG) << "senenv: " << xdg_runtime_dir;
65       setenv("XDG_RUNTIME_DIR", xdg_runtime_dir, 1);
66     } else {
67       LOGGER(ERROR) << "failed to get xdgruntimedir";
68     }
69
70     if (wayland_display) {
71       LOGGER(DEBUG) << "setenv: " << wayland_display;
72       setenv("WAYLAND_DISPLAY", wayland_display, 1);
73     } else {
74       LOGGER(ERROR) << "failed to get waylanddisplay";
75     }
76     bundle_free(kb);
77   } else {
78     LOGGER(ERROR) << "failed to get launch argv";
79   }
80   return 0;
81 }
82 #endif  // WATCH_FACE_FEATURE_SUPPORT
83
84 int real_main(int argc, char* argv[]) {
85   STEP_PROFILE_START("Start -> Launch Completed");
86   STEP_PROFILE_START("Start -> OnCreate");
87   // Parse commandline.
88   common::CommandLine::Init(argc, argv);
89
90   common::CommandLine* cmd = common::CommandLine::ForCurrentProcess();
91   std::string appid = cmd->GetAppIdFromCommandLine(runtime::kRuntimeExecName);
92
93   // Load Manifest
94   auto appdata_manager = common::ApplicationDataManager::GetInstance();
95   common::ApplicationData* appdata = appdata_manager->GetApplicationData(appid);
96   if (!appdata->LoadManifestData()) {
97     return false;
98   }
99
100 #ifdef WATCH_FACE_FEATURE_SUPPORT
101   if (appdata->app_type() == common::ApplicationData::WATCH) {
102     setWatchEnv(argc, argv);
103   }
104 #endif  // WATCH_FACE_FEATURE_SUPPORT
105
106   // Default behavior, run as runtime.
107   LOGGER(INFO) << "Runtime process has been created.";
108   if (!g_prelaunch) {
109     ewk_init();
110     char* chromium_arg_options[] = {
111       argv[0],
112       const_cast<char*>("--no-sandbox"),
113       const_cast<char*>("--enable-file-cookies"),
114       const_cast<char*>("--allow-file-access-from-files"),
115       const_cast<char*>("--allow-universal-access-from-files"),
116       const_cast<char*>("--single-process")
117     };
118     const int chromium_arg_cnt =
119         sizeof(chromium_arg_options) / sizeof(chromium_arg_options[0]);
120     ewk_set_arguments(chromium_arg_cnt, chromium_arg_options);
121 #ifdef WATCH_FACE_FEATURE_SUPPORT
122   } else {
123     if (appdata->app_type() == common::ApplicationData::WATCH) {
124       // Below code will be enabled after testing
125       //ecore_wl_shutdown();
126       //ecore_wl_init(NULL);
127     }
128 #endif  // WATCH_FACE_FEATURE_SUPPORT
129   }
130
131   int ret = 0;
132   // Runtime's destructor should be called before ewk_shutdown()
133   {
134     std::unique_ptr<runtime::Runtime> runtime =
135         runtime::Runtime::MakeRuntime(appdata);
136     ret = runtime->Exec(argc, argv);
137     if (ret)
138       LOGGER(ERROR) << "Exec returns non zero.";
139     LOGGER(DEBUG) << "plugin_session_count : " <<
140         XWalkExtensionRendererController::plugin_session_count;
141     if (XWalkExtensionRendererController::plugin_session_count > 0) {
142       LOGGER(DEBUG) << "Defer termination of main loop";
143       ecore_main_loop_begin();
144     }
145     runtime.reset();
146   }
147   LOGGER(DEBUG) << "ewk_shutdown";
148   ewk_shutdown();
149   elm_shutdown();
150   elm_exit();
151
152   LOGGER(DEBUG) << "EXIT_SUCCESS";
153   return EXIT_SUCCESS;
154 }
155
156 __attribute__((visibility("default")))
157 int main(int argc, char* argv[]) {
158   if (strcmp(argv[0], "/usr/bin/wrt-loader") == 0) {
159     elm_init(argc, argv);
160     elm_config_cache_flush_enabled_set(EINA_TRUE);
161     auto preload = [argv](void) {
162       g_prelaunch = true;
163       ewk_init();
164       char* chromium_arg_options[] = {
165         argv[0],
166         const_cast<char*>("--no-sandbox"),
167         const_cast<char*>("--enable-file-cookies"),
168         const_cast<char*>("--allow-file-access-from-files"),
169         const_cast<char*>("--allow-universal-access-from-files"),
170         const_cast<char*>("--single-process")
171       };
172       const int chromium_arg_cnt =
173           sizeof(chromium_arg_options) / sizeof(chromium_arg_options[0]);
174       ewk_set_arguments(chromium_arg_cnt, chromium_arg_options);
175       runtime::PreloadManager::GetInstance()->CreateCacheComponet();
176     };
177     auto did_launch = [](const std::string& app_path) {
178     };
179     auto prelaunch = runtime::PreLauncher::Prelaunch;
180     return prelaunch(argc, argv, preload, did_launch, real_main);
181   } else {
182     return real_main(argc, argv);
183   }
184 }