
Creating objects USING_FAST_MALLOC in the MTS fail due to WTF being allocated and initialized after the main thread. This CL ensures that that happens before the MT is created fixing issues like crbug.com/584196. Bug: 1058645 Change-Id: I6c53e7b5cd48532c2e5ff0d3313e911a5ef4fc6d Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2103787 Commit-Queue: Katie Dillon <kdillon@chromium.org> Reviewed-by: Kentaro Hara <haraken@chromium.org> Reviewed-by: Scott Haseley <shaseley@chromium.org> Cr-Commit-Position: refs/heads/master@{#761573}
246 lines
8.7 KiB
C++
246 lines
8.7 KiB
C++
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#include <stddef.h>
|
|
#include <utility>
|
|
|
|
#include "base/base_switches.h"
|
|
#include "base/command_line.h"
|
|
#include "base/debug/debugger.h"
|
|
#include "base/debug/leak_annotations.h"
|
|
#include "base/i18n/rtl.h"
|
|
#include "base/message_loop/message_pump.h"
|
|
#include "base/message_loop/message_pump_type.h"
|
|
#include "base/metrics/histogram_macros.h"
|
|
#include "base/pending_task.h"
|
|
#include "base/run_loop.h"
|
|
#include "base/strings/string_number_conversions.h"
|
|
#include "base/system/sys_info.h"
|
|
#include "base/task/sequence_manager/sequence_manager.h"
|
|
#include "base/threading/platform_thread.h"
|
|
#include "base/timer/hi_res_timer_manager.h"
|
|
#include "base/trace_event/trace_event.h"
|
|
#include "build/build_config.h"
|
|
#include "content/common/content_constants_internal.h"
|
|
#include "content/common/content_switches_internal.h"
|
|
#include "content/common/skia_utils.h"
|
|
#include "content/public/common/content_switches.h"
|
|
#include "content/public/common/main_function_params.h"
|
|
#include "content/public/renderer/content_renderer_client.h"
|
|
#include "content/public/renderer/render_thread.h"
|
|
#include "content/renderer/render_process_impl.h"
|
|
#include "content/renderer/render_thread_impl.h"
|
|
#include "content/renderer/renderer_main_platform_delegate.h"
|
|
#include "media/media_buildflags.h"
|
|
#include "mojo/public/cpp/bindings/mojo_buildflags.h"
|
|
#include "ppapi/buildflags/buildflags.h"
|
|
#include "services/service_manager/sandbox/switches.h"
|
|
#include "services/tracing/public/cpp/trace_startup.h"
|
|
#include "third_party/blink/public/platform/platform.h"
|
|
#include "third_party/blink/public/platform/scheduler/web_thread_scheduler.h"
|
|
#include "third_party/webrtc_overrides/init_webrtc.h" // nogncheck
|
|
#include "ui/base/ui_base_switches.h"
|
|
|
|
#if defined(OS_ANDROID)
|
|
#include "base/android/library_loader/library_loader_hooks.h"
|
|
#endif // OS_ANDROID
|
|
|
|
#if defined(OS_MACOSX)
|
|
#include <Carbon/Carbon.h>
|
|
#include <signal.h>
|
|
#include <unistd.h>
|
|
|
|
#include "base/mac/scoped_nsautorelease_pool.h"
|
|
#include "base/message_loop/message_pump_mac.h"
|
|
#include "third_party/blink/public/web/web_view.h"
|
|
#endif // OS_MACOSX
|
|
|
|
#if defined(OS_CHROMEOS)
|
|
#include "chromeos/system/core_scheduling.h"
|
|
#endif // OS_CHROMEOS
|
|
|
|
#if BUILDFLAG(ENABLE_PLUGINS)
|
|
#include "content/renderer/pepper/pepper_plugin_registry.h"
|
|
#endif
|
|
|
|
#if BUILDFLAG(MOJO_RANDOM_DELAYS_ENABLED)
|
|
#include "mojo/public/cpp/bindings/lib/test_random_mojo_delays.h"
|
|
#endif
|
|
|
|
namespace content {
|
|
namespace {
|
|
|
|
// This function provides some ways to test crash and assertion handling
|
|
// behavior of the renderer.
|
|
static void HandleRendererErrorTestParameters(
|
|
const base::CommandLine& command_line) {
|
|
if (command_line.HasSwitch(switches::kWaitForDebugger))
|
|
base::debug::WaitForDebugger(60, true);
|
|
|
|
if (command_line.HasSwitch(switches::kRendererStartupDialog))
|
|
WaitForDebugger("Renderer");
|
|
}
|
|
|
|
std::unique_ptr<base::MessagePump> CreateMainThreadMessagePump() {
|
|
#if defined(OS_MACOSX)
|
|
// As long as scrollbars on Mac are painted with Cocoa, the message pump
|
|
// needs to be backed by a Foundation-level loop to process NSTimers. See
|
|
// http://crbug.com/306348#c24 for details.
|
|
return base::MessagePump::Create(base::MessagePumpType::NS_RUNLOOP);
|
|
#elif defined(OS_FUCHSIA)
|
|
// Allow FIDL APIs on renderer main thread.
|
|
return base::MessagePump::Create(base::MessagePumpType::IO);
|
|
#else
|
|
return base::MessagePump::Create(base::MessagePumpType::DEFAULT);
|
|
#endif
|
|
}
|
|
|
|
} // namespace
|
|
|
|
// mainline routine for running as the Renderer process
|
|
int RendererMain(const MainFunctionParams& parameters) {
|
|
// Don't use the TRACE_EVENT0 macro because the tracing infrastructure doesn't
|
|
// expect synchronous events around the main loop of a thread.
|
|
TRACE_EVENT_ASYNC_BEGIN1("startup", "RendererMain", 0, "zygote_child", false);
|
|
|
|
base::trace_event::TraceLog::GetInstance()->set_process_name("Renderer");
|
|
base::trace_event::TraceLog::GetInstance()->SetProcessSortIndex(
|
|
kTraceEventRendererProcessSortIndex);
|
|
|
|
const base::CommandLine& command_line = parameters.command_line;
|
|
|
|
#if defined(OS_MACOSX)
|
|
base::mac::ScopedNSAutoreleasePool* pool = parameters.autorelease_pool;
|
|
#endif // OS_MACOSX
|
|
|
|
#if defined(OS_CHROMEOS)
|
|
// As Zygote process starts up earlier than browser process gets its own
|
|
// locale (at login time for Chrome OS), we have to set the ICU default
|
|
// locale for renderer process here.
|
|
// ICU locale will be used for fallback font selection etc.
|
|
if (command_line.HasSwitch(switches::kLang)) {
|
|
const std::string locale =
|
|
command_line.GetSwitchValueASCII(switches::kLang);
|
|
base::i18n::SetICUDefaultLocale(locale);
|
|
}
|
|
|
|
// When we start the renderer on ChromeOS if the system has core scheduling
|
|
// available we want to turn it on.
|
|
chromeos::system::EnableCoreSchedulingIfAvailable();
|
|
#endif
|
|
|
|
InitializeSkia();
|
|
|
|
// This function allows pausing execution using the --renderer-startup-dialog
|
|
// flag allowing us to attach a debugger.
|
|
// Do not move this function down since that would mean we can't easily debug
|
|
// whatever occurs before it.
|
|
HandleRendererErrorTestParameters(command_line);
|
|
|
|
RendererMainPlatformDelegate platform(parameters);
|
|
|
|
base::PlatformThread::SetName("CrRendererMain");
|
|
|
|
// Force main thread initialization. When the implementation is based on a
|
|
// better means of determining which is the main thread, remove.
|
|
RenderThread::IsMainThread();
|
|
|
|
#if defined(OS_ANDROID)
|
|
// If we have any pending LibraryLoader histograms, record them.
|
|
base::android::RecordLibraryLoaderRendererHistograms();
|
|
#endif
|
|
|
|
base::Optional<base::Time> initial_virtual_time;
|
|
if (command_line.HasSwitch(switches::kInitialVirtualTime)) {
|
|
double initial_time;
|
|
if (base::StringToDouble(
|
|
command_line.GetSwitchValueASCII(switches::kInitialVirtualTime),
|
|
&initial_time)) {
|
|
initial_virtual_time = base::Time::FromDoubleT(initial_time);
|
|
}
|
|
}
|
|
|
|
blink::Platform::InitializeBlink();
|
|
std::unique_ptr<blink::scheduler::WebThreadScheduler> main_thread_scheduler =
|
|
blink::scheduler::WebThreadScheduler::CreateMainThreadScheduler(
|
|
CreateMainThreadMessagePump(), initial_virtual_time);
|
|
|
|
platform.PlatformInitialize();
|
|
|
|
#if BUILDFLAG(ENABLE_PLUGINS)
|
|
// Load pepper plugins before engaging the sandbox.
|
|
PepperPluginRegistry::GetInstance();
|
|
#endif
|
|
// Initialize WebRTC before engaging the sandbox.
|
|
// NOTE: On linux, this call could already have been made from
|
|
// zygote_main_linux.cc. However, calling multiple times from the same thread
|
|
// is OK.
|
|
InitializeWebRtcModule();
|
|
|
|
{
|
|
bool should_run_loop = true;
|
|
bool need_sandbox =
|
|
!command_line.HasSwitch(service_manager::switches::kNoSandbox);
|
|
|
|
#if !defined(OS_WIN) && !defined(OS_MACOSX)
|
|
// Sandbox is enabled before RenderProcess initialization on all platforms,
|
|
// except Windows and Mac.
|
|
// TODO(markus): Check if it is OK to remove ifdefs for Windows and Mac.
|
|
if (need_sandbox) {
|
|
should_run_loop = platform.EnableSandbox();
|
|
need_sandbox = false;
|
|
}
|
|
#endif
|
|
|
|
std::unique_ptr<RenderProcess> render_process = RenderProcessImpl::Create();
|
|
// It's not a memory leak since RenderThread has the same lifetime
|
|
// as a renderer process.
|
|
base::RunLoop run_loop;
|
|
new RenderThreadImpl(run_loop.QuitClosure(),
|
|
std::move(main_thread_scheduler));
|
|
|
|
#if defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX)
|
|
// Startup tracing is usually enabled earlier, but if we forked from a
|
|
// zygote, we can only enable it after mojo IPC support is brought up
|
|
// initialized by RenderThreadImpl, because the mojo broker has to create
|
|
// the tracing SMB on our behalf due to the zygote sandbox.
|
|
if (parameters.zygote_child) {
|
|
tracing::EnableStartupTracingIfNeeded();
|
|
TRACE_EVENT_ASYNC_BEGIN1("startup", "RendererMain", 0, "zygote_child",
|
|
true);
|
|
}
|
|
#endif // OS_POSIX && !OS_ANDROID && !!OS_MACOSX
|
|
|
|
if (need_sandbox)
|
|
should_run_loop = platform.EnableSandbox();
|
|
|
|
#if BUILDFLAG(MOJO_RANDOM_DELAYS_ENABLED)
|
|
mojo::BeginRandomMojoDelays();
|
|
#endif
|
|
|
|
base::HighResolutionTimerManager hi_res_timer_manager;
|
|
|
|
if (should_run_loop) {
|
|
#if defined(OS_MACOSX)
|
|
if (pool)
|
|
pool->Recycle();
|
|
#endif
|
|
TRACE_EVENT_ASYNC_BEGIN0("toplevel", "RendererMain.START_MSG_LOOP", 0);
|
|
run_loop.Run();
|
|
TRACE_EVENT_ASYNC_END0("toplevel", "RendererMain.START_MSG_LOOP", 0);
|
|
}
|
|
|
|
#if defined(LEAK_SANITIZER)
|
|
// Run leak detection before RenderProcessImpl goes out of scope. This helps
|
|
// ignore shutdown-only leaks.
|
|
__lsan_do_leak_check();
|
|
#endif
|
|
}
|
|
platform.PlatformUninitialize();
|
|
TRACE_EVENT_ASYNC_END0("startup", "RendererMain", 0);
|
|
return 0;
|
|
}
|
|
|
|
} // namespace content
|