| 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/stl_util.h" | 7 #include "base/stl_util.h" |
| 8 #include "content/browser/service_worker/embedded_worker_instance.h" | 8 #include "content/browser/service_worker/embedded_worker_instance.h" |
| 9 #include "content/browser/service_worker/service_worker_context_core.h" | 9 #include "content/browser/service_worker/service_worker_context_core.h" |
| 10 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 10 #include "content/common/service_worker/embedded_worker_messages.h" | 11 #include "content/common/service_worker/embedded_worker_messages.h" |
| 12 #include "content/public/browser/browser_thread.h" |
| 11 #include "ipc/ipc_message.h" | 13 #include "ipc/ipc_message.h" |
| 12 #include "ipc/ipc_sender.h" | 14 #include "ipc/ipc_sender.h" |
| 13 | 15 |
| 14 namespace content { | 16 namespace content { |
| 15 | 17 |
| 16 EmbeddedWorkerRegistry::EmbeddedWorkerRegistry( | 18 EmbeddedWorkerRegistry::EmbeddedWorkerRegistry( |
| 17 base::WeakPtr<ServiceWorkerContextCore> context) | 19 base::WeakPtr<ServiceWorkerContextCore> context) |
| 18 : context_(context), | 20 : context_(context), |
| 19 next_embedded_worker_id_(0) {} | 21 next_embedded_worker_id_(0) {} |
| 20 | 22 |
| 21 scoped_ptr<EmbeddedWorkerInstance> EmbeddedWorkerRegistry::CreateWorker() { | 23 scoped_ptr<EmbeddedWorkerInstance> EmbeddedWorkerRegistry::CreateWorker() { |
| 22 scoped_ptr<EmbeddedWorkerInstance> worker( | 24 scoped_ptr<EmbeddedWorkerInstance> worker( |
| 23 new EmbeddedWorkerInstance(this, next_embedded_worker_id_)); | 25 new EmbeddedWorkerInstance(this, next_embedded_worker_id_)); |
| 24 worker_map_[next_embedded_worker_id_++] = worker.get(); | 26 worker_map_[next_embedded_worker_id_++] = worker.get(); |
| 25 return worker.Pass(); | 27 return worker.Pass(); |
| 26 } | 28 } |
| 27 | 29 |
| 28 ServiceWorkerStatusCode EmbeddedWorkerRegistry::StartWorker( | 30 void EmbeddedWorkerRegistry::StartWorker(const std::vector<int>& process_ids, |
| 29 int process_id, | 31 int embedded_worker_id, |
| 32 int64 service_worker_version_id, |
| 33 const GURL& scope, |
| 34 const GURL& script_url, |
| 35 const StatusCallback& callback) { |
| 36 BrowserThread::PostTask( |
| 37 BrowserThread::UI, |
| 38 FROM_HERE, |
| 39 base::Bind( |
| 40 &ServiceWorkerContextWrapper::IncrementWorkerRef, |
| 41 context_->wrapper(), |
| 42 process_ids, |
| 43 script_url, |
| 44 base::Bind(&EmbeddedWorkerRegistry::StartWorkerWithProcessId, |
| 45 this, |
| 46 embedded_worker_id, |
| 47 Passed(make_scoped_ptr(new EmbeddedWorkerMsg_StartWorker( |
| 48 embedded_worker_id, |
| 49 service_worker_version_id, |
| 50 scope, |
| 51 script_url))), |
| 52 callback))); |
| 53 } |
| 54 |
| 55 void EmbeddedWorkerRegistry::StartWorkerWithProcessId( |
| 30 int embedded_worker_id, | 56 int embedded_worker_id, |
| 31 int64 service_worker_version_id, | 57 scoped_ptr<EmbeddedWorkerMsg_StartWorker> message, |
| 32 const GURL& scope, | 58 const StatusCallback& callback, |
| 33 const GURL& script_url) { | 59 ServiceWorkerStatusCode status, |
| 34 return Send( | 60 int process_id) { |
| 35 process_id, | 61 DCHECK(ContainsKey(worker_map_, embedded_worker_id)); |
| 36 new EmbeddedWorkerMsg_StartWorker( | 62 worker_map_[embedded_worker_id]->RecordProcessId(process_id, status); |
| 37 embedded_worker_id, service_worker_version_id, scope, script_url)); | 63 |
| 64 if (status != SERVICE_WORKER_OK) { |
| 65 callback.Run(status); |
| 66 return; |
| 67 } |
| 68 DCHECK(ContainsKey(process_sender_map_, process_id)); |
| 69 callback.Run(Send(process_id, message.release())); |
| 38 } | 70 } |
| 39 | 71 |
| 40 ServiceWorkerStatusCode EmbeddedWorkerRegistry::StopWorker( | 72 ServiceWorkerStatusCode EmbeddedWorkerRegistry::StopWorker( |
| 41 int process_id, int embedded_worker_id) { | 73 int process_id, int embedded_worker_id) { |
| 74 BrowserThread::PostTask( |
| 75 BrowserThread::UI, |
| 76 FROM_HERE, |
| 77 base::Bind(&ServiceWorkerContextWrapper::DecrementWorkerRef, process_id)); |
| 42 return Send(process_id, | 78 return Send(process_id, |
| 43 new EmbeddedWorkerMsg_StopWorker(embedded_worker_id)); | 79 new EmbeddedWorkerMsg_StopWorker(embedded_worker_id)); |
| 44 } | 80 } |
| 45 | 81 |
| 46 bool EmbeddedWorkerRegistry::OnMessageReceived(const IPC::Message& message) { | 82 bool EmbeddedWorkerRegistry::OnMessageReceived(const IPC::Message& message) { |
| 47 // TODO(kinuko): Move all EmbeddedWorker message handling from | 83 // TODO(kinuko): Move all EmbeddedWorker message handling from |
| 48 // ServiceWorkerDispatcherHost. | 84 // ServiceWorkerDispatcherHost. |
| 49 | 85 |
| 50 WorkerInstanceMap::iterator found = worker_map_.find(message.routing_id()); | 86 WorkerInstanceMap::iterator found = worker_map_.find(message.routing_id()); |
| 51 if (found == worker_map_.end()) { | 87 if (found == worker_map_.end()) { |
| 52 LOG(ERROR) << "Worker " << message.routing_id() << " not registered"; | 88 LOG(ERROR) << "Worker " << message.routing_id() << " not registered"; |
| 53 return false; | 89 return false; |
| 54 } | 90 } |
| 55 return found->second->OnMessageReceived(message); | 91 return found->second->OnMessageReceived(message); |
| 56 } | 92 } |
| 57 | 93 |
| 94 void EmbeddedWorkerRegistry::Shutdown() { |
| 95 for (WorkerInstanceMap::iterator it = worker_map_.begin(); |
| 96 it != worker_map_.end(); |
| 97 ++it) { |
| 98 it->second->Stop(); |
| 99 } |
| 100 } |
| 101 |
| 58 void EmbeddedWorkerRegistry::OnWorkerStarted( | 102 void EmbeddedWorkerRegistry::OnWorkerStarted( |
| 59 int process_id, int thread_id, int embedded_worker_id) { | 103 int process_id, int thread_id, int embedded_worker_id) { |
| 60 DCHECK(!ContainsKey(worker_process_map_, process_id) || | 104 DCHECK(!ContainsKey(worker_process_map_, process_id) || |
| 61 worker_process_map_[process_id].count(embedded_worker_id) == 0); | 105 worker_process_map_[process_id].count(embedded_worker_id) == 0); |
| 62 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); | 106 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); |
| 63 if (found == worker_map_.end()) { | 107 if (found == worker_map_.end()) { |
| 64 LOG(ERROR) << "Worker " << embedded_worker_id << " not registered"; | 108 LOG(ERROR) << "Worker " << embedded_worker_id << " not registered"; |
| 65 return; | 109 return; |
| 66 } | 110 } |
| 67 worker_process_map_[process_id].insert(embedded_worker_id); | 111 worker_process_map_[process_id].insert(embedded_worker_id); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 } | 180 } |
| 137 | 181 |
| 138 EmbeddedWorkerInstance* EmbeddedWorkerRegistry::GetWorker( | 182 EmbeddedWorkerInstance* EmbeddedWorkerRegistry::GetWorker( |
| 139 int embedded_worker_id) { | 183 int embedded_worker_id) { |
| 140 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); | 184 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); |
| 141 if (found == worker_map_.end()) | 185 if (found == worker_map_.end()) |
| 142 return NULL; | 186 return NULL; |
| 143 return found->second; | 187 return found->second; |
| 144 } | 188 } |
| 145 | 189 |
| 146 EmbeddedWorkerRegistry::~EmbeddedWorkerRegistry() {} | 190 EmbeddedWorkerRegistry::~EmbeddedWorkerRegistry() { |
| 191 Shutdown(); |
| 192 } |
| 147 | 193 |
| 148 ServiceWorkerStatusCode EmbeddedWorkerRegistry::Send( | 194 ServiceWorkerStatusCode EmbeddedWorkerRegistry::Send( |
| 149 int process_id, IPC::Message* message) { | 195 int process_id, IPC::Message* message) { |
| 150 if (!context_) | 196 if (!context_) |
| 151 return SERVICE_WORKER_ERROR_ABORT; | 197 return SERVICE_WORKER_ERROR_ABORT; |
| 152 ProcessToSenderMap::iterator found = process_sender_map_.find(process_id); | 198 ProcessToSenderMap::iterator found = process_sender_map_.find(process_id); |
| 153 if (found == process_sender_map_.end()) | 199 if (found == process_sender_map_.end()) |
| 154 return SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND; | 200 return SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND; |
| 155 if (!found->second->Send(message)) | 201 if (!found->second->Send(message)) |
| 156 return SERVICE_WORKER_ERROR_IPC_FAILED; | 202 return SERVICE_WORKER_ERROR_IPC_FAILED; |
| 157 return SERVICE_WORKER_OK; | 203 return SERVICE_WORKER_OK; |
| 158 } | 204 } |
| 159 | 205 |
| 160 void EmbeddedWorkerRegistry::RemoveWorker(int process_id, | 206 void EmbeddedWorkerRegistry::RemoveWorker(int process_id, |
| 161 int embedded_worker_id) { | 207 int embedded_worker_id) { |
| 162 DCHECK(ContainsKey(worker_map_, embedded_worker_id)); | 208 DCHECK(ContainsKey(worker_map_, embedded_worker_id)); |
| 163 worker_map_.erase(embedded_worker_id); | 209 worker_map_.erase(embedded_worker_id); |
| 164 worker_process_map_.erase(process_id); | 210 worker_process_map_.erase(process_id); |
| 165 } | 211 } |
| 166 | 212 |
| 167 } // namespace content | 213 } // namespace content |
| OLD | NEW |