| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/browser/worker_host/worker_service.h" | 5 #include "chrome/browser/worker_host/worker_service.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/singleton.h" | 8 #include "base/singleton.h" |
| 9 #include "base/sys_info.h" | 9 #include "base/sys_info.h" |
| 10 #include "base/thread.h" | 10 #include "base/thread.h" |
| 11 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/plugin_service.h" | 12 #include "chrome/browser/plugin_service.h" |
| 13 #include "chrome/browser/worker_host/worker_process_host.h" | 13 #include "chrome/browser/worker_host/worker_process_host.h" |
| 14 #include "chrome/browser/renderer_host/render_process_host.h" | 14 #include "chrome/browser/renderer_host/render_process_host.h" |
| 15 #include "chrome/browser/renderer_host/resource_message_filter.h" | 15 #include "chrome/browser/renderer_host/resource_message_filter.h" |
| 16 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
| 17 #include "chrome/common/notification_service.h" |
| 17 #include "net/base/registry_controlled_domain.h" | 18 #include "net/base/registry_controlled_domain.h" |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 static const int kMaxWorkerProcesses = 10; | 21 static const int kMaxWorkerProcesses = 10; |
| 21 } | 22 } |
| 22 | 23 |
| 23 WorkerService* WorkerService::GetInstance() { | 24 WorkerService* WorkerService::GetInstance() { |
| 24 return Singleton<WorkerService>::get(); | 25 return Singleton<WorkerService>::get(); |
| 25 } | 26 } |
| 26 | 27 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 50 return false; | 51 return false; |
| 51 } | 52 } |
| 52 } | 53 } |
| 53 | 54 |
| 54 // Generate a unique route id for the browser-worker communication that's | 55 // Generate a unique route id for the browser-worker communication that's |
| 55 // unique among all worker processes. That way when the worker process sends | 56 // unique among all worker processes. That way when the worker process sends |
| 56 // a wrapped IPC message through us, we know which WorkerProcessHost to give | 57 // a wrapped IPC message through us, we know which WorkerProcessHost to give |
| 57 // it to. | 58 // it to. |
| 58 worker->CreateWorker(url, render_view_route_id, ++next_worker_route_id_, | 59 worker->CreateWorker(url, render_view_route_id, ++next_worker_route_id_, |
| 59 renderer_route_id, filter); | 60 renderer_route_id, filter); |
| 61 |
| 62 // Receive a notification if the message filter is deleted. |
| 63 NotificationService::current()->AddObserver( |
| 64 this, |
| 65 NotificationType::RESOURCE_MESSAGE_FILTER_SHUTDOWN, |
| 66 Source<ResourceMessageFilter>(filter)); |
| 67 |
| 60 return true; | 68 return true; |
| 61 } | 69 } |
| 62 | 70 |
| 63 void WorkerService::ForwardMessage(const IPC::Message& message) { | 71 void WorkerService::ForwardMessage(const IPC::Message& message) { |
| 64 for (ChildProcessHost::Iterator iter(ChildProcessInfo::WORKER_PROCESS); | 72 for (ChildProcessHost::Iterator iter(ChildProcessInfo::WORKER_PROCESS); |
| 65 !iter.Done(); ++iter) { | 73 !iter.Done(); ++iter) { |
| 66 WorkerProcessHost* worker = static_cast<WorkerProcessHost*>(*iter); | 74 WorkerProcessHost* worker = static_cast<WorkerProcessHost*>(*iter); |
| 67 if (worker->FilterMessage(message)) | 75 if (worker->FilterMessage(message)) |
| 68 return; | 76 return; |
| 69 } | 77 } |
| 70 | 78 |
| 71 // TODO(jabdelmalek): tell sender that callee is gone | 79 // TODO(jabdelmalek): tell sender that callee is gone |
| 72 } | 80 } |
| 73 | 81 |
| 74 void WorkerService::RendererShutdown(ResourceMessageFilter* filter) { | |
| 75 for (ChildProcessHost::Iterator iter(ChildProcessInfo::WORKER_PROCESS); | |
| 76 !iter.Done(); ++iter) { | |
| 77 WorkerProcessHost* worker = static_cast<WorkerProcessHost*>(*iter); | |
| 78 worker->RendererShutdown(filter); | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 WorkerProcessHost* WorkerService::GetProcessForDomain(const GURL& url) { | 82 WorkerProcessHost* WorkerService::GetProcessForDomain(const GURL& url) { |
| 83 int num_processes = 0; | 83 int num_processes = 0; |
| 84 std::string domain = | 84 std::string domain = |
| 85 net::RegistryControlledDomainService::GetDomainAndRegistry(url); | 85 net::RegistryControlledDomainService::GetDomainAndRegistry(url); |
| 86 for (ChildProcessHost::Iterator iter(ChildProcessInfo::WORKER_PROCESS); | 86 for (ChildProcessHost::Iterator iter(ChildProcessInfo::WORKER_PROCESS); |
| 87 !iter.Done(); ++iter) { | 87 !iter.Done(); ++iter) { |
| 88 num_processes++; | 88 num_processes++; |
| 89 WorkerProcessHost* worker = static_cast<WorkerProcessHost*>(*iter); | 89 WorkerProcessHost* worker = static_cast<WorkerProcessHost*>(*iter); |
| 90 for (WorkerProcessHost::Instances::const_iterator instance = | 90 for (WorkerProcessHost::Instances::const_iterator instance = |
| 91 worker->instances().begin(); | 91 worker->instances().begin(); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 119 WorkerProcessHost* smallest = NULL; | 119 WorkerProcessHost* smallest = NULL; |
| 120 for (ChildProcessHost::Iterator iter(ChildProcessInfo::WORKER_PROCESS); | 120 for (ChildProcessHost::Iterator iter(ChildProcessInfo::WORKER_PROCESS); |
| 121 !iter.Done(); ++iter) { | 121 !iter.Done(); ++iter) { |
| 122 WorkerProcessHost* worker = static_cast<WorkerProcessHost*>(*iter); | 122 WorkerProcessHost* worker = static_cast<WorkerProcessHost*>(*iter); |
| 123 if (!smallest || worker->instances().size() < smallest->instances().size()) | 123 if (!smallest || worker->instances().size() < smallest->instances().size()) |
| 124 smallest = worker; | 124 smallest = worker; |
| 125 } | 125 } |
| 126 | 126 |
| 127 return smallest; | 127 return smallest; |
| 128 } | 128 } |
| 129 |
| 130 void WorkerService::Observe(NotificationType type, |
| 131 const NotificationSource& source, |
| 132 const NotificationDetails& details) { |
| 133 DCHECK(type.value == NotificationType::RESOURCE_MESSAGE_FILTER_SHUTDOWN); |
| 134 ResourceMessageFilter* filter = Source<ResourceMessageFilter>(source).ptr(); |
| 135 |
| 136 for (ChildProcessHost::Iterator iter(ChildProcessInfo::WORKER_PROCESS); |
| 137 !iter.Done(); ++iter) { |
| 138 WorkerProcessHost* worker = static_cast<WorkerProcessHost*>(*iter); |
| 139 worker->RendererShutdown(filter); |
| 140 } |
| 141 |
| 142 NotificationService::current()->RemoveObserver( |
| 143 this, |
| 144 NotificationType::RESOURCE_MESSAGE_FILTER_SHUTDOWN, |
| 145 Source<ResourceMessageFilter>(filter)); |
| 146 } |
| OLD | NEW |