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/child/service_worker/service_worker_dispatcher.h" | 5 #include "content/child/service_worker/service_worker_dispatcher.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "base/threading/thread_local.h" | 9 #include "base/threading/thread_local.h" |
10 #include "base/trace_event/trace_event.h" | 10 #include "base/trace_event/trace_event.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 OnAssociateRegistrationWithServiceWorker) | 61 OnAssociateRegistrationWithServiceWorker) |
62 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_AssociateRegistration, | 62 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_AssociateRegistration, |
63 OnAssociateRegistration) | 63 OnAssociateRegistration) |
64 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DisassociateRegistration, | 64 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DisassociateRegistration, |
65 OnDisassociateRegistration) | 65 OnDisassociateRegistration) |
66 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerRegistered, OnRegistered) | 66 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerRegistered, OnRegistered) |
67 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerUnregistered, | 67 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerUnregistered, |
68 OnUnregistered) | 68 OnUnregistered) |
69 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetRegistration, | 69 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetRegistration, |
70 OnDidGetRegistration) | 70 OnDidGetRegistration) |
| 71 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetRegistrationForReady, |
| 72 OnDidGetRegistrationForReady) |
71 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerRegistrationError, | 73 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerRegistrationError, |
72 OnRegistrationError) | 74 OnRegistrationError) |
73 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerUnregistrationError, | 75 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerUnregistrationError, |
74 OnUnregistrationError) | 76 OnUnregistrationError) |
75 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerGetRegistrationError, | 77 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerGetRegistrationError, |
76 OnGetRegistrationError) | 78 OnGetRegistrationError) |
77 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerStateChanged, | 79 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ServiceWorkerStateChanged, |
78 OnServiceWorkerStateChanged) | 80 OnServiceWorkerStateChanged) |
79 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_SetVersionAttributes, | 81 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_SetVersionAttributes, |
80 OnSetVersionAttributes) | 82 OnSetVersionAttributes) |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 | 172 |
171 int request_id = pending_get_registration_callbacks_.Add(callbacks); | 173 int request_id = pending_get_registration_callbacks_.Add(callbacks); |
172 TRACE_EVENT_ASYNC_BEGIN1("ServiceWorker", | 174 TRACE_EVENT_ASYNC_BEGIN1("ServiceWorker", |
173 "ServiceWorkerDispatcher::GetRegistration", | 175 "ServiceWorkerDispatcher::GetRegistration", |
174 request_id, | 176 request_id, |
175 "Document URL", document_url.spec()); | 177 "Document URL", document_url.spec()); |
176 thread_safe_sender_->Send(new ServiceWorkerHostMsg_GetRegistration( | 178 thread_safe_sender_->Send(new ServiceWorkerHostMsg_GetRegistration( |
177 CurrentWorkerId(), request_id, provider_id, document_url)); | 179 CurrentWorkerId(), request_id, provider_id, document_url)); |
178 } | 180 } |
179 | 181 |
| 182 void ServiceWorkerDispatcher::GetRegistrationForReady( |
| 183 int provider_id, |
| 184 WebServiceWorkerGetRegistrationForReadyCallbacks* callbacks) { |
| 185 int request_id = get_for_ready_callbacks_.Add(callbacks); |
| 186 TRACE_EVENT_ASYNC_BEGIN0("ServiceWorker", |
| 187 "ServiceWorkerDispatcher::GetRegistrationForReady", |
| 188 request_id); |
| 189 thread_safe_sender_->Send(new ServiceWorkerHostMsg_GetRegistrationForReady( |
| 190 CurrentWorkerId(), request_id, provider_id)); |
| 191 } |
| 192 |
180 void ServiceWorkerDispatcher::AddProviderContext( | 193 void ServiceWorkerDispatcher::AddProviderContext( |
181 ServiceWorkerProviderContext* provider_context) { | 194 ServiceWorkerProviderContext* provider_context) { |
182 DCHECK(provider_context); | 195 DCHECK(provider_context); |
183 int provider_id = provider_context->provider_id(); | 196 int provider_id = provider_context->provider_id(); |
184 DCHECK(!ContainsKey(provider_contexts_, provider_id)); | 197 DCHECK(!ContainsKey(provider_contexts_, provider_id)); |
185 provider_contexts_[provider_id] = provider_context; | 198 provider_contexts_[provider_id] = provider_context; |
186 } | 199 } |
187 | 200 |
188 void ServiceWorkerDispatcher::RemoveProviderContext( | 201 void ServiceWorkerDispatcher::RemoveProviderContext( |
189 ServiceWorkerProviderContext* provider_context) { | 202 ServiceWorkerProviderContext* provider_context) { |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 return; | 426 return; |
414 | 427 |
415 WebServiceWorkerRegistrationImpl* registration = NULL; | 428 WebServiceWorkerRegistrationImpl* registration = NULL; |
416 if (info.handle_id != kInvalidServiceWorkerHandleId) | 429 if (info.handle_id != kInvalidServiceWorkerHandleId) |
417 registration = FindOrCreateRegistration(info, attrs); | 430 registration = FindOrCreateRegistration(info, attrs); |
418 | 431 |
419 callbacks->onSuccess(registration); | 432 callbacks->onSuccess(registration); |
420 pending_get_registration_callbacks_.Remove(request_id); | 433 pending_get_registration_callbacks_.Remove(request_id); |
421 } | 434 } |
422 | 435 |
| 436 void ServiceWorkerDispatcher::OnDidGetRegistrationForReady( |
| 437 int thread_id, |
| 438 int request_id, |
| 439 const ServiceWorkerRegistrationObjectInfo& info, |
| 440 const ServiceWorkerVersionAttributes& attrs) { |
| 441 TRACE_EVENT_ASYNC_STEP_INTO0( |
| 442 "ServiceWorker", |
| 443 "ServiceWorkerDispatcher::GetRegistrationForReady", |
| 444 request_id, |
| 445 "OnDidGetRegistrationForReady"); |
| 446 TRACE_EVENT_ASYNC_END0("ServiceWorker", |
| 447 "ServiceWorkerDispatcher::GetRegistrationForReady", |
| 448 request_id); |
| 449 WebServiceWorkerGetRegistrationForReadyCallbacks* callbacks = |
| 450 get_for_ready_callbacks_.Lookup(request_id); |
| 451 DCHECK(callbacks); |
| 452 if (!callbacks) |
| 453 return; |
| 454 |
| 455 WebServiceWorkerRegistrationImpl* registration = NULL; |
| 456 DCHECK(info.handle_id != kInvalidServiceWorkerHandleId); |
| 457 registration = FindOrCreateRegistration(info, attrs); |
| 458 callbacks->onSuccess(registration); |
| 459 get_for_ready_callbacks_.Remove(request_id); |
| 460 } |
| 461 |
423 void ServiceWorkerDispatcher::OnRegistrationError( | 462 void ServiceWorkerDispatcher::OnRegistrationError( |
424 int thread_id, | 463 int thread_id, |
425 int request_id, | 464 int request_id, |
426 WebServiceWorkerError::ErrorType error_type, | 465 WebServiceWorkerError::ErrorType error_type, |
427 const base::string16& message) { | 466 const base::string16& message) { |
428 TRACE_EVENT_ASYNC_STEP_INTO0("ServiceWorker", | 467 TRACE_EVENT_ASYNC_STEP_INTO0("ServiceWorker", |
429 "ServiceWorkerDispatcher::RegisterServiceWorker", | 468 "ServiceWorkerDispatcher::RegisterServiceWorker", |
430 request_id, | 469 request_id, |
431 "OnRegistrationError"); | 470 "OnRegistrationError"); |
432 TRACE_EVENT_ASYNC_END0("ServiceWorker", | 471 TRACE_EVENT_ASYNC_END0("ServiceWorker", |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
547 registrations_.find(registration_handle_id); | 586 registrations_.find(registration_handle_id); |
548 if (found != registrations_.end()) { | 587 if (found != registrations_.end()) { |
549 // Populate the version fields (eg. .installing) with new worker objects. | 588 // Populate the version fields (eg. .installing) with new worker objects. |
550 if (mask.installing_changed()) | 589 if (mask.installing_changed()) |
551 found->second->SetInstalling(GetServiceWorker(attrs.installing, false)); | 590 found->second->SetInstalling(GetServiceWorker(attrs.installing, false)); |
552 if (mask.waiting_changed()) | 591 if (mask.waiting_changed()) |
553 found->second->SetWaiting(GetServiceWorker(attrs.waiting, false)); | 592 found->second->SetWaiting(GetServiceWorker(attrs.waiting, false)); |
554 if (mask.active_changed()) | 593 if (mask.active_changed()) |
555 found->second->SetActive(GetServiceWorker(attrs.active, false)); | 594 found->second->SetActive(GetServiceWorker(attrs.active, false)); |
556 } | 595 } |
557 | |
558 if (mask.active_changed()) | |
559 SetReadyRegistration(provider_id, registration_handle_id); | |
560 } | 596 } |
561 | 597 |
562 void ServiceWorkerDispatcher::OnUpdateFound( | 598 void ServiceWorkerDispatcher::OnUpdateFound( |
563 int thread_id, | 599 int thread_id, |
564 const ServiceWorkerRegistrationObjectInfo& info) { | 600 const ServiceWorkerRegistrationObjectInfo& info) { |
565 TRACE_EVENT0("ServiceWorker", | 601 TRACE_EVENT0("ServiceWorker", |
566 "ServiceWorkerDispatcher::OnUpdateFound"); | 602 "ServiceWorkerDispatcher::OnUpdateFound"); |
567 RegistrationObjectMap::iterator found = registrations_.find(info.handle_id); | 603 RegistrationObjectMap::iterator found = registrations_.find(info.handle_id); |
568 if (found != registrations_.end()) | 604 if (found != registrations_.end()) |
569 found->second->OnUpdateFound(); | 605 found->second->OnUpdateFound(); |
570 } | 606 } |
571 | 607 |
572 void ServiceWorkerDispatcher::SetReadyRegistration( | |
573 int provider_id, | |
574 int registration_handle_id) { | |
575 ProviderContextMap::iterator provider = provider_contexts_.find(provider_id); | |
576 if (provider == provider_contexts_.end() || | |
577 provider->second->registration_handle_id() != registration_handle_id || | |
578 provider->second->active_handle_id() == kInvalidServiceWorkerHandleId) { | |
579 return; | |
580 } | |
581 | |
582 ProviderClientMap::iterator client = provider_clients_.find(provider_id); | |
583 if (client == provider_clients_.end()) | |
584 return; | |
585 | |
586 ServiceWorkerRegistrationObjectInfo info; | |
587 ServiceWorkerVersionAttributes attrs; | |
588 bool found = | |
589 provider->second->GetRegistrationInfoAndVersionAttributes(&info, &attrs); | |
590 DCHECK(found); | |
591 | |
592 WebServiceWorkerRegistrationImpl* registration = | |
593 FindServiceWorkerRegistration(info, false); | |
594 if (!registration) { | |
595 registration = CreateServiceWorkerRegistration(info, false); | |
596 registration->SetInstalling(GetServiceWorker(attrs.installing, false)); | |
597 registration->SetWaiting(GetServiceWorker(attrs.waiting, false)); | |
598 registration->SetActive(GetServiceWorker(attrs.active, false)); | |
599 } | |
600 | |
601 // Resolve the .ready promise with the registration object. | |
602 client->second->setReadyRegistration(registration); | |
603 } | |
604 | |
605 void ServiceWorkerDispatcher::OnSetControllerServiceWorker( | 608 void ServiceWorkerDispatcher::OnSetControllerServiceWorker( |
606 int thread_id, | 609 int thread_id, |
607 int provider_id, | 610 int provider_id, |
608 const ServiceWorkerObjectInfo& info, | 611 const ServiceWorkerObjectInfo& info, |
609 bool should_notify_controllerchange) { | 612 bool should_notify_controllerchange) { |
610 TRACE_EVENT2("ServiceWorker", | 613 TRACE_EVENT2("ServiceWorker", |
611 "ServiceWorkerDispatcher::OnSetControllerServiceWorker", | 614 "ServiceWorkerDispatcher::OnSetControllerServiceWorker", |
612 "Thread ID", thread_id, | 615 "Thread ID", thread_id, |
613 "Provider ID", provider_id); | 616 "Provider ID", provider_id); |
614 | 617 |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
698 attrs.installing, thread_safe_sender_.get()); | 701 attrs.installing, thread_safe_sender_.get()); |
699 ServiceWorkerHandleReference::Adopt( | 702 ServiceWorkerHandleReference::Adopt( |
700 attrs.waiting, thread_safe_sender_.get()); | 703 attrs.waiting, thread_safe_sender_.get()); |
701 ServiceWorkerHandleReference::Adopt( | 704 ServiceWorkerHandleReference::Adopt( |
702 attrs.active, thread_safe_sender_.get()); | 705 attrs.active, thread_safe_sender_.get()); |
703 } | 706 } |
704 return registration; | 707 return registration; |
705 } | 708 } |
706 | 709 |
707 } // namespace content | 710 } // namespace content |
OLD | NEW |