Index: content/app/content_main_runner.cc |
=================================================================== |
--- content/app/content_main_runner.cc (revision 140381) |
+++ content/app/content_main_runner.cc (working copy) |
@@ -11,6 +11,7 @@ |
#include "base/debug/trace_event.h" |
#include "base/file_path.h" |
#include "base/i18n/icu_util.h" |
+#include "base/lazy_instance.h" |
#include "base/logging.h" |
#include "base/memory/scoped_ptr.h" |
#include "base/metrics/stats_table.h" |
@@ -23,12 +24,16 @@ |
#include "content/common/url_schemes.h" |
#include "content/public/app/content_main_delegate.h" |
#include "content/public/app/startup_helper_win.h" |
+#include "content/public/browser/content_browser_client.h" |
#include "content/public/common/content_client.h" |
#include "content/public/common/content_constants.h" |
#include "content/public/common/content_paths.h" |
#include "content/public/common/content_switches.h" |
#include "content/public/common/main_function_params.h" |
#include "content/public/common/sandbox_init.h" |
+#include "content/public/plugin/content_plugin_client.h" |
+#include "content/public/renderer/content_renderer_client.h" |
+#include "content/public/utility/content_utility_client.h" |
#include "crypto/nss_util.h" |
#include "ipc/ipc_switches.h" |
#include "media/base/media.h" |
@@ -81,13 +86,22 @@ |
extern int UtilityMain(const content::MainFunctionParams&); |
#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
namespace content { |
-extern int ZygoteMain(const content::MainFunctionParams&, |
- content::ZygoteForkDelegate* forkdelegate); |
+extern int ZygoteMain(const MainFunctionParams&, |
+ ZygoteForkDelegate* forkdelegate); |
} // namespace content |
#endif |
-namespace { |
+namespace content { |
+base::LazyInstance<ContentBrowserClient> |
+ g_empty_content_browser_client = LAZY_INSTANCE_INITIALIZER; |
+base::LazyInstance<ContentPluginClient> |
+ g_empty_content_plugin_client = LAZY_INSTANCE_INITIALIZER; |
+base::LazyInstance<ContentRendererClient> |
+ g_empty_content_renderer_client = LAZY_INSTANCE_INITIALIZER; |
+base::LazyInstance<ContentUtilityClient> |
+ g_empty_content_utility_client = LAZY_INSTANCE_INITIALIZER; |
+ |
#if defined(OS_WIN) |
static CAppModule _Module; |
@@ -206,20 +220,50 @@ |
// NOTIMPLEMENTED: we probably need to shut this down correctly to avoid |
// leaking shared memory regions on posix platforms. |
std::string statsfile = |
- base::StringPrintf( |
- "%s-%u", content::kStatsFilename, |
+ base::StringPrintf("%s-%u", kStatsFilename, |
static_cast<unsigned int>(GetBrowserPid(command_line))); |
base::StatsTable* stats_table = new base::StatsTable(statsfile, |
- content::kStatsMaxThreads, content::kStatsMaxCounters); |
+ kStatsMaxThreads, kStatsMaxCounters); |
base::StatsTable::set_current(stats_table); |
} |
} |
+class ContentClientInitializer { |
+ public: |
+ static void Set(const std::string& process_type, |
+ ContentMainDelegate* delegate) { |
+ ContentClient* content_client = GetContentClient(); |
+ if (process_type.empty()) { |
+ if (delegate) |
+ content_client->browser_ = delegate->CreateContentBrowserClient(); |
+ if (!content_client->browser_) |
+ content_client->browser_ = &g_empty_content_browser_client.Get(); |
+ } |
+ |
+ if (process_type == switches::kPluginProcess) { |
+ if (delegate) |
+ content_client->plugin_ = delegate->CreateContentPluginClient(); |
+ if (!content_client->plugin_) |
+ content_client->plugin_ = &g_empty_content_plugin_client.Get(); |
+ } else if (process_type == switches::kRendererProcess) { |
+ if (delegate) |
+ content_client->renderer_ = delegate->CreateContentRendererClient(); |
+ if (!content_client->renderer_) |
+ content_client->renderer_ = &g_empty_content_renderer_client.Get(); |
+ } else if (process_type == switches::kUtilityProcess) { |
+ if (delegate) |
+ content_client->utility_ = delegate->CreateContentUtilityClient(); |
+ if (!content_client->utility_) |
+ content_client->utility_ = &g_empty_content_utility_client.Get(); |
+ } |
+ } |
+}; |
+ |
// We dispatch to a process-type-specific FooMain() based on a command-line |
// flag. This struct is used to build a table of (flag, main function) pairs. |
struct MainFunction { |
const char* name; |
- int (*function)(const content::MainFunctionParams&); |
+ int (*function)(const MainFunctionParams&); |
}; |
#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
@@ -227,8 +271,8 @@ |
// subprocesses that are launched via the zygote. This function |
// fills in some process-launching bits around ZygoteMain(). |
// Returns the exit code of the subprocess. |
-int RunZygote(const content::MainFunctionParams& main_function_params, |
- content::ContentMainDelegate* delegate) { |
+int RunZygote(const MainFunctionParams& main_function_params, |
+ ContentMainDelegate* delegate) { |
static const MainFunction kMainFunctions[] = { |
{ switches::kRendererProcess, RendererMain }, |
{ switches::kWorkerProcess, WorkerMain }, |
@@ -236,19 +280,19 @@ |
{ switches::kUtilityProcess, UtilityMain }, |
}; |
- scoped_ptr<content::ZygoteForkDelegate> zygote_fork_delegate; |
+ scoped_ptr<ZygoteForkDelegate> zygote_fork_delegate; |
if (delegate) { |
zygote_fork_delegate.reset(delegate->ZygoteStarting()); |
// Each Renderer we spawn will re-attempt initialization of the media |
// libraries, at which point failure will be detected and handled, so |
// we do not need to cope with initialization failures here. |
FilePath media_path; |
- if (PathService::Get(content::DIR_MEDIA_LIBS, &media_path)) |
+ if (PathService::Get(DIR_MEDIA_LIBS, &media_path)) |
media::InitializeMediaLibrary(media_path); |
} |
// This function call can return multiple times, once per fork(). |
- if (!content::ZygoteMain(main_function_params, zygote_fork_delegate.get())) |
+ if (!ZygoteMain(main_function_params, zygote_fork_delegate.get())) |
return 1; |
if (delegate) delegate->ZygoteForked(); |
@@ -256,6 +300,9 @@ |
// Zygote::HandleForkRequest may have reallocated the command |
// line so update it here with the new version. |
const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
+ std::string process_type = |
+ command_line.GetSwitchValueASCII(switches::kProcessType); |
+ ContentClientInitializer::Set(process_type, delegate); |
// If a custom user agent was passed on the command line, we need |
// to (re)set it now, rather than using the default one the zygote |
@@ -270,12 +317,8 @@ |
// within the new processes as well. |
InitializeStatsTable(command_line); |
- content::MainFunctionParams main_params(command_line); |
+ MainFunctionParams main_params(command_line); |
- // Get the new process type from the new command line. |
- std::string process_type = |
- command_line.GetSwitchValueASCII(switches::kProcessType); |
- |
for (size_t i = 0; i < arraysize(kMainFunctions); ++i) { |
if (process_type == kMainFunctions[i].name) |
return kMainFunctions[i].function(main_params); |
@@ -294,8 +337,8 @@ |
// Returns the exit code for this process. |
int RunNamedProcessTypeMain( |
const std::string& process_type, |
- const content::MainFunctionParams& main_function_params, |
- content::ContentMainDelegate* delegate) { |
+ const MainFunctionParams& main_function_params, |
+ ContentMainDelegate* delegate) { |
static const MainFunction kMainFunctions[] = { |
{ "", BrowserMain }, |
{ switches::kRendererProcess, RendererMain }, |
@@ -334,7 +377,7 @@ |
return 1; |
} |
-class ContentMainRunnerImpl : public content::ContentMainRunner { |
+class ContentMainRunnerImpl : public ContentMainRunner { |
public: |
ContentMainRunnerImpl() |
: is_initialized_(false), |
@@ -361,12 +404,12 @@ |
#if defined(OS_WIN) |
virtual int Initialize(HINSTANCE instance, |
sandbox::SandboxInterfaceInfo* sandbox_info, |
- content::ContentMainDelegate* delegate) OVERRIDE { |
+ ContentMainDelegate* delegate) OVERRIDE { |
// argc/argv are ignored on Windows; see command_line.h for details. |
int argc = 0; |
char** argv = NULL; |
- content::RegisterInvalidParamHandler(); |
+ RegisterInvalidParamHandler(); |
_Module.Init(NULL, static_cast<HINSTANCE>(instance)); |
if (sandbox_info) |
@@ -377,7 +420,7 @@ |
#else // !OS_WIN |
virtual int Initialize(int argc, |
const char** argv, |
- content::ContentMainDelegate* delegate) OVERRIDE { |
+ 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 |
@@ -448,15 +491,17 @@ |
int exit_code; |
if (delegate && delegate->BasicStartupComplete(&exit_code)) |
return exit_code; |
- DCHECK(!delegate || content::GetContentClient()) << |
- "BasicStartupComplete didn't set the content client"; |
completed_basic_startup_ = true; |
const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
std::string process_type = |
- command_line.GetSwitchValueASCII(switches::kProcessType); |
+ command_line.GetSwitchValueASCII(switches::kProcessType); |
+ if (!GetContentClient()) |
+ SetContentClient(&empty_content_client_); |
+ ContentClientInitializer::Set(process_type, delegate_); |
+ |
// Enable startup tracing asap to avoid early TRACE_EVENT calls being |
// ignored. |
if (command_line.HasSwitch(switches::kTraceStartup)) { |
@@ -487,7 +532,7 @@ |
#if defined(ENABLE_HIDPI) |
ui::EnableHighDPISupport(); |
#endif |
- content::SetupCRT(command_line); |
+ SetupCRT(command_line); |
#endif |
#if defined(OS_POSIX) |
@@ -513,8 +558,8 @@ |
#endif |
ui::RegisterPathProvider(); |
- content::RegisterPathProvider(); |
- content::RegisterContentSchemes(true); |
+ RegisterPathProvider(); |
+ RegisterContentSchemes(true); |
CHECK(icu_util::Initialize()); |
@@ -535,7 +580,7 @@ |
CommonSubprocessInit(process_type); |
#if defined(OS_WIN) |
- CHECK(content::InitializeSandbox(sandbox_info)); |
+ CHECK(InitializeSandbox(sandbox_info)); |
#elif defined(OS_MACOSX) |
if (process_type == switches::kRendererProcess || |
process_type == switches::kPpapiPluginProcess || |
@@ -543,7 +588,7 @@ |
// On OS X the renderer sandbox needs to be initialized later in the |
// startup sequence in RendererMainPlatformDelegate::EnableSandbox(). |
} else { |
- CHECK(content::InitializeSandbox()); |
+ CHECK(InitializeSandbox()); |
} |
#endif |
@@ -565,7 +610,7 @@ |
std::string process_type = |
command_line.GetSwitchValueASCII(switches::kProcessType); |
- content::MainFunctionParams main_params(command_line); |
+ MainFunctionParams main_params(command_line); |
#if defined(OS_WIN) |
main_params.sandbox_info = &sandbox_info_; |
#elif defined(OS_MACOSX) |
@@ -605,7 +650,7 @@ |
is_shutdown_ = true; |
} |
- protected: |
+ private: |
// True if the runner has been initialized. |
bool is_initialized_; |
@@ -615,8 +660,11 @@ |
// True if basic startup was completed. |
bool completed_basic_startup_; |
+ // Used if the embedder doesn't set one. |
+ ContentClient empty_content_client_; |
+ |
// The delegate will outlive this object. |
- content::ContentMainDelegate* delegate_; |
+ ContentMainDelegate* delegate_; |
scoped_ptr<base::AtExitManager> exit_manager_; |
#if defined(OS_WIN) |
@@ -628,10 +676,6 @@ |
DISALLOW_COPY_AND_ASSIGN(ContentMainRunnerImpl); |
}; |
-} // namespace |
- |
-namespace content { |
- |
// static |
ContentMainRunner* ContentMainRunner::Create() { |
return new ContentMainRunnerImpl(); |