| 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/single_thread_task_runner.h" | 8 #include "base/single_thread_task_runner.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
| 11 #include "base/threading/thread_local.h" | 11 #include "base/threading/thread_local.h" |
| 12 #include "base/trace_event/trace_event.h" | 12 #include "base/trace_event/trace_event.h" |
| 13 #include "content/child/child_thread_impl.h" | |
| 14 #include "content/child/service_worker/service_worker_handle_reference.h" | 13 #include "content/child/service_worker/service_worker_handle_reference.h" |
| 15 #include "content/child/service_worker/service_worker_provider_context.h" | 14 #include "content/child/service_worker/service_worker_provider_context.h" |
| 16 #include "content/child/service_worker/service_worker_registration_handle_refere
nce.h" | 15 #include "content/child/service_worker/service_worker_registration_handle_refere
nce.h" |
| 17 #include "content/child/service_worker/web_service_worker_impl.h" | 16 #include "content/child/service_worker/web_service_worker_impl.h" |
| 18 #include "content/child/service_worker/web_service_worker_registration_impl.h" | 17 #include "content/child/service_worker/web_service_worker_registration_impl.h" |
| 19 #include "content/child/thread_safe_sender.h" | 18 #include "content/child/thread_safe_sender.h" |
| 20 #include "content/child/webmessageportchannel_impl.h" | 19 #include "content/child/webmessageportchannel_impl.h" |
| 21 #include "content/common/service_worker/service_worker_messages.h" | 20 #include "content/common/service_worker/service_worker_messages.h" |
| 22 #include "content/common/service_worker/service_worker_types.h" | 21 #include "content/common/service_worker/service_worker_types.h" |
| 23 #include "content/public/common/url_utils.h" | 22 #include "content/public/common/url_utils.h" |
| 24 #include "third_party/WebKit/public/platform/WebString.h" | 23 #include "third_party/WebKit/public/platform/WebString.h" |
| 25 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWor
kerClientsInfo.h" | |
| 26 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWor
kerProviderClient.h" | 24 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWor
kerProviderClient.h" |
| 27 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" | |
| 28 | 25 |
| 29 using blink::WebServiceWorkerError; | 26 using blink::WebServiceWorkerError; |
| 30 using blink::WebServiceWorkerProvider; | 27 using blink::WebServiceWorkerProvider; |
| 31 using base::ThreadLocalPointer; | 28 using base::ThreadLocalPointer; |
| 32 | 29 |
| 33 namespace content { | 30 namespace content { |
| 34 | 31 |
| 35 namespace { | 32 namespace { |
| 36 | 33 |
| 37 base::LazyInstance<ThreadLocalPointer<void>>::Leaky g_dispatcher_tls = | 34 base::LazyInstance<ThreadLocalPointer<void>>::Leaky g_dispatcher_tls = |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 return static_cast<ServiceWorkerDispatcher*>( | 256 return static_cast<ServiceWorkerDispatcher*>( |
| 260 g_dispatcher_tls.Pointer()->Get()); | 257 g_dispatcher_tls.Pointer()->Get()); |
| 261 } | 258 } |
| 262 | 259 |
| 263 void ServiceWorkerDispatcher::WillStopCurrentWorkerThread() { | 260 void ServiceWorkerDispatcher::WillStopCurrentWorkerThread() { |
| 264 delete this; | 261 delete this; |
| 265 } | 262 } |
| 266 | 263 |
| 267 scoped_refptr<WebServiceWorkerImpl> | 264 scoped_refptr<WebServiceWorkerImpl> |
| 268 ServiceWorkerDispatcher::GetOrCreateServiceWorker( | 265 ServiceWorkerDispatcher::GetOrCreateServiceWorker( |
| 269 const ServiceWorkerObjectInfo& info) { | 266 scoped_ptr<ServiceWorkerHandleReference> handle_ref) { |
| 270 if (info.handle_id == kInvalidServiceWorkerHandleId) | 267 if (handle_ref->handle_id() == kInvalidServiceWorkerHandleId) |
| 271 return nullptr; | 268 return nullptr; |
| 272 | 269 |
| 273 WorkerObjectMap::iterator found = service_workers_.find(info.handle_id); | 270 WorkerObjectMap::iterator found = |
| 271 service_workers_.find(handle_ref->handle_id()); |
| 274 if (found != service_workers_.end()) | 272 if (found != service_workers_.end()) |
| 275 return found->second; | 273 return found->second; |
| 276 | 274 |
| 277 // WebServiceWorkerImpl constructor calls AddServiceWorker. | |
| 278 return new WebServiceWorkerImpl( | |
| 279 ServiceWorkerHandleReference::Create(info, thread_safe_sender_.get()), | |
| 280 thread_safe_sender_.get()); | |
| 281 } | |
| 282 | |
| 283 scoped_refptr<WebServiceWorkerImpl> | |
| 284 ServiceWorkerDispatcher::GetOrAdoptServiceWorker( | |
| 285 const ServiceWorkerObjectInfo& info) { | |
| 286 if (info.handle_id == kInvalidServiceWorkerHandleId) | |
| 287 return nullptr; | |
| 288 | |
| 289 scoped_ptr<ServiceWorkerHandleReference> handle_ref = | |
| 290 ServiceWorkerHandleReference::Adopt(info, thread_safe_sender_.get()); | |
| 291 | |
| 292 WorkerObjectMap::iterator found = service_workers_.find(info.handle_id); | |
| 293 if (found != service_workers_.end()) | |
| 294 return found->second; | |
| 295 | |
| 296 // WebServiceWorkerImpl constructor calls AddServiceWorker. | 275 // WebServiceWorkerImpl constructor calls AddServiceWorker. |
| 297 return new WebServiceWorkerImpl(handle_ref.Pass(), thread_safe_sender_.get()); | 276 return new WebServiceWorkerImpl(handle_ref.Pass(), thread_safe_sender_.get()); |
| 298 } | 277 } |
| 299 | 278 |
| 300 scoped_refptr<WebServiceWorkerRegistrationImpl> | 279 scoped_refptr<WebServiceWorkerRegistrationImpl> |
| 301 ServiceWorkerDispatcher::GetOrCreateRegistration( | 280 ServiceWorkerDispatcher::GetOrCreateRegistration( |
| 302 const ServiceWorkerRegistrationObjectInfo& info, | 281 const ServiceWorkerRegistrationObjectInfo& info, |
| 303 const ServiceWorkerVersionAttributes& attrs) { | 282 const ServiceWorkerVersionAttributes& attrs) { |
| 304 RegistrationObjectMap::iterator found = registrations_.find(info.handle_id); | 283 RegistrationObjectMap::iterator found = registrations_.find(info.handle_id); |
| 305 if (found != registrations_.end()) | 284 if (found != registrations_.end()) |
| 306 return found->second; | 285 return found->second; |
| 307 | 286 |
| 308 // WebServiceWorkerRegistrationImpl constructor calls | 287 // WebServiceWorkerRegistrationImpl constructor calls |
| 309 // AddServiceWorkerRegistration. | 288 // AddServiceWorkerRegistration. |
| 310 scoped_refptr<WebServiceWorkerRegistrationImpl> registration( | 289 scoped_refptr<WebServiceWorkerRegistrationImpl> registration( |
| 311 new WebServiceWorkerRegistrationImpl( | 290 new WebServiceWorkerRegistrationImpl( |
| 312 ServiceWorkerRegistrationHandleReference::Create( | 291 ServiceWorkerRegistrationHandleReference::Create( |
| 313 info, thread_safe_sender_.get()))); | 292 info, thread_safe_sender_.get()))); |
| 314 | 293 |
| 315 registration->SetInstalling(GetOrCreateServiceWorker(attrs.installing)); | 294 registration->SetInstalling( |
| 316 registration->SetWaiting(GetOrCreateServiceWorker(attrs.waiting)); | 295 GetOrCreateServiceWorker(ServiceWorkerHandleReference::Create( |
| 317 registration->SetActive(GetOrCreateServiceWorker(attrs.active)); | 296 attrs.installing, thread_safe_sender_.get()))); |
| 297 registration->SetWaiting( |
| 298 GetOrCreateServiceWorker(ServiceWorkerHandleReference::Create( |
| 299 attrs.waiting, thread_safe_sender_.get()))); |
| 300 registration->SetActive( |
| 301 GetOrCreateServiceWorker(ServiceWorkerHandleReference::Create( |
| 302 attrs.active, thread_safe_sender_.get()))); |
| 318 return registration.Pass(); | 303 return registration.Pass(); |
| 319 } | 304 } |
| 320 | 305 |
| 321 scoped_refptr<WebServiceWorkerRegistrationImpl> | 306 scoped_refptr<WebServiceWorkerRegistrationImpl> |
| 322 ServiceWorkerDispatcher::GetOrAdoptRegistration( | 307 ServiceWorkerDispatcher::GetOrAdoptRegistration( |
| 323 const ServiceWorkerRegistrationObjectInfo& info, | 308 const ServiceWorkerRegistrationObjectInfo& info, |
| 324 const ServiceWorkerVersionAttributes& attrs) { | 309 const ServiceWorkerVersionAttributes& attrs) { |
| 310 scoped_ptr<ServiceWorkerRegistrationHandleReference> registration_ref = |
| 311 Adopt(info); |
| 312 scoped_ptr<ServiceWorkerHandleReference> installing_ref = |
| 313 Adopt(attrs.installing); |
| 314 scoped_ptr<ServiceWorkerHandleReference> waiting_ref = Adopt(attrs.waiting); |
| 315 scoped_ptr<ServiceWorkerHandleReference> active_ref = Adopt(attrs.active); |
| 316 |
| 325 RegistrationObjectMap::iterator found = registrations_.find(info.handle_id); | 317 RegistrationObjectMap::iterator found = registrations_.find(info.handle_id); |
| 326 if (found != registrations_.end()) { | 318 if (found != registrations_.end()) |
| 327 ServiceWorkerHandleReference::Adopt(attrs.installing, | |
| 328 thread_safe_sender_.get()); | |
| 329 ServiceWorkerHandleReference::Adopt(attrs.waiting, | |
| 330 thread_safe_sender_.get()); | |
| 331 ServiceWorkerHandleReference::Adopt(attrs.active, | |
| 332 thread_safe_sender_.get()); | |
| 333 ServiceWorkerRegistrationHandleReference::Adopt(info, | |
| 334 thread_safe_sender_.get()); | |
| 335 return found->second; | 319 return found->second; |
| 336 } | |
| 337 | 320 |
| 338 // WebServiceWorkerRegistrationImpl constructor calls | 321 // WebServiceWorkerRegistrationImpl constructor calls |
| 339 // AddServiceWorkerRegistration. | 322 // AddServiceWorkerRegistration. |
| 340 scoped_refptr<WebServiceWorkerRegistrationImpl> registration( | 323 scoped_refptr<WebServiceWorkerRegistrationImpl> registration( |
| 341 new WebServiceWorkerRegistrationImpl( | 324 new WebServiceWorkerRegistrationImpl(registration_ref.Pass())); |
| 342 ServiceWorkerRegistrationHandleReference::Adopt( | 325 registration->SetInstalling(GetOrCreateServiceWorker(installing_ref.Pass())); |
| 343 info, thread_safe_sender_.get()))); | 326 registration->SetWaiting(GetOrCreateServiceWorker(waiting_ref.Pass())); |
| 344 | 327 registration->SetActive(GetOrCreateServiceWorker(active_ref.Pass())); |
| 345 registration->SetInstalling(GetOrAdoptServiceWorker(attrs.installing)); | |
| 346 registration->SetWaiting(GetOrAdoptServiceWorker(attrs.waiting)); | |
| 347 registration->SetActive(GetOrAdoptServiceWorker(attrs.active)); | |
| 348 return registration.Pass(); | 328 return registration.Pass(); |
| 349 } | 329 } |
| 350 | 330 |
| 351 void ServiceWorkerDispatcher::OnAssociateRegistration( | 331 void ServiceWorkerDispatcher::OnAssociateRegistration( |
| 352 int thread_id, | 332 int thread_id, |
| 353 int provider_id, | 333 int provider_id, |
| 354 const ServiceWorkerRegistrationObjectInfo& info, | 334 const ServiceWorkerRegistrationObjectInfo& info, |
| 355 const ServiceWorkerVersionAttributes& attrs) { | 335 const ServiceWorkerVersionAttributes& attrs) { |
| 356 // Adopt the references sent from the browser process and pass them to the | 336 // Adopt the references sent from the browser process and pass them to the |
| 357 // provider context if it exists. | 337 // provider context if it exists. |
| (...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 656 | 636 |
| 657 void ServiceWorkerDispatcher::OnSetVersionAttributes( | 637 void ServiceWorkerDispatcher::OnSetVersionAttributes( |
| 658 int thread_id, | 638 int thread_id, |
| 659 int registration_handle_id, | 639 int registration_handle_id, |
| 660 int changed_mask, | 640 int changed_mask, |
| 661 const ServiceWorkerVersionAttributes& attrs) { | 641 const ServiceWorkerVersionAttributes& attrs) { |
| 662 TRACE_EVENT1("ServiceWorker", | 642 TRACE_EVENT1("ServiceWorker", |
| 663 "ServiceWorkerDispatcher::OnSetVersionAttributes", | 643 "ServiceWorkerDispatcher::OnSetVersionAttributes", |
| 664 "Thread ID", thread_id); | 644 "Thread ID", thread_id); |
| 665 | 645 |
| 646 // Adopt the references sent from the browser process and pass it to the |
| 647 // registration if it exists. |
| 648 scoped_ptr<ServiceWorkerHandleReference> installing = Adopt(attrs.installing); |
| 649 scoped_ptr<ServiceWorkerHandleReference> waiting = Adopt(attrs.waiting); |
| 650 scoped_ptr<ServiceWorkerHandleReference> active = Adopt(attrs.active); |
| 651 |
| 666 RegistrationObjectMap::iterator found = | 652 RegistrationObjectMap::iterator found = |
| 667 registrations_.find(registration_handle_id); | 653 registrations_.find(registration_handle_id); |
| 668 if (found != registrations_.end()) { | 654 if (found != registrations_.end()) { |
| 669 // Populate the version fields (eg. .installing) with new worker objects. | 655 // Populate the version fields (eg. .installing) with worker objects. |
| 670 ChangedVersionAttributesMask mask(changed_mask); | 656 ChangedVersionAttributesMask mask(changed_mask); |
| 671 if (mask.installing_changed()) | 657 if (mask.installing_changed()) |
| 672 found->second->SetInstalling(GetOrAdoptServiceWorker(attrs.installing)); | 658 found->second->SetInstalling(GetOrCreateServiceWorker(installing.Pass())); |
| 673 if (mask.waiting_changed()) | 659 if (mask.waiting_changed()) |
| 674 found->second->SetWaiting(GetOrAdoptServiceWorker(attrs.waiting)); | 660 found->second->SetWaiting(GetOrCreateServiceWorker(waiting.Pass())); |
| 675 if (mask.active_changed()) | 661 if (mask.active_changed()) |
| 676 found->second->SetActive(GetOrAdoptServiceWorker(attrs.active)); | 662 found->second->SetActive(GetOrCreateServiceWorker(active.Pass())); |
| 677 } | 663 } |
| 678 } | 664 } |
| 679 | 665 |
| 680 void ServiceWorkerDispatcher::OnUpdateFound( | 666 void ServiceWorkerDispatcher::OnUpdateFound( |
| 681 int thread_id, | 667 int thread_id, |
| 682 int registration_handle_id) { | 668 int registration_handle_id) { |
| 683 TRACE_EVENT0("ServiceWorker", | 669 TRACE_EVENT0("ServiceWorker", |
| 684 "ServiceWorkerDispatcher::OnUpdateFound"); | 670 "ServiceWorkerDispatcher::OnUpdateFound"); |
| 685 RegistrationObjectMap::iterator found = | 671 RegistrationObjectMap::iterator found = |
| 686 registrations_.find(registration_handle_id); | 672 registrations_.find(registration_handle_id); |
| 687 if (found != registrations_.end()) | 673 if (found != registrations_.end()) |
| 688 found->second->OnUpdateFound(); | 674 found->second->OnUpdateFound(); |
| 689 } | 675 } |
| 690 | 676 |
| 691 void ServiceWorkerDispatcher::OnSetControllerServiceWorker( | 677 void ServiceWorkerDispatcher::OnSetControllerServiceWorker( |
| 692 int thread_id, | 678 int thread_id, |
| 693 int provider_id, | 679 int provider_id, |
| 694 const ServiceWorkerObjectInfo& info, | 680 const ServiceWorkerObjectInfo& info, |
| 695 bool should_notify_controllerchange) { | 681 bool should_notify_controllerchange) { |
| 696 TRACE_EVENT2("ServiceWorker", | 682 TRACE_EVENT2("ServiceWorker", |
| 697 "ServiceWorkerDispatcher::OnSetControllerServiceWorker", | 683 "ServiceWorkerDispatcher::OnSetControllerServiceWorker", |
| 698 "Thread ID", thread_id, | 684 "Thread ID", thread_id, |
| 699 "Provider ID", provider_id); | 685 "Provider ID", provider_id); |
| 700 | 686 |
| 701 // Adopt the reference sent from the browser process and pass it to the | 687 // Adopt the reference sent from the browser process and pass it to the |
| 702 // provider context if it exists. | 688 // provider context if it exists. |
| 703 scoped_ptr<ServiceWorkerHandleReference> handle_ref = | 689 scoped_ptr<ServiceWorkerHandleReference> handle_ref = Adopt(info); |
| 704 ServiceWorkerHandleReference::Adopt(info, thread_safe_sender_.get()); | |
| 705 ProviderContextMap::iterator provider = provider_contexts_.find(provider_id); | 690 ProviderContextMap::iterator provider = provider_contexts_.find(provider_id); |
| 706 if (provider != provider_contexts_.end()) | 691 if (provider != provider_contexts_.end()) |
| 707 provider->second->OnSetControllerServiceWorker(handle_ref.Pass()); | 692 provider->second->OnSetControllerServiceWorker(handle_ref.Pass()); |
| 708 | 693 |
| 709 ProviderClientMap::iterator found = provider_clients_.find(provider_id); | 694 ProviderClientMap::iterator found = provider_clients_.find(provider_id); |
| 710 if (found != provider_clients_.end()) { | 695 if (found != provider_clients_.end()) { |
| 711 // Get the existing worker object or create a new one with a new reference | 696 // Get the existing worker object or create a new one with a new reference |
| 712 // to populate the .controller field. | 697 // to populate the .controller field. |
| 713 scoped_refptr<WebServiceWorkerImpl> worker = GetOrCreateServiceWorker(info); | 698 scoped_refptr<WebServiceWorkerImpl> worker = GetOrCreateServiceWorker( |
| 699 ServiceWorkerHandleReference::Create(info, thread_safe_sender_.get())); |
| 714 found->second->setController(WebServiceWorkerImpl::CreateHandle(worker), | 700 found->second->setController(WebServiceWorkerImpl::CreateHandle(worker), |
| 715 should_notify_controllerchange); | 701 should_notify_controllerchange); |
| 716 } | 702 } |
| 717 } | 703 } |
| 718 | 704 |
| 719 void ServiceWorkerDispatcher::OnPostMessage( | 705 void ServiceWorkerDispatcher::OnPostMessage( |
| 720 const ServiceWorkerMsg_MessageToDocument_Params& params) { | 706 const ServiceWorkerMsg_MessageToDocument_Params& params) { |
| 721 // Make sure we're on the main document thread. (That must be the only | 707 // Make sure we're on the main document thread. (That must be the only |
| 722 // thread we get this message) | 708 // thread we get this message) |
| 723 DCHECK(ChildThreadImpl::current()); | 709 DCHECK_EQ(kDocumentMainThreadId, params.thread_id); |
| 724 TRACE_EVENT1("ServiceWorker", "ServiceWorkerDispatcher::OnPostMessage", | 710 TRACE_EVENT1("ServiceWorker", "ServiceWorkerDispatcher::OnPostMessage", |
| 725 "Thread ID", params.thread_id); | 711 "Thread ID", params.thread_id); |
| 726 | 712 |
| 713 // Adopt the reference sent from the browser process and get the corresponding |
| 714 // worker object. |
| 715 scoped_refptr<WebServiceWorkerImpl> worker = |
| 716 GetOrCreateServiceWorker(Adopt(params.service_worker_info)); |
| 717 |
| 727 ProviderClientMap::iterator found = | 718 ProviderClientMap::iterator found = |
| 728 provider_clients_.find(params.provider_id); | 719 provider_clients_.find(params.provider_id); |
| 729 if (found == provider_clients_.end()) { | 720 if (found == provider_clients_.end()) { |
| 730 // For now we do no queueing for messages sent to nonexistent / unattached | 721 // For now we do no queueing for messages sent to nonexistent / unattached |
| 731 // client. | 722 // client. |
| 732 return; | 723 return; |
| 733 } | 724 } |
| 734 | 725 |
| 735 blink::WebMessagePortChannelArray ports = | 726 blink::WebMessagePortChannelArray ports = |
| 736 WebMessagePortChannelImpl::CreatePorts( | 727 WebMessagePortChannelImpl::CreatePorts( |
| 737 params.message_ports, params.new_routing_ids, | 728 params.message_ports, params.new_routing_ids, |
| 738 base::ThreadTaskRunnerHandle::Get()); | 729 base::ThreadTaskRunnerHandle::Get()); |
| 739 | 730 |
| 740 scoped_refptr<WebServiceWorkerImpl> worker = | |
| 741 GetOrAdoptServiceWorker(params.service_worker_info); | |
| 742 found->second->dispatchMessageEvent( | 731 found->second->dispatchMessageEvent( |
| 743 WebServiceWorkerImpl::CreateHandle(worker), params.message, ports); | 732 WebServiceWorkerImpl::CreateHandle(worker), params.message, ports); |
| 744 } | 733 } |
| 745 | 734 |
| 746 void ServiceWorkerDispatcher::AddServiceWorker( | 735 void ServiceWorkerDispatcher::AddServiceWorker( |
| 747 int handle_id, WebServiceWorkerImpl* worker) { | 736 int handle_id, WebServiceWorkerImpl* worker) { |
| 748 DCHECK(!ContainsKey(service_workers_, handle_id)); | 737 DCHECK(!ContainsKey(service_workers_, handle_id)); |
| 749 service_workers_[handle_id] = worker; | 738 service_workers_[handle_id] = worker; |
| 750 } | 739 } |
| 751 | 740 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 773 return ServiceWorkerRegistrationHandleReference::Adopt( | 762 return ServiceWorkerRegistrationHandleReference::Adopt( |
| 774 info, thread_safe_sender_.get()); | 763 info, thread_safe_sender_.get()); |
| 775 } | 764 } |
| 776 | 765 |
| 777 scoped_ptr<ServiceWorkerHandleReference> ServiceWorkerDispatcher::Adopt( | 766 scoped_ptr<ServiceWorkerHandleReference> ServiceWorkerDispatcher::Adopt( |
| 778 const ServiceWorkerObjectInfo& info) { | 767 const ServiceWorkerObjectInfo& info) { |
| 779 return ServiceWorkerHandleReference::Adopt(info, thread_safe_sender_.get()); | 768 return ServiceWorkerHandleReference::Adopt(info, thread_safe_sender_.get()); |
| 780 } | 769 } |
| 781 | 770 |
| 782 } // namespace content | 771 } // namespace content |
| OLD | NEW |