[email protected] | 06c694d | 2012-02-01 22:26:16 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
avi | 1023d01 | 2015-12-25 02:39:14 | [diff] [blame] | 5 | #include <stddef.h> |
dcheng | 07945f63 | 2015-12-26 07:59:32 | [diff] [blame] | 6 | #include <utility> |
avi | 1023d01 | 2015-12-25 02:39:14 | [diff] [blame] | 7 | |
[email protected] | f09d55d | 2011-11-09 20:57:28 | [diff] [blame] | 8 | #include "base/base_switches.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 9 | #include "base/command_line.h" |
[email protected] | f09d55d | 2011-11-09 20:57:28 | [diff] [blame] | 10 | #include "base/debug/debugger.h" |
earthdok | a41371f8 | 2015-02-18 03:30:12 | [diff] [blame] | 11 | #include "base/debug/leak_annotations.h" |
[email protected] | 683f918 | 2011-03-07 08:20:43 | [diff] [blame] | 12 | #include "base/i18n/rtl.h" |
[email protected] | aaf6889 | 2013-07-18 00:11:30 | [diff] [blame] | 13 | #include "base/message_loop/message_loop.h" |
rockot | fcf7354b | 2016-03-08 00:55:13 | [diff] [blame] | 14 | #include "base/metrics/histogram_macros.h" |
[email protected] | 7c477f8 | 2012-08-15 01:41:01 | [diff] [blame] | 15 | #include "base/metrics/statistics_recorder.h" |
[email protected] | 1d88aea9 | 2013-02-03 08:16:24 | [diff] [blame] | 16 | #include "base/pending_task.h" |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame] | 17 | #include "base/run_loop.h" |
[email protected] | 21aa9968 | 2013-06-11 07:17:01 | [diff] [blame] | 18 | #include "base/strings/string_util.h" |
[email protected] | 35b4f0c | 2014-06-26 16:55:27 | [diff] [blame] | 19 | #include "base/sys_info.h" |
[email protected] | f214f879 | 2011-01-01 02:17:08 | [diff] [blame] | 20 | #include "base/threading/platform_thread.h" |
[email protected] | 89bf27e | 2013-06-27 18:04:56 | [diff] [blame] | 21 | #include "base/timer/hi_res_timer_manager.h" |
primiano | 9e38d55 | 2015-01-28 04:18:01 | [diff] [blame] | 22 | #include "base/trace_event/trace_event.h" |
avi | 1023d01 | 2015-12-25 02:39:14 | [diff] [blame] | 23 | #include "build/build_config.h" |
[email protected] | 10208ea | 2013-06-06 20:08:03 | [diff] [blame] | 24 | #include "content/child/child_process.h" |
[email protected] | 91a2aea | 2013-07-08 23:14:39 | [diff] [blame] | 25 | #include "content/common/content_constants_internal.h" |
rockot | 19c030e86 | 2016-02-26 05:46:58 | [diff] [blame] | 26 | #include "content/common/mojo/mojo_shell_connection_impl.h" |
[email protected] | c08950d2 | 2011-10-13 22:20:29 | [diff] [blame] | 27 | #include "content/public/common/content_switches.h" |
[email protected] | 4573fbd | 2011-10-31 20:25:18 | [diff] [blame] | 28 | #include "content/public/common/main_function_params.h" |
[email protected] | 68bcd4f | 2011-11-03 21:48:37 | [diff] [blame] | 29 | #include "content/public/renderer/content_renderer_client.h" |
[email protected] | 8704f89b | 2011-04-15 00:30:05 | [diff] [blame] | 30 | #include "content/renderer/render_process_impl.h" |
[email protected] | f1a29a0 | 2011-10-06 23:08:44 | [diff] [blame] | 31 | #include "content/renderer/render_thread_impl.h" |
[email protected] | bf2ecb42 | 2011-04-18 17:30:35 | [diff] [blame] | 32 | #include "content/renderer/renderer_main_platform_delegate.h" |
skyostil | 529caa29 | 2016-08-10 17:44:51 | [diff] [blame] | 33 | #include "third_party/WebKit/public/platform/scheduler/renderer/renderer_scheduler.h" |
mtklein | 696d1565 | 2015-07-30 20:30:19 | [diff] [blame] | 34 | #include "third_party/skia/include/core/SkGraphics.h" |
[email protected] | 683f918 | 2011-03-07 08:20:43 | [diff] [blame] | 35 | #include "ui/base/ui_base_switches.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 36 | |
[email protected] | 8e3f8c8 | 2013-08-21 15:09:55 | [diff] [blame] | 37 | #if defined(OS_ANDROID) |
simonb | 7ae383a | 2014-10-13 13:22:44 | [diff] [blame] | 38 | #include "base/android/library_loader/library_loader_hooks.h" |
[email protected] | 8e3f8c8 | 2013-08-21 15:09:55 | [diff] [blame] | 39 | #endif // OS_ANDROID |
| 40 | |
[email protected] | 41f82d2 | 2009-12-11 01:14:56 | [diff] [blame] | 41 | #if defined(OS_MACOSX) |
[email protected] | ba64e2b | 2011-06-14 18:18:38 | [diff] [blame] | 42 | #include <Carbon/Carbon.h> |
| 43 | #include <signal.h> |
| 44 | #include <unistd.h> |
[email protected] | 3283493 | 2011-04-13 23:00:35 | [diff] [blame] | 45 | |
[email protected] | badf5cf | 2011-10-29 03:44:44 | [diff] [blame] | 46 | #include "base/mac/scoped_nsautorelease_pool.h" |
[email protected] | c457e8f | 2014-02-28 18:33:30 | [diff] [blame] | 47 | #include "base/message_loop/message_pump_mac.h" |
[email protected] | 2255a933 | 2013-06-17 05:12:31 | [diff] [blame] | 48 | #include "third_party/WebKit/public/web/WebView.h" |
[email protected] | 41f82d2 | 2009-12-11 01:14:56 | [diff] [blame] | 49 | #endif // OS_MACOSX |
| 50 | |
thestig | c4cac8f | 2014-09-04 21:17:50 | [diff] [blame] | 51 | #if defined(ENABLE_PLUGINS) |
| 52 | #include "content/renderer/pepper/pepper_plugin_registry.h" |
| 53 | #endif |
| 54 | |
[email protected] | 91feba4 | 2013-07-02 08:23:01 | [diff] [blame] | 55 | #if defined(ENABLE_WEBRTC) |
kjellander | d117efc | 2016-06-20 20:04:05 | [diff] [blame] | 56 | #include "third_party/webrtc_overrides/init_webrtc.h" |
[email protected] | 91feba4 | 2013-07-02 08:23:01 | [diff] [blame] | 57 | #endif |
| 58 | |
dongseong.hwang | 16f5a8a4 | 2015-08-06 11:00:30 | [diff] [blame] | 59 | #if defined(USE_OZONE) |
| 60 | #include "ui/ozone/public/client_native_pixmap_factory.h" |
| 61 | #endif |
| 62 | |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 63 | namespace content { |
[email protected] | f3ecd3c | 2013-07-23 16:47:21 | [diff] [blame] | 64 | namespace { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 65 | // This function provides some ways to test crash and assertion handling |
| 66 | // behavior of the renderer. |
avi | 83883c8 | 2014-12-23 00:08:49 | [diff] [blame] | 67 | static void HandleRendererErrorTestParameters( |
| 68 | const base::CommandLine& command_line) { |
[email protected] | f09d55d | 2011-11-09 20:57:28 | [diff] [blame] | 69 | if (command_line.HasSwitch(switches::kWaitForDebugger)) |
| 70 | base::debug::WaitForDebugger(60, true); |
| 71 | |
| 72 | if (command_line.HasSwitch(switches::kRendererStartupDialog)) |
[email protected] | 75fcc27 | 2011-03-08 20:50:48 | [diff] [blame] | 73 | ChildProcess::WaitForDebugger("Renderer"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 74 | } |
| 75 | |
dongseong.hwang | 16f5a8a4 | 2015-08-06 11:00:30 | [diff] [blame] | 76 | #if defined(USE_OZONE) |
dcheng | cedca561 | 2016-04-09 01:40:15 | [diff] [blame] | 77 | base::LazyInstance<std::unique_ptr<ui::ClientNativePixmapFactory>> |
| 78 | g_pixmap_factory = LAZY_INSTANCE_INITIALIZER; |
dongseong.hwang | 16f5a8a4 | 2015-08-06 11:00:30 | [diff] [blame] | 79 | #endif |
| 80 | |
[email protected] | f3ecd3c | 2013-07-23 16:47:21 | [diff] [blame] | 81 | } // namespace |
| 82 | |
[email protected] | ccaddf9 | 2009-06-19 10:49:32 | [diff] [blame] | 83 | // mainline routine for running as the Renderer process |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 84 | int RendererMain(const MainFunctionParams& parameters) { |
fdoray | f6d8624 | 2015-10-08 16:49:53 | [diff] [blame] | 85 | // 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 | |
ssid | b2e3ece | 2015-02-09 16:02:20 | [diff] [blame] | 89 | base::trace_event::TraceLog::GetInstance()->SetProcessName("Renderer"); |
| 90 | base::trace_event::TraceLog::GetInstance()->SetProcessSortIndex( |
[email protected] | 91a2aea | 2013-07-08 23:14:39 | [diff] [blame] | 91 | kTraceEventRendererProcessSortIndex); |
[email protected] | a872ea1f | 2010-08-11 04:45:33 | [diff] [blame] | 92 | |
avi | 83883c8 | 2014-12-23 00:08:49 | [diff] [blame] | 93 | const base::CommandLine& parsed_command_line = parameters.command_line; |
[email protected] | d47bae8c | 2009-02-25 15:35:24 | [diff] [blame] | 94 | |
[email protected] | 41f82d2 | 2009-12-11 01:14:56 | [diff] [blame] | 95 | #if defined(OS_MACOSX) |
[email protected] | badf5cf | 2011-10-29 03:44:44 | [diff] [blame] | 96 | base::mac::ScopedNSAutoreleasePool* pool = parameters.autorelease_pool; |
[email protected] | 41f82d2 | 2009-12-11 01:14:56 | [diff] [blame] | 97 | #endif // OS_MACOSX |
| 98 | |
[email protected] | 683f918 | 2011-03-07 08:20:43 | [diff] [blame] | 99 | #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 | |
jyasskin | 739ff31 | 2015-09-10 17:55:59 | [diff] [blame] | 111 | SkGraphics::Init(); |
[email protected] | 8e3f8c8 | 2013-08-21 15:09:55 | [diff] [blame] | 112 | #if defined(OS_ANDROID) |
| 113 | const int kMB = 1024 * 1024; |
| 114 | size_t font_cache_limit = |
[email protected] | 35b4f0c | 2014-06-26 16:55:27 | [diff] [blame] | 115 | base::SysInfo::IsLowEndDevice() ? kMB : 8 * kMB; |
[email protected] | 8e3f8c8 | 2013-08-21 15:09:55 | [diff] [blame] | 116 | SkGraphics::SetFontCacheLimit(font_cache_limit); |
| 117 | #endif |
| 118 | |
dongseong.hwang | 16f5a8a4 | 2015-08-06 11:00:30 | [diff] [blame] | 119 | #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] | d47bae8c | 2009-02-25 15:35:24 | [diff] [blame] | 124 | // 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] | 0bc2a57b | 2009-02-04 20:32:54 | [diff] [blame] | 130 | RendererMainPlatformDelegate platform(parameters); |
[email protected] | a0f625c | 2009-08-20 19:44:07 | [diff] [blame] | 131 | #if defined(OS_MACOSX) |
[email protected] | c457e8f | 2014-02-28 18:33:30 | [diff] [blame] | 132 | // 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. |
dcheng | cedca561 | 2016-04-09 01:40:15 | [diff] [blame] | 135 | std::unique_ptr<base::MessagePump> pump(new base::MessagePumpNSRunLoop()); |
| 136 | std::unique_ptr<base::MessageLoop> main_message_loop( |
dcheng | f26eed3 | 2016-01-13 10:58:14 | [diff] [blame] | 137 | new base::MessageLoop(std::move(pump))); |
[email protected] | a0f625c | 2009-08-20 19:44:07 | [diff] [blame] | 138 | #else |
[email protected] | 7c85e7a4 | 2013-12-26 20:52:58 | [diff] [blame] | 139 | // The main message loop of the renderer services doesn't have IO or UI tasks. |
dcheng | cedca561 | 2016-04-09 01:40:15 | [diff] [blame] | 140 | std::unique_ptr<base::MessageLoop> main_message_loop(new base::MessageLoop()); |
[email protected] | a0f625c | 2009-08-20 19:44:07 | [diff] [blame] | 141 | #endif |
[email protected] | 42f1d782 | 2009-07-23 18:17:55 | [diff] [blame] | 142 | |
[email protected] | f214f879 | 2011-01-01 02:17:08 | [diff] [blame] | 143 | base::PlatformThread::SetName("CrRendererMain"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 144 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 145 | bool no_sandbox = parsed_command_line.HasSwitch(switches::kNoSandbox); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 146 | |
[email protected] | 7c477f8 | 2012-08-15 01:41:01 | [diff] [blame] | 147 | // Initialize histogram statistics gathering system. |
| 148 | base::StatisticsRecorder::Initialize(); |
| 149 | |
simonb | 7ae383a | 2014-10-13 13:22:44 | [diff] [blame] | 150 | #if defined(OS_ANDROID) |
michaelbai | 8369bdb | 2016-05-28 03:37:49 | [diff] [blame] | 151 | // If we have any pending LibraryLoader histograms, record them. |
| 152 | base::android::RecordLibraryLoaderRendererHistograms(); |
simonb | 7ae383a | 2014-10-13 13:22:44 | [diff] [blame] | 153 | #endif |
| 154 | |
skyostil | 529caa29 | 2016-08-10 17:44:51 | [diff] [blame] | 155 | std::unique_ptr<blink::scheduler::RendererScheduler> renderer_scheduler( |
| 156 | blink::scheduler::RendererScheduler::Create()); |
skyostil | 819799e | 2016-01-14 12:15:48 | [diff] [blame] | 157 | |
[email protected] | c0a4633 | 2014-06-19 01:49:37 | [diff] [blame] | 158 | // PlatformInitialize uses FieldTrials, so this must happen later. |
| 159 | platform.PlatformInitialize(); |
| 160 | |
[email protected] | ebd7196 | 2012-12-20 02:56:55 | [diff] [blame] | 161 | #if defined(ENABLE_PLUGINS) |
[email protected] | 53af1d6 | 2010-05-14 03:40:26 | [diff] [blame] | 162 | // Load pepper plugins before engaging the sandbox. |
| 163 | PepperPluginRegistry::GetInstance(); |
[email protected] | ebd7196 | 2012-12-20 02:56:55 | [diff] [blame] | 164 | #endif |
[email protected] | e4aa290b | 2013-05-01 18:54:19 | [diff] [blame] | 165 | #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] | 53af1d6 | 2010-05-14 03:40:26 | [diff] [blame] | 172 | |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 173 | { |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 174 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | 0fb2bd9 | 2009-08-11 21:46:07 | [diff] [blame] | 175 | // TODO(markus): Check if it is OK to unconditionally move this |
| 176 | // instruction down. |
[email protected] | 00c3961 | 2010-03-06 02:53:28 | [diff] [blame] | 177 | RenderProcessImpl render_process; |
dcheng | f26eed3 | 2016-01-13 10:58:14 | [diff] [blame] | 178 | RenderThreadImpl::Create(std::move(main_message_loop), |
| 179 | std::move(renderer_scheduler)); |
[email protected] | 0fb2bd9 | 2009-08-11 21:46:07 | [diff] [blame] | 180 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 181 | bool run_loop = true; |
jam | 79dc59a | 2015-08-17 03:38:16 | [diff] [blame] | 182 | if (!no_sandbox) |
[email protected] | 0bc2a57b | 2009-02-04 20:32:54 | [diff] [blame] | 183 | run_loop = platform.EnableSandbox(); |
[email protected] | e63c4d7 | 2011-05-31 22:38:29 | [diff] [blame] | 184 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
[email protected] | 00c3961 | 2010-03-06 02:53:28 | [diff] [blame] | 185 | RenderProcessImpl render_process; |
dcheng | 07945f63 | 2015-12-26 07:59:32 | [diff] [blame] | 186 | RenderThreadImpl::Create(std::move(main_message_loop), |
| 187 | std::move(renderer_scheduler)); |
[email protected] | 0fb2bd9 | 2009-08-11 21:46:07 | [diff] [blame] | 188 | #endif |
fdoray | 443bd11 | 2015-11-16 20:44:23 | [diff] [blame] | 189 | |
[email protected] | 9dd9015 | 2013-08-02 22:09:13 | [diff] [blame] | 190 | base::HighResolutionTimerManager hi_res_timer_manager; |
| 191 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 192 | if (run_loop) { |
[email protected] | badf5cf | 2011-10-29 03:44:44 | [diff] [blame] | 193 | #if defined(OS_MACOSX) |
[email protected] | 29712f6 | 2009-07-28 16:50:27 | [diff] [blame] | 194 | if (pool) |
| 195 | pool->Recycle(); |
[email protected] | badf5cf | 2011-10-29 03:44:44 | [diff] [blame] | 196 | #endif |
fdoray | f6d8624 | 2015-10-08 16:49:53 | [diff] [blame] | 197 | TRACE_EVENT_ASYNC_BEGIN0("toplevel", "RendererMain.START_MSG_LOOP", 0); |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame] | 198 | base::RunLoop().Run(); |
fdoray | f6d8624 | 2015-10-08 16:49:53 | [diff] [blame] | 199 | TRACE_EVENT_ASYNC_END0("toplevel", "RendererMain.START_MSG_LOOP", 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 200 | } |
Ken Rockot | 6cb09bb5 | 2016-03-01 04:17:49 | [diff] [blame] | 201 | |
earthdok | a41371f8 | 2015-02-18 03:30:12 | [diff] [blame] | 202 | #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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 207 | } |
[email protected] | 0bc2a57b | 2009-02-04 20:32:54 | [diff] [blame] | 208 | platform.PlatformUninitialize(); |
fdoray | f6d8624 | 2015-10-08 16:49:53 | [diff] [blame] | 209 | TRACE_EVENT_ASYNC_END0("startup", "RendererMain", 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 210 | return 0; |
| 211 | } |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 212 | |
| 213 | } // namespace content |