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/hi_res_timer_manager.h"
11 #include "base/i18n/rtl.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/metrics/field_trial.h"
14 #include "base/message_loop.h"
15 #include "base/metrics/histogram.h"
16 #include "base/metrics/statistics_recorder.h"
17 #include "base/metrics/stats_counters.h"
18 #include "base/path_service.h"
19 #include "base/pending_task.h"
20 #include "base/power_monitor/power_monitor.h"
21 #include "base/process_util.h"
22 #include "base/string_util.h"
23 #include "base/threading/platform_thread.h"
24 #include "base/time.h"
25 #include "content/common/pepper_plugin_registry.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/render_process_impl.h"
31 #include "content/renderer/render_thread_impl.h"
32 #include "content/renderer/renderer_main_platform_delegate.h"
33 #include "ui/base/ui_base_switches.h"
34 #include "webkit/glue/webkit_glue.h"
35 #include "webkit/plugins/ppapi/ppapi_interface_factory.h"
37 #if defined(OS_MACOSX)
38 #include <Carbon/Carbon.h>
42 #include "base/mac/mac_util.h"
43 #include "base/mac/scoped_nsautorelease_pool.h"
44 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
49 // This function provides some ways to test crash and assertion handling
50 // behavior of the renderer.
51 static void HandleRendererErrorTestParameters(const CommandLine
& command_line
) {
52 if (command_line
.HasSwitch(switches::kWaitForDebugger
))
53 base::debug::WaitForDebugger(60, true);
55 if (command_line
.HasSwitch(switches::kRendererStartupDialog
))
56 ChildProcess::WaitForDebugger("Renderer");
58 // This parameter causes an assertion.
59 if (command_line
.HasSwitch(switches::kRendererAssertTest
)) {
64 // This is a simplified version of the browser Jankometer, which measures
65 // the processing time of tasks on the render thread.
66 class RendererMessageLoopObserver
: public MessageLoop::TaskObserver
{
68 RendererMessageLoopObserver()
69 : process_times_(base::Histogram::FactoryGet(
70 "Chrome.ProcMsgL RenderThread",
71 1, 3600000, 50, base::Histogram::kUmaTargetedHistogramFlag
)) {}
72 virtual ~RendererMessageLoopObserver() {}
74 virtual void WillProcessTask(const base::PendingTask
& pending_task
) OVERRIDE
{
75 begin_process_message_
= base::TimeTicks::Now();
78 virtual void DidProcessTask(const base::PendingTask
& pending_task
) OVERRIDE
{
79 if (!begin_process_message_
.is_null())
80 process_times_
->AddTime(base::TimeTicks::Now() - begin_process_message_
);
84 base::TimeTicks begin_process_message_
;
85 base::HistogramBase
* const process_times_
;
86 DISALLOW_COPY_AND_ASSIGN(RendererMessageLoopObserver
);
89 // For measuring memory usage after each task. Behind a command line flag.
90 class MemoryObserver
: public MessageLoop::TaskObserver
{
93 virtual ~MemoryObserver() {}
95 virtual void WillProcessTask(const base::PendingTask
& pending_task
) OVERRIDE
{
98 virtual void DidProcessTask(const base::PendingTask
& pending_task
) OVERRIDE
{
99 HISTOGRAM_MEMORY_KB("Memory.RendererUsed", webkit_glue::MemoryUsageKB());
102 DISALLOW_COPY_AND_ASSIGN(MemoryObserver
);
105 // mainline routine for running as the Renderer process
106 int RendererMain(const MainFunctionParams
& parameters
) {
107 TRACE_EVENT_BEGIN_ETW("RendererMain", 0, "");
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 // This function allows pausing execution using the --renderer-startup-dialog
128 // flag allowing us to attach a debugger.
129 // Do not move this function down since that would mean we can't easily debug
130 // whatever occurs before it.
131 HandleRendererErrorTestParameters(parsed_command_line
);
133 RendererMainPlatformDelegate
platform(parameters
);
135 webkit::ppapi::PpapiInterfaceFactoryManager
* factory_manager
=
136 webkit::ppapi::PpapiInterfaceFactoryManager::GetInstance();
137 GetContentClient()->renderer()->RegisterPPAPIInterfaceFactories(
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 we use Cocoa in the renderer (for the forseeable future as of
146 // now; see http://crbug.com/13890 for info) we need to have a UI loop.
147 MessageLoop
main_message_loop(MessageLoop::TYPE_UI
);
149 // The main message loop of the renderer services doesn't have IO or UI tasks,
150 // unless in-process-plugins is used.
151 MessageLoop
main_message_loop(RenderProcessImpl::InProcessPlugins() ?
152 MessageLoop::TYPE_UI
: MessageLoop::TYPE_DEFAULT
);
154 main_message_loop
.AddTaskObserver(&task_observer
);
156 scoped_ptr
<MemoryObserver
> memory_observer
;
157 if (parsed_command_line
.HasSwitch(switches::kMemoryMetrics
)) {
158 memory_observer
.reset(new MemoryObserver());
159 main_message_loop
.AddTaskObserver(memory_observer
.get());
162 base::PlatformThread::SetName("CrRendererMain");
164 base::PowerMonitor power_monitor
;
165 HighResolutionTimerManager hi_res_timer_manager
;
167 platform
.PlatformInitialize();
169 bool no_sandbox
= parsed_command_line
.HasSwitch(switches::kNoSandbox
);
170 platform
.InitSandboxTests(no_sandbox
);
172 // Initialize histogram statistics gathering system.
173 base::StatisticsRecorder::Initialize();
175 // Initialize statistical testing infrastructure. We set the entropy provider
176 // to NULL to disallow the renderer process from creating its own one-time
177 // randomized trials; they should be created in the browser process.
178 base::FieldTrialList
field_trial_list(NULL
);
179 // Ensure any field trials in browser are reflected into renderer.
180 if (parsed_command_line
.HasSwitch(switches::kForceFieldTrials
)) {
181 std::string persistent
= parsed_command_line
.GetSwitchValueASCII(
182 switches::kForceFieldTrials
);
183 bool ret
= base::FieldTrialList::CreateTrialsFromString(persistent
);
187 #if defined(ENABLE_PLUGINS)
188 // Load pepper plugins before engaging the sandbox.
189 PepperPluginRegistry::GetInstance();
193 #if defined(OS_WIN) || defined(OS_MACOSX)
194 // TODO(markus): Check if it is OK to unconditionally move this
196 RenderProcessImpl render_process
;
197 new RenderThreadImpl();
199 bool run_loop
= true;
201 run_loop
= platform
.EnableSandbox();
203 LOG(ERROR
) << "Running without renderer sandbox";
205 // For convenience, we print the stack trace for crashes. We can't get
206 // symbols when the sandbox is enabled, so only try when the sandbox is
208 base::debug::EnableInProcessStackDumping();
211 #if defined(OS_POSIX) && !defined(OS_MACOSX)
212 RenderProcessImpl render_process
;
213 new RenderThreadImpl();
216 platform
.RunSandboxTests(no_sandbox
);
218 startup_timer
.Stop(); // End of Startup Time Measurement.
221 #if defined(OS_MACOSX)
225 TRACE_EVENT_BEGIN_ETW("RendererMain.START_MSG_LOOP", 0, 0);
226 MessageLoop::current()->Run();
227 TRACE_EVENT_END_ETW("RendererMain.START_MSG_LOOP", 0, 0);
230 platform
.PlatformUninitialize();
231 TRACE_EVENT_END_ETW("RendererMain", 0, "");
235 } // namespace content