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_instance.h" | 5 #include "content/browser/service_worker/embedded_worker_instance.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "content/browser/devtools/embedded_worker_devtools_manager.h" | 11 #include "content/browser/devtools/embedded_worker_devtools_manager.h" |
12 #include "content/browser/service_worker/embedded_worker_registry.h" | 12 #include "content/browser/service_worker/embedded_worker_registry.h" |
13 #include "content/browser/service_worker/service_worker_context_core.h" | 13 #include "content/browser/service_worker/service_worker_context_core.h" |
14 #include "content/common/service_worker/embedded_worker_messages.h" | 14 #include "content/common/service_worker/embedded_worker_messages.h" |
| 15 #include "content/common/service_worker/service_worker_types.h" |
15 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
16 #include "content/public/browser/render_process_host.h" | 17 #include "content/public/browser/render_process_host.h" |
17 #include "ipc/ipc_message.h" | 18 #include "ipc/ipc_message.h" |
18 #include "url/gurl.h" | 19 #include "url/gurl.h" |
19 | 20 |
20 namespace content { | 21 namespace content { |
21 | 22 |
22 namespace { | 23 namespace { |
23 | 24 |
24 // Functor to sort by the .second element of a struct. | 25 // Functor to sort by the .second element of a struct. |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 | 165 |
165 void EmbeddedWorkerInstance::ResumeAfterDownload() { | 166 void EmbeddedWorkerInstance::ResumeAfterDownload() { |
166 DCHECK_EQ(STARTING, status_); | 167 DCHECK_EQ(STARTING, status_); |
167 registry_->Send( | 168 registry_->Send( |
168 process_id_, | 169 process_id_, |
169 new EmbeddedWorkerMsg_ResumeAfterDownload(embedded_worker_id_)); | 170 new EmbeddedWorkerMsg_ResumeAfterDownload(embedded_worker_id_)); |
170 } | 171 } |
171 | 172 |
172 ServiceWorkerStatusCode EmbeddedWorkerInstance::SendMessage( | 173 ServiceWorkerStatusCode EmbeddedWorkerInstance::SendMessage( |
173 const IPC::Message& message) { | 174 const IPC::Message& message) { |
174 DCHECK(status_ == RUNNING); | 175 DCHECK_NE(kInvalidEmbeddedWorkerThreadId, thread_id_); |
| 176 if (status_ != RUNNING && status_ != STARTING) |
| 177 return SERVICE_WORKER_ERROR_IPC_FAILED; |
175 return registry_->Send(process_id_, | 178 return registry_->Send(process_id_, |
176 new EmbeddedWorkerContextMsg_MessageToWorker( | 179 new EmbeddedWorkerContextMsg_MessageToWorker( |
177 thread_id_, embedded_worker_id_, message)); | 180 thread_id_, embedded_worker_id_, message)); |
178 } | 181 } |
179 | 182 |
180 void EmbeddedWorkerInstance::AddProcessReference(int process_id) { | 183 void EmbeddedWorkerInstance::AddProcessReference(int process_id) { |
181 ProcessRefMap::iterator found = process_refs_.find(process_id); | 184 ProcessRefMap::iterator found = process_refs_.find(process_id); |
182 if (found == process_refs_.end()) | 185 if (found == process_refs_.end()) |
183 found = process_refs_.insert(std::make_pair(process_id, 0)).first; | 186 found = process_refs_.insert(std::make_pair(process_id, 0)).first; |
184 ++found->second; | 187 ++found->second; |
(...skipping 10 matching lines...) Expand all Loading... |
195 } | 198 } |
196 | 199 |
197 EmbeddedWorkerInstance::EmbeddedWorkerInstance( | 200 EmbeddedWorkerInstance::EmbeddedWorkerInstance( |
198 base::WeakPtr<ServiceWorkerContextCore> context, | 201 base::WeakPtr<ServiceWorkerContextCore> context, |
199 int embedded_worker_id) | 202 int embedded_worker_id) |
200 : context_(context), | 203 : context_(context), |
201 registry_(context->embedded_worker_registry()), | 204 registry_(context->embedded_worker_registry()), |
202 embedded_worker_id_(embedded_worker_id), | 205 embedded_worker_id_(embedded_worker_id), |
203 status_(STOPPED), | 206 status_(STOPPED), |
204 process_id_(-1), | 207 process_id_(-1), |
205 thread_id_(-1), | 208 thread_id_(kInvalidEmbeddedWorkerThreadId), |
206 worker_devtools_agent_route_id_(MSG_ROUTING_NONE), | 209 worker_devtools_agent_route_id_(MSG_ROUTING_NONE), |
207 weak_factory_(this) { | 210 weak_factory_(this) { |
208 } | 211 } |
209 | 212 |
210 // static | 213 // static |
211 void EmbeddedWorkerInstance::RunProcessAllocated( | 214 void EmbeddedWorkerInstance::RunProcessAllocated( |
212 base::WeakPtr<EmbeddedWorkerInstance> instance, | 215 base::WeakPtr<EmbeddedWorkerInstance> instance, |
213 base::WeakPtr<ServiceWorkerContextCore> context, | 216 base::WeakPtr<ServiceWorkerContextCore> context, |
214 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, | 217 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, |
215 const EmbeddedWorkerInstance::StatusCallback& callback, | 218 const EmbeddedWorkerInstance::StatusCallback& callback, |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 params->wait_for_debugger = wait_for_debugger; | 270 params->wait_for_debugger = wait_for_debugger; |
268 registry_->SendStartWorker(params.Pass(), callback, process_id_); | 271 registry_->SendStartWorker(params.Pass(), callback, process_id_); |
269 } | 272 } |
270 | 273 |
271 void EmbeddedWorkerInstance::OnReadyForInspection() { | 274 void EmbeddedWorkerInstance::OnReadyForInspection() { |
272 if (worker_devtools_agent_route_id_ != MSG_ROUTING_NONE) | 275 if (worker_devtools_agent_route_id_ != MSG_ROUTING_NONE) |
273 NotifyWorkerReadyForInspection(process_id_, | 276 NotifyWorkerReadyForInspection(process_id_, |
274 worker_devtools_agent_route_id_); | 277 worker_devtools_agent_route_id_); |
275 } | 278 } |
276 | 279 |
277 void EmbeddedWorkerInstance::OnScriptLoaded() { | 280 void EmbeddedWorkerInstance::OnScriptLoaded(int thread_id) { |
| 281 thread_id_ = thread_id; |
278 if (worker_devtools_agent_route_id_ != MSG_ROUTING_NONE) | 282 if (worker_devtools_agent_route_id_ != MSG_ROUTING_NONE) |
279 NotifyWorkerContextStarted(process_id_, worker_devtools_agent_route_id_); | 283 NotifyWorkerContextStarted(process_id_, worker_devtools_agent_route_id_); |
280 } | 284 } |
281 | 285 |
282 void EmbeddedWorkerInstance::OnScriptLoadFailed() { | 286 void EmbeddedWorkerInstance::OnScriptLoadFailed() { |
283 } | 287 } |
284 | 288 |
285 void EmbeddedWorkerInstance::OnStarted(int thread_id) { | 289 void EmbeddedWorkerInstance::OnStarted() { |
286 // Stop is requested before OnStarted is sent back from the worker. | 290 // Stop is requested before OnStarted is sent back from the worker. |
287 if (status_ == STOPPING) | 291 if (status_ == STOPPING) |
288 return; | 292 return; |
289 DCHECK(status_ == STARTING); | 293 DCHECK(status_ == STARTING); |
290 status_ = RUNNING; | 294 status_ = RUNNING; |
291 thread_id_ = thread_id; | |
292 FOR_EACH_OBSERVER(Listener, listener_list_, OnStarted()); | 295 FOR_EACH_OBSERVER(Listener, listener_list_, OnStarted()); |
293 } | 296 } |
294 | 297 |
295 void EmbeddedWorkerInstance::OnStopped() { | 298 void EmbeddedWorkerInstance::OnStopped() { |
296 if (worker_devtools_agent_route_id_ != MSG_ROUTING_NONE) | 299 if (worker_devtools_agent_route_id_ != MSG_ROUTING_NONE) |
297 NotifyWorkerDestroyed(process_id_, worker_devtools_agent_route_id_); | 300 NotifyWorkerDestroyed(process_id_, worker_devtools_agent_route_id_); |
298 if (context_) | 301 if (context_) |
299 context_->process_manager()->ReleaseWorkerProcess(embedded_worker_id_); | 302 context_->process_manager()->ReleaseWorkerProcess(embedded_worker_id_); |
300 status_ = STOPPED; | 303 status_ = STOPPED; |
301 process_id_ = -1; | 304 process_id_ = -1; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
369 // Sort descending by the reference count. | 372 // Sort descending by the reference count. |
370 std::sort(counted.begin(), counted.end(), SecondGreater()); | 373 std::sort(counted.begin(), counted.end(), SecondGreater()); |
371 | 374 |
372 std::vector<int> result(counted.size()); | 375 std::vector<int> result(counted.size()); |
373 for (size_t i = 0; i < counted.size(); ++i) | 376 for (size_t i = 0; i < counted.size(); ++i) |
374 result[i] = counted[i].first; | 377 result[i] = counted[i].first; |
375 return result; | 378 return result; |
376 } | 379 } |
377 | 380 |
378 } // namespace content | 381 } // namespace content |
OLD | NEW |