| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // TODO(port): the ifdefs in here are a first step towards trying to determine | 5 // TODO(port): the ifdefs in here are a first step towards trying to determine |
| 6 // the correct abstraction for all the OS functionality required at this | 6 // the correct abstraction for all the OS functionality required at this |
| 7 // stage of process initialization. It should not be taken as a final | 7 // stage of process initialization. It should not be taken as a final |
| 8 // abstraction. | 8 // abstraction. |
| 9 | 9 |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 11 | 11 |
| 12 #if defined(OS_WIN) | 12 #if defined(OS_WIN) |
| 13 #include <atlbase.h> | 13 #include <atlbase.h> |
| 14 #include <atlapp.h> | 14 #include <atlapp.h> |
| 15 #include <malloc.h> | 15 #include <malloc.h> |
| 16 #include <new.h> | 16 #include <new.h> |
| 17 #elif defined(OS_MACOSX) | |
| 18 extern "C" { | |
| 19 #include <sandbox.h> | |
| 20 } | |
| 21 #endif | 17 #endif |
| 22 | 18 |
| 23 #include "base/at_exit.h" | 19 #include "base/at_exit.h" |
| 24 #include "base/command_line.h" | 20 #include "base/command_line.h" |
| 25 #include "base/icu_util.h" | 21 #include "base/icu_util.h" |
| 26 #include "base/message_loop.h" | 22 #include "base/message_loop.h" |
| 27 #include "base/path_service.h" | 23 #include "base/path_service.h" |
| 28 #include "base/process_util.h" | 24 #include "base/process_util.h" |
| 29 #include "base/stats_table.h" | 25 #include "base/stats_table.h" |
| 30 #include "base/string_util.h" | 26 #include "base/string_util.h" |
| 31 #if defined(OS_WIN) | 27 #if defined(OS_WIN) |
| 32 #include "base/win_util.h" | 28 #include "base/win_util.h" |
| 33 #include "chrome/browser/render_process_host.h" | 29 #include "chrome/browser/render_process_host.h" |
| 34 #endif | 30 #endif |
| 31 #include "chrome/app/scoped_ole_initializer.h" |
| 35 #include "chrome/common/chrome_constants.h" | 32 #include "chrome/common/chrome_constants.h" |
| 36 #include "chrome/common/chrome_counters.h" | 33 #include "chrome/common/chrome_counters.h" |
| 37 #include "chrome/common/chrome_paths.h" | 34 #include "chrome/common/chrome_paths.h" |
| 38 #include "chrome/common/chrome_switches.h" | 35 #include "chrome/common/chrome_switches.h" |
| 39 #include "chrome/common/logging_chrome.h" | 36 #include "chrome/common/logging_chrome.h" |
| 37 #include "chrome/common/main_function_params.h" |
| 40 #if defined(OS_WIN) | 38 #if defined(OS_WIN) |
| 41 #include "chrome/common/resource_bundle.h" | 39 #include "chrome/common/resource_bundle.h" |
| 42 #endif | 40 #endif |
| 41 #include "chrome/common/sandbox_init_wrapper.h" |
| 42 #if defined(OS_WIN) |
| 43 #include "sandbox/src/sandbox.h" | 43 #include "sandbox/src/sandbox.h" |
| 44 #if defined(OS_WIN) | |
| 45 #include "tools/memory_watcher/memory_watcher.h" | 44 #include "tools/memory_watcher/memory_watcher.h" |
| 46 #endif | 45 #endif |
| 47 | 46 |
| 48 extern int BrowserMain(CommandLine&, sandbox::BrokerServices*); | 47 extern int BrowserMain(const MainFunctionParams&); |
| 49 extern int RendererMain(CommandLine&, sandbox::TargetServices*); | 48 extern int RendererMain(const MainFunctionParams&); |
| 50 extern int PluginMain(CommandLine&, sandbox::TargetServices*); | 49 extern int PluginMain(const MainFunctionParams&); |
| 51 | 50 |
| 52 #if defined(OS_WIN) | 51 #if defined(OS_WIN) |
| 53 // TODO(erikkay): isn't this already defined somewhere? | 52 // TODO(erikkay): isn't this already defined somewhere? |
| 54 #define DLLEXPORT __declspec(dllexport) | 53 #define DLLEXPORT __declspec(dllexport) |
| 55 | 54 |
| 56 // We use extern C for the prototype DLLEXPORT to avoid C++ name mangling. | 55 // We use extern C for the prototype DLLEXPORT to avoid C++ name mangling. |
| 57 extern "C" { | 56 extern "C" { |
| 58 DLLEXPORT int __cdecl ChromeMain(HINSTANCE instance, | 57 DLLEXPORT int __cdecl ChromeMain(HINSTANCE instance, |
| 59 sandbox::SandboxInterfaceInfo* sandbox_info, | 58 sandbox::SandboxInterfaceInfo* sandbox_info, |
| 60 TCHAR* command_line); | 59 TCHAR* command_line); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 DumpProcessFunction DumpProcess = reinterpret_cast<DumpProcessFunction>( | 107 DumpProcessFunction DumpProcess = reinterpret_cast<DumpProcessFunction>( |
| 109 ::GetProcAddress(::GetModuleHandle(L"chrome.exe"), "DumpProcess")); | 108 ::GetProcAddress(::GetModuleHandle(L"chrome.exe"), "DumpProcess")); |
| 110 if (DumpProcess) | 109 if (DumpProcess) |
| 111 DumpProcess(); | 110 DumpProcess(); |
| 112 } | 111 } |
| 113 | 112 |
| 114 #pragma optimize("", on) | 113 #pragma optimize("", on) |
| 115 | 114 |
| 116 #endif // OS_WIN | 115 #endif // OS_WIN |
| 117 | 116 |
| 118 // Called before/after the call to BrowseMain() to handle platform-specific | |
| 119 // setup/teardown. | |
| 120 void PreBrowserMain() { | |
| 121 #if defined(OS_WIN) | |
| 122 int ole_result = OleInitialize(NULL); | |
| 123 DCHECK(ole_result == S_OK); | |
| 124 #endif | |
| 125 } | |
| 126 | |
| 127 void PostBrowserMain() { | |
| 128 #if defined(OS_WIN) | |
| 129 OleUninitialize(); | |
| 130 #endif | |
| 131 } | |
| 132 | |
| 133 // Register the invalid param handler and pure call handler to be able to | 117 // Register the invalid param handler and pure call handler to be able to |
| 134 // notify breakpad when it happens. | 118 // notify breakpad when it happens. |
| 135 void RegisterInvalidParamHandler() { | 119 void RegisterInvalidParamHandler() { |
| 136 #if defined(OS_WIN) | 120 #if defined(OS_WIN) |
| 137 _set_invalid_parameter_handler(InvalidParameter); | 121 _set_invalid_parameter_handler(InvalidParameter); |
| 138 _set_purecall_handler(PureCall); | 122 _set_purecall_handler(PureCall); |
| 139 // Gather allocation failure. | 123 // Gather allocation failure. |
| 140 _set_new_handler(&OnNoMemory); | 124 _set_new_handler(&OnNoMemory); |
| 141 // Make sure malloc() calls the new handler too. | 125 // Make sure malloc() calls the new handler too. |
| 142 _set_new_mode(1); | 126 _set_new_mode(1); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 173 // Enable the heap profiler if the appropriate command-line switch is | 157 // Enable the heap profiler if the appropriate command-line switch is |
| 174 // present, bailing out of the app we can't. | 158 // present, bailing out of the app we can't. |
| 175 void EnableHeapProfiler(const CommandLine& parsed_command_line) { | 159 void EnableHeapProfiler(const CommandLine& parsed_command_line) { |
| 176 #if defined(OS_WIN) | 160 #if defined(OS_WIN) |
| 177 if (parsed_command_line.HasSwitch(switches::kMemoryProfiling)) | 161 if (parsed_command_line.HasSwitch(switches::kMemoryProfiling)) |
| 178 if (!LoadMemoryProfiler()) | 162 if (!LoadMemoryProfiler()) |
| 179 exit(-1); | 163 exit(-1); |
| 180 #endif | 164 #endif |
| 181 } | 165 } |
| 182 | 166 |
| 183 // Checks if the sandbox is enabled in this process and initializes it if this | |
| 184 // is the case. Sandboxing is only valid on render and plugin processes. It's | |
| 185 // meaningless for the browser process. | |
| 186 void InitializeSandbox(const CommandLine& parsed_command_line, | |
| 187 const std::wstring process_type, | |
| 188 sandbox::BrokerServices* broker_services, | |
| 189 sandbox::TargetServices* target_services) { | |
| 190 if (target_services && !parsed_command_line.HasSwitch(switches::kNoSandbox)) { | |
| 191 if ((process_type == switches::kRendererProcess) || | |
| 192 (process_type == switches::kPluginProcess && | |
| 193 parsed_command_line.HasSwitch(switches::kSafePlugins))) { | |
| 194 #if defined(OS_WIN) | |
| 195 target_services->Init(); | |
| 196 #elif defined(OS_MACOSX) | |
| 197 // TODO(pinkerton): note, this leaks |error_buff|. What do we want to | |
| 198 // do with the error? Pass it back to main? | |
| 199 char* error_buff; | |
| 200 int error = sandbox_init(kSBXProfilePureComputation, SANDBOX_NAMED, | |
| 201 &error_buff); | |
| 202 if (error) | |
| 203 exit(-1); | |
| 204 #endif | |
| 205 } | |
| 206 } | |
| 207 } | |
| 208 | |
| 209 void CommonSubprocessInit() { | 167 void CommonSubprocessInit() { |
| 210 #if defined(OS_WIN) | 168 #if defined(OS_WIN) |
| 211 // Initialize ResourceBundle which handles files loaded from external | 169 // Initialize ResourceBundle which handles files loaded from external |
| 212 // sources. The language should have been passed in to us from the | 170 // sources. The language should have been passed in to us from the |
| 213 // browser process as a command line flag. | 171 // browser process as a command line flag. |
| 214 // TODO(port): enable when we figure out resource bundle issues | 172 // TODO(port): enable when we figure out resource bundle issues |
| 215 ResourceBundle::InitSharedInstance(std::wstring()); | 173 ResourceBundle::InitSharedInstance(std::wstring()); |
| 216 | 174 |
| 217 // HACK: Let Windows know that we have started. This is needed to suppress | 175 // HACK: Let Windows know that we have started. This is needed to suppress |
| 218 // the IDC_APPSTARTING cursor from being displayed for a prolonged period | 176 // the IDC_APPSTARTING cursor from being displayed for a prolonged period |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 | 225 |
| 268 // Enable the heap profiler as early as possible! | 226 // Enable the heap profiler as early as possible! |
| 269 EnableHeapProfiler(parsed_command_line); | 227 EnableHeapProfiler(parsed_command_line); |
| 270 | 228 |
| 271 // Enable Message Loop related state asap. | 229 // Enable Message Loop related state asap. |
| 272 if (parsed_command_line.HasSwitch(switches::kMessageLoopHistogrammer)) | 230 if (parsed_command_line.HasSwitch(switches::kMessageLoopHistogrammer)) |
| 273 MessageLoop::EnableHistogrammer(true); | 231 MessageLoop::EnableHistogrammer(true); |
| 274 | 232 |
| 275 std::wstring process_type = | 233 std::wstring process_type = |
| 276 parsed_command_line.GetSwitchValue(switches::kProcessType); | 234 parsed_command_line.GetSwitchValue(switches::kProcessType); |
| 277 | 235 |
| 278 sandbox::BrokerServices* broker_services = NULL; | |
| 279 sandbox::TargetServices* target_services = NULL; | |
| 280 #if defined(OS_WIN) | |
| 281 if (sandbox_info) { | |
| 282 target_services = sandbox_info->target_services; | |
| 283 broker_services = sandbox_info->broker_services; | |
| 284 } | |
| 285 #endif | |
| 286 | |
| 287 // Checks if the sandbox is enabled in this process and initializes it if this | 236 // Checks if the sandbox is enabled in this process and initializes it if this |
| 288 // is the case. The crash handler depends on this so it has to be done before | 237 // is the case. The crash handler depends on this so it has to be done before |
| 289 // its initialization. | 238 // its initialization. |
| 290 InitializeSandbox(parsed_command_line, process_type, broker_services, | 239 SandboxInitWrapper sandbox_wrapper; |
| 291 target_services); | 240 #if defined(OS_WIN) |
| 292 | 241 sandbox_wrapper.SetServices(sandbox_info); |
| 242 #endif |
| 243 sandbox_wrapper.InitializeSandbox(parsed_command_line, process_type); |
| 244 |
| 293 #if defined(OS_WIN) | 245 #if defined(OS_WIN) |
| 294 _Module.Init(NULL, instance); | 246 _Module.Init(NULL, instance); |
| 295 #endif | 247 #endif |
| 296 | 248 |
| 297 // Notice a user data directory override if any | 249 // Notice a user data directory override if any |
| 298 const std::wstring user_data_dir = | 250 const std::wstring user_data_dir = |
| 299 parsed_command_line.GetSwitchValue(switches::kUserDataDir); | 251 parsed_command_line.GetSwitchValue(switches::kUserDataDir); |
| 300 if (!user_data_dir.empty()) | 252 if (!user_data_dir.empty()) |
| 301 PathService::Override(chrome::DIR_USER_DATA, user_data_dir); | 253 PathService::Override(chrome::DIR_USER_DATA, user_data_dir); |
| 302 | 254 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 326 logging::SetLogAssertHandler(ChromeAssert); | 278 logging::SetLogAssertHandler(ChromeAssert); |
| 327 #endif | 279 #endif |
| 328 } | 280 } |
| 329 #endif // NDEBUG | 281 #endif // NDEBUG |
| 330 | 282 |
| 331 if (!process_type.empty()) | 283 if (!process_type.empty()) |
| 332 CommonSubprocessInit(); | 284 CommonSubprocessInit(); |
| 333 | 285 |
| 334 startup_timer.Stop(); // End of Startup Time Measurement. | 286 startup_timer.Stop(); // End of Startup Time Measurement. |
| 335 | 287 |
| 288 MainFunctionParams main_params(parsed_command_line, sandbox_wrapper); |
| 289 |
| 336 // TODO(port): turn on these main() functions as they've been de-winified. | 290 // TODO(port): turn on these main() functions as they've been de-winified. |
| 337 int rv = -1; | 291 int rv = -1; |
| 338 if (process_type == switches::kRendererProcess) { | 292 if (process_type == switches::kRendererProcess) { |
| 339 #if defined(OS_WIN) | 293 #if defined(OS_WIN) |
| 340 rv = RendererMain(parsed_command_line, target_services); | 294 rv = RendererMain(main_params); |
| 341 #endif | 295 #endif |
| 342 } else if (process_type == switches::kPluginProcess) { | 296 } else if (process_type == switches::kPluginProcess) { |
| 343 #if defined(OS_WIN) | 297 #if defined(OS_WIN) |
| 344 rv = PluginMain(parsed_command_line, target_services); | 298 rv = PluginMain(main_params); |
| 345 #endif | 299 #endif |
| 346 } else if (process_type.empty()) { | 300 } else if (process_type.empty()) { |
| 347 PreBrowserMain(); | 301 ScopedOleInitializer ole_initializer; |
| 348 rv = BrowserMain(parsed_command_line, broker_services); | 302 rv = BrowserMain(main_params); |
| 349 PostBrowserMain(); | |
| 350 } else { | 303 } else { |
| 351 NOTREACHED() << "Unknown process type"; | 304 NOTREACHED() << "Unknown process type"; |
| 352 } | 305 } |
| 353 | 306 |
| 354 // TODO(pinkerton): nothing after this point will be hit on Mac if this is the | 307 // TODO(pinkerton): nothing after this point will be hit on Mac if this is the |
| 355 // browser process, as NSApplicationMain doesn't return. There are a couple of | 308 // browser process, as NSApplicationMain doesn't return. There are a couple of |
| 356 // possible solutions, including breaking this up into pre/post code (won't | 309 // possible solutions, including breaking this up into pre/post code (won't |
| 357 // work for stack-based objects) or making sure we fall out of the runloop | 310 // work for stack-based objects) or making sure we fall out of the runloop |
| 358 // ourselves, then manually call |-NSApp terminate:|. We'll most likely | 311 // ourselves, then manually call |-NSApp terminate:|. We'll most likely |
| 359 // need to do the latter. | 312 // need to do the latter. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 372 | 325 |
| 373 _Module.Term(); | 326 _Module.Term(); |
| 374 #endif | 327 #endif |
| 375 | 328 |
| 376 logging::CleanupChromeLogging(); | 329 logging::CleanupChromeLogging(); |
| 377 | 330 |
| 378 return rv; | 331 return rv; |
| 379 } | 332 } |
| 380 | 333 |
| 381 | 334 |
| OLD | NEW |