atom_main_delegate.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. #if defined(OS_LINUX)
  9. #include <glib.h> // for g_setenv()
  10. #endif
  11. #include "atom/app/atom_content_client.h"
  12. #include "atom/browser/atom_browser_client.h"
  13. #include "atom/browser/relauncher.h"
  14. #include "atom/common/options_switches.h"
  15. #include "atom/renderer/atom_renderer_client.h"
  16. #include "atom/renderer/atom_sandboxed_renderer_client.h"
  17. #include "atom/utility/atom_content_utility_client.h"
  18. #include "base/command_line.h"
  19. #include "base/debug/stack_trace.h"
  20. #include "base/environment.h"
  21. #include "base/logging.h"
  22. #include "base/mac/bundle_locations.h"
  23. #include "base/path_service.h"
  24. #include "chrome/common/chrome_paths.h"
  25. #include "content/public/common/content_switches.h"
  26. #include "electron/buildflags/buildflags.h"
  27. #include "ipc/ipc_buildflags.h"
  28. #include "services/service_manager/embedder/switches.h"
  29. #include "services/service_manager/sandbox/switches.h"
  30. #include "ui/base/l10n/l10n_util.h"
  31. #include "ui/base/resource/resource_bundle.h"
  32. #include "ui/base/ui_base_switches.h"
  33. #if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED)
  34. #define IPC_MESSAGE_MACROS_LOG_ENABLED
  35. #include "content/public/common/content_ipc_logging.h"
  36. #define IPC_LOG_TABLE_ADD_ENTRY(msg_id, logger) \
  37. content::RegisterIPCLogger(msg_id, logger)
  38. #include "atom/common/common_message_generator.h"
  39. #endif
  40. #if defined(OS_MACOSX)
  41. #include "atom/app/atom_main_delegate_mac.h"
  42. #endif
  43. #if defined(OS_WIN)
  44. #include "base/win/win_util.h"
  45. #if defined(_WIN64)
  46. #include "atom/common/crash_reporter/crash_reporter_win.h"
  47. #endif
  48. #endif
  49. namespace atom {
  50. namespace {
  51. const char* kRelauncherProcess = "relauncher";
  52. bool IsBrowserProcess(base::CommandLine* cmd) {
  53. std::string process_type = cmd->GetSwitchValueASCII(::switches::kProcessType);
  54. return process_type.empty();
  55. }
  56. bool IsSandboxEnabled(base::CommandLine* command_line) {
  57. return command_line->HasSwitch(switches::kEnableSandbox) ||
  58. !command_line->HasSwitch(service_manager::switches::kNoSandbox);
  59. }
  60. // Returns true if this subprocess type needs the ResourceBundle initialized
  61. // and resources loaded.
  62. bool SubprocessNeedsResourceBundle(const std::string& process_type) {
  63. return
  64. #if defined(OS_POSIX) && !defined(OS_MACOSX)
  65. // The zygote process opens the resources for the renderers.
  66. process_type == service_manager::switches::kZygoteProcess ||
  67. #endif
  68. #if defined(OS_MACOSX)
  69. // Mac needs them too for scrollbar related images and for sandbox
  70. // profiles.
  71. process_type == ::switches::kPpapiPluginProcess ||
  72. process_type == ::switches::kPpapiBrokerProcess ||
  73. process_type == ::switches::kGpuProcess ||
  74. #endif
  75. process_type == ::switches::kRendererProcess ||
  76. process_type == ::switches::kUtilityProcess;
  77. }
  78. #if defined(OS_WIN)
  79. void InvalidParameterHandler(const wchar_t*,
  80. const wchar_t*,
  81. const wchar_t*,
  82. unsigned int,
  83. uintptr_t) {
  84. // noop.
  85. }
  86. #endif
  87. } // namespace
  88. void LoadResourceBundle(const std::string& locale) {
  89. const bool initialized = ui::ResourceBundle::HasSharedInstance();
  90. if (initialized)
  91. ui::ResourceBundle::CleanupSharedInstance();
  92. // Load other resource files.
  93. base::FilePath pak_dir;
  94. #if defined(OS_MACOSX)
  95. pak_dir =
  96. base::mac::FrameworkBundlePath().Append(FILE_PATH_LITERAL("Resources"));
  97. #else
  98. base::PathService::Get(base::DIR_MODULE, &pak_dir);
  99. #endif
  100. ui::ResourceBundle::InitSharedInstanceWithLocale(
  101. locale, nullptr, ui::ResourceBundle::LOAD_COMMON_RESOURCES);
  102. ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
  103. bundle.ReloadLocaleResources(locale);
  104. bundle.AddDataPackFromPath(pak_dir.Append(FILE_PATH_LITERAL("resources.pak")),
  105. ui::SCALE_FACTOR_NONE);
  106. #if BUILDFLAG(ENABLE_PDF_VIEWER)
  107. NOTIMPLEMENTED()
  108. << "Hi, whoever's fixing PDF support! Thanks! The pdf "
  109. "viewer resources haven't been ported over to the GN build yet, so "
  110. "you'll probably need to change this bit of code.";
  111. bundle.AddDataPackFromPath(
  112. pak_dir.Append(FILE_PATH_LITERAL("pdf_viewer_resources.pak")),
  113. ui::GetSupportedScaleFactors()[0]);
  114. #endif // BUILDFLAG(ENABLE_PDF_VIEWER)
  115. }
  116. AtomMainDelegate::AtomMainDelegate() {}
  117. AtomMainDelegate::~AtomMainDelegate() {}
  118. bool AtomMainDelegate::BasicStartupComplete(int* exit_code) {
  119. auto* command_line = base::CommandLine::ForCurrentProcess();
  120. logging::LoggingSettings settings;
  121. #if defined(OS_WIN)
  122. #if defined(_WIN64)
  123. crash_reporter::CrashReporterWin::SetUnhandledExceptionFilter();
  124. #endif
  125. // On Windows the terminal returns immediately, so we add a new line to
  126. // prevent output in the same line as the prompt.
  127. if (IsBrowserProcess(command_line))
  128. std::wcout << std::endl;
  129. #if defined(DEBUG)
  130. // Print logging to debug.log on Windows
  131. settings.logging_dest = logging::LOG_TO_ALL;
  132. settings.log_file = L"debug.log";
  133. settings.lock_log = logging::LOCK_LOG_FILE;
  134. settings.delete_old = logging::DELETE_OLD_LOG_FILE;
  135. #else
  136. settings.logging_dest =
  137. logging::LOG_TO_SYSTEM_DEBUG_LOG | logging::LOG_TO_STDERR;
  138. #endif // defined(DEBUG)
  139. #else // defined(OS_WIN)
  140. settings.logging_dest =
  141. logging::LOG_TO_SYSTEM_DEBUG_LOG | logging::LOG_TO_STDERR;
  142. #endif // !defined(OS_WIN)
  143. // Only enable logging when --enable-logging is specified.
  144. auto env = base::Environment::Create();
  145. if (!command_line->HasSwitch(::switches::kEnableLogging) &&
  146. !env->HasVar("ELECTRON_ENABLE_LOGGING")) {
  147. settings.logging_dest = logging::LOG_NONE;
  148. logging::SetMinLogLevel(logging::LOG_NUM_SEVERITIES);
  149. }
  150. logging::InitLogging(settings);
  151. // Logging with pid and timestamp.
  152. logging::SetLogItems(true, false, true, false);
  153. // Enable convient stack printing. This is enabled by default in non-official
  154. // builds.
  155. if (env->HasVar("ELECTRON_ENABLE_STACK_DUMPING"))
  156. base::debug::EnableInProcessStackDumping();
  157. if (env->HasVar("ELECTRON_DISABLE_SANDBOX"))
  158. command_line->AppendSwitch(service_manager::switches::kNoSandbox);
  159. chrome::RegisterPathProvider();
  160. #if defined(OS_MACOSX)
  161. OverrideChildProcessPath();
  162. OverrideFrameworkBundlePath();
  163. SetUpBundleOverrides();
  164. #endif
  165. #if defined(OS_WIN)
  166. // Ignore invalid parameter errors.
  167. _set_invalid_parameter_handler(InvalidParameterHandler);
  168. // Disable the ActiveVerifier, which is used by Chrome to track possible
  169. // bugs, but no use in Electron.
  170. base::win::DisableHandleVerifier();
  171. if (IsBrowserProcess(command_line))
  172. base::win::PinUser32();
  173. #endif
  174. #if defined(OS_LINUX)
  175. // Check for --no-sandbox parameter when running as root.
  176. if (getuid() == 0 && IsSandboxEnabled(command_line))
  177. LOG(FATAL) << "Running as root without --"
  178. << service_manager::switches::kNoSandbox
  179. << " is not supported. See https://crbug.com/638180.";
  180. #endif
  181. content_client_ = std::make_unique<AtomContentClient>();
  182. SetContentClient(content_client_.get());
  183. return false;
  184. }
  185. void AtomMainDelegate::PostEarlyInitialization(bool is_running_tests) {
  186. std::string custom_locale;
  187. ui::ResourceBundle::InitSharedInstanceWithLocale(
  188. custom_locale, nullptr, ui::ResourceBundle::LOAD_COMMON_RESOURCES);
  189. auto* cmd_line = base::CommandLine::ForCurrentProcess();
  190. if (cmd_line->HasSwitch(::switches::kLang)) {
  191. const std::string locale = cmd_line->GetSwitchValueASCII(::switches::kLang);
  192. const base::FilePath locale_file_path =
  193. ui::ResourceBundle::GetSharedInstance().GetLocaleFilePath(locale, true);
  194. if (!locale_file_path.empty()) {
  195. custom_locale = locale;
  196. #if defined(OS_LINUX)
  197. /* When built with USE_GLIB, libcc's GetApplicationLocaleInternal() uses
  198. * glib's g_get_language_names(), which keys off of getenv("LC_ALL") */
  199. g_setenv("LC_ALL", custom_locale.c_str(), TRUE);
  200. #endif
  201. }
  202. }
  203. #if defined(OS_MACOSX)
  204. if (custom_locale.empty())
  205. l10n_util::OverrideLocaleWithCocoaLocale();
  206. #endif
  207. LoadResourceBundle(custom_locale);
  208. AtomBrowserClient::SetApplicationLocale(
  209. l10n_util::GetApplicationLocale(custom_locale));
  210. }
  211. void AtomMainDelegate::PreSandboxStartup() {
  212. auto* command_line = base::CommandLine::ForCurrentProcess();
  213. std::string process_type =
  214. command_line->GetSwitchValueASCII(::switches::kProcessType);
  215. // Initialize ResourceBundle which handles files loaded from external
  216. // sources. The language should have been passed in to us from the
  217. // browser process as a command line flag.
  218. if (SubprocessNeedsResourceBundle(process_type)) {
  219. std::string locale = command_line->GetSwitchValueASCII(::switches::kLang);
  220. LoadResourceBundle(locale);
  221. }
  222. // Only append arguments for browser process.
  223. if (!IsBrowserProcess(command_line))
  224. return;
  225. // Allow file:// URIs to read other file:// URIs by default.
  226. command_line->AppendSwitch(::switches::kAllowFileAccessFromFiles);
  227. #if defined(OS_MACOSX)
  228. // Enable AVFoundation.
  229. command_line->AppendSwitch("enable-avfoundation");
  230. #endif
  231. }
  232. void AtomMainDelegate::PreCreateMainMessageLoop() {
  233. #if defined(OS_MACOSX)
  234. RegisterAtomCrApp();
  235. #endif
  236. }
  237. content::ContentBrowserClient* AtomMainDelegate::CreateContentBrowserClient() {
  238. browser_client_.reset(new AtomBrowserClient);
  239. return browser_client_.get();
  240. }
  241. content::ContentRendererClient*
  242. AtomMainDelegate::CreateContentRendererClient() {
  243. auto* command_line = base::CommandLine::ForCurrentProcess();
  244. if (IsSandboxEnabled(command_line)) {
  245. renderer_client_.reset(new AtomSandboxedRendererClient);
  246. } else {
  247. renderer_client_.reset(new AtomRendererClient);
  248. }
  249. return renderer_client_.get();
  250. }
  251. content::ContentUtilityClient* AtomMainDelegate::CreateContentUtilityClient() {
  252. utility_client_.reset(new AtomContentUtilityClient);
  253. return utility_client_.get();
  254. }
  255. int AtomMainDelegate::RunProcess(
  256. const std::string& process_type,
  257. const content::MainFunctionParams& main_function_params) {
  258. if (process_type == kRelauncherProcess)
  259. return relauncher::RelauncherMain(main_function_params);
  260. else
  261. return -1;
  262. }
  263. #if defined(OS_MACOSX)
  264. bool AtomMainDelegate::ShouldSendMachPort(const std::string& process_type) {
  265. return process_type != kRelauncherProcess;
  266. }
  267. bool AtomMainDelegate::DelaySandboxInitialization(
  268. const std::string& process_type) {
  269. return process_type == kRelauncherProcess;
  270. }
  271. #endif
  272. bool AtomMainDelegate::ShouldLockSchemeRegistry() {
  273. return false;
  274. }
  275. bool AtomMainDelegate::ShouldCreateFeatureList() {
  276. return false;
  277. }
  278. } // namespace atom