Defer main loop until window is completely closed
[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 "runtime/browser/runtime.h"
32 #include "runtime/common/constants.h"
33 #include "runtime/browser/prelauncher.h"
34 #include "runtime/browser/preload_manager.h"
35
36 #include "runtime/browser/ui_runtime.h"
37
38 bool g_prelaunch = false;
39
40 #ifdef WATCH_FACE_FEATURE_SUPPORT
41 static int setWatchEnv(int argc, char **argv) {
42   bundle *kb = NULL;
43   char *wayland_display = NULL;
44   char *xdg_runtime_dir = NULL;
45   char *width_str = NULL;
46   char *height_str = NULL;
47
48   if (argc <= 0 || argv == NULL) {
49     errno = EINVAL;
50     LOGGER(ERROR) << "argument are invalid";
51     return -1;
52   }
53
54   kb = bundle_import_from_argv(argc, argv);
55   if (kb) {
56     bundle_get_str(kb, "XDG_RUNTIME_DIR", &xdg_runtime_dir);
57     bundle_get_str(kb, "WAYLAND_DISPLAY", &wayland_display);
58     bundle_get_str(kb, "WATCH_WIDTH", &width_str);
59     bundle_get_str(kb, "WATCH_HEIGHT", &height_str);
60
61     if (xdg_runtime_dir) {
62       LOGGER(DEBUG) << "senenv: " << xdg_runtime_dir;
63       setenv("XDG_RUNTIME_DIR", xdg_runtime_dir, 1);
64     } else {
65       LOGGER(ERROR) << "failed to get xdgruntimedir";
66     }
67
68     if (wayland_display) {
69       LOGGER(DEBUG) << "setenv: " << wayland_display;
70       setenv("WAYLAND_DISPLAY", wayland_display, 1);
71     } else {
72       LOGGER(ERROR) << "failed to get waylanddisplay";
73     }
74     bundle_free(kb);
75   } else {
76     LOGGER(ERROR) << "failed to get launch argv";
77   }
78   return 0;
79 }
80 #endif  // WATCH_FACE_FEATURE_SUPPORT
81
82 int real_main(int argc, char* argv[]) {
83   STEP_PROFILE_START("Start -> Launch Completed");
84   STEP_PROFILE_START("Start -> OnCreate");
85   // Parse commandline.
86   common::CommandLine::Init(argc, argv);
87
88   common::CommandLine* cmd = common::CommandLine::ForCurrentProcess();
89   std::string appid = cmd->GetAppIdFromCommandLine(runtime::kRuntimeExecName);
90
91   // Load Manifest
92   auto appdata_manager = common::ApplicationDataManager::GetInstance();
93   common::ApplicationData* appdata = appdata_manager->GetApplicationData(appid);
94   if (!appdata->LoadManifestData()) {
95     return false;
96   }
97
98 #ifdef WATCH_FACE_FEATURE_SUPPORT
99   if (appdata->app_type() == common::ApplicationData::WATCH) {
100     setWatchEnv(argc, argv);
101   }
102 #endif  // WATCH_FACE_FEATURE_SUPPORT
103
104   // Default behavior, run as runtime.
105   LOGGER(INFO) << "Runtime process has been created.";
106   if (!g_prelaunch) {
107     ewk_init();
108     char* chromium_arg_options[] = {
109       argv[0],
110       const_cast<char*>("--no-sandbox"),
111       const_cast<char*>("--enable-file-cookies"),
112       const_cast<char*>("--allow-file-access-from-files"),
113       const_cast<char*>("--allow-universal-access-from-files"),
114       const_cast<char*>("--single-process")
115     };
116     const int chromium_arg_cnt =
117         sizeof(chromium_arg_options) / sizeof(chromium_arg_options[0]);
118     ewk_set_arguments(chromium_arg_cnt, chromium_arg_options);
119 #ifdef WATCH_FACE_FEATURE_SUPPORT
120   } else {
121     if (appdata->app_type() == common::ApplicationData::WATCH) {
122       // Below code will be enabled after testing
123       //ecore_wl_shutdown();
124       //ecore_wl_init(NULL);
125     }
126 #endif  // WATCH_FACE_FEATURE_SUPPORT
127   }
128
129   int ret = 0;
130   // Runtime's destructor should be called before ewk_shutdown()
131   {
132     std::unique_ptr<runtime::Runtime> runtime =
133         runtime::Runtime::MakeRuntime(appdata);
134     ret = runtime->Exec(argc, argv);
135     if (runtime->is_on_terminate_called) {
136       ecore_main_loop_begin();
137     }
138     runtime.reset();
139   }
140   ewk_shutdown();
141   elm_shutdown();
142   elm_exit();
143
144   return EXIT_SUCCESS;
145 }
146
147 __attribute__((visibility("default")))
148 int main(int argc, char* argv[]) {
149   if (strcmp(argv[0], "/usr/bin/wrt-loader") == 0) {
150     elm_init(argc, argv);
151     elm_config_cache_flush_enabled_set(EINA_TRUE);
152     auto preload = [argv](void) {
153       g_prelaunch = true;
154       ewk_init();
155       char* chromium_arg_options[] = {
156         argv[0],
157         const_cast<char*>("--no-sandbox"),
158         const_cast<char*>("--enable-file-cookies"),
159         const_cast<char*>("--allow-file-access-from-files"),
160         const_cast<char*>("--allow-universal-access-from-files"),
161         const_cast<char*>("--single-process")
162       };
163       const int chromium_arg_cnt =
164           sizeof(chromium_arg_options) / sizeof(chromium_arg_options[0]);
165       ewk_set_arguments(chromium_arg_cnt, chromium_arg_options);
166       runtime::PreloadManager::GetInstance()->CreateCacheComponet();
167     };
168     auto did_launch = [](const std::string& app_path) {
169     };
170     auto prelaunch = runtime::PreLauncher::Prelaunch;
171     return prelaunch(argc, argv, preload, did_launch, real_main);
172   } else {
173     return real_main(argc, argv);
174   }
175 }