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 "base/android/library_loader/library_loader_hooks.h"
36 #include "third_party/skia/include/core/SkGraphics.h"
39 #if defined(OS_MACOSX)
40 #include <Carbon/Carbon.h>
44 #include "base/mac/mac_util.h"
45 #include "base/mac/scoped_nsautorelease_pool.h"
46 #include "base/message_loop/message_pump_mac.h"
47 #include "third_party/WebKit/public/web/WebView.h"
50 #if defined(ENABLE_PLUGINS)
51 #include "content/renderer/pepper/pepper_plugin_registry.h"
54 #if defined(ENABLE_WEBRTC)
55 #include "third_party/libjingle/overrides/init_webrtc.h"
60 // This function provides some ways to test crash and assertion handling
61 // behavior of the renderer.
62 static void HandleRendererErrorTestParameters(const CommandLine
& command_line
) {
63 if (command_line
.HasSwitch(switches::kWaitForDebugger
))
64 base::debug::WaitForDebugger(60, true);
66 if (command_line
.HasSwitch(switches::kRendererStartupDialog
))
67 ChildProcess::WaitForDebugger("Renderer");
69 // This parameter causes an assertion.
70 if (command_line
.HasSwitch(switches::kRendererAssertTest
)) {
75 // This is a simplified version of the browser Jankometer, which measures
76 // the processing time of tasks on the render thread.
77 class RendererMessageLoopObserver
: public base::MessageLoop::TaskObserver
{
79 RendererMessageLoopObserver()
80 : process_times_(base::Histogram::FactoryGet(
81 "Chrome.ProcMsgL RenderThread",
82 1, 3600000, 50, base::Histogram::kUmaTargetedHistogramFlag
)) {}
83 virtual ~RendererMessageLoopObserver() {}
85 virtual void WillProcessTask(const base::PendingTask
& pending_task
) override
{
86 begin_process_message_
= base::TimeTicks::Now();
89 virtual void DidProcessTask(const base::PendingTask
& pending_task
) override
{
90 if (!begin_process_message_
.is_null())
91 process_times_
->AddTime(base::TimeTicks::Now() - begin_process_message_
);
95 base::TimeTicks begin_process_message_
;
96 base::HistogramBase
* const process_times_
;
97 DISALLOW_COPY_AND_ASSIGN(RendererMessageLoopObserver
);
102 // mainline routine for running as the Renderer process
103 int RendererMain(const MainFunctionParams
& parameters
) {
104 TRACE_EVENT_BEGIN_ETW("RendererMain", 0, "");
105 base::debug::TraceLog::GetInstance()->SetProcessName("Renderer");
106 base::debug::TraceLog::GetInstance()->SetProcessSortIndex(
107 kTraceEventRendererProcessSortIndex
);
109 const CommandLine
& parsed_command_line
= parameters
.command_line
;
111 #if defined(OS_MACOSX)
112 base::mac::ScopedNSAutoreleasePool
* pool
= parameters
.autorelease_pool
;
115 #if defined(OS_CHROMEOS)
116 // As Zygote process starts up earlier than browser process gets its own
117 // locale (at login time for Chrome OS), we have to set the ICU default
118 // locale for renderer process here.
119 // ICU locale will be used for fallback font selection etc.
120 if (parsed_command_line
.HasSwitch(switches::kLang
)) {
121 const std::string locale
=
122 parsed_command_line
.GetSwitchValueASCII(switches::kLang
);
123 base::i18n::SetICUDefaultLocale(locale
);
127 #if defined(OS_ANDROID)
128 const int kMB
= 1024 * 1024;
129 size_t font_cache_limit
=
130 base::SysInfo::IsLowEndDevice() ? kMB
: 8 * kMB
;
131 SkGraphics::SetFontCacheLimit(font_cache_limit
);
134 // This function allows pausing execution using the --renderer-startup-dialog
135 // flag allowing us to attach a debugger.
136 // Do not move this function down since that would mean we can't easily debug
137 // whatever occurs before it.
138 HandleRendererErrorTestParameters(parsed_command_line
);
140 RendererMainPlatformDelegate
platform(parameters
);
143 base::StatsCounterTimer
stats_counter_timer("Content.RendererInit");
144 base::StatsScope
<base::StatsCounterTimer
> startup_timer(stats_counter_timer
);
146 RendererMessageLoopObserver task_observer
;
147 #if defined(OS_MACOSX)
148 // As long as scrollbars on Mac are painted with Cocoa, the message pump
149 // needs to be backed by a Foundation-level loop to process NSTimers. See
150 // http://crbug.com/306348#c24 for details.
151 scoped_ptr
<base::MessagePump
> pump(new base::MessagePumpNSRunLoop());
152 base::MessageLoop
main_message_loop(pump
.Pass());
154 // The main message loop of the renderer services doesn't have IO or UI tasks.
155 base::MessageLoop main_message_loop
;
157 main_message_loop
.AddTaskObserver(&task_observer
);
159 base::PlatformThread::SetName("CrRendererMain");
161 bool no_sandbox
= parsed_command_line
.HasSwitch(switches::kNoSandbox
);
163 // Initialize histogram statistics gathering system.
164 base::StatisticsRecorder::Initialize();
166 #if defined(OS_ANDROID)
167 // If we have a pending chromium android linker histogram, record it.
168 base::android::RecordChromiumAndroidLinkerRendererHistogram();
171 // Initialize statistical testing infrastructure. We set the entropy provider
172 // to NULL to disallow the renderer process from creating its own one-time
173 // randomized trials; they should be created in the browser process.
174 base::FieldTrialList
field_trial_list(NULL
);
175 // Ensure any field trials in browser are reflected into renderer.
176 if (parsed_command_line
.HasSwitch(switches::kForceFieldTrials
)) {
177 // Field trials are created in an "activated" state to ensure they get
178 // reported in crash reports.
179 bool result
= base::FieldTrialList::CreateTrialsFromString(
180 parsed_command_line
.GetSwitchValueASCII(switches::kForceFieldTrials
),
181 base::FieldTrialList::ACTIVATE_TRIALS
,
182 std::set
<std::string
>());
186 // PlatformInitialize uses FieldTrials, so this must happen later.
187 platform
.PlatformInitialize();
189 #if defined(ENABLE_PLUGINS)
190 // Load pepper plugins before engaging the sandbox.
191 PepperPluginRegistry::GetInstance();
193 #if defined(ENABLE_WEBRTC)
194 // Initialize WebRTC before engaging the sandbox.
195 // NOTE: On linux, this call could already have been made from
196 // zygote_main_linux.cc. However, calling multiple times from the same thread
198 InitializeWebRtcModule();
202 #if defined(OS_WIN) || defined(OS_MACOSX)
203 // TODO(markus): Check if it is OK to unconditionally move this
205 RenderProcessImpl render_process
;
206 new RenderThreadImpl();
208 bool run_loop
= true;
210 run_loop
= platform
.EnableSandbox();
212 LOG(ERROR
) << "Running without renderer sandbox";
214 // For convenience, we print the stack traces for crashes. When sandbox
215 // is enabled, the in-process stack dumping is enabled as part of the
216 // EnableSandbox() call.
217 base::debug::EnableInProcessStackDumping();
220 #if defined(OS_POSIX) && !defined(OS_MACOSX)
221 RenderProcessImpl render_process
;
222 new RenderThreadImpl();
225 base::HighResolutionTimerManager hi_res_timer_manager
;
227 startup_timer
.Stop(); // End of Startup Time Measurement.
230 #if defined(OS_MACOSX)
234 TRACE_EVENT_BEGIN_ETW("RendererMain.START_MSG_LOOP", 0, 0);
235 base::MessageLoop::current()->Run();
236 TRACE_EVENT_END_ETW("RendererMain.START_MSG_LOOP", 0, 0);
239 platform
.PlatformUninitialize();
240 TRACE_EVENT_END_ETW("RendererMain", 0, "");
244 } // namespace content