Fix a couple bugs in the settings_page_header wrt breadcrumbs.
[chromium-blink-merge.git] / content / renderer / renderer_main.cc
bloba76df805790ff7d12a0b4b62fb67518edaaa36da
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/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/pending_task.h"
17 #include "base/strings/string_util.h"
18 #include "base/sys_info.h"
19 #include "base/threading/platform_thread.h"
20 #include "base/time/time.h"
21 #include "base/timer/hi_res_timer_manager.h"
22 #include "base/trace_event/trace_event.h"
23 #include "content/child/child_process.h"
24 #include "content/common/content_constants_internal.h"
25 #include "content/public/common/content_switches.h"
26 #include "content/public/common/main_function_params.h"
27 #include "content/public/renderer/content_renderer_client.h"
28 #include "content/renderer/render_process_impl.h"
29 #include "content/renderer/render_thread_impl.h"
30 #include "content/renderer/renderer_main_platform_delegate.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 #include "third_party/skia/include/core/SkGraphics.h"
36 #endif // OS_ANDROID
38 #if defined(OS_MACOSX)
39 #include <Carbon/Carbon.h>
40 #include <signal.h>
41 #include <unistd.h>
43 #include "base/mac/scoped_nsautorelease_pool.h"
44 #include "base/message_loop/message_pump_mac.h"
45 #include "third_party/WebKit/public/web/WebView.h"
46 #endif // OS_MACOSX
48 #if defined(ENABLE_PLUGINS)
49 #include "content/renderer/pepper/pepper_plugin_registry.h"
50 #endif
52 #if defined(ENABLE_WEBRTC)
53 #include "third_party/libjingle/overrides/init_webrtc.h"
54 #endif
56 namespace content {
57 namespace {
58 // This function provides some ways to test crash and assertion handling
59 // behavior of the renderer.
60 static void HandleRendererErrorTestParameters(
61 const base::CommandLine& command_line) {
62 if (command_line.HasSwitch(switches::kWaitForDebugger))
63 base::debug::WaitForDebugger(60, true);
65 if (command_line.HasSwitch(switches::kRendererStartupDialog))
66 ChildProcess::WaitForDebugger("Renderer");
69 // This is a simplified version of the browser Jankometer, which measures
70 // the processing time of tasks on the render thread.
71 class RendererMessageLoopObserver : public base::MessageLoop::TaskObserver {
72 public:
73 RendererMessageLoopObserver()
74 : process_times_(base::Histogram::FactoryGet(
75 "Chrome.ProcMsgL RenderThread",
76 1, 3600000, 50, base::Histogram::kUmaTargetedHistogramFlag)) {}
77 ~RendererMessageLoopObserver() override {}
79 void WillProcessTask(const base::PendingTask& pending_task) override {
80 begin_process_message_ = base::TimeTicks::Now();
83 void DidProcessTask(const base::PendingTask& pending_task) override {
84 if (!begin_process_message_.is_null())
85 process_times_->AddTime(base::TimeTicks::Now() - begin_process_message_);
88 private:
89 base::TimeTicks begin_process_message_;
90 base::HistogramBase* const process_times_;
91 DISALLOW_COPY_AND_ASSIGN(RendererMessageLoopObserver);
94 } // namespace
96 // mainline routine for running as the Renderer process
97 int RendererMain(const MainFunctionParams& parameters) {
98 TRACE_EVENT_BEGIN_ETW("RendererMain", 0, "");
99 base::trace_event::TraceLog::GetInstance()->SetProcessName("Renderer");
100 base::trace_event::TraceLog::GetInstance()->SetProcessSortIndex(
101 kTraceEventRendererProcessSortIndex);
103 const base::CommandLine& parsed_command_line = parameters.command_line;
105 #if defined(OS_MACOSX)
106 base::mac::ScopedNSAutoreleasePool* pool = parameters.autorelease_pool;
107 #endif // OS_MACOSX
109 #if defined(OS_CHROMEOS)
110 // As Zygote process starts up earlier than browser process gets its own
111 // locale (at login time for Chrome OS), we have to set the ICU default
112 // locale for renderer process here.
113 // ICU locale will be used for fallback font selection etc.
114 if (parsed_command_line.HasSwitch(switches::kLang)) {
115 const std::string locale =
116 parsed_command_line.GetSwitchValueASCII(switches::kLang);
117 base::i18n::SetICUDefaultLocale(locale);
119 #endif
121 #if defined(OS_ANDROID)
122 const int kMB = 1024 * 1024;
123 size_t font_cache_limit =
124 base::SysInfo::IsLowEndDevice() ? kMB : 8 * kMB;
125 SkGraphics::SetFontCacheLimit(font_cache_limit);
126 #endif
128 // This function allows pausing execution using the --renderer-startup-dialog
129 // flag allowing us to attach a debugger.
130 // Do not move this function down since that would mean we can't easily debug
131 // whatever occurs before it.
132 HandleRendererErrorTestParameters(parsed_command_line);
134 RendererMainPlatformDelegate platform(parameters);
135 RendererMessageLoopObserver task_observer;
136 #if defined(OS_MACOSX)
137 // As long as scrollbars on Mac are painted with Cocoa, the message pump
138 // needs to be backed by a Foundation-level loop to process NSTimers. See
139 // http://crbug.com/306348#c24 for details.
140 scoped_ptr<base::MessagePump> pump(new base::MessagePumpNSRunLoop());
141 scoped_ptr<base::MessageLoop> main_message_loop(
142 new base::MessageLoop(pump.Pass()));
143 #else
144 // The main message loop of the renderer services doesn't have IO or UI tasks.
145 scoped_ptr<base::MessageLoop> main_message_loop(new base::MessageLoop());
146 #endif
147 main_message_loop->AddTaskObserver(&task_observer);
149 base::PlatformThread::SetName("CrRendererMain");
151 bool no_sandbox = parsed_command_line.HasSwitch(switches::kNoSandbox);
153 // Initialize histogram statistics gathering system.
154 base::StatisticsRecorder::Initialize();
156 #if defined(OS_ANDROID)
157 // If we have a pending chromium android linker histogram, record it.
158 base::android::RecordChromiumAndroidLinkerRendererHistogram();
159 #endif
161 // Initialize statistical testing infrastructure. We set the entropy provider
162 // to NULL to disallow the renderer process from creating its own one-time
163 // randomized trials; they should be created in the browser process.
164 base::FieldTrialList field_trial_list(NULL);
165 // Ensure any field trials in browser are reflected into renderer.
166 if (parsed_command_line.HasSwitch(switches::kForceFieldTrials)) {
167 bool result = base::FieldTrialList::CreateTrialsFromString(
168 parsed_command_line.GetSwitchValueASCII(switches::kForceFieldTrials),
169 base::FieldTrialList::DONT_ACTIVATE_TRIALS,
170 std::set<std::string>());
171 DCHECK(result);
174 // PlatformInitialize uses FieldTrials, so this must happen later.
175 platform.PlatformInitialize();
177 #if defined(ENABLE_PLUGINS)
178 // Load pepper plugins before engaging the sandbox.
179 PepperPluginRegistry::GetInstance();
180 #endif
181 #if defined(ENABLE_WEBRTC)
182 // Initialize WebRTC before engaging the sandbox.
183 // NOTE: On linux, this call could already have been made from
184 // zygote_main_linux.cc. However, calling multiple times from the same thread
185 // is OK.
186 InitializeWebRtcModule();
187 #endif
190 #if defined(OS_WIN) || defined(OS_MACOSX)
191 // TODO(markus): Check if it is OK to unconditionally move this
192 // instruction down.
193 RenderProcessImpl render_process;
194 new RenderThreadImpl(main_message_loop.Pass());
195 #endif
196 bool run_loop = true;
197 if (!no_sandbox) {
198 run_loop = platform.EnableSandbox();
199 } else {
200 LOG(ERROR) << "Running without renderer sandbox";
201 #ifndef NDEBUG
202 // For convenience, we print the stack traces for crashes. When sandbox
203 // is enabled, the in-process stack dumping is enabled as part of the
204 // EnableSandbox() call.
205 base::debug::EnableInProcessStackDumping();
206 #endif
208 #if defined(OS_POSIX) && !defined(OS_MACOSX)
209 RenderProcessImpl render_process;
210 new RenderThreadImpl(main_message_loop.Pass());
211 #endif
212 base::HighResolutionTimerManager hi_res_timer_manager;
214 if (run_loop) {
215 #if defined(OS_MACOSX)
216 if (pool)
217 pool->Recycle();
218 #endif
219 TRACE_EVENT_BEGIN_ETW("RendererMain.START_MSG_LOOP", 0, 0);
220 base::MessageLoop::current()->Run();
221 TRACE_EVENT_END_ETW("RendererMain.START_MSG_LOOP", 0, 0);
223 #if defined(LEAK_SANITIZER)
224 // Run leak detection before RenderProcessImpl goes out of scope. This helps
225 // ignore shutdown-only leaks.
226 __lsan_do_leak_check();
227 #endif
229 platform.PlatformUninitialize();
230 TRACE_EVENT_END_ETW("RendererMain", 0, "");
231 return 0;
234 } // namespace content