blob: b875325ed53e98f7d97419629af6dc50701fbd61 [file] [log] [blame]
[email protected]06c694d2012-02-01 22:26:161// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
avi1023d012015-12-25 02:39:145#include <stddef.h>
dcheng07945f632015-12-26 07:59:326#include <utility>
avi1023d012015-12-25 02:39:147
[email protected]f09d55d2011-11-09 20:57:288#include "base/base_switches.h"
initial.commit09911bf2008-07-26 23:55:299#include "base/command_line.h"
[email protected]f09d55d2011-11-09 20:57:2810#include "base/debug/debugger.h"
earthdoka41371f82015-02-18 03:30:1211#include "base/debug/leak_annotations.h"
[email protected]683f9182011-03-07 08:20:4312#include "base/i18n/rtl.h"
[email protected]aaf68892013-07-18 00:11:3013#include "base/message_loop/message_loop.h"
rockotfcf7354b2016-03-08 00:55:1314#include "base/metrics/histogram_macros.h"
[email protected]7c477f82012-08-15 01:41:0115#include "base/metrics/statistics_recorder.h"
[email protected]1d88aea92013-02-03 08:16:2416#include "base/pending_task.h"
fdoraye716a902016-07-05 16:05:4917#include "base/run_loop.h"
[email protected]21aa99682013-06-11 07:17:0118#include "base/strings/string_util.h"
[email protected]35b4f0c2014-06-26 16:55:2719#include "base/sys_info.h"
[email protected]f214f8792011-01-01 02:17:0820#include "base/threading/platform_thread.h"
[email protected]89bf27e2013-06-27 18:04:5621#include "base/timer/hi_res_timer_manager.h"
primiano9e38d552015-01-28 04:18:0122#include "base/trace_event/trace_event.h"
avi1023d012015-12-25 02:39:1423#include "build/build_config.h"
[email protected]10208ea2013-06-06 20:08:0324#include "content/child/child_process.h"
[email protected]91a2aea2013-07-08 23:14:3925#include "content/common/content_constants_internal.h"
rockot19c030e862016-02-26 05:46:5826#include "content/common/mojo/mojo_shell_connection_impl.h"
[email protected]c08950d22011-10-13 22:20:2927#include "content/public/common/content_switches.h"
[email protected]4573fbd2011-10-31 20:25:1828#include "content/public/common/main_function_params.h"
[email protected]68bcd4f2011-11-03 21:48:3729#include "content/public/renderer/content_renderer_client.h"
[email protected]8704f89b2011-04-15 00:30:0530#include "content/renderer/render_process_impl.h"
[email protected]f1a29a02011-10-06 23:08:4431#include "content/renderer/render_thread_impl.h"
[email protected]bf2ecb422011-04-18 17:30:3532#include "content/renderer/renderer_main_platform_delegate.h"
skyostil529caa292016-08-10 17:44:5133#include "third_party/WebKit/public/platform/scheduler/renderer/renderer_scheduler.h"
mtklein696d15652015-07-30 20:30:1934#include "third_party/skia/include/core/SkGraphics.h"
[email protected]683f9182011-03-07 08:20:4335#include "ui/base/ui_base_switches.h"
initial.commit09911bf2008-07-26 23:55:2936
[email protected]8e3f8c82013-08-21 15:09:5537#if defined(OS_ANDROID)
simonb7ae383a2014-10-13 13:22:4438#include "base/android/library_loader/library_loader_hooks.h"
[email protected]8e3f8c82013-08-21 15:09:5539#endif // OS_ANDROID
40
[email protected]41f82d22009-12-11 01:14:5641#if defined(OS_MACOSX)
[email protected]ba64e2b2011-06-14 18:18:3842#include <Carbon/Carbon.h>
43#include <signal.h>
44#include <unistd.h>
[email protected]32834932011-04-13 23:00:3545
[email protected]badf5cf2011-10-29 03:44:4446#include "base/mac/scoped_nsautorelease_pool.h"
[email protected]c457e8f2014-02-28 18:33:3047#include "base/message_loop/message_pump_mac.h"
[email protected]2255a9332013-06-17 05:12:3148#include "third_party/WebKit/public/web/WebView.h"
[email protected]41f82d22009-12-11 01:14:5649#endif // OS_MACOSX
50
thestigc4cac8f2014-09-04 21:17:5051#if defined(ENABLE_PLUGINS)
52#include "content/renderer/pepper/pepper_plugin_registry.h"
53#endif
54
[email protected]91feba42013-07-02 08:23:0155#if defined(ENABLE_WEBRTC)
kjellanderd117efc2016-06-20 20:04:0556#include "third_party/webrtc_overrides/init_webrtc.h"
[email protected]91feba42013-07-02 08:23:0157#endif
58
dongseong.hwang16f5a8a42015-08-06 11:00:3059#if defined(USE_OZONE)
60#include "ui/ozone/public/client_native_pixmap_factory.h"
61#endif
62
[email protected]e9ff79c2012-10-19 21:31:2663namespace content {
[email protected]f3ecd3c2013-07-23 16:47:2164namespace {
initial.commit09911bf2008-07-26 23:55:2965// This function provides some ways to test crash and assertion handling
66// behavior of the renderer.
avi83883c82014-12-23 00:08:4967static void HandleRendererErrorTestParameters(
68 const base::CommandLine& command_line) {
[email protected]f09d55d2011-11-09 20:57:2869 if (command_line.HasSwitch(switches::kWaitForDebugger))
70 base::debug::WaitForDebugger(60, true);
71
72 if (command_line.HasSwitch(switches::kRendererStartupDialog))
[email protected]75fcc272011-03-08 20:50:4873 ChildProcess::WaitForDebugger("Renderer");
initial.commit09911bf2008-07-26 23:55:2974}
75
dongseong.hwang16f5a8a42015-08-06 11:00:3076#if defined(USE_OZONE)
dchengcedca5612016-04-09 01:40:1577base::LazyInstance<std::unique_ptr<ui::ClientNativePixmapFactory>>
78 g_pixmap_factory = LAZY_INSTANCE_INITIALIZER;
dongseong.hwang16f5a8a42015-08-06 11:00:3079#endif
80
[email protected]f3ecd3c2013-07-23 16:47:2181} // namespace
82
[email protected]ccaddf92009-06-19 10:49:3283// mainline routine for running as the Renderer process
[email protected]e9ff79c2012-10-19 21:31:2684int RendererMain(const MainFunctionParams& parameters) {
fdorayf6d86242015-10-08 16:49:5385 // Don't use the TRACE_EVENT0 macro because the tracing infrastructure doesn't
86 // expect synchronous events around the main loop of a thread.
87 TRACE_EVENT_ASYNC_BEGIN0("startup", "RendererMain", 0);
88
ssidb2e3ece2015-02-09 16:02:2089 base::trace_event::TraceLog::GetInstance()->SetProcessName("Renderer");
90 base::trace_event::TraceLog::GetInstance()->SetProcessSortIndex(
[email protected]91a2aea2013-07-08 23:14:3991 kTraceEventRendererProcessSortIndex);
[email protected]a872ea1f2010-08-11 04:45:3392
avi83883c82014-12-23 00:08:4993 const base::CommandLine& parsed_command_line = parameters.command_line;
[email protected]d47bae8c2009-02-25 15:35:2494
[email protected]41f82d22009-12-11 01:14:5695#if defined(OS_MACOSX)
[email protected]badf5cf2011-10-29 03:44:4496 base::mac::ScopedNSAutoreleasePool* pool = parameters.autorelease_pool;
[email protected]41f82d22009-12-11 01:14:5697#endif // OS_MACOSX
98
[email protected]683f9182011-03-07 08:20:4399#if defined(OS_CHROMEOS)
100 // As Zygote process starts up earlier than browser process gets its own
101 // locale (at login time for Chrome OS), we have to set the ICU default
102 // locale for renderer process here.
103 // ICU locale will be used for fallback font selection etc.
104 if (parsed_command_line.HasSwitch(switches::kLang)) {
105 const std::string locale =
106 parsed_command_line.GetSwitchValueASCII(switches::kLang);
107 base::i18n::SetICUDefaultLocale(locale);
108 }
109#endif
110
jyasskin739ff312015-09-10 17:55:59111 SkGraphics::Init();
[email protected]8e3f8c82013-08-21 15:09:55112#if defined(OS_ANDROID)
113 const int kMB = 1024 * 1024;
114 size_t font_cache_limit =
[email protected]35b4f0c2014-06-26 16:55:27115 base::SysInfo::IsLowEndDevice() ? kMB : 8 * kMB;
[email protected]8e3f8c82013-08-21 15:09:55116 SkGraphics::SetFontCacheLimit(font_cache_limit);
117#endif
118
dongseong.hwang16f5a8a42015-08-06 11:00:30119#if defined(USE_OZONE)
120 g_pixmap_factory.Get() = ui::ClientNativePixmapFactory::Create();
121 ui::ClientNativePixmapFactory::SetInstance(g_pixmap_factory.Get().get());
122#endif
123
[email protected]d47bae8c2009-02-25 15:35:24124 // This function allows pausing execution using the --renderer-startup-dialog
125 // flag allowing us to attach a debugger.
126 // Do not move this function down since that would mean we can't easily debug
127 // whatever occurs before it.
128 HandleRendererErrorTestParameters(parsed_command_line);
129
[email protected]0bc2a57b2009-02-04 20:32:54130 RendererMainPlatformDelegate platform(parameters);
[email protected]a0f625c2009-08-20 19:44:07131#if defined(OS_MACOSX)
[email protected]c457e8f2014-02-28 18:33:30132 // As long as scrollbars on Mac are painted with Cocoa, the message pump
133 // needs to be backed by a Foundation-level loop to process NSTimers. See
134 // http://crbug.com/306348#c24 for details.
dchengcedca5612016-04-09 01:40:15135 std::unique_ptr<base::MessagePump> pump(new base::MessagePumpNSRunLoop());
136 std::unique_ptr<base::MessageLoop> main_message_loop(
dchengf26eed32016-01-13 10:58:14137 new base::MessageLoop(std::move(pump)));
[email protected]a0f625c2009-08-20 19:44:07138#else
[email protected]7c85e7a42013-12-26 20:52:58139 // The main message loop of the renderer services doesn't have IO or UI tasks.
dchengcedca5612016-04-09 01:40:15140 std::unique_ptr<base::MessageLoop> main_message_loop(new base::MessageLoop());
[email protected]a0f625c2009-08-20 19:44:07141#endif
[email protected]42f1d7822009-07-23 18:17:55142
[email protected]f214f8792011-01-01 02:17:08143 base::PlatformThread::SetName("CrRendererMain");
initial.commit09911bf2008-07-26 23:55:29144
initial.commit09911bf2008-07-26 23:55:29145 bool no_sandbox = parsed_command_line.HasSwitch(switches::kNoSandbox);
initial.commit09911bf2008-07-26 23:55:29146
[email protected]7c477f82012-08-15 01:41:01147 // Initialize histogram statistics gathering system.
148 base::StatisticsRecorder::Initialize();
149
simonb7ae383a2014-10-13 13:22:44150#if defined(OS_ANDROID)
michaelbai8369bdb2016-05-28 03:37:49151 // If we have any pending LibraryLoader histograms, record them.
152 base::android::RecordLibraryLoaderRendererHistograms();
simonb7ae383a2014-10-13 13:22:44153#endif
154
skyostil529caa292016-08-10 17:44:51155 std::unique_ptr<blink::scheduler::RendererScheduler> renderer_scheduler(
156 blink::scheduler::RendererScheduler::Create());
skyostil819799e2016-01-14 12:15:48157
[email protected]c0a46332014-06-19 01:49:37158 // PlatformInitialize uses FieldTrials, so this must happen later.
159 platform.PlatformInitialize();
160
[email protected]ebd71962012-12-20 02:56:55161#if defined(ENABLE_PLUGINS)
[email protected]53af1d62010-05-14 03:40:26162 // Load pepper plugins before engaging the sandbox.
163 PepperPluginRegistry::GetInstance();
[email protected]ebd71962012-12-20 02:56:55164#endif
[email protected]e4aa290b2013-05-01 18:54:19165#if defined(ENABLE_WEBRTC)
166 // Initialize WebRTC before engaging the sandbox.
167 // NOTE: On linux, this call could already have been made from
168 // zygote_main_linux.cc. However, calling multiple times from the same thread
169 // is OK.
170 InitializeWebRtcModule();
171#endif
[email protected]53af1d62010-05-14 03:40:26172
[email protected]8930d472009-02-21 08:05:28173 {
[email protected]e63c4d72011-05-31 22:38:29174#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]0fb2bd92009-08-11 21:46:07175 // TODO(markus): Check if it is OK to unconditionally move this
176 // instruction down.
[email protected]00c39612010-03-06 02:53:28177 RenderProcessImpl render_process;
dchengf26eed32016-01-13 10:58:14178 RenderThreadImpl::Create(std::move(main_message_loop),
179 std::move(renderer_scheduler));
[email protected]0fb2bd92009-08-11 21:46:07180#endif
initial.commit09911bf2008-07-26 23:55:29181 bool run_loop = true;
jam79dc59a2015-08-17 03:38:16182 if (!no_sandbox)
[email protected]0bc2a57b2009-02-04 20:32:54183 run_loop = platform.EnableSandbox();
[email protected]e63c4d72011-05-31 22:38:29184#if defined(OS_POSIX) && !defined(OS_MACOSX)
[email protected]00c39612010-03-06 02:53:28185 RenderProcessImpl render_process;
dcheng07945f632015-12-26 07:59:32186 RenderThreadImpl::Create(std::move(main_message_loop),
187 std::move(renderer_scheduler));
[email protected]0fb2bd92009-08-11 21:46:07188#endif
fdoray443bd112015-11-16 20:44:23189
[email protected]9dd90152013-08-02 22:09:13190 base::HighResolutionTimerManager hi_res_timer_manager;
191
initial.commit09911bf2008-07-26 23:55:29192 if (run_loop) {
[email protected]badf5cf2011-10-29 03:44:44193#if defined(OS_MACOSX)
[email protected]29712f62009-07-28 16:50:27194 if (pool)
195 pool->Recycle();
[email protected]badf5cf2011-10-29 03:44:44196#endif
fdorayf6d86242015-10-08 16:49:53197 TRACE_EVENT_ASYNC_BEGIN0("toplevel", "RendererMain.START_MSG_LOOP", 0);
fdoraye716a902016-07-05 16:05:49198 base::RunLoop().Run();
fdorayf6d86242015-10-08 16:49:53199 TRACE_EVENT_ASYNC_END0("toplevel", "RendererMain.START_MSG_LOOP", 0);
initial.commit09911bf2008-07-26 23:55:29200 }
Ken Rockot6cb09bb52016-03-01 04:17:49201
earthdoka41371f82015-02-18 03:30:12202#if defined(LEAK_SANITIZER)
203 // Run leak detection before RenderProcessImpl goes out of scope. This helps
204 // ignore shutdown-only leaks.
205 __lsan_do_leak_check();
206#endif
initial.commit09911bf2008-07-26 23:55:29207 }
[email protected]0bc2a57b2009-02-04 20:32:54208 platform.PlatformUninitialize();
fdorayf6d86242015-10-08 16:49:53209 TRACE_EVENT_ASYNC_END0("startup", "RendererMain", 0);
initial.commit09911bf2008-07-26 23:55:29210 return 0;
211}
[email protected]e9ff79c2012-10-19 21:31:26212
213} // namespace content