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

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

Issue 2506263005: Prevent a service worker from keeping itself alive by self postMessage. (Closed)
Patch Set: fix unit test Created 4 years, 1 month 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/debug/crash_logging.h" 9 #include "base/debug/crash_logging.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 903 matching lines...) Expand 10 before | Expand all | Expand 10 after
914 switch (sender_provider_host->provider_type()) { 914 switch (sender_provider_host->provider_type()) {
915 case SERVICE_WORKER_PROVIDER_FOR_WINDOW: 915 case SERVICE_WORKER_PROVIDER_FOR_WINDOW:
916 case SERVICE_WORKER_PROVIDER_FOR_WORKER: 916 case SERVICE_WORKER_PROVIDER_FOR_WORKER:
917 case SERVICE_WORKER_PROVIDER_FOR_SHARED_WORKER: 917 case SERVICE_WORKER_PROVIDER_FOR_SHARED_WORKER:
918 service_worker_client_utils::GetClient( 918 service_worker_client_utils::GetClient(
919 sender_provider_host, 919 sender_provider_host,
920 base::Bind(&ServiceWorkerDispatcherHost:: 920 base::Bind(&ServiceWorkerDispatcherHost::
921 DispatchExtendableMessageEventInternal< 921 DispatchExtendableMessageEventInternal<
922 ServiceWorkerClientInfo>, 922 ServiceWorkerClientInfo>,
923 this, worker, message, source_origin, sent_message_ports, 923 this, worker, message, source_origin, sent_message_ports,
924 callback)); 924 callback, base::nullopt));
925 break; 925 break;
926 case SERVICE_WORKER_PROVIDER_FOR_CONTROLLER: 926 case SERVICE_WORKER_PROVIDER_FOR_CONTROLLER: {
falken 2016/11/21 08:01:48 nit: maybe add some comment like "Clamp timeout to
Marijn Kruisselbrink 2016/12/01 19:54:30 Done
927 base::TimeDelta timeout =
928 sender_provider_host->running_hosted_version()->remaining_timeout();
927 RunSoon(base::Bind( 929 RunSoon(base::Bind(
928 &ServiceWorkerDispatcherHost::DispatchExtendableMessageEventInternal< 930 &ServiceWorkerDispatcherHost::DispatchExtendableMessageEventInternal<
929 ServiceWorkerObjectInfo>, 931 ServiceWorkerObjectInfo>,
930 this, worker, message, source_origin, sent_message_ports, callback, 932 this, worker, message, source_origin, sent_message_ports, callback,
933 base::make_optional(timeout),
931 sender_provider_host->GetOrCreateServiceWorkerHandle( 934 sender_provider_host->GetOrCreateServiceWorkerHandle(
932 sender_provider_host->running_hosted_version()))); 935 sender_provider_host->running_hosted_version())));
933 break; 936 break;
937 }
934 case SERVICE_WORKER_PROVIDER_UNKNOWN: 938 case SERVICE_WORKER_PROVIDER_UNKNOWN:
935 default: 939 default:
936 NOTREACHED() << sender_provider_host->provider_type(); 940 NOTREACHED() << sender_provider_host->provider_type();
937 break; 941 break;
938 } 942 }
939 } 943 }
940 944
941 void ServiceWorkerDispatcherHost::OnProviderCreated( 945 void ServiceWorkerDispatcherHost::OnProviderCreated(
942 int provider_id, 946 int provider_id,
943 int route_id, 947 int route_id,
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
1078 provider_id, info, attrs)); 1082 provider_id, info, attrs));
1079 } 1083 }
1080 1084
1081 template <typename SourceInfo> 1085 template <typename SourceInfo>
1082 void ServiceWorkerDispatcherHost::DispatchExtendableMessageEventInternal( 1086 void ServiceWorkerDispatcherHost::DispatchExtendableMessageEventInternal(
1083 scoped_refptr<ServiceWorkerVersion> worker, 1087 scoped_refptr<ServiceWorkerVersion> worker,
1084 const base::string16& message, 1088 const base::string16& message,
1085 const url::Origin& source_origin, 1089 const url::Origin& source_origin,
1086 const std::vector<int>& sent_message_ports, 1090 const std::vector<int>& sent_message_ports,
1087 const StatusCallback& callback, 1091 const StatusCallback& callback,
1092 const base::Optional<base::TimeDelta>& timeout,
1088 const SourceInfo& source_info) { 1093 const SourceInfo& source_info) {
1089 if (!source_info.IsValid()) { 1094 if (!source_info.IsValid()) {
1090 DidFailToDispatchExtendableMessageEvent<SourceInfo>( 1095 DidFailToDispatchExtendableMessageEvent<SourceInfo>(
1091 sent_message_ports, source_info, callback, SERVICE_WORKER_ERROR_FAILED); 1096 sent_message_ports, source_info, callback, SERVICE_WORKER_ERROR_FAILED);
1092 return; 1097 return;
1093 } 1098 }
1099
1100 // If not enough time is left to actually process the event don't even
1101 // bother starting the worker and sending the event.
1102 if (timeout && *timeout < base::TimeDelta::FromMilliseconds(100)) {
1103 DidFailToDispatchExtendableMessageEvent<SourceInfo>(
1104 sent_message_ports, source_info, callback, SERVICE_WORKER_ERROR_FAILED);
falken 2016/11/21 08:01:48 Hm, TIMEOUT seems like a better error code here, s
Marijn Kruisselbrink 2016/12/01 19:54:29 Good point, done.
1105 return;
1106 }
1107
1094 worker->RunAfterStartWorker( 1108 worker->RunAfterStartWorker(
1095 ServiceWorkerMetrics::EventType::MESSAGE, 1109 ServiceWorkerMetrics::EventType::MESSAGE,
1096 base::Bind(&ServiceWorkerDispatcherHost:: 1110 base::Bind(&ServiceWorkerDispatcherHost::
1097 DispatchExtendableMessageEventAfterStartWorker, 1111 DispatchExtendableMessageEventAfterStartWorker,
1098 this, worker, message, source_origin, sent_message_ports, 1112 this, worker, message, source_origin, sent_message_ports,
1099 ExtendableMessageEventSource(source_info), callback), 1113 ExtendableMessageEventSource(source_info), timeout, callback),
1100 base::Bind( 1114 base::Bind(
1101 &ServiceWorkerDispatcherHost::DidFailToDispatchExtendableMessageEvent< 1115 &ServiceWorkerDispatcherHost::DidFailToDispatchExtendableMessageEvent<
1102 SourceInfo>, 1116 SourceInfo>,
1103 this, sent_message_ports, source_info, callback)); 1117 this, sent_message_ports, source_info, callback));
1104 } 1118 }
1105 1119
1106 void ServiceWorkerDispatcherHost:: 1120 void ServiceWorkerDispatcherHost::
1107 DispatchExtendableMessageEventAfterStartWorker( 1121 DispatchExtendableMessageEventAfterStartWorker(
1108 scoped_refptr<ServiceWorkerVersion> worker, 1122 scoped_refptr<ServiceWorkerVersion> worker,
1109 const base::string16& message, 1123 const base::string16& message,
1110 const url::Origin& source_origin, 1124 const url::Origin& source_origin,
1111 const std::vector<int>& sent_message_ports, 1125 const std::vector<int>& sent_message_ports,
1112 const ExtendableMessageEventSource& source, 1126 const ExtendableMessageEventSource& source,
1127 const base::Optional<base::TimeDelta>& timeout,
1113 const StatusCallback& callback) { 1128 const StatusCallback& callback) {
1114 int request_id = 1129 int request_id;
1115 worker->StartRequest(ServiceWorkerMetrics::EventType::MESSAGE, callback); 1130 if (timeout) {
1131 request_id = worker->StartRequestWithCustomTimeout(
1132 ServiceWorkerMetrics::EventType::MESSAGE, callback, *timeout,
1133 ServiceWorkerVersion::CONTINUE_ON_TIMEOUT);
1134 } else {
1135 request_id = worker->StartRequest(ServiceWorkerMetrics::EventType::MESSAGE,
1136 callback);
1137 }
1116 1138
1117 MessagePortMessageFilter* filter = 1139 MessagePortMessageFilter* filter =
1118 worker->embedded_worker()->message_port_message_filter(); 1140 worker->embedded_worker()->message_port_message_filter();
1119 std::vector<int> new_routing_ids; 1141 std::vector<int> new_routing_ids;
1120 filter->UpdateMessagePortsWithNewRoutes(sent_message_ports, &new_routing_ids); 1142 filter->UpdateMessagePortsWithNewRoutes(sent_message_ports, &new_routing_ids);
1121 1143
1122 ServiceWorkerMsg_ExtendableMessageEvent_Params params; 1144 ServiceWorkerMsg_ExtendableMessageEvent_Params params;
1123 params.message = message; 1145 params.message = message;
1124 params.source_origin = source_origin; 1146 params.source_origin = source_origin;
1125 params.message_ports = sent_message_ports; 1147 params.message_ports = sent_message_ports;
(...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after
1656 if (!handle) { 1678 if (!handle) {
1657 bad_message::ReceivedBadMessage(this, 1679 bad_message::ReceivedBadMessage(this,
1658 bad_message::SWDH_TERMINATE_BAD_HANDLE); 1680 bad_message::SWDH_TERMINATE_BAD_HANDLE);
1659 return; 1681 return;
1660 } 1682 }
1661 handle->version()->StopWorker( 1683 handle->version()->StopWorker(
1662 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); 1684 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
1663 } 1685 }
1664 1686
1665 } // namespace content 1687 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698