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/stack_trace.h"
9 #include "base/debug/trace_event.h"
10 #include "base/i18n/rtl.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/metrics/field_trial.h"
14 #include "base/metrics/histogram.h"
15 #include "base/metrics/statistics_recorder.h"
16 #include "base/metrics/stats_counters.h"
17 #include "base/path_service.h"
18 #include "base/pending_task.h"
19 #include "base/strings/string_util.h"
20 #include "base/sys_info.h"
21 #include "base/threading/platform_thread.h"
22 #include "base/time/time.h"
23 #include "base/timer/hi_res_timer_manager.h"
24 #include "content/child/child_process.h"
25 #include "content/common/content_constants_internal.h"
26 #include "content/public/common/content_switches.h"
27 #include "content/public/common/main_function_params.h"
28 #include "content/public/renderer/content_renderer_client.h"
29 #include "content/renderer/render_process_impl.h"
30 #include "content/renderer/render_thread_impl.h"
31 #include "content/renderer/renderer_main_platform_delegate.h"
32 #include "ui/base/ui_base_switches.h"
34 #if defined(OS_ANDROID)
35 #include "third_party/skia/include/core/SkGraphics.h"
38 #if defined(OS_MACOSX)
39 #include <Carbon/Carbon.h>
43 #include "base/mac/mac_util.h"
44 #include "base/mac/scoped_nsautorelease_pool.h"
45 #include "base/message_loop/message_pump_mac.h"
46 #include "third_party/WebKit/public/web/WebView.h"
49 #if defined(ENABLE_PLUGINS)
50 #include "content/renderer/pepper/pepper_plugin_registry.h"
53 #if defined(ENABLE_WEBRTC)
54 #include "third_party/libjingle/overrides/init_webrtc.h"
59 // This function provides some ways to test crash and assertion handling
60 // behavior of the renderer.
61 static void HandleRendererErrorTestParameters(const CommandLine
& command_line
) {
62 if (command_line
.HasSwitch(switches::kWaitForDebugger
))
63 base::debug::WaitForDebugger(60, true);
65 if (command_line
.HasSwitch(switches::kRendererStartupDialog
))
66 ChildProcess::WaitForDebugger("Renderer");
68 // This parameter causes an assertion.
69 if (command_line
.HasSwitch(switches::kRendererAssertTest
)) {
74 // This is a simplified version of the browser Jankometer, which measures
75 // the processing time of tasks on the render thread.
76 class RendererMessageLoopObserver
: public base::MessageLoop::TaskObserver
{
78 RendererMessageLoopObserver()
79 : process_times_(base::Histogram::FactoryGet(
80 "Chrome.ProcMsgL RenderThread",
81 1, 3600000, 50, base::Histogram::kUmaTargetedHistogramFlag
)) {}
82 virtual ~RendererMessageLoopObserver() {}
84 virtual void WillProcessTask(const base::PendingTask
& pending_task
) override
{
85 begin_process_message_
= base::TimeTicks::Now();
88 virtual void DidProcessTask(const base::PendingTask
& pending_task
) override
{
89 if (!begin_process_message_
.is_null())
90 process_times_
->AddTime(base::TimeTicks::Now() - begin_process_message_
);
94 base::TimeTicks begin_process_message_
;
95 base::HistogramBase
* const process_times_
;
96 DISALLOW_COPY_AND_ASSIGN(RendererMessageLoopObserver
);
101 // mainline routine for running as the Renderer process
102 int RendererMain(const MainFunctionParams
& parameters
) {
103 TRACE_EVENT_BEGIN_ETW("RendererMain", 0, "");
104 base::debug::TraceLog::GetInstance()->SetProcessName("Renderer");
105 base::debug::TraceLog::GetInstance()->SetProcessSortIndex(
106 kTraceEventRendererProcessSortIndex
);
108 const CommandLine
& parsed_command_line
= parameters
.command_line
;
110 #if defined(OS_MACOSX)
111 base::mac::ScopedNSAutoreleasePool
* pool
= parameters
.autorelease_pool
;
114 #if defined(OS_CHROMEOS)
115 // As Zygote process starts up earlier than browser process gets its own
116 // locale (at login time for Chrome OS), we have to set the ICU default
117 // locale for renderer process here.
118 // ICU locale will be used for fallback font selection etc.
119 if (parsed_command_line
.HasSwitch(switches::kLang
)) {
120 const std::string locale
=
121 parsed_command_line
.GetSwitchValueASCII(switches::kLang
);
122 base::i18n::SetICUDefaultLocale(locale
);
126 #if defined(OS_ANDROID)
127 const int kMB
= 1024 * 1024;
128 size_t font_cache_limit
=
129 base::SysInfo::IsLowEndDevice() ? kMB
: 8 * kMB
;
130 SkGraphics::SetFontCacheLimit(font_cache_limit
);
133 // This function allows pausing execution using the --renderer-startup-dialog
134 // flag allowing us to attach a debugger.
135 // Do not move this function down since that would mean we can't easily debug
136 // whatever occurs before it.
137 HandleRendererErrorTestParameters(parsed_command_line
);
139 RendererMainPlatformDelegate
platform(parameters
);
142 base::StatsCounterTimer
stats_counter_timer("Content.RendererInit");
143 base::StatsScope
<base::StatsCounterTimer
> startup_timer(stats_counter_timer
);
145 RendererMessageLoopObserver task_observer
;
146 #if defined(OS_MACOSX)
147 // As long as scrollbars on Mac are painted with Cocoa, the message pump
148 // needs to be backed by a Foundation-level loop to process NSTimers. See
149 // http://crbug.com/306348#c24 for details.
150 scoped_ptr
<base::MessagePump
> pump(new base::MessagePumpNSRunLoop());
151 base::MessageLoop
main_message_loop(pump
.Pass());
153 // The main message loop of the renderer services doesn't have IO or UI tasks.
154 base::MessageLoop main_message_loop
;
156 main_message_loop
.AddTaskObserver(&task_observer
);
158 base::PlatformThread::SetName("CrRendererMain");
160 bool no_sandbox
= parsed_command_line
.HasSwitch(switches::kNoSandbox
);
162 // Initialize histogram statistics gathering system.
163 base::StatisticsRecorder::Initialize();
165 // Initialize statistical testing infrastructure. We set the entropy provider
166 // to NULL to disallow the renderer process from creating its own one-time
167 // randomized trials; they should be created in the browser process.
168 base::FieldTrialList
field_trial_list(NULL
);
169 // Ensure any field trials in browser are reflected into renderer.
170 if (parsed_command_line
.HasSwitch(switches::kForceFieldTrials
)) {
171 // Field trials are created in an "activated" state to ensure they get
172 // reported in crash reports.
173 bool result
= base::FieldTrialList::CreateTrialsFromString(
174 parsed_command_line
.GetSwitchValueASCII(switches::kForceFieldTrials
),
175 base::FieldTrialList::ACTIVATE_TRIALS
,
176 std::set
<std::string
>());
180 // PlatformInitialize uses FieldTrials, so this must happen later.
181 platform
.PlatformInitialize();
183 #if defined(ENABLE_PLUGINS)
184 // Load pepper plugins before engaging the sandbox.
185 PepperPluginRegistry::GetInstance();
187 #if defined(ENABLE_WEBRTC)
188 // Initialize WebRTC before engaging the sandbox.
189 // NOTE: On linux, this call could already have been made from
190 // zygote_main_linux.cc. However, calling multiple times from the same thread
192 InitializeWebRtcModule();
196 #if defined(OS_WIN) || defined(OS_MACOSX)
197 // TODO(markus): Check if it is OK to unconditionally move this
199 RenderProcessImpl render_process
;
200 new RenderThreadImpl();
202 bool run_loop
= true;
204 run_loop
= platform
.EnableSandbox();
206 LOG(ERROR
) << "Running without renderer sandbox";
208 // For convenience, we print the stack traces for crashes. When sandbox
209 // is enabled, the in-process stack dumping is enabled as part of the
210 // EnableSandbox() call.
211 base::debug::EnableInProcessStackDumping();
214 #if defined(OS_POSIX) && !defined(OS_MACOSX)
215 RenderProcessImpl render_process
;
216 new RenderThreadImpl();
219 base::HighResolutionTimerManager hi_res_timer_manager
;
221 startup_timer
.Stop(); // End of Startup Time Measurement.
224 #if defined(OS_MACOSX)
228 TRACE_EVENT_BEGIN_ETW("RendererMain.START_MSG_LOOP", 0, 0);
229 base::MessageLoop::current()->Run();
230 TRACE_EVENT_END_ETW("RendererMain.START_MSG_LOOP", 0, 0);
233 platform
.PlatformUninitialize();
234 TRACE_EVENT_END_ETW("RendererMain", 0, "");
238 } // namespace content