Commit 8a820c82 authored by jam@chromium.org's avatar jam@chromium.org

Convert ContentMain to take a struct instead of parameters that vary depending...

Convert ContentMain to take a struct instead of parameters that vary depending on the platform. This helps reduce ifdef mess, and makes it easier to add extra optional parameters. In a followup cl, I'll move the ui_task parameter from MainFunctionParams to ContentMainParams.

BUG=350550
R=sky@chromium.org

Review URL: https://codereview.chromium.org/190853004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@255866 0039d316-1c4b-4281-b951-d872f2087c98
parent a4afc43b
......@@ -11,19 +11,22 @@
#endif
#if defined(OS_WIN)
int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE, wchar_t*, int) {
sandbox::SandboxInterfaceInfo sandbox_info = {0};
content::InitializeSandboxInfo(&sandbox_info);
#else
int main(int argc, const char** argv) {
#endif
apps::ShellMainDelegate delegate;
return content::ContentMain(instance, &sandbox_info, &delegate);
}
content::ContentMainParams params(&delegate);
#if defined(OS_WIN)
sandbox::SandboxInterfaceInfo sandbox_info = {0};
content::InitializeSandboxInfo(&sandbox_info);
params.instance = instance;
params.sandbox_info = &sandbox_info;
#else
params.argc = argc;
params.argv = argv;
#endif
int main(int argc, const char** argv) {
apps::ShellMainDelegate delegate;
return content::ContentMain(argc, argv, &delegate);
return content::ContentMain(params);
}
#endif // OS_WIN
......@@ -12,14 +12,21 @@
#if defined(OS_WIN)
int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE, wchar_t*, int) {
sandbox::SandboxInterfaceInfo sandbox_info = {0};
content::InitializeSandboxInfo(&sandbox_info);
ash::shell::ShellMainDelegate delegate;
return content::ContentMain(instance, &sandbox_info, &delegate);
}
#else
int main(int argc, const char** argv) {
#endif
ash::shell::ShellMainDelegate delegate;
return content::ContentMain(argc, argv, &delegate);
}
content::ContentMainParams params(&delegate);
#if defined(OS_WIN)
sandbox::SandboxInterfaceInfo sandbox_info = {0};
content::InitializeSandboxInfo(&sandbox_info);
params.instance = instance;
params.sandbox_info = &sandbox_info;
#else
params.argc = argc;
params.argv = argv;
#endif
return content::ContentMain(params);
}
......@@ -26,16 +26,28 @@ int ChromeMain(int argc, const char** argv);
#if defined(OS_WIN)
DLLEXPORT int __cdecl ChromeMain(HINSTANCE instance,
sandbox::SandboxInterfaceInfo* sandbox_info) {
#elif defined(OS_POSIX)
int ChromeMain(int argc, const char** argv) {
#endif
ChromeMainDelegate chrome_main_delegate;
content::ContentMainParams params(&chrome_main_delegate);
#if defined(OS_WIN)
// The process should crash when going through abnormal termination.
base::win::SetShouldCrashOnProcessDetach(true);
base::win::SetAbortBehaviorForCrashReporting();
ChromeMainDelegate chrome_main_delegate;
int rv = content::ContentMain(instance, sandbox_info, &chrome_main_delegate);
params.instance = instance;
params.sandbox_info = sandbox_info;
#else
params.argc = argc;
params.argv = argv;
#endif
int rv = content::ContentMain(params);
#if defined(OS_WIN)
base::win::SetShouldCrashOnProcessDetach(false);
return rv;
#elif defined(OS_POSIX)
int ChromeMain(int argc, const char** argv) {
ChromeMainDelegate chrome_main_delegate;
return content::ContentMain(argc, argv, &chrome_main_delegate);
#endif
return rv;
}
......@@ -16,8 +16,8 @@
#include "jni/ContentMain_jni.h"
using base::LazyInstance;
using content::ContentMainRunner;
using content::ContentMainDelegate;
namespace content {
namespace {
LazyInstance<scoped_ptr<ContentMainRunner> > g_content_runner =
......@@ -28,8 +28,6 @@ LazyInstance<scoped_ptr<ContentMainDelegate> > g_content_main_delegate =
} // namespace
namespace content {
static void InitApplicationContext(JNIEnv* env, jclass clazz, jobject context) {
base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context);
base::android::InitApplicationContext(env, scoped_context);
......@@ -43,9 +41,9 @@ static jint Start(JNIEnv* env, jclass clazz) {
// request then we have to call this a second time to finish starting the
// browser synchronously.
if (!g_content_runner.Get().get()) {
ContentMainParams params(g_content_main_delegate.Get().get());
g_content_runner.Get().reset(ContentMainRunner::Create());
g_content_runner.Get()->Initialize(
0, NULL, g_content_main_delegate.Get().get());
g_content_runner.Get()->Initialize(params);
}
return g_content_runner.Get()->Run();
}
......
......@@ -9,26 +9,10 @@
namespace content {
#if defined(OS_WIN)
int ContentMain(HINSTANCE instance,
sandbox::SandboxInterfaceInfo* sandbox_info,
ContentMainDelegate* delegate) {
#else
int ContentMain(int argc,
const char** argv,
ContentMainDelegate* delegate) {
#endif // OS_WIN
int ContentMain(const ContentMainParams& params) {
scoped_ptr<ContentMainRunner> main_runner(ContentMainRunner::Create());
int exit_code;
#if defined(OS_WIN)
exit_code = main_runner->Initialize(instance, sandbox_info, delegate);
#else
exit_code = main_runner->Initialize(argc, argv, delegate);
#endif // OS_WIN
int exit_code = main_runner->Initialize(params);
if (exit_code >= 0)
return exit_code;
......
......@@ -30,6 +30,7 @@
#include "content/common/set_process_title.h"
#include "content/common/url_schemes.h"
#include "content/gpu/in_process_gpu_thread.h"
#include "content/public/app/content_main.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"
......@@ -524,22 +525,13 @@ class ContentMainRunnerImpl : public ContentMainRunner {
}
#endif
virtual int Initialize(const ContentMainParams& params) OVERRIDE {
#if defined(OS_WIN)
virtual int Initialize(HINSTANCE instance,
sandbox::SandboxInterfaceInfo* sandbox_info,
ContentMainDelegate* delegate) OVERRIDE {
// argc/argv are ignored on Windows; see command_line.h for details.
int argc = 0;
char** argv = NULL;
RegisterInvalidParamHandler();
_Module.Init(NULL, static_cast<HINSTANCE>(instance));
_Module.Init(NULL, static_cast<HINSTANCE>(params.instance));
sandbox_info_ = *sandbox_info;
sandbox_info_ = *params.sandbox_info;
#else // !OS_WIN
virtual int Initialize(int argc,
const char** argv,
ContentMainDelegate* delegate) OVERRIDE {
#if defined(OS_ANDROID)
// See note at the initialization of ExitManager, below; basically,
......@@ -609,7 +601,7 @@ class ContentMainRunnerImpl : public ContentMainRunner {
#endif // !OS_WIN
is_initialized_ = true;
delegate_ = delegate;
delegate_ = params.delegate;
base::EnableTerminationOnHeapCorruption();
base::EnableTerminationOnOutOfMemory();
......@@ -634,11 +626,25 @@ class ContentMainRunnerImpl : public ContentMainRunner {
// On Android, the command line is initialized when library is loaded and
// we have already started our TRACE_EVENT0.
#if !defined(OS_ANDROID)
// argc/argv are ignored on Windows and Android; see command_line.h for
// details.
int argc = 0;
const char** argv = NULL;
#if !defined(OS_WIN)
argc = params.argc;
argv = params.argv;
#endif
CommandLine::Init(argc, argv);
#if !defined(OS_IOS)
SetProcessTitleFromCommandLine(argv);
#endif
#endif // !OS_ANDROID
int exit_code;
if (delegate && delegate->BasicStartupComplete(&exit_code))
if (delegate_ && delegate_->BasicStartupComplete(&exit_code))
return exit_code;
completed_basic_startup_ = true;
......@@ -680,13 +686,13 @@ class ContentMainRunnerImpl : public ContentMainRunner {
// It's important not to allocate the ports for processes which don't
// register with the power monitor - see crbug.com/88867.
if (process_type.empty() ||
(delegate &&
delegate->ProcessRegistersWithSystemProcess(process_type))) {
(delegate_ &&
delegate_->ProcessRegistersWithSystemProcess(process_type))) {
base::PowerMonitorDeviceSource::AllocateSystemIOPorts();
}
if (!process_type.empty() &&
(!delegate || delegate->ShouldSendMachPort(process_type))) {
(!delegate_ || delegate_->ShouldSendMachPort(process_type))) {
MachBroker::ChildSendTaskPortToParent();
}
#elif defined(OS_WIN)
......@@ -729,18 +735,18 @@ class ContentMainRunnerImpl : public ContentMainRunner {
InitializeStatsTable(command_line);
if (delegate)
delegate->PreSandboxStartup();
if (delegate_)
delegate_->PreSandboxStartup();
if (!process_type.empty())
CommonSubprocessInit(process_type);
#if defined(OS_WIN)
CHECK(InitializeSandbox(sandbox_info));
CHECK(InitializeSandbox(params.sandbox_info));
#elif defined(OS_MACOSX) && !defined(OS_IOS)
if (process_type == switches::kRendererProcess ||
process_type == switches::kPpapiPluginProcess ||
(delegate && delegate->DelaySandboxInitialization(process_type))) {
(delegate_ && delegate_->DelaySandboxInitialization(process_type))) {
// On OS X the renderer sandbox needs to be initialized later in the
// startup sequence in RendererMainPlatformDelegate::EnableSandbox().
} else {
......@@ -748,12 +754,8 @@ class ContentMainRunnerImpl : public ContentMainRunner {
}
#endif
if (delegate)
delegate->SandboxInitialized(process_type);
#if defined(OS_POSIX) && !defined(OS_IOS)
SetProcessTitleFromCommandLine(argv);
#endif
if (delegate_)
delegate_->SandboxInitialized(process_type);
// Return -1 to indicate no early termination.
return -1;
......
......@@ -5,6 +5,8 @@
#ifndef CONTENT_PUBLIC_APP_CONTENT_MAIN_H_
#define CONTENT_PUBLIC_APP_CONTENT_MAIN_H_
#include <stddef.h>
#include "build/build_config.h"
#include "content/common/content_export.h"
......@@ -17,21 +19,36 @@ struct SandboxInterfaceInfo;
}
namespace content {
class ContentMainDelegate;
// ContentMain should be called from the embedder's main() function to do the
// initial setup for every process. The embedder has a chance to customize
// startup using the ContentMainDelegate interface. The embedder can also pass
// in NULL for |delegate| if they don't want to override default startup.
struct ContentMainParams {
explicit ContentMainParams(ContentMainDelegate* delegate)
: delegate(delegate)
#if defined(OS_WIN)
, instance(NULL),
sandbox_info(NULL)
#elif !defined(OS_ANDROID)
, argc(0),
argv(NULL)
#endif
{
}
// |sandbox_info| should be initialized using InitializeSandboxInfo from
// content_main_win.h
CONTENT_EXPORT int ContentMain(HINSTANCE instance,
sandbox::SandboxInterfaceInfo* sandbox_info,
ContentMainDelegate* delegate);
#elif defined(OS_ANDROID)
ContentMainDelegate* delegate;
#if defined(OS_WIN)
HINSTANCE instance;
// |sandbox_info| should be initialized using InitializeSandboxInfo from
// content_main_win.h
sandbox::SandboxInterfaceInfo* sandbox_info;
#elif !defined(OS_ANDROID)
int argc;
const char** argv;
#endif
};
#if defined(OS_ANDROID)
// In the Android, the content main starts from ContentMain.java, This function
// provides a way to set the |delegate| as ContentMainDelegate for
// ContentMainRunner.
......@@ -39,10 +56,12 @@ CONTENT_EXPORT int ContentMain(HINSTANCE instance,
// The ownership of |delegate| is transferred.
CONTENT_EXPORT void SetContentMainDelegate(ContentMainDelegate* delegate);
#else
CONTENT_EXPORT int ContentMain(int argc,
const char** argv,
ContentMainDelegate* delegate);
#endif // defined(OS_WIN)
// ContentMain should be called from the embedder's main() function to do the
// initial setup for every process. The embedder has a chance to customize
// startup using the ContentMainDelegate interface. The embedder can also pass
// in NULL for |delegate| if they don't want to override default startup.
CONTENT_EXPORT int ContentMain(const ContentMainParams& params);
#endif
} // namespace content
......
......@@ -5,21 +5,8 @@
#ifndef CONTENT_PUBLIC_APP_CONTENT_MAIN_RUNNER_H_
#define CONTENT_PUBLIC_APP_CONTENT_MAIN_RUNNER_H_
#include <string>
#include "build/build_config.h"
#if defined(OS_WIN)
#include <windows.h>
#endif
namespace sandbox {
struct SandboxInterfaceInfo;
}
namespace content {
class ContentMainDelegate;
struct ContentMainParams;
// This class is responsible for content initialization, running and shutdown.
class ContentMainRunner {
......@@ -30,19 +17,7 @@ class ContentMainRunner {
static ContentMainRunner* Create();
// Initialize all necessary content state.
#if defined(OS_WIN)
// The |sandbox_info| and |delegate| objects must outlive this class.
// |sandbox_info| should be initialized using InitializeSandboxInfo from
// content_main_win.h.
virtual int Initialize(HINSTANCE instance,
sandbox::SandboxInterfaceInfo* sandbox_info,
ContentMainDelegate* delegate) = 0;
#else
// The |delegate| object must outlive this class.
virtual int Initialize(int argc,
const char** argv,
ContentMainDelegate* delegate) = 0;
#endif
virtual int Initialize(const ContentMainParams& params) = 0;
// Perform the default run logic.
virtual int Run() = 0;
......
......@@ -9,8 +9,8 @@
#ifndef CONTENT_PUBLIC_COMMON_MAIN_FUNCTION_PARAMS_H_
#define CONTENT_PUBLIC_COMMON_MAIN_FUNCTION_PARAMS_H_
#include "base/command_line.h"
#include "base/callback_forward.h"
#include "base/command_line.h"
#if defined(OS_WIN)
namespace sandbox {
......@@ -34,13 +34,17 @@ struct MainFunctionParams {
#elif defined(OS_MACOSX)
autorelease_pool(NULL),
#endif
ui_task(NULL) {}
ui_task(NULL) {
}
const CommandLine& command_line;
#if defined(OS_WIN)
sandbox::SandboxInterfaceInfo* sandbox_info;
#elif defined(OS_MACOSX)
base::mac::ScopedNSAutoreleasePool* autorelease_pool;
#endif
// Used by InProcessBrowserTest. If non-null BrowserMain schedules this
// task to run on the MessageLoop and BrowserInit is not invoked.
base::Closure* ui_task;
......
......@@ -427,35 +427,34 @@ const char kSingleProcessTestsFlag[] = "single_process";
TestLauncherDelegate::~TestLauncherDelegate() {
}
bool ShouldRunContentMain() {
#if defined(OS_WIN) || defined(OS_LINUX)
bool ShouldRunContentMain() {
CommandLine* command_line = CommandLine::ForCurrentProcess();
return command_line->HasSwitch(switches::kProcessType) ||
command_line->HasSwitch(kLaunchAsBrowser);
#else
return false;
#endif // defined(OS_WIN) || defined(OS_LINUX)
}
int RunContentMain(int argc, char** argv,
TestLauncherDelegate* launcher_delegate) {
scoped_ptr<ContentMainDelegate> chrome_main_delegate(
launcher_delegate->CreateContentMainDelegate());
ContentMainParams params(chrome_main_delegate.get());
#if defined(OS_WIN)
sandbox::SandboxInterfaceInfo sandbox_info = {0};
InitializeSandboxInfo(&sandbox_info);
scoped_ptr<ContentMainDelegate> chrome_main_delegate(
launcher_delegate->CreateContentMainDelegate());
return ContentMain(GetModuleHandle(NULL),
&sandbox_info,
chrome_main_delegate.get());
params.instance = GetModuleHandle(NULL);
params.sandbox_info = &sandbox_info;
#elif defined(OS_LINUX)
scoped_ptr<ContentMainDelegate> chrome_main_delegate(
launcher_delegate->CreateContentMainDelegate());
return ContentMain(argc, const_cast<const char**>(argv),
chrome_main_delegate.get());
params.argc = argc;
params.argv = const_cast<const char**>(argv);
#endif // defined(OS_WIN)
NOTREACHED();
return 0;
return ContentMain(params);
}
#endif
int LaunchTests(TestLauncherDelegate* launcher_delegate,
int default_jobs,
......@@ -487,8 +486,10 @@ int LaunchTests(TestLauncherDelegate* launcher_delegate,
return launcher_delegate->RunTestSuite(argc, argv);
}
#if defined(OS_WIN) || defined(OS_LINUX)
if (ShouldRunContentMain())
return RunContentMain(argc, argv, launcher_delegate);
#endif
base::AtExitManager at_exit;
testing::InitGoogleTest(&argc, argv);
......
......@@ -11,6 +11,9 @@
int ContentMain(int argc,
const char** argv) {
content::ShellMainDelegate delegate;
return content::ContentMain(argc, argv, &delegate);
content::ContentMainParams params(&delegate);
params.argc = argc;
params.argv = argv;
return content::ContentMain(params);
}
#endif // OS_MACOSX
......@@ -20,7 +20,11 @@ int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE, wchar_t*, int) {
sandbox::SandboxInterfaceInfo sandbox_info = {0};
content::InitializeSandboxInfo(&sandbox_info);
content::ShellMainDelegate delegate;
return content::ContentMain(instance, &sandbox_info, &delegate);
content::ContentMainParams params(&delegate);
params.instance = instance;
params.sandbox_info = &sandbox_info;
return content::ContentMain(params);
}
#else
......@@ -32,7 +36,10 @@ int main(int argc, const char** argv) {
return ::ContentMain(argc, argv);
#else
content::ShellMainDelegate delegate;
return content::ContentMain(argc, argv, &delegate);
content::ContentMainParams params(&delegate);
params.argc = argc;
params.argv = argv;
return content::ContentMain(params);
#endif // OS_MACOSX
}
......
......@@ -12,14 +12,21 @@
#if defined(OS_WIN)
int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE, wchar_t*, int) {
sandbox::SandboxInterfaceInfo sandbox_info = {0};
content::InitializeSandboxInfo(&sandbox_info);
views::examples::ExamplesMainDelegate delegate;
return content::ContentMain(instance, &sandbox_info, &delegate);
}
#else
int main(int argc, const char** argv) {
#endif
views::examples::ExamplesMainDelegate delegate;
return content::ContentMain(argc, argv, &delegate);
}
content::ContentMainParams params(&delegate);
#if defined(OS_WIN)
sandbox::SandboxInterfaceInfo sandbox_info = {0};
content::InitializeSandboxInfo(&sandbox_info);
params.instance = instance;
params.sandbox_info = &sandbox_info;
#else
params.argc = argc;
params.argv = argv;
#endif
return content::ContentMain(params);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment