blob: 33800c023f557e1d6a61042a4e4411823514c7bb [file] [log] [blame]
[email protected]fd911dd2012-01-27 01:57:101// 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
[email protected]d353541f2012-05-03 22:45:415#include "content/renderer/render_process_impl.h"
6
[email protected]037fce02009-01-22 01:42:157#include "build/build_config.h"
8
[email protected]037fce02009-01-22 01:42:159#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:2910#include <windows.h>
11#include <objidl.h>
12#include <mlang.h>
[email protected]037fce02009-01-22 01:42:1513#endif
initial.commit09911bf2008-07-26 23:55:2914
fdorayd2233a72016-12-13 17:18:2115#include <stddef.h>
16
fdoray743f8d6d2017-02-09 15:24:2317#include <algorithm>
fdoray31cc6f82017-02-10 23:31:1018#include <utility>
fdorayd2233a72016-12-13 17:18:2119
Eric Holk1384f6d2018-01-05 00:49:3620#include "base/base_switches.h"
fdorayd2233a72016-12-13 17:18:2121#include "base/bind.h"
initial.commit09911bf2008-07-26 23:55:2922#include "base/command_line.h"
[email protected]037fce02009-01-22 01:42:1523#include "base/compiler_specific.h"
georgesak80353b52017-01-10 21:18:5124#include "base/debug/crash_logging.h"
Eric Holkdc499db2017-07-17 17:57:3525#include "base/debug/stack_trace.h"
ishell75fddc12016-04-12 14:03:1426#include "base/feature_list.h"
fdoray31cc6f82017-02-10 23:31:1027#include "base/memory/ptr_util.h"
[email protected]35b4f0c2014-06-26 16:55:2728#include "base/sys_info.h"
fdorayd2233a72016-12-13 17:18:2129#include "base/task_scheduler/initialization_util.h"
fdorayd2233a72016-12-13 17:18:2130#include "base/time/time.h"
Francois Doray6d3c649692017-06-16 19:20:2531#include "content/common/task_scheduler.h"
sammc7f6c6a02017-01-30 00:53:5132#include "content/public/common/bindings_policy.h"
fdorayd2233a72016-12-13 17:18:2133#include "content/public/common/content_client.h"
bradnelsonc79f5a6f2016-10-10 18:31:1434#include "content/public/common/content_features.h"
[email protected]c08950d22011-10-13 22:20:2935#include "content/public/common/content_switches.h"
[email protected]d344114c2011-10-01 01:24:3436#include "content/public/renderer/content_renderer_client.h"
Eric Holk1384f6d2018-01-05 00:49:3637#include "services/service_manager/embedder/switches.h"
Blink Reformata30d4232018-04-07 15:31:0638#include "third_party/blink/public/web/web_frame.h"
[email protected]067f5192014-01-29 05:22:0939#include "v8/include/v8.h"
initial.commit09911bf2008-07-26 23:55:2940
georgesak80353b52017-01-10 21:18:5141#if defined(OS_WIN)
42#include "base/win/win_util.h"
43#endif
44
ishell75fddc12016-04-12 14:03:1445namespace {
46
ishell75fddc12016-04-12 14:03:1447void SetV8FlagIfFeature(const base::Feature& feature, const char* v8_flag) {
48 if (base::FeatureList::IsEnabled(feature)) {
49 v8::V8::SetFlagsFromString(v8_flag, strlen(v8_flag));
50 }
51}
52
bradnelson2730e3512017-01-21 20:32:2153void SetV8FlagIfNotFeature(const base::Feature& feature, const char* v8_flag) {
54 if (!base::FeatureList::IsEnabled(feature)) {
55 v8::V8::SetFlagsFromString(v8_flag, strlen(v8_flag));
56 }
57}
58
ishell75fddc12016-04-12 14:03:1459void SetV8FlagIfHasSwitch(const char* switch_name, const char* v8_flag) {
60 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switch_name)) {
61 v8::V8::SetFlagsFromString(v8_flag, strlen(v8_flag));
62 }
63}
64
fdoraya70d7172017-04-04 20:52:2065std::unique_ptr<base::TaskScheduler::InitParams>
66GetDefaultTaskSchedulerInitParams() {
fdoraya70d7172017-04-04 20:52:2067
fdoray743f8d6d2017-02-09 15:24:2368 constexpr int kMaxNumThreadsInBackgroundPool = 1;
fdoraye68b6f92017-02-21 19:39:4669 constexpr int kMaxNumThreadsInBackgroundBlockingPool = 1;
fdorayd2233a72016-12-13 17:18:2170 constexpr int kMaxNumThreadsInForegroundPoolLowerBound = 2;
fdoraye68b6f92017-02-21 19:39:4671 constexpr int kMaxNumThreadsInForegroundBlockingPool = 1;
fdorayd2233a72016-12-13 17:18:2172 constexpr auto kSuggestedReclaimTime = base::TimeDelta::FromSeconds(30);
73
Jeremy Roman04f27c372017-10-27 15:20:5574 return std::make_unique<base::TaskScheduler::InitParams>(
Jeffrey Hefbf000f42017-07-26 22:44:4575 base::SchedulerWorkerPoolParams(kMaxNumThreadsInBackgroundPool,
fdoraya70d7172017-04-04 20:52:2076 kSuggestedReclaimTime),
Jeffrey Hefbf000f42017-07-26 22:44:4577 base::SchedulerWorkerPoolParams(kMaxNumThreadsInBackgroundBlockingPool,
fdoraya70d7172017-04-04 20:52:2078 kSuggestedReclaimTime),
79 base::SchedulerWorkerPoolParams(
Francois Doray6d3c649692017-06-16 19:20:2580 std::max(
81 kMaxNumThreadsInForegroundPoolLowerBound,
82 content::GetMinThreadsInRendererTaskSchedulerForegroundPool()),
fdoraya70d7172017-04-04 20:52:2083 kSuggestedReclaimTime),
Jeffrey Hefbf000f42017-07-26 22:44:4584 base::SchedulerWorkerPoolParams(kMaxNumThreadsInForegroundBlockingPool,
fdoraya70d7172017-04-04 20:52:2085 kSuggestedReclaimTime));
fdorayd2233a72016-12-13 17:18:2186}
87
Weza6ca5b92018-03-23 19:03:0788#if DCHECK_IS_CONFIGURABLE
Sigurdur Asgeirsson379c51e42017-09-21 12:52:4589void V8DcheckCallbackHandler(const char* file, int line, const char* message) {
90 // TODO(siggi): Set a crash key or a breadcrumb so the fact that we hit a
91 // V8 DCHECK gets out in the crash report.
92 ::logging::LogMessage(file, line, logging::LOG_DCHECK).stream() << message;
93}
Weza6ca5b92018-03-23 19:03:0794#endif // DCHECK_IS_CONFIGURABLE
Sigurdur Asgeirsson379c51e42017-09-21 12:52:4595
ishell75fddc12016-04-12 14:03:1496} // namespace
97
[email protected]eb398192012-10-22 20:16:1998namespace content {
99
fdoray31cc6f82017-02-10 23:31:10100RenderProcessImpl::RenderProcessImpl(
fdoraya70d7172017-04-04 20:52:20101 std::unique_ptr<base::TaskScheduler::InitParams> task_scheduler_init_params)
102 : RenderProcess("Renderer", std::move(task_scheduler_init_params)),
fdoray31cc6f82017-02-10 23:31:10103 enabled_bindings_(0) {
Weza6ca5b92018-03-23 19:03:07104#if DCHECK_IS_CONFIGURABLE
105 // Some official builds ship with DCHECKs compiled in. Failing DCHECKs then
106 // are either fatal or simply log the error, based on a feature flag.
Sigurdur Asgeirsson379c51e42017-09-21 12:52:45107 // Make sure V8 follows suit by setting a Dcheck handler that forwards to
108 // the Chrome base logging implementation.
109 v8::V8::SetDcheckErrorHandler(&V8DcheckCallbackHandler);
110
Weza6ca5b92018-03-23 19:03:07111 if (!base::FeatureList::IsEnabled(base::kDCheckIsFatalFeature)) {
Sigurdur Asgeirsson379c51e42017-09-21 12:52:45112 // These V8 flags default on in this build configuration. This triggers
113 // additional verification and code generation, which both slows down V8,
114 // and can lead to fatal CHECKs. Turn these flags down to get something
115 // closer to V8s normal performance and behavior.
116 constexpr char kDisabledFlags[] =
117 "--noturbo_verify "
118 "--noverify_csa "
119 "--noturbo_verify_allocation "
120 "--nodebug_code";
121
122 v8::V8::SetFlagsFromString(kDisabledFlags, sizeof(kDisabledFlags));
123 }
Weza6ca5b92018-03-23 19:03:07124#endif // DCHECK_IS_CONFIGURABLE
Sigurdur Asgeirsson379c51e42017-09-21 12:52:45125
[email protected]35b4f0c2014-06-26 16:55:27126 if (base::SysInfo::IsLowEndDevice()) {
[email protected]067f5192014-01-29 05:22:09127 std::string optimize_flag("--optimize-for-size");
128 v8::V8::SetFlagsFromString(optimize_flag.c_str(),
129 static_cast<int>(optimize_flag.size()));
130 }
[email protected]987422f2013-10-01 10:33:31131
ishell75fddc12016-04-12 14:03:14132 SetV8FlagIfHasSwitch(switches::kDisableJavaScriptHarmonyShipping,
133 "--noharmony-shipping");
134 SetV8FlagIfHasSwitch(switches::kJavaScriptHarmony, "--harmony");
Kouhei Ueno7f2eeeb2017-09-20 10:34:54135 SetV8FlagIfFeature(features::kModuleScriptsDynamicImport,
136 "--harmony-dynamic-import");
Hiroshige Hayashizaki15c2eb92017-10-20 04:57:05137 SetV8FlagIfFeature(features::kModuleScriptsImportMetaUrl,
138 "--harmony-import-meta");
bradnelsonc79f5a6f2016-10-10 18:31:14139 SetV8FlagIfFeature(features::kAsmJsToWebAssembly, "--validate-asm");
Brad Nelson2b2ec4a2017-08-17 21:56:07140 SetV8FlagIfNotFeature(features::kAsmJsToWebAssembly, "--no-validate-asm");
bradnelson2730e3512017-01-21 20:32:21141 SetV8FlagIfNotFeature(features::kWebAssembly,
142 "--wasm-disable-structured-cloning");
ishell75fddc12016-04-12 14:03:14143
Michael Hablich896d52662017-10-23 15:59:57144 SetV8FlagIfFeature(features::kV8VmFuture, "--future");
145 SetV8FlagIfNotFeature(features::kV8VmFuture, "--no-future");
Brad Nelson5cd500e2018-01-04 00:50:19146 SetV8FlagIfFeature(features::kSharedArrayBuffer,
147 "--harmony-sharedarraybuffer");
148 SetV8FlagIfNotFeature(features::kSharedArrayBuffer,
149 "--no-harmony-sharedarraybuffer");
Michael Hablich896d52662017-10-23 15:59:57150
Eric Holkb4f60132017-08-18 19:37:41151 SetV8FlagIfNotFeature(features::kWebAssemblyTrapHandler,
152 "--no-wasm-trap-handler");
Sathya Gunasekaranbc706d62018-02-09 21:44:22153 SetV8FlagIfFeature(features::kArrayPrototypeValues,
154 "--harmony-array-prototype-values");
155 SetV8FlagIfNotFeature(features::kArrayPrototypeValues,
Eric Holk90f2d932018-03-21 23:07:47156 "--no-harmony-array-prototype-values");
Eric Holk2c28bc9e2018-04-02 21:59:44157#if defined(OS_LINUX) && defined(ARCH_CPU_X86_64) && !defined(OS_ANDROID)
Eric Holkdc499db2017-07-17 17:57:35158 if (base::FeatureList::IsEnabled(features::kWebAssemblyTrapHandler)) {
Eric Holk90f2d932018-03-21 23:07:47159 bool use_v8_signal_handler = false;
Eric Holk1384f6d2018-01-05 00:49:36160 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
161 if (!command_line->HasSwitch(
162 service_manager::switches::kDisableInProcessStackTraces)) {
163 base::debug::SetStackDumpFirstChanceCallback(v8::V8::TryHandleSignal);
164 } else if (!command_line->HasSwitch(switches::kEnableCrashReporter) &&
165 !command_line->HasSwitch(
166 switches::kEnableCrashReporterForTesting)) {
167 // If we are using WebAssembly trap handling but both Breakpad and
168 // in-process stack traces are disabled then there will be no signal
169 // handler. In this case, we fall back on V8's default handler
170 // (https://crbug.com/798150).
Eric Holk90f2d932018-03-21 23:07:47171 use_v8_signal_handler = true;
Eric Holk1384f6d2018-01-05 00:49:36172 }
Eric Holk2c28bc9e2018-04-02 21:59:44173 // TODO(eholk): report UMA stat for how often this succeeds
174 v8::V8::EnableWebAssemblyTrapHandler(use_v8_signal_handler);
Eric Holkdc499db2017-07-17 17:57:35175 }
176#endif
177
avi83883c82014-12-23 00:08:49178 const base::CommandLine& command_line =
179 *base::CommandLine::ForCurrentProcess();
ishell75fddc12016-04-12 14:03:14180
[email protected]396c3a462010-03-03 05:03:22181 if (command_line.HasSwitch(switches::kJavaScriptFlags)) {
[email protected]067f5192014-01-29 05:22:09182 std::string flags(
[email protected]95edc392010-07-30 22:00:38183 command_line.GetSwitchValueASCII(switches::kJavaScriptFlags));
[email protected]067f5192014-01-29 05:22:09184 v8::V8::SetFlagsFromString(flags.c_str(), static_cast<int>(flags.size()));
[email protected]396c3a462010-03-03 05:03:22185 }
[email protected]55dd9332013-09-04 17:17:50186
sammc7f6c6a02017-01-30 00:53:51187 if (command_line.HasSwitch(switches::kDomAutomationController))
188 enabled_bindings_ |= BINDINGS_POLICY_DOM_AUTOMATION;
189 if (command_line.HasSwitch(switches::kStatsCollectionController))
190 enabled_bindings_ |= BINDINGS_POLICY_STATS_COLLECTION;
[email protected]e68e62fa2009-02-20 02:00:04191}
192
[email protected]396c3a462010-03-03 05:03:22193RenderProcessImpl::~RenderProcessImpl() {
[email protected]396c3a462010-03-03 05:03:22194#ifndef NDEBUG
Blink Reformat1c4d759e2017-04-09 16:34:54195 int count = blink::WebFrame::InstanceCount();
[email protected]6bd867b2013-07-24 22:10:20196 if (count)
197 DLOG(ERROR) << "WebFrame LEAKED " << count << " TIMES";
[email protected]396c3a462010-03-03 05:03:22198#endif
[email protected]e68e62fa2009-02-20 02:00:04199
[email protected]396c3a462010-03-03 05:03:22200 GetShutDownEvent()->Signal();
[email protected]396c3a462010-03-03 05:03:22201}
[email protected]e68e62fa2009-02-20 02:00:04202
fdoray31cc6f82017-02-10 23:31:10203std::unique_ptr<RenderProcess> RenderProcessImpl::Create() {
fdoraya70d7172017-04-04 20:52:20204 auto task_scheduler_init_params =
205 content::GetContentClient()->renderer()->GetTaskSchedulerInitParams();
206 if (!task_scheduler_init_params)
207 task_scheduler_init_params = GetDefaultTaskSchedulerInitParams();
fdoray31cc6f82017-02-10 23:31:10208
209 return base::WrapUnique(
fdoraya70d7172017-04-04 20:52:20210 new RenderProcessImpl(std::move(task_scheduler_init_params)));
fdoray31cc6f82017-02-10 23:31:10211}
212
[email protected]744c2a22012-03-15 18:42:04213void RenderProcessImpl::AddBindings(int bindings) {
214 enabled_bindings_ |= bindings;
215}
216
217int RenderProcessImpl::GetEnabledBindings() const {
218 return enabled_bindings_;
219}
220
[email protected]eb398192012-10-22 20:16:19221} // namespace content