atom_main.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 <stdlib.h>
  6. #if defined(OS_WIN)
  7. #include <windows.h> // windows.h must be included first
  8. #include <shellapi.h>
  9. #include <shellscalingapi.h>
  10. #include <tchar.h>
  11. #include "atom/app/atom_main_delegate.h"
  12. #include "atom/common/crash_reporter/win/crash_service_main.h"
  13. #include "base/environment.h"
  14. #include "base/process/launch.h"
  15. #include "base/win/windows_version.h"
  16. #include "content/public/app/sandbox_helper_win.h"
  17. #include "sandbox/win/src/sandbox_types.h"
  18. #elif defined(OS_LINUX) // defined(OS_WIN)
  19. #include "atom/app/atom_main_delegate.h" // NOLINT
  20. #include "content/public/app/content_main.h"
  21. #else // defined(OS_LINUX)
  22. #include "atom/app/atom_library_main.h"
  23. #endif // defined(OS_MACOSX)
  24. #include "atom/app/node_main.h"
  25. #include "atom/common/atom_command_line.h"
  26. #include "base/at_exit.h"
  27. #include "base/i18n/icu_util.h"
  28. namespace {
  29. const char* kRunAsNode = "ELECTRON_RUN_AS_NODE";
  30. bool IsEnvSet(const char* name) {
  31. #if defined(OS_WIN)
  32. size_t required_size;
  33. getenv_s(&required_size, nullptr, 0, name);
  34. return required_size != 0;
  35. #else
  36. char* indicator = getenv(name);
  37. return indicator && indicator[0] != '\0';
  38. #endif
  39. }
  40. } // namespace
  41. #if defined(OS_WIN)
  42. int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE, wchar_t* cmd, int) {
  43. int argc = 0;
  44. wchar_t** wargv = ::CommandLineToArgvW(::GetCommandLineW(), &argc);
  45. bool run_as_node = IsEnvSet(kRunAsNode);
  46. // Make sure the output is printed to console.
  47. if (run_as_node || !IsEnvSet("ELECTRON_NO_ATTACH_CONSOLE"))
  48. base::RouteStdioToConsole(false);
  49. // Convert argv to to UTF8
  50. char** argv = new char*[argc];
  51. for (int i = 0; i < argc; i++) {
  52. // Compute the size of the required buffer
  53. DWORD size = WideCharToMultiByte(CP_UTF8,
  54. 0,
  55. wargv[i],
  56. -1,
  57. NULL,
  58. 0,
  59. NULL,
  60. NULL);
  61. if (size == 0) {
  62. // This should never happen.
  63. fprintf(stderr, "Could not convert arguments to utf8.");
  64. exit(1);
  65. }
  66. // Do the actual conversion
  67. argv[i] = new char[size];
  68. DWORD result = WideCharToMultiByte(CP_UTF8,
  69. 0,
  70. wargv[i],
  71. -1,
  72. argv[i],
  73. size,
  74. NULL,
  75. NULL);
  76. if (result == 0) {
  77. // This should never happen.
  78. fprintf(stderr, "Could not convert arguments to utf8.");
  79. exit(1);
  80. }
  81. }
  82. if (run_as_node) {
  83. // Now that argv conversion is done, we can finally start.
  84. base::AtExitManager atexit_manager;
  85. base::i18n::InitializeICU();
  86. return atom::NodeMain(argc, argv);
  87. } else if (IsEnvSet("ELECTRON_INTERNAL_CRASH_SERVICE")) {
  88. return crash_service::Main(cmd);
  89. }
  90. sandbox::SandboxInterfaceInfo sandbox_info = {0};
  91. content::InitializeSandboxInfo(&sandbox_info);
  92. atom::AtomMainDelegate delegate;
  93. content::ContentMainParams params(&delegate);
  94. params.instance = instance;
  95. params.sandbox_info = &sandbox_info;
  96. atom::AtomCommandLine::Init(argc, argv);
  97. atom::AtomCommandLine::InitW(argc, wargv);
  98. return content::ContentMain(params);
  99. }
  100. #elif defined(OS_LINUX) // defined(OS_WIN)
  101. int main(int argc, const char* argv[]) {
  102. if (IsEnvSet(kRunAsNode)) {
  103. base::i18n::InitializeICU();
  104. base::AtExitManager atexit_manager;
  105. return atom::NodeMain(argc, const_cast<char**>(argv));
  106. }
  107. atom::AtomMainDelegate delegate;
  108. content::ContentMainParams params(&delegate);
  109. params.argc = argc;
  110. params.argv = argv;
  111. atom::AtomCommandLine::Init(argc, argv);
  112. return content::ContentMain(params);
  113. }
  114. #else // defined(OS_LINUX)
  115. int main(int argc, const char* argv[]) {
  116. if (IsEnvSet(kRunAsNode)) {
  117. return AtomInitializeICUandStartNode(argc, const_cast<char**>(argv));
  118. }
  119. return AtomMain(argc, argv);
  120. }
  121. #endif // defined(OS_MACOSX)