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 <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
13 #include "base/threading/non_thread_safe.h" | 13 #include "base/threading/non_thread_safe.h" |
14 #include "base/trace_event/trace_event.h" | 14 #include "base/trace_event/trace_event.h" |
15 #include "content/browser/devtools/service_worker_devtools_manager.h" | 15 #include "content/browser/devtools/service_worker_devtools_manager.h" |
16 #include "content/browser/service_worker/embedded_worker_registry.h" | 16 #include "content/browser/service_worker/embedded_worker_registry.h" |
17 #include "content/browser/service_worker/embedded_worker_status.h" | |
17 #include "content/browser/service_worker/service_worker_context_core.h" | 18 #include "content/browser/service_worker/service_worker_context_core.h" |
18 #include "content/common/content_switches_internal.h" | 19 #include "content/common/content_switches_internal.h" |
19 #include "content/common/mojo/service_registry_impl.h" | 20 #include "content/common/mojo/service_registry_impl.h" |
20 #include "content/common/service_worker/embedded_worker_messages.h" | 21 #include "content/common/service_worker/embedded_worker_messages.h" |
21 #include "content/common/service_worker/embedded_worker_settings.h" | 22 #include "content/common/service_worker/embedded_worker_settings.h" |
22 #include "content/common/service_worker/embedded_worker_setup.mojom.h" | 23 #include "content/common/service_worker/embedded_worker_setup.mojom.h" |
23 #include "content/common/service_worker/service_worker_types.h" | 24 #include "content/common/service_worker/service_worker_types.h" |
24 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
25 #include "content/public/browser/content_browser_client.h" | 26 #include "content/public/browser/content_browser_client.h" |
26 #include "content/public/browser/render_process_host.h" | 27 #include "content/public/browser/render_process_host.h" |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
209 // We can abort starting worker by destroying this task anytime during the | 210 // We can abort starting worker by destroying this task anytime during the |
210 // sequence. | 211 // sequence. |
211 class EmbeddedWorkerInstance::StartTask { | 212 class EmbeddedWorkerInstance::StartTask { |
212 public: | 213 public: |
213 enum class ProcessAllocationState { NOT_ALLOCATED, ALLOCATING, ALLOCATED }; | 214 enum class ProcessAllocationState { NOT_ALLOCATED, ALLOCATING, ALLOCATED }; |
214 | 215 |
215 StartTask(EmbeddedWorkerInstance* instance, const GURL& script_url) | 216 StartTask(EmbeddedWorkerInstance* instance, const GURL& script_url) |
216 : instance_(instance), | 217 : instance_(instance), |
217 state_(ProcessAllocationState::NOT_ALLOCATED), | 218 state_(ProcessAllocationState::NOT_ALLOCATED), |
218 is_installed_(false), | 219 is_installed_(false), |
219 start_situation_(ServiceWorkerMetrics::StartSituation::UNKNOWN), | 220 started_during_browser_startup_(false), |
220 weak_factory_(this) { | 221 weak_factory_(this) { |
221 TRACE_EVENT_ASYNC_BEGIN1("ServiceWorker", "EmbeddedWorkerInstance::Start", | 222 TRACE_EVENT_ASYNC_BEGIN1("ServiceWorker", "EmbeddedWorkerInstance::Start", |
222 this, "Script", script_url.spec()); | 223 this, "Script", script_url.spec()); |
223 } | 224 } |
224 | 225 |
225 ~StartTask() { | 226 ~StartTask() { |
226 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 227 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
227 TRACE_EVENT_ASYNC_END0("ServiceWorker", "EmbeddedWorkerInstance::Start", | 228 TRACE_EVENT_ASYNC_END0("ServiceWorker", "EmbeddedWorkerInstance::Start", |
228 this); | 229 this); |
229 | 230 |
(...skipping 26 matching lines...) Expand all Loading... | |
256 } | 257 } |
257 | 258 |
258 void Start(std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, | 259 void Start(std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, |
259 const StatusCallback& callback) { | 260 const StatusCallback& callback) { |
260 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 261 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
261 state_ = ProcessAllocationState::ALLOCATING; | 262 state_ = ProcessAllocationState::ALLOCATING; |
262 start_callback_ = callback; | 263 start_callback_ = callback; |
263 is_installed_ = params->is_installed; | 264 is_installed_ = params->is_installed; |
264 | 265 |
265 if (!GetContentClient()->browser()->IsBrowserStartupComplete()) | 266 if (!GetContentClient()->browser()->IsBrowserStartupComplete()) |
266 start_situation_ = ServiceWorkerMetrics::StartSituation::DURING_STARTUP; | 267 started_during_browser_startup_ = true; |
267 | 268 |
268 GURL scope(params->scope); | 269 GURL scope(params->scope); |
269 GURL script_url(params->script_url); | 270 GURL script_url(params->script_url); |
270 | 271 |
271 bool can_use_existing_process = | 272 bool can_use_existing_process = |
272 instance_->context_->GetVersionFailureCount( | 273 instance_->context_->GetVersionFailureCount( |
273 params->service_worker_version_id) < kMaxSameProcessFailureCount; | 274 params->service_worker_version_id) < kMaxSameProcessFailureCount; |
274 instance_->context_->process_manager()->AllocateWorkerProcess( | 275 instance_->context_->process_manager()->AllocateWorkerProcess( |
275 instance_->embedded_worker_id_, scope, script_url, | 276 instance_->embedded_worker_id_, scope, script_url, |
276 can_use_existing_process, | 277 can_use_existing_process, |
277 base::Bind(&StartTask::OnProcessAllocated, weak_factory_.GetWeakPtr(), | 278 base::Bind(&StartTask::OnProcessAllocated, weak_factory_.GetWeakPtr(), |
278 base::Passed(¶ms))); | 279 base::Passed(¶ms))); |
279 } | 280 } |
280 | 281 |
281 static void RunStartCallback(StartTask* task, | 282 static void RunStartCallback(StartTask* task, |
282 ServiceWorkerStatusCode status) { | 283 ServiceWorkerStatusCode status) { |
283 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 284 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
284 StatusCallback callback = task->start_callback_; | 285 StatusCallback callback = task->start_callback_; |
285 task->start_callback_.Reset(); | 286 task->start_callback_.Reset(); |
286 callback.Run(status); | 287 callback.Run(status); |
287 // |task| may be destroyed. | 288 // |task| may be destroyed. |
288 } | 289 } |
289 | 290 |
290 bool is_installed() const { return is_installed_; } | 291 bool is_installed() const { return is_installed_; } |
291 ServiceWorkerMetrics::StartSituation start_situation() const { | |
292 return start_situation_; | |
293 } | |
294 | 292 |
295 private: | 293 private: |
296 void OnProcessAllocated( | 294 void OnProcessAllocated( |
297 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, | 295 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, |
298 ServiceWorkerStatusCode status, | 296 ServiceWorkerStatusCode status, |
299 int process_id, | 297 int process_id, |
300 bool is_new_process, | 298 bool is_new_process, |
301 const EmbeddedWorkerSettings& settings) { | 299 const EmbeddedWorkerSettings& settings) { |
302 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 300 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
303 | 301 |
304 if (status != SERVICE_WORKER_OK) { | 302 if (status != SERVICE_WORKER_OK) { |
305 TRACE_EVENT_ASYNC_STEP_PAST1( | 303 TRACE_EVENT_ASYNC_STEP_PAST1( |
306 "ServiceWorker", "EmbeddedWorkerInstance::Start", this, | 304 "ServiceWorker", "EmbeddedWorkerInstance::Start", this, |
307 "OnProcessAllocated", "Error", ServiceWorkerStatusToString(status)); | 305 "OnProcessAllocated", "Error", ServiceWorkerStatusToString(status)); |
308 DCHECK_EQ(ChildProcessHost::kInvalidUniqueID, process_id); | 306 DCHECK_EQ(ChildProcessHost::kInvalidUniqueID, process_id); |
309 StatusCallback callback = start_callback_; | 307 StatusCallback callback = start_callback_; |
310 start_callback_.Reset(); | 308 start_callback_.Reset(); |
311 instance_->OnStartFailed(callback, status); | 309 instance_->OnStartFailed(callback, status); |
312 // |this| may be destroyed. | 310 // |this| may be destroyed. |
313 return; | 311 return; |
314 } | 312 } |
315 | 313 |
316 TRACE_EVENT_ASYNC_STEP_PAST1( | 314 TRACE_EVENT_ASYNC_STEP_PAST1( |
317 "ServiceWorker", "EmbeddedWorkerInstance::Start", this, | 315 "ServiceWorker", "EmbeddedWorkerInstance::Start", this, |
318 "OnProcessAllocated", "Is New Process", is_new_process); | 316 "OnProcessAllocated", "Is New Process", is_new_process); |
319 if (is_installed_) | 317 if (is_installed_) |
320 ServiceWorkerMetrics::RecordProcessCreated(is_new_process); | 318 ServiceWorkerMetrics::RecordProcessCreated(is_new_process); |
321 | 319 |
322 if (start_situation_ == ServiceWorkerMetrics::StartSituation::UNKNOWN) { | 320 ServiceWorkerMetrics::StartSituation start_situation = |
323 if (is_new_process) | 321 ServiceWorkerMetrics::StartSituation::UNKNOWN; |
324 start_situation_ = ServiceWorkerMetrics::StartSituation::NEW_PROCESS; | 322 if (started_during_browser_startup_) |
325 else | 323 start_situation = ServiceWorkerMetrics::StartSituation::DURING_STARTUP; |
326 start_situation_ = | 324 else if (is_new_process) |
327 ServiceWorkerMetrics::StartSituation::EXISTING_PROCESS; | 325 start_situation = ServiceWorkerMetrics::StartSituation::NEW_PROCESS; |
328 } | 326 else |
327 start_situation = ServiceWorkerMetrics::StartSituation::EXISTING_PROCESS; | |
329 | 328 |
330 // Notify the instance that a process is allocated. | 329 // Notify the instance that a process is allocated. |
331 state_ = ProcessAllocationState::ALLOCATED; | 330 state_ = ProcessAllocationState::ALLOCATED; |
332 instance_->OnProcessAllocated(base::WrapUnique(new WorkerProcessHandle( | 331 instance_->OnProcessAllocated( |
333 instance_->context_, instance_->embedded_worker_id(), process_id, | 332 base::WrapUnique(new WorkerProcessHandle( |
334 is_new_process))); | 333 instance_->context_, instance_->embedded_worker_id(), process_id, |
334 is_new_process)), | |
335 start_situation); | |
335 | 336 |
336 // TODO(bengr): Support changes to this setting while the worker | 337 // TODO(bengr): Support changes to this setting while the worker |
337 // is running. | 338 // is running. |
338 params->settings.data_saver_enabled = settings.data_saver_enabled; | 339 params->settings.data_saver_enabled = settings.data_saver_enabled; |
339 | 340 |
340 // Register the instance to DevToolsManager on UI thread. | 341 // Register the instance to DevToolsManager on UI thread. |
341 const int64_t service_worker_version_id = params->service_worker_version_id; | 342 const int64_t service_worker_version_id = params->service_worker_version_id; |
342 const GURL& scope = params->scope; | 343 const GURL& scope = params->scope; |
343 GURL script_url(params->script_url); | 344 GURL script_url(params->script_url); |
344 BrowserThread::PostTask( | 345 BrowserThread::PostTask( |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
392 } | 393 } |
393 | 394 |
394 // |instance_| must outlive |this|. | 395 // |instance_| must outlive |this|. |
395 EmbeddedWorkerInstance* instance_; | 396 EmbeddedWorkerInstance* instance_; |
396 | 397 |
397 StatusCallback start_callback_; | 398 StatusCallback start_callback_; |
398 ProcessAllocationState state_; | 399 ProcessAllocationState state_; |
399 | 400 |
400 // Used for UMA. | 401 // Used for UMA. |
401 bool is_installed_; | 402 bool is_installed_; |
402 ServiceWorkerMetrics::StartSituation start_situation_; | 403 bool started_during_browser_startup_; |
403 | 404 |
404 base::WeakPtrFactory<StartTask> weak_factory_; | 405 base::WeakPtrFactory<StartTask> weak_factory_; |
405 | 406 |
406 DISALLOW_COPY_AND_ASSIGN(StartTask); | 407 DISALLOW_COPY_AND_ASSIGN(StartTask); |
407 }; | 408 }; |
408 | 409 |
409 bool EmbeddedWorkerInstance::Listener::OnMessageReceived( | 410 bool EmbeddedWorkerInstance::Listener::OnMessageReceived( |
410 const IPC::Message& message) { | 411 const IPC::Message& message) { |
411 return false; | 412 return false; |
412 } | 413 } |
413 | 414 |
414 EmbeddedWorkerInstance::~EmbeddedWorkerInstance() { | 415 EmbeddedWorkerInstance::~EmbeddedWorkerInstance() { |
415 DCHECK(status_ == STOPPING || status_ == STOPPED) << status_; | 416 DCHECK(status_ == EmbeddedWorkerStatus::STOPPING || |
417 status_ == EmbeddedWorkerStatus::STOPPED) | |
418 << static_cast<int>(status_); | |
416 devtools_proxy_.reset(); | 419 devtools_proxy_.reset(); |
417 if (registry_->GetWorker(embedded_worker_id_)) | 420 if (registry_->GetWorker(embedded_worker_id_)) |
418 registry_->RemoveWorker(process_id(), embedded_worker_id_); | 421 registry_->RemoveWorker(process_id(), embedded_worker_id_); |
419 process_handle_.reset(); | 422 process_handle_.reset(); |
420 } | 423 } |
421 | 424 |
422 void EmbeddedWorkerInstance::Start( | 425 void EmbeddedWorkerInstance::Start( |
423 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, | 426 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, |
424 const StatusCallback& callback) { | 427 const StatusCallback& callback) { |
425 if (!context_) { | 428 if (!context_) { |
426 callback.Run(SERVICE_WORKER_ERROR_ABORT); | 429 callback.Run(SERVICE_WORKER_ERROR_ABORT); |
427 // |this| may be destroyed by the callback. | 430 // |this| may be destroyed by the callback. |
428 return; | 431 return; |
429 } | 432 } |
430 DCHECK(status_ == STOPPED); | 433 DCHECK(status_ == EmbeddedWorkerStatus::STOPPED); |
431 | 434 |
432 DCHECK(!params->pause_after_download || !params->is_installed); | 435 DCHECK(!params->pause_after_download || !params->is_installed); |
433 DCHECK_NE(kInvalidServiceWorkerVersionId, params->service_worker_version_id); | 436 DCHECK_NE(kInvalidServiceWorkerVersionId, params->service_worker_version_id); |
434 step_time_ = base::TimeTicks::Now(); | 437 step_time_ = base::TimeTicks::Now(); |
435 status_ = STARTING; | 438 status_ = EmbeddedWorkerStatus::STARTING; |
436 starting_phase_ = ALLOCATING_PROCESS; | 439 starting_phase_ = ALLOCATING_PROCESS; |
437 network_accessed_for_script_ = false; | 440 network_accessed_for_script_ = false; |
438 service_registry_.reset(new ServiceRegistryImpl()); | 441 service_registry_.reset(new ServiceRegistryImpl()); |
439 FOR_EACH_OBSERVER(Listener, listener_list_, OnStarting()); | 442 FOR_EACH_OBSERVER(Listener, listener_list_, OnStarting()); |
440 | 443 |
441 params->embedded_worker_id = embedded_worker_id_; | 444 params->embedded_worker_id = embedded_worker_id_; |
442 params->worker_devtools_agent_route_id = MSG_ROUTING_NONE; | 445 params->worker_devtools_agent_route_id = MSG_ROUTING_NONE; |
443 params->wait_for_debugger = false; | 446 params->wait_for_debugger = false; |
444 params->settings.v8_cache_options = GetV8CacheOptions(); | 447 params->settings.v8_cache_options = GetV8CacheOptions(); |
445 | 448 |
446 inflight_start_task_.reset(new StartTask(this, params->script_url)); | 449 inflight_start_task_.reset(new StartTask(this, params->script_url)); |
447 inflight_start_task_->Start(std::move(params), callback); | 450 inflight_start_task_->Start(std::move(params), callback); |
448 } | 451 } |
449 | 452 |
450 ServiceWorkerStatusCode EmbeddedWorkerInstance::Stop() { | 453 ServiceWorkerStatusCode EmbeddedWorkerInstance::Stop() { |
451 DCHECK(status_ == STARTING || status_ == RUNNING) << status_; | 454 DCHECK(status_ == EmbeddedWorkerStatus::STARTING || |
455 status_ == EmbeddedWorkerStatus::RUNNING) | |
456 << static_cast<int>(status_); | |
452 | 457 |
453 // Abort an inflight start task. | 458 // Abort an inflight start task. |
454 inflight_start_task_.reset(); | 459 inflight_start_task_.reset(); |
455 | 460 |
456 ServiceWorkerStatusCode status = | 461 ServiceWorkerStatusCode status = |
457 registry_->StopWorker(process_id(), embedded_worker_id_); | 462 registry_->StopWorker(process_id(), embedded_worker_id_); |
458 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.SendStopWorker.Status", status, | 463 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.SendStopWorker.Status", status, |
459 SERVICE_WORKER_ERROR_MAX_VALUE); | 464 SERVICE_WORKER_ERROR_MAX_VALUE); |
460 // StopWorker could fail if we were starting up and don't have a process yet, | 465 // StopWorker could fail if we were starting up and don't have a process yet, |
461 // or we can no longer communicate with the process. So just detach. | 466 // or we can no longer communicate with the process. So just detach. |
462 if (status != SERVICE_WORKER_OK) { | 467 if (status != SERVICE_WORKER_OK) { |
463 OnDetached(); | 468 OnDetached(); |
464 return status; | 469 return status; |
465 } | 470 } |
466 | 471 |
467 status_ = STOPPING; | 472 status_ = EmbeddedWorkerStatus::STOPPING; |
468 FOR_EACH_OBSERVER(Listener, listener_list_, OnStopping()); | 473 FOR_EACH_OBSERVER(Listener, listener_list_, OnStopping()); |
469 return status; | 474 return status; |
470 } | 475 } |
471 | 476 |
472 void EmbeddedWorkerInstance::StopIfIdle() { | 477 void EmbeddedWorkerInstance::StopIfIdle() { |
473 if (devtools_attached_) { | 478 if (devtools_attached_) { |
474 if (devtools_proxy_) | 479 if (devtools_proxy_) |
475 devtools_proxy_->NotifyWorkerStopIgnored(); | 480 devtools_proxy_->NotifyWorkerStopIgnored(); |
476 return; | 481 return; |
477 } | 482 } |
478 Stop(); | 483 Stop(); |
479 } | 484 } |
480 | 485 |
481 ServiceWorkerStatusCode EmbeddedWorkerInstance::SendMessage( | 486 ServiceWorkerStatusCode EmbeddedWorkerInstance::SendMessage( |
482 const IPC::Message& message) { | 487 const IPC::Message& message) { |
483 DCHECK_NE(kInvalidEmbeddedWorkerThreadId, thread_id_); | 488 DCHECK_NE(kInvalidEmbeddedWorkerThreadId, thread_id_); |
484 if (status_ != RUNNING && status_ != STARTING) | 489 if (status_ != EmbeddedWorkerStatus::RUNNING && |
490 status_ != EmbeddedWorkerStatus::STARTING) { | |
485 return SERVICE_WORKER_ERROR_IPC_FAILED; | 491 return SERVICE_WORKER_ERROR_IPC_FAILED; |
492 } | |
486 return registry_->Send(process_id(), | 493 return registry_->Send(process_id(), |
487 new EmbeddedWorkerContextMsg_MessageToWorker( | 494 new EmbeddedWorkerContextMsg_MessageToWorker( |
488 thread_id_, embedded_worker_id_, message)); | 495 thread_id_, embedded_worker_id_, message)); |
489 } | 496 } |
490 | 497 |
491 void EmbeddedWorkerInstance::ResumeAfterDownload() { | 498 void EmbeddedWorkerInstance::ResumeAfterDownload() { |
492 if (process_id() == ChildProcessHost::kInvalidUniqueID || status_ != STARTING) | 499 if (process_id() == ChildProcessHost::kInvalidUniqueID || |
500 status_ != EmbeddedWorkerStatus::STARTING) { | |
493 return; | 501 return; |
502 } | |
494 registry_->Send(process_id(), new EmbeddedWorkerMsg_ResumeAfterDownload( | 503 registry_->Send(process_id(), new EmbeddedWorkerMsg_ResumeAfterDownload( |
495 embedded_worker_id_)); | 504 embedded_worker_id_)); |
496 } | 505 } |
497 | 506 |
498 ServiceRegistry* EmbeddedWorkerInstance::GetServiceRegistry() { | 507 ServiceRegistry* EmbeddedWorkerInstance::GetServiceRegistry() { |
499 DCHECK(status_ == STARTING || status_ == RUNNING) << status_; | 508 DCHECK(status_ == EmbeddedWorkerStatus::STARTING || |
509 status_ == EmbeddedWorkerStatus::RUNNING) | |
510 << static_cast<int>(status_); | |
500 return service_registry_.get(); | 511 return service_registry_.get(); |
501 } | 512 } |
502 | 513 |
503 EmbeddedWorkerInstance::EmbeddedWorkerInstance( | 514 EmbeddedWorkerInstance::EmbeddedWorkerInstance( |
504 base::WeakPtr<ServiceWorkerContextCore> context, | 515 base::WeakPtr<ServiceWorkerContextCore> context, |
505 int embedded_worker_id) | 516 int embedded_worker_id) |
506 : context_(context), | 517 : context_(context), |
507 registry_(context->embedded_worker_registry()), | 518 registry_(context->embedded_worker_registry()), |
508 embedded_worker_id_(embedded_worker_id), | 519 embedded_worker_id_(embedded_worker_id), |
509 status_(STOPPED), | 520 status_(EmbeddedWorkerStatus::STOPPED), |
510 starting_phase_(NOT_STARTING), | 521 starting_phase_(NOT_STARTING), |
511 thread_id_(kInvalidEmbeddedWorkerThreadId), | 522 thread_id_(kInvalidEmbeddedWorkerThreadId), |
512 devtools_attached_(false), | 523 devtools_attached_(false), |
513 network_accessed_for_script_(false), | 524 network_accessed_for_script_(false), |
514 weak_factory_(this) {} | 525 weak_factory_(this) {} |
515 | 526 |
516 void EmbeddedWorkerInstance::OnProcessAllocated( | 527 void EmbeddedWorkerInstance::OnProcessAllocated( |
517 std::unique_ptr<WorkerProcessHandle> handle) { | 528 std::unique_ptr<WorkerProcessHandle> handle, |
518 DCHECK_EQ(STARTING, status_); | 529 ServiceWorkerMetrics::StartSituation start_situation) { |
530 DCHECK_EQ(EmbeddedWorkerStatus::STARTING, status_); | |
519 DCHECK(!process_handle_); | 531 DCHECK(!process_handle_); |
520 | 532 |
521 process_handle_ = std::move(handle); | 533 process_handle_ = std::move(handle); |
522 starting_phase_ = REGISTERING_TO_DEVTOOLS; | 534 starting_phase_ = REGISTERING_TO_DEVTOOLS; |
535 start_situation_ = start_situation; | |
523 FOR_EACH_OBSERVER(Listener, listener_list_, OnProcessAllocated()); | 536 FOR_EACH_OBSERVER(Listener, listener_list_, OnProcessAllocated()); |
524 } | 537 } |
525 | 538 |
526 void EmbeddedWorkerInstance::OnRegisteredToDevToolsManager( | 539 void EmbeddedWorkerInstance::OnRegisteredToDevToolsManager( |
527 bool is_new_process, | 540 bool is_new_process, |
528 int worker_devtools_agent_route_id, | 541 int worker_devtools_agent_route_id, |
529 bool wait_for_debugger) { | 542 bool wait_for_debugger) { |
530 if (worker_devtools_agent_route_id != MSG_ROUTING_NONE) { | 543 if (worker_devtools_agent_route_id != MSG_ROUTING_NONE) { |
531 DCHECK(!devtools_proxy_); | 544 DCHECK(!devtools_proxy_); |
532 devtools_proxy_.reset( | 545 devtools_proxy_.reset( |
533 new DevToolsProxy(process_id(), worker_devtools_agent_route_id)); | 546 new DevToolsProxy(process_id(), worker_devtools_agent_route_id)); |
534 } | 547 } |
535 if (wait_for_debugger) { | 548 if (wait_for_debugger) { |
536 // We don't measure the start time when wait_for_debugger flag is set. So | 549 // We don't measure the start time when wait_for_debugger flag is set. So |
537 // we set the NULL time here. | 550 // we set the NULL time here. |
538 step_time_ = base::TimeTicks(); | 551 step_time_ = base::TimeTicks(); |
539 } | 552 } |
540 } | 553 } |
541 | 554 |
542 void EmbeddedWorkerInstance::OnStartWorkerMessageSent() { | 555 void EmbeddedWorkerInstance::OnStartWorkerMessageSent() { |
543 if (!step_time_.is_null()) { | 556 if (!step_time_.is_null()) { |
544 base::TimeDelta duration = UpdateStepTime(); | 557 base::TimeDelta duration = UpdateStepTime(); |
545 if (inflight_start_task_->is_installed()) { | 558 if (inflight_start_task_->is_installed()) { |
546 ServiceWorkerMetrics::RecordTimeToSendStartWorker( | 559 ServiceWorkerMetrics::RecordTimeToSendStartWorker(duration, |
547 duration, inflight_start_task_->start_situation()); | 560 start_situation_); |
548 } | 561 } |
549 } | 562 } |
550 | 563 |
551 starting_phase_ = SENT_START_WORKER; | 564 starting_phase_ = SENT_START_WORKER; |
552 FOR_EACH_OBSERVER(Listener, listener_list_, OnStartWorkerMessageSent()); | 565 FOR_EACH_OBSERVER(Listener, listener_list_, OnStartWorkerMessageSent()); |
553 } | 566 } |
554 | 567 |
555 void EmbeddedWorkerInstance::OnReadyForInspection() { | 568 void EmbeddedWorkerInstance::OnReadyForInspection() { |
556 if (devtools_proxy_) | 569 if (devtools_proxy_) |
557 devtools_proxy_->NotifyWorkerReadyForInspection(); | 570 devtools_proxy_->NotifyWorkerReadyForInspection(); |
(...skipping 21 matching lines...) Expand all Loading... | |
579 } else { | 592 } else { |
580 source = LoadSource::HTTP_CACHE; | 593 source = LoadSource::HTTP_CACHE; |
581 } | 594 } |
582 TRACE_EVENT_ASYNC_STEP_PAST1( | 595 TRACE_EVENT_ASYNC_STEP_PAST1( |
583 "ServiceWorker", "EmbeddedWorkerInstance::Start", | 596 "ServiceWorker", "EmbeddedWorkerInstance::Start", |
584 inflight_start_task_.get(), "OnScriptLoaded", "Source", | 597 inflight_start_task_.get(), "OnScriptLoaded", "Source", |
585 ServiceWorkerMetrics::LoadSourceToString(source)); | 598 ServiceWorkerMetrics::LoadSourceToString(source)); |
586 | 599 |
587 if (!step_time_.is_null()) { | 600 if (!step_time_.is_null()) { |
588 base::TimeDelta duration = UpdateStepTime(); | 601 base::TimeDelta duration = UpdateStepTime(); |
589 ServiceWorkerMetrics::RecordTimeToLoad( | 602 ServiceWorkerMetrics::RecordTimeToLoad(duration, source, start_situation_); |
590 duration, source, inflight_start_task_->start_situation()); | |
591 } | 603 } |
592 | 604 |
593 starting_phase_ = SCRIPT_LOADED; | 605 starting_phase_ = SCRIPT_LOADED; |
594 FOR_EACH_OBSERVER(Listener, listener_list_, OnScriptLoaded()); | 606 FOR_EACH_OBSERVER(Listener, listener_list_, OnScriptLoaded()); |
595 // |this| may be destroyed by the callback. | 607 // |this| may be destroyed by the callback. |
596 } | 608 } |
597 | 609 |
598 void EmbeddedWorkerInstance::OnURLJobCreatedForMainScript() { | 610 void EmbeddedWorkerInstance::OnURLJobCreatedForMainScript() { |
599 if (!inflight_start_task_) | 611 if (!inflight_start_task_) |
600 return; | 612 return; |
601 TRACE_EVENT_ASYNC_STEP_PAST0("ServiceWorker", "EmbeddedWorkerInstance::Start", | 613 TRACE_EVENT_ASYNC_STEP_PAST0("ServiceWorker", "EmbeddedWorkerInstance::Start", |
602 inflight_start_task_.get(), "OnURLJobCreated"); | 614 inflight_start_task_.get(), "OnURLJobCreated"); |
603 if (!step_time_.is_null()) { | 615 if (!step_time_.is_null()) { |
604 base::TimeDelta duration = UpdateStepTime(); | 616 base::TimeDelta duration = UpdateStepTime(); |
605 if (inflight_start_task_->is_installed()) | 617 if (inflight_start_task_->is_installed()) |
606 ServiceWorkerMetrics::RecordTimeToURLJob( | 618 ServiceWorkerMetrics::RecordTimeToURLJob(duration, start_situation_); |
607 duration, inflight_start_task_->start_situation()); | |
608 } | 619 } |
609 } | 620 } |
610 | 621 |
611 void EmbeddedWorkerInstance::OnWorkerVersionInstalled() { | 622 void EmbeddedWorkerInstance::OnWorkerVersionInstalled() { |
612 if (devtools_proxy_) | 623 if (devtools_proxy_) |
613 devtools_proxy_->NotifyWorkerVersionInstalled(); | 624 devtools_proxy_->NotifyWorkerVersionInstalled(); |
614 } | 625 } |
615 | 626 |
616 void EmbeddedWorkerInstance::OnWorkerVersionDoomed() { | 627 void EmbeddedWorkerInstance::OnWorkerVersionDoomed() { |
617 if (devtools_proxy_) | 628 if (devtools_proxy_) |
618 devtools_proxy_->NotifyWorkerVersionDoomed(); | 629 devtools_proxy_->NotifyWorkerVersionDoomed(); |
619 } | 630 } |
620 | 631 |
621 void EmbeddedWorkerInstance::OnThreadStarted(int thread_id) { | 632 void EmbeddedWorkerInstance::OnThreadStarted(int thread_id) { |
622 if (!inflight_start_task_) | 633 if (!inflight_start_task_) |
623 return; | 634 return; |
624 TRACE_EVENT_ASYNC_STEP_PAST0("ServiceWorker", "EmbeddedWorkerInstance::Start", | 635 TRACE_EVENT_ASYNC_STEP_PAST0("ServiceWorker", "EmbeddedWorkerInstance::Start", |
625 inflight_start_task_.get(), "OnThreadStarted"); | 636 inflight_start_task_.get(), "OnThreadStarted"); |
626 | 637 |
627 starting_phase_ = THREAD_STARTED; | 638 starting_phase_ = THREAD_STARTED; |
628 if (!step_time_.is_null()) { | 639 if (!step_time_.is_null()) { |
629 base::TimeDelta duration = UpdateStepTime(); | 640 base::TimeDelta duration = UpdateStepTime(); |
630 if (inflight_start_task_->is_installed()) | 641 if (inflight_start_task_->is_installed()) |
631 ServiceWorkerMetrics::RecordTimeToStartThread( | 642 ServiceWorkerMetrics::RecordTimeToStartThread(duration, start_situation_); |
632 duration, inflight_start_task_->start_situation()); | |
633 } | 643 } |
634 | 644 |
635 thread_id_ = thread_id; | 645 thread_id_ = thread_id; |
636 FOR_EACH_OBSERVER(Listener, listener_list_, OnThreadStarted()); | 646 FOR_EACH_OBSERVER(Listener, listener_list_, OnThreadStarted()); |
637 | 647 |
638 shell::mojom::InterfaceProviderPtr exposed_services; | 648 shell::mojom::InterfaceProviderPtr exposed_services; |
639 service_registry_->Bind(GetProxy(&exposed_services)); | 649 service_registry_->Bind(GetProxy(&exposed_services)); |
640 shell::mojom::InterfaceProviderPtr services; | 650 shell::mojom::InterfaceProviderPtr services; |
641 shell::mojom::InterfaceProviderRequest services_request = GetProxy(&services); | 651 shell::mojom::InterfaceProviderRequest services_request = GetProxy(&services); |
642 BrowserThread::PostTask( | 652 BrowserThread::PostTask( |
643 BrowserThread::UI, FROM_HERE, | 653 BrowserThread::UI, FROM_HERE, |
644 base::Bind(SetupMojoOnUIThread, process_id(), thread_id_, | 654 base::Bind(SetupMojoOnUIThread, process_id(), thread_id_, |
645 base::Passed(&services_request), | 655 base::Passed(&services_request), |
646 base::Passed(exposed_services.PassInterface()))); | 656 base::Passed(exposed_services.PassInterface()))); |
647 service_registry_->BindRemoteServiceProvider(std::move(services)); | 657 service_registry_->BindRemoteServiceProvider(std::move(services)); |
648 } | 658 } |
649 | 659 |
650 void EmbeddedWorkerInstance::OnScriptLoadFailed() { | 660 void EmbeddedWorkerInstance::OnScriptLoadFailed() { |
651 if (!inflight_start_task_) | 661 if (!inflight_start_task_) |
652 return; | 662 return; |
653 TRACE_EVENT_ASYNC_STEP_PAST0("ServiceWorker", "EmbeddedWorkerInstance::Start", | 663 TRACE_EVENT_ASYNC_STEP_PAST0("ServiceWorker", "EmbeddedWorkerInstance::Start", |
654 inflight_start_task_.get(), | 664 inflight_start_task_.get(), |
655 "OnScriptLoadFailed"); | 665 "OnScriptLoadFailed"); |
656 FOR_EACH_OBSERVER(Listener, listener_list_, OnScriptLoadFailed()); | 666 FOR_EACH_OBSERVER(Listener, listener_list_, OnScriptLoadFailed()); |
657 } | 667 } |
658 | 668 |
659 void EmbeddedWorkerInstance::OnScriptEvaluated(bool success) { | 669 void EmbeddedWorkerInstance::OnScriptEvaluated(bool success) { |
660 if (!inflight_start_task_) | 670 if (!inflight_start_task_) |
661 return; | 671 return; |
662 DCHECK_EQ(STARTING, status_); | 672 DCHECK_EQ(EmbeddedWorkerStatus::STARTING, status_); |
663 | 673 |
664 TRACE_EVENT_ASYNC_STEP_PAST1("ServiceWorker", "EmbeddedWorkerInstance::Start", | 674 TRACE_EVENT_ASYNC_STEP_PAST1("ServiceWorker", "EmbeddedWorkerInstance::Start", |
665 inflight_start_task_.get(), "OnScriptEvaluated", | 675 inflight_start_task_.get(), "OnScriptEvaluated", |
666 "Success", success); | 676 "Success", success); |
667 starting_phase_ = SCRIPT_EVALUATED; | 677 starting_phase_ = SCRIPT_EVALUATED; |
668 if (!step_time_.is_null()) { | 678 if (!step_time_.is_null()) { |
669 base::TimeDelta duration = UpdateStepTime(); | 679 base::TimeDelta duration = UpdateStepTime(); |
670 if (success && inflight_start_task_->is_installed()) | 680 if (success && inflight_start_task_->is_installed()) |
671 ServiceWorkerMetrics::RecordTimeToEvaluateScript( | 681 ServiceWorkerMetrics::RecordTimeToEvaluateScript(duration, |
672 duration, inflight_start_task_->start_situation()); | 682 start_situation_); |
673 } | 683 } |
674 | 684 |
675 base::WeakPtr<EmbeddedWorkerInstance> weak_this = weak_factory_.GetWeakPtr(); | 685 base::WeakPtr<EmbeddedWorkerInstance> weak_this = weak_factory_.GetWeakPtr(); |
676 StartTask::RunStartCallback( | 686 StartTask::RunStartCallback( |
677 inflight_start_task_.get(), | 687 inflight_start_task_.get(), |
678 success ? SERVICE_WORKER_OK | 688 success ? SERVICE_WORKER_OK |
679 : SERVICE_WORKER_ERROR_SCRIPT_EVALUATE_FAILED); | 689 : SERVICE_WORKER_ERROR_SCRIPT_EVALUATE_FAILED); |
680 // |this| may be destroyed by the callback. | 690 // |this| may be destroyed by the callback. |
681 } | 691 } |
682 | 692 |
683 void EmbeddedWorkerInstance::OnStarted() { | 693 void EmbeddedWorkerInstance::OnStarted() { |
684 // Stop is requested before OnStarted is sent back from the worker. | 694 // Stop is requested before OnStarted is sent back from the worker. |
685 if (status_ == STOPPING) | 695 if (status_ == EmbeddedWorkerStatus::STOPPING) |
686 return; | 696 return; |
687 DCHECK(status_ == STARTING); | 697 DCHECK(status_ == EmbeddedWorkerStatus::STARTING); |
688 status_ = RUNNING; | 698 status_ = EmbeddedWorkerStatus::RUNNING; |
689 inflight_start_task_.reset(); | 699 inflight_start_task_.reset(); |
690 FOR_EACH_OBSERVER(Listener, listener_list_, OnStarted()); | 700 FOR_EACH_OBSERVER(Listener, listener_list_, OnStarted()); |
691 } | 701 } |
692 | 702 |
693 void EmbeddedWorkerInstance::OnStopped() { | 703 void EmbeddedWorkerInstance::OnStopped() { |
694 Status old_status = status_; | 704 EmbeddedWorkerStatus old_status = status_; |
695 ReleaseProcess(); | 705 ReleaseProcess(); |
696 FOR_EACH_OBSERVER(Listener, listener_list_, OnStopped(old_status)); | 706 FOR_EACH_OBSERVER(Listener, listener_list_, OnStopped(old_status)); |
697 } | 707 } |
698 | 708 |
699 void EmbeddedWorkerInstance::OnDetached() { | 709 void EmbeddedWorkerInstance::OnDetached() { |
700 Status old_status = status_; | 710 EmbeddedWorkerStatus old_status = status_; |
701 ReleaseProcess(); | 711 ReleaseProcess(); |
702 FOR_EACH_OBSERVER(Listener, listener_list_, OnDetached(old_status)); | 712 FOR_EACH_OBSERVER(Listener, listener_list_, OnDetached(old_status)); |
703 } | 713 } |
704 | 714 |
705 void EmbeddedWorkerInstance::Detach() { | 715 void EmbeddedWorkerInstance::Detach() { |
706 registry_->RemoveWorker(process_id(), embedded_worker_id_); | 716 registry_->RemoveWorker(process_id(), embedded_worker_id_); |
707 OnDetached(); | 717 OnDetached(); |
708 } | 718 } |
709 | 719 |
710 base::WeakPtr<EmbeddedWorkerInstance> EmbeddedWorkerInstance::AsWeakPtr() { | 720 base::WeakPtr<EmbeddedWorkerInstance> EmbeddedWorkerInstance::AsWeakPtr() { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
778 starting_phase_ = SCRIPT_DOWNLOADING; | 788 starting_phase_ = SCRIPT_DOWNLOADING; |
779 network_accessed_for_script_ = true; | 789 network_accessed_for_script_ = true; |
780 } | 790 } |
781 | 791 |
782 void EmbeddedWorkerInstance::ReleaseProcess() { | 792 void EmbeddedWorkerInstance::ReleaseProcess() { |
783 // Abort an inflight start task. | 793 // Abort an inflight start task. |
784 inflight_start_task_.reset(); | 794 inflight_start_task_.reset(); |
785 | 795 |
786 devtools_proxy_.reset(); | 796 devtools_proxy_.reset(); |
787 process_handle_.reset(); | 797 process_handle_.reset(); |
788 status_ = STOPPED; | 798 status_ = EmbeddedWorkerStatus::STOPPED; |
789 thread_id_ = kInvalidEmbeddedWorkerThreadId; | 799 thread_id_ = kInvalidEmbeddedWorkerThreadId; |
790 service_registry_.reset(); | 800 service_registry_.reset(); |
791 } | 801 } |
792 | 802 |
793 void EmbeddedWorkerInstance::OnStartFailed(const StatusCallback& callback, | 803 void EmbeddedWorkerInstance::OnStartFailed(const StatusCallback& callback, |
794 ServiceWorkerStatusCode status) { | 804 ServiceWorkerStatusCode status) { |
795 Status old_status = status_; | 805 EmbeddedWorkerStatus old_status = status_; |
796 ReleaseProcess(); | 806 ReleaseProcess(); |
797 base::WeakPtr<EmbeddedWorkerInstance> weak_this = weak_factory_.GetWeakPtr(); | 807 base::WeakPtr<EmbeddedWorkerInstance> weak_this = weak_factory_.GetWeakPtr(); |
798 callback.Run(status); | 808 callback.Run(status); |
799 if (weak_this && old_status != STOPPED) | 809 if (weak_this && old_status != EmbeddedWorkerStatus::STOPPED) |
800 FOR_EACH_OBSERVER(Listener, weak_this->listener_list_, | 810 FOR_EACH_OBSERVER(Listener, weak_this->listener_list_, |
801 OnStopped(old_status)); | 811 OnStopped(old_status)); |
802 } | 812 } |
803 | 813 |
804 base::TimeDelta EmbeddedWorkerInstance::UpdateStepTime() { | 814 base::TimeDelta EmbeddedWorkerInstance::UpdateStepTime() { |
805 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 815 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
806 DCHECK(!step_time_.is_null()); | 816 DCHECK(!step_time_.is_null()); |
807 base::TimeTicks now = base::TimeTicks::Now(); | 817 base::TimeTicks now = base::TimeTicks::Now(); |
808 base::TimeDelta duration = now - step_time_; | 818 base::TimeDelta duration = now - step_time_; |
809 step_time_ = now; | 819 step_time_ = now; |
810 return duration; | 820 return duration; |
811 } | 821 } |
812 | 822 |
813 void EmbeddedWorkerInstance::AddMessageToConsole(ConsoleMessageLevel level, | 823 void EmbeddedWorkerInstance::AddMessageToConsole(ConsoleMessageLevel level, |
814 const std::string& message) { | 824 const std::string& message) { |
815 if (status_ != RUNNING && status_ != STARTING) | 825 if (status_ != EmbeddedWorkerStatus::RUNNING && |
826 status_ != EmbeddedWorkerStatus::STARTING) { | |
816 return; | 827 return; |
828 } | |
817 registry_->Send(process_id(), new EmbeddedWorkerMsg_AddMessageToConsole( | 829 registry_->Send(process_id(), new EmbeddedWorkerMsg_AddMessageToConsole( |
818 embedded_worker_id_, level, message)); | 830 embedded_worker_id_, level, message)); |
819 } | 831 } |
820 | 832 |
821 // static | 833 // static |
822 std::string EmbeddedWorkerInstance::StatusToString(Status status) { | 834 std::string EmbeddedWorkerInstance::StatusToString( |
835 EmbeddedWorkerStatus status) { | |
823 switch (status) { | 836 switch (status) { |
824 case STOPPED: | 837 case EmbeddedWorkerStatus::STOPPED: |
825 return "STOPPED"; | 838 return "STOPPED"; |
826 case STARTING: | 839 case EmbeddedWorkerStatus::STARTING: |
827 return "STARTING"; | 840 return "STARTING"; |
828 case RUNNING: | 841 case EmbeddedWorkerStatus::RUNNING: |
829 return "RUNNING"; | 842 return "RUNNING"; |
830 case STOPPING: | 843 case EmbeddedWorkerStatus::STOPPING: |
831 return "STOPPING"; | 844 return "STOPPING"; |
832 } | 845 } |
833 NOTREACHED() << status; | 846 NOTREACHED(); |
falken
2016/06/07 06:20:30
curious: why remove the status output?
horo
2016/06/07 06:58:48
Done.
| |
834 return std::string(); | 847 return std::string(); |
835 } | 848 } |
836 | 849 |
837 // static | 850 // static |
838 std::string EmbeddedWorkerInstance::StartingPhaseToString(StartingPhase phase) { | 851 std::string EmbeddedWorkerInstance::StartingPhaseToString(StartingPhase phase) { |
839 switch (phase) { | 852 switch (phase) { |
840 case NOT_STARTING: | 853 case NOT_STARTING: |
841 return "Not in STARTING status"; | 854 return "Not in STARTING status"; |
842 case ALLOCATING_PROCESS: | 855 case ALLOCATING_PROCESS: |
843 return "Allocating process"; | 856 return "Allocating process"; |
(...skipping 14 matching lines...) Expand all Loading... | |
858 case SCRIPT_READ_FINISHED: | 871 case SCRIPT_READ_FINISHED: |
859 return "Script read finished"; | 872 return "Script read finished"; |
860 case STARTING_PHASE_MAX_VALUE: | 873 case STARTING_PHASE_MAX_VALUE: |
861 NOTREACHED(); | 874 NOTREACHED(); |
862 } | 875 } |
863 NOTREACHED() << phase; | 876 NOTREACHED() << phase; |
864 return std::string(); | 877 return std::string(); |
865 } | 878 } |
866 | 879 |
867 } // namespace content | 880 } // namespace content |
OLD | NEW |