[ServiceWorker] Implement WebServiceWorkerContextClient::openWindow().
[chromium-blink-merge.git] / content / renderer / renderer_main.cc
blob85d78c41145eb1898e028c77fec98a72946c2b42
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/i18n/rtl.h"
10 #include "base/memory/ref_counted.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/metrics/stats_counters.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::debug::TraceLog::GetInstance()->SetProcessName("Renderer");
100 base::debug::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);
137 base::StatsCounterTimer stats_counter_timer("Content.RendererInit");
138 base::StatsScope<base::StatsCounterTimer> startup_timer(stats_counter_timer);
140 RendererMessageLoopObserver task_observer;
141 #if defined(OS_MACOSX)
142 // As long as scrollbars on Mac are painted with Cocoa, the message pump
143 // needs to be backed by a Foundation-level loop to process NSTimers. See
144 // http://crbug.com/306348#c24 for details.
145 scoped_ptr<base::MessagePump> pump(new base::MessagePumpNSRunLoop());
146 scoped_ptr<base::MessageLoop> main_message_loop(
147 new base::MessageLoop(pump.Pass()));
148 #else
149 // The main message loop of the renderer services doesn't have IO or UI tasks.
150 scoped_ptr<base::MessageLoop> main_message_loop(new base::MessageLoop());
151 #endif
152 main_message_loop->AddTaskObserver(&task_observer);
154 base::PlatformThread::SetName("CrRendererMain");
156 bool no_sandbox = parsed_command_line.HasSwitch(switches::kNoSandbox);
158 // Initialize histogram statistics gathering system.
159 base::StatisticsRecorder::Initialize();
161 #if defined(OS_ANDROID)
162 // If we have a pending chromium android linker histogram, record it.
163 base::android::RecordChromiumAndroidLinkerRendererHistogram();
164 #endif
166 // Initialize statistical testing infrastructure. We set the entropy provider
167 // to NULL to disallow the renderer process from creating its own one-time
168 // randomized trials; they should be created in the browser process.
169 base::FieldTrialList field_trial_list(NULL);
170 // Ensure any field trials in browser are reflected into renderer.
171 if (parsed_command_line.HasSwitch(switches::kForceFieldTrials)) {
172 bool result = base::FieldTrialList::CreateTrialsFromString(
173 parsed_command_line.GetSwitchValueASCII(switches::kForceFieldTrials),
174 base::FieldTrialList::DONT_ACTIVATE_TRIALS,
175 std::set<std::string>());
176 DCHECK(result);
179 // PlatformInitialize uses FieldTrials, so this must happen later.
180 platform.PlatformInitialize();
182 #if defined(ENABLE_PLUGINS)
183 // Load pepper plugins before engaging the sandbox.
184 PepperPluginRegistry::GetInstance();
185 #endif
186 #if defined(ENABLE_WEBRTC)
187 // Initialize WebRTC before engaging the sandbox.
188 // NOTE: On linux, this call could already have been made from
189 // zygote_main_linux.cc. However, calling multiple times from the same thread
190 // is OK.
191 InitializeWebRtcModule();
192 #endif
195 #if defined(OS_WIN) || defined(OS_MACOSX)
196 // TODO(markus): Check if it is OK to unconditionally move this
197 // instruction down.
198 RenderProcessImpl render_process;
199 new RenderThreadImpl(main_message_loop.Pass());
200 #endif
201 bool run_loop = true;
202 if (!no_sandbox) {
203 run_loop = platform.EnableSandbox();
204 } else {
205 LOG(ERROR) << "Running without renderer sandbox";
206 #ifndef NDEBUG
207 // For convenience, we print the stack traces for crashes. When sandbox
208 // is enabled, the in-process stack dumping is enabled as part of the
209 // EnableSandbox() call.
210 base::debug::EnableInProcessStackDumping();
211 #endif
213 #if defined(OS_POSIX) && !defined(OS_MACOSX)
214 RenderProcessImpl render_process;
215 new RenderThreadImpl(main_message_loop.Pass());
216 #endif
218 base::HighResolutionTimerManager hi_res_timer_manager;
220 startup_timer.Stop(); // End of Startup Time Measurement.
222 if (run_loop) {
223 #if defined(OS_MACOSX)
224 if (pool)
225 pool->Recycle();
226 #endif
227 TRACE_EVENT_BEGIN_ETW("RendererMain.START_MSG_LOOP", 0, 0);
228 base::MessageLoop::current()->Run();
229 TRACE_EVENT_END_ETW("RendererMain.START_MSG_LOOP", 0, 0);
232 platform.PlatformUninitialize();
233 TRACE_EVENT_END_ETW("RendererMain", 0, "");
234 return 0;
237 } // namespace content