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 // For debug purpose only.
142 // TODO: To be removed later
143 bool hasTizenPackageID(int argc, const char* const* argv) {
145 if (0 == strncmp(argv[0], "/opt/usr/globalapps/", strlen("/opt/usr/globalapps/"))) {
152 int real_main(int argc, char* argv[]) {
154 int main(int argc, char* argv[]) {
156 if (IsEnvSet(kRunAsNode)) {
157 base::i18n::InitializeICU();
158 base::AtExitManager atexit_manager;
159 return atom::NodeMain(argc, const_cast<char**>(argv));
163 common::CommandLine::Init(argc, argv);
164 common::CommandLine* runtime_cmd = common::CommandLine::ForCurrentProcess();
165 std::string appid = runtime_cmd->GetAppIdFromCommandLine("/usr/bin/wrt");
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()) {
176 if (efl::Initialize(argc, const_cast<const char**>(argv)))
179 // Add params for EFL port
180 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
182 static std::vector<char*> flags;
183 for (auto arg : kDefaultCommandLineFlags)
184 command_line->AppendSwitch(const_cast<char*>(arg));
185 efl::AppendPortParams(*command_line);
188 atom::AtomMainDelegate delegate;
189 content::ContentMainParams params(&delegate);
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(¶ms);
197 return runtime->Exec();
200 return content::ContentMain(params);
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) {
210 auto did_launch = [](const std::string& app_path) {
212 auto prelaunch = runtime::PreLauncher::Prelaunch;
213 return prelaunch(argc, const_cast<char**>(argv), preload, did_launch, real_main);
215 LOG(INFO) << "run without wrt-loader";
216 return real_main(argc, const_cast<char**>(argv));
221 #else // defined(OS_LINUX)
223 int main(int argc, const char* argv[]) {
224 if (IsEnvSet(kRunAsNode)) {
225 return AtomInitializeICUandStartNode(argc, const_cast<char**>(argv));
228 return AtomMain(argc, argv);
231 #endif // defined(OS_MACOSX)