atom_main_delegate.cc 10 KB

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