| 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 const GURL& scope) { |
| 26 scoped_ptr<EmbeddedWorkerInstance> worker( | 27 scoped_ptr<EmbeddedWorkerInstance> worker( |
| 27 new EmbeddedWorkerInstance(this, next_embedded_worker_id_)); | 28 new EmbeddedWorkerInstance(context_, next_embedded_worker_id_, scope)); |
| 28 worker_map_[next_embedded_worker_id_++] = worker.get(); | 29 worker_map_[next_embedded_worker_id_++] = worker.get(); |
| 29 return worker.Pass(); | 30 return worker.Pass(); |
| 30 } | 31 } |
| 31 | 32 |
| 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( | 33 ServiceWorkerStatusCode EmbeddedWorkerRegistry::StopWorker( |
| 60 int process_id, int embedded_worker_id) { | 34 int process_id, int embedded_worker_id) { |
| 61 if (context_) | 35 if (context_) |
| 62 context_->process_manager()->ReleaseWorkerProcess(process_id); | 36 context_->process_manager()->ReleaseWorkerProcess(process_id); |
| 63 return Send(process_id, | 37 return Send(process_id, |
| 64 new EmbeddedWorkerMsg_StopWorker(embedded_worker_id)); | 38 new EmbeddedWorkerMsg_StopWorker(embedded_worker_id)); |
| 65 } | 39 } |
| 66 | 40 |
| 67 bool EmbeddedWorkerRegistry::OnMessageReceived(const IPC::Message& message) { | 41 bool EmbeddedWorkerRegistry::OnMessageReceived(const IPC::Message& message) { |
| 68 // TODO(kinuko): Move all EmbeddedWorker message handling from | 42 // TODO(kinuko): Move all EmbeddedWorker message handling from |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); | 177 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); |
| 204 if (found == worker_map_.end()) | 178 if (found == worker_map_.end()) |
| 205 return NULL; | 179 return NULL; |
| 206 return found->second; | 180 return found->second; |
| 207 } | 181 } |
| 208 | 182 |
| 209 EmbeddedWorkerRegistry::~EmbeddedWorkerRegistry() { | 183 EmbeddedWorkerRegistry::~EmbeddedWorkerRegistry() { |
| 210 Shutdown(); | 184 Shutdown(); |
| 211 } | 185 } |
| 212 | 186 |
| 213 void EmbeddedWorkerRegistry::StartWorkerWithProcessId( | 187 void EmbeddedWorkerRegistry::SendStartWorker( |
| 214 int embedded_worker_id, | |
| 215 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, | 188 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, |
| 216 const StatusCallback& callback, | 189 const StatusCallback& callback, |
| 217 ServiceWorkerStatusCode status, | |
| 218 int process_id) { | 190 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 | 191 // The ServiceWorkerDispatcherHost is supposed to be created when the process |
| 245 // is created, and keep an entry in process_sender_map_ for its whole | 192 // is created, and keep an entry in process_sender_map_ for its whole |
| 246 // lifetime. | 193 // lifetime. |
| 247 DCHECK(ContainsKey(process_sender_map_, process_id)); | 194 DCHECK(ContainsKey(process_sender_map_, process_id)); |
| 248 callback.Run(Send(process_id, new EmbeddedWorkerMsg_StartWorker(*params))); | 195 callback.Run(Send(process_id, new EmbeddedWorkerMsg_StartWorker(*params))); |
| 249 } | 196 } |
| 250 | 197 |
| 251 ServiceWorkerStatusCode EmbeddedWorkerRegistry::Send( | 198 ServiceWorkerStatusCode EmbeddedWorkerRegistry::Send( |
| 252 int process_id, IPC::Message* message) { | 199 int process_id, IPC::Message* message) { |
| 253 if (!context_) | 200 if (!context_) |
| 254 return SERVICE_WORKER_ERROR_ABORT; | 201 return SERVICE_WORKER_ERROR_ABORT; |
| 255 ProcessToSenderMap::iterator found = process_sender_map_.find(process_id); | 202 ProcessToSenderMap::iterator found = process_sender_map_.find(process_id); |
| 256 if (found == process_sender_map_.end()) | 203 if (found == process_sender_map_.end()) |
| 257 return SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND; | 204 return SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND; |
| 258 if (!found->second->Send(message)) | 205 if (!found->second->Send(message)) |
| 259 return SERVICE_WORKER_ERROR_IPC_FAILED; | 206 return SERVICE_WORKER_ERROR_IPC_FAILED; |
| 260 return SERVICE_WORKER_OK; | 207 return SERVICE_WORKER_OK; |
| 261 } | 208 } |
| 262 | 209 |
| 263 void EmbeddedWorkerRegistry::RemoveWorker(int process_id, | 210 void EmbeddedWorkerRegistry::RemoveWorker(int process_id, |
| 264 int embedded_worker_id) { | 211 int embedded_worker_id) { |
| 265 DCHECK(ContainsKey(worker_map_, embedded_worker_id)); | 212 DCHECK(ContainsKey(worker_map_, embedded_worker_id)); |
| 266 worker_map_.erase(embedded_worker_id); | 213 worker_map_.erase(embedded_worker_id); |
| 267 worker_process_map_.erase(process_id); | 214 worker_process_map_.erase(process_id); |
| 268 } | 215 } |
| 269 | 216 |
| 270 } // namespace content | 217 } // namespace content |
| OLD | NEW |