[content shell] implement testRunner.overridePreference
[chromium-blink-merge.git] / content / renderer / renderer_main.cc
blob245064821a240e55dfd63bf4874e1e19d5a2d537
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/process_util.h"
20 #include "base/string_util.h"
21 #include "base/system_monitor/system_monitor.h"
22 #include "base/threading/platform_thread.h"
23 #include "base/time.h"
24 #include "content/common/pepper_plugin_registry.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/browser_plugin/browser_plugin_manager_impl.h"
29 #include "content/renderer/render_process_impl.h"
30 #include "content/renderer/render_thread_impl.h"
31 #include "content/renderer/renderer_main_platform_delegate.h"
32 #include "ui/base/ui_base_switches.h"
33 #include "webkit/plugins/ppapi/ppapi_interface_factory.h"
35 #if defined(OS_MACOSX)
36 #include <Carbon/Carbon.h>
37 #include <signal.h>
38 #include <unistd.h>
40 #include "base/mac/mac_util.h"
41 #include "base/mac/scoped_nsautorelease_pool.h"
42 #include "third_party/mach_override/mach_override.h"
43 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
44 #endif // OS_MACOSX
46 namespace content {
48 namespace {
50 #if defined(OS_MACOSX)
52 CFArrayRef ChromeTISCreateInputSourceList(
53 CFDictionaryRef properties,
54 Boolean includeAllInstalled) {
55 CFTypeRef values[] = { CFSTR("") };
56 return CFArrayCreate(
57 kCFAllocatorDefault, values, arraysize(values), &kCFTypeArrayCallBacks);
60 void InstallFrameworkHacks() {
61 // See http://crbug.com/31225
62 // TODO: Don't do this on newer OS X revisions that have a fix for
63 // http://openradar.appspot.com/radar?id=1156410
64 // To check if this is broken:
65 // 1. Enable Multi language input (simplified chinese)
66 // 2. Ensure "Show/Hide Trackpad Handwriting" shortcut works.
67 // (ctrl+shift+space).
68 // 3. Now open a new tab in Google Chrome or start Google Chrome
69 // 4. Try ctrl+shift+space shortcut again. Shortcut will not work, IME will
70 // either not appear or (worse) not disappear on ctrl-shift-space.
71 // (Run `ps aux | grep Chinese` (10.6/10.7) or `ps aux | grep Trackpad`
72 // and then kill that pid to make it go away.)
74 // Chinese Handwriting was introduced in 10.6 and is confirmed broken on
75 // 10.6, 10.7, and 10.8.
76 mach_error_t err = mach_override_ptr(
77 (void*)&TISCreateInputSourceList,
78 (void*)&ChromeTISCreateInputSourceList,
79 NULL);
80 CHECK_EQ(err_none, err);
83 #endif // OS_MACOSX
85 } // namespace
87 // This function provides some ways to test crash and assertion handling
88 // behavior of the renderer.
89 static void HandleRendererErrorTestParameters(const CommandLine& command_line) {
90 if (command_line.HasSwitch(switches::kWaitForDebugger))
91 base::debug::WaitForDebugger(60, true);
93 if (command_line.HasSwitch(switches::kRendererStartupDialog))
94 ChildProcess::WaitForDebugger("Renderer");
96 // This parameter causes an assertion.
97 if (command_line.HasSwitch(switches::kRendererAssertTest)) {
98 DCHECK(false);
102 // This is a simplified version of the browser Jankometer, which measures
103 // the processing time of tasks on the render thread.
104 class RendererMessageLoopObserver : public MessageLoop::TaskObserver {
105 public:
106 RendererMessageLoopObserver()
107 : process_times_(base::Histogram::FactoryGet(
108 "Chrome.ProcMsgL RenderThread",
109 1, 3600000, 50, base::Histogram::kUmaTargetedHistogramFlag)) {}
110 virtual ~RendererMessageLoopObserver() {}
112 virtual void WillProcessTask(base::TimeTicks time_posted) {
113 begin_process_message_ = base::TimeTicks::Now();
116 virtual void DidProcessTask(base::TimeTicks time_posted) {
117 if (!begin_process_message_.is_null())
118 process_times_->AddTime(base::TimeTicks::Now() - begin_process_message_);
121 private:
122 base::TimeTicks begin_process_message_;
123 base::Histogram* const process_times_;
124 DISALLOW_COPY_AND_ASSIGN(RendererMessageLoopObserver);
127 // mainline routine for running as the Renderer process
128 int RendererMain(const MainFunctionParams& parameters) {
129 TRACE_EVENT_BEGIN_ETW("RendererMain", 0, "");
131 const CommandLine& parsed_command_line = parameters.command_line;
133 #if defined(OS_MACOSX)
134 base::mac::ScopedNSAutoreleasePool* pool = parameters.autorelease_pool;
135 InstallFrameworkHacks();
136 #endif // OS_MACOSX
138 #if defined(OS_CHROMEOS)
139 // As Zygote process starts up earlier than browser process gets its own
140 // locale (at login time for Chrome OS), we have to set the ICU default
141 // locale for renderer process here.
142 // ICU locale will be used for fallback font selection etc.
143 if (parsed_command_line.HasSwitch(switches::kLang)) {
144 const std::string locale =
145 parsed_command_line.GetSwitchValueASCII(switches::kLang);
146 base::i18n::SetICUDefaultLocale(locale);
148 #endif
150 // This function allows pausing execution using the --renderer-startup-dialog
151 // flag allowing us to attach a debugger.
152 // Do not move this function down since that would mean we can't easily debug
153 // whatever occurs before it.
154 HandleRendererErrorTestParameters(parsed_command_line);
156 RendererMainPlatformDelegate platform(parameters);
158 webkit::ppapi::PpapiInterfaceFactoryManager* factory_manager =
159 webkit::ppapi::PpapiInterfaceFactoryManager::GetInstance();
160 GetContentClient()->renderer()->RegisterPPAPIInterfaceFactories(
161 factory_manager);
163 base::StatsCounterTimer stats_counter_timer("Content.RendererInit");
164 base::StatsScope<base::StatsCounterTimer> startup_timer(stats_counter_timer);
166 RendererMessageLoopObserver task_observer;
167 #if defined(OS_MACOSX)
168 // As long as we use Cocoa in the renderer (for the forseeable future as of
169 // now; see http://crbug.com/13890 for info) we need to have a UI loop.
170 MessageLoop main_message_loop(MessageLoop::TYPE_UI);
171 #else
172 // The main message loop of the renderer services doesn't have IO or UI tasks,
173 // unless in-process-plugins is used.
174 MessageLoop main_message_loop(RenderProcessImpl::InProcessPlugins() ?
175 MessageLoop::TYPE_UI : MessageLoop::TYPE_DEFAULT);
176 #endif
177 main_message_loop.AddTaskObserver(&task_observer);
179 base::PlatformThread::SetName("CrRendererMain");
181 base::SystemMonitor system_monitor;
182 HighResolutionTimerManager hi_res_timer_manager;
184 platform.PlatformInitialize();
186 bool no_sandbox = parsed_command_line.HasSwitch(switches::kNoSandbox);
187 platform.InitSandboxTests(no_sandbox);
189 // Initialize histogram statistics gathering system.
190 base::StatisticsRecorder::Initialize();
192 // Initialize statistical testing infrastructure. We set the entropy provider
193 // to NULL to disallow the renderer process from creating its own one-time
194 // randomized trials; they should be created in the browser process.
195 base::FieldTrialList field_trial_list(NULL);
196 // Ensure any field trials in browser are reflected into renderer.
197 if (parsed_command_line.HasSwitch(switches::kForceFieldTrials)) {
198 std::string persistent = parsed_command_line.GetSwitchValueASCII(
199 switches::kForceFieldTrials);
200 bool ret = base::FieldTrialList::CreateTrialsFromString(persistent);
201 DCHECK(ret);
204 // Load pepper plugins before engaging the sandbox.
205 PepperPluginRegistry::GetInstance();
208 #if defined(OS_WIN) || defined(OS_MACOSX)
209 // TODO(markus): Check if it is OK to unconditionally move this
210 // instruction down.
211 RenderProcessImpl render_process;
212 new RenderThreadImpl();
213 #endif
214 bool run_loop = true;
215 if (!no_sandbox) {
216 run_loop = platform.EnableSandbox();
217 } else {
218 LOG(ERROR) << "Running without renderer sandbox";
219 #ifndef NDEBUG
220 // For convenience, we print the stack trace for crashes. We can't get
221 // symbols when the sandbox is enabled, so only try when the sandbox is
222 // disabled.
223 base::debug::EnableInProcessStackDumping();
224 #endif
226 #if defined(OS_POSIX) && !defined(OS_MACOSX)
227 RenderProcessImpl render_process;
228 new RenderThreadImpl();
229 #endif
231 platform.RunSandboxTests(no_sandbox);
233 startup_timer.Stop(); // End of Startup Time Measurement.
235 if (run_loop) {
236 #if defined(OS_MACOSX)
237 if (pool)
238 pool->Recycle();
239 #endif
240 TRACE_EVENT_BEGIN_ETW("RendererMain.START_MSG_LOOP", 0, 0);
241 MessageLoop::current()->Run();
242 TRACE_EVENT_END_ETW("RendererMain.START_MSG_LOOP", 0, 0);
245 platform.PlatformUninitialize();
246 TRACE_EVENT_END_ETW("RendererMain", 0, "");
247 return 0;
250 } // namespace content