Clear webapp storage when site data is cleared
[chromium-blink-merge.git] / content / app / content_main_runner.cc
blobf35ed299817fee22c9f6feb56c313f1a7da49ab2
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
3 // found in the LICENSE file.
5 #include "content/public/app/content_main_runner.h"
7 #include <stdlib.h>
9 #include "base/allocator/allocator_extension.h"
10 #include "base/at_exit.h"
11 #include "base/command_line.h"
12 #include "base/debug/debugger.h"
13 #include "base/debug/stack_trace.h"
14 #include "base/files/file_path.h"
15 #include "base/i18n/icu_util.h"
16 #include "base/lazy_instance.h"
17 #include "base/logging.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "base/memory/scoped_vector.h"
20 #include "base/metrics/statistics_recorder.h"
21 #include "base/path_service.h"
22 #include "base/process/launch.h"
23 #include "base/process/memory.h"
24 #include "base/process/process_handle.h"
25 #include "base/profiler/alternate_timer.h"
26 #include "base/profiler/scoped_tracker.h"
27 #include "base/strings/string_number_conversions.h"
28 #include "base/strings/string_util.h"
29 #include "base/strings/stringprintf.h"
30 #include "base/trace_event/trace_event.h"
31 #include "components/tracing/trace_config_file.h"
32 #include "components/tracing/tracing_switches.h"
33 #include "content/browser/browser_main.h"
34 #include "content/common/set_process_title.h"
35 #include "content/common/url_schemes.h"
36 #include "content/gpu/in_process_gpu_thread.h"
37 #include "content/public/app/content_main.h"
38 #include "content/public/app/content_main_delegate.h"
39 #include "content/public/app/startup_helper_win.h"
40 #include "content/public/browser/content_browser_client.h"
41 #include "content/public/common/content_client.h"
42 #include "content/public/common/content_constants.h"
43 #include "content/public/common/content_paths.h"
44 #include "content/public/common/content_switches.h"
45 #include "content/public/common/main_function_params.h"
46 #include "content/public/common/sandbox_init.h"
47 #include "content/renderer/in_process_renderer_thread.h"
48 #include "content/utility/in_process_utility_thread.h"
49 #include "crypto/nss_util.h"
50 #include "ipc/ipc_descriptors.h"
51 #include "ipc/ipc_switches.h"
52 #include "media/base/media.h"
53 #include "sandbox/win/src/sandbox_types.h"
54 #include "ui/base/ui_base_paths.h"
55 #include "ui/base/ui_base_switches.h"
57 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
58 #include "gin/v8_initializer.h"
59 #endif
61 #if defined(USE_TCMALLOC)
62 #include "third_party/tcmalloc/chromium/src/gperftools/malloc_extension.h"
63 #if defined(TYPE_PROFILING)
64 #include "base/allocator/type_profiler.h"
65 #include "base/allocator/type_profiler_tcmalloc.h"
66 #endif
67 #endif
69 #if !defined(OS_IOS)
70 #include "content/app/mojo/mojo_init.h"
71 #include "content/browser/gpu/gpu_process_host.h"
72 #include "content/browser/renderer_host/render_process_host_impl.h"
73 #include "content/browser/utility_process_host_impl.h"
74 #include "content/public/plugin/content_plugin_client.h"
75 #include "content/public/renderer/content_renderer_client.h"
76 #include "content/public/utility/content_utility_client.h"
77 #endif
79 #if defined(OS_WIN)
80 #include <malloc.h>
81 #include <cstring>
83 #include "base/strings/string_number_conversions.h"
84 #include "base/trace_event/trace_event_etw_export_win.h"
85 #include "ui/base/win/atl_module.h"
86 #include "ui/gfx/win/dpi.h"
87 #elif defined(OS_MACOSX)
88 #include "base/mac/scoped_nsautorelease_pool.h"
89 #if !defined(OS_IOS)
90 #include "base/power_monitor/power_monitor_device_source.h"
91 #include "content/app/mac/mac_init.h"
92 #include "content/browser/browser_io_surface_manager_mac.h"
93 #include "content/browser/mach_broker_mac.h"
94 #include "content/child/child_io_surface_manager_mac.h"
95 #include "content/common/sandbox_init_mac.h"
96 #endif // !OS_IOS
97 #endif // OS_WIN
99 #if defined(OS_POSIX)
100 #include <signal.h>
102 #include "base/posix/global_descriptors.h"
103 #include "content/public/common/content_descriptors.h"
105 #if !defined(OS_MACOSX)
106 #include "content/public/common/content_descriptors.h"
107 #include "content/public/common/zygote_fork_delegate_linux.h"
108 #endif
109 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
110 #include "content/zygote/zygote_main.h"
111 #endif
113 #endif // OS_POSIX
115 #if !defined(OS_MACOSX) && defined(USE_TCMALLOC)
116 extern "C" {
117 int tc_set_new_mode(int mode);
119 #endif
121 namespace content {
122 extern int GpuMain(const content::MainFunctionParams&);
123 #if defined(ENABLE_PLUGINS)
124 #if !defined(OS_LINUX)
125 extern int PluginMain(const content::MainFunctionParams&);
126 #endif
127 extern int PpapiPluginMain(const MainFunctionParams&);
128 extern int PpapiBrokerMain(const MainFunctionParams&);
129 #endif
130 extern int RendererMain(const content::MainFunctionParams&);
131 extern int UtilityMain(const MainFunctionParams&);
132 } // namespace content
134 namespace content {
136 #if !defined(CHROME_MULTIPLE_DLL_CHILD)
137 base::LazyInstance<ContentBrowserClient>
138 g_empty_content_browser_client = LAZY_INSTANCE_INITIALIZER;
139 #endif // !CHROME_MULTIPLE_DLL_CHILD
141 #if !defined(OS_IOS) && !defined(CHROME_MULTIPLE_DLL_BROWSER)
142 base::LazyInstance<ContentPluginClient>
143 g_empty_content_plugin_client = LAZY_INSTANCE_INITIALIZER;
144 base::LazyInstance<ContentRendererClient>
145 g_empty_content_renderer_client = LAZY_INSTANCE_INITIALIZER;
146 base::LazyInstance<ContentUtilityClient>
147 g_empty_content_utility_client = LAZY_INSTANCE_INITIALIZER;
148 #endif // !OS_IOS && !CHROME_MULTIPLE_DLL_BROWSER
150 #if defined(OS_WIN)
152 #endif // defined(OS_WIN)
154 #if defined(OS_POSIX) && !defined(OS_IOS)
156 // Setup signal-handling state: resanitize most signals, ignore SIGPIPE.
157 void SetupSignalHandlers() {
158 // Sanitise our signal handling state. Signals that were ignored by our
159 // parent will also be ignored by us. We also inherit our parent's sigmask.
160 sigset_t empty_signal_set;
161 CHECK(0 == sigemptyset(&empty_signal_set));
162 CHECK(0 == sigprocmask(SIG_SETMASK, &empty_signal_set, NULL));
164 struct sigaction sigact;
165 memset(&sigact, 0, sizeof(sigact));
166 sigact.sa_handler = SIG_DFL;
167 static const int signals_to_reset[] =
168 {SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGABRT, SIGFPE, SIGSEGV,
169 SIGALRM, SIGTERM, SIGCHLD, SIGBUS, SIGTRAP}; // SIGPIPE is set below.
170 for (unsigned i = 0; i < arraysize(signals_to_reset); i++) {
171 CHECK(0 == sigaction(signals_to_reset[i], &sigact, NULL));
174 // Always ignore SIGPIPE. We check the return value of write().
175 CHECK(signal(SIGPIPE, SIG_IGN) != SIG_ERR);
178 #endif // OS_POSIX && !OS_IOS
180 void CommonSubprocessInit(const std::string& process_type) {
181 #if defined(OS_WIN)
182 // HACK: Let Windows know that we have started. This is needed to suppress
183 // the IDC_APPSTARTING cursor from being displayed for a prolonged period
184 // while a subprocess is starting.
185 PostThreadMessage(GetCurrentThreadId(), WM_NULL, 0, 0);
186 MSG msg;
187 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
188 #endif
189 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
190 // Various things break when you're using a locale where the decimal
191 // separator isn't a period. See e.g. bugs 22782 and 39964. For
192 // all processes except the browser process (where we call system
193 // APIs that may rely on the correct locale for formatting numbers
194 // when presenting them to the user), reset the locale for numeric
195 // formatting.
196 // Note that this is not correct for plugin processes -- they can
197 // surface UI -- but it's likely they get this wrong too so why not.
198 setlocale(LC_NUMERIC, "C");
199 #endif
201 #if !defined(OFFICIAL_BUILD)
202 // Print stack traces to stderr when crashes occur. This opens up security
203 // holes so it should never be enabled for official builds.
204 base::debug::EnableInProcessStackDumping();
205 #if defined(OS_WIN)
206 base::RouteStdioToConsole(false);
207 LoadLibraryA("dbghelp.dll");
208 #endif
209 #endif
212 class ContentClientInitializer {
213 public:
214 static void Set(const std::string& process_type,
215 ContentMainDelegate* delegate) {
216 ContentClient* content_client = GetContentClient();
217 #if !defined(CHROME_MULTIPLE_DLL_CHILD)
218 if (process_type.empty()) {
219 if (delegate)
220 content_client->browser_ = delegate->CreateContentBrowserClient();
221 if (!content_client->browser_)
222 content_client->browser_ = &g_empty_content_browser_client.Get();
224 #endif // !CHROME_MULTIPLE_DLL_CHILD
226 #if !defined(OS_IOS) && !defined(CHROME_MULTIPLE_DLL_BROWSER)
227 if (process_type == switches::kPluginProcess ||
228 process_type == switches::kPpapiPluginProcess) {
229 if (delegate)
230 content_client->plugin_ = delegate->CreateContentPluginClient();
231 if (!content_client->plugin_)
232 content_client->plugin_ = &g_empty_content_plugin_client.Get();
233 // Single process not supported in split dll mode.
234 } else if (process_type == switches::kRendererProcess ||
235 base::CommandLine::ForCurrentProcess()->HasSwitch(
236 switches::kSingleProcess)) {
237 if (delegate)
238 content_client->renderer_ = delegate->CreateContentRendererClient();
239 if (!content_client->renderer_)
240 content_client->renderer_ = &g_empty_content_renderer_client.Get();
243 if (process_type == switches::kUtilityProcess ||
244 base::CommandLine::ForCurrentProcess()->HasSwitch(
245 switches::kSingleProcess)) {
246 if (delegate)
247 content_client->utility_ = delegate->CreateContentUtilityClient();
248 // TODO(scottmg): http://crbug.com/237249 Should be in _child.
249 if (!content_client->utility_)
250 content_client->utility_ = &g_empty_content_utility_client.Get();
252 #endif // !OS_IOS && !CHROME_MULTIPLE_DLL_BROWSER
256 // We dispatch to a process-type-specific FooMain() based on a command-line
257 // flag. This struct is used to build a table of (flag, main function) pairs.
258 struct MainFunction {
259 const char* name;
260 int (*function)(const MainFunctionParams&);
263 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
264 // On platforms that use the zygote, we have a special subset of
265 // subprocesses that are launched via the zygote. This function
266 // fills in some process-launching bits around ZygoteMain().
267 // Returns the exit code of the subprocess.
268 int RunZygote(const MainFunctionParams& main_function_params,
269 ContentMainDelegate* delegate) {
270 static const MainFunction kMainFunctions[] = {
271 { switches::kRendererProcess, RendererMain },
272 #if defined(ENABLE_PLUGINS)
273 { switches::kPpapiPluginProcess, PpapiPluginMain },
274 #endif
275 { switches::kUtilityProcess, UtilityMain },
278 ScopedVector<ZygoteForkDelegate> zygote_fork_delegates;
279 if (delegate) {
280 delegate->ZygoteStarting(&zygote_fork_delegates);
281 media::InitializeMediaLibrary();
284 // This function call can return multiple times, once per fork().
285 if (!ZygoteMain(main_function_params, zygote_fork_delegates.Pass()))
286 return 1;
288 if (delegate) delegate->ZygoteForked();
290 // Zygote::HandleForkRequest may have reallocated the command
291 // line so update it here with the new version.
292 const base::CommandLine& command_line =
293 *base::CommandLine::ForCurrentProcess();
294 std::string process_type =
295 command_line.GetSwitchValueASCII(switches::kProcessType);
296 ContentClientInitializer::Set(process_type, delegate);
298 MainFunctionParams main_params(command_line);
299 main_params.zygote_child = true;
301 for (size_t i = 0; i < arraysize(kMainFunctions); ++i) {
302 if (process_type == kMainFunctions[i].name)
303 return kMainFunctions[i].function(main_params);
306 if (delegate)
307 return delegate->RunProcess(process_type, main_params);
309 NOTREACHED() << "Unknown zygote process type: " << process_type;
310 return 1;
312 #endif // defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
314 #if !defined(OS_IOS)
315 static void RegisterMainThreadFactories() {
316 #if !defined(CHROME_MULTIPLE_DLL_BROWSER) && !defined(CHROME_MULTIPLE_DLL_CHILD)
317 UtilityProcessHostImpl::RegisterUtilityMainThreadFactory(
318 CreateInProcessUtilityThread);
319 RenderProcessHostImpl::RegisterRendererMainThreadFactory(
320 CreateInProcessRendererThread);
321 GpuProcessHost::RegisterGpuMainThreadFactory(
322 CreateInProcessGpuThread);
323 #else
324 base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess();
325 if (command_line.HasSwitch(switches::kSingleProcess)) {
326 LOG(FATAL) <<
327 "--single-process is not supported in chrome multiple dll browser.";
329 if (command_line.HasSwitch(switches::kInProcessGPU)) {
330 LOG(FATAL) <<
331 "--in-process-gpu is not supported in chrome multiple dll browser.";
333 #endif // !CHROME_MULTIPLE_DLL_BROWSER && !CHROME_MULTIPLE_DLL_CHILD
336 // Run the FooMain() for a given process type.
337 // If |process_type| is empty, runs BrowserMain().
338 // Returns the exit code for this process.
339 int RunNamedProcessTypeMain(
340 const std::string& process_type,
341 const MainFunctionParams& main_function_params,
342 ContentMainDelegate* delegate) {
343 static const MainFunction kMainFunctions[] = {
344 #if !defined(CHROME_MULTIPLE_DLL_CHILD)
345 { "", BrowserMain },
346 #endif
347 #if !defined(CHROME_MULTIPLE_DLL_BROWSER)
348 #if defined(ENABLE_PLUGINS)
349 #if !defined(OS_LINUX)
350 { switches::kPluginProcess, PluginMain },
351 #endif
352 { switches::kPpapiPluginProcess, PpapiPluginMain },
353 { switches::kPpapiBrokerProcess, PpapiBrokerMain },
354 #endif // ENABLE_PLUGINS
355 { switches::kUtilityProcess, UtilityMain },
356 { switches::kRendererProcess, RendererMain },
357 { switches::kGpuProcess, GpuMain },
358 #endif // !CHROME_MULTIPLE_DLL_BROWSER
361 RegisterMainThreadFactories();
363 for (size_t i = 0; i < arraysize(kMainFunctions); ++i) {
364 if (process_type == kMainFunctions[i].name) {
365 if (delegate) {
366 int exit_code = delegate->RunProcess(process_type,
367 main_function_params);
368 #if defined(OS_ANDROID)
369 // In Android's browser process, the negative exit code doesn't mean the
370 // default behavior should be used as the UI message loop is managed by
371 // the Java and the browser process's default behavior is always
372 // overridden.
373 if (process_type.empty())
374 return exit_code;
375 #endif
376 if (exit_code >= 0)
377 return exit_code;
379 return kMainFunctions[i].function(main_function_params);
383 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
384 // Zygote startup is special -- see RunZygote comments above
385 // for why we don't use ZygoteMain directly.
386 if (process_type == switches::kZygoteProcess)
387 return RunZygote(main_function_params, delegate);
388 #endif
390 // If it's a process we don't know about, the embedder should know.
391 if (delegate)
392 return delegate->RunProcess(process_type, main_function_params);
394 NOTREACHED() << "Unknown process type: " << process_type;
395 return 1;
397 #endif // !OS_IOS
399 class ContentMainRunnerImpl : public ContentMainRunner {
400 public:
401 ContentMainRunnerImpl()
402 : is_initialized_(false),
403 is_shutdown_(false),
404 completed_basic_startup_(false),
405 delegate_(NULL),
406 ui_task_(NULL) {
407 #if defined(OS_WIN)
408 memset(&sandbox_info_, 0, sizeof(sandbox_info_));
409 #endif
412 ~ContentMainRunnerImpl() override {
413 if (is_initialized_ && !is_shutdown_)
414 Shutdown();
417 #if defined(USE_TCMALLOC)
418 static bool GetAllocatorWasteSizeThunk(size_t* size) {
419 size_t heap_size, allocated_bytes, unmapped_bytes;
420 MallocExtension* ext = MallocExtension::instance();
421 if (ext->GetNumericProperty("generic.heap_size", &heap_size) &&
422 ext->GetNumericProperty("generic.current_allocated_bytes",
423 &allocated_bytes) &&
424 ext->GetNumericProperty("tcmalloc.pageheap_unmapped_bytes",
425 &unmapped_bytes)) {
426 *size = heap_size - allocated_bytes - unmapped_bytes;
427 return true;
429 DCHECK(false);
430 return false;
433 static void GetStatsThunk(char* buffer, int buffer_length) {
434 MallocExtension::instance()->GetStats(buffer, buffer_length);
437 static void ReleaseFreeMemoryThunk() {
438 MallocExtension::instance()->ReleaseFreeMemory();
440 #endif
442 int Initialize(const ContentMainParams& params) override {
443 ui_task_ = params.ui_task;
445 base::EnableTerminationOnOutOfMemory();
446 #if defined(OS_WIN)
447 RegisterInvalidParamHandler();
448 ui::win::CreateATLModuleIfNeeded();
450 sandbox_info_ = *params.sandbox_info;
451 #else // !OS_WIN
453 #if defined(OS_ANDROID)
454 // See note at the initialization of ExitManager, below; basically,
455 // only Android builds have the ctor/dtor handlers set up to use
456 // TRACE_EVENT right away.
457 TRACE_EVENT0("startup", "ContentMainRunnerImpl::Initialize");
458 #endif // OS_ANDROID
460 // NOTE(willchan): One might ask why these TCMalloc-related calls are done
461 // here rather than in process_util_linux.cc with the definition of
462 // EnableTerminationOnOutOfMemory(). That's because base shouldn't have a
463 // dependency on TCMalloc. Really, we ought to have our allocator shim code
464 // implement this EnableTerminationOnOutOfMemory() function. Whateverz.
465 // This works for now.
466 #if !defined(OS_MACOSX) && defined(USE_TCMALLOC)
468 #if defined(TYPE_PROFILING)
469 base::type_profiler::InterceptFunctions::SetFunctions(
470 base::type_profiler::NewInterceptForTCMalloc,
471 base::type_profiler::DeleteInterceptForTCMalloc);
472 #endif
474 // For tcmalloc, we need to tell it to behave like new.
475 tc_set_new_mode(1);
477 // On windows, we've already set these thunks up in _heap_init()
478 base::allocator::SetGetAllocatorWasteSizeFunction(
479 GetAllocatorWasteSizeThunk);
480 base::allocator::SetGetStatsFunction(GetStatsThunk);
481 base::allocator::SetReleaseFreeMemoryFunction(ReleaseFreeMemoryThunk);
483 // Provide optional hook for monitoring allocation quantities on a
484 // per-thread basis. Only set the hook if the environment indicates this
485 // needs to be enabled.
486 const char* profiling = getenv(tracked_objects::kAlternateProfilerTime);
487 if (profiling &&
488 (atoi(profiling) == tracked_objects::TIME_SOURCE_TYPE_TCMALLOC)) {
489 tracked_objects::SetAlternateTimeSource(
490 MallocExtension::GetBytesAllocatedOnCurrentThread,
491 tracked_objects::TIME_SOURCE_TYPE_TCMALLOC);
493 #endif // !OS_MACOSX && USE_TCMALLOC
495 #if !defined(OS_IOS)
496 base::GlobalDescriptors* g_fds = base::GlobalDescriptors::GetInstance();
497 #endif
499 // On Android,
500 // - setlocale() is not supported.
501 // - We do not override the signal handlers so that we can get
502 // stack trace when crashing.
503 // - The ipc_fd is passed through the Java service.
504 // Thus, these are all disabled.
505 #if !defined(OS_ANDROID) && !defined(OS_IOS)
506 // Set C library locale to make sure CommandLine can parse argument values
507 // in correct encoding.
508 setlocale(LC_ALL, "");
510 SetupSignalHandlers();
511 g_fds->Set(kPrimaryIPCChannel,
512 kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor);
513 #endif // !OS_ANDROID && !OS_IOS
515 #if defined(OS_LINUX) || defined(OS_OPENBSD)
516 g_fds->Set(kCrashDumpSignal,
517 kCrashDumpSignal + base::GlobalDescriptors::kBaseDescriptor);
518 #endif // OS_LINUX || OS_OPENBSD
521 #endif // !OS_WIN
523 is_initialized_ = true;
524 delegate_ = params.delegate;
526 // The exit manager is in charge of calling the dtors of singleton objects.
527 // On Android, AtExitManager is set up when library is loaded.
528 // On iOS, it's set up in main(), which can't call directly through to here.
529 // A consequence of this is that you can't use the ctor/dtor-based
530 // TRACE_EVENT methods on Linux or iOS builds till after we set this up.
531 #if !defined(OS_ANDROID) && !defined(OS_IOS)
532 if (!ui_task_) {
533 // When running browser tests, don't create a second AtExitManager as that
534 // interfers with shutdown when objects created before ContentMain is
535 // called are destructed when it returns.
536 exit_manager_.reset(new base::AtExitManager);
538 #endif // !OS_ANDROID && !OS_IOS
540 // Don't create this loop on iOS, since the outer loop is already handled
541 // and a loop that's destroyed in shutdown interleaves badly with the event
542 // loop pool on iOS.
543 #if defined(OS_MACOSX) && !defined(OS_IOS)
544 // We need this pool for all the objects created before we get to the
545 // event loop, but we don't want to leave them hanging around until the
546 // app quits. Each "main" needs to flush this pool right before it goes into
547 // its main event loop to get rid of the cruft.
548 autorelease_pool_.reset(new base::mac::ScopedNSAutoreleasePool());
549 InitializeMac();
550 #endif
552 // On Android, the command line is initialized when library is loaded and
553 // we have already started our TRACE_EVENT0.
554 #if !defined(OS_ANDROID)
555 // argc/argv are ignored on Windows and Android; see command_line.h for
556 // details.
557 int argc = 0;
558 const char** argv = NULL;
560 #if !defined(OS_WIN)
561 argc = params.argc;
562 argv = params.argv;
563 #endif
565 base::CommandLine::Init(argc, argv);
567 base::EnableTerminationOnHeapCorruption();
569 // TODO(yiyaoliu, vadimt): Remove this once crbug.com/453640 is fixed.
570 // Enable profiler recording right after command line is initialized so that
571 // browser startup can be instrumented.
572 if (delegate_ && delegate_->ShouldEnableProfilerRecording())
573 tracked_objects::ScopedTracker::Enable();
575 #if !defined(OS_IOS)
576 SetProcessTitleFromCommandLine(argv);
577 #endif
578 #endif // !OS_ANDROID
580 int exit_code = 0;
581 if (delegate_ && delegate_->BasicStartupComplete(&exit_code))
582 return exit_code;
584 completed_basic_startup_ = true;
586 const base::CommandLine& command_line =
587 *base::CommandLine::ForCurrentProcess();
588 std::string process_type =
589 command_line.GetSwitchValueASCII(switches::kProcessType);
591 #if !defined(OS_IOS)
592 // Initialize mojo here so that services can be registered.
593 InitializeMojo();
594 #endif
596 #if defined(OS_WIN)
597 bool init_device_scale_factor = true;
598 if (command_line.HasSwitch(switches::kDeviceScaleFactor)) {
599 std::string scale_factor_string = command_line.GetSwitchValueASCII(
600 switches::kDeviceScaleFactor);
601 double scale_factor = 0;
602 if (base::StringToDouble(scale_factor_string, &scale_factor)) {
603 init_device_scale_factor = false;
604 gfx::InitDeviceScaleFactor(scale_factor);
607 if (init_device_scale_factor)
608 gfx::InitDeviceScaleFactor(gfx::GetDPIScale());
609 #endif
611 if (!GetContentClient())
612 SetContentClient(&empty_content_client_);
613 ContentClientInitializer::Set(process_type, delegate_);
615 #if defined(OS_WIN)
616 // Route stdio to parent console (if any) or create one.
617 if (command_line.HasSwitch(switches::kEnableLogging))
618 base::RouteStdioToConsole(true);
619 #endif
621 // Enable startup tracing asap to avoid early TRACE_EVENT calls being
622 // ignored.
623 if (command_line.HasSwitch(switches::kTraceStartup)) {
624 base::trace_event::TraceConfig trace_config(
625 command_line.GetSwitchValueASCII(switches::kTraceStartup),
626 base::trace_event::RECORD_UNTIL_FULL);
627 base::trace_event::TraceLog::GetInstance()->SetEnabled(
628 trace_config,
629 base::trace_event::TraceLog::RECORDING_MODE);
630 } else if (process_type != switches::kZygoteProcess &&
631 process_type != switches::kRendererProcess) {
632 if (tracing::TraceConfigFile::GetInstance()->IsEnabled()) {
633 base::trace_event::TraceLog::GetInstance()->SetEnabled(
634 tracing::TraceConfigFile::GetInstance()->GetTraceConfig(),
635 base::trace_event::TraceLog::RECORDING_MODE);
639 #if defined(OS_WIN)
640 // Enable exporting of events to ETW if requested on the command line.
641 if (command_line.HasSwitch(switches::kTraceExportEventsToETW))
642 base::trace_event::TraceEventETWExport::EnableETWExport();
643 #endif // OS_WIN
645 #if !defined(OS_ANDROID)
646 // Android tracing started at the beginning of the method.
647 // Other OSes have to wait till we get here in order for all the memory
648 // management setup to be completed.
649 TRACE_EVENT0("startup", "ContentMainRunnerImpl::Initialize");
650 #endif // !OS_ANDROID
652 #if defined(OS_MACOSX) && !defined(OS_IOS)
653 // We need to allocate the IO Ports before the Sandbox is initialized or
654 // the first instance of PowerMonitor is created.
655 // It's important not to allocate the ports for processes which don't
656 // register with the power monitor - see crbug.com/88867.
657 if (process_type.empty() ||
658 (delegate_ &&
659 delegate_->ProcessRegistersWithSystemProcess(process_type))) {
660 base::PowerMonitorDeviceSource::AllocateSystemIOPorts();
663 if (!process_type.empty() &&
664 (!delegate_ || delegate_->ShouldSendMachPort(process_type))) {
665 MachBroker::ChildSendTaskPortToParent();
668 if (!command_line.HasSwitch(switches::kSingleProcess) &&
669 !process_type.empty() && (process_type == switches::kRendererProcess ||
670 process_type == switches::kGpuProcess)) {
671 base::mac::ScopedMachSendRight service_port =
672 BrowserIOSurfaceManager::LookupServicePort(getppid());
673 if (service_port.is_valid()) {
674 ChildIOSurfaceManager::GetInstance()->set_service_port(
675 service_port.release());
676 IOSurfaceManager::SetInstance(ChildIOSurfaceManager::GetInstance());
679 #elif defined(OS_WIN)
680 SetupCRT(command_line);
681 #endif
683 #if defined(OS_POSIX)
684 if (!process_type.empty()) {
685 // When you hit Ctrl-C in a terminal running the browser
686 // process, a SIGINT is delivered to the entire process group.
687 // When debugging the browser process via gdb, gdb catches the
688 // SIGINT for the browser process (and dumps you back to the gdb
689 // console) but doesn't for the child processes, killing them.
690 // The fix is to have child processes ignore SIGINT; they'll die
691 // on their own when the browser process goes away.
693 // Note that we *can't* rely on BeingDebugged to catch this case because
694 // we are the child process, which is not being debugged.
695 // TODO(evanm): move this to some shared subprocess-init function.
696 if (!base::debug::BeingDebugged())
697 signal(SIGINT, SIG_IGN);
699 #endif
701 #if defined(USE_NSS_CERTS)
702 crypto::EarlySetupForNSSInit();
703 #endif
705 ui::RegisterPathProvider();
706 RegisterPathProvider();
707 RegisterContentSchemes(true);
709 #if defined(OS_ANDROID)
710 int icudata_fd = g_fds->MaybeGet(kAndroidICUDataDescriptor);
711 if (icudata_fd != -1) {
712 auto icudata_region = g_fds->GetRegion(kAndroidICUDataDescriptor);
713 CHECK(base::i18n::InitializeICUWithFileDescriptor(icudata_fd,
714 icudata_region));
715 } else {
716 CHECK(base::i18n::InitializeICU());
718 #else
719 CHECK(base::i18n::InitializeICU());
720 #endif // OS_ANDROID
722 base::StatisticsRecorder::Initialize();
724 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
725 #if defined(OS_POSIX) && !defined(OS_MACOSX)
726 #if !defined(OS_ANDROID)
727 // kV8NativesDataDescriptor and kV8SnapshotDataDescriptor could be shared
728 // with child processes via file descriptors. On Android they are set in
729 // ChildProcessService::InternalInitChildProcess, otherwise set them here.
730 if (command_line.HasSwitch(switches::kV8NativesPassedByFD)) {
731 g_fds->Set(
732 kV8NativesDataDescriptor,
733 kV8NativesDataDescriptor + base::GlobalDescriptors::kBaseDescriptor);
735 if (command_line.HasSwitch(switches::kV8SnapshotPassedByFD)) {
736 g_fds->Set(
737 kV8SnapshotDataDescriptor,
738 kV8SnapshotDataDescriptor + base::GlobalDescriptors::kBaseDescriptor);
740 #endif // !OS_ANDROID
741 int v8_natives_fd = g_fds->MaybeGet(kV8NativesDataDescriptor);
742 int v8_snapshot_fd = g_fds->MaybeGet(kV8SnapshotDataDescriptor);
743 if (v8_snapshot_fd != -1) {
744 auto v8_snapshot_region = g_fds->GetRegion(kV8SnapshotDataDescriptor);
745 gin::V8Initializer::LoadV8SnapshotFromFD(
746 v8_snapshot_fd, v8_snapshot_region.offset, v8_snapshot_region.size);
747 } else {
748 gin::V8Initializer::LoadV8Snapshot();
750 if (v8_natives_fd != -1) {
751 auto v8_natives_region = g_fds->GetRegion(kV8NativesDataDescriptor);
752 gin::V8Initializer::LoadV8NativesFromFD(
753 v8_natives_fd, v8_natives_region.offset, v8_natives_region.size);
754 } else {
755 gin::V8Initializer::LoadV8Natives();
757 #else
758 gin::V8Initializer::LoadV8Snapshot();
759 gin::V8Initializer::LoadV8Natives();
760 #endif // OS_POSIX && !OS_MACOSX
761 #endif // V8_USE_EXTERNAL_STARTUP_DATA
763 if (delegate_)
764 delegate_->PreSandboxStartup();
766 if (!process_type.empty())
767 CommonSubprocessInit(process_type);
769 #if defined(OS_WIN)
770 CHECK(InitializeSandbox(params.sandbox_info));
771 #elif defined(OS_MACOSX) && !defined(OS_IOS)
772 if (process_type == switches::kRendererProcess ||
773 process_type == switches::kPpapiPluginProcess ||
774 (delegate_ && delegate_->DelaySandboxInitialization(process_type))) {
775 // On OS X the renderer sandbox needs to be initialized later in the
776 // startup sequence in RendererMainPlatformDelegate::EnableSandbox().
777 } else {
778 CHECK(InitializeSandbox());
780 #endif
782 if (delegate_)
783 delegate_->SandboxInitialized(process_type);
785 // Return -1 to indicate no early termination.
786 return -1;
789 int Run() override {
790 DCHECK(is_initialized_);
791 DCHECK(!is_shutdown_);
792 const base::CommandLine& command_line =
793 *base::CommandLine::ForCurrentProcess();
794 std::string process_type =
795 command_line.GetSwitchValueASCII(switches::kProcessType);
797 MainFunctionParams main_params(command_line);
798 main_params.ui_task = ui_task_;
799 #if defined(OS_WIN)
800 main_params.sandbox_info = &sandbox_info_;
801 #elif defined(OS_MACOSX)
802 main_params.autorelease_pool = autorelease_pool_.get();
803 #endif
805 #if !defined(OS_IOS)
806 return RunNamedProcessTypeMain(process_type, main_params, delegate_);
807 #else
808 return 1;
809 #endif
812 void Shutdown() override {
813 DCHECK(is_initialized_);
814 DCHECK(!is_shutdown_);
816 if (completed_basic_startup_ && delegate_) {
817 const base::CommandLine& command_line =
818 *base::CommandLine::ForCurrentProcess();
819 std::string process_type =
820 command_line.GetSwitchValueASCII(switches::kProcessType);
822 delegate_->ProcessExiting(process_type);
825 #if defined(OS_WIN)
826 #ifdef _CRTDBG_MAP_ALLOC
827 _CrtDumpMemoryLeaks();
828 #endif // _CRTDBG_MAP_ALLOC
829 #endif // OS_WIN
831 #if defined(OS_MACOSX) && !defined(OS_IOS)
832 autorelease_pool_.reset(NULL);
833 #endif
835 exit_manager_.reset(NULL);
837 delegate_ = NULL;
838 is_shutdown_ = true;
841 private:
842 // True if the runner has been initialized.
843 bool is_initialized_;
845 // True if the runner has been shut down.
846 bool is_shutdown_;
848 // True if basic startup was completed.
849 bool completed_basic_startup_;
851 // Used if the embedder doesn't set one.
852 ContentClient empty_content_client_;
854 // The delegate will outlive this object.
855 ContentMainDelegate* delegate_;
857 scoped_ptr<base::AtExitManager> exit_manager_;
858 #if defined(OS_WIN)
859 sandbox::SandboxInterfaceInfo sandbox_info_;
860 #elif defined(OS_MACOSX)
861 scoped_ptr<base::mac::ScopedNSAutoreleasePool> autorelease_pool_;
862 #endif
864 base::Closure* ui_task_;
866 DISALLOW_COPY_AND_ASSIGN(ContentMainRunnerImpl);
869 // static
870 ContentMainRunner* ContentMainRunner::Create() {
871 return new ContentMainRunnerImpl();
874 } // namespace content