| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/service_worker/embedded_worker_registry.h" | 5 #include "content/browser/service_worker/embedded_worker_registry.h" |
| 6 | 6 |
| 7 #include "base/bind_helpers.h" | 7 #include "base/bind_helpers.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "content/browser/renderer_host/render_widget_helper.h" | 9 #include "content/browser/renderer_host/render_widget_helper.h" |
| 10 #include "content/browser/service_worker/embedded_worker_instance.h" | 10 #include "content/browser/service_worker/embedded_worker_instance.h" |
| 11 #include "content/browser/service_worker/service_worker_context_core.h" | 11 #include "content/browser/service_worker/service_worker_context_core.h" |
| 12 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 12 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 13 #include "content/common/service_worker/embedded_worker_messages.h" | 13 #include "content/common/service_worker/embedded_worker_messages.h" |
| 14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "ipc/ipc_message.h" | 15 #include "ipc/ipc_message.h" |
| 16 #include "ipc/ipc_sender.h" | 16 #include "ipc/ipc_sender.h" |
| 17 | 17 |
| 18 namespace content { | 18 namespace content { |
| 19 | 19 |
| 20 EmbeddedWorkerRegistry::EmbeddedWorkerRegistry( | 20 EmbeddedWorkerRegistry::EmbeddedWorkerRegistry( |
| 21 base::WeakPtr<ServiceWorkerContextCore> context) | 21 base::WeakPtr<ServiceWorkerContextCore> context) |
| 22 : context_(context), | 22 : context_(context), next_embedded_worker_id_(0) { |
| 23 next_embedded_worker_id_(0) {} | 23 } |
| 24 | 24 |
| 25 scoped_ptr<EmbeddedWorkerInstance> EmbeddedWorkerRegistry::CreateWorker() { | 25 scoped_ptr<EmbeddedWorkerInstance> EmbeddedWorkerRegistry::CreateWorker() { |
| 26 scoped_ptr<EmbeddedWorkerInstance> worker( | 26 scoped_ptr<EmbeddedWorkerInstance> worker( |
| 27 new EmbeddedWorkerInstance(this, next_embedded_worker_id_)); | 27 new EmbeddedWorkerInstance(context_, next_embedded_worker_id_)); |
| 28 worker_map_[next_embedded_worker_id_++] = worker.get(); | 28 worker_map_[next_embedded_worker_id_++] = worker.get(); |
| 29 return worker.Pass(); | 29 return worker.Pass(); |
| 30 } | 30 } |
| 31 | 31 |
| 32 void EmbeddedWorkerRegistry::StartWorker(const std::vector<int>& process_ids, | |
| 33 int embedded_worker_id, | |
| 34 int64 service_worker_version_id, | |
| 35 const GURL& scope, | |
| 36 const GURL& script_url, | |
| 37 const StatusCallback& callback) { | |
| 38 if (!context_) { | |
| 39 callback.Run(SERVICE_WORKER_ERROR_ABORT); | |
| 40 return; | |
| 41 } | |
| 42 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( | |
| 43 new EmbeddedWorkerMsg_StartWorker_Params()); | |
| 44 params->embedded_worker_id = embedded_worker_id; | |
| 45 params->service_worker_version_id = service_worker_version_id; | |
| 46 params->scope = scope; | |
| 47 params->script_url = script_url; | |
| 48 params->worker_devtools_agent_route_id = MSG_ROUTING_NONE; | |
| 49 context_->process_manager()->AllocateWorkerProcess( | |
| 50 process_ids, | |
| 51 script_url, | |
| 52 base::Bind(&EmbeddedWorkerRegistry::StartWorkerWithProcessId, | |
| 53 this, | |
| 54 embedded_worker_id, | |
| 55 base::Passed(¶ms), | |
| 56 callback)); | |
| 57 } | |
| 58 | |
| 59 ServiceWorkerStatusCode EmbeddedWorkerRegistry::StopWorker( | 32 ServiceWorkerStatusCode EmbeddedWorkerRegistry::StopWorker( |
| 60 int process_id, int embedded_worker_id) { | 33 int process_id, int embedded_worker_id) { |
| 61 if (context_) | |
| 62 context_->process_manager()->ReleaseWorkerProcess(process_id); | |
| 63 return Send(process_id, | 34 return Send(process_id, |
| 64 new EmbeddedWorkerMsg_StopWorker(embedded_worker_id)); | 35 new EmbeddedWorkerMsg_StopWorker(embedded_worker_id)); |
| 65 } | 36 } |
| 66 | 37 |
| 67 bool EmbeddedWorkerRegistry::OnMessageReceived(const IPC::Message& message) { | 38 bool EmbeddedWorkerRegistry::OnMessageReceived(const IPC::Message& message) { |
| 68 // TODO(kinuko): Move all EmbeddedWorker message handling from | 39 // TODO(kinuko): Move all EmbeddedWorker message handling from |
| 69 // ServiceWorkerDispatcherHost. | 40 // ServiceWorkerDispatcherHost. |
| 70 | 41 |
| 71 WorkerInstanceMap::iterator found = worker_map_.find(message.routing_id()); | 42 WorkerInstanceMap::iterator found = worker_map_.find(message.routing_id()); |
| 72 if (found == worker_map_.end()) { | 43 if (found == worker_map_.end()) { |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); | 174 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); |
| 204 if (found == worker_map_.end()) | 175 if (found == worker_map_.end()) |
| 205 return NULL; | 176 return NULL; |
| 206 return found->second; | 177 return found->second; |
| 207 } | 178 } |
| 208 | 179 |
| 209 EmbeddedWorkerRegistry::~EmbeddedWorkerRegistry() { | 180 EmbeddedWorkerRegistry::~EmbeddedWorkerRegistry() { |
| 210 Shutdown(); | 181 Shutdown(); |
| 211 } | 182 } |
| 212 | 183 |
| 213 void EmbeddedWorkerRegistry::StartWorkerWithProcessId( | 184 void EmbeddedWorkerRegistry::SendStartWorker( |
| 214 int embedded_worker_id, | |
| 215 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, | 185 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, |
| 216 const StatusCallback& callback, | 186 const StatusCallback& callback, |
| 217 ServiceWorkerStatusCode status, | |
| 218 int process_id) { | 187 int process_id) { |
| 219 WorkerInstanceMap::const_iterator worker = | |
| 220 worker_map_.find(embedded_worker_id); | |
| 221 if (worker == worker_map_.end()) { | |
| 222 // The Instance was destroyed before it could finish starting. Undo what | |
| 223 // we've done so far. | |
| 224 if (context_) | |
| 225 context_->process_manager()->ReleaseWorkerProcess(process_id); | |
| 226 callback.Run(SERVICE_WORKER_ERROR_ABORT); | |
| 227 return; | |
| 228 } | |
| 229 if (status == SERVICE_WORKER_OK) { | |
| 230 // Gets the new routing id for the renderer process. | |
| 231 scoped_refptr<RenderWidgetHelper> helper( | |
| 232 RenderWidgetHelper::FromProcessHostID(process_id)); | |
| 233 // |helper| may be NULL in unittest. | |
| 234 params->worker_devtools_agent_route_id = | |
| 235 helper ? helper->GetNextRoutingID() : MSG_ROUTING_NONE; | |
| 236 } | |
| 237 worker->second->RecordProcessId( | |
| 238 process_id, status, params->worker_devtools_agent_route_id); | |
| 239 | |
| 240 if (status != SERVICE_WORKER_OK) { | |
| 241 callback.Run(status); | |
| 242 return; | |
| 243 } | |
| 244 // The ServiceWorkerDispatcherHost is supposed to be created when the process | 188 // The ServiceWorkerDispatcherHost is supposed to be created when the process |
| 245 // is created, and keep an entry in process_sender_map_ for its whole | 189 // is created, and keep an entry in process_sender_map_ for its whole |
| 246 // lifetime. | 190 // lifetime. |
| 247 DCHECK(ContainsKey(process_sender_map_, process_id)); | 191 DCHECK(ContainsKey(process_sender_map_, process_id)); |
| 248 callback.Run(Send(process_id, new EmbeddedWorkerMsg_StartWorker(*params))); | 192 callback.Run(Send(process_id, new EmbeddedWorkerMsg_StartWorker(*params))); |
| 249 } | 193 } |
| 250 | 194 |
| 251 ServiceWorkerStatusCode EmbeddedWorkerRegistry::Send( | 195 ServiceWorkerStatusCode EmbeddedWorkerRegistry::Send( |
| 252 int process_id, IPC::Message* message_ptr) { | 196 int process_id, IPC::Message* message_ptr) { |
| 253 scoped_ptr<IPC::Message> message(message_ptr); | 197 scoped_ptr<IPC::Message> message(message_ptr); |
| 254 if (!context_) | 198 if (!context_) |
| 255 return SERVICE_WORKER_ERROR_ABORT; | 199 return SERVICE_WORKER_ERROR_ABORT; |
| 256 ProcessToSenderMap::iterator found = process_sender_map_.find(process_id); | 200 ProcessToSenderMap::iterator found = process_sender_map_.find(process_id); |
| 257 if (found == process_sender_map_.end()) | 201 if (found == process_sender_map_.end()) |
| 258 return SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND; | 202 return SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND; |
| 259 if (!found->second->Send(message.release())) | 203 if (!found->second->Send(message.release())) |
| 260 return SERVICE_WORKER_ERROR_IPC_FAILED; | 204 return SERVICE_WORKER_ERROR_IPC_FAILED; |
| 261 return SERVICE_WORKER_OK; | 205 return SERVICE_WORKER_OK; |
| 262 } | 206 } |
| 263 | 207 |
| 264 void EmbeddedWorkerRegistry::RemoveWorker(int process_id, | 208 void EmbeddedWorkerRegistry::RemoveWorker(int process_id, |
| 265 int embedded_worker_id) { | 209 int embedded_worker_id) { |
| 266 DCHECK(ContainsKey(worker_map_, embedded_worker_id)); | 210 DCHECK(ContainsKey(worker_map_, embedded_worker_id)); |
| 267 worker_map_.erase(embedded_worker_id); | 211 worker_map_.erase(embedded_worker_id); |
| 268 worker_process_map_.erase(process_id); | 212 worker_process_map_.erase(process_id); |
| 269 } | 213 } |
| 270 | 214 |
| 271 } // namespace content | 215 } // namespace content |
| OLD | NEW |