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/stl_util.h" | 8 #include "base/stl_util.h" |
8 #include "content/browser/service_worker/embedded_worker_instance.h" | 9 #include "content/browser/service_worker/embedded_worker_instance.h" |
9 #include "content/browser/service_worker/service_worker_context_core.h" | 10 #include "content/browser/service_worker/service_worker_context_core.h" |
11 #include "content/browser/service_worker/service_worker_context_wrapper.h" | |
10 #include "content/common/service_worker/embedded_worker_messages.h" | 12 #include "content/common/service_worker/embedded_worker_messages.h" |
13 #include "content/public/browser/browser_thread.h" | |
11 #include "ipc/ipc_message.h" | 14 #include "ipc/ipc_message.h" |
12 #include "ipc/ipc_sender.h" | 15 #include "ipc/ipc_sender.h" |
13 | 16 |
14 namespace content { | 17 namespace content { |
15 | 18 |
16 EmbeddedWorkerRegistry::EmbeddedWorkerRegistry( | 19 EmbeddedWorkerRegistry::EmbeddedWorkerRegistry( |
17 base::WeakPtr<ServiceWorkerContextCore> context) | 20 base::WeakPtr<ServiceWorkerContextCore> context) |
18 : context_(context), | 21 : context_(context), |
19 next_embedded_worker_id_(0) {} | 22 next_embedded_worker_id_(0) {} |
20 | 23 |
21 scoped_ptr<EmbeddedWorkerInstance> EmbeddedWorkerRegistry::CreateWorker() { | 24 scoped_ptr<EmbeddedWorkerInstance> EmbeddedWorkerRegistry::CreateWorker() { |
22 scoped_ptr<EmbeddedWorkerInstance> worker( | 25 scoped_ptr<EmbeddedWorkerInstance> worker( |
23 new EmbeddedWorkerInstance(this, next_embedded_worker_id_)); | 26 new EmbeddedWorkerInstance(this, next_embedded_worker_id_)); |
24 worker_map_[next_embedded_worker_id_++] = worker.get(); | 27 worker_map_[next_embedded_worker_id_++] = worker.get(); |
25 return worker.Pass(); | 28 return worker.Pass(); |
26 } | 29 } |
27 | 30 |
28 ServiceWorkerStatusCode EmbeddedWorkerRegistry::StartWorker( | 31 void EmbeddedWorkerRegistry::StartWorker(const std::vector<int>& process_ids, |
29 int process_id, | 32 int embedded_worker_id, |
30 int embedded_worker_id, | 33 int64 service_worker_version_id, |
31 int64 service_worker_version_id, | 34 const GURL& scope, |
32 const GURL& scope, | 35 const GURL& script_url, |
33 const GURL& script_url) { | 36 const StatusCallback& callback) { |
34 return Send( | 37 context_->process_manager()->AllocateWorkerProcess( |
35 process_id, | 38 process_ids, |
36 new EmbeddedWorkerMsg_StartWorker( | 39 script_url, |
37 embedded_worker_id, service_worker_version_id, scope, script_url)); | 40 base::Bind(&EmbeddedWorkerRegistry::StartWorkerWithProcessId, |
41 this, | |
42 embedded_worker_id, | |
43 base::Passed(make_scoped_ptr(new EmbeddedWorkerMsg_StartWorker( | |
44 embedded_worker_id, | |
45 service_worker_version_id, | |
46 scope, | |
47 script_url))), | |
48 callback)); | |
38 } | 49 } |
39 | 50 |
40 ServiceWorkerStatusCode EmbeddedWorkerRegistry::StopWorker( | 51 ServiceWorkerStatusCode EmbeddedWorkerRegistry::StopWorker( |
41 int process_id, int embedded_worker_id) { | 52 int process_id, int embedded_worker_id) { |
53 if (context_) | |
54 context_->process_manager()->ReleaseWorkerProcess(process_id); | |
42 return Send(process_id, | 55 return Send(process_id, |
43 new EmbeddedWorkerMsg_StopWorker(embedded_worker_id)); | 56 new EmbeddedWorkerMsg_StopWorker(embedded_worker_id)); |
44 } | 57 } |
45 | 58 |
46 bool EmbeddedWorkerRegistry::OnMessageReceived(const IPC::Message& message) { | 59 bool EmbeddedWorkerRegistry::OnMessageReceived(const IPC::Message& message) { |
47 // TODO(kinuko): Move all EmbeddedWorker message handling from | 60 // TODO(kinuko): Move all EmbeddedWorker message handling from |
48 // ServiceWorkerDispatcherHost. | 61 // ServiceWorkerDispatcherHost. |
49 | 62 |
50 WorkerInstanceMap::iterator found = worker_map_.find(message.routing_id()); | 63 WorkerInstanceMap::iterator found = worker_map_.find(message.routing_id()); |
51 if (found == worker_map_.end()) { | 64 if (found == worker_map_.end()) { |
52 LOG(ERROR) << "Worker " << message.routing_id() << " not registered"; | 65 LOG(ERROR) << "Worker " << message.routing_id() << " not registered"; |
53 return false; | 66 return false; |
54 } | 67 } |
55 return found->second->OnMessageReceived(message); | 68 return found->second->OnMessageReceived(message); |
56 } | 69 } |
57 | 70 |
71 void EmbeddedWorkerRegistry::Shutdown() { | |
72 for (WorkerInstanceMap::iterator it = worker_map_.begin(); | |
73 it != worker_map_.end(); | |
74 ++it) { | |
75 it->second->Stop(); | |
76 } | |
77 } | |
78 | |
58 void EmbeddedWorkerRegistry::OnWorkerStarted( | 79 void EmbeddedWorkerRegistry::OnWorkerStarted( |
59 int process_id, int thread_id, int embedded_worker_id) { | 80 int process_id, int thread_id, int embedded_worker_id) { |
60 DCHECK(!ContainsKey(worker_process_map_, process_id) || | 81 DCHECK(!ContainsKey(worker_process_map_, process_id) || |
61 worker_process_map_[process_id].count(embedded_worker_id) == 0); | 82 worker_process_map_[process_id].count(embedded_worker_id) == 0); |
62 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); | 83 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); |
63 if (found == worker_map_.end()) { | 84 if (found == worker_map_.end()) { |
64 LOG(ERROR) << "Worker " << embedded_worker_id << " not registered"; | 85 LOG(ERROR) << "Worker " << embedded_worker_id << " not registered"; |
65 return; | 86 return; |
66 } | 87 } |
67 worker_process_map_[process_id].insert(embedded_worker_id); | 88 worker_process_map_[process_id].insert(embedded_worker_id); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
136 } | 157 } |
137 | 158 |
138 EmbeddedWorkerInstance* EmbeddedWorkerRegistry::GetWorker( | 159 EmbeddedWorkerInstance* EmbeddedWorkerRegistry::GetWorker( |
139 int embedded_worker_id) { | 160 int embedded_worker_id) { |
140 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); | 161 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); |
141 if (found == worker_map_.end()) | 162 if (found == worker_map_.end()) |
142 return NULL; | 163 return NULL; |
143 return found->second; | 164 return found->second; |
144 } | 165 } |
145 | 166 |
146 EmbeddedWorkerRegistry::~EmbeddedWorkerRegistry() {} | 167 EmbeddedWorkerRegistry::~EmbeddedWorkerRegistry() { |
168 Shutdown(); | |
169 } | |
170 | |
171 void EmbeddedWorkerRegistry::StartWorkerWithProcessId( | |
172 int embedded_worker_id, | |
173 scoped_ptr<EmbeddedWorkerMsg_StartWorker> message, | |
174 const StatusCallback& callback, | |
175 ServiceWorkerStatusCode status, | |
176 int process_id) { | |
177 WorkerInstanceMap::const_iterator worker = | |
178 worker_map_.find(embedded_worker_id); | |
179 if (worker == worker_map_.end()) { | |
180 // The Instance was destroyed before it could finish starting. Undo what | |
181 // we've done so far. | |
182 context_->process_manager()->ReleaseWorkerProcess(process_id); | |
kinuko
2014/04/29 01:06:52
Check if (context_) before accessing it here too?
Jeffrey Yasskin
2014/04/29 01:37:29
Done, here and in StartWorker itself.
| |
183 callback.Run(SERVICE_WORKER_ERROR_ABORT); | |
184 return; | |
185 } | |
186 worker->second->RecordProcessId(process_id, status); | |
187 | |
188 if (status != SERVICE_WORKER_OK) { | |
189 callback.Run(status); | |
190 return; | |
191 } | |
192 // The ServiceWorkerDispatcherHost is supposed to be created when the process | |
193 // is created, and keep an entry in process_sender_map_ for its whole | |
194 // lifetime. | |
195 DCHECK(ContainsKey(process_sender_map_, process_id)); | |
196 callback.Run(Send(process_id, message.release())); | |
197 } | |
147 | 198 |
148 ServiceWorkerStatusCode EmbeddedWorkerRegistry::Send( | 199 ServiceWorkerStatusCode EmbeddedWorkerRegistry::Send( |
149 int process_id, IPC::Message* message) { | 200 int process_id, IPC::Message* message) { |
150 if (!context_) | 201 if (!context_) |
151 return SERVICE_WORKER_ERROR_ABORT; | 202 return SERVICE_WORKER_ERROR_ABORT; |
152 ProcessToSenderMap::iterator found = process_sender_map_.find(process_id); | 203 ProcessToSenderMap::iterator found = process_sender_map_.find(process_id); |
153 if (found == process_sender_map_.end()) | 204 if (found == process_sender_map_.end()) |
154 return SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND; | 205 return SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND; |
155 if (!found->second->Send(message)) | 206 if (!found->second->Send(message)) |
156 return SERVICE_WORKER_ERROR_IPC_FAILED; | 207 return SERVICE_WORKER_ERROR_IPC_FAILED; |
157 return SERVICE_WORKER_OK; | 208 return SERVICE_WORKER_OK; |
158 } | 209 } |
159 | 210 |
160 void EmbeddedWorkerRegistry::RemoveWorker(int process_id, | 211 void EmbeddedWorkerRegistry::RemoveWorker(int process_id, |
161 int embedded_worker_id) { | 212 int embedded_worker_id) { |
162 DCHECK(ContainsKey(worker_map_, embedded_worker_id)); | 213 DCHECK(ContainsKey(worker_map_, embedded_worker_id)); |
163 worker_map_.erase(embedded_worker_id); | 214 worker_map_.erase(embedded_worker_id); |
164 worker_process_map_.erase(process_id); | 215 worker_process_map_.erase(process_id); |
165 } | 216 } |
166 | 217 |
167 } // namespace content | 218 } // namespace content |
OLD | NEW |