atom_main_delegate.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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_delegate.h"
  5. #include <iostream>
  6. #include <string>
  7. #include "atom/app/atom_content_client.h"
  8. #include "atom/browser/atom_browser_client.h"
  9. #include "atom/browser/relauncher.h"
  10. #include "atom/common/google_api_key.h"
  11. #include "atom/common/options_switches.h"
  12. #include "atom/renderer/atom_renderer_client.h"
  13. #include "atom/renderer/atom_sandboxed_renderer_client.h"
  14. #include "atom/utility/atom_content_utility_client.h"
  15. #include "base/command_line.h"
  16. #include "base/debug/stack_trace.h"
  17. #include "base/environment.h"
  18. #include "base/logging.h"
  19. #include "chrome/common/chrome_paths.h"
  20. #include "content/public/common/content_switches.h"
  21. #include "ipc/ipc_features.h"
  22. #include "services/service_manager/sandbox/switches.h"
  23. #include "ui/base/l10n/l10n_util.h"
  24. #include "ui/base/resource/resource_bundle.h"
  25. #if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED)
  26. #define IPC_MESSAGE_MACROS_LOG_ENABLED
  27. #include "content/public/common/content_ipc_logging.h"
  28. #define IPC_LOG_TABLE_ADD_ENTRY(msg_id, logger) \
  29. content::RegisterIPCLogger(msg_id, logger)
  30. #include "atom/common/common_message_generator.h"
  31. #endif
  32. namespace atom {
  33. namespace {
  34. const char* kRelauncherProcess = "relauncher";
  35. bool IsBrowserProcess(base::CommandLine* cmd) {
  36. std::string process_type = cmd->GetSwitchValueASCII(::switches::kProcessType);
  37. return process_type.empty();
  38. }
  39. #if defined(OS_WIN)
  40. void InvalidParameterHandler(const wchar_t*,
  41. const wchar_t*,
  42. const wchar_t*,
  43. unsigned int,
  44. uintptr_t) {
  45. // noop.
  46. }
  47. #endif
  48. } // namespace
  49. AtomMainDelegate::AtomMainDelegate() {}
  50. AtomMainDelegate::~AtomMainDelegate() {}
  51. bool AtomMainDelegate::BasicStartupComplete(int* exit_code) {
  52. auto* command_line = base::CommandLine::ForCurrentProcess();
  53. logging::LoggingSettings settings;
  54. #if defined(OS_WIN)
  55. // On Windows the terminal returns immediately, so we add a new line to
  56. // prevent output in the same line as the prompt.
  57. if (IsBrowserProcess(command_line))
  58. std::wcout << std::endl;
  59. #if defined(DEBUG)
  60. // Print logging to debug.log on Windows
  61. settings.logging_dest = logging::LOG_TO_ALL;
  62. settings.log_file = L"debug.log";
  63. settings.lock_log = logging::LOCK_LOG_FILE;
  64. settings.delete_old = logging::DELETE_OLD_LOG_FILE;
  65. #else
  66. settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
  67. #endif // defined(DEBUG)
  68. #else // defined(OS_WIN)
  69. settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
  70. #endif // !defined(OS_WIN)
  71. // Only enable logging when --enable-logging is specified.
  72. auto env = base::Environment::Create();
  73. if (!command_line->HasSwitch(::switches::kEnableLogging) &&
  74. !env->HasVar("ELECTRON_ENABLE_LOGGING")) {
  75. settings.logging_dest = logging::LOG_NONE;
  76. logging::SetMinLogLevel(logging::LOG_NUM_SEVERITIES);
  77. }
  78. logging::InitLogging(settings);
  79. // Logging with pid and timestamp.
  80. logging::SetLogItems(true, false, true, false);
  81. // Enable convient stack printing.
  82. #if defined(DEBUG) && defined(OS_LINUX)
  83. bool enable_stack_dumping = true;
  84. #else
  85. bool enable_stack_dumping = env->HasVar("ELECTRON_ENABLE_STACK_DUMPING");
  86. #endif
  87. #if defined(ARCH_CPU_ARM_FAMILY) && defined(ARCH_CPU_32_BITS)
  88. // For 32bit ARM enabling stack printing would end up crashing.
  89. // https://github.com/electron/electron/pull/11230#issuecomment-363232482
  90. enable_stack_dumping = false;
  91. #endif
  92. if (enable_stack_dumping)
  93. base::debug::EnableInProcessStackDumping();
  94. chrome::RegisterPathProvider();
  95. #if defined(OS_MACOSX)
  96. SetUpBundleOverrides();
  97. #endif
  98. #if defined(OS_WIN)
  99. // Ignore invalid parameter errors.
  100. _set_invalid_parameter_handler(InvalidParameterHandler);
  101. // Disable the ActiveVerifier, which is used by Chrome to track possible
  102. // bugs, but no use in Electron.
  103. base::win::DisableHandleVerifier();
  104. #endif
  105. return brightray::MainDelegate::BasicStartupComplete(exit_code);
  106. }
  107. void AtomMainDelegate::PreSandboxStartup() {
  108. brightray::MainDelegate::PreSandboxStartup();
  109. auto* command_line = base::CommandLine::ForCurrentProcess();
  110. std::string process_type =
  111. command_line->GetSwitchValueASCII(::switches::kProcessType);
  112. // Only append arguments for browser process.
  113. if (!IsBrowserProcess(command_line))
  114. return;
  115. if (!command_line->HasSwitch(switches::kEnableMixedSandbox)) {
  116. if (command_line->HasSwitch(switches::kEnableSandbox)) {
  117. // Disable setuid sandbox since it is not longer required on
  118. // linux(namespace sandbox is available on most distros).
  119. command_line->AppendSwitch(
  120. service_manager::switches::kDisableSetuidSandbox);
  121. } else {
  122. // Disable renderer sandbox for most of node's functions.
  123. command_line->AppendSwitch(::switches::kNoSandbox);
  124. }
  125. }
  126. // Allow file:// URIs to read other file:// URIs by default.
  127. command_line->AppendSwitch(::switches::kAllowFileAccessFromFiles);
  128. #if defined(OS_MACOSX)
  129. // Enable AVFoundation.
  130. command_line->AppendSwitch("enable-avfoundation");
  131. #endif
  132. }
  133. content::ContentBrowserClient* AtomMainDelegate::CreateContentBrowserClient() {
  134. browser_client_.reset(new AtomBrowserClient);
  135. return browser_client_.get();
  136. }
  137. content::ContentRendererClient*
  138. AtomMainDelegate::CreateContentRendererClient() {
  139. if (base::CommandLine::ForCurrentProcess()->HasSwitch(
  140. switches::kEnableSandbox) ||
  141. !base::CommandLine::ForCurrentProcess()->HasSwitch(
  142. ::switches::kNoSandbox)) {
  143. renderer_client_.reset(new AtomSandboxedRendererClient);
  144. } else {
  145. renderer_client_.reset(new AtomRendererClient);
  146. }
  147. return renderer_client_.get();
  148. }
  149. content::ContentUtilityClient* AtomMainDelegate::CreateContentUtilityClient() {
  150. utility_client_.reset(new AtomContentUtilityClient);
  151. return utility_client_.get();
  152. }
  153. int AtomMainDelegate::RunProcess(
  154. const std::string& process_type,
  155. const content::MainFunctionParams& main_function_params) {
  156. if (process_type == kRelauncherProcess)
  157. return relauncher::RelauncherMain(main_function_params);
  158. else
  159. return -1;
  160. }
  161. #if defined(OS_MACOSX)
  162. bool AtomMainDelegate::ShouldSendMachPort(const std::string& process_type) {
  163. return process_type != kRelauncherProcess;
  164. }
  165. bool AtomMainDelegate::DelaySandboxInitialization(
  166. const std::string& process_type) {
  167. return process_type == kRelauncherProcess;
  168. }
  169. #endif
  170. std::unique_ptr<brightray::ContentClient>
  171. AtomMainDelegate::CreateContentClient() {
  172. return std::make_unique<AtomContentClient>();
  173. }
  174. } // namespace atom