1diff --git content/app/content_main.cc content/app/content_main.cc 2index bf4cbe990cb4d..e494fff1fa5d5 100644 3--- content/app/content_main.cc 4+++ content/app/content_main.cc 5@@ -225,16 +225,10 @@ ContentMainParams::~ContentMainParams() = default; 6 ContentMainParams::ContentMainParams(ContentMainParams&&) = default; 7 ContentMainParams& ContentMainParams::operator=(ContentMainParams&&) = default; 8 9-// This function must be marked with NO_STACK_PROTECTOR or it may crash on 10-// return, see the --change-stack-guard-on-fork command line flag. 11-int NO_STACK_PROTECTOR 12-RunContentProcess(ContentMainParams params, 13- ContentMainRunner* content_main_runner) { 14+int ContentMainInitialize(ContentMainParams params, 15+ ContentMainRunner* content_main_runner) { 16 int exit_code = -1; 17 base::debug::GlobalActivityTracker* tracker = nullptr; 18-#if BUILDFLAG(IS_MAC) 19- std::unique_ptr<base::mac::ScopedNSAutoreleasePool> autorelease_pool; 20-#endif 21 22 // A flag to indicate whether Main() has been called before. On Android, we 23 // may re-run Main() without restarting the browser process. This flag 24@@ -318,12 +312,6 @@ RunContentProcess(ContentMainParams params, 25 #endif 26 27 #if BUILDFLAG(IS_MAC) 28- // We need this pool for all the objects created before we get to the event 29- // loop, but we don't want to leave them hanging around until the app quits. 30- // Each "main" needs to flush this pool right before it goes into its main 31- // event loop to get rid of the cruft. 32- autorelease_pool = std::make_unique<base::mac::ScopedNSAutoreleasePool>(); 33- params.autorelease_pool = autorelease_pool.get(); 34 InitializeMac(); 35 #endif 36 37@@ -396,8 +384,18 @@ RunContentProcess(ContentMainParams params, 38 39 if (IsSubprocess()) 40 CommonSubprocessInit(); 41- exit_code = content_main_runner->Run(); 42 43+ return exit_code; 44+} 45+ 46+// This function must be marked with NO_STACK_PROTECTOR or it may crash on 47+// return, see the --change-stack-guard-on-fork command line flag. 48+int NO_STACK_PROTECTOR 49+ContentMainRun(ContentMainRunner* content_main_runner) { 50+ int exit_code = content_main_runner->Run(); 51+ 52+ base::debug::GlobalActivityTracker* tracker = 53+ base::debug::GlobalActivityTracker::Get(); 54 if (tracker) { 55 if (exit_code == 0) { 56 tracker->SetProcessPhaseIfEnabled( 57@@ -409,14 +407,41 @@ RunContentProcess(ContentMainParams params, 58 } 59 } 60 61-#if BUILDFLAG(IS_MAC) 62- autorelease_pool.reset(); 63-#endif 64+ return exit_code; 65+} 66 67+void ContentMainShutdown(ContentMainRunner* content_main_runner) { 68 #if !BUILDFLAG(IS_ANDROID) 69 content_main_runner->Shutdown(); 70 #endif 71+} 72+ 73+// This function must be marked with NO_STACK_PROTECTOR or it may crash on 74+// return, see the --change-stack-guard-on-fork command line flag. 75+int NO_STACK_PROTECTOR 76+RunContentProcess(ContentMainParams params, 77+ ContentMainRunner* content_main_runner) { 78+#if BUILDFLAG(IS_MAC) 79+ // We need this pool for all the objects created before we get to the event 80+ // loop, but we don't want to leave them hanging around until the app quits. 81+ // Each "main" needs to flush this pool right before it goes into its main 82+ // event loop to get rid of the cruft. 83+ std::unique_ptr<base::mac::ScopedNSAutoreleasePool> autorelease_pool = 84+ std::make_unique<base::mac::ScopedNSAutoreleasePool>(); 85+ params.autorelease_pool = autorelease_pool.get(); 86+#endif 87+ 88+ int exit_code = ContentMainInitialize(std::move(params), content_main_runner); 89+ if (exit_code >= 0) 90+ return exit_code; 91+ exit_code = ContentMainRun(content_main_runner); 92+ 93+#if BUILDFLAG(IS_MAC) 94+ params.autorelease_pool = nullptr; 95+ autorelease_pool.reset(); 96+#endif 97 98+ ContentMainShutdown(content_main_runner); 99 return exit_code; 100 } 101 102diff --git content/app/content_main_runner_impl.cc content/app/content_main_runner_impl.cc 103index 5a365936031e6..ab4a3f2138e28 100644 104--- content/app/content_main_runner_impl.cc 105+++ content/app/content_main_runner_impl.cc 106@@ -44,6 +44,7 @@ 107 #include "base/task/thread_pool/thread_pool_instance.h" 108 #include "base/threading/hang_watcher.h" 109 #include "base/threading/platform_thread.h" 110+#include "base/threading/thread_restrictions.h" 111 #include "base/time/time.h" 112 #include "base/trace_event/trace_event.h" 113 #include "build/build_config.h" 114@@ -1182,6 +1183,11 @@ void ContentMainRunnerImpl::Shutdown() { 115 is_shutdown_ = true; 116 } 117 118+void ContentMainRunnerImpl::ShutdownOnUIThread() { 119+ base::ScopedAllowBaseSyncPrimitivesForTesting allow_wait; 120+ discardable_shared_memory_manager_.reset(); 121+} 122+ 123 // static 124 std::unique_ptr<ContentMainRunner> ContentMainRunner::Create() { 125 return ContentMainRunnerImpl::Create(); 126diff --git content/app/content_main_runner_impl.h content/app/content_main_runner_impl.h 127index 2cfcb5330af5d..e1966af803dfb 100644 128--- content/app/content_main_runner_impl.h 129+++ content/app/content_main_runner_impl.h 130@@ -31,7 +31,7 @@ namespace content { 131 class ContentClient; 132 class MojoIpcSupport; 133 134-class ContentMainRunnerImpl : public ContentMainRunner { 135+class CONTENT_EXPORT ContentMainRunnerImpl : public ContentMainRunner { 136 public: 137 static std::unique_ptr<ContentMainRunnerImpl> Create(); 138 139@@ -50,6 +50,8 @@ class ContentMainRunnerImpl : public ContentMainRunner { 140 int Run() override; 141 void Shutdown() override; 142 143+ void ShutdownOnUIThread(); 144+ 145 private: 146 int RunBrowser(MainFunctionParams main_function_params, 147 bool start_minimal_browser); 148diff --git content/common/set_process_title.cc content/common/set_process_title.cc 149index 06cdab47c8cdf..1f0ea6c7215ca 100644 150--- content/common/set_process_title.cc 151+++ content/common/set_process_title.cc 152@@ -54,7 +54,7 @@ void SetProcessTitleFromCommandLine(const char** main_argv) { 153 bool have_argv0 = false; 154 155 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) 156- DCHECK_EQ(base::PlatformThread::CurrentId(), getpid()); 157+ // DCHECK_EQ(base::PlatformThread::CurrentId(), getpid()); 158 159 if (main_argv) 160 setproctitle_init(main_argv); 161diff --git content/public/app/content_main.h content/public/app/content_main.h 162index 268b201ab060a..b745f44139a3d 100644 163--- content/public/app/content_main.h 164+++ content/public/app/content_main.h 165@@ -93,6 +93,13 @@ struct CONTENT_EXPORT ContentMainParams { 166 } 167 }; 168 169+// Split RunContentProcess() into separate stages. 170+CONTENT_EXPORT int ContentMainInitialize( 171+ ContentMainParams params, 172+ ContentMainRunner* content_main_runner); 173+CONTENT_EXPORT int ContentMainRun(ContentMainRunner* content_main_runner); 174+CONTENT_EXPORT void ContentMainShutdown(ContentMainRunner* content_main_runner); 175+ 176 CONTENT_EXPORT int RunContentProcess(ContentMainParams params, 177 ContentMainRunner* content_main_runner); 178 179