| 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/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
| 8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "base/threading/thread_local.h" | 10 #include "base/threading/thread_local.h" |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 ServiceWorkerProviderContext* provider_context) { | 178 ServiceWorkerProviderContext* provider_context) { |
| 179 DCHECK(provider_context); | 179 DCHECK(provider_context); |
| 180 DCHECK(ContainsKey(provider_contexts_, provider_context->provider_id())); | 180 DCHECK(ContainsKey(provider_contexts_, provider_context->provider_id())); |
| 181 provider_contexts_.erase(provider_context->provider_id()); | 181 provider_contexts_.erase(provider_context->provider_id()); |
| 182 worker_to_provider_.erase(provider_context->installing_handle_id()); | 182 worker_to_provider_.erase(provider_context->installing_handle_id()); |
| 183 worker_to_provider_.erase(provider_context->waiting_handle_id()); | 183 worker_to_provider_.erase(provider_context->waiting_handle_id()); |
| 184 worker_to_provider_.erase(provider_context->active_handle_id()); | 184 worker_to_provider_.erase(provider_context->active_handle_id()); |
| 185 worker_to_provider_.erase(provider_context->controller_handle_id()); | 185 worker_to_provider_.erase(provider_context->controller_handle_id()); |
| 186 } | 186 } |
| 187 | 187 |
| 188 void ServiceWorkerDispatcher::AddScriptClient( | 188 void ServiceWorkerDispatcher::AddProviderClient( |
| 189 int provider_id, | 189 int provider_id, |
| 190 blink::WebServiceWorkerProviderClient* client) { | 190 blink::WebServiceWorkerProviderClient* client) { |
| 191 DCHECK(client); | 191 DCHECK(client); |
| 192 DCHECK(!ContainsKey(script_clients_, provider_id)); | 192 DCHECK(!ContainsKey(provider_clients_, provider_id)); |
| 193 script_clients_[provider_id] = client; | 193 provider_clients_[provider_id] = client; |
| 194 } | 194 } |
| 195 | 195 |
| 196 void ServiceWorkerDispatcher::RemoveScriptClient(int provider_id) { | 196 void ServiceWorkerDispatcher::RemoveProviderClient(int provider_id) { |
| 197 // This could be possibly called multiple times to ensure termination. | 197 // This could be possibly called multiple times to ensure termination. |
| 198 if (ContainsKey(script_clients_, provider_id)) | 198 if (ContainsKey(provider_clients_, provider_id)) |
| 199 script_clients_.erase(provider_id); | 199 provider_clients_.erase(provider_id); |
| 200 } | 200 } |
| 201 | 201 |
| 202 ServiceWorkerDispatcher* | 202 ServiceWorkerDispatcher* |
| 203 ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance( | 203 ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance( |
| 204 ThreadSafeSender* thread_safe_sender) { | 204 ThreadSafeSender* thread_safe_sender) { |
| 205 if (g_dispatcher_tls.Pointer()->Get() == kHasBeenDeleted) { | 205 if (g_dispatcher_tls.Pointer()->Get() == kHasBeenDeleted) { |
| 206 NOTREACHED() << "Re-instantiating TLS ServiceWorkerDispatcher."; | 206 NOTREACHED() << "Re-instantiating TLS ServiceWorkerDispatcher."; |
| 207 g_dispatcher_tls.Pointer()->Set(NULL); | 207 g_dispatcher_tls.Pointer()->Set(NULL); |
| 208 } | 208 } |
| 209 if (g_dispatcher_tls.Pointer()->Get()) | 209 if (g_dispatcher_tls.Pointer()->Get()) |
| (...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 583 void ServiceWorkerDispatcher::SetReadyRegistration( | 583 void ServiceWorkerDispatcher::SetReadyRegistration( |
| 584 int provider_id, | 584 int provider_id, |
| 585 int registration_handle_id) { | 585 int registration_handle_id) { |
| 586 ProviderContextMap::iterator provider = provider_contexts_.find(provider_id); | 586 ProviderContextMap::iterator provider = provider_contexts_.find(provider_id); |
| 587 if (provider == provider_contexts_.end() || | 587 if (provider == provider_contexts_.end() || |
| 588 provider->second->registration_handle_id() != registration_handle_id || | 588 provider->second->registration_handle_id() != registration_handle_id || |
| 589 provider->second->active_handle_id() == kInvalidServiceWorkerHandleId) { | 589 provider->second->active_handle_id() == kInvalidServiceWorkerHandleId) { |
| 590 return; | 590 return; |
| 591 } | 591 } |
| 592 | 592 |
| 593 ScriptClientMap::iterator client = script_clients_.find(provider_id); | 593 ProviderClientMap::iterator client = provider_clients_.find(provider_id); |
| 594 if (client == script_clients_.end()) | 594 if (client == provider_clients_.end()) |
| 595 return; | 595 return; |
| 596 | 596 |
| 597 ServiceWorkerRegistrationObjectInfo info = | 597 ServiceWorkerRegistrationObjectInfo info = |
| 598 provider->second->registration()->info(); | 598 provider->second->registration()->info(); |
| 599 WebServiceWorkerRegistrationImpl* registration = | 599 WebServiceWorkerRegistrationImpl* registration = |
| 600 FindServiceWorkerRegistration(info, false); | 600 FindServiceWorkerRegistration(info, false); |
| 601 if (!registration) { | 601 if (!registration) { |
| 602 registration = CreateServiceWorkerRegistration(info, false); | 602 registration = CreateServiceWorkerRegistration(info, false); |
| 603 ServiceWorkerVersionAttributes attrs = | 603 ServiceWorkerVersionAttributes attrs = |
| 604 provider->second->GetVersionAttributes(); | 604 provider->second->GetVersionAttributes(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 622 "Provider ID", provider_id); | 622 "Provider ID", provider_id); |
| 623 | 623 |
| 624 ProviderContextMap::iterator provider = provider_contexts_.find(provider_id); | 624 ProviderContextMap::iterator provider = provider_contexts_.find(provider_id); |
| 625 if (provider != provider_contexts_.end()) { | 625 if (provider != provider_contexts_.end()) { |
| 626 worker_to_provider_.erase(provider->second->controller_handle_id()); | 626 worker_to_provider_.erase(provider->second->controller_handle_id()); |
| 627 if (info.handle_id != kInvalidServiceWorkerHandleId) | 627 if (info.handle_id != kInvalidServiceWorkerHandleId) |
| 628 worker_to_provider_[info.handle_id] = provider->second; | 628 worker_to_provider_[info.handle_id] = provider->second; |
| 629 provider->second->OnSetControllerServiceWorker(info); | 629 provider->second->OnSetControllerServiceWorker(info); |
| 630 } | 630 } |
| 631 | 631 |
| 632 ScriptClientMap::iterator found = script_clients_.find(provider_id); | 632 ProviderClientMap::iterator found = provider_clients_.find(provider_id); |
| 633 if (found != script_clients_.end()) { | 633 if (found != provider_clients_.end()) { |
| 634 // Populate the .controller field with the new worker object. | 634 // Populate the .controller field with the new worker object. |
| 635 found->second->setController(GetServiceWorker(info, false), | 635 found->second->setController(GetServiceWorker(info, false), |
| 636 should_notify_controllerchange); | 636 should_notify_controllerchange); |
| 637 } | 637 } |
| 638 } | 638 } |
| 639 | 639 |
| 640 void ServiceWorkerDispatcher::OnPostMessage( | 640 void ServiceWorkerDispatcher::OnPostMessage( |
| 641 int thread_id, | 641 int thread_id, |
| 642 int provider_id, | 642 int provider_id, |
| 643 const base::string16& message, | 643 const base::string16& message, |
| 644 const std::vector<int>& sent_message_port_ids, | 644 const std::vector<int>& sent_message_port_ids, |
| 645 const std::vector<int>& new_routing_ids) { | 645 const std::vector<int>& new_routing_ids) { |
| 646 // Make sure we're on the main document thread. (That must be the only | 646 // Make sure we're on the main document thread. (That must be the only |
| 647 // thread we get this message) | 647 // thread we get this message) |
| 648 DCHECK(ChildThread::current()); | 648 DCHECK(ChildThread::current()); |
| 649 TRACE_EVENT1("ServiceWorker", | 649 TRACE_EVENT1("ServiceWorker", |
| 650 "ServiceWorkerDispatcher::OnPostMessage", | 650 "ServiceWorkerDispatcher::OnPostMessage", |
| 651 "Thread ID", thread_id); | 651 "Thread ID", thread_id); |
| 652 | 652 |
| 653 ScriptClientMap::iterator found = script_clients_.find(provider_id); | 653 ProviderClientMap::iterator found = provider_clients_.find(provider_id); |
| 654 if (found == script_clients_.end()) { | 654 if (found == provider_clients_.end()) { |
| 655 // For now we do no queueing for messages sent to nonexistent / unattached | 655 // For now we do no queueing for messages sent to nonexistent / unattached |
| 656 // client. | 656 // client. |
| 657 return; | 657 return; |
| 658 } | 658 } |
| 659 | 659 |
| 660 std::vector<WebMessagePortChannelImpl*> ports; | 660 std::vector<WebMessagePortChannelImpl*> ports; |
| 661 if (!sent_message_port_ids.empty()) { | 661 if (!sent_message_port_ids.empty()) { |
| 662 ports.resize(sent_message_port_ids.size()); | 662 ports.resize(sent_message_port_ids.size()); |
| 663 for (size_t i = 0; i < sent_message_port_ids.size(); ++i) { | 663 for (size_t i = 0; i < sent_message_port_ids.size(); ++i) { |
| 664 ports[i] = new WebMessagePortChannelImpl( | 664 ports[i] = new WebMessagePortChannelImpl( |
| 665 new_routing_ids[i], sent_message_port_ids[i], | 665 new_routing_ids[i], sent_message_port_ids[i], |
| 666 base::MessageLoopProxy::current()); | 666 base::MessageLoopProxy::current()); |
| 667 } | 667 } |
| 668 } | 668 } |
| 669 | 669 |
| 670 found->second->dispatchMessageEvent(message, ports); | 670 found->second->dispatchMessageEvent(message, ports); |
| 671 } | 671 } |
| 672 | 672 |
| 673 void ServiceWorkerDispatcher::OnGetClientInfo(int thread_id, | 673 void ServiceWorkerDispatcher::OnGetClientInfo(int thread_id, |
| 674 int embedded_worker_id, | 674 int embedded_worker_id, |
| 675 int request_id, | 675 int request_id, |
| 676 int provider_id) { | 676 int provider_id) { |
| 677 blink::WebServiceWorkerClientInfo info; | 677 blink::WebServiceWorkerClientInfo info; |
| 678 ScriptClientMap::iterator found = script_clients_.find(provider_id); | 678 ProviderClientMap::iterator found = provider_clients_.find(provider_id); |
| 679 // TODO(ksakamoto): Could we track these values in the browser side? Except | 679 // TODO(ksakamoto): Could we track these values in the browser side? Except |
| 680 // for |isFocused|, it would be pretty easy. | 680 // for |isFocused|, it would be pretty easy. |
| 681 if (found != script_clients_.end() && found->second->getClientInfo(&info)) { | 681 if (found != provider_clients_.end() && found->second->getClientInfo(&info)) { |
| 682 ServiceWorkerClientInfo result; | 682 ServiceWorkerClientInfo result; |
| 683 result.client_id = info.clientID; | 683 result.client_id = info.clientID; |
| 684 result.page_visibility_state = info.pageVisibilityState; | 684 result.page_visibility_state = info.pageVisibilityState; |
| 685 result.is_focused = info.isFocused; | 685 result.is_focused = info.isFocused; |
| 686 result.url = info.url; | 686 result.url = info.url; |
| 687 result.frame_type = static_cast<RequestContextFrameType>(info.frameType); | 687 result.frame_type = static_cast<RequestContextFrameType>(info.frameType); |
| 688 | 688 |
| 689 thread_safe_sender_->Send(new ServiceWorkerHostMsg_GetClientInfoSuccess( | 689 thread_safe_sender_->Send(new ServiceWorkerHostMsg_GetClientInfoSuccess( |
| 690 embedded_worker_id, request_id, result)); | 690 embedded_worker_id, request_id, result)); |
| 691 } else { | 691 } else { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 736 attrs.installing, thread_safe_sender_.get()); | 736 attrs.installing, thread_safe_sender_.get()); |
| 737 ServiceWorkerHandleReference::Adopt( | 737 ServiceWorkerHandleReference::Adopt( |
| 738 attrs.waiting, thread_safe_sender_.get()); | 738 attrs.waiting, thread_safe_sender_.get()); |
| 739 ServiceWorkerHandleReference::Adopt( | 739 ServiceWorkerHandleReference::Adopt( |
| 740 attrs.active, thread_safe_sender_.get()); | 740 attrs.active, thread_safe_sender_.get()); |
| 741 } | 741 } |
| 742 return registration; | 742 return registration; |
| 743 } | 743 } |
| 744 | 744 |
| 745 } // namespace content | 745 } // namespace content |
| OLD | NEW |