Add ICU message format support
[chromium-blink-merge.git] / content / renderer / renderer_main.cc
blob2d82597ee48cf7142cfc54e85ed5c5cd6e8e08cb
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/debug/stack_trace.h"
10 #include "base/i18n/rtl.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/metrics/field_trial.h"
13 #include "base/metrics/histogram.h"
14 #include "base/metrics/statistics_recorder.h"
15 #include "base/pending_task.h"
16 #include "base/strings/string_util.h"
17 #include "base/sys_info.h"
18 #include "base/threading/platform_thread.h"
19 #include "base/timer/hi_res_timer_manager.h"
20 #include "base/trace_event/trace_event.h"
21 #include "components/scheduler/renderer/renderer_scheduler.h"
22 #include "content/child/child_process.h"
23 #include "content/common/content_constants_internal.h"
24 #include "content/public/common/content_switches.h"
25 #include "content/public/common/main_function_params.h"
26 #include "content/public/renderer/content_renderer_client.h"
27 #include "content/renderer/render_process_impl.h"
28 #include "content/renderer/render_thread_impl.h"
29 #include "content/renderer/renderer_main_platform_delegate.h"
30 #include "third_party/skia/include/core/SkGraphics.h"
31 #include "ui/base/ui_base_switches.h"
33 #if defined(OS_ANDROID)
34 #include "base/android/library_loader/library_loader_hooks.h"
35 #endif // OS_ANDROID
37 #if defined(OS_MACOSX)
38 #include <Carbon/Carbon.h>
39 #include <signal.h>
40 #include <unistd.h>
42 #include "base/mac/scoped_nsautorelease_pool.h"
43 #include "base/message_loop/message_pump_mac.h"
44 #include "third_party/WebKit/public/web/WebView.h"
45 #endif // OS_MACOSX
47 #if defined(ENABLE_PLUGINS)
48 #include "content/renderer/pepper/pepper_plugin_registry.h"
49 #endif
51 #if defined(ENABLE_WEBRTC)
52 #include "third_party/libjingle/overrides/init_webrtc.h"
53 #endif
55 #if defined(USE_OZONE)
56 #include "ui/ozone/public/client_native_pixmap_factory.h"
57 #endif
59 namespace content {
60 namespace {
61 // This function provides some ways to test crash and assertion handling
62 // behavior of the renderer.
63 static void HandleRendererErrorTestParameters(
64 const base::CommandLine& command_line) {
65 if (command_line.HasSwitch(switches::kWaitForDebugger))
66 base::debug::WaitForDebugger(60, true);
68 if (command_line.HasSwitch(switches::kRendererStartupDialog))
69 ChildProcess::WaitForDebugger("Renderer");
72 #if defined(USE_OZONE)
73 base::LazyInstance<scoped_ptr<ui::ClientNativePixmapFactory>> g_pixmap_factory =
74 LAZY_INSTANCE_INITIALIZER;
75 #endif
77 } // namespace
79 // mainline routine for running as the Renderer process
80 int RendererMain(const MainFunctionParams& parameters) {
81 TRACE_EVENT_BEGIN_ETW("RendererMain", 0, "");
82 base::trace_event::TraceLog::GetInstance()->SetProcessName("Renderer");
83 base::trace_event::TraceLog::GetInstance()->SetProcessSortIndex(
84 kTraceEventRendererProcessSortIndex);
86 const base::CommandLine& parsed_command_line = parameters.command_line;
88 #if defined(OS_MACOSX)
89 base::mac::ScopedNSAutoreleasePool* pool = parameters.autorelease_pool;
90 #endif // OS_MACOSX
92 #if defined(OS_CHROMEOS)
93 // As Zygote process starts up earlier than browser process gets its own
94 // locale (at login time for Chrome OS), we have to set the ICU default
95 // locale for renderer process here.
96 // ICU locale will be used for fallback font selection etc.
97 if (parsed_command_line.HasSwitch(switches::kLang)) {
98 const std::string locale =
99 parsed_command_line.GetSwitchValueASCII(switches::kLang);
100 base::i18n::SetICUDefaultLocale(locale);
102 #endif
104 SkGraphics::Init();
105 #if defined(OS_ANDROID)
106 const int kMB = 1024 * 1024;
107 size_t font_cache_limit =
108 base::SysInfo::IsLowEndDevice() ? kMB : 8 * kMB;
109 SkGraphics::SetFontCacheLimit(font_cache_limit);
110 #endif
112 #if defined(USE_OZONE)
113 g_pixmap_factory.Get() = ui::ClientNativePixmapFactory::Create();
114 ui::ClientNativePixmapFactory::SetInstance(g_pixmap_factory.Get().get());
115 #endif
117 // This function allows pausing execution using the --renderer-startup-dialog
118 // flag allowing us to attach a debugger.
119 // Do not move this function down since that would mean we can't easily debug
120 // whatever occurs before it.
121 HandleRendererErrorTestParameters(parsed_command_line);
123 RendererMainPlatformDelegate platform(parameters);
124 #if defined(OS_MACOSX)
125 // As long as scrollbars on Mac are painted with Cocoa, the message pump
126 // needs to be backed by a Foundation-level loop to process NSTimers. See
127 // http://crbug.com/306348#c24 for details.
128 scoped_ptr<base::MessagePump> pump(new base::MessagePumpNSRunLoop());
129 scoped_ptr<base::MessageLoop> main_message_loop(
130 new base::MessageLoop(pump.Pass()));
131 #else
132 // The main message loop of the renderer services doesn't have IO or UI tasks.
133 scoped_ptr<base::MessageLoop> main_message_loop(new base::MessageLoop());
134 #endif
136 base::PlatformThread::SetName("CrRendererMain");
137 scoped_ptr<scheduler::RendererScheduler> renderer_scheduler(
138 scheduler::RendererScheduler::Create());
140 bool no_sandbox = parsed_command_line.HasSwitch(switches::kNoSandbox);
142 // Initialize histogram statistics gathering system.
143 base::StatisticsRecorder::Initialize();
145 #if defined(OS_ANDROID)
146 // If we have a pending chromium android linker histogram, record it.
147 base::android::RecordChromiumAndroidLinkerRendererHistogram();
148 #endif
150 // Initialize statistical testing infrastructure. We set the entropy provider
151 // to NULL to disallow the renderer process from creating its own one-time
152 // randomized trials; they should be created in the browser process.
153 base::FieldTrialList field_trial_list(NULL);
154 // Ensure any field trials in browser are reflected into renderer.
155 if (parsed_command_line.HasSwitch(switches::kForceFieldTrials)) {
156 bool result = base::FieldTrialList::CreateTrialsFromString(
157 parsed_command_line.GetSwitchValueASCII(switches::kForceFieldTrials),
158 base::FieldTrialList::DONT_ACTIVATE_TRIALS,
159 std::set<std::string>());
160 DCHECK(result);
163 // PlatformInitialize uses FieldTrials, so this must happen later.
164 platform.PlatformInitialize();
166 #if defined(ENABLE_PLUGINS)
167 // Load pepper plugins before engaging the sandbox.
168 PepperPluginRegistry::GetInstance();
169 #endif
170 #if defined(ENABLE_WEBRTC)
171 // Initialize WebRTC before engaging the sandbox.
172 // NOTE: On linux, this call could already have been made from
173 // zygote_main_linux.cc. However, calling multiple times from the same thread
174 // is OK.
175 InitializeWebRtcModule();
176 #endif
179 #if defined(OS_WIN) || defined(OS_MACOSX)
180 // TODO(markus): Check if it is OK to unconditionally move this
181 // instruction down.
182 RenderProcessImpl render_process;
183 RenderThreadImpl::Create(main_message_loop.Pass(),
184 renderer_scheduler.Pass());
185 #endif
186 bool run_loop = true;
187 if (!no_sandbox) {
188 run_loop = platform.EnableSandbox();
189 } else {
190 LOG(ERROR) << "Running without renderer sandbox";
191 #if !defined(NDEBUG) || (defined(CFI_ENFORCEMENT) && !defined(OFFICIAL_BUILD))
192 // For convenience, we print the stack traces for crashes. When sandbox
193 // is enabled, the in-process stack dumping is enabled as part of the
194 // EnableSandbox() call.
195 base::debug::EnableInProcessStackDumping();
196 #endif
198 #if defined(OS_POSIX) && !defined(OS_MACOSX)
199 RenderProcessImpl render_process;
200 RenderThreadImpl::Create(main_message_loop.Pass(),
201 renderer_scheduler.Pass());
202 #endif
203 base::HighResolutionTimerManager hi_res_timer_manager;
205 if (run_loop) {
206 #if defined(OS_MACOSX)
207 if (pool)
208 pool->Recycle();
209 #endif
210 TRACE_EVENT_BEGIN_ETW("RendererMain.START_MSG_LOOP", 0, 0);
211 base::MessageLoop::current()->Run();
212 TRACE_EVENT_END_ETW("RendererMain.START_MSG_LOOP", 0, 0);
214 #if defined(LEAK_SANITIZER)
215 // Run leak detection before RenderProcessImpl goes out of scope. This helps
216 // ignore shutdown-only leaks.
217 __lsan_do_leak_check();
218 #endif
220 platform.PlatformUninitialize();
221 TRACE_EVENT_END_ETW("RendererMain", 0, "");
222 return 0;
225 } // namespace content