blob: db042b903f1748a8b4ff9fbb4e92f0b3bb048555 [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
17#include <vector>
18
19#include "base/bind.h"
initial.commit09911bf2008-07-26 23:55:2920#include "base/command_line.h"
[email protected]037fce02009-01-22 01:42:1521#include "base/compiler_specific.h"
georgesak80353b52017-01-10 21:18:5122#include "base/debug/crash_logging.h"
ishell75fddc12016-04-12 14:03:1423#include "base/feature_list.h"
[email protected]35b4f0c2014-06-26 16:55:2724#include "base/sys_info.h"
fdorayd2233a72016-12-13 17:18:2125#include "base/task_scheduler/initialization_util.h"
26#include "base/task_scheduler/scheduler_worker_pool_params.h"
27#include "base/task_scheduler/task_scheduler.h"
28#include "base/task_scheduler/task_traits.h"
29#include "base/threading/platform_thread.h"
30#include "base/time/time.h"
nicke7cd12a2015-06-17 06:48:3831#include "content/child/site_isolation_stats_gatherer.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"
[email protected]6bd867b2013-07-24 22:10:2037#include "third_party/WebKit/public/web/WebFrame.h"
[email protected]067f5192014-01-29 05:22:0938#include "v8/include/v8.h"
initial.commit09911bf2008-07-26 23:55:2939
georgesak80353b52017-01-10 21:18:5140#if defined(OS_WIN)
41#include "base/win/win_util.h"
42#endif
43
ishell75fddc12016-04-12 14:03:1444namespace {
45
fdorayd2233a72016-12-13 17:18:2146enum WorkerPoolType : size_t {
47 BACKGROUND = 0,
48 BACKGROUND_FILE_IO,
49 FOREGROUND,
50 FOREGROUND_FILE_IO,
51 WORKER_POOL_COUNT // Always last.
52};
53
ishell75fddc12016-04-12 14:03:1454const base::Feature kV8_ES2015_TailCalls_Feature {
55 "V8_ES2015_TailCalls", base::FEATURE_DISABLED_BY_DEFAULT
56};
57
ishell73f577b2016-04-14 11:59:3958const base::Feature kV8_ES2016_ExplicitTailCalls_Feature{
59 "V8_ES2016_ExplicitTailCalls", base::FEATURE_DISABLED_BY_DEFAULT};
60
ishell75fddc12016-04-12 14:03:1461const base::Feature kV8SerializeEagerFeature{"V8_Serialize_Eager",
62 base::FEATURE_DISABLED_BY_DEFAULT};
63
64const base::Feature kV8SerializeAgeCodeFeature{
65 "V8_Serialize_Age_Code", base::FEATURE_DISABLED_BY_DEFAULT};
66
67void SetV8FlagIfFeature(const base::Feature& feature, const char* v8_flag) {
68 if (base::FeatureList::IsEnabled(feature)) {
69 v8::V8::SetFlagsFromString(v8_flag, strlen(v8_flag));
70 }
71}
72
bradnelson2730e3512017-01-21 20:32:2173void SetV8FlagIfNotFeature(const base::Feature& feature, const char* v8_flag) {
74 if (!base::FeatureList::IsEnabled(feature)) {
75 v8::V8::SetFlagsFromString(v8_flag, strlen(v8_flag));
76 }
77}
78
ishell75fddc12016-04-12 14:03:1479void SetV8FlagIfHasSwitch(const char* switch_name, const char* v8_flag) {
80 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switch_name)) {
81 v8::V8::SetFlagsFromString(v8_flag, strlen(v8_flag));
82 }
83}
84
fdorayd2233a72016-12-13 17:18:2185std::vector<base::SchedulerWorkerPoolParams>
86GetDefaultSchedulerWorkerPoolParams() {
87 using StandbyThreadPolicy =
88 base::SchedulerWorkerPoolParams::StandbyThreadPolicy;
89 using ThreadPriority = base::ThreadPriority;
90 constexpr size_t kMaxNumThreadsInBackgroundPool = 1;
91 constexpr size_t kMaxNumThreadsInBackgroundFileIOPool = 1;
92 constexpr int kMaxNumThreadsInForegroundPoolLowerBound = 2;
93 constexpr int kMaxNumThreadsInForegroundPoolUpperBound = 4;
94 constexpr double kMaxNumThreadsInForegroundPoolCoresMultiplier = 1;
95 constexpr int kMaxNumThreadsInForegroundPoolOffset = 0;
96 constexpr size_t kMaxNumThreadsInForegroundFileIOPool = 1;
97 constexpr auto kSuggestedReclaimTime = base::TimeDelta::FromSeconds(30);
98
99 std::vector<base::SchedulerWorkerPoolParams> params_vector;
100 params_vector.emplace_back("RendererBackground", ThreadPriority::BACKGROUND,
101 StandbyThreadPolicy::LAZY,
102 kMaxNumThreadsInBackgroundPool,
103 kSuggestedReclaimTime);
104 params_vector.emplace_back(
105 "RendererBackgroundFileIO", ThreadPriority::BACKGROUND,
106 StandbyThreadPolicy::LAZY, kMaxNumThreadsInBackgroundFileIOPool,
107 kSuggestedReclaimTime);
108 params_vector.emplace_back("RendererForeground", ThreadPriority::NORMAL,
109 StandbyThreadPolicy::LAZY,
110 base::RecommendedMaxNumberOfThreadsInPool(
111 kMaxNumThreadsInForegroundPoolLowerBound,
112 kMaxNumThreadsInForegroundPoolUpperBound,
113 kMaxNumThreadsInForegroundPoolCoresMultiplier,
114 kMaxNumThreadsInForegroundPoolOffset),
115 kSuggestedReclaimTime);
116 params_vector.emplace_back("RendererForegroundFileIO", ThreadPriority::NORMAL,
117 StandbyThreadPolicy::LAZY,
118 kMaxNumThreadsInForegroundFileIOPool,
119 kSuggestedReclaimTime);
120 DCHECK_EQ(WORKER_POOL_COUNT, params_vector.size());
121 return params_vector;
122}
123
124// Returns the worker pool index for |traits| defaulting to FOREGROUND or
125// FOREGROUND_FILE_IO on any other priorities based off of worker pools defined
126// in GetDefaultSchedulerWorkerPoolParams().
127size_t DefaultRendererWorkerPoolIndexForTraits(const base::TaskTraits& traits) {
128 const bool is_background =
129 traits.priority() == base::TaskPriority::BACKGROUND;
fdorayeefdba82017-01-13 13:50:27130 if (traits.may_block() || traits.with_base_sync_primitives())
fdorayd2233a72016-12-13 17:18:21131 return is_background ? BACKGROUND_FILE_IO : FOREGROUND_FILE_IO;
132
133 return is_background ? BACKGROUND : FOREGROUND;
134}
135
ishell75fddc12016-04-12 14:03:14136} // namespace
137
[email protected]eb398192012-10-22 20:16:19138namespace content {
139
[email protected]396c3a462010-03-03 05:03:22140RenderProcessImpl::RenderProcessImpl()
[email protected]1cf03f7f2014-04-24 03:09:49141 : enabled_bindings_(0) {
[email protected]396c3a462010-03-03 05:03:22142#if defined(OS_WIN)
georgesak80353b52017-01-10 21:18:51143 // Record whether the machine is domain joined in a crash key. This will be
144 // used to better identify whether crashes are from enterprise users.
145 // Note that this is done very early on so that crashes have the highest
146 // chance of getting tagged.
147 base::debug::SetCrashKeyValue("enrolled-to-domain",
148 base::win::IsEnrolledToDomain() ? "yes" : "no");
149
[email protected]396c3a462010-03-03 05:03:22150 // HACK: See http://b/issue?id=1024307 for rationale.
151 if (GetModuleHandle(L"LPK.DLL") == NULL) {
152 // Makes sure lpk.dll is loaded by gdi32 to make sure ExtTextOut() works
153 // when buffering into a EMF buffer for printing.
154 typedef BOOL (__stdcall *GdiInitializeLanguagePack)(int LoadedShapingDLLs);
155 GdiInitializeLanguagePack gdi_init_lpk =
156 reinterpret_cast<GdiInitializeLanguagePack>(GetProcAddress(
157 GetModuleHandle(L"GDI32.DLL"),
158 "GdiInitializeLanguagePack"));
159 DCHECK(gdi_init_lpk);
[email protected]00c39612010-03-06 02:53:28160 if (gdi_init_lpk) {
[email protected]396c3a462010-03-03 05:03:22161 gdi_init_lpk(0);
[email protected]00c39612010-03-06 02:53:28162 }
[email protected]396c3a462010-03-03 05:03:22163 }
[email protected]e68e62fa2009-02-20 02:00:04164#endif
165
[email protected]35b4f0c2014-06-26 16:55:27166 if (base::SysInfo::IsLowEndDevice()) {
[email protected]067f5192014-01-29 05:22:09167 std::string optimize_flag("--optimize-for-size");
168 v8::V8::SetFlagsFromString(optimize_flag.c_str(),
169 static_cast<int>(optimize_flag.size()));
170 }
[email protected]987422f2013-10-01 10:33:31171
ishell75fddc12016-04-12 14:03:14172 SetV8FlagIfFeature(kV8_ES2015_TailCalls_Feature, "--harmony-tailcalls");
ishell73f577b2016-04-14 11:59:39173 SetV8FlagIfFeature(kV8_ES2016_ExplicitTailCalls_Feature,
174 "--harmony-explicit-tailcalls");
ishell75fddc12016-04-12 14:03:14175 SetV8FlagIfFeature(kV8SerializeEagerFeature, "--serialize_eager");
176 SetV8FlagIfFeature(kV8SerializeAgeCodeFeature, "--serialize_age_code");
177 SetV8FlagIfHasSwitch(switches::kDisableJavaScriptHarmonyShipping,
178 "--noharmony-shipping");
179 SetV8FlagIfHasSwitch(switches::kJavaScriptHarmony, "--harmony");
bradnelsonc79f5a6f2016-10-10 18:31:14180 SetV8FlagIfFeature(features::kAsmJsToWebAssembly, "--validate-asm");
bradnelson2730e3512017-01-21 20:32:21181 SetV8FlagIfNotFeature(features::kWebAssembly,
182 "--wasm-disable-structured-cloning");
bradnelsonf5cb1b62016-10-12 18:21:08183 SetV8FlagIfFeature(features::kSharedArrayBuffer,
184 "--harmony-sharedarraybuffer");
ishell75fddc12016-04-12 14:03:14185
avi83883c82014-12-23 00:08:49186 const base::CommandLine& command_line =
187 *base::CommandLine::ForCurrentProcess();
ishell75fddc12016-04-12 14:03:14188
[email protected]396c3a462010-03-03 05:03:22189 if (command_line.HasSwitch(switches::kJavaScriptFlags)) {
[email protected]067f5192014-01-29 05:22:09190 std::string flags(
[email protected]95edc392010-07-30 22:00:38191 command_line.GetSwitchValueASCII(switches::kJavaScriptFlags));
[email protected]067f5192014-01-29 05:22:09192 v8::V8::SetFlagsFromString(flags.c_str(), static_cast<int>(flags.size()));
[email protected]396c3a462010-03-03 05:03:22193 }
[email protected]55dd9332013-09-04 17:17:50194
nick88299ba2015-06-16 23:57:41195 SiteIsolationStatsGatherer::SetEnabled(
196 GetContentClient()->renderer()->ShouldGatherSiteIsolationStats());
sammc7f6c6a02017-01-30 00:53:51197
198 if (command_line.HasSwitch(switches::kDomAutomationController))
199 enabled_bindings_ |= BINDINGS_POLICY_DOM_AUTOMATION;
200 if (command_line.HasSwitch(switches::kStatsCollectionController))
201 enabled_bindings_ |= BINDINGS_POLICY_STATS_COLLECTION;
[email protected]e68e62fa2009-02-20 02:00:04202}
203
[email protected]396c3a462010-03-03 05:03:22204RenderProcessImpl::~RenderProcessImpl() {
[email protected]396c3a462010-03-03 05:03:22205#ifndef NDEBUG
[email protected]180ef242013-11-07 06:50:46206 int count = blink::WebFrame::instanceCount();
[email protected]6bd867b2013-07-24 22:10:20207 if (count)
208 DLOG(ERROR) << "WebFrame LEAKED " << count << " TIMES";
[email protected]396c3a462010-03-03 05:03:22209#endif
[email protected]e68e62fa2009-02-20 02:00:04210
[email protected]396c3a462010-03-03 05:03:22211 GetShutDownEvent()->Signal();
[email protected]396c3a462010-03-03 05:03:22212}
[email protected]e68e62fa2009-02-20 02:00:04213
[email protected]744c2a22012-03-15 18:42:04214void RenderProcessImpl::AddBindings(int bindings) {
215 enabled_bindings_ |= bindings;
216}
217
218int RenderProcessImpl::GetEnabledBindings() const {
219 return enabled_bindings_;
220}
221
fdoray71ee0722017-01-17 18:50:34222void RenderProcessImpl::InitializeTaskScheduler() {
223 std::vector<base::SchedulerWorkerPoolParams> params_vector;
224 base::TaskScheduler::WorkerPoolIndexForTraitsCallback
225 index_to_traits_callback;
226 content::GetContentClient()->renderer()->GetTaskSchedulerInitializationParams(
227 &params_vector, &index_to_traits_callback);
228
229 if (params_vector.empty()) {
230 params_vector = GetDefaultSchedulerWorkerPoolParams();
231 index_to_traits_callback =
232 base::Bind(&DefaultRendererWorkerPoolIndexForTraits);
233 }
234 DCHECK(index_to_traits_callback);
235
236 base::TaskScheduler::CreateAndSetDefaultTaskScheduler(
237 params_vector, index_to_traits_callback);
238}
239
[email protected]eb398192012-10-22 20:16:19240} // namespace content