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.
10 #include <windows.h> // windows.h must be included first
13 #include <shellscalingapi.h>
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)
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"
40 #include <Elementary.h>
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"
51 const char* kRunAsNode = "ELECTRON_RUN_AS_NODE";
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",
59 bool IsEnvSet(const char* name) {
62 getenv_s(&required_size, nullptr, 0, name);
63 return required_size != 0;
65 char* indicator = getenv(name);
66 return indicator && indicator[0] != '\0';
73 int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE, wchar_t* cmd, int) {
75 wchar_t** wargv = ::CommandLineToArgvW(::GetCommandLineW(), &argc);
77 bool run_as_node = IsEnvSet(kRunAsNode);
79 // Make sure the output is printed to console.
80 if (run_as_node || !IsEnvSet("ELECTRON_NO_ATTACH_CONSOLE"))
81 base::RouteStdioToConsole(false);
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,
96 // This should never happen.
97 fprintf(stderr, "Could not convert arguments to utf8.");
100 // Do the actual conversion
101 argv[i] = new char[size];
102 DWORD result = WideCharToMultiByte(CP_UTF8,
111 // This should never happen.
112 fprintf(stderr, "Could not convert arguments to utf8.");
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);
126 sandbox::SandboxInterfaceInfo sandbox_info = {0};
127 content::InitializeSandboxInfo(&sandbox_info);
128 atom::AtomMainDelegate delegate;
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);
138 #elif defined(OS_LINUX) // defined(OS_WIN)
140 #if defined(OS_TIZEN)
141 bool g_initialized_ = false;
142 std::unique_ptr<runtime::Runtime> runtime_;
144 // For debug purpose only.
145 // TODO: To be removed later
146 bool hasTizenPackageID(int argc, const char* const* argv) {
148 if (0 == strncmp(argv[0], "/opt/usr/globalapps/", strlen("/opt/usr/globalapps/"))) {
155 int real_main(int argc, char* argv[]) {
157 int main(int argc, char* argv[]) {
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));
168 if (!common::CommandLine::Init(argc, argv)) {
169 common::CommandLine::Reset();
170 common::CommandLine::Init(argc, argv);
173 if (!base::CommandLine::Init(argc, argv)) {
174 base::CommandLine::Reset();
175 base::CommandLine::Init(argc, argv);
177 common::CommandLine* runtime_cmd = common::CommandLine::ForCurrentProcess();
178 std::string appid = runtime_cmd->GetAppIdFromCommandLine("/usr/bin/wrt");
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()) {
188 if (!g_initialized_) {
189 if (efl::Initialize(argc, const_cast<const char**>(argv)))
193 // Add params for EFL port
194 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
196 static std::vector<char*> flags;
197 for (auto arg : kDefaultCommandLineFlags)
198 command_line->AppendSwitch(const_cast<char*>(arg));
199 efl::AppendPortParams(*command_line);
202 atom::AtomMainDelegate delegate;
203 content::ContentMainParams params(&delegate);
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(¶ms);
213 runtime_->SetParam(¶ms);
215 return runtime_->Exec();
218 return content::ContentMain(params);
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)))
233 atom::AtomMainDelegate delegate;
234 content::ContentMainParams params(&delegate);
236 params.argv = const_cast<const char**>(argv);
237 atom::AtomCommandLine::Init(argc, argv);
238 runtime_ = runtime::Runtime::MakeRuntime(nullptr);
241 auto did_launch = [](const std::string& app_path) {
243 auto prelaunch = runtime::PreLauncher::Prelaunch;
244 return prelaunch(argc, const_cast<char**>(argv), preload, did_launch, real_main);
246 LOG(ERROR) << "run without wrt-loader";
247 return real_main(argc, const_cast<char**>(argv));
252 #else // defined(OS_LINUX)
254 int main(int argc, const char* argv[]) {
255 if (IsEnvSet(kRunAsNode)) {
256 return AtomInitializeICUandStartNode(argc, const_cast<char**>(argv));
259 return AtomMain(argc, argv);
262 #endif // defined(OS_MACOSX)