atom_main_delegate.cc 12 KB

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