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, |
33 int64 service_worker_version_id, | |
34 const GURL& scope, | |
35 const GURL& script_url, | |
36 const StatusCallback& callback) { | |
37 context_->process_manager()->AllocateWorkerProcess( | |
38 process_ids, | |
39 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)); | |
49 } | |
50 | |
51 void EmbeddedWorkerRegistry::StartWorkerWithProcessId( | |
kinuko
2014/04/28 06:58:16
nit: method order's different from .h
Jeffrey Yasskin
2014/04/28 20:47:40
Done.
| |
30 int embedded_worker_id, | 52 int embedded_worker_id, |
31 int64 service_worker_version_id, | 53 scoped_ptr<EmbeddedWorkerMsg_StartWorker> message, |
32 const GURL& scope, | 54 const StatusCallback& callback, |
33 const GURL& script_url) { | 55 ServiceWorkerStatusCode status, |
34 return Send( | 56 int process_id) { |
35 process_id, | 57 DCHECK(ContainsKey(worker_map_, embedded_worker_id)); |
kinuko
2014/04/28 06:58:16
Technically ContainsKey() could be false if all wo
Jeffrey Yasskin
2014/04/28 20:47:40
Good catch. I think I'd gotten confused between wo
| |
36 new EmbeddedWorkerMsg_StartWorker( | 58 worker_map_[embedded_worker_id]->RecordProcessId(process_id, status); |
37 embedded_worker_id, service_worker_version_id, scope, script_url)); | 59 |
60 if (status != SERVICE_WORKER_OK) { | |
61 callback.Run(status); | |
62 return; | |
63 } | |
64 DCHECK(ContainsKey(process_sender_map_, process_id)); | |
65 callback.Run(Send(process_id, message.release())); | |
38 } | 66 } |
39 | 67 |
40 ServiceWorkerStatusCode EmbeddedWorkerRegistry::StopWorker( | 68 ServiceWorkerStatusCode EmbeddedWorkerRegistry::StopWorker( |
41 int process_id, int embedded_worker_id) { | 69 int process_id, int embedded_worker_id) { |
70 if (context_) | |
71 context_->process_manager()->ReleaseWorkerProcess(process_id); | |
42 return Send(process_id, | 72 return Send(process_id, |
43 new EmbeddedWorkerMsg_StopWorker(embedded_worker_id)); | 73 new EmbeddedWorkerMsg_StopWorker(embedded_worker_id)); |
44 } | 74 } |
45 | 75 |
46 bool EmbeddedWorkerRegistry::OnMessageReceived(const IPC::Message& message) { | 76 bool EmbeddedWorkerRegistry::OnMessageReceived(const IPC::Message& message) { |
47 // TODO(kinuko): Move all EmbeddedWorker message handling from | 77 // TODO(kinuko): Move all EmbeddedWorker message handling from |
48 // ServiceWorkerDispatcherHost. | 78 // ServiceWorkerDispatcherHost. |
49 | 79 |
50 WorkerInstanceMap::iterator found = worker_map_.find(message.routing_id()); | 80 WorkerInstanceMap::iterator found = worker_map_.find(message.routing_id()); |
51 if (found == worker_map_.end()) { | 81 if (found == worker_map_.end()) { |
52 LOG(ERROR) << "Worker " << message.routing_id() << " not registered"; | 82 LOG(ERROR) << "Worker " << message.routing_id() << " not registered"; |
53 return false; | 83 return false; |
54 } | 84 } |
55 return found->second->OnMessageReceived(message); | 85 return found->second->OnMessageReceived(message); |
56 } | 86 } |
57 | 87 |
88 void EmbeddedWorkerRegistry::Shutdown() { | |
89 for (WorkerInstanceMap::iterator it = worker_map_.begin(); | |
90 it != worker_map_.end(); | |
91 ++it) { | |
92 it->second->Stop(); | |
93 } | |
94 } | |
95 | |
58 void EmbeddedWorkerRegistry::OnWorkerStarted( | 96 void EmbeddedWorkerRegistry::OnWorkerStarted( |
59 int process_id, int thread_id, int embedded_worker_id) { | 97 int process_id, int thread_id, int embedded_worker_id) { |
60 DCHECK(!ContainsKey(worker_process_map_, process_id) || | 98 DCHECK(!ContainsKey(worker_process_map_, process_id) || |
61 worker_process_map_[process_id].count(embedded_worker_id) == 0); | 99 worker_process_map_[process_id].count(embedded_worker_id) == 0); |
62 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); | 100 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); |
63 if (found == worker_map_.end()) { | 101 if (found == worker_map_.end()) { |
64 LOG(ERROR) << "Worker " << embedded_worker_id << " not registered"; | 102 LOG(ERROR) << "Worker " << embedded_worker_id << " not registered"; |
65 return; | 103 return; |
66 } | 104 } |
67 worker_process_map_[process_id].insert(embedded_worker_id); | 105 worker_process_map_[process_id].insert(embedded_worker_id); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
136 } | 174 } |
137 | 175 |
138 EmbeddedWorkerInstance* EmbeddedWorkerRegistry::GetWorker( | 176 EmbeddedWorkerInstance* EmbeddedWorkerRegistry::GetWorker( |
139 int embedded_worker_id) { | 177 int embedded_worker_id) { |
140 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); | 178 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); |
141 if (found == worker_map_.end()) | 179 if (found == worker_map_.end()) |
142 return NULL; | 180 return NULL; |
143 return found->second; | 181 return found->second; |
144 } | 182 } |
145 | 183 |
146 EmbeddedWorkerRegistry::~EmbeddedWorkerRegistry() {} | 184 EmbeddedWorkerRegistry::~EmbeddedWorkerRegistry() { |
185 Shutdown(); | |
186 } | |
147 | 187 |
148 ServiceWorkerStatusCode EmbeddedWorkerRegistry::Send( | 188 ServiceWorkerStatusCode EmbeddedWorkerRegistry::Send( |
149 int process_id, IPC::Message* message) { | 189 int process_id, IPC::Message* message) { |
150 if (!context_) | 190 if (!context_) |
151 return SERVICE_WORKER_ERROR_ABORT; | 191 return SERVICE_WORKER_ERROR_ABORT; |
152 ProcessToSenderMap::iterator found = process_sender_map_.find(process_id); | 192 ProcessToSenderMap::iterator found = process_sender_map_.find(process_id); |
153 if (found == process_sender_map_.end()) | 193 if (found == process_sender_map_.end()) |
154 return SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND; | 194 return SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND; |
155 if (!found->second->Send(message)) | 195 if (!found->second->Send(message)) |
156 return SERVICE_WORKER_ERROR_IPC_FAILED; | 196 return SERVICE_WORKER_ERROR_IPC_FAILED; |
157 return SERVICE_WORKER_OK; | 197 return SERVICE_WORKER_OK; |
158 } | 198 } |
159 | 199 |
160 void EmbeddedWorkerRegistry::RemoveWorker(int process_id, | 200 void EmbeddedWorkerRegistry::RemoveWorker(int process_id, |
161 int embedded_worker_id) { | 201 int embedded_worker_id) { |
162 DCHECK(ContainsKey(worker_map_, embedded_worker_id)); | 202 DCHECK(ContainsKey(worker_map_, embedded_worker_id)); |
163 worker_map_.erase(embedded_worker_id); | 203 worker_map_.erase(embedded_worker_id); |
164 worker_process_map_.erase(process_id); | 204 worker_process_map_.erase(process_id); |
165 } | 205 } |
166 | 206 |
167 } // namespace content | 207 } // namespace content |
OLD | NEW |