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/browser_plugin/browser_plugin_manager_impl.h"
30 #include "content/renderer/pepper/pepper_plugin_registry.h"
31 #include "content/renderer/render_process_impl.h"
32 #include "content/renderer/render_thread_impl.h"
33 #include "content/renderer/renderer_main_platform_delegate.h"
34 #include "ui/base/ui_base_switches.h"
36 #if defined(OS_ANDROID)
37 #include "third_party/skia/include/core/SkGraphics.h"
40 #if defined(OS_MACOSX)
41 #include <Carbon/Carbon.h>
45 #include "base/mac/mac_util.h"
46 #include "base/mac/scoped_nsautorelease_pool.h"
47 #include "base/message_loop/message_pump_mac.h"
48 #include "third_party/WebKit/public/web/WebView.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(const CommandLine
& command_line
) {
60 if (command_line
.HasSwitch(switches::kWaitForDebugger
))
61 base::debug::WaitForDebugger(60, true);
63 if (command_line
.HasSwitch(switches::kRendererStartupDialog
))
64 ChildProcess::WaitForDebugger("Renderer");
66 // This parameter causes an assertion.
67 if (command_line
.HasSwitch(switches::kRendererAssertTest
)) {
72 // This is a simplified version of the browser Jankometer, which measures
73 // the processing time of tasks on the render thread.
74 class RendererMessageLoopObserver
: public base::MessageLoop::TaskObserver
{
76 RendererMessageLoopObserver()
77 : process_times_(base::Histogram::FactoryGet(
78 "Chrome.ProcMsgL RenderThread",
79 1, 3600000, 50, base::Histogram::kUmaTargetedHistogramFlag
)) {}
80 virtual ~RendererMessageLoopObserver() {}
82 virtual void WillProcessTask(const base::PendingTask
& pending_task
) OVERRIDE
{
83 begin_process_message_
= base::TimeTicks::Now();
86 virtual void DidProcessTask(const base::PendingTask
& pending_task
) OVERRIDE
{
87 if (!begin_process_message_
.is_null())
88 process_times_
->AddTime(base::TimeTicks::Now() - begin_process_message_
);
92 base::TimeTicks begin_process_message_
;
93 base::HistogramBase
* const process_times_
;
94 DISALLOW_COPY_AND_ASSIGN(RendererMessageLoopObserver
);
99 // mainline routine for running as the Renderer process
100 int RendererMain(const MainFunctionParams
& parameters
) {
101 TRACE_EVENT_BEGIN_ETW("RendererMain", 0, "");
102 base::debug::TraceLog::GetInstance()->SetProcessName("Renderer");
103 base::debug::TraceLog::GetInstance()->SetProcessSortIndex(
104 kTraceEventRendererProcessSortIndex
);
106 const CommandLine
& parsed_command_line
= parameters
.command_line
;
108 #if defined(OS_MACOSX)
109 base::mac::ScopedNSAutoreleasePool
* pool
= parameters
.autorelease_pool
;
112 #if defined(OS_CHROMEOS)
113 // As Zygote process starts up earlier than browser process gets its own
114 // locale (at login time for Chrome OS), we have to set the ICU default
115 // locale for renderer process here.
116 // ICU locale will be used for fallback font selection etc.
117 if (parsed_command_line
.HasSwitch(switches::kLang
)) {
118 const std::string locale
=
119 parsed_command_line
.GetSwitchValueASCII(switches::kLang
);
120 base::i18n::SetICUDefaultLocale(locale
);
124 #if defined(OS_ANDROID)
125 const int kMB
= 1024 * 1024;
126 size_t font_cache_limit
=
127 base::SysInfo::IsLowEndDevice() ? kMB
: 8 * kMB
;
128 SkGraphics::SetFontCacheLimit(font_cache_limit
);
131 // This function allows pausing execution using the --renderer-startup-dialog
132 // flag allowing us to attach a debugger.
133 // Do not move this function down since that would mean we can't easily debug
134 // whatever occurs before it.
135 HandleRendererErrorTestParameters(parsed_command_line
);
137 RendererMainPlatformDelegate
platform(parameters
);
140 base::StatsCounterTimer
stats_counter_timer("Content.RendererInit");
141 base::StatsScope
<base::StatsCounterTimer
> startup_timer(stats_counter_timer
);
143 RendererMessageLoopObserver task_observer
;
144 #if defined(OS_MACOSX)
145 // As long as scrollbars on Mac are painted with Cocoa, the message pump
146 // needs to be backed by a Foundation-level loop to process NSTimers. See
147 // http://crbug.com/306348#c24 for details.
148 scoped_ptr
<base::MessagePump
> pump(new base::MessagePumpNSRunLoop());
149 base::MessageLoop
main_message_loop(pump
.Pass());
151 // The main message loop of the renderer services doesn't have IO or UI tasks.
152 base::MessageLoop main_message_loop
;
154 main_message_loop
.AddTaskObserver(&task_observer
);
156 base::PlatformThread::SetName("CrRendererMain");
158 bool no_sandbox
= parsed_command_line
.HasSwitch(switches::kNoSandbox
);
160 // Initialize histogram statistics gathering system.
161 base::StatisticsRecorder::Initialize();
163 // Initialize statistical testing infrastructure. We set the entropy provider
164 // to NULL to disallow the renderer process from creating its own one-time
165 // randomized trials; they should be created in the browser process.
166 base::FieldTrialList
field_trial_list(NULL
);
167 // Ensure any field trials in browser are reflected into renderer.
168 if (parsed_command_line
.HasSwitch(switches::kForceFieldTrials
)) {
169 // Field trials are created in an "activated" state to ensure they get
170 // reported in crash reports.
171 bool result
= base::FieldTrialList::CreateTrialsFromString(
172 parsed_command_line
.GetSwitchValueASCII(switches::kForceFieldTrials
),
173 base::FieldTrialList::ACTIVATE_TRIALS
,
174 std::set
<std::string
>());
178 // PlatformInitialize uses FieldTrials, so this must happen later.
179 platform
.PlatformInitialize();
181 #if defined(ENABLE_PLUGINS)
182 // Load pepper plugins before engaging the sandbox.
183 PepperPluginRegistry::GetInstance();
185 #if defined(ENABLE_WEBRTC)
186 // Initialize WebRTC before engaging the sandbox.
187 // NOTE: On linux, this call could already have been made from
188 // zygote_main_linux.cc. However, calling multiple times from the same thread
190 InitializeWebRtcModule();
194 #if defined(OS_WIN) || defined(OS_MACOSX)
195 // TODO(markus): Check if it is OK to unconditionally move this
197 RenderProcessImpl render_process
;
198 new RenderThreadImpl();
200 bool run_loop
= true;
202 run_loop
= platform
.EnableSandbox();
204 LOG(ERROR
) << "Running without renderer sandbox";
206 // For convenience, we print the stack traces for crashes. When sandbox
207 // is enabled, the in-process stack dumping is enabled as part of the
208 // EnableSandbox() call.
209 base::debug::EnableInProcessStackDumping();
212 #if defined(OS_POSIX) && !defined(OS_MACOSX)
213 RenderProcessImpl render_process
;
214 new RenderThreadImpl();
217 base::HighResolutionTimerManager hi_res_timer_manager
;
219 startup_timer
.Stop(); // End of Startup Time Measurement.
222 #if defined(OS_MACOSX)
226 TRACE_EVENT_BEGIN_ETW("RendererMain.START_MSG_LOOP", 0, 0);
227 base::MessageLoop::current()->Run();
228 TRACE_EVENT_END_ETW("RendererMain.START_MSG_LOOP", 0, 0);
231 platform
.PlatformUninitialize();
232 TRACE_EVENT_END_ETW("RendererMain", 0, "");
236 } // namespace content