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