| [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" |
| fdoray | d2233a7 | 2016-12-13 17:18:21 | [diff] [blame] | 32 | #include "content/public/common/content_client.h" |
| bradnelson | c79f5a6f | 2016-10-10 18:31:14 | [diff] [blame] | 33 | #include "content/public/common/content_features.h" |
| [email protected] | c08950d2 | 2011-10-13 22:20:29 | [diff] [blame] | 34 | #include "content/public/common/content_switches.h" |
| [email protected] | d344114c | 2011-10-01 01:24:34 | [diff] [blame] | 35 | #include "content/public/renderer/content_renderer_client.h" |
| [email protected] | 6bd867b | 2013-07-24 22:10:20 | [diff] [blame] | 36 | #include "third_party/WebKit/public/web/WebFrame.h" |
| [email protected] | 067f519 | 2014-01-29 05:22:09 | [diff] [blame] | 37 | #include "v8/include/v8.h" |
| initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 38 | |
| georgesak | 80353b5 | 2017-01-10 21:18:51 | [diff] [blame^] | 39 | #if defined(OS_WIN) |
| 40 | #include "base/win/win_util.h" |
| 41 | #endif |
| 42 | |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 43 | namespace { |
| 44 | |
| fdoray | d2233a7 | 2016-12-13 17:18:21 | [diff] [blame] | 45 | enum WorkerPoolType : size_t { |
| 46 | BACKGROUND = 0, |
| 47 | BACKGROUND_FILE_IO, |
| 48 | FOREGROUND, |
| 49 | FOREGROUND_FILE_IO, |
| 50 | WORKER_POOL_COUNT // Always last. |
| 51 | }; |
| 52 | |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 53 | const base::Feature kV8_ES2015_TailCalls_Feature { |
| 54 | "V8_ES2015_TailCalls", base::FEATURE_DISABLED_BY_DEFAULT |
| 55 | }; |
| 56 | |
| ishell | 73f577b | 2016-04-14 11:59:39 | [diff] [blame] | 57 | const base::Feature kV8_ES2016_ExplicitTailCalls_Feature{ |
| 58 | "V8_ES2016_ExplicitTailCalls", base::FEATURE_DISABLED_BY_DEFAULT}; |
| 59 | |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 60 | const base::Feature kV8SerializeEagerFeature{"V8_Serialize_Eager", |
| 61 | base::FEATURE_DISABLED_BY_DEFAULT}; |
| 62 | |
| 63 | const base::Feature kV8SerializeAgeCodeFeature{ |
| 64 | "V8_Serialize_Age_Code", base::FEATURE_DISABLED_BY_DEFAULT}; |
| 65 | |
| 66 | void SetV8FlagIfFeature(const base::Feature& feature, const char* v8_flag) { |
| 67 | if (base::FeatureList::IsEnabled(feature)) { |
| 68 | v8::V8::SetFlagsFromString(v8_flag, strlen(v8_flag)); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void SetV8FlagIfHasSwitch(const char* switch_name, const char* v8_flag) { |
| 73 | if (base::CommandLine::ForCurrentProcess()->HasSwitch(switch_name)) { |
| 74 | v8::V8::SetFlagsFromString(v8_flag, strlen(v8_flag)); |
| 75 | } |
| 76 | } |
| 77 | |
| fdoray | d2233a7 | 2016-12-13 17:18:21 | [diff] [blame] | 78 | std::vector<base::SchedulerWorkerPoolParams> |
| 79 | GetDefaultSchedulerWorkerPoolParams() { |
| 80 | using StandbyThreadPolicy = |
| 81 | base::SchedulerWorkerPoolParams::StandbyThreadPolicy; |
| 82 | using ThreadPriority = base::ThreadPriority; |
| 83 | constexpr size_t kMaxNumThreadsInBackgroundPool = 1; |
| 84 | constexpr size_t kMaxNumThreadsInBackgroundFileIOPool = 1; |
| 85 | constexpr int kMaxNumThreadsInForegroundPoolLowerBound = 2; |
| 86 | constexpr int kMaxNumThreadsInForegroundPoolUpperBound = 4; |
| 87 | constexpr double kMaxNumThreadsInForegroundPoolCoresMultiplier = 1; |
| 88 | constexpr int kMaxNumThreadsInForegroundPoolOffset = 0; |
| 89 | constexpr size_t kMaxNumThreadsInForegroundFileIOPool = 1; |
| 90 | constexpr auto kSuggestedReclaimTime = base::TimeDelta::FromSeconds(30); |
| 91 | |
| 92 | std::vector<base::SchedulerWorkerPoolParams> params_vector; |
| 93 | params_vector.emplace_back("RendererBackground", ThreadPriority::BACKGROUND, |
| 94 | StandbyThreadPolicy::LAZY, |
| 95 | kMaxNumThreadsInBackgroundPool, |
| 96 | kSuggestedReclaimTime); |
| 97 | params_vector.emplace_back( |
| 98 | "RendererBackgroundFileIO", ThreadPriority::BACKGROUND, |
| 99 | StandbyThreadPolicy::LAZY, kMaxNumThreadsInBackgroundFileIOPool, |
| 100 | kSuggestedReclaimTime); |
| 101 | params_vector.emplace_back("RendererForeground", ThreadPriority::NORMAL, |
| 102 | StandbyThreadPolicy::LAZY, |
| 103 | base::RecommendedMaxNumberOfThreadsInPool( |
| 104 | kMaxNumThreadsInForegroundPoolLowerBound, |
| 105 | kMaxNumThreadsInForegroundPoolUpperBound, |
| 106 | kMaxNumThreadsInForegroundPoolCoresMultiplier, |
| 107 | kMaxNumThreadsInForegroundPoolOffset), |
| 108 | kSuggestedReclaimTime); |
| 109 | params_vector.emplace_back("RendererForegroundFileIO", ThreadPriority::NORMAL, |
| 110 | StandbyThreadPolicy::LAZY, |
| 111 | kMaxNumThreadsInForegroundFileIOPool, |
| 112 | kSuggestedReclaimTime); |
| 113 | DCHECK_EQ(WORKER_POOL_COUNT, params_vector.size()); |
| 114 | return params_vector; |
| 115 | } |
| 116 | |
| 117 | // Returns the worker pool index for |traits| defaulting to FOREGROUND or |
| 118 | // FOREGROUND_FILE_IO on any other priorities based off of worker pools defined |
| 119 | // in GetDefaultSchedulerWorkerPoolParams(). |
| 120 | size_t DefaultRendererWorkerPoolIndexForTraits(const base::TaskTraits& traits) { |
| 121 | const bool is_background = |
| 122 | traits.priority() == base::TaskPriority::BACKGROUND; |
| 123 | if (traits.with_file_io()) |
| 124 | return is_background ? BACKGROUND_FILE_IO : FOREGROUND_FILE_IO; |
| 125 | |
| 126 | return is_background ? BACKGROUND : FOREGROUND; |
| 127 | } |
| 128 | |
| 129 | void InitializeTaskScheduler() { |
| 130 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 131 | switches::kSingleProcess)) { |
| 132 | // There should already be a TaskScheduler when the renderer runs inside the |
| 133 | // browser process. |
| 134 | DCHECK(base::TaskScheduler::GetInstance()); |
| 135 | return; |
| 136 | } |
| 137 | DCHECK(!base::TaskScheduler::GetInstance()); |
| 138 | |
| 139 | std::vector<base::SchedulerWorkerPoolParams> params_vector; |
| 140 | base::TaskScheduler::WorkerPoolIndexForTraitsCallback |
| 141 | index_to_traits_callback; |
| 142 | content::GetContentClient()->renderer()->GetTaskSchedulerInitializationParams( |
| 143 | ¶ms_vector, &index_to_traits_callback); |
| 144 | |
| 145 | if (params_vector.empty()) { |
| 146 | params_vector = GetDefaultSchedulerWorkerPoolParams(); |
| 147 | index_to_traits_callback = |
| 148 | base::Bind(&DefaultRendererWorkerPoolIndexForTraits); |
| 149 | } |
| 150 | DCHECK(index_to_traits_callback); |
| 151 | |
| 152 | base::TaskScheduler::CreateAndSetDefaultTaskScheduler( |
| 153 | params_vector, index_to_traits_callback); |
| 154 | } |
| 155 | |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 156 | } // namespace |
| 157 | |
| [email protected] | eb39819 | 2012-10-22 20:16:19 | [diff] [blame] | 158 | namespace content { |
| 159 | |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 160 | RenderProcessImpl::RenderProcessImpl() |
| [email protected] | 1cf03f7f | 2014-04-24 03:09:49 | [diff] [blame] | 161 | : enabled_bindings_(0) { |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 162 | #if defined(OS_WIN) |
| georgesak | 80353b5 | 2017-01-10 21:18:51 | [diff] [blame^] | 163 | // Record whether the machine is domain joined in a crash key. This will be |
| 164 | // used to better identify whether crashes are from enterprise users. |
| 165 | // Note that this is done very early on so that crashes have the highest |
| 166 | // chance of getting tagged. |
| 167 | base::debug::SetCrashKeyValue("enrolled-to-domain", |
| 168 | base::win::IsEnrolledToDomain() ? "yes" : "no"); |
| 169 | |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 170 | // HACK: See http://b/issue?id=1024307 for rationale. |
| 171 | if (GetModuleHandle(L"LPK.DLL") == NULL) { |
| 172 | // Makes sure lpk.dll is loaded by gdi32 to make sure ExtTextOut() works |
| 173 | // when buffering into a EMF buffer for printing. |
| 174 | typedef BOOL (__stdcall *GdiInitializeLanguagePack)(int LoadedShapingDLLs); |
| 175 | GdiInitializeLanguagePack gdi_init_lpk = |
| 176 | reinterpret_cast<GdiInitializeLanguagePack>(GetProcAddress( |
| 177 | GetModuleHandle(L"GDI32.DLL"), |
| 178 | "GdiInitializeLanguagePack")); |
| 179 | DCHECK(gdi_init_lpk); |
| [email protected] | 00c3961 | 2010-03-06 02:53:28 | [diff] [blame] | 180 | if (gdi_init_lpk) { |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 181 | gdi_init_lpk(0); |
| [email protected] | 00c3961 | 2010-03-06 02:53:28 | [diff] [blame] | 182 | } |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 183 | } |
| [email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 184 | #endif |
| 185 | |
| [email protected] | 35b4f0c | 2014-06-26 16:55:27 | [diff] [blame] | 186 | if (base::SysInfo::IsLowEndDevice()) { |
| [email protected] | 067f519 | 2014-01-29 05:22:09 | [diff] [blame] | 187 | std::string optimize_flag("--optimize-for-size"); |
| 188 | v8::V8::SetFlagsFromString(optimize_flag.c_str(), |
| 189 | static_cast<int>(optimize_flag.size())); |
| 190 | } |
| [email protected] | 987422f | 2013-10-01 10:33:31 | [diff] [blame] | 191 | |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 192 | SetV8FlagIfFeature(kV8_ES2015_TailCalls_Feature, "--harmony-tailcalls"); |
| ishell | 73f577b | 2016-04-14 11:59:39 | [diff] [blame] | 193 | SetV8FlagIfFeature(kV8_ES2016_ExplicitTailCalls_Feature, |
| 194 | "--harmony-explicit-tailcalls"); |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 195 | SetV8FlagIfFeature(kV8SerializeEagerFeature, "--serialize_eager"); |
| 196 | SetV8FlagIfFeature(kV8SerializeAgeCodeFeature, "--serialize_age_code"); |
| 197 | SetV8FlagIfHasSwitch(switches::kDisableJavaScriptHarmonyShipping, |
| 198 | "--noharmony-shipping"); |
| 199 | SetV8FlagIfHasSwitch(switches::kJavaScriptHarmony, "--harmony"); |
| bradnelson | c79f5a6f | 2016-10-10 18:31:14 | [diff] [blame] | 200 | SetV8FlagIfFeature(features::kAsmJsToWebAssembly, "--validate-asm"); |
| 201 | SetV8FlagIfFeature(features::kWebAssembly, "--expose-wasm"); |
| bradnelson | f5cb1b6 | 2016-10-12 18:21:08 | [diff] [blame] | 202 | SetV8FlagIfFeature(features::kSharedArrayBuffer, |
| 203 | "--harmony-sharedarraybuffer"); |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 204 | |
| avi | 83883c8 | 2014-12-23 00:08:49 | [diff] [blame] | 205 | const base::CommandLine& command_line = |
| 206 | *base::CommandLine::ForCurrentProcess(); |
| ishell | 75fddc1 | 2016-04-12 14:03:14 | [diff] [blame] | 207 | |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 208 | if (command_line.HasSwitch(switches::kJavaScriptFlags)) { |
| [email protected] | 067f519 | 2014-01-29 05:22:09 | [diff] [blame] | 209 | std::string flags( |
| [email protected] | 95edc39 | 2010-07-30 22:00:38 | [diff] [blame] | 210 | command_line.GetSwitchValueASCII(switches::kJavaScriptFlags)); |
| [email protected] | 067f519 | 2014-01-29 05:22:09 | [diff] [blame] | 211 | v8::V8::SetFlagsFromString(flags.c_str(), static_cast<int>(flags.size())); |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 212 | } |
| [email protected] | 55dd933 | 2013-09-04 17:17:50 | [diff] [blame] | 213 | |
| nick | 88299ba | 2015-06-16 23:57:41 | [diff] [blame] | 214 | SiteIsolationStatsGatherer::SetEnabled( |
| 215 | GetContentClient()->renderer()->ShouldGatherSiteIsolationStats()); |
| fdoray | d2233a7 | 2016-12-13 17:18:21 | [diff] [blame] | 216 | |
| 217 | InitializeTaskScheduler(); |
| [email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 218 | } |
| 219 | |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 220 | RenderProcessImpl::~RenderProcessImpl() { |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 221 | #ifndef NDEBUG |
| [email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 222 | int count = blink::WebFrame::instanceCount(); |
| [email protected] | 6bd867b | 2013-07-24 22:10:20 | [diff] [blame] | 223 | if (count) |
| 224 | DLOG(ERROR) << "WebFrame LEAKED " << count << " TIMES"; |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 225 | #endif |
| [email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 226 | |
| fdoray | d2233a7 | 2016-12-13 17:18:21 | [diff] [blame] | 227 | if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 228 | switches::kSingleProcess)) { |
| 229 | DCHECK(base::TaskScheduler::GetInstance()); |
| 230 | base::TaskScheduler::GetInstance()->Shutdown(); |
| 231 | } |
| 232 | |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 233 | GetShutDownEvent()->Signal(); |
| [email protected] | 396c3a46 | 2010-03-03 05:03:22 | [diff] [blame] | 234 | } |
| [email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 235 | |
| [email protected] | 744c2a2 | 2012-03-15 18:42:04 | [diff] [blame] | 236 | void RenderProcessImpl::AddBindings(int bindings) { |
| 237 | enabled_bindings_ |= bindings; |
| 238 | } |
| 239 | |
| 240 | int RenderProcessImpl::GetEnabledBindings() const { |
| 241 | return enabled_bindings_; |
| 242 | } |
| 243 | |
| [email protected] | eb39819 | 2012-10-22 20:16:19 | [diff] [blame] | 244 | } // namespace content |