chrome_main.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <stdint.h>
  5. #include "base/bind.h"
  6. #include "base/callback_helpers.h"
  7. #include "base/command_line.h"
  8. #include "base/time/time.h"
  9. #include "build/build_config.h"
  10. #include "chrome/app/chrome_main_delegate.h"
  11. #include "chrome/browser/headless/headless_mode_util.h"
  12. #include "chrome/common/buildflags.h"
  13. #include "chrome/common/chrome_result_codes.h"
  14. #include "chrome/common/chrome_switches.h"
  15. #include "chrome/common/profiler/main_thread_stack_sampling_profiler.h"
  16. #include "content/public/app/content_main.h"
  17. #include "content/public/common/content_switches.h"
  18. #include "headless/app/headless_shell_switches.h"
  19. #include "headless/public/headless_shell.h"
  20. #include "ui/gfx/switches.h"
  21. #if BUILDFLAG(IS_MAC)
  22. #include "chrome/app/chrome_main_mac.h"
  23. #include "chrome/app/notification_metrics.h"
  24. #endif
  25. #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX)
  26. #include "base/base_switches.h"
  27. #endif
  28. #if BUILDFLAG(IS_WIN)
  29. #include "base/allocator/buildflags.h"
  30. #include "base/dcheck_is_on.h"
  31. #include "base/debug/handle_hooks_win.h"
  32. #include "base/win/current_module.h"
  33. #if BUILDFLAG(USE_ALLOCATOR_SHIM)
  34. #include "base/allocator/allocator_shim.h"
  35. #endif
  36. #include <timeapi.h>
  37. #include "base/debug/dump_without_crashing.h"
  38. #include "base/win/win_util.h"
  39. #include "chrome/chrome_elf/chrome_elf_main.h"
  40. #include "chrome/common/chrome_constants.h"
  41. #include "chrome/install_static/initialize_from_primary_module.h"
  42. #include "chrome/install_static/install_details.h"
  43. #define DLLEXPORT __declspec(dllexport)
  44. // We use extern C for the prototype DLLEXPORT to avoid C++ name mangling.
  45. extern "C" {
  46. DLLEXPORT int __cdecl ChromeMain(HINSTANCE instance,
  47. sandbox::SandboxInterfaceInfo* sandbox_info,
  48. int64_t exe_entry_point_ticks);
  49. }
  50. #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  51. extern "C" {
  52. // This function must be marked with NO_STACK_PROTECTOR or it may crash on
  53. // return, see the --change-stack-guard-on-fork command line flag.
  54. __attribute__((visibility("default"))) int NO_STACK_PROTECTOR
  55. ChromeMain(int argc, const char** argv);
  56. }
  57. #else
  58. #error Unknown platform.
  59. #endif
  60. #if BUILDFLAG(IS_WIN)
  61. DLLEXPORT int __cdecl ChromeMain(HINSTANCE instance,
  62. sandbox::SandboxInterfaceInfo* sandbox_info,
  63. int64_t exe_entry_point_ticks) {
  64. #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  65. int ChromeMain(int argc, const char** argv) {
  66. int64_t exe_entry_point_ticks = 0;
  67. #else
  68. #error Unknown platform.
  69. #endif
  70. #if BUILDFLAG(IS_WIN)
  71. #if BUILDFLAG(USE_ALLOCATOR_SHIM) && BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
  72. // Call this early on in order to configure heap workarounds. This must be
  73. // called from chrome.dll. This may be a NOP on some platforms.
  74. base::allocator::ConfigurePartitionAlloc();
  75. #endif
  76. install_static::InitializeFromPrimaryModule();
  77. #if !defined(COMPONENT_BUILD) && DCHECK_IS_ON()
  78. // Patch the main EXE on non-component builds when DCHECKs are enabled.
  79. // This allows detection of third party code that might attempt to meddle with
  80. // Chrome's handles. This must be done when single-threaded to avoid other
  81. // threads attempting to make calls through the hooks while they are being
  82. // emplaced.
  83. // Note: The EXE is patched separately, in chrome/app/chrome_exe_main_win.cc.
  84. base::debug::HandleHooks::AddIATPatch(CURRENT_MODULE());
  85. #endif // !defined(COMPONENT_BUILD) && DCHECK_IS_ON()
  86. #endif // BUILDFLAG(IS_WIN)
  87. ChromeMainDelegate chrome_main_delegate(
  88. base::TimeTicks::FromInternalValue(exe_entry_point_ticks));
  89. content::ContentMainParams params(&chrome_main_delegate);
  90. #if BUILDFLAG(IS_WIN)
  91. // The process should crash when going through abnormal termination, but we
  92. // must be sure to reset this setting when ChromeMain returns normally.
  93. auto crash_on_detach_resetter = base::ScopedClosureRunner(
  94. base::BindOnce(&base::win::SetShouldCrashOnProcessDetach,
  95. base::win::ShouldCrashOnProcessDetach()));
  96. base::win::SetShouldCrashOnProcessDetach(true);
  97. base::win::SetAbortBehaviorForCrashReporting();
  98. params.instance = instance;
  99. params.sandbox_info = sandbox_info;
  100. // Pass chrome_elf's copy of DumpProcessWithoutCrash resolved via load-time
  101. // dynamic linking.
  102. base::debug::SetDumpWithoutCrashingFunction(&DumpProcessWithoutCrash);
  103. // Verify that chrome_elf and this module (chrome.dll and chrome_child.dll)
  104. // have the same version.
  105. if (install_static::InstallDetails::Get().VersionMismatch())
  106. base::debug::DumpWithoutCrashing();
  107. #else
  108. params.argc = argc;
  109. params.argv = argv;
  110. base::CommandLine::Init(params.argc, params.argv);
  111. #endif // BUILDFLAG(IS_WIN)
  112. base::CommandLine::Init(0, nullptr);
  113. [[maybe_unused]] base::CommandLine* command_line(
  114. base::CommandLine::ForCurrentProcess());
  115. #if BUILDFLAG(IS_WIN)
  116. if (base::CommandLine::ForCurrentProcess()->HasSwitch(
  117. ::switches::kRaiseTimerFrequency)) {
  118. // Raise the timer interrupt frequency and leave it raised.
  119. timeBeginPeriod(1);
  120. }
  121. #endif
  122. #if BUILDFLAG(IS_MAC)
  123. SetUpBundleOverrides();
  124. #endif
  125. // Start the sampling profiler as early as possible - namely, once the command
  126. // line data is available. Allocated as an object on the stack to ensure that
  127. // the destructor runs on shutdown, which is important to avoid the profiler
  128. // thread's destruction racing with main thread destruction.
  129. MainThreadStackSamplingProfiler scoped_sampling_profiler;
  130. // Chrome-specific process modes.
  131. if (headless::IsChromeNativeHeadless()) {
  132. headless::SetUpCommandLine(command_line);
  133. } else {
  134. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_MAC) || \
  135. BUILDFLAG(IS_WIN)
  136. if (command_line->HasSwitch(switches::kHeadless)) {
  137. #if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  138. command_line->AppendSwitch(::headless::switches::kEnableCrashReporter);
  139. #endif
  140. return headless::HeadlessShellMain(std::move(params));
  141. }
  142. #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_MAC) ||
  143. // BUILDFLAG(IS_WIN)
  144. }
  145. #if BUILDFLAG(IS_LINUX)
  146. // TODO(https://crbug.com/1176772): Remove when Chrome Linux is fully migrated
  147. // to Crashpad.
  148. base::CommandLine::ForCurrentProcess()->AppendSwitch(
  149. ::switches::kEnableCrashpad);
  150. #endif
  151. #if BUILDFLAG(IS_MAC)
  152. // Gracefully exit if the system tried to launch the macOS notification helper
  153. // app when a user clicked on a notification.
  154. if (IsAlertsHelperLaunchedViaNotificationAction()) {
  155. LogLaunchedViaNotificationAction(NotificationActionSource::kHelperApp);
  156. return 0;
  157. }
  158. #endif
  159. int rv = content::ContentMain(std::move(params));
  160. if (chrome::IsNormalResultCode(static_cast<chrome::ResultCode>(rv)))
  161. return content::RESULT_CODE_NORMAL_EXIT;
  162. return rv;
  163. }