atom_main_delegate.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 "ui/base/l10n/l10n_util.h"
  22. #include "ui/base/resource/resource_bundle.h"
  23. namespace atom {
  24. namespace {
  25. const char* kRelauncherProcess = "relauncher";
  26. bool IsBrowserProcess(base::CommandLine* cmd) {
  27. std::string process_type = cmd->GetSwitchValueASCII(::switches::kProcessType);
  28. return process_type.empty();
  29. }
  30. #if defined(OS_WIN)
  31. void InvalidParameterHandler(const wchar_t*, const wchar_t*, const wchar_t*,
  32. unsigned int, uintptr_t) {
  33. // noop.
  34. }
  35. #endif
  36. } // namespace
  37. AtomMainDelegate::AtomMainDelegate() {
  38. }
  39. AtomMainDelegate::~AtomMainDelegate() {
  40. }
  41. bool AtomMainDelegate::BasicStartupComplete(int* exit_code) {
  42. auto command_line = base::CommandLine::ForCurrentProcess();
  43. logging::LoggingSettings settings;
  44. #if defined(OS_WIN)
  45. // On Windows the terminal returns immediately, so we add a new line to
  46. // prevent output in the same line as the prompt.
  47. if (IsBrowserProcess(command_line))
  48. std::wcout << std::endl;
  49. #if defined(DEBUG)
  50. // Print logging to debug.log on Windows
  51. settings.logging_dest = logging::LOG_TO_ALL;
  52. settings.log_file = L"debug.log";
  53. settings.lock_log = logging::LOCK_LOG_FILE;
  54. settings.delete_old = logging::DELETE_OLD_LOG_FILE;
  55. #else
  56. settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
  57. #endif // defined(DEBUG)
  58. #else // defined(OS_WIN)
  59. settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
  60. #endif // !defined(OS_WIN)
  61. // Only enable logging when --enable-logging is specified.
  62. std::unique_ptr<base::Environment> env(base::Environment::Create());
  63. if (!command_line->HasSwitch(::switches::kEnableLogging) &&
  64. !env->HasVar("ELECTRON_ENABLE_LOGGING")) {
  65. settings.logging_dest = logging::LOG_NONE;
  66. logging::SetMinLogLevel(logging::LOG_NUM_SEVERITIES);
  67. }
  68. logging::InitLogging(settings);
  69. // Logging with pid and timestamp.
  70. logging::SetLogItems(true, false, true, false);
  71. // Enable convient stack printing.
  72. bool enable_stack_dumping = env->HasVar("ELECTRON_ENABLE_STACK_DUMPING");
  73. #if defined(DEBUG) && defined(OS_LINUX)
  74. enable_stack_dumping = true;
  75. #endif
  76. if (enable_stack_dumping)
  77. base::debug::EnableInProcessStackDumping();
  78. chrome::RegisterPathProvider();
  79. #if defined(OS_MACOSX)
  80. SetUpBundleOverrides();
  81. #endif
  82. #if defined(OS_WIN)
  83. // Ignore invalid parameter errors.
  84. _set_invalid_parameter_handler(InvalidParameterHandler);
  85. #endif
  86. return brightray::MainDelegate::BasicStartupComplete(exit_code);
  87. }
  88. void AtomMainDelegate::PreSandboxStartup() {
  89. brightray::MainDelegate::PreSandboxStartup();
  90. // Set google API key.
  91. std::unique_ptr<base::Environment> env(base::Environment::Create());
  92. if (!env->HasVar("GOOGLE_API_KEY"))
  93. env->SetVar("GOOGLE_API_KEY", GOOGLEAPIS_API_KEY);
  94. auto command_line = base::CommandLine::ForCurrentProcess();
  95. std::string process_type = command_line->GetSwitchValueASCII(
  96. ::switches::kProcessType);
  97. // Only append arguments for browser process.
  98. if (!IsBrowserProcess(command_line))
  99. return;
  100. if (command_line->HasSwitch(switches::kEnableSandbox)) {
  101. // Disable setuid sandbox since it is not longer required on linux(namespace
  102. // sandbox is available on most distros).
  103. command_line->AppendSwitch(::switches::kDisableSetuidSandbox);
  104. } else {
  105. // Disable renderer sandbox for most of node's functions.
  106. command_line->AppendSwitch(::switches::kNoSandbox);
  107. }
  108. // Allow file:// URIs to read other file:// URIs by default.
  109. command_line->AppendSwitch(::switches::kAllowFileAccessFromFiles);
  110. #if defined(OS_MACOSX)
  111. // Enable AVFoundation.
  112. command_line->AppendSwitch("enable-avfoundation");
  113. #endif
  114. }
  115. content::ContentBrowserClient* AtomMainDelegate::CreateContentBrowserClient() {
  116. browser_client_.reset(new AtomBrowserClient);
  117. return browser_client_.get();
  118. }
  119. content::ContentRendererClient*
  120. AtomMainDelegate::CreateContentRendererClient() {
  121. if (base::CommandLine::ForCurrentProcess()->HasSwitch(
  122. switches::kEnableSandbox)) {
  123. renderer_client_.reset(new AtomSandboxedRendererClient);
  124. } else {
  125. renderer_client_.reset(new AtomRendererClient);
  126. }
  127. return renderer_client_.get();
  128. }
  129. content::ContentUtilityClient* AtomMainDelegate::CreateContentUtilityClient() {
  130. utility_client_.reset(new AtomContentUtilityClient);
  131. return utility_client_.get();
  132. }
  133. int AtomMainDelegate::RunProcess(
  134. const std::string& process_type,
  135. const content::MainFunctionParams& main_function_params) {
  136. if (process_type == kRelauncherProcess)
  137. return relauncher::RelauncherMain(main_function_params);
  138. else
  139. return -1;
  140. }
  141. #if defined(OS_MACOSX)
  142. bool AtomMainDelegate::ShouldSendMachPort(const std::string& process_type) {
  143. return process_type != kRelauncherProcess;
  144. }
  145. bool AtomMainDelegate::DelaySandboxInitialization(
  146. const std::string& process_type) {
  147. return process_type == kRelauncherProcess;
  148. }
  149. #endif
  150. std::unique_ptr<brightray::ContentClient>
  151. AtomMainDelegate::CreateContentClient() {
  152. return std::unique_ptr<brightray::ContentClient>(new AtomContentClient);
  153. }
  154. } // namespace atom