Index: content/app/content_main_runner.cc |
=================================================================== |
--- content/app/content_main_runner.cc (revision 121246) |
+++ content/app/content_main_runner.cc (working copy) |
@@ -2,7 +2,7 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "content/app/content_main.h" |
+#include "content/public/app/content_main_runner.h" |
#include "base/at_exit.h" |
#include "base/command_line.h" |
@@ -27,6 +27,7 @@ |
#include "content/public/common/sandbox_init.h" |
#include "crypto/nss_util.h" |
#include "ipc/ipc_switches.h" |
+#include "sandbox/src/sandbox_types.h" |
#include "ui/base/ui_base_switches.h" |
#include "ui/base/ui_base_paths.h" |
#include "webkit/glue/webkit_glue.h" |
@@ -41,7 +42,7 @@ |
#include "base/system_monitor/system_monitor.h" |
#include "content/browser/mach_broker_mac.h" |
#include "content/common/sandbox_init_mac.h" |
-#endif // OS_WIN |
+#endif // OS_WIN |
#if defined(OS_POSIX) |
#include <signal.h> |
@@ -260,8 +261,15 @@ |
}; |
for (size_t i = 0; i < arraysize(kMainFunctions); ++i) { |
- if (process_type == kMainFunctions[i].name) |
+ if (process_type == kMainFunctions[i].name) { |
+ if (delegate) { |
+ int exit_code = delegate->RunProcess(process_type, |
+ main_function_params); |
+ if (exit_code >= 0) |
+ return exit_code; |
+ } |
return kMainFunctions[i].function(main_function_params); |
+ } |
} |
#if defined(OS_POSIX) && !defined(OS_MACOSX) |
@@ -279,194 +287,271 @@ |
return 1; |
} |
-} // namespace |
+class ContentMainRunnerImpl : public content::ContentMainRunner { |
+ public: |
+ ContentMainRunnerImpl() |
+ : is_initialized_(false), |
+ is_shutdown_(false) { |
+ } |
-namespace content { |
+ ~ContentMainRunnerImpl() { |
+ if (is_initialized_ && !is_shutdown_) |
+ Shutdown(); |
+ } |
#if defined(OS_WIN) |
-int ContentMain(HINSTANCE instance, |
- sandbox::SandboxInterfaceInfo* sandbox_info, |
- ContentMainDelegate* delegate) { |
- // argc/argv are ignored on Windows; see command_line.h for details. |
- int argc = 0; |
- char** argv = NULL; |
+ virtual int Initialize(HINSTANCE instance, |
+ sandbox::SandboxInterfaceInfo* sandbox_info, |
+ content::ContentMainDelegate* delegate) OVERRIDE { |
+ // argc/argv are ignored on Windows; see command_line.h for details. |
+ int argc = 0; |
+ char** argv = NULL; |
- content::RegisterInvalidParamHandler(); |
- _Module.Init(NULL, static_cast<HINSTANCE>(instance)); |
-#else |
-int ContentMain(int argc, |
- const char** argv, |
- ContentMainDelegate* delegate) { |
- // NOTE(willchan): One might ask why this call is done here rather than in |
- // process_util_linux.cc with the definition of |
- // EnableTerminationOnOutOfMemory(). That's because base shouldn't have a |
- // dependency on TCMalloc. Really, we ought to have our allocator shim code |
- // implement this EnableTerminationOnOutOfMemory() function. Whateverz. This |
- // works for now. |
+ content::RegisterInvalidParamHandler(); |
+ _Module.Init(NULL, static_cast<HINSTANCE>(instance)); |
+ |
+ if (sandbox_info) |
+ sandbox_info_ = *sandbox_info; |
+ else |
+ memset(&sandbox_info_, 0, sizeof(sandbox_info_)); |
+ |
+#else // !OS_WIN |
+ virtual int Initialize(int argc, |
+ const char** argv, |
+ content::ContentMainDelegate* delegate) OVERRIDE { |
+ // NOTE(willchan): One might ask why this call is done here rather than in |
+ // process_util_linux.cc with the definition of |
+ // EnableTerminationOnOutOfMemory(). That's because base shouldn't have a |
+ // dependency on TCMalloc. Really, we ought to have our allocator shim code |
+ // implement this EnableTerminationOnOutOfMemory() function. Whateverz. |
+ // This works for now. |
#if !defined(OS_MACOSX) && defined(USE_TCMALLOC) |
- // For tcmalloc, we need to tell it to behave like new. |
- tc_set_new_mode(1); |
+ // For tcmalloc, we need to tell it to behave like new. |
+ tc_set_new_mode(1); |
#endif |
#if !defined(OS_ANDROID) |
- // Set C library locale to make sure CommandLine can parse argument values |
- // in correct encoding. |
- setlocale(LC_ALL, ""); |
+ // Set C library locale to make sure CommandLine can parse argument values |
+ // in correct encoding. |
+ setlocale(LC_ALL, ""); |
#endif |
- SetupSignalHandlers(); |
+ SetupSignalHandlers(); |
- base::GlobalDescriptors* g_fds = base::GlobalDescriptors::GetInstance(); |
- g_fds->Set(kPrimaryIPCChannel, |
- kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor); |
+ base::GlobalDescriptors* g_fds = base::GlobalDescriptors::GetInstance(); |
+ g_fds->Set(kPrimaryIPCChannel, |
+ kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor); |
#if defined(OS_LINUX) || defined(OS_OPENBSD) |
- g_fds->Set(kCrashDumpSignal, |
- kCrashDumpSignal + base::GlobalDescriptors::kBaseDescriptor); |
+ g_fds->Set(kCrashDumpSignal, |
+ kCrashDumpSignal + base::GlobalDescriptors::kBaseDescriptor); |
#endif |
-#endif // OS_WIN |
+#endif // !OS_WIN |
- base::EnableTerminationOnHeapCorruption(); |
- base::EnableTerminationOnOutOfMemory(); |
+ is_initialized_ = true; |
+ delegate_ = delegate; |
- // The exit manager is in charge of calling the dtors of singleton objects. |
- base::AtExitManager exit_manager; |
+ base::EnableTerminationOnHeapCorruption(); |
+ base::EnableTerminationOnOutOfMemory(); |
+ // The exit manager is in charge of calling the dtors of singleton objects. |
+ exit_manager_.reset(new base::AtExitManager); |
+ |
#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. |
- base::mac::ScopedNSAutoreleasePool autorelease_pool; |
+ // 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_.reset(new base::mac::ScopedNSAutoreleasePool()); |
#endif |
- CommandLine::Init(argc, argv); |
+ CommandLine::Init(argc, argv); |
- int exit_code; |
- if (delegate && delegate->BasicStartupComplete(&exit_code)) |
- return exit_code; |
+ int exit_code; |
+ if (delegate && delegate->BasicStartupComplete(&exit_code)) |
+ return exit_code; |
- const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
- std::string process_type = |
- command_line.GetSwitchValueASCII(switches::kProcessType); |
+ const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
+ std::string process_type = |
+ command_line.GetSwitchValueASCII(switches::kProcessType); |
- // Enable startup tracing asap to avoid early TRACE_EVENT calls being ignored. |
- if (command_line.HasSwitch(switches::kTraceStartup)) { |
- base::debug::TraceLog::GetInstance()->SetEnabled( |
- command_line.GetSwitchValueASCII(switches::kTraceStartup)); |
- } |
+ // Enable startup tracing asap to avoid early TRACE_EVENT calls being |
+ // ignored. |
+ if (command_line.HasSwitch(switches::kTraceStartup)) { |
+ base::debug::TraceLog::GetInstance()->SetEnabled( |
+ command_line.GetSwitchValueASCII(switches::kTraceStartup)); |
+ } |
#if defined(OS_MACOSX) |
- // We need to allocate the IO Ports before the Sandbox is initialized or |
- // the first instance of SystemMonitor is created. |
- // It's important not to allocate the ports for processes which don't register |
- // with the system monitor - see crbug.com/88867. |
- if (process_type.empty() || |
- process_type == switches::kPluginProcess || |
- process_type == switches::kRendererProcess || |
- process_type == switches::kUtilityProcess || |
- process_type == switches::kWorkerProcess || |
- (delegate && delegate->ProcessRegistersWithSystemProcess(process_type))) { |
- base::SystemMonitor::AllocateSystemIOPorts(); |
- } |
+ // We need to allocate the IO Ports before the Sandbox is initialized or |
+ // the first instance of SystemMonitor is created. |
+ // It's important not to allocate the ports for processes which don't |
+ // register with the system monitor - see crbug.com/88867. |
+ if (process_type.empty() || |
+ process_type == switches::kPluginProcess || |
+ process_type == switches::kRendererProcess || |
+ process_type == switches::kUtilityProcess || |
+ process_type == switches::kWorkerProcess || |
+ (delegate && |
+ delegate->ProcessRegistersWithSystemProcess(process_type))) { |
+ base::SystemMonitor::AllocateSystemIOPorts(); |
+ } |
- if (!process_type.empty() && |
- (!delegate || delegate->ShouldSendMachPort(process_type))) { |
- SendTaskPortToParentProcess(); |
- } |
+ if (!process_type.empty() && |
+ (!delegate || delegate->ShouldSendMachPort(process_type))) { |
+ SendTaskPortToParentProcess(); |
+ } |
#elif defined(OS_WIN) |
- content::SetupCRT(command_line); |
+ content::SetupCRT(command_line); |
#endif |
#if defined(OS_POSIX) |
- if (!process_type.empty()) { |
- // When you hit Ctrl-C in a terminal running the browser |
- // process, a SIGINT is delivered to the entire process group. |
- // When debugging the browser process via gdb, gdb catches the |
- // SIGINT for the browser process (and dumps you back to the gdb |
- // console) but doesn't for the child processes, killing them. |
- // The fix is to have child processes ignore SIGINT; they'll die |
- // on their own when the browser process goes away. |
- // |
- // Note that we *can't* rely on BeingDebugged to catch this case because we |
- // are the child process, which is not being debugged. |
- // TODO(evanm): move this to some shared subprocess-init function. |
- if (!base::debug::BeingDebugged()) |
- signal(SIGINT, SIG_IGN); |
- } |
+ if (!process_type.empty()) { |
+ // When you hit Ctrl-C in a terminal running the browser |
+ // process, a SIGINT is delivered to the entire process group. |
+ // When debugging the browser process via gdb, gdb catches the |
+ // SIGINT for the browser process (and dumps you back to the gdb |
+ // console) but doesn't for the child processes, killing them. |
+ // The fix is to have child processes ignore SIGINT; they'll die |
+ // on their own when the browser process goes away. |
+ // |
+ // Note that we *can't* rely on BeingDebugged to catch this case because |
+ // we are the child process, which is not being debugged. |
+ // TODO(evanm): move this to some shared subprocess-init function. |
+ if (!base::debug::BeingDebugged()) |
+ signal(SIGINT, SIG_IGN); |
+ } |
#endif |
#if defined(USE_NSS) |
- crypto::EarlySetupForNSSInit(); |
+ crypto::EarlySetupForNSSInit(); |
#endif |
- ui::RegisterPathProvider(); |
- content::RegisterPathProvider(); |
+ ui::RegisterPathProvider(); |
+ content::RegisterPathProvider(); |
- CHECK(icu_util::Initialize()); |
+ CHECK(icu_util::Initialize()); |
- base::ProcessId browser_pid = base::GetCurrentProcId(); |
- if (command_line.HasSwitch(switches::kProcessChannelID)) { |
+ base::ProcessId browser_pid = base::GetCurrentProcId(); |
+ if (command_line.HasSwitch(switches::kProcessChannelID)) { |
#if defined(OS_WIN) || defined(OS_MACOSX) |
- std::string channel_name = |
- command_line.GetSwitchValueASCII(switches::kProcessChannelID); |
+ std::string channel_name = |
+ command_line.GetSwitchValueASCII(switches::kProcessChannelID); |
- int browser_pid_int; |
- base::StringToInt(channel_name, &browser_pid_int); |
- browser_pid = static_cast<base::ProcessId>(browser_pid_int); |
- DCHECK_NE(browser_pid_int, 0); |
+ int browser_pid_int; |
+ base::StringToInt(channel_name, &browser_pid_int); |
+ browser_pid = static_cast<base::ProcessId>(browser_pid_int); |
+ DCHECK_NE(browser_pid_int, 0); |
#elif defined(OS_POSIX) |
- // On linux, we're in the zygote here; so we need the parent process' id. |
- browser_pid = base::GetParentProcessId(base::GetCurrentProcId()); |
+ // On linux, we're in the zygote here; so we need the parent process' id. |
+ browser_pid = base::GetParentProcessId(base::GetCurrentProcId()); |
#endif |
- } |
+ } |
- InitializeStatsTable(browser_pid, command_line); |
+ InitializeStatsTable(browser_pid, command_line); |
- if (delegate) delegate->PreSandboxStartup(); |
+ if (delegate) |
+ delegate->PreSandboxStartup(); |
- if (!process_type.empty()) |
- CommonSubprocessInit(process_type); |
+ if (!process_type.empty()) |
+ CommonSubprocessInit(process_type); |
#if defined(OS_WIN) |
- CHECK(content::InitializeSandbox(sandbox_info)); |
+ CHECK(content::InitializeSandbox(sandbox_info)); |
#elif defined(OS_MACOSX) |
- if (process_type == switches::kRendererProcess || |
- process_type == switches::kPpapiPluginProcess || |
- (delegate && delegate->DelaySandboxInitialization(process_type))) { |
- // On OS X the renderer sandbox needs to be initialized later in the startup |
- // sequence in RendererMainPlatformDelegate::EnableSandbox(). |
- } else { |
- CHECK(content::InitializeSandbox()); |
- } |
+ if (process_type == switches::kRendererProcess || |
+ process_type == switches::kPpapiPluginProcess || |
+ (delegate && delegate->DelaySandboxInitialization(process_type))) { |
+ // On OS X the renderer sandbox needs to be initialized later in the |
+ // startup sequence in RendererMainPlatformDelegate::EnableSandbox(). |
+ } else { |
+ CHECK(content::InitializeSandbox()); |
+ } |
#endif |
- if (delegate) delegate->SandboxInitialized(process_type); |
+ if (delegate) |
+ delegate->SandboxInitialized(process_type); |
#if defined(OS_POSIX) |
- SetProcessTitleFromCommandLine(argv); |
+ SetProcessTitleFromCommandLine(argv); |
#endif |
- content::MainFunctionParams main_params(command_line); |
+ // Return -1 to indicate no early termination. |
+ return -1; |
+ } |
+ |
+ virtual int Run() OVERRIDE { |
+ DCHECK(is_initialized_); |
+ DCHECK(!is_shutdown_); |
+ const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
+ std::string process_type = |
+ command_line.GetSwitchValueASCII(switches::kProcessType); |
+ |
+ content::MainFunctionParams main_params(command_line); |
#if defined(OS_WIN) |
- main_params.sandbox_info = sandbox_info; |
+ main_params.sandbox_info = &sandbox_info_; |
#elif defined(OS_MACOSX) |
- main_params.autorelease_pool = &autorelease_pool; |
+ main_params.autorelease_pool = autorelease_pool_.get(); |
#endif |
- exit_code = RunNamedProcessTypeMain(process_type, main_params, delegate); |
+ return RunNamedProcessTypeMain(process_type, main_params, delegate_); |
+ } |
- if (delegate) delegate->ProcessExiting(process_type); |
+ virtual void Shutdown() OVERRIDE { |
+ DCHECK(is_initialized_); |
+ DCHECK(!is_shutdown_); |
+ const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
+ std::string process_type = |
+ command_line.GetSwitchValueASCII(switches::kProcessType); |
+ if (delegate_) |
+ delegate_->ProcessExiting(process_type); |
+ |
#if defined(OS_WIN) |
#ifdef _CRTDBG_MAP_ALLOC |
- _CrtDumpMemoryLeaks(); |
+ _CrtDumpMemoryLeaks(); |
#endif // _CRTDBG_MAP_ALLOC |
- _Module.Term(); |
+ _Module.Term(); |
#endif // OS_WIN |
- return exit_code; |
+#if defined(OS_MACOSX) |
+ autorelease_pool_.reset(NULL); |
+#endif |
+ |
+ exit_manager_.reset(NULL); |
+ |
+ delegate_ = NULL; |
+ is_shutdown_ = true; |
+ } |
+ |
+ protected: |
+ // True if the runner has been initialized. |
+ bool is_initialized_; |
+ |
+ // True if the runner has been shut down. |
+ bool is_shutdown_; |
+ |
+ // The delegate will outlive this object. |
+ content::ContentMainDelegate* delegate_; |
+ |
+ scoped_ptr<base::AtExitManager> exit_manager_; |
+#if defined(OS_WIN) |
+ sandbox::SandboxInterfaceInfo sandbox_info_; |
+#elif defined(OS_MACOSX) |
+ scoped_ptr<base::mac::ScopedNSAutoreleasePool> autorelease_pool_; |
+#endif |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ContentMainRunnerImpl); |
+}; |
+ |
+} // namespace |
+ |
+namespace content { |
+ |
+// static |
+ContentMainRunner* ContentMainRunner::Create() { |
+ return new ContentMainRunnerImpl(); |
} |
} // namespace content |