| 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/service_worker_dispatcher_host.h" | 5 #include "content/browser/service_worker/service_worker_dispatcher_host.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 683 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 694 return; | 694 return; |
| 695 } | 695 } |
| 696 | 696 |
| 697 ServiceWorkerProviderHost* sender_provider_host = | 697 ServiceWorkerProviderHost* sender_provider_host = |
| 698 GetContext()->GetProviderHost(render_process_id_, provider_id); | 698 GetContext()->GetProviderHost(render_process_id_, provider_id); |
| 699 if (!sender_provider_host) { | 699 if (!sender_provider_host) { |
| 700 bad_message::ReceivedBadMessage(this, bad_message::SWDH_POST_MESSAGE); | 700 bad_message::ReceivedBadMessage(this, bad_message::SWDH_POST_MESSAGE); |
| 701 return; | 701 return; |
| 702 } | 702 } |
| 703 | 703 |
| 704 DispatchExtendableMessageEvent( |
| 705 make_scoped_refptr(handle->version()), message, source_origin, |
| 706 sent_message_ports, sender_provider_host, |
| 707 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 708 } |
| 709 |
| 710 void ServiceWorkerDispatcherHost::DispatchExtendableMessageEvent( |
| 711 scoped_refptr<ServiceWorkerVersion> worker, |
| 712 const base::string16& message, |
| 713 const url::Origin& source_origin, |
| 714 const std::vector<TransferredMessagePort>& sent_message_ports, |
| 715 ServiceWorkerProviderHost* sender_provider_host, |
| 716 const StatusCallback& callback) { |
| 704 for (const TransferredMessagePort& port : sent_message_ports) | 717 for (const TransferredMessagePort& port : sent_message_ports) |
| 705 MessagePortService::GetInstance()->HoldMessages(port.id); | 718 MessagePortService::GetInstance()->HoldMessages(port.id); |
| 706 | 719 |
| 707 switch (sender_provider_host->provider_type()) { | 720 switch (sender_provider_host->provider_type()) { |
| 708 case SERVICE_WORKER_PROVIDER_FOR_WINDOW: | 721 case SERVICE_WORKER_PROVIDER_FOR_WINDOW: |
| 709 case SERVICE_WORKER_PROVIDER_FOR_WORKER: | 722 case SERVICE_WORKER_PROVIDER_FOR_WORKER: |
| 710 case SERVICE_WORKER_PROVIDER_FOR_SHARED_WORKER: | 723 case SERVICE_WORKER_PROVIDER_FOR_SHARED_WORKER: |
| 711 service_worker_client_utils::GetClient( | 724 service_worker_client_utils::GetClient( |
| 712 sender_provider_host, | 725 sender_provider_host, |
| 713 base::Bind( | 726 base::Bind(&ServiceWorkerDispatcherHost:: |
| 714 &ServiceWorkerDispatcherHost::DispatchExtendableMessageEvent< | 727 DispatchExtendableMessageEventInternal< |
| 715 ServiceWorkerClientInfo>, | 728 ServiceWorkerClientInfo>, |
| 716 this, make_scoped_refptr(handle->version()), message, | 729 this, worker, message, source_origin, sent_message_ports, |
| 717 source_origin, sent_message_ports)); | 730 callback)); |
| 718 break; | 731 break; |
| 719 case SERVICE_WORKER_PROVIDER_FOR_CONTROLLER: | 732 case SERVICE_WORKER_PROVIDER_FOR_CONTROLLER: |
| 720 // TODO(nhiroki): Decrement a reference to ServiceWorkerHandle if starting | |
| 721 // worker fails (http://crbug.com/543198). | |
| 722 RunSoon(base::Bind( | 733 RunSoon(base::Bind( |
| 723 &ServiceWorkerDispatcherHost::DispatchExtendableMessageEvent< | 734 &ServiceWorkerDispatcherHost::DispatchExtendableMessageEventInternal< |
| 724 ServiceWorkerObjectInfo>, | 735 ServiceWorkerObjectInfo>, |
| 725 this, make_scoped_refptr(handle->version()), message, source_origin, | 736 this, worker, message, source_origin, sent_message_ports, callback, |
| 726 sent_message_ports, | |
| 727 sender_provider_host->GetOrCreateServiceWorkerHandle( | 737 sender_provider_host->GetOrCreateServiceWorkerHandle( |
| 728 sender_provider_host->running_hosted_version()))); | 738 sender_provider_host->running_hosted_version()))); |
| 729 break; | 739 break; |
| 730 case SERVICE_WORKER_PROVIDER_FOR_SANDBOXED_FRAME: | 740 case SERVICE_WORKER_PROVIDER_FOR_SANDBOXED_FRAME: |
| 731 case SERVICE_WORKER_PROVIDER_UNKNOWN: | 741 case SERVICE_WORKER_PROVIDER_UNKNOWN: |
| 732 NOTREACHED() << sender_provider_host->provider_type(); | 742 NOTREACHED() << sender_provider_host->provider_type(); |
| 733 break; | 743 break; |
| 734 } | 744 } |
| 735 } | 745 } |
| 736 | 746 |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 859 ServiceWorkerRegistrationObjectInfo info; | 869 ServiceWorkerRegistrationObjectInfo info; |
| 860 ServiceWorkerVersionAttributes attrs; | 870 ServiceWorkerVersionAttributes attrs; |
| 861 GetRegistrationObjectInfoAndVersionAttributes( | 871 GetRegistrationObjectInfoAndVersionAttributes( |
| 862 provider_host->AsWeakPtr(), registration, &info, &attrs); | 872 provider_host->AsWeakPtr(), registration, &info, &attrs); |
| 863 | 873 |
| 864 Send(new ServiceWorkerMsg_AssociateRegistration(kDocumentMainThreadId, | 874 Send(new ServiceWorkerMsg_AssociateRegistration(kDocumentMainThreadId, |
| 865 provider_id, info, attrs)); | 875 provider_id, info, attrs)); |
| 866 } | 876 } |
| 867 | 877 |
| 868 template <typename SourceInfo> | 878 template <typename SourceInfo> |
| 869 void ServiceWorkerDispatcherHost::DispatchExtendableMessageEvent( | 879 void ServiceWorkerDispatcherHost::DispatchExtendableMessageEventInternal( |
| 870 scoped_refptr<ServiceWorkerVersion> worker, | 880 scoped_refptr<ServiceWorkerVersion> worker, |
| 871 const base::string16& message, | 881 const base::string16& message, |
| 872 const url::Origin& source_origin, | 882 const url::Origin& source_origin, |
| 873 const std::vector<TransferredMessagePort>& sent_message_ports, | 883 const std::vector<TransferredMessagePort>& sent_message_ports, |
| 884 const StatusCallback& callback, |
| 874 const SourceInfo& source_info) { | 885 const SourceInfo& source_info) { |
| 875 if (!source_info.IsValid()) { | 886 if (!source_info.IsValid()) { |
| 876 DidFailToDispatchExtendableMessageEvent(sent_message_ports, | 887 DidFailToDispatchExtendableMessageEvent<SourceInfo>( |
| 877 SERVICE_WORKER_ERROR_FAILED); | 888 sent_message_ports, source_info, callback, SERVICE_WORKER_ERROR_FAILED); |
| 878 return; | 889 return; |
| 879 } | 890 } |
| 880 worker->RunAfterStartWorker( | 891 worker->RunAfterStartWorker( |
| 881 ServiceWorkerMetrics::EventType::MESSAGE, | 892 ServiceWorkerMetrics::EventType::MESSAGE, |
| 882 base::Bind(&ServiceWorkerDispatcherHost:: | 893 base::Bind(&ServiceWorkerDispatcherHost:: |
| 883 DispatchExtendableMessageEventAfterStartWorker, | 894 DispatchExtendableMessageEventAfterStartWorker, |
| 884 this, worker, message, source_origin, sent_message_ports, | 895 this, worker, message, source_origin, sent_message_ports, |
| 885 ExtendableMessageEventSource(source_info)), | 896 ExtendableMessageEventSource(source_info), callback), |
| 886 base::Bind( | 897 base::Bind( |
| 887 &ServiceWorkerDispatcherHost::DidFailToDispatchExtendableMessageEvent, | 898 &ServiceWorkerDispatcherHost::DidFailToDispatchExtendableMessageEvent< |
| 888 this, sent_message_ports)); | 899 SourceInfo>, |
| 900 this, sent_message_ports, source_info, callback)); |
| 889 } | 901 } |
| 890 | 902 |
| 891 void ServiceWorkerDispatcherHost:: | 903 void ServiceWorkerDispatcherHost:: |
| 892 DispatchExtendableMessageEventAfterStartWorker( | 904 DispatchExtendableMessageEventAfterStartWorker( |
| 893 scoped_refptr<ServiceWorkerVersion> worker, | 905 scoped_refptr<ServiceWorkerVersion> worker, |
| 894 const base::string16& message, | 906 const base::string16& message, |
| 895 const url::Origin& source_origin, | 907 const url::Origin& source_origin, |
| 896 const std::vector<TransferredMessagePort>& sent_message_ports, | 908 const std::vector<TransferredMessagePort>& sent_message_ports, |
| 897 const ExtendableMessageEventSource& source) { | 909 const ExtendableMessageEventSource& source, |
| 910 const StatusCallback& callback) { |
| 898 int request_id = | 911 int request_id = |
| 899 worker->StartRequest(ServiceWorkerMetrics::EventType::MESSAGE, | 912 worker->StartRequest(ServiceWorkerMetrics::EventType::MESSAGE, callback); |
| 900 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | |
| 901 | 913 |
| 902 MessagePortMessageFilter* filter = | 914 MessagePortMessageFilter* filter = |
| 903 worker->embedded_worker()->message_port_message_filter(); | 915 worker->embedded_worker()->message_port_message_filter(); |
| 904 std::vector<int> new_routing_ids; | 916 std::vector<int> new_routing_ids; |
| 905 filter->UpdateMessagePortsWithNewRoutes(sent_message_ports, &new_routing_ids); | 917 filter->UpdateMessagePortsWithNewRoutes(sent_message_ports, &new_routing_ids); |
| 906 | 918 |
| 907 ServiceWorkerMsg_ExtendableMessageEvent_Params params; | 919 ServiceWorkerMsg_ExtendableMessageEvent_Params params; |
| 908 params.message = message; | 920 params.message = message; |
| 909 params.source_origin = source_origin; | 921 params.source_origin = source_origin; |
| 910 params.message_ports = sent_message_ports; | 922 params.message_ports = sent_message_ports; |
| 911 params.new_routing_ids = new_routing_ids; | 923 params.new_routing_ids = new_routing_ids; |
| 912 params.source = source; | 924 params.source = source; |
| 913 | 925 |
| 914 // Hide the client url if the client has a unique origin. | 926 // Hide the client url if the client has a unique origin. |
| 915 if (source_origin.unique()) { | 927 if (source_origin.unique()) { |
| 916 if (params.source.client_info.IsValid()) | 928 if (params.source.client_info.IsValid()) |
| 917 params.source.client_info.url = GURL(); | 929 params.source.client_info.url = GURL(); |
| 918 else | 930 else |
| 919 params.source.service_worker_info.url = GURL(); | 931 params.source.service_worker_info.url = GURL(); |
| 920 } | 932 } |
| 921 | 933 |
| 922 worker->DispatchSimpleEvent< | 934 worker->DispatchSimpleEvent< |
| 923 ServiceWorkerHostMsg_ExtendableMessageEventFinished>( | 935 ServiceWorkerHostMsg_ExtendableMessageEventFinished>( |
| 924 request_id, ServiceWorkerMsg_ExtendableMessageEvent(request_id, params)); | 936 request_id, ServiceWorkerMsg_ExtendableMessageEvent(request_id, params)); |
| 925 } | 937 } |
| 926 | 938 |
| 939 template <typename SourceInfo> |
| 927 void ServiceWorkerDispatcherHost::DidFailToDispatchExtendableMessageEvent( | 940 void ServiceWorkerDispatcherHost::DidFailToDispatchExtendableMessageEvent( |
| 928 const std::vector<TransferredMessagePort>& sent_message_ports, | 941 const std::vector<TransferredMessagePort>& sent_message_ports, |
| 942 const SourceInfo& source_info, |
| 943 const StatusCallback& callback, |
| 929 ServiceWorkerStatusCode status) { | 944 ServiceWorkerStatusCode status) { |
| 930 // Transfering the message ports failed, so destroy the ports. | 945 // Transfering the message ports failed, so destroy the ports. |
| 931 for (const TransferredMessagePort& port : sent_message_ports) | 946 for (const TransferredMessagePort& port : sent_message_ports) |
| 932 MessagePortService::GetInstance()->ClosePort(port.id); | 947 MessagePortService::GetInstance()->ClosePort(port.id); |
| 948 if (source_info.IsValid()) |
| 949 ReleaseSourceInfo(source_info); |
| 950 callback.Run(status); |
| 951 } |
| 952 |
| 953 void ServiceWorkerDispatcherHost::ReleaseSourceInfo( |
| 954 const ServiceWorkerClientInfo& source_info) { |
| 955 // ServiceWorkerClientInfo is just a snapshot of the client. There is no need |
| 956 // to do anything for it. |
| 957 } |
| 958 |
| 959 void ServiceWorkerDispatcherHost::ReleaseSourceInfo( |
| 960 const ServiceWorkerObjectInfo& source_info) { |
| 961 ServiceWorkerHandle* handle = handles_.Lookup(source_info.handle_id); |
| 962 DCHECK(handle); |
| 963 handle->DecrementRefCount(); |
| 964 if (handle->HasNoRefCount()) |
| 965 handles_.Remove(source_info.handle_id); |
| 933 } | 966 } |
| 934 | 967 |
| 935 ServiceWorkerRegistrationHandle* | 968 ServiceWorkerRegistrationHandle* |
| 936 ServiceWorkerDispatcherHost::FindRegistrationHandle(int provider_id, | 969 ServiceWorkerDispatcherHost::FindRegistrationHandle(int provider_id, |
| 937 int64_t registration_id) { | 970 int64_t registration_id) { |
| 938 for (RegistrationHandleMap::iterator iter(®istration_handles_); | 971 for (RegistrationHandleMap::iterator iter(®istration_handles_); |
| 939 !iter.IsAtEnd(); iter.Advance()) { | 972 !iter.IsAtEnd(); iter.Advance()) { |
| 940 ServiceWorkerRegistrationHandle* handle = iter.GetCurrentValue(); | 973 ServiceWorkerRegistrationHandle* handle = iter.GetCurrentValue(); |
| 941 if (handle->provider_id() == provider_id && | 974 if (handle->provider_id() == provider_id && |
| 942 handle->registration()->id() == registration_id) { | 975 handle->registration()->id() == registration_id) { |
| (...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1390 if (!handle) { | 1423 if (!handle) { |
| 1391 bad_message::ReceivedBadMessage(this, | 1424 bad_message::ReceivedBadMessage(this, |
| 1392 bad_message::SWDH_TERMINATE_BAD_HANDLE); | 1425 bad_message::SWDH_TERMINATE_BAD_HANDLE); |
| 1393 return; | 1426 return; |
| 1394 } | 1427 } |
| 1395 handle->version()->StopWorker( | 1428 handle->version()->StopWorker( |
| 1396 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 1429 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 1397 } | 1430 } |
| 1398 | 1431 |
| 1399 } // namespace content | 1432 } // namespace content |
| OLD | NEW |