799621ccf8ba6721c98dff6fa1eea6baf505437b
[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 // For debug purpose only.
142 // TODO: To be removed later
143 bool hasTizenPackageID(int argc, const char* const* argv) {
144   if (argc > 3) {
145     if (0 == strncmp(argv[0], "/opt/usr/globalapps/", strlen("/opt/usr/globalapps/"))) {
146       return true;
147     }
148   }
149   return false;
150 }
151
152 int real_main(int argc, char* argv[]) {
153 #else
154 int main(int argc, char* argv[]) {
155 #endif
156   if (IsEnvSet(kRunAsNode)) {
157     base::i18n::InitializeICU();
158     base::AtExitManager atexit_manager;
159     return atom::NodeMain(argc, const_cast<char**>(argv));
160   }
161
162 #if defined(USE_EFL)
163   common::CommandLine::Init(argc, argv);
164   common::CommandLine* runtime_cmd = common::CommandLine::ForCurrentProcess();
165   std::string appid = runtime_cmd->GetAppIdFromCommandLine("/usr/bin/wrt");
166
167   // load manifest
168   if (appid != "wrt") { // TODO: Any better way to distinguish?
169     auto appdata_manager = common::ApplicationDataManager::GetInstance();
170     common::ApplicationData* appdata = appdata_manager->GetApplicationData(appid);
171     if (!appdata->LoadManifestData()) {
172       return false;
173     }
174   }
175
176   if (efl::Initialize(argc, const_cast<const char**>(argv)))
177     return 1;
178
179   // Add params for EFL port
180   base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
181
182   static std::vector<char*> flags;
183   for (auto arg : kDefaultCommandLineFlags)
184     command_line->AppendSwitch(const_cast<char*>(arg));
185   efl::AppendPortParams(*command_line);
186 #endif
187
188   atom::AtomMainDelegate delegate;
189   content::ContentMainParams params(&delegate);
190   params.argc = argc;
191   params.argv = const_cast<const char**>(argv);
192   atom::AtomCommandLine::Init(argc, argv);
193 #if defined(OS_TIZEN)
194   if (hasTizenPackageID(argc,argv)) { // TODO: Check to be removed later
195     elm_init(argc, argv);
196     std::unique_ptr<runtime::Runtime> runtime = runtime::Runtime::MakeRuntime(&params);
197     return runtime->Exec();
198   }
199 #endif
200   return content::ContentMain(params);
201 }
202
203 #if defined(OS_TIZEN)
204 __attribute__((visibility("default")))
205 int main(int argc, const char* argv[]) {
206   if (strcmp(argv[0], "/usr/bin/wrt-loader") == 0) {
207     LOG(INFO) << "run with wrt-loader";
208     auto preload = [argv](void) {
209     };
210     auto did_launch = [](const std::string& app_path) {
211     };
212     auto prelaunch = runtime::PreLauncher::Prelaunch;
213     return prelaunch(argc, const_cast<char**>(argv), preload, did_launch, real_main);
214   } else {
215     LOG(INFO) << "run without wrt-loader";
216     return real_main(argc, const_cast<char**>(argv));
217   }
218 }
219 #endif
220
221 #else  // defined(OS_LINUX)
222
223 int main(int argc, const char* argv[]) {
224   if (IsEnvSet(kRunAsNode)) {
225     return AtomInitializeICUandStartNode(argc, const_cast<char**>(argv));
226   }
227
228   return AtomMain(argc, argv);
229 }
230
231 #endif  // defined(OS_MACOSX)