| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/renderer_host/render_process_host.h" | 5 #include "content/browser/renderer_host/render_process_host.h" |
| 6 | 6 |
| 7 #include "base/rand_util.h" | 7 #include "base/rand_util.h" |
| 8 #include "base/sys_info.h" | 8 #include "base/sys_info.h" |
| 9 #include "chrome/browser/profiles/profile.h" |
| 9 #include "content/browser/browser_thread.h" | 10 #include "content/browser/browser_thread.h" |
| 10 #include "content/browser/child_process_security_policy.h" | 11 #include "content/browser/child_process_security_policy.h" |
| 11 #include "content/common/child_process_info.h" | 12 #include "content/common/child_process_info.h" |
| 12 #include "content/common/content_constants.h" | 13 #include "content/common/content_constants.h" |
| 13 #include "content/common/notification_service.h" | 14 #include "content/common/notification_service.h" |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| 17 size_t max_renderer_count_override = 0; | 18 size_t max_renderer_count_override = 0; |
| 18 | 19 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 size_t memory_tier = base::SysInfo::AmountOfPhysicalMemoryMB() / 256; | 53 size_t memory_tier = base::SysInfo::AmountOfPhysicalMemoryMB() / 256; |
| 53 if (memory_tier >= arraysize(kMaxRenderersByRamTier)) | 54 if (memory_tier >= arraysize(kMaxRenderersByRamTier)) |
| 54 max_count = content::kMaxRendererProcessCount; | 55 max_count = content::kMaxRendererProcessCount; |
| 55 else | 56 else |
| 56 max_count = kMaxRenderersByRamTier[memory_tier]; | 57 max_count = kMaxRenderersByRamTier[memory_tier]; |
| 57 } | 58 } |
| 58 return max_count; | 59 return max_count; |
| 59 } | 60 } |
| 60 | 61 |
| 61 // Returns true if the given host is suitable for launching a new view | 62 // Returns true if the given host is suitable for launching a new view |
| 62 // associated with the given profile. | 63 // associated with the given context. |
| 63 static bool IsSuitableHost(RenderProcessHost* host, Profile* profile, | 64 static bool IsSuitableHost(RenderProcessHost* host, |
| 65 content::BrowserContext* context, |
| 64 RenderProcessHost::Type type) { | 66 RenderProcessHost::Type type) { |
| 65 if (host->profile() != profile) | 67 if (host->context() != context) |
| 66 return false; | 68 return false; |
| 67 | 69 |
| 68 RenderProcessHost::Type host_type = RenderProcessHost::TYPE_NORMAL; | 70 RenderProcessHost::Type host_type = RenderProcessHost::TYPE_NORMAL; |
| 69 if (ChildProcessSecurityPolicy::GetInstance()->HasWebUIBindings(host->id())) | 71 if (ChildProcessSecurityPolicy::GetInstance()->HasWebUIBindings(host->id())) |
| 70 host_type = RenderProcessHost::TYPE_WEBUI; | 72 host_type = RenderProcessHost::TYPE_WEBUI; |
| 71 if (ChildProcessSecurityPolicy::GetInstance()-> | 73 if (ChildProcessSecurityPolicy::GetInstance()-> |
| 72 HasExtensionBindings(host->id())) | 74 HasExtensionBindings(host->id())) |
| 73 host_type = RenderProcessHost::TYPE_EXTENSION; | 75 host_type = RenderProcessHost::TYPE_EXTENSION; |
| 74 | 76 |
| 75 return host_type == type; | 77 return host_type == type; |
| 76 } | 78 } |
| 77 | 79 |
| 78 // the global list of all renderer processes | 80 // the global list of all renderer processes |
| 79 IDMap<RenderProcessHost> all_hosts; | 81 IDMap<RenderProcessHost> all_hosts; |
| 80 | 82 |
| 81 } // namespace | 83 } // namespace |
| 82 | 84 |
| 83 extern bool g_log_bug53991; | 85 extern bool g_log_bug53991; |
| 84 | 86 |
| 85 // static | 87 // static |
| 86 bool RenderProcessHost::run_renderer_in_process_ = false; | 88 bool RenderProcessHost::run_renderer_in_process_ = false; |
| 87 | 89 |
| 88 // static | 90 // static |
| 89 void RenderProcessHost::SetMaxRendererProcessCount(size_t count) { | 91 void RenderProcessHost::SetMaxRendererProcessCount(size_t count) { |
| 90 max_renderer_count_override = count; | 92 max_renderer_count_override = count; |
| 91 } | 93 } |
| 92 | 94 |
| 93 RenderProcessHost::RenderProcessHost(Profile* profile) | 95 RenderProcessHost::RenderProcessHost(content::BrowserContext* context) |
| 94 : max_page_id_(-1), | 96 : max_page_id_(-1), |
| 95 fast_shutdown_started_(false), | 97 fast_shutdown_started_(false), |
| 96 deleting_soon_(false), | 98 deleting_soon_(false), |
| 97 is_extension_process_(false), | 99 is_extension_process_(false), |
| 98 pending_views_(0), | 100 pending_views_(0), |
| 99 id_(ChildProcessInfo::GenerateChildProcessUniqueId()), | 101 id_(ChildProcessInfo::GenerateChildProcessUniqueId()), |
| 100 profile_(profile), | 102 context_(context), |
| 103 profile_(static_cast<Profile*>(context)), |
| 101 sudden_termination_allowed_(true), | 104 sudden_termination_allowed_(true), |
| 102 ignore_input_events_(false) { | 105 ignore_input_events_(false) { |
| 103 all_hosts.AddWithID(this, id()); | 106 all_hosts.AddWithID(this, id()); |
| 104 all_hosts.set_check_on_null_data(true); | 107 all_hosts.set_check_on_null_data(true); |
| 105 // Initialize |child_process_activity_time_| to a reasonable value. | 108 // Initialize |child_process_activity_time_| to a reasonable value. |
| 106 mark_child_process_activity_time(); | 109 mark_child_process_activity_time(); |
| 107 } | 110 } |
| 108 | 111 |
| 109 RenderProcessHost::~RenderProcessHost() { | 112 RenderProcessHost::~RenderProcessHost() { |
| 110 // In unit tests, Release() might not have been called. | 113 // In unit tests, Release() might not have been called. |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 177 return all_hosts.Lookup(render_process_id); | 180 return all_hosts.Lookup(render_process_id); |
| 178 } | 181 } |
| 179 | 182 |
| 180 // static | 183 // static |
| 181 bool RenderProcessHost::ShouldTryToUseExistingProcessHost() { | 184 bool RenderProcessHost::ShouldTryToUseExistingProcessHost() { |
| 182 size_t renderer_process_count = all_hosts.size(); | 185 size_t renderer_process_count = all_hosts.size(); |
| 183 | 186 |
| 184 // NOTE: Sometimes it's necessary to create more render processes than | 187 // NOTE: Sometimes it's necessary to create more render processes than |
| 185 // GetMaxRendererProcessCount(), for instance when we want to create | 188 // GetMaxRendererProcessCount(), for instance when we want to create |
| 186 // a renderer process for a profile that has no existing renderers. | 189 // a renderer process for a context that has no existing renderers. |
| 187 // This is OK in moderation, since the GetMaxRendererProcessCount() | 190 // This is OK in moderation, since the GetMaxRendererProcessCount() |
| 188 // is conservative. | 191 // is conservative. |
| 189 | 192 |
| 190 return run_renderer_in_process() || | 193 return run_renderer_in_process() || |
| 191 (renderer_process_count >= GetMaxRendererProcessCount()); | 194 (renderer_process_count >= GetMaxRendererProcessCount()); |
| 192 } | 195 } |
| 193 | 196 |
| 194 // static | 197 // static |
| 195 RenderProcessHost* RenderProcessHost::GetExistingProcessHost(Profile* profile, | 198 RenderProcessHost* RenderProcessHost::GetExistingProcessHost( |
| 196 Type type) { | 199 content::BrowserContext* context, Type type) { |
| 197 // First figure out which existing renderers we can use. | 200 // First figure out which existing renderers we can use. |
| 198 std::vector<RenderProcessHost*> suitable_renderers; | 201 std::vector<RenderProcessHost*> suitable_renderers; |
| 199 suitable_renderers.reserve(all_hosts.size()); | 202 suitable_renderers.reserve(all_hosts.size()); |
| 200 | 203 |
| 201 iterator iter(AllHostsIterator()); | 204 iterator iter(AllHostsIterator()); |
| 202 while (!iter.IsAtEnd()) { | 205 while (!iter.IsAtEnd()) { |
| 203 if (run_renderer_in_process() || | 206 if (run_renderer_in_process() || |
| 204 IsSuitableHost(iter.GetCurrentValue(), profile, type)) | 207 IsSuitableHost(iter.GetCurrentValue(), context, type)) |
| 205 suitable_renderers.push_back(iter.GetCurrentValue()); | 208 suitable_renderers.push_back(iter.GetCurrentValue()); |
| 206 | 209 |
| 207 iter.Advance(); | 210 iter.Advance(); |
| 208 } | 211 } |
| 209 | 212 |
| 210 // Now pick a random suitable renderer, if we have any. | 213 // Now pick a random suitable renderer, if we have any. |
| 211 if (!suitable_renderers.empty()) { | 214 if (!suitable_renderers.empty()) { |
| 212 int suitable_count = static_cast<int>(suitable_renderers.size()); | 215 int suitable_count = static_cast<int>(suitable_renderers.size()); |
| 213 int random_index = base::RandInt(0, suitable_count - 1); | 216 int random_index = base::RandInt(0, suitable_count - 1); |
| 214 return suitable_renderers[random_index]; | 217 return suitable_renderers[random_index]; |
| 215 } | 218 } |
| 216 | 219 |
| 217 return NULL; | 220 return NULL; |
| 218 } | 221 } |
| OLD | NEW |