#include <stdint.h>
#include <iostream>
#include <memory>
#include "base/command_line.h"
#include "base/environment.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/sampling_heap_profiler/poisson_allocation_sampler.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/app/chrome_main_delegate.h"
#include "chrome/app/startup_timestamps.h"
#include "chrome/browser/headless/headless_mode_util.h"
#include "chrome/common/buildflags.h"
#include "chrome/common/chrome_result_codes.h"
#include "chrome/common/chrome_switches.h"
#include "content/public/app/content_main.h"
#include "content/public/common/content_switches.h"
#include "partition_alloc/buildflags.h"
#if BUILDFLAG(IS_MAC)
#include "chrome/app/chrome_main_mac.h"
#endif
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX)
#include "base/base_switches.h"
#endif
#if BUILDFLAG(IS_LINUX)
#include "chrome/app/chrome_main_linux.h"
#endif
#if BUILDFLAG(IS_WIN)
#include <timeapi.h>
#include "base/dcheck_is_on.h"
#include "base/debug/dump_without_crashing.h"
#include "base/debug/handle_hooks_win.h"
#include "base/win/current_module.h"
#include "base/win/win_util.h"
#include "chrome/chrome_elf/chrome_elf_main.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/install_static/initialize_from_primary_module.h"
#include "chrome/install_static/install_details.h"
#define DLLEXPORT __declspec(dllexport)
#endif
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_MAC) || \
BUILDFLAG(IS_WIN)
#define ENABLE_OLD_HEADLESS_INFO
#endif
#ifdef ENABLE_OLD_HEADLESS_INFO
namespace {
void ShowOldHeadlessInfoMaybe(const base::CommandLine* command_line) {
if (!command_line->GetSwitchValueASCII(::switches::kProcessType).empty()) {
return;
}
std::cerr
<< "Old Headless mode has been removed from the Chrome binary. "
"Please use the new Headless mode "
"(https://developer.chrome.com/docs/chromium/new-headless) or the "
"chrome-headless-shell which is a standalone implementation of "
"the old Headless mode "
"(https://developer.chrome.com/blog/chrome-headless-shell)."
<< std::endl
<< std::endl;
}
}
#endif
#if BUILDFLAG(IS_WIN)
extern "C" {
DLLEXPORT int __cdecl ChromeMain(HINSTANCE instance,
sandbox::SandboxInterfaceInfo* sandbox_info,
int64_t exe_main_entry_point_ticks,
int64_t preread_begin_ticks,
int64_t preread_end_ticks);
}
#elif BUILDFLAG(IS_POSIX)
extern "C" {
NO_STACK_PROTECTOR __attribute__((visibility("default"))) int ChromeMain(
int argc,
const char** argv);
}
#else
#error Unknown platform.
#endif
#if BUILDFLAG(IS_WIN)
DLLEXPORT int __cdecl ChromeMain(HINSTANCE instance,
sandbox::SandboxInterfaceInfo* sandbox_info,
int64_t exe_entry_point_ticks,
int64_t preread_begin_ticks,
int64_t preread_end_ticks) {
#elif BUILDFLAG(IS_POSIX)
int ChromeMain(int argc, const char** argv) {
#else
#error Unknown platform.
#endif
#if BUILDFLAG(IS_LINUX)
PossiblyDetermineFallbackChromeChannel(argv[0]);
#endif
#if BUILDFLAG(IS_WIN)
install_static::InitializeFromPrimaryModule();
#if !defined(COMPONENT_BUILD) && DCHECK_IS_ON()
base::debug::HandleHooks::AddIATPatch(CURRENT_MODULE());
#endif
StartupTimestamps timestamps{
base::TimeTicks::FromInternalValue(exe_entry_point_ticks),
base::TimeTicks::FromInternalValue(preread_begin_ticks),
base::TimeTicks::FromInternalValue(preread_end_ticks)};
ChromeMainDelegate chrome_main_delegate(timestamps);
#else
ChromeMainDelegate chrome_main_delegate(
{.exe_entry_point_ticks = base::TimeTicks::Now()});
#endif
content::ContentMainParams params(&chrome_main_delegate);
#if BUILDFLAG(IS_WIN)
auto crash_on_detach_resetter = base::ScopedClosureRunner(
base::BindOnce(&base::win::SetShouldCrashOnProcessDetach,
base::win::ShouldCrashOnProcessDetach()));
base::win::SetShouldCrashOnProcessDetach(true);
base::win::SetAbortBehaviorForCrashReporting();
params.instance = instance;
params.sandbox_info = sandbox_info;
base::debug::SetDumpWithoutCrashingFunction(&DumpProcessWithoutCrash);
if (install_static::InstallDetails::Get().VersionMismatch())
base::debug::DumpWithoutCrashing();
#else
params.argc = argc;
params.argv = argv;
base::CommandLine::Init(params.argc, params.argv);
#endif
base::CommandLine::Init(0, nullptr);
[[maybe_unused]] base::CommandLine* command_line(
base::CommandLine::ForCurrentProcess());
#if BUILDFLAG(IS_WIN)
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
::switches::kRaiseTimerFrequency)) {
timeBeginPeriod(1);
}
#endif
#if BUILDFLAG(IS_MAC)
SetUpBundleOverrides();
#endif
#if BUILDFLAG(IS_LINUX)
AppendExtraArgumentsToCommandLine(command_line);
#endif
base::PoissonAllocationSampler::Init();
std::unique_ptr<headless::HeadlessModeHandle> headless_mode_handle;
if (headless::IsHeadlessMode()) {
if (command_line->GetArgs().size() > 1) {
LOG(ERROR) << "Multiple targets are not supported in headless mode.";
return CHROME_RESULT_CODE_UNSUPPORTED_PARAM;
}
auto init_headless_mode = headless::InitHeadlessMode();
if (!init_headless_mode.has_value()) {
LOG(ERROR) << init_headless_mode.error();
return EXIT_FAILURE;
}
headless_mode_handle = std::move(init_headless_mode.value());
} else {
#ifdef ENABLE_OLD_HEADLESS_INFO
if (headless::IsOldHeadlessMode()) {
ShowOldHeadlessInfoMaybe(command_line);
return EXIT_FAILURE;
}
#endif
}
#if BUILDFLAG(IS_MAC)
if (IsHelperAppLaunchedBySystemOrThirdPartyApplication()) {
return 0;
}
#endif
int rv = content::ContentMain(std::move(params));
if (IsNormalResultCode(static_cast<ResultCode>(rv))) {
return content::RESULT_CODE_NORMAL_EXIT;
}
return rv;
}