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

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: rebase Created 4 years 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 926 matching lines...) Expand 10 before | Expand all | Expand 10 after
937 switch (sender_provider_host->provider_type()) { 937 switch (sender_provider_host->provider_type()) {
938 case SERVICE_WORKER_PROVIDER_FOR_WINDOW: 938 case SERVICE_WORKER_PROVIDER_FOR_WINDOW:
939 case SERVICE_WORKER_PROVIDER_FOR_WORKER: 939 case SERVICE_WORKER_PROVIDER_FOR_WORKER:
940 case SERVICE_WORKER_PROVIDER_FOR_SHARED_WORKER: 940 case SERVICE_WORKER_PROVIDER_FOR_SHARED_WORKER:
941 service_worker_client_utils::GetClient( 941 service_worker_client_utils::GetClient(
942 sender_provider_host, 942 sender_provider_host,
943 base::Bind(&ServiceWorkerDispatcherHost:: 943 base::Bind(&ServiceWorkerDispatcherHost::
944 DispatchExtendableMessageEventInternal< 944 DispatchExtendableMessageEventInternal<
945 ServiceWorkerClientInfo>, 945 ServiceWorkerClientInfo>,
946 this, worker, message, source_origin, sent_message_ports, 946 this, worker, message, source_origin, sent_message_ports,
947 callback)); 947 base::nullopt, callback));
948 break; 948 break;
949 case SERVICE_WORKER_PROVIDER_FOR_CONTROLLER: 949 case SERVICE_WORKER_PROVIDER_FOR_CONTROLLER: {
950 // Clamp timeout to the sending worker's remaining timeout, to prevent
951 // postMessage from keeping workers alive forever.
952 base::TimeDelta timeout =
953 sender_provider_host->running_hosted_version()->remaining_timeout();
950 RunSoon(base::Bind( 954 RunSoon(base::Bind(
951 &ServiceWorkerDispatcherHost::DispatchExtendableMessageEventInternal< 955 &ServiceWorkerDispatcherHost::DispatchExtendableMessageEventInternal<
952 ServiceWorkerObjectInfo>, 956 ServiceWorkerObjectInfo>,
953 this, worker, message, source_origin, sent_message_ports, callback, 957 this, worker, message, source_origin, sent_message_ports,
958 base::make_optional(timeout), callback,
954 sender_provider_host->GetOrCreateServiceWorkerHandle( 959 sender_provider_host->GetOrCreateServiceWorkerHandle(
955 sender_provider_host->running_hosted_version()))); 960 sender_provider_host->running_hosted_version())));
956 break; 961 break;
962 }
957 case SERVICE_WORKER_PROVIDER_UNKNOWN: 963 case SERVICE_WORKER_PROVIDER_UNKNOWN:
958 default: 964 default:
959 NOTREACHED() << sender_provider_host->provider_type(); 965 NOTREACHED() << sender_provider_host->provider_type();
960 break; 966 break;
961 } 967 }
962 } 968 }
963 969
964 void ServiceWorkerDispatcherHost::OnProviderCreated( 970 void ServiceWorkerDispatcherHost::OnProviderCreated(
965 int provider_id, 971 int provider_id,
966 int route_id, 972 int route_id,
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
1100 Send(new ServiceWorkerMsg_AssociateRegistration(kDocumentMainThreadId, 1106 Send(new ServiceWorkerMsg_AssociateRegistration(kDocumentMainThreadId,
1101 provider_id, info, attrs)); 1107 provider_id, info, attrs));
1102 } 1108 }
1103 1109
1104 template <typename SourceInfo> 1110 template <typename SourceInfo>
1105 void ServiceWorkerDispatcherHost::DispatchExtendableMessageEventInternal( 1111 void ServiceWorkerDispatcherHost::DispatchExtendableMessageEventInternal(
1106 scoped_refptr<ServiceWorkerVersion> worker, 1112 scoped_refptr<ServiceWorkerVersion> worker,
1107 const base::string16& message, 1113 const base::string16& message,
1108 const url::Origin& source_origin, 1114 const url::Origin& source_origin,
1109 const std::vector<int>& sent_message_ports, 1115 const std::vector<int>& sent_message_ports,
1116 const base::Optional<base::TimeDelta>& timeout,
1110 const StatusCallback& callback, 1117 const StatusCallback& callback,
1111 const SourceInfo& source_info) { 1118 const SourceInfo& source_info) {
1112 if (!source_info.IsValid()) { 1119 if (!source_info.IsValid()) {
1113 DidFailToDispatchExtendableMessageEvent<SourceInfo>( 1120 DidFailToDispatchExtendableMessageEvent<SourceInfo>(
1114 sent_message_ports, source_info, callback, SERVICE_WORKER_ERROR_FAILED); 1121 sent_message_ports, source_info, callback, SERVICE_WORKER_ERROR_FAILED);
1115 return; 1122 return;
1116 } 1123 }
1124
1125 // If not enough time is left to actually process the event don't even
1126 // bother starting the worker and sending the event.
1127 if (timeout && *timeout < base::TimeDelta::FromMilliseconds(100)) {
1128 DidFailToDispatchExtendableMessageEvent<SourceInfo>(
1129 sent_message_ports, source_info, callback,
1130 SERVICE_WORKER_ERROR_TIMEOUT);
1131 return;
1132 }
1133
1117 worker->RunAfterStartWorker( 1134 worker->RunAfterStartWorker(
1118 ServiceWorkerMetrics::EventType::MESSAGE, 1135 ServiceWorkerMetrics::EventType::MESSAGE,
1119 base::Bind(&ServiceWorkerDispatcherHost:: 1136 base::Bind(&ServiceWorkerDispatcherHost::
1120 DispatchExtendableMessageEventAfterStartWorker, 1137 DispatchExtendableMessageEventAfterStartWorker,
1121 this, worker, message, source_origin, sent_message_ports, 1138 this, worker, message, source_origin, sent_message_ports,
1122 ExtendableMessageEventSource(source_info), callback), 1139 ExtendableMessageEventSource(source_info), timeout, callback),
1123 base::Bind( 1140 base::Bind(
1124 &ServiceWorkerDispatcherHost::DidFailToDispatchExtendableMessageEvent< 1141 &ServiceWorkerDispatcherHost::DidFailToDispatchExtendableMessageEvent<
1125 SourceInfo>, 1142 SourceInfo>,
1126 this, sent_message_ports, source_info, callback)); 1143 this, sent_message_ports, source_info, callback));
1127 } 1144 }
1128 1145
1129 void ServiceWorkerDispatcherHost:: 1146 void ServiceWorkerDispatcherHost::
1130 DispatchExtendableMessageEventAfterStartWorker( 1147 DispatchExtendableMessageEventAfterStartWorker(
1131 scoped_refptr<ServiceWorkerVersion> worker, 1148 scoped_refptr<ServiceWorkerVersion> worker,
1132 const base::string16& message, 1149 const base::string16& message,
1133 const url::Origin& source_origin, 1150 const url::Origin& source_origin,
1134 const std::vector<int>& sent_message_ports, 1151 const std::vector<int>& sent_message_ports,
1135 const ExtendableMessageEventSource& source, 1152 const ExtendableMessageEventSource& source,
1153 const base::Optional<base::TimeDelta>& timeout,
1136 const StatusCallback& callback) { 1154 const StatusCallback& callback) {
1137 int request_id = 1155 int request_id;
1138 worker->StartRequest(ServiceWorkerMetrics::EventType::MESSAGE, callback); 1156 if (timeout) {
1157 request_id = worker->StartRequestWithCustomTimeout(
1158 ServiceWorkerMetrics::EventType::MESSAGE, callback, *timeout,
1159 ServiceWorkerVersion::CONTINUE_ON_TIMEOUT);
1160 } else {
1161 request_id = worker->StartRequest(ServiceWorkerMetrics::EventType::MESSAGE,
1162 callback);
1163 }
1139 1164
1140 MessagePortMessageFilter* filter = 1165 MessagePortMessageFilter* filter =
1141 worker->embedded_worker()->message_port_message_filter(); 1166 worker->embedded_worker()->message_port_message_filter();
1142 std::vector<int> new_routing_ids; 1167 std::vector<int> new_routing_ids;
1143 filter->UpdateMessagePortsWithNewRoutes(sent_message_ports, &new_routing_ids); 1168 filter->UpdateMessagePortsWithNewRoutes(sent_message_ports, &new_routing_ids);
1144 1169
1145 ServiceWorkerMsg_ExtendableMessageEvent_Params params; 1170 ServiceWorkerMsg_ExtendableMessageEvent_Params params;
1146 params.message = message; 1171 params.message = message;
1147 params.source_origin = source_origin; 1172 params.source_origin = source_origin;
1148 params.message_ports = sent_message_ports; 1173 params.message_ports = sent_message_ports;
(...skipping 571 matching lines...) Expand 10 before | Expand all | Expand 10 after
1720 if (!handle) { 1745 if (!handle) {
1721 bad_message::ReceivedBadMessage(this, 1746 bad_message::ReceivedBadMessage(this,
1722 bad_message::SWDH_TERMINATE_BAD_HANDLE); 1747 bad_message::SWDH_TERMINATE_BAD_HANDLE);
1723 return; 1748 return;
1724 } 1749 }
1725 handle->version()->StopWorker( 1750 handle->version()->StopWorker(
1726 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); 1751 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
1727 } 1752 }
1728 1753
1729 } // namespace content 1754 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698