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