atom_main_delegate.cc 11 KB

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