| 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/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
| 9 #include "base/sys_info.h" | 9 #include "base/sys_info.h" |
| 10 #include "content/browser/browser_thread.h" | 10 #include "content/browser/browser_thread.h" |
| 11 #include "content/browser/child_process_security_policy.h" | 11 #include "content/browser/child_process_security_policy.h" |
| 12 #include "content/browser/content_browser_client.h" |
| 13 #include "content/browser/webui/web_ui_factory.h" |
| 12 #include "content/common/child_process_info.h" | 14 #include "content/common/child_process_info.h" |
| 15 #include "content/common/content_client.h" |
| 13 #include "content/common/content_constants.h" | 16 #include "content/common/content_constants.h" |
| 14 #include "content/common/content_switches.h" | 17 #include "content/common/content_switches.h" |
| 15 #include "content/common/notification_service.h" | 18 #include "content/common/notification_service.h" |
| 16 | 19 |
| 17 namespace { | 20 namespace { |
| 18 | 21 |
| 19 size_t max_renderer_count_override = 0; | 22 size_t max_renderer_count_override = 0; |
| 20 | 23 |
| 21 size_t GetMaxRendererProcessCount() { | 24 size_t GetMaxRendererProcessCount() { |
| 22 if (max_renderer_count_override) | 25 if (max_renderer_count_override) |
| (...skipping 30 matching lines...) Expand all Loading... |
| 53 if (!max_count) { | 56 if (!max_count) { |
| 54 size_t memory_tier = base::SysInfo::AmountOfPhysicalMemoryMB() / 256; | 57 size_t memory_tier = base::SysInfo::AmountOfPhysicalMemoryMB() / 256; |
| 55 if (memory_tier >= arraysize(kMaxRenderersByRamTier)) | 58 if (memory_tier >= arraysize(kMaxRenderersByRamTier)) |
| 56 max_count = content::kMaxRendererProcessCount; | 59 max_count = content::kMaxRendererProcessCount; |
| 57 else | 60 else |
| 58 max_count = kMaxRenderersByRamTier[memory_tier]; | 61 max_count = kMaxRenderersByRamTier[memory_tier]; |
| 59 } | 62 } |
| 60 return max_count; | 63 return max_count; |
| 61 } | 64 } |
| 62 | 65 |
| 63 // Returns true if the given host is suitable for launching a new view | |
| 64 // associated with the given browser context. | |
| 65 static bool IsSuitableHost(RenderProcessHost* host, | |
| 66 content::BrowserContext* browser_context, | |
| 67 RenderProcessHost::Type type) { | |
| 68 if (host->browser_context() != browser_context) | |
| 69 return false; | |
| 70 | |
| 71 RenderProcessHost::Type host_type = RenderProcessHost::TYPE_NORMAL; | |
| 72 if (ChildProcessSecurityPolicy::GetInstance()->HasWebUIBindings(host->id())) | |
| 73 host_type = RenderProcessHost::TYPE_WEBUI; | |
| 74 if (ChildProcessSecurityPolicy::GetInstance()-> | |
| 75 HasExtensionBindings(host->id()) || | |
| 76 host->is_extension_process()) | |
| 77 host_type = RenderProcessHost::TYPE_EXTENSION; | |
| 78 | |
| 79 return host_type == type; | |
| 80 } | |
| 81 | |
| 82 // the global list of all renderer processes | 66 // the global list of all renderer processes |
| 83 IDMap<RenderProcessHost> all_hosts; | 67 IDMap<RenderProcessHost> all_hosts; |
| 84 | 68 |
| 85 } // namespace | 69 } // namespace |
| 86 | 70 |
| 87 // static | 71 // static |
| 88 bool RenderProcessHost::run_renderer_in_process_ = false; | 72 bool RenderProcessHost::run_renderer_in_process_ = false; |
| 89 | 73 |
| 90 // static | 74 // static |
| 91 void RenderProcessHost::SetMaxRendererProcessCount(size_t count) { | 75 void RenderProcessHost::SetMaxRendererProcessCount(size_t count) { |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 // a renderer process for a browser context that has no existing | 196 // a renderer process for a browser context that has no existing |
| 213 // renderers. This is OK in moderation, since the | 197 // renderers. This is OK in moderation, since the |
| 214 // GetMaxRendererProcessCount() is conservative. | 198 // GetMaxRendererProcessCount() is conservative. |
| 215 | 199 |
| 216 return run_renderer_in_process() || | 200 return run_renderer_in_process() || |
| 217 (renderer_process_count >= GetMaxRendererProcessCount()); | 201 (renderer_process_count >= GetMaxRendererProcessCount()); |
| 218 } | 202 } |
| 219 | 203 |
| 220 // static | 204 // static |
| 221 RenderProcessHost* RenderProcessHost::GetExistingProcessHost( | 205 RenderProcessHost* RenderProcessHost::GetExistingProcessHost( |
| 222 content::BrowserContext* browser_context, Type type) { | 206 content::BrowserContext* browser_context, |
| 207 const GURL& site_url) { |
| 223 // First figure out which existing renderers we can use. | 208 // First figure out which existing renderers we can use. |
| 224 std::vector<RenderProcessHost*> suitable_renderers; | 209 std::vector<RenderProcessHost*> suitable_renderers; |
| 225 suitable_renderers.reserve(all_hosts.size()); | 210 suitable_renderers.reserve(all_hosts.size()); |
| 226 | 211 |
| 227 iterator iter(AllHostsIterator()); | 212 iterator iter(AllHostsIterator()); |
| 228 while (!iter.IsAtEnd()) { | 213 while (!iter.IsAtEnd()) { |
| 229 if (run_renderer_in_process() || | 214 if (run_renderer_in_process() || |
| 230 IsSuitableHost(iter.GetCurrentValue(), browser_context, type)) | 215 (iter.GetCurrentValue()->browser_context() == browser_context && |
| 216 iter.GetCurrentValue()->IsSuitableHost(site_url))) |
| 231 suitable_renderers.push_back(iter.GetCurrentValue()); | 217 suitable_renderers.push_back(iter.GetCurrentValue()); |
| 232 | 218 |
| 233 iter.Advance(); | 219 iter.Advance(); |
| 234 } | 220 } |
| 235 | 221 |
| 236 // Now pick a random suitable renderer, if we have any. | 222 // Now pick a random suitable renderer, if we have any. |
| 237 if (!suitable_renderers.empty()) { | 223 if (!suitable_renderers.empty()) { |
| 238 int suitable_count = static_cast<int>(suitable_renderers.size()); | 224 int suitable_count = static_cast<int>(suitable_renderers.size()); |
| 239 int random_index = base::RandInt(0, suitable_count - 1); | 225 int random_index = base::RandInt(0, suitable_count - 1); |
| 240 return suitable_renderers[random_index]; | 226 return suitable_renderers[random_index]; |
| 241 } | 227 } |
| 242 | 228 |
| 243 return NULL; | 229 return NULL; |
| 244 } | 230 } |
| 231 |
| 232 bool RenderProcessHost::IsSuitableHost(const GURL& site_url) { |
| 233 if (ChildProcessSecurityPolicy::GetInstance()->HasWebUIBindings(id_) != |
| 234 content::WebUIFactory::Get()->HasWebUIScheme(site_url)) |
| 235 return false; |
| 236 return true; |
| 237 } |
| OLD | NEW |