diff options
author | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-09-01 15:51:53 +0000 |
---|---|---|
committer | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-09-01 15:51:53 +0000 |
commit | d85d670009ad28c375fc0dbcd8f333ba00ef7327 (patch) | |
tree | 6c6e8ef196182a36ab8b1a0f547874e5ee4c049e /content | |
parent | e601bd195eea2dfb27dc590d6f122f5715087769 (diff) | |
download | chromium_src-d85d670009ad28c375fc0dbcd8f333ba00ef7327.zip chromium_src-d85d670009ad28c375fc0dbcd8f333ba00ef7327.tar.gz chromium_src-d85d670009ad28c375fc0dbcd8f333ba00ef7327.tar.bz2 |
Start moving ChromeMain code to content so it can be reused by other embedders. I've only moved a little bit of the code for now, and will do the rest in followup changes.
BUG=90445
Review URL: http://codereview.chromium.org/7817015
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@99193 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content')
-rw-r--r-- | content/app/DEPS | 3 | ||||
-rw-r--r-- | content/app/content_main.cc | 173 | ||||
-rw-r--r-- | content/app/content_main.h | 45 | ||||
-rw-r--r-- | content/app/content_main_delegate.h | 29 | ||||
-rw-r--r-- | content/app/sandbox_helper_win.cc | 24 | ||||
-rw-r--r-- | content/app/sandbox_helper_win.h | 20 | ||||
-rw-r--r-- | content/content.gyp | 1 | ||||
-rw-r--r-- | content/content_app.gypi | 33 |
8 files changed, 328 insertions, 0 deletions
diff --git a/content/app/DEPS b/content/app/DEPS new file mode 100644 index 0000000..60dbcf4 --- /dev/null +++ b/content/app/DEPS @@ -0,0 +1,3 @@ +include_rules = [ + "+content", +] diff --git a/content/app/content_main.cc b/content/app/content_main.cc new file mode 100644 index 0000000..6204f95 --- /dev/null +++ b/content/app/content_main.cc @@ -0,0 +1,173 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// 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 "base/at_exit.h" +#include "base/command_line.h" +#include "base/logging.h" +#include "base/mac/scoped_nsautorelease_pool.h" +#include "base/process_util.h" +#include "content/app/content_main_delegate.h" + +#if defined(OS_WIN) +#include <atlbase.h> +#include <atlapp.h> +#include <new.h> +#include <malloc.h> +#elif defined(OS_POSIX) +#include <signal.h> + +#include "base/global_descriptors_posix.h" +#include "content/common/chrome_descriptors.h" +#endif // OS_WIN + +#if !defined(OS_MACOSX) && defined(USE_TCMALLOC) +extern "C" { +int tc_set_new_mode(int mode); +} +#endif + +namespace { + +base::AtExitManager* g_exit_manager; +base::mac::ScopedNSAutoreleasePool* g_autorelease_pool; + +#if defined(OS_WIN) + +static CAppModule _Module; + +#pragma optimize("", off) +// Handlers for invalid parameter and pure call. They generate a breakpoint to +// tell breakpad that it needs to dump the process. +void InvalidParameter(const wchar_t* expression, const wchar_t* function, + const wchar_t* file, unsigned int line, + uintptr_t reserved) { + __debugbreak(); + _exit(1); +} + +void PureCall() { + __debugbreak(); + _exit(1); +} +#pragma optimize("", on) + +// Register the invalid param handler and pure call handler to be able to +// notify breakpad when it happens. +void RegisterInvalidParamHandler() { + _set_invalid_parameter_handler(InvalidParameter); + _set_purecall_handler(PureCall); + // Also enable the new handler for malloc() based failures. + _set_new_mode(1); +} + +#elif defined(OS_POSIX) + +// Setup signal-handling state: resanitize most signals, ignore SIGPIPE. +void SetupSignalHandlers() { + // Sanitise our signal handling state. Signals that were ignored by our + // parent will also be ignored by us. We also inherit our parent's sigmask. + sigset_t empty_signal_set; + CHECK(0 == sigemptyset(&empty_signal_set)); + CHECK(0 == sigprocmask(SIG_SETMASK, &empty_signal_set, NULL)); + + struct sigaction sigact; + memset(&sigact, 0, sizeof(sigact)); + sigact.sa_handler = SIG_DFL; + static const int signals_to_reset[] = + {SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGABRT, SIGFPE, SIGSEGV, + SIGALRM, SIGTERM, SIGCHLD, SIGBUS, SIGTRAP}; // SIGPIPE is set below. + for (unsigned i = 0; i < arraysize(signals_to_reset); i++) { + CHECK(0 == sigaction(signals_to_reset[i], &sigact, NULL)); + } + + // Always ignore SIGPIPE. We check the return value of write(). + CHECK(signal(SIGPIPE, SIG_IGN) != SIG_ERR); +} + +#endif // OS_WIN + +} // namespace + +namespace content { + +#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; + + RegisterInvalidParamHandler(); + _Module.Init(NULL, static_cast<HINSTANCE>(instance)); +#else +int ContentMain(int argc, + 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. +#if !defined(OS_MACOSX) && defined(USE_TCMALLOC) + // For tcmalloc, we need to tell it to behave like new. + tc_set_new_mode(1); +#endif + + // Set C library locale to make sure CommandLine can parse argument values + // in correct encoding. + setlocale(LC_ALL, ""); + + SetupSignalHandlers(); + + base::GlobalDescriptors* g_fds = base::GlobalDescriptors::GetInstance(); + g_fds->Set(kPrimaryIPCChannel, + kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor); +#if defined(OS_LINUX) + g_fds->Set(kCrashDumpSignal, + kCrashDumpSignal + base::GlobalDescriptors::kBaseDescriptor); +#endif + +#endif // OS_WIN + + base::EnableTerminationOnHeapCorruption(); + base::EnableTerminationOnOutOfMemory(); + + // The exit manager is in charge of calling the dtors of singleton objects. + g_exit_manager = new base::AtExitManager(); + + // 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. + g_autorelease_pool = new base::mac::ScopedNSAutoreleasePool(); + + CommandLine::Init(argc, argv); + + int exit_code; + if (delegate && + delegate->BasicStartupComplete(&exit_code, g_autorelease_pool)) + return exit_code; + + // Temporary + return 0; +} + +void ContentMainEnd() { +#if defined(OS_WIN) +#ifdef _CRTDBG_MAP_ALLOC + _CrtDumpMemoryLeaks(); +#endif // _CRTDBG_MAP_ALLOC + + _Module.Term(); + + delete g_autorelease_pool; + delete g_exit_manager; +#endif // OS_WIN +} + +} // namespace content diff --git a/content/app/content_main.h b/content/app/content_main.h new file mode 100644 index 0000000..285fc6a --- /dev/null +++ b/content/app/content_main.h @@ -0,0 +1,45 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CONTENT_APP_CONTENT_MAIN_H_ +#define CONTENT_APP_CONTENT_MAIN_H_ +#pragma once + +#include "build/build_config.h" + +#if defined(OS_WIN) +#include <windows.h> +#endif + +namespace sandbox { +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. +#if defined(OS_WIN) + +// |sandbox_info| should be initialized using InitializeSandboxInfo from +// content_main_win.h +int ContentMain(HINSTANCE instance, + sandbox::SandboxInterfaceInfo* sandbox_info, + ContentMainDelegate* delegate); +#else +int ContentMain(int argc, + char** argv, + ContentMainDelegate* delegate); +#endif + +// Temporary function so that we can move code from chrome in stages. +void ContentMainEnd(); + +} // namespace content + +#endif // CONTENT_APP_CONTENT_MAIN_H_ diff --git a/content/app/content_main_delegate.h b/content/app/content_main_delegate.h new file mode 100644 index 0000000..78b84dc --- /dev/null +++ b/content/app/content_main_delegate.h @@ -0,0 +1,29 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CONTENT_APP_CONTENT_MAIN_DELEGATE_H_ +#define CONTENT_APP_CONTENT_MAIN_DELEGATE_H_ +#pragma once + +namespace base { +namespace mac { +class ScopedNSAutoreleasePool; +} +} + +namespace content { + +class ContentMainDelegate { + public: + // Tells the embedder that the absolute basic startup has been done, i.e. it's + // now safe to create singeltons and check the command line. Return true if + // the process should exit afterwards, and if so, |exit_code| should be set. + virtual bool BasicStartupComplete( + int* exit_code, + base::mac::ScopedNSAutoreleasePool* autorelease_pool) = 0; +}; + +} // namespace content + +#endif // CONTENT_APP_CONTENT_MAIN_DELEGATE_H_ diff --git a/content/app/sandbox_helper_win.cc b/content/app/sandbox_helper_win.cc new file mode 100644 index 0000000..a817e08 --- /dev/null +++ b/content/app/sandbox_helper_win.cc @@ -0,0 +1,24 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// 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 "base/win/windows_version.h" +#include "sandbox/src/dep.h" +#include "sandbox/src/sandbox_factory.h" + +namespace content { + +void InitializeSandboxInfo(sandbox::SandboxInterfaceInfo* info) { + info->broker_services = sandbox::SandboxFactory::GetBrokerServices(); + if (!info->broker_services) + info->target_services = sandbox::SandboxFactory::GetTargetServices(); + + if (base::win::GetVersion() < base::win::VERSION_VISTA) { + // Enforces strong DEP support. Vista uses the NXCOMPAT flag in the exe. + sandbox::SetCurrentProcessDEP(sandbox::DEP_ENABLED); + } +} + +} // namespace content diff --git a/content/app/sandbox_helper_win.h b/content/app/sandbox_helper_win.h new file mode 100644 index 0000000..4fc736d --- /dev/null +++ b/content/app/sandbox_helper_win.h @@ -0,0 +1,20 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CONTENT_APP_SANDBOX_HELPER_WIN_H_ +#define CONTENT_APP_SANDBOX_HELPER_WIN_H_ +#pragma once + +namespace sandbox { +struct SandboxInterfaceInfo; +} + +namespace content { + +// Initializes the sandbox code and turns on DEP. +void InitializeSandboxInfo(sandbox::SandboxInterfaceInfo* sandbox_info); + +} // namespace content + +#endif // CONTENT_APP_SANDBOX_HELPER_WIN_H_ diff --git a/content/content.gyp b/content/content.gyp index 172a790..bb977db 100644 --- a/content/content.gyp +++ b/content/content.gyp @@ -7,6 +7,7 @@ 'chromium_code': 1, # Use higher warning level. }, 'includes': [ + 'content_app.gypi', 'content_browser.gypi', 'content_common.gypi', 'content_gpu.gypi', diff --git a/content/content_app.gypi b/content/content_app.gypi new file mode 100644 index 0000000..e87b81e --- /dev/null +++ b/content/content_app.gypi @@ -0,0 +1,33 @@ +# Copyright (c) 2011 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +{ + 'targets': [ + { + 'target_name': 'content_app', + 'type': 'static_library', + 'include_dirs': [ + '..', + ], + 'dependencies': [ + 'content_common', + '../base/base.gyp:base', + ], + 'sources': [ + 'app/content_main.cc', + 'app/content_main.h', + 'app/content_main_delegate.h', + 'app/sandbox_helper_win.cc', + 'app/sandbox_helper_win.h', + ], + 'conditions': [ + ['OS=="win"', { + 'dependencies': [ + '../sandbox/sandbox.gyp:sandbox', + ], + }], + ], + }, + ], +} |