| [email protected] | fd911dd | 2012-01-27 01:57:10 | [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 | |
| [email protected] | d353541f | 2012-05-03 22:45:41 | [diff] [blame] | 5 | #include "content/renderer/render_process_impl.h" |
| 6 | |
| [email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame] | 7 | #include "build/build_config.h" |
| 8 | |
| [email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame] | 9 | #if defined(OS_WIN) |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | #include <windows.h> |
| 11 | #include <objidl.h> |
| 12 | #include <mlang.h> |
| [email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame] | 13 | #endif |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | |
| fdoray | d2233a7 | 2016-12-13 17:18:21 | [diff] [blame] | 15 | #include <stddef.h> |
| 16 | |
| 17 | #include <vector> |
| 18 | |
| 19 | #include "base/bind.h" |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 20 | #include "base/command_line.h" |
| [email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame] | 21 | #include "base/compiler_specific.h" |
| georgesak | 80353b5 | 2017-01-10 21:18:51 | [diff] [blame] | 22 | #include "base/debug/crash_logging.h" |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 23 | #include "base/feature_list.h" |
| [email protected] | 35b4f0c | 2014-06-26 16:55:27 | [diff] [blame] | 24 | #include "base/sys_info.h" |
| fdoray | d2233a7 | 2016-12-13 17:18:21 | [diff] [blame] | 25 | #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" |
| nick | e7cd12a | 2015-06-17 06:48:38 | [diff] [blame] | 31 | #include "content/child/site_isolation_stats_gatherer.h" |
| sammc | 7f6c6a0 | 2017-01-30 00:53:51 | [diff] [blame^] | 32 | #include "content/public/common/bindings_policy.h" |
| fdoray | d2233a7 | 2016-12-13 17:18:21 | [diff] [blame] | 33 | #include "content/public/common/content_client.h" |
| bradnelson | c79f5a6f | 2016-10-10 18:31:14 | [diff] [blame] | 34 | #include "content/public/common/content_features.h" |
| [email protected] | c08950d2 | 2011-10-13 22:20:29 | [diff] [blame] | 35 | #include "content/public/common/content_switches.h" |
| [email protected] | d344114c | 2011-10-01 01:24:34 | [diff] [blame] | 36 | #include "content/public/renderer/content_renderer_client.h" |
| [email protected] | 6bd867b | 2013-07-24 22:10:20 | [diff] [blame] | 37 | #include "third_party/WebKit/public/web/WebFrame.h" |
| [email protected] | 067f519 | 2014-01-29 05:22:09 | [diff] [blame] | 38 | #include "v8/include/v8.h" |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 39 | |
| georgesak | 80353b5 | 2017-01-10 21:18:51 | [diff] [blame] | 40 | #if defined(OS_WIN) |
| 41 | #include "base/win/win_util.h" |
| 42 | #endif |
| 43 | |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 44 | namespace { |
| 45 | |
| fdoray | d2233a7 | 2016-12-13 17:18:21 | [diff] [blame] | 46 | enum WorkerPoolType : size_t { |
| 47 | BACKGROUND = 0, |
| 48 | BACKGROUND_FILE_IO, |
| 49 | FOREGROUND, |
| 50 | FOREGROUND_FILE_IO, |
| 51 | WORKER_POOL_COUNT // Always last. |
| 52 | }; |
| 53 | |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 54 | const base::Feature kV8_ES2015_TailCalls_Feature { |
| 55 | "V8_ES2015_TailCalls", base::FEATURE_DISABLED_BY_DEFAULT |
| 56 | }; |
| 57 | |
| ishell | 73f577b | 2016-04-14 11:59:39 | [diff] [blame] | 58 | const base::Feature kV8_ES2016_ExplicitTailCalls_Feature{ |
| 59 | "V8_ES2016_ExplicitTailCalls", base::FEATURE_DISABLED_BY_DEFAULT}; |
| 60 | |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 61 | const base::Feature kV8SerializeEagerFeature{"V8_Serialize_Eager", |
| 62 | base::FEATURE_DISABLED_BY_DEFAULT}; |
| 63 | |
| 64 | const base::Feature kV8SerializeAgeCodeFeature{ |
| 65 | "V8_Serialize_Age_Code", base::FEATURE_DISABLED_BY_DEFAULT}; |
| 66 | |
| 67 | void 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 | |
| bradnelson | 2730e351 | 2017-01-21 20:32:21 | [diff] [blame] | 73 | void 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 | |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 79 | void 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 | |
| fdoray | d2233a7 | 2016-12-13 17:18:21 | [diff] [blame] | 85 | std::vector<base::SchedulerWorkerPoolParams> |
| 86 | GetDefaultSchedulerWorkerPoolParams() { |
| 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(). |
| 127 | size_t DefaultRendererWorkerPoolIndexForTraits(const base::TaskTraits& traits) { |
| 128 | const bool is_background = |
| 129 | traits.priority() == base::TaskPriority::BACKGROUND; |
| fdoray | eefdba8 | 2017-01-13 13:50:27 | [diff] [blame] | 130 | if (traits.may_block() || traits.with_base_sync_primitives()) |
| fdoray | d2233a7 | 2016-12-13 17:18:21 | [diff] [blame] | 131 | return is_background ? BACKGROUND_FILE_IO : FOREGROUND_FILE_IO; |
| 132 | |
| 133 | return is_background ? BACKGROUND : FOREGROUND; |
| 134 | } |
| 135 | |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 136 | } // namespace |
| 137 | |
| [email protected] | eb39819 | 2012-10-22 20:16:19 | [diff] [blame] | 138 | namespace content { |
| 139 | |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 140 | RenderProcessImpl::RenderProcessImpl() |
| [email protected] | 1cf03f7f | 2014-04-24 03:09:49 | [diff] [blame] | 141 | : enabled_bindings_(0) { |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 142 | #if defined(OS_WIN) |
| georgesak | 80353b5 | 2017-01-10 21:18:51 | [diff] [blame] | 143 | // 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] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 150 | // 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] | 00c3961 | 2010-03-06 02:53:28 | [diff] [blame] | 160 | if (gdi_init_lpk) { |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 161 | gdi_init_lpk(0); |
| [email protected] | 00c3961 | 2010-03-06 02:53:28 | [diff] [blame] | 162 | } |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 163 | } |
| [email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 164 | #endif |
| 165 | |
| [email protected] | 35b4f0c | 2014-06-26 16:55:27 | [diff] [blame] | 166 | if (base::SysInfo::IsLowEndDevice()) { |
| [email protected] | 067f519 | 2014-01-29 05:22:09 | [diff] [blame] | 167 | 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] | 987422f | 2013-10-01 10:33:31 | [diff] [blame] | 171 | |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 172 | SetV8FlagIfFeature(kV8_ES2015_TailCalls_Feature, "--harmony-tailcalls"); |
| ishell | 73f577b | 2016-04-14 11:59:39 | [diff] [blame] | 173 | SetV8FlagIfFeature(kV8_ES2016_ExplicitTailCalls_Feature, |
| 174 | "--harmony-explicit-tailcalls"); |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 175 | SetV8FlagIfFeature(kV8SerializeEagerFeature, "--serialize_eager"); |
| 176 | SetV8FlagIfFeature(kV8SerializeAgeCodeFeature, "--serialize_age_code"); |
| 177 | SetV8FlagIfHasSwitch(switches::kDisableJavaScriptHarmonyShipping, |
| 178 | "--noharmony-shipping"); |
| 179 | SetV8FlagIfHasSwitch(switches::kJavaScriptHarmony, "--harmony"); |
| bradnelson | c79f5a6f | 2016-10-10 18:31:14 | [diff] [blame] | 180 | SetV8FlagIfFeature(features::kAsmJsToWebAssembly, "--validate-asm"); |
| bradnelson | 2730e351 | 2017-01-21 20:32:21 | [diff] [blame] | 181 | SetV8FlagIfNotFeature(features::kWebAssembly, |
| 182 | "--wasm-disable-structured-cloning"); |
| bradnelson | f5cb1b6 | 2016-10-12 18:21:08 | [diff] [blame] | 183 | SetV8FlagIfFeature(features::kSharedArrayBuffer, |
| 184 | "--harmony-sharedarraybuffer"); |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 185 | |
| avi | 83883c8 | 2014-12-23 00:08:49 | [diff] [blame] | 186 | const base::CommandLine& command_line = |
| 187 | *base::CommandLine::ForCurrentProcess(); |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 188 | |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 189 | if (command_line.HasSwitch(switches::kJavaScriptFlags)) { |
| [email protected] | 067f519 | 2014-01-29 05:22:09 | [diff] [blame] | 190 | std::string flags( |
| [email protected] | 95edc39 | 2010-07-30 22:00:38 | [diff] [blame] | 191 | command_line.GetSwitchValueASCII(switches::kJavaScriptFlags)); |
| [email protected] | 067f519 | 2014-01-29 05:22:09 | [diff] [blame] | 192 | v8::V8::SetFlagsFromString(flags.c_str(), static_cast<int>(flags.size())); |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 193 | } |
| [email protected] | 55dd933 | 2013-09-04 17:17:50 | [diff] [blame] | 194 | |
| nick | 88299ba | 2015-06-16 23:57:41 | [diff] [blame] | 195 | SiteIsolationStatsGatherer::SetEnabled( |
| 196 | GetContentClient()->renderer()->ShouldGatherSiteIsolationStats()); |
| sammc | 7f6c6a0 | 2017-01-30 00:53:51 | [diff] [blame^] | 197 | |
| 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] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 202 | } |
| 203 | |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 204 | RenderProcessImpl::~RenderProcessImpl() { |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 205 | #ifndef NDEBUG |
| [email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 206 | int count = blink::WebFrame::instanceCount(); |
| [email protected] | 6bd867b | 2013-07-24 22:10:20 | [diff] [blame] | 207 | if (count) |
| 208 | DLOG(ERROR) << "WebFrame LEAKED " << count << " TIMES"; |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 209 | #endif |
| [email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 210 | |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 211 | GetShutDownEvent()->Signal(); |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 212 | } |
| [email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 213 | |
| [email protected] | 744c2a2 | 2012-03-15 18:42:04 | [diff] [blame] | 214 | void RenderProcessImpl::AddBindings(int bindings) { |
| 215 | enabled_bindings_ |= bindings; |
| 216 | } |
| 217 | |
| 218 | int RenderProcessImpl::GetEnabledBindings() const { |
| 219 | return enabled_bindings_; |
| 220 | } |
| 221 | |
| fdoray | 71ee072 | 2017-01-17 18:50:34 | [diff] [blame] | 222 | void RenderProcessImpl::InitializeTaskScheduler() { |
| 223 | std::vector<base::SchedulerWorkerPoolParams> params_vector; |
| 224 | base::TaskScheduler::WorkerPoolIndexForTraitsCallback |
| 225 | index_to_traits_callback; |
| 226 | content::GetContentClient()->renderer()->GetTaskSchedulerInitializationParams( |
| 227 | ¶ms_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] | eb39819 | 2012-10-22 20:16:19 | [diff] [blame] | 240 | } // namespace content |