atom_main_delegate.cc 6.6 KB

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