OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "content/app/content_main.h" | 5 #include "content/public/app/content_main_runner.h" |
6 | 6 |
7 #include "base/at_exit.h" | 7 #include "base/at_exit.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/debug/debugger.h" | 9 #include "base/debug/debugger.h" |
10 #include "base/debug/trace_event.h" | 10 #include "base/debug/trace_event.h" |
11 #include "base/i18n/icu_util.h" | 11 #include "base/i18n/icu_util.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/metrics/stats_table.h" | 14 #include "base/metrics/stats_table.h" |
15 #include "base/process_util.h" | 15 #include "base/process_util.h" |
16 #include "base/stringprintf.h" | 16 #include "base/stringprintf.h" |
17 #include "base/string_number_conversions.h" | 17 #include "base/string_number_conversions.h" |
18 #include "content/browser/browser_main.h" | 18 #include "content/browser/browser_main.h" |
19 #include "content/common/set_process_title.h" | 19 #include "content/common/set_process_title.h" |
20 #include "content/public/app/content_main_delegate.h" | 20 #include "content/public/app/content_main_delegate.h" |
21 #include "content/public/app/startup_helper_win.h" | 21 #include "content/public/app/startup_helper_win.h" |
22 #include "content/public/common/content_client.h" | 22 #include "content/public/common/content_client.h" |
23 #include "content/public/common/content_constants.h" | 23 #include "content/public/common/content_constants.h" |
24 #include "content/public/common/content_paths.h" | 24 #include "content/public/common/content_paths.h" |
25 #include "content/public/common/content_switches.h" | 25 #include "content/public/common/content_switches.h" |
26 #include "content/public/common/main_function_params.h" | 26 #include "content/public/common/main_function_params.h" |
27 #include "content/public/common/sandbox_init.h" | 27 #include "content/public/common/sandbox_init.h" |
28 #include "crypto/nss_util.h" | 28 #include "crypto/nss_util.h" |
29 #include "ipc/ipc_switches.h" | 29 #include "ipc/ipc_switches.h" |
30 #include "sandbox/src/sandbox_types.h" | |
30 #include "ui/base/ui_base_switches.h" | 31 #include "ui/base/ui_base_switches.h" |
31 #include "ui/base/ui_base_paths.h" | 32 #include "ui/base/ui_base_paths.h" |
32 #include "webkit/glue/webkit_glue.h" | 33 #include "webkit/glue/webkit_glue.h" |
33 | 34 |
34 #if defined(OS_WIN) | 35 #if defined(OS_WIN) |
35 #include <atlbase.h> | 36 #include <atlbase.h> |
36 #include <atlapp.h> | 37 #include <atlapp.h> |
37 #include <malloc.h> | 38 #include <malloc.h> |
38 #elif defined(OS_MACOSX) | 39 #elif defined(OS_MACOSX) |
39 #include "base/mac/scoped_nsautorelease_pool.h" | 40 #include "base/mac/scoped_nsautorelease_pool.h" |
(...skipping 28 matching lines...) Expand all Loading... | |
68 extern int RendererMain(const content::MainFunctionParams&); | 69 extern int RendererMain(const content::MainFunctionParams&); |
69 extern int WorkerMain(const content::MainFunctionParams&); | 70 extern int WorkerMain(const content::MainFunctionParams&); |
70 extern int UtilityMain(const content::MainFunctionParams&); | 71 extern int UtilityMain(const content::MainFunctionParams&); |
71 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 72 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
72 extern int ZygoteMain(const content::MainFunctionParams&, | 73 extern int ZygoteMain(const content::MainFunctionParams&, |
73 content::ZygoteForkDelegate* forkdelegate); | 74 content::ZygoteForkDelegate* forkdelegate); |
74 #endif | 75 #endif |
75 | 76 |
76 namespace { | 77 namespace { |
77 | 78 |
78 #if defined(OS_WIN) | 79 #if defined(OS_MACOSX) |
79 | |
80 static CAppModule _Module; | |
81 | |
82 #elif defined(OS_MACOSX) | |
83 | 80 |
84 // Completes the Mach IPC handshake by sending this process' task port to the | 81 // Completes the Mach IPC handshake by sending this process' task port to the |
85 // parent process. The parent is listening on the Mach port given by | 82 // parent process. The parent is listening on the Mach port given by |
86 // |GetMachPortName()|. The task port is used by the parent to get CPU/memory | 83 // |GetMachPortName()|. The task port is used by the parent to get CPU/memory |
87 // stats to display in the task manager. | 84 // stats to display in the task manager. |
88 void SendTaskPortToParentProcess() { | 85 void SendTaskPortToParentProcess() { |
89 const mach_msg_timeout_t kTimeoutMs = 100; | 86 const mach_msg_timeout_t kTimeoutMs = 100; |
90 const int32_t kMessageId = 0; | 87 const int32_t kMessageId = 0; |
91 std::string mach_port_name = MachBroker::GetMachPortName(); | 88 std::string mach_port_name = MachBroker::GetMachPortName(); |
92 | 89 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
134 | 131 |
135 void CommonSubprocessInit(const std::string& process_type) { | 132 void CommonSubprocessInit(const std::string& process_type) { |
136 #if defined(OS_WIN) | 133 #if defined(OS_WIN) |
137 // HACK: Let Windows know that we have started. This is needed to suppress | 134 // HACK: Let Windows know that we have started. This is needed to suppress |
138 // the IDC_APPSTARTING cursor from being displayed for a prolonged period | 135 // the IDC_APPSTARTING cursor from being displayed for a prolonged period |
139 // while a subprocess is starting. | 136 // while a subprocess is starting. |
140 PostThreadMessage(GetCurrentThreadId(), WM_NULL, 0, 0); | 137 PostThreadMessage(GetCurrentThreadId(), WM_NULL, 0, 0); |
141 MSG msg; | 138 MSG msg; |
142 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE); | 139 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE); |
143 #endif | 140 #endif |
144 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 141 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
145 // Various things break when you're using a locale where the decimal | 142 // Various things break when you're using a locale where the decimal |
146 // separator isn't a period. See e.g. bugs 22782 and 39964. For | 143 // separator isn't a period. See e.g. bugs 22782 and 39964. For |
147 // all processes except the browser process (where we call system | 144 // all processes except the browser process (where we call system |
148 // APIs that may rely on the correct locale for formatting numbers | 145 // APIs that may rely on the correct locale for formatting numbers |
149 // when presenting them to the user), reset the locale for numeric | 146 // when presenting them to the user), reset the locale for numeric |
150 // formatting. | 147 // formatting. |
151 // Note that this is not correct for plugin processes -- they can | 148 // Note that this is not correct for plugin processes -- they can |
152 // surface UI -- but it's likely they get this wrong too so why not. | 149 // surface UI -- but it's likely they get this wrong too so why not. |
153 setlocale(LC_NUMERIC, "C"); | 150 setlocale(LC_NUMERIC, "C"); |
154 #endif | 151 #endif |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
253 { switches::kRendererProcess, RendererMain }, | 250 { switches::kRendererProcess, RendererMain }, |
254 { switches::kPluginProcess, PluginMain }, | 251 { switches::kPluginProcess, PluginMain }, |
255 { switches::kWorkerProcess, WorkerMain }, | 252 { switches::kWorkerProcess, WorkerMain }, |
256 { switches::kPpapiPluginProcess, PpapiPluginMain }, | 253 { switches::kPpapiPluginProcess, PpapiPluginMain }, |
257 { switches::kPpapiBrokerProcess, PpapiBrokerMain }, | 254 { switches::kPpapiBrokerProcess, PpapiBrokerMain }, |
258 { switches::kUtilityProcess, UtilityMain }, | 255 { switches::kUtilityProcess, UtilityMain }, |
259 { switches::kGpuProcess, GpuMain }, | 256 { switches::kGpuProcess, GpuMain }, |
260 }; | 257 }; |
261 | 258 |
262 for (size_t i = 0; i < arraysize(kMainFunctions); ++i) { | 259 for (size_t i = 0; i < arraysize(kMainFunctions); ++i) { |
263 if (process_type == kMainFunctions[i].name) | 260 if (process_type == kMainFunctions[i].name) { |
261 if (delegate) { | |
262 int exit_code = delegate->RunProcess(process_type, | |
263 main_function_params); | |
264 if (exit_code >= 0) | |
265 return exit_code; | |
266 } | |
264 return kMainFunctions[i].function(main_function_params); | 267 return kMainFunctions[i].function(main_function_params); |
268 } | |
265 } | 269 } |
266 | 270 |
267 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 271 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
268 // Zygote startup is special -- see RunZygote comments above | 272 // Zygote startup is special -- see RunZygote comments above |
269 // for why we don't use ZygoteMain directly. | 273 // for why we don't use ZygoteMain directly. |
270 if (process_type == switches::kZygoteProcess) | 274 if (process_type == switches::kZygoteProcess) |
271 return RunZygote(main_function_params, delegate); | 275 return RunZygote(main_function_params, delegate); |
272 #endif | 276 #endif |
273 | 277 |
274 // If it's a process we don't know about, the embedder should know. | 278 // If it's a process we don't know about, the embedder should know. |
275 if (delegate) | 279 if (delegate) |
276 return delegate->RunProcess(process_type, main_function_params); | 280 return delegate->RunProcess(process_type, main_function_params); |
277 | 281 |
278 NOTREACHED() << "Unknown process type: " << process_type; | 282 NOTREACHED() << "Unknown process type: " << process_type; |
279 return 1; | 283 return 1; |
280 } | 284 } |
281 | 285 |
282 } // namespace | 286 class ContentMainRunnerImpl : public content::ContentMainRunner { |
283 | 287 public: |
284 namespace content { | 288 ContentMainRunnerImpl(); |
jam
2012/01/31 21:11:29
since this class is only in the cc file, there's n
| |
289 ~ContentMainRunnerImpl(); | |
285 | 290 |
286 #if defined(OS_WIN) | 291 #if defined(OS_WIN) |
287 int ContentMain(HINSTANCE instance, | 292 virtual int Initialize(HINSTANCE instance, |
288 sandbox::SandboxInterfaceInfo* sandbox_info, | 293 sandbox::SandboxInterfaceInfo* sandbox_info, |
289 ContentMainDelegate* delegate) { | 294 content::ContentMainDelegate* delegate) OVERRIDE; |
295 #else | |
296 virtual int Initialize(int argc, | |
297 const char** argv, | |
298 content::ContentMainDelegate* delegate) OVERRIDE; | |
299 #endif | |
300 virtual int Run() OVERRIDE; | |
301 virtual void Shutdown() OVERRIDE; | |
302 | |
303 protected: | |
304 // True if the runner has been initialized. | |
305 bool is_initialized_; | |
306 | |
307 // True if the runner has been shut down. | |
308 bool is_shutdown_; | |
309 | |
310 // The delegate will outlive this object. | |
311 content::ContentMainDelegate* delegate_; | |
312 | |
313 scoped_ptr<base::AtExitManager> exit_manager_; | |
314 #if defined(OS_WIN) | |
315 sandbox::SandboxInterfaceInfo sandbox_info_; | |
316 scoped_ptr<CAppModule> app_module_; | |
317 #elif defined(OS_MACOSX) | |
318 scoped_ptr<base::mac::ScopedNSAutoreleasePool> autorelease_pool_; | |
319 #endif | |
320 | |
321 DISALLOW_COPY_AND_ASSIGN(ContentMainRunnerImpl); | |
322 }; | |
323 | |
324 ContentMainRunnerImpl::ContentMainRunnerImpl() | |
325 : is_initialized_(false), | |
326 is_shutdown_(false) { | |
327 } | |
328 | |
329 ContentMainRunnerImpl::~ContentMainRunnerImpl() { | |
330 if (is_initialized_ && !is_shutdown_) | |
331 Shutdown(); | |
332 } | |
333 | |
334 #if defined(OS_WIN) | |
335 int ContentMainRunnerImpl::Initialize( | |
336 HINSTANCE instance, | |
337 sandbox::SandboxInterfaceInfo* sandbox_info, | |
338 content::ContentMainDelegate* delegate) { | |
339 is_initialized_ = true; | |
340 | |
290 // argc/argv are ignored on Windows; see command_line.h for details. | 341 // argc/argv are ignored on Windows; see command_line.h for details. |
291 int argc = 0; | 342 int argc = 0; |
292 char** argv = NULL; | 343 char** argv = NULL; |
293 | 344 |
294 content::RegisterInvalidParamHandler(); | 345 content::RegisterInvalidParamHandler(); |
295 _Module.Init(NULL, static_cast<HINSTANCE>(instance)); | 346 app_module_.reset(new CAppModule); |
347 app_module_->Init(NULL, static_cast<HINSTANCE>(instance)); | |
348 | |
349 if (sandbox_info) | |
350 sandbox_info_ = *sandbox_info; | |
351 else | |
352 memset(&sandbox_info_, 0, sizeof(sandbox_info_)); | |
353 | |
296 #else | 354 #else |
297 int ContentMain(int argc, | 355 int ContentMainRunnerImpl::Initialize( |
298 const char** argv, | 356 int argc, |
299 ContentMainDelegate* delegate) { | 357 const char** argv, |
358 content::ContentMainDelegate* delegate) { | |
359 is_initialized_ = true; | |
360 | |
300 // NOTE(willchan): One might ask why this call is done here rather than in | 361 // NOTE(willchan): One might ask why this call is done here rather than in |
301 // process_util_linux.cc with the definition of | 362 // process_util_linux.cc with the definition of |
302 // EnableTerminationOnOutOfMemory(). That's because base shouldn't have a | 363 // EnableTerminationOnOutOfMemory(). That's because base shouldn't have a |
303 // dependency on TCMalloc. Really, we ought to have our allocator shim code | 364 // dependency on TCMalloc. Really, we ought to have our allocator shim code |
304 // implement this EnableTerminationOnOutOfMemory() function. Whateverz. This | 365 // implement this EnableTerminationOnOutOfMemory() function. Whateverz. This |
305 // works for now. | 366 // works for now. |
306 #if !defined(OS_MACOSX) && defined(USE_TCMALLOC) | 367 #if !defined(OS_MACOSX) && defined(USE_TCMALLOC) |
307 // For tcmalloc, we need to tell it to behave like new. | 368 // For tcmalloc, we need to tell it to behave like new. |
308 tc_set_new_mode(1); | 369 tc_set_new_mode(1); |
309 #endif | 370 #endif |
310 | 371 |
372 #if !defined(OS_ANDROID) | |
jam
2012/01/31 21:11:29
merge issue?
| |
311 // Set C library locale to make sure CommandLine can parse argument values | 373 // Set C library locale to make sure CommandLine can parse argument values |
312 // in correct encoding. | 374 // in correct encoding. |
313 setlocale(LC_ALL, ""); | 375 setlocale(LC_ALL, ""); |
376 #endif | |
314 | 377 |
315 SetupSignalHandlers(); | 378 SetupSignalHandlers(); |
316 | 379 |
317 base::GlobalDescriptors* g_fds = base::GlobalDescriptors::GetInstance(); | 380 base::GlobalDescriptors* g_fds = base::GlobalDescriptors::GetInstance(); |
318 g_fds->Set(kPrimaryIPCChannel, | 381 g_fds->Set(kPrimaryIPCChannel, |
319 kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor); | 382 kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor); |
320 #if defined(OS_LINUX) || defined(OS_OPENBSD) | 383 #if defined(OS_LINUX) || defined(OS_OPENBSD) |
321 g_fds->Set(kCrashDumpSignal, | 384 g_fds->Set(kCrashDumpSignal, |
322 kCrashDumpSignal + base::GlobalDescriptors::kBaseDescriptor); | 385 kCrashDumpSignal + base::GlobalDescriptors::kBaseDescriptor); |
323 #endif | 386 #endif |
324 | 387 |
325 #endif // OS_WIN | 388 #endif // OS_WIN |
326 | 389 |
390 delegate_ = delegate; | |
391 | |
327 base::EnableTerminationOnHeapCorruption(); | 392 base::EnableTerminationOnHeapCorruption(); |
328 base::EnableTerminationOnOutOfMemory(); | 393 base::EnableTerminationOnOutOfMemory(); |
329 | 394 |
330 // The exit manager is in charge of calling the dtors of singleton objects. | 395 // The exit manager is in charge of calling the dtors of singleton objects. |
331 base::AtExitManager exit_manager; | 396 exit_manager_.reset(new base::AtExitManager); |
332 | 397 |
333 #if defined(OS_MACOSX) | 398 #if defined(OS_MACOSX) |
334 // We need this pool for all the objects created before we get to the | 399 // We need this pool for all the objects created before we get to the |
335 // event loop, but we don't want to leave them hanging around until the | 400 // event loop, but we don't want to leave them hanging around until the |
336 // app quits. Each "main" needs to flush this pool right before it goes into | 401 // app quits. Each "main" needs to flush this pool right before it goes into |
337 // its main event loop to get rid of the cruft. | 402 // its main event loop to get rid of the cruft. |
338 base::mac::ScopedNSAutoreleasePool autorelease_pool; | 403 autorelease_pool_.reset(new base::mac::ScopedNSAutoreleasePool()); |
339 #endif | 404 #endif |
340 | 405 |
341 CommandLine::Init(argc, argv); | 406 CommandLine::Init(argc, argv); |
342 | 407 |
343 int exit_code; | 408 int exit_code; |
344 if (delegate && delegate->BasicStartupComplete(&exit_code)) | 409 if (delegate && delegate->BasicStartupComplete(&exit_code)) |
345 return exit_code; | 410 return exit_code; |
346 | 411 |
347 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 412 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
348 std::string process_type = | 413 std::string process_type = |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
438 CHECK(content::InitializeSandbox()); | 503 CHECK(content::InitializeSandbox()); |
439 } | 504 } |
440 #endif | 505 #endif |
441 | 506 |
442 if (delegate) delegate->SandboxInitialized(process_type); | 507 if (delegate) delegate->SandboxInitialized(process_type); |
443 | 508 |
444 #if defined(OS_POSIX) | 509 #if defined(OS_POSIX) |
445 SetProcessTitleFromCommandLine(argv); | 510 SetProcessTitleFromCommandLine(argv); |
446 #endif | 511 #endif |
447 | 512 |
513 // Return -1 to indicate no early termination. | |
514 return -1; | |
515 } | |
516 | |
517 int ContentMainRunnerImpl::Run() { | |
518 DCHECK(is_initialized_); | |
519 DCHECK(!is_shutdown_); | |
520 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
521 std::string process_type = | |
522 command_line.GetSwitchValueASCII(switches::kProcessType); | |
523 | |
448 content::MainFunctionParams main_params(command_line); | 524 content::MainFunctionParams main_params(command_line); |
449 #if defined(OS_WIN) | 525 #if defined(OS_WIN) |
450 main_params.sandbox_info = sandbox_info; | 526 main_params.sandbox_info = &sandbox_info_; |
451 #elif defined(OS_MACOSX) | 527 #elif defined(OS_MACOSX) |
452 main_params.autorelease_pool = &autorelease_pool; | 528 main_params.autorelease_pool = autorelease_pool_.get(); |
453 #endif | 529 #endif |
454 | 530 |
455 exit_code = RunNamedProcessTypeMain(process_type, main_params, delegate); | 531 return RunNamedProcessTypeMain(process_type, main_params, delegate_); |
532 } | |
456 | 533 |
457 if (delegate) delegate->ProcessExiting(process_type); | 534 void ContentMainRunnerImpl::Shutdown() { |
535 DCHECK(is_initialized_); | |
536 DCHECK(!is_shutdown_); | |
537 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
538 std::string process_type = | |
539 command_line.GetSwitchValueASCII(switches::kProcessType); | |
540 | |
541 if (delegate_) delegate_->ProcessExiting(process_type); | |
458 | 542 |
459 #if defined(OS_WIN) | 543 #if defined(OS_WIN) |
460 #ifdef _CRTDBG_MAP_ALLOC | 544 #ifdef _CRTDBG_MAP_ALLOC |
461 _CrtDumpMemoryLeaks(); | 545 _CrtDumpMemoryLeaks(); |
462 #endif // _CRTDBG_MAP_ALLOC | 546 #endif // _CRTDBG_MAP_ALLOC |
463 | 547 |
464 _Module.Term(); | 548 app_module_->Term(); |
465 #endif // OS_WIN | 549 #endif // OS_WIN |
466 | 550 |
467 return exit_code; | 551 #if defined(OS_MACOSX) |
552 autorelease_pool_.reset(NULL); | |
553 #endif | |
554 | |
555 exit_manager_.reset(NULL); | |
556 | |
557 #if defined(OS_WIN) | |
558 app_module_.reset(NULL); | |
559 #endif | |
560 | |
561 delegate_ = NULL; | |
562 is_shutdown_ = true; | |
563 } | |
564 | |
565 } // namespace | |
566 | |
567 namespace content { | |
568 | |
569 // static | |
570 ContentMainRunner* ContentMainRunner::Create() { | |
571 return new ContentMainRunnerImpl(); | |
468 } | 572 } |
469 | 573 |
470 } // namespace content | 574 } // namespace content |
OLD | NEW |