Initial Implementation of Node prelaunch
[platform/framework/web/crosswalk-tizen.git] / tizen / src / wrt_main.cc
1 // Copyright (c) 2013 GitHub, Inc.
2 // Use of this source code is governed by the MIT license that can be
3 // found in the LICENSE file.
4
5 #include "wrt_main.h"
6
7 #include <stdlib.h>
8
9 #if defined(OS_WIN)
10 #include <windows.h>  // windows.h must be included first
11
12 #include <shellapi.h>
13 #include <shellscalingapi.h>
14 #include <tchar.h>
15
16 #include "atom/app/atom_main_delegate.h"
17 #include "atom/common/crash_reporter/win/crash_service_main.h"
18 #include "base/environment.h"
19 #include "base/process/launch.h"
20 #include "base/win/windows_version.h"
21 #include "content/public/app/sandbox_helper_win.h"
22 #include "sandbox/win/src/sandbox_types.h"
23 #elif defined(OS_LINUX)  // defined(OS_WIN)
24 #include "atom/app/atom_main_delegate.h"  // NOLINT
25 #include "content/public/app/content_main.h"
26 #else  // defined(OS_LINUX)
27 #include "atom/app/atom_library_main.h"
28 #endif  // defined(OS_MACOSX)
29
30 #if defined(USE_EFL)
31 #include "efl/init.h"
32 #endif
33
34 #include "atom/app/node_main.h"
35 #include "atom/common/atom_command_line.h"
36 #include "base/at_exit.h"
37 #include "base/i18n/icu_util.h"
38
39 #if defined(OS_TIZEN)
40 #include <Elementary.h>
41
42 #include "atom/app/runtime.h"
43 #include "base/logging.h"
44 #include "tizen/common/application_data.h"
45 #include "tizen/common/command_line.h"
46 #include "tizen/loader/prelauncher.h"
47 #endif
48
49 namespace {
50
51 const char* kRunAsNode = "ELECTRON_RUN_AS_NODE";
52
53 // Default command line flags for all profiles and platforms
54 const char* kDefaultCommandLineFlags[] = {
55   "allow-file-access-from-files",
56   "enable-tizen-app-container",
57 };
58
59 bool IsEnvSet(const char* name) {
60 #if defined(OS_WIN)
61   size_t required_size;
62   getenv_s(&required_size, nullptr, 0, name);
63   return required_size != 0;
64 #else
65   char* indicator = getenv(name);
66   return indicator && indicator[0] != '\0';
67 #endif
68 }
69
70 }  // namespace
71
72 #if defined(OS_WIN)
73 int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE, wchar_t* cmd, int) {
74   int argc = 0;
75   wchar_t** wargv = ::CommandLineToArgvW(::GetCommandLineW(), &argc);
76
77   bool run_as_node = IsEnvSet(kRunAsNode);
78
79   // Make sure the output is printed to console.
80   if (run_as_node || !IsEnvSet("ELECTRON_NO_ATTACH_CONSOLE"))
81     base::RouteStdioToConsole(false);
82
83   // Convert argv to to UTF8
84   char** argv = new char*[argc];
85   for (int i = 0; i < argc; i++) {
86     // Compute the size of the required buffer
87     DWORD size = WideCharToMultiByte(CP_UTF8,
88                                      0,
89                                      wargv[i],
90                                      -1,
91                                      NULL,
92                                      0,
93                                      NULL,
94                                      NULL);
95     if (size == 0) {
96       // This should never happen.
97       fprintf(stderr, "Could not convert arguments to utf8.");
98       exit(1);
99     }
100     // Do the actual conversion
101     argv[i] = new char[size];
102     DWORD result = WideCharToMultiByte(CP_UTF8,
103                                        0,
104                                        wargv[i],
105                                        -1,
106                                        argv[i],
107                                        size,
108                                        NULL,
109                                        NULL);
110     if (result == 0) {
111       // This should never happen.
112       fprintf(stderr, "Could not convert arguments to utf8.");
113       exit(1);
114     }
115   }
116
117   if (run_as_node) {
118     // Now that argv conversion is done, we can finally start.
119     base::AtExitManager atexit_manager;
120     base::i18n::InitializeICU();
121     return atom::NodeMain(argc, argv);
122   } else if (IsEnvSet("ELECTRON_INTERNAL_CRASH_SERVICE")) {
123     return crash_service::Main(cmd);
124   }
125
126   sandbox::SandboxInterfaceInfo sandbox_info = {0};
127   content::InitializeSandboxInfo(&sandbox_info);
128   atom::AtomMainDelegate delegate;
129
130   content::ContentMainParams params(&delegate);
131   params.instance = instance;
132   params.sandbox_info = &sandbox_info;
133   atom::AtomCommandLine::Init(argc, argv);
134   atom::AtomCommandLine::InitW(argc, wargv);
135   return content::ContentMain(params);
136 }
137
138 #elif defined(OS_LINUX)  // defined(OS_WIN)
139
140 #if defined(OS_TIZEN)
141 bool g_initialized_ = false;
142 std::unique_ptr<runtime::Runtime> runtime_;
143
144 // For debug purpose only.
145 // TODO: To be removed later
146 bool hasTizenPackageID(int argc, const char* const* argv) {
147   if (argc > 3) {
148     if (0 == strncmp(argv[0], "/opt/usr/globalapps/", strlen("/opt/usr/globalapps/"))) {
149       return true;
150     }
151   }
152   return false;
153 }
154
155 int real_main(int argc, char* argv[]) {
156 #else
157 int main(int argc, char* argv[]) {
158 #endif
159   for (int i = 0; i < argc; ++i)
160     LOG(ERROR) << "argv[" << i << "] : " << argv[i];
161   if (IsEnvSet(kRunAsNode)) {
162     base::i18n::InitializeICU();
163     base::AtExitManager atexit_manager;
164     return atom::NodeMain(argc, const_cast<char**>(argv));
165   }
166
167 #if defined(USE_EFL)
168   if (!common::CommandLine::Init(argc, argv)) {
169     common::CommandLine::Reset();
170     common::CommandLine::Init(argc, argv);
171   }
172
173   if (!base::CommandLine::Init(argc, argv)) {
174     base::CommandLine::Reset();
175     base::CommandLine::Init(argc, argv);
176   }
177   common::CommandLine* runtime_cmd = common::CommandLine::ForCurrentProcess();
178   std::string appid = runtime_cmd->GetAppIdFromCommandLine("/usr/bin/wrt");
179
180   // load manifest
181   if (appid != "wrt") { // TODO: Any better way to distinguish?
182     auto appdata_manager = common::ApplicationDataManager::GetInstance();
183     common::ApplicationData* appdata = appdata_manager->GetApplicationData(appid);
184     if (!appdata->LoadManifestData()) {
185       return false;
186     }
187   }
188   if (!g_initialized_) {
189     if (efl::Initialize(argc, const_cast<const char**>(argv)))
190       return 1;
191   }
192
193   // Add params for EFL port
194   base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
195
196   static std::vector<char*> flags;
197   for (auto arg : kDefaultCommandLineFlags)
198     command_line->AppendSwitch(const_cast<char*>(arg));
199   efl::AppendPortParams(*command_line);
200 #endif
201
202   atom::AtomMainDelegate delegate;
203   content::ContentMainParams params(&delegate);
204   params.argc = argc;
205   params.argv = const_cast<const char**>(argv);
206   atom::AtomCommandLine::Init(argc, argv);
207 #if defined(OS_TIZEN)
208   if (hasTizenPackageID(argc,argv)) { // TODO: Check to be removed later
209     elm_init(argc, argv);
210     if (!g_initialized_) {
211       runtime_ = runtime::Runtime::MakeRuntime(&params);
212     } else {
213       runtime_->SetParam(&params);
214     }
215     return runtime_->Exec();
216   }
217 #endif
218   return content::ContentMain(params);
219 }
220
221 #if defined(OS_TIZEN)
222 __attribute__((visibility("default")))
223 int main(int argc, const char* argv[]) {
224   for (int i = 0; i < argc; ++i)
225     LOG(ERROR) << "argv[" << i << "] : " << argv[i];
226   if (strcmp(argv[0], "/usr/bin/wrt-loader") == 0) {
227     LOG(ERROR) << "run with wrt-loader";
228     auto preload = [argc, argv](void) {
229       g_initialized_ = true;
230       if (efl::Initialize(argc, const_cast<const char**>(argv)))
231         return 1;
232
233       atom::AtomMainDelegate delegate;
234       content::ContentMainParams params(&delegate);
235       params.argc = argc;
236       params.argv = const_cast<const char**>(argv);
237       atom::AtomCommandLine::Init(argc, argv);
238       runtime_ = runtime::Runtime::MakeRuntime(nullptr);
239       return 0;
240     };
241     auto did_launch = [](const std::string& app_path) {
242     };
243     auto prelaunch = runtime::PreLauncher::Prelaunch;
244     return prelaunch(argc, const_cast<char**>(argv), preload, did_launch, real_main);
245   } else {
246     LOG(ERROR) << "run without wrt-loader";
247     return real_main(argc, const_cast<char**>(argv));
248   }
249 }
250 #endif
251
252 #else  // defined(OS_LINUX)
253
254 int main(int argc, const char* argv[]) {
255   if (IsEnvSet(kRunAsNode)) {
256     return AtomInitializeICUandStartNode(argc, const_cast<char**>(argv));
257   }
258
259   return AtomMain(argc, argv);
260 }
261
262 #endif  // defined(OS_MACOSX)