atom_main.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #include "atom/app/atom_main.h"
  5. #include <cstdlib>
  6. #include <vector>
  7. #if defined(OS_WIN)
  8. #include <windows.h> // windows.h must be included first
  9. #include <atlbase.h> // ensures that ATL statics like `_AtlWinModule` are initialized (it's an issue in static debug build)
  10. #include <shellapi.h>
  11. #include <shellscalingapi.h>
  12. #include <tchar.h>
  13. #include "atom/app/atom_main_delegate.h"
  14. #include "atom/app/command_line_args.h"
  15. #include "atom/common/crash_reporter/win/crash_service_main.h"
  16. #include "base/environment.h"
  17. #include "base/process/launch.h"
  18. #include "base/strings/utf_string_conversions.h"
  19. #include "base/win/windows_version.h"
  20. #include "content/public/app/sandbox_helper_win.h"
  21. #include "sandbox/win/src/sandbox_types.h"
  22. #elif defined(OS_LINUX) // defined(OS_WIN)
  23. #include "atom/app/atom_main_delegate.h" // NOLINT
  24. #include "content/public/app/content_main.h"
  25. #else // defined(OS_LINUX)
  26. #include "atom/app/atom_library_main.h"
  27. #endif // defined(OS_MACOSX)
  28. #include "atom/app/node_main.h"
  29. #include "atom/common/atom_command_line.h"
  30. #include "base/at_exit.h"
  31. #include "base/i18n/icu_util.h"
  32. namespace {
  33. #ifdef ENABLE_RUN_AS_NODE
  34. const char kRunAsNode[] = "ELECTRON_RUN_AS_NODE";
  35. #endif
  36. #if defined(ENABLE_RUN_AS_NODE) || defined(OS_WIN)
  37. bool IsEnvSet(const char* name) {
  38. #if defined(OS_WIN)
  39. size_t required_size;
  40. getenv_s(&required_size, nullptr, 0, name);
  41. return required_size != 0;
  42. #else
  43. char* indicator = getenv(name);
  44. return indicator && indicator[0] != '\0';
  45. #endif
  46. }
  47. #endif
  48. } // namespace
  49. #if defined(OS_WIN)
  50. int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE, wchar_t* cmd, int) {
  51. struct Arguments {
  52. int argc = 0;
  53. wchar_t** argv = ::CommandLineToArgvW(::GetCommandLineW(), &argc);
  54. ~Arguments() { LocalFree(argv); }
  55. } arguments;
  56. if (!arguments.argv)
  57. return -1;
  58. #ifdef _DEBUG
  59. // Don't display assert dialog boxes in CI test runs
  60. static const auto kCI = "ELECTRON_CI";
  61. bool is_ci = IsEnvSet(kCI);
  62. if (!is_ci) {
  63. for (int i = 0; i < arguments.argc; ++i) {
  64. if (!_wcsicmp(arguments.argv[i], L"--ci")) {
  65. is_ci = true;
  66. _putenv_s(kCI, "1"); // set flag for child processes
  67. break;
  68. }
  69. }
  70. }
  71. if (is_ci) {
  72. _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_FILE);
  73. _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
  74. _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_FILE);
  75. _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
  76. _set_error_mode(_OUT_TO_STDERR);
  77. }
  78. #endif
  79. #ifdef ENABLE_RUN_AS_NODE
  80. bool run_as_node = IsEnvSet(kRunAsNode);
  81. #else
  82. bool run_as_node = false;
  83. #endif
  84. // Make sure the output is printed to console.
  85. if (run_as_node || !IsEnvSet("ELECTRON_NO_ATTACH_CONSOLE"))
  86. base::RouteStdioToConsole(false);
  87. #ifndef DEBUG
  88. // Chromium has its own TLS subsystem which supports automatic destruction
  89. // of thread-local data, and also depends on memory allocation routines
  90. // provided by the CRT. The problem is that the auto-destruction mechanism
  91. // uses a hidden feature of the OS loader which calls a callback on thread
  92. // exit, but only after all loaded DLLs have been detached. Since the CRT is
  93. // also a DLL, it happens that by the time Chromium's `OnThreadExit` function
  94. // is called, the heap functions, though still in memory, no longer perform
  95. // their duties, and when Chromium calls `free` on its buffer, it triggers
  96. // an access violation error.
  97. // We work around this problem by invoking Chromium's `OnThreadExit` in time
  98. // from within the CRT's atexit facility, ensuring the heap functions are
  99. // still active. The second invocation from the OS loader will be a no-op.
  100. extern void NTAPI OnThreadExit(PVOID module, DWORD reason, PVOID reserved);
  101. atexit([]() { OnThreadExit(nullptr, DLL_THREAD_DETACH, nullptr); });
  102. #endif
  103. #ifdef ENABLE_RUN_AS_NODE
  104. if (run_as_node) {
  105. std::vector<char*> argv(arguments.argc);
  106. std::transform(
  107. arguments.argv, arguments.argv + arguments.argc, argv.begin(),
  108. [](auto& a) { return _strdup(base::WideToUTF8(a).c_str()); });
  109. base::AtExitManager atexit_manager;
  110. base::i18n::InitializeICU();
  111. auto ret = atom::NodeMain(argv.size(), argv.data());
  112. std::for_each(argv.begin(), argv.end(), free);
  113. return ret;
  114. }
  115. #endif
  116. if (IsEnvSet("ELECTRON_INTERNAL_CRASH_SERVICE")) {
  117. return crash_service::Main(cmd);
  118. }
  119. if (!atom::CheckCommandLineArguments(arguments.argc, arguments.argv))
  120. return -1;
  121. sandbox::SandboxInterfaceInfo sandbox_info = {0};
  122. content::InitializeSandboxInfo(&sandbox_info);
  123. atom::AtomMainDelegate delegate;
  124. content::ContentMainParams params(&delegate);
  125. params.instance = instance;
  126. params.sandbox_info = &sandbox_info;
  127. atom::AtomCommandLine::Init(arguments.argc, arguments.argv);
  128. return content::ContentMain(params);
  129. }
  130. #elif defined(OS_LINUX) // defined(OS_WIN)
  131. int main(int argc, char* argv[]) {
  132. #ifdef ENABLE_RUN_AS_NODE
  133. if (IsEnvSet(kRunAsNode)) {
  134. base::i18n::InitializeICU();
  135. base::AtExitManager atexit_manager;
  136. return atom::NodeMain(argc, argv);
  137. }
  138. #endif
  139. atom::AtomMainDelegate delegate;
  140. content::ContentMainParams params(&delegate);
  141. params.argc = argc;
  142. params.argv = const_cast<const char**>(argv);
  143. atom::AtomCommandLine::Init(argc, argv);
  144. return content::ContentMain(params);
  145. }
  146. #else // defined(OS_LINUX)
  147. int main(int argc, char* argv[]) {
  148. #ifdef ENABLE_RUN_AS_NODE
  149. if (IsEnvSet(kRunAsNode)) {
  150. return AtomInitializeICUandStartNode(argc, argv);
  151. }
  152. #endif
  153. return AtomMain(argc, argv);
  154. }
  155. #endif // defined(OS_MACOSX)