diff --git services/service_manager/embedder/main.cc services/service_manager/embedder/main.cc index 53e9e0aafc49..20b13ebc17b5 100644 --- services/service_manager/embedder/main.cc +++ services/service_manager/embedder/main.cc @@ -241,22 +241,36 @@ int RunService(MainDelegate* delegate) { return 0; } +ProcessType GetProcessType(MainDelegate* delegate, + const base::CommandLine& command_line) { + ProcessType process_type = delegate->OverrideProcessType(); + if (process_type == ProcessType::kDefault) { + const std::string& type_switch = + command_line.GetSwitchValueASCII(switches::kProcessType); + if (type_switch == switches::kProcessTypeServiceManager) { + process_type = ProcessType::kServiceManager; + } else if (type_switch == switches::kProcessTypeService) { + process_type = ProcessType::kService; + } else { + process_type = ProcessType::kEmbedder; + } + } + return process_type; +} + } // namespace MainParams::MainParams(MainDelegate* delegate) : delegate(delegate) {} MainParams::~MainParams() {} -int Main(const MainParams& params) { +int MainInitialize(MainParams& params) { MainDelegate* delegate = params.delegate; DCHECK(delegate); int exit_code = -1; base::debug::GlobalActivityTracker* tracker = nullptr; ProcessType process_type = delegate->OverrideProcessType(); -#if defined(OS_MACOSX) - std::unique_ptr 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 @@ -342,12 +356,7 @@ int Main(const MainParams& params) { MainDelegate::InitializeParams init_params; #if defined(OS_MACOSX) - // 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. - autorelease_pool = std::make_unique(); - init_params.autorelease_pool = autorelease_pool.get(); + init_params.autorelease_pool = params.autorelease_pool.get(); InitializeMac(); #endif @@ -391,18 +400,16 @@ int Main(const MainParams& params) { } } + return exit_code; +} + +int MainRun(MainParams& params) { + MainDelegate* delegate = params.delegate; + DCHECK(delegate); + + int exit_code = 0; const auto& command_line = *base::CommandLine::ForCurrentProcess(); - if (process_type == ProcessType::kDefault) { - std::string type_switch = - command_line.GetSwitchValueASCII(switches::kProcessType); - if (type_switch == switches::kProcessTypeServiceManager) { - process_type = ProcessType::kServiceManager; - } else if (type_switch == switches::kProcessTypeService) { - process_type = ProcessType::kService; - } else { - process_type = ProcessType::kEmbedder; - } - } + const ProcessType process_type = GetProcessType(delegate, command_line); switch (process_type) { case ProcessType::kDefault: NOTREACHED(); @@ -424,6 +431,8 @@ int Main(const MainParams& params) { break; } + base::debug::GlobalActivityTracker* tracker = + base::debug::GlobalActivityTracker::Get(); if (tracker) { if (exit_code == 0) { tracker->SetProcessPhaseIfEnabled( @@ -435,13 +444,38 @@ int Main(const MainParams& params) { } } + return exit_code; +} + +void MainShutdown(MainParams& params) { + MainDelegate* delegate = params.delegate; + DCHECK(delegate); + #if defined(OS_MACOSX) - autorelease_pool.reset(); + params.autorelease_pool.reset(); #endif + const ProcessType process_type = + GetProcessType(delegate, *base::CommandLine::ForCurrentProcess()); if (process_type == ProcessType::kEmbedder) delegate->ShutDownEmbedderProcess(); +} + +int Main(MainParams& params) { +#if defined(OS_MACOSX) + // 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. + params.autorelease_pool = + std::make_unique(); +#endif + int exit_code = MainInitialize(params); + if (exit_code >= 0) + return exit_code; + exit_code = MainRun(params); + MainShutdown(params); return exit_code; } diff --git services/service_manager/embedder/main.h services/service_manager/embedder/main.h index 57e88aa85dfe..5ed6ec2abfda 100644 --- services/service_manager/embedder/main.h +++ services/service_manager/embedder/main.h @@ -5,9 +5,15 @@ #ifndef SERVICES_SERVICE_MANAGER_EMBEDDER_MAIN_H_ #define SERVICES_SERVICE_MANAGER_EMBEDDER_MAIN_H_ +#include + #include "base/component_export.h" #include "build/build_config.h" +#if defined(OS_MACOSX) +#include "base/mac/scoped_nsautorelease_pool.h" +#endif // defined(OS_MACOSX) + namespace service_manager { class MainDelegate; @@ -22,11 +28,22 @@ struct COMPONENT_EXPORT(SERVICE_MANAGER_EMBEDDER) MainParams { int argc = 0; const char** argv = nullptr; #endif + +#if defined(OS_MACOSX) + std::unique_ptr autorelease_pool; +#endif }; +// Split Main() into separate stages. +int COMPONENT_EXPORT(SERVICE_MANAGER_EMBEDDER) + MainInitialize(MainParams& params); +int COMPONENT_EXPORT(SERVICE_MANAGER_EMBEDDER) MainRun(MainParams& params); +void COMPONENT_EXPORT(SERVICE_MANAGER_EMBEDDER) + MainShutdown(MainParams& params); + // Main function which should be called as early as possible by any executable // embedding the service manager. -int COMPONENT_EXPORT(SERVICE_MANAGER_EMBEDDER) Main(const MainParams& params); +int COMPONENT_EXPORT(SERVICE_MANAGER_EMBEDDER) Main(MainParams& params); } // namespace service_manager diff --git services/service_manager/embedder/set_process_title.cc services/service_manager/embedder/set_process_title.cc index 1dc53b847ef9..5432ab02a088 100644 --- services/service_manager/embedder/set_process_title.cc +++ services/service_manager/embedder/set_process_title.cc @@ -44,7 +44,7 @@ void SetProcessTitleFromCommandLine(const char** main_argv) { bool have_argv0 = false; #if defined(OS_LINUX) - DCHECK_EQ(base::PlatformThread::CurrentId(), getpid()); + //DCHECK_EQ(base::PlatformThread::CurrentId(), getpid()); if (main_argv) setproctitle_init(main_argv);