diff --git base/process/set_process_title.cc base/process/set_process_title.cc index 79ba3ac1913f8..46bcb4366d2f8 100644 --- base/process/set_process_title.cc +++ base/process/set_process_title.cc @@ -54,7 +54,7 @@ void SetProcessTitleFromCommandLine(const char** main_argv) { bool have_argv0 = false; #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) - DCHECK_EQ(base::PlatformThread::CurrentId(), getpid()); + // DCHECK_EQ(base::PlatformThread::CurrentId(), getpid()); if (main_argv) setproctitle_init(main_argv); diff --git content/app/content_main.cc content/app/content_main.cc index ffbb5986ca997..9f77c4603bf55 100644 --- content/app/content_main.cc +++ content/app/content_main.cc @@ -175,11 +175,8 @@ ContentMainParams::~ContentMainParams() = default; ContentMainParams::ContentMainParams(ContentMainParams&&) = default; ContentMainParams& ContentMainParams::operator=(ContentMainParams&&) = default; -// This function must be marked with NO_STACK_PROTECTOR or it may crash on -// return, see the --change-stack-guard-on-fork command line flag. -int NO_STACK_PROTECTOR -RunContentProcess(ContentMainParams params, - ContentMainRunner* content_main_runner) { +int ContentMainInitialize(ContentMainParams params, + ContentMainRunner* content_main_runner) { base::FeatureList::FailOnFeatureAccessWithoutFeatureList(); #if BUILDFLAG(IS_CHROMEOS_LACROS) // Lacros is launched with inherited priority. Revert to normal priority @@ -187,9 +184,6 @@ RunContentProcess(ContentMainParams params, base::PlatformThread::SetCurrentThreadType(base::ThreadType::kDefault); #endif int exit_code = -1; -#if BUILDFLAG(IS_MAC) - base::apple::ScopedNSAutoreleasePool autorelease_pool; -#endif // A flag to indicate whether Main() has been called before. On Android, we // may re-run Main() without restarting the browser process. This flag @@ -275,21 +269,14 @@ RunContentProcess(ContentMainParams params, #endif #if BUILDFLAG(IS_MAC) - // We need this pool for all the objects created before we get to the event - // loop, but we don't want to leave them hanging around until the app quits. - // Each "main" needs to flush this pool right before it goes into its main - // event loop to get rid of the cruft. TODO(https://crbug.com/1424190): This - // is not safe. Each main loop should create and destroy its own pool; it - // should not be flushing the pool at the base of the autorelease pool - // stack. - params.autorelease_pool = &autorelease_pool; InitializeMac(); #endif + base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); + #if BUILDFLAG(IS_IOS) // TODO(crbug.com/1412835): We support multiprocess launch of the content // process, but for now networking and GPU are still in process. - base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); command_line->AppendSwitch(switches::kInProcessGPU); command_line->AppendSwitch(switches::kEnableViewport); command_line->AppendSwitch(switches::kUseMobileUserAgent); @@ -308,19 +295,20 @@ RunContentProcess(ContentMainParams params, #if BUILDFLAG(IS_WIN) // Route stdio to parent console (if any) or create one. - if (base::CommandLine::ForCurrentProcess()->HasSwitch( - switches::kEnableLogging)) { - base::RouteStdioToConsole(/*create_console_if_not_found*/ true); - } else if (base::CommandLine::ForCurrentProcess()->HasSwitch( - switches::kHeadless)) { + if (command_line->HasSwitch(switches::kEnableLogging)) { + // Don't route to console for "handle" type in child processes. + if (command_line->GetSwitchValueASCII(switches::kEnableLogging) != + "handle") { + base::RouteStdioToConsole(/*create_console_if_not_found*/ true); + } + } else if (command_line->HasSwitch(switches::kHeadless)) { // When running in headless mode we want stdio routed however if // console does not exist we should not create one. base::RouteStdioToConsole(/*create_console_if_not_found*/ false); } #endif - if (base::CommandLine::ForCurrentProcess()->HasSwitch( - ::switches::kTraceToConsole)) { + if (command_line->HasSwitch(::switches::kTraceToConsole)) { base::trace_event::TraceConfig trace_config = tracing::GetConfigForTraceToConsole(); base::trace_event::TraceLog::GetInstance()->SetEnabled( @@ -330,12 +318,46 @@ RunContentProcess(ContentMainParams params, if (IsSubprocess()) CommonSubprocessInit(); - exit_code = content_main_runner->Run(); + return exit_code; +} + +// This function must be marked with NO_STACK_PROTECTOR or it may crash on +// return, see the --change-stack-guard-on-fork command line flag. +int NO_STACK_PROTECTOR +ContentMainRun(ContentMainRunner* content_main_runner) { + return content_main_runner->Run(); +} + +void ContentMainShutdown(ContentMainRunner* content_main_runner) { #if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS) content_main_runner->Shutdown(); #endif +} + +// This function must be marked with NO_STACK_PROTECTOR or it may crash on +// return, see the --change-stack-guard-on-fork command line flag. +int NO_STACK_PROTECTOR +RunContentProcess(ContentMainParams params, + ContentMainRunner* content_main_runner) { +#if BUILDFLAG(IS_MAC) + // We need this pool for all the objects created before we get to the event + // loop, but we don't want to leave them hanging around until the app quits. + // Each "main" needs to flush this pool right before it goes into its main + // event loop to get rid of the cruft. TODO(https://crbug.com/1424190): This + // is not safe. Each main loop should create and destroy its own pool; it + // should not be flushing the pool at the base of the autorelease pool + // stack. + base::apple::ScopedNSAutoreleasePool autorelease_pool; + params.autorelease_pool = &autorelease_pool; +#endif + + int exit_code = ContentMainInitialize(std::move(params), content_main_runner); + if (exit_code >= 0) + return exit_code; + exit_code = ContentMainRun(content_main_runner); + ContentMainShutdown(content_main_runner); return exit_code; } diff --git content/app/content_main_runner_impl.cc content/app/content_main_runner_impl.cc index 824026e6e2bd6..7de0c987dfd3d 100644 --- content/app/content_main_runner_impl.cc +++ content/app/content_main_runner_impl.cc @@ -46,6 +46,7 @@ #include "base/task/thread_pool/thread_pool_instance.h" #include "base/threading/hang_watcher.h" #include "base/threading/platform_thread.h" +#include "base/threading/thread_restrictions.h" #include "base/time/time.h" #include "base/trace_event/trace_event.h" #include "build/build_config.h" @@ -1339,6 +1340,11 @@ void ContentMainRunnerImpl::Shutdown() { is_shutdown_ = true; } +void ContentMainRunnerImpl::ShutdownOnUIThread() { + base::ScopedAllowBaseSyncPrimitivesForTesting allow_wait; + discardable_shared_memory_manager_.reset(); +} + // static std::unique_ptr ContentMainRunner::Create() { return ContentMainRunnerImpl::Create(); diff --git content/app/content_main_runner_impl.h content/app/content_main_runner_impl.h index cbbc2f3ec12fa..f097b3cdded2f 100644 --- content/app/content_main_runner_impl.h +++ content/app/content_main_runner_impl.h @@ -27,7 +27,7 @@ class DiscardableSharedMemoryManager; namespace content { class MojoIpcSupport; -class ContentMainRunnerImpl : public ContentMainRunner { +class CONTENT_EXPORT ContentMainRunnerImpl : public ContentMainRunner { public: static std::unique_ptr Create(); @@ -46,6 +46,8 @@ class ContentMainRunnerImpl : public ContentMainRunner { int Run() override; void Shutdown() override; + void ShutdownOnUIThread(); + private: int RunBrowser(MainFunctionParams main_function_params, bool start_minimal_browser); diff --git content/public/app/content_main.h content/public/app/content_main.h index 7f9b515297357..89b52e34fa31a 100644 --- content/public/app/content_main.h +++ content/public/app/content_main.h @@ -94,6 +94,13 @@ struct CONTENT_EXPORT ContentMainParams { } }; +// Split RunContentProcess() into separate stages. +CONTENT_EXPORT int ContentMainInitialize( + ContentMainParams params, + ContentMainRunner* content_main_runner); +CONTENT_EXPORT int ContentMainRun(ContentMainRunner* content_main_runner); +CONTENT_EXPORT void ContentMainShutdown(ContentMainRunner* content_main_runner); + CONTENT_EXPORT int RunContentProcess(ContentMainParams params, ContentMainRunner* content_main_runner);