Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(90)

Side by Side Diff: content/browser/service_worker/service_worker_dispatcher_host.cc

Issue 1799413002: ServiceWorker: Release a reference when it fails to dispatch ExtendableMessageEvent (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@move_dispatch_extendable_message_event
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 base::Bind(&ServiceWorkerDispatcherHost:: 892 base::Bind(&ServiceWorkerDispatcherHost::
882 DispatchExtendableMessageEventAfterStartWorker, 893 DispatchExtendableMessageEventAfterStartWorker,
883 this, worker, message, source_origin, sent_message_ports, 894 this, worker, message, source_origin, sent_message_ports,
884 ExtendableMessageEventSource(source_info)), 895 ExtendableMessageEventSource(source_info), callback),
885 base::Bind( 896 base::Bind(
886 &ServiceWorkerDispatcherHost::DidFailToDispatchExtendableMessageEvent, 897 &ServiceWorkerDispatcherHost::DidFailToDispatchExtendableMessageEvent<
887 this, sent_message_ports)); 898 SourceInfo>,
899 this, sent_message_ports, source_info, callback));
888 } 900 }
889 901
890 void ServiceWorkerDispatcherHost:: 902 void ServiceWorkerDispatcherHost::
891 DispatchExtendableMessageEventAfterStartWorker( 903 DispatchExtendableMessageEventAfterStartWorker(
892 scoped_refptr<ServiceWorkerVersion> worker, 904 scoped_refptr<ServiceWorkerVersion> worker,
893 const base::string16& message, 905 const base::string16& message,
894 const url::Origin& source_origin, 906 const url::Origin& source_origin,
895 const std::vector<TransferredMessagePort>& sent_message_ports, 907 const std::vector<TransferredMessagePort>& sent_message_ports,
896 const ExtendableMessageEventSource& source) { 908 const ExtendableMessageEventSource& source,
909 const StatusCallback& callback) {
897 int request_id = 910 int request_id =
898 worker->StartRequest(ServiceWorkerMetrics::EventType::MESSAGE, 911 worker->StartRequest(ServiceWorkerMetrics::EventType::MESSAGE, callback);
899 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
900 912
901 MessagePortMessageFilter* filter = 913 MessagePortMessageFilter* filter =
902 worker->embedded_worker()->message_port_message_filter(); 914 worker->embedded_worker()->message_port_message_filter();
903 std::vector<int> new_routing_ids; 915 std::vector<int> new_routing_ids;
904 filter->UpdateMessagePortsWithNewRoutes(sent_message_ports, &new_routing_ids); 916 filter->UpdateMessagePortsWithNewRoutes(sent_message_ports, &new_routing_ids);
905 917
906 ServiceWorkerMsg_ExtendableMessageEvent_Params params; 918 ServiceWorkerMsg_ExtendableMessageEvent_Params params;
907 params.message = message; 919 params.message = message;
908 params.source_origin = source_origin; 920 params.source_origin = source_origin;
909 params.message_ports = sent_message_ports; 921 params.message_ports = sent_message_ports;
910 params.new_routing_ids = new_routing_ids; 922 params.new_routing_ids = new_routing_ids;
911 params.source = source; 923 params.source = source;
912 924
913 // Hide the client url if the client has a unique origin. 925 // Hide the client url if the client has a unique origin.
914 if (source_origin.unique()) { 926 if (source_origin.unique()) {
915 if (params.source.client_info.IsValid()) 927 if (params.source.client_info.IsValid())
916 params.source.client_info.url = GURL(); 928 params.source.client_info.url = GURL();
917 else 929 else
918 params.source.service_worker_info.url = GURL(); 930 params.source.service_worker_info.url = GURL();
919 } 931 }
920 932
921 worker->DispatchSimpleEvent< 933 worker->DispatchSimpleEvent<
922 ServiceWorkerHostMsg_ExtendableMessageEventFinished>( 934 ServiceWorkerHostMsg_ExtendableMessageEventFinished>(
923 request_id, ServiceWorkerMsg_ExtendableMessageEvent(request_id, params)); 935 request_id, ServiceWorkerMsg_ExtendableMessageEvent(request_id, params));
924 } 936 }
925 937
938 template <typename SourceInfo>
926 void ServiceWorkerDispatcherHost::DidFailToDispatchExtendableMessageEvent( 939 void ServiceWorkerDispatcherHost::DidFailToDispatchExtendableMessageEvent(
927 const std::vector<TransferredMessagePort>& sent_message_ports, 940 const std::vector<TransferredMessagePort>& sent_message_ports,
941 const SourceInfo& source_info,
942 const StatusCallback& callback,
928 ServiceWorkerStatusCode status) { 943 ServiceWorkerStatusCode status) {
929 // Transfering the message ports failed, so destroy the ports. 944 // Transfering the message ports failed, so destroy the ports.
930 for (const TransferredMessagePort& port : sent_message_ports) 945 for (const TransferredMessagePort& port : sent_message_ports)
931 MessagePortService::GetInstance()->ClosePort(port.id); 946 MessagePortService::GetInstance()->ClosePort(port.id);
947 callback.Run(status);
948 }
949
950 template <>
951 void ServiceWorkerDispatcherHost::DidFailToDispatchExtendableMessageEvent(
falken 2016/03/17 05:10:49 I'm kind of confused why there are two ServiceWork
nhiroki 2016/03/17 06:05:30 "typename SourceInfo" is resolved to ServiceWorker
nhiroki 2016/03/17 06:33:28 FYI: I tried to use overloaded functions instead o
952 const std::vector<TransferredMessagePort>& sent_message_ports,
953 const ServiceWorkerObjectInfo& source_info,
954 const StatusCallback& callback,
955 ServiceWorkerStatusCode status) {
956 // Transfering the message ports failed, so destroy the ports.
957 for (const TransferredMessagePort& port : sent_message_ports)
958 MessagePortService::GetInstance()->ClosePort(port.id);
959 if (source_info.IsValid()) {
960 ServiceWorkerHandle* handle = handles_.Lookup(source_info.handle_id);
961 DCHECK(handle);
962 handle->DecrementRefCount();
963 if (handle->HasNoRefCount())
964 handles_.Remove(source_info.handle_id);
965 }
966 callback.Run(status);
932 } 967 }
933 968
934 ServiceWorkerRegistrationHandle* 969 ServiceWorkerRegistrationHandle*
935 ServiceWorkerDispatcherHost::FindRegistrationHandle(int provider_id, 970 ServiceWorkerDispatcherHost::FindRegistrationHandle(int provider_id,
936 int64_t registration_id) { 971 int64_t registration_id) {
937 for (RegistrationHandleMap::iterator iter(&registration_handles_); 972 for (RegistrationHandleMap::iterator iter(&registration_handles_);
938 !iter.IsAtEnd(); iter.Advance()) { 973 !iter.IsAtEnd(); iter.Advance()) {
939 ServiceWorkerRegistrationHandle* handle = iter.GetCurrentValue(); 974 ServiceWorkerRegistrationHandle* handle = iter.GetCurrentValue();
940 if (handle->provider_id() == provider_id && 975 if (handle->provider_id() == provider_id &&
941 handle->registration()->id() == registration_id) { 976 handle->registration()->id() == registration_id) {
(...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after
1389 if (!handle) { 1424 if (!handle) {
1390 bad_message::ReceivedBadMessage(this, 1425 bad_message::ReceivedBadMessage(this,
1391 bad_message::SWDH_TERMINATE_BAD_HANDLE); 1426 bad_message::SWDH_TERMINATE_BAD_HANDLE);
1392 return; 1427 return;
1393 } 1428 }
1394 handle->version()->StopWorker( 1429 handle->version()->StopWorker(
1395 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); 1430 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
1396 } 1431 }
1397 1432
1398 } // namespace content 1433 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698