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 "base/base_switches.h"
6 #include "base/command_line.h"
7 #include "base/debug/debugger.h"
8 #include "base/debug/leak_annotations.h"
9 #include "base/debug/stack_trace.h"
10 #include "base/i18n/rtl.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/metrics/field_trial.h"
13 #include "base/metrics/histogram.h"
14 #include "base/metrics/statistics_recorder.h"
15 #include "base/pending_task.h"
16 #include "base/strings/string_util.h"
17 #include "base/sys_info.h"
18 #include "base/threading/platform_thread.h"
19 #include "base/timer/hi_res_timer_manager.h"
20 #include "base/trace_event/trace_event.h"
21 #include "components/scheduler/renderer/renderer_scheduler.h"
22 #include "content/child/child_process.h"
23 #include "content/common/content_constants_internal.h"
24 #include "content/public/common/content_switches.h"
25 #include "content/public/common/main_function_params.h"
26 #include "content/public/renderer/content_renderer_client.h"
27 #include "content/renderer/render_process_impl.h"
28 #include "content/renderer/render_thread_impl.h"
29 #include "content/renderer/renderer_main_platform_delegate.h"
30 #include "third_party/skia/include/core/SkGraphics.h"
31 #include "ui/base/ui_base_switches.h"
33 #if defined(OS_ANDROID)
34 #include "base/android/library_loader/library_loader_hooks.h"
37 #if defined(OS_MACOSX)
38 #include <Carbon/Carbon.h>
42 #include "base/mac/scoped_nsautorelease_pool.h"
43 #include "base/message_loop/message_pump_mac.h"
44 #include "third_party/WebKit/public/web/WebView.h"
47 #if defined(ENABLE_PLUGINS)
48 #include "content/renderer/pepper/pepper_plugin_registry.h"
51 #if defined(ENABLE_WEBRTC)
52 #include "third_party/libjingle/overrides/init_webrtc.h"
57 // This function provides some ways to test crash and assertion handling
58 // behavior of the renderer.
59 static void HandleRendererErrorTestParameters(
60 const base::CommandLine
& command_line
) {
61 if (command_line
.HasSwitch(switches::kWaitForDebugger
))
62 base::debug::WaitForDebugger(60, true);
64 if (command_line
.HasSwitch(switches::kRendererStartupDialog
))
65 ChildProcess::WaitForDebugger("Renderer");
70 // mainline routine for running as the Renderer process
71 int RendererMain(const MainFunctionParams
& parameters
) {
72 TRACE_EVENT_BEGIN_ETW("RendererMain", 0, "");
73 base::trace_event::TraceLog::GetInstance()->SetProcessName("Renderer");
74 base::trace_event::TraceLog::GetInstance()->SetProcessSortIndex(
75 kTraceEventRendererProcessSortIndex
);
77 const base::CommandLine
& parsed_command_line
= parameters
.command_line
;
79 #if defined(OS_MACOSX)
80 base::mac::ScopedNSAutoreleasePool
* pool
= parameters
.autorelease_pool
;
83 #if defined(OS_CHROMEOS)
84 // As Zygote process starts up earlier than browser process gets its own
85 // locale (at login time for Chrome OS), we have to set the ICU default
86 // locale for renderer process here.
87 // ICU locale will be used for fallback font selection etc.
88 if (parsed_command_line
.HasSwitch(switches::kLang
)) {
89 const std::string locale
=
90 parsed_command_line
.GetSwitchValueASCII(switches::kLang
);
91 base::i18n::SetICUDefaultLocale(locale
);
96 #if defined(OS_ANDROID)
97 const int kMB
= 1024 * 1024;
98 size_t font_cache_limit
=
99 base::SysInfo::IsLowEndDevice() ? kMB
: 8 * kMB
;
100 SkGraphics::SetFontCacheLimit(font_cache_limit
);
103 // This function allows pausing execution using the --renderer-startup-dialog
104 // flag allowing us to attach a debugger.
105 // Do not move this function down since that would mean we can't easily debug
106 // whatever occurs before it.
107 HandleRendererErrorTestParameters(parsed_command_line
);
109 RendererMainPlatformDelegate
platform(parameters
);
110 #if defined(OS_MACOSX)
111 // As long as scrollbars on Mac are painted with Cocoa, the message pump
112 // needs to be backed by a Foundation-level loop to process NSTimers. See
113 // http://crbug.com/306348#c24 for details.
114 scoped_ptr
<base::MessagePump
> pump(new base::MessagePumpNSRunLoop());
115 scoped_ptr
<base::MessageLoop
> main_message_loop(
116 new base::MessageLoop(pump
.Pass()));
118 // The main message loop of the renderer services doesn't have IO or UI tasks.
119 scoped_ptr
<base::MessageLoop
> main_message_loop(new base::MessageLoop());
122 base::PlatformThread::SetName("CrRendererMain");
123 scoped_ptr
<scheduler::RendererScheduler
> renderer_scheduler(
124 scheduler::RendererScheduler::Create());
126 bool no_sandbox
= parsed_command_line
.HasSwitch(switches::kNoSandbox
);
128 // Initialize histogram statistics gathering system.
129 base::StatisticsRecorder::Initialize();
131 #if defined(OS_ANDROID)
132 // If we have a pending chromium android linker histogram, record it.
133 base::android::RecordChromiumAndroidLinkerRendererHistogram();
136 // Initialize statistical testing infrastructure. We set the entropy provider
137 // to NULL to disallow the renderer process from creating its own one-time
138 // randomized trials; they should be created in the browser process.
139 base::FieldTrialList
field_trial_list(NULL
);
140 // Ensure any field trials in browser are reflected into renderer.
141 if (parsed_command_line
.HasSwitch(switches::kForceFieldTrials
)) {
142 bool result
= base::FieldTrialList::CreateTrialsFromString(
143 parsed_command_line
.GetSwitchValueASCII(switches::kForceFieldTrials
),
144 base::FieldTrialList::DONT_ACTIVATE_TRIALS
,
145 std::set
<std::string
>());
149 // PlatformInitialize uses FieldTrials, so this must happen later.
150 platform
.PlatformInitialize();
152 #if defined(ENABLE_PLUGINS)
153 // Load pepper plugins before engaging the sandbox.
154 PepperPluginRegistry::GetInstance();
156 #if defined(ENABLE_WEBRTC)
157 // Initialize WebRTC before engaging the sandbox.
158 // NOTE: On linux, this call could already have been made from
159 // zygote_main_linux.cc. However, calling multiple times from the same thread
161 InitializeWebRtcModule();
165 #if defined(OS_WIN) || defined(OS_MACOSX)
166 // TODO(markus): Check if it is OK to unconditionally move this
168 RenderProcessImpl render_process
;
169 RenderThreadImpl::Create(main_message_loop
.Pass(),
170 renderer_scheduler
.Pass());
172 bool run_loop
= true;
174 run_loop
= platform
.EnableSandbox();
176 LOG(ERROR
) << "Running without renderer sandbox";
178 // For convenience, we print the stack traces for crashes. When sandbox
179 // is enabled, the in-process stack dumping is enabled as part of the
180 // EnableSandbox() call.
181 base::debug::EnableInProcessStackDumping();
184 #if defined(OS_POSIX) && !defined(OS_MACOSX)
185 RenderProcessImpl render_process
;
186 RenderThreadImpl::Create(main_message_loop
.Pass(),
187 renderer_scheduler
.Pass());
189 base::HighResolutionTimerManager hi_res_timer_manager
;
192 #if defined(OS_MACOSX)
196 TRACE_EVENT_BEGIN_ETW("RendererMain.START_MSG_LOOP", 0, 0);
197 base::MessageLoop::current()->Run();
198 TRACE_EVENT_END_ETW("RendererMain.START_MSG_LOOP", 0, 0);
200 #if defined(LEAK_SANITIZER)
201 // Run leak detection before RenderProcessImpl goes out of scope. This helps
202 // ignore shutdown-only leaks.
203 __lsan_do_leak_check();
206 platform
.PlatformUninitialize();
207 TRACE_EVENT_END_ETW("RendererMain", 0, "");
211 } // namespace content