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

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

Issue 1210643002: Update navigator.services API to use the new services.onconnect event [2/3]. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@serviceport
Patch Set: decouple renderer side code more Created 5 years, 5 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_version.h" 5 #include "content/browser/service_worker/service_worker_version.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/location.h" 8 #include "base/location.h"
9 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
10 #include "base/metrics/histogram_macros.h" 10 #include "base/metrics/histogram_macros.h"
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 const std::vector<TransferredMessagePort>& sent_message_ports, 155 const std::vector<TransferredMessagePort>& sent_message_ports,
156 const ServiceWorkerVersion::StatusCallback& callback, 156 const ServiceWorkerVersion::StatusCallback& callback,
157 ServiceWorkerStatusCode status) { 157 ServiceWorkerStatusCode status) {
158 // Transfering the message ports failed, so destroy the ports. 158 // Transfering the message ports failed, so destroy the ports.
159 for (const TransferredMessagePort& port : sent_message_ports) { 159 for (const TransferredMessagePort& port : sent_message_ports) {
160 MessagePortService::GetInstance()->ClosePort(port.id); 160 MessagePortService::GetInstance()->ClosePort(port.id);
161 } 161 }
162 callback.Run(status); 162 callback.Run(status);
163 } 163 }
164 164
165 void RunErrorCrossOriginConnectCallback( 165 void RunErrorServicePortConnectCallback(
166 const ServiceWorkerVersion::CrossOriginConnectCallback& callback, 166 const ServiceWorkerVersion::ServicePortConnectCallback& callback,
167 ServiceWorkerStatusCode status) { 167 ServiceWorkerStatusCode status) {
168 callback.Run(status, false /* accept_connection */); 168 callback.Run(status, false /* accept_connection */, base::string16(),
169 base::string16());
169 } 170 }
170 171
171 void RunErrorSendStashedPortsCallback( 172 void RunErrorSendStashedPortsCallback(
172 const ServiceWorkerVersion::SendStashedPortsCallback& callback, 173 const ServiceWorkerVersion::SendStashedPortsCallback& callback,
173 ServiceWorkerStatusCode status) { 174 ServiceWorkerStatusCode status) {
174 callback.Run(status, std::vector<int>()); 175 callback.Run(status, std::vector<int>());
175 } 176 }
176 177
177 using WindowOpenedCallback = base::Callback<void(int, int)>; 178 using WindowOpenedCallback = base::Callback<void(int, int)>;
178 179
(...skipping 711 matching lines...) Expand 10 before | Expand all | Expand 10 after
890 AddRequest(callback, &geofencing_callbacks_, REQUEST_GEOFENCING); 891 AddRequest(callback, &geofencing_callbacks_, REQUEST_GEOFENCING);
891 ServiceWorkerStatusCode status = 892 ServiceWorkerStatusCode status =
892 embedded_worker_->SendMessage(ServiceWorkerMsg_GeofencingEvent( 893 embedded_worker_->SendMessage(ServiceWorkerMsg_GeofencingEvent(
893 request_id, event_type, region_id, region)); 894 request_id, event_type, region_id, region));
894 if (status != SERVICE_WORKER_OK) { 895 if (status != SERVICE_WORKER_OK) {
895 geofencing_callbacks_.Remove(request_id); 896 geofencing_callbacks_.Remove(request_id);
896 RunSoon(base::Bind(callback, status)); 897 RunSoon(base::Bind(callback, status));
897 } 898 }
898 } 899 }
899 900
900 void ServiceWorkerVersion::DispatchCrossOriginConnectEvent( 901 void ServiceWorkerVersion::DispatchServicePortConnectEvent(
901 const CrossOriginConnectCallback& callback, 902 const ServicePortConnectCallback& callback,
902 const NavigatorConnectClient& client) { 903 const GURL& target_url,
904 const GURL& origin,
905 int port_id) {
903 DCHECK_EQ(ACTIVATED, status()) << status(); 906 DCHECK_EQ(ACTIVATED, status()) << status();
904 907
905 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( 908 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
906 switches::kEnableExperimentalWebPlatformFeatures)) { 909 switches::kEnableExperimentalWebPlatformFeatures)) {
907 callback.Run(SERVICE_WORKER_ERROR_ABORT, false); 910 callback.Run(SERVICE_WORKER_ERROR_ABORT, false, base::string16(),
911 base::string16());
908 return; 912 return;
909 } 913 }
910 914
911 if (running_status() != RUNNING) { 915 if (running_status() != RUNNING) {
912 // Schedule calling this method after starting the worker. 916 // Schedule calling this method after starting the worker.
913 StartWorker( 917 StartWorker(
914 base::Bind(&RunTaskAfterStartWorker, weak_factory_.GetWeakPtr(), 918 base::Bind(&RunTaskAfterStartWorker, weak_factory_.GetWeakPtr(),
915 base::Bind(&RunErrorCrossOriginConnectCallback, callback), 919 base::Bind(&RunErrorServicePortConnectCallback, callback),
916 base::Bind(&self::DispatchCrossOriginConnectEvent, 920 base::Bind(&self::DispatchServicePortConnectEvent,
917 weak_factory_.GetWeakPtr(), callback, client))); 921 weak_factory_.GetWeakPtr(), callback, target_url,
922 origin, port_id)));
918 return; 923 return;
919 } 924 }
920 925
921 int request_id = AddRequest(callback, &cross_origin_connect_callbacks_, 926 int request_id = AddRequest(callback, &service_port_connect_callbacks_,
922 REQUEST_CROSS_ORIGIN_CONNECT); 927 REQUEST_SERVICE_PORT_CONNECT);
923 ServiceWorkerStatusCode status = embedded_worker_->SendMessage( 928 if (!service_port_dispatcher_) {
924 ServiceWorkerMsg_CrossOriginConnectEvent(request_id, client)); 929 embedded_worker_->GetServiceRegistry()->ConnectToRemoteService(
iclelland 2015/07/08 02:59:29 This looks to me like it has the same problem as o
925 if (status != SERVICE_WORKER_OK) { 930 mojo::GetProxy(&service_port_dispatcher_));
926 cross_origin_connect_callbacks_.Remove(request_id); 931 service_port_dispatcher_.set_connection_error_handler(
927 RunSoon(base::Bind(callback, status, false)); 932 base::Bind(&ServiceWorkerVersion::OnMojoConnectionError<
933 ServicePortDispatcher, ServicePortConnectCallback>,
934 weak_factory_.GetWeakPtr(), &service_port_dispatcher_,
935 &service_port_connect_callbacks_,
936 base::Bind(&RunErrorServicePortConnectCallback)));
928 } 937 }
938 service_port_dispatcher_->Connect(
michaeln 2015/07/07 22:13:17 What happens to service_port_dispatcher_ when the
Marijn Kruisselbrink 2015/07/07 23:58:42 Hmm, good question. I seem to have assumed that th
iclelland 2015/07/08 02:59:29 I took a closer look at this last week, since I ha
939 mojo::String::From(target_url), mojo::String::From(origin), port_id,
940 base::Bind(&ServiceWorkerVersion::OnServicePortConnectEventFinished,
941 weak_factory_.GetWeakPtr(), request_id));
929 } 942 }
930 943
931 void ServiceWorkerVersion::DispatchCrossOriginMessageEvent( 944 void ServiceWorkerVersion::DispatchCrossOriginMessageEvent(
932 const NavigatorConnectClient& client, 945 const NavigatorConnectClient& client,
933 const base::string16& message, 946 const base::string16& message,
934 const std::vector<TransferredMessagePort>& sent_message_ports, 947 const std::vector<TransferredMessagePort>& sent_message_ports,
935 const StatusCallback& callback) { 948 const StatusCallback& callback) {
936 // Unlike in the case of DispatchMessageEvent, here the caller is assumed to 949 // Unlike in the case of DispatchMessageEvent, here the caller is assumed to
937 // have already put all the sent message ports on hold. So no need to do that 950 // have already put all the sent message ports on hold. So no need to do that
938 // here again. 951 // here again.
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
1186 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FetchEventFinished, 1199 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FetchEventFinished,
1187 OnFetchEventFinished) 1200 OnFetchEventFinished)
1188 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SyncEventFinished, 1201 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SyncEventFinished,
1189 OnSyncEventFinished) 1202 OnSyncEventFinished)
1190 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_NotificationClickEventFinished, 1203 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_NotificationClickEventFinished,
1191 OnNotificationClickEventFinished) 1204 OnNotificationClickEventFinished)
1192 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PushEventFinished, 1205 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PushEventFinished,
1193 OnPushEventFinished) 1206 OnPushEventFinished)
1194 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GeofencingEventFinished, 1207 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GeofencingEventFinished,
1195 OnGeofencingEventFinished) 1208 OnGeofencingEventFinished)
1196 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CrossOriginConnectEventFinished,
1197 OnCrossOriginConnectEventFinished)
1198 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_OpenWindow, 1209 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_OpenWindow,
1199 OnOpenWindow) 1210 OnOpenWindow)
1200 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SetCachedMetadata, 1211 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SetCachedMetadata,
1201 OnSetCachedMetadata) 1212 OnSetCachedMetadata)
1202 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ClearCachedMetadata, 1213 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ClearCachedMetadata,
1203 OnClearCachedMetadata) 1214 OnClearCachedMetadata)
1204 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToClient, 1215 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToClient,
1205 OnPostMessageToClient) 1216 OnPostMessageToClient)
1206 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FocusClient, 1217 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FocusClient,
1207 OnFocusClient) 1218 OnFocusClient)
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
1430 if (!callback) { 1441 if (!callback) {
1431 NOTREACHED() << "Got unexpected message: " << request_id; 1442 NOTREACHED() << "Got unexpected message: " << request_id;
1432 return; 1443 return;
1433 } 1444 }
1434 1445
1435 scoped_refptr<ServiceWorkerVersion> protect(this); 1446 scoped_refptr<ServiceWorkerVersion> protect(this);
1436 callback->Run(SERVICE_WORKER_OK); 1447 callback->Run(SERVICE_WORKER_OK);
1437 RemoveCallbackAndStopIfRedundant(&geofencing_callbacks_, request_id); 1448 RemoveCallbackAndStopIfRedundant(&geofencing_callbacks_, request_id);
1438 } 1449 }
1439 1450
1440 void ServiceWorkerVersion::OnCrossOriginConnectEventFinished( 1451 void ServiceWorkerVersion::OnServicePortConnectEventFinished(
1441 int request_id, 1452 int request_id,
1442 bool accept_connection) { 1453 ServicePortConnectResult result,
1454 const mojo::String& name,
1455 const mojo::String& data) {
1443 TRACE_EVENT1("ServiceWorker", 1456 TRACE_EVENT1("ServiceWorker",
1444 "ServiceWorkerVersion::OnCrossOriginConnectEventFinished", 1457 "ServiceWorkerVersion::OnServicePortConnectEventFinished",
1445 "Request id", request_id); 1458 "Request id", request_id);
1446 CrossOriginConnectCallback* callback = 1459 ServicePortConnectCallback* callback =
1447 cross_origin_connect_callbacks_.Lookup(request_id); 1460 service_port_connect_callbacks_.Lookup(request_id);
1448 if (!callback) { 1461 if (!callback) {
1449 NOTREACHED() << "Got unexpected message: " << request_id; 1462 NOTREACHED() << "Got unexpected message: " << request_id;
1450 return; 1463 return;
1451 } 1464 }
1452 1465
1453 scoped_refptr<ServiceWorkerVersion> protect(this); 1466 scoped_refptr<ServiceWorkerVersion> protect(this);
1454 callback->Run(SERVICE_WORKER_OK, accept_connection); 1467 callback->Run(SERVICE_WORKER_OK, result == SERVICE_PORT_CONNECT_RESULT_ACCEPT,
1455 RemoveCallbackAndStopIfRedundant(&cross_origin_connect_callbacks_, 1468 name.To<base::string16>(), data.To<base::string16>());
1469 RemoveCallbackAndStopIfRedundant(&service_port_connect_callbacks_,
1456 request_id); 1470 request_id);
1457 } 1471 }
1458 1472
1459 void ServiceWorkerVersion::OnOpenWindow(int request_id, GURL url) { 1473 void ServiceWorkerVersion::OnOpenWindow(int request_id, GURL url) {
1460 // Just abort if we are shutting down. 1474 // Just abort if we are shutting down.
1461 if (!context_) 1475 if (!context_)
1462 return; 1476 return;
1463 1477
1464 if (!url.is_valid()) { 1478 if (!url.is_valid()) {
1465 DVLOG(1) << "Received unexpected invalid URL from renderer process."; 1479 DVLOG(1) << "Received unexpected invalid URL from renderer process.";
(...skipping 477 matching lines...) Expand 10 before | Expand all | Expand 10 after
1943 return; 1957 return;
1944 } 1958 }
1945 1959
1946 // TODO(falken): We may need to handle StopIfIdle failure and 1960 // TODO(falken): We may need to handle StopIfIdle failure and
1947 // forcibly fail pending callbacks so no one is stuck waiting 1961 // forcibly fail pending callbacks so no one is stuck waiting
1948 // for the worker. 1962 // for the worker.
1949 embedded_worker_->StopIfIdle(); 1963 embedded_worker_->StopIfIdle();
1950 } 1964 }
1951 1965
1952 bool ServiceWorkerVersion::HasInflightRequests() const { 1966 bool ServiceWorkerVersion::HasInflightRequests() const {
1953 return 1967 return !activate_callbacks_.IsEmpty() || !install_callbacks_.IsEmpty() ||
1954 !activate_callbacks_.IsEmpty() || 1968 !fetch_callbacks_.IsEmpty() || !sync_callbacks_.IsEmpty() ||
1955 !install_callbacks_.IsEmpty() || 1969 !notification_click_callbacks_.IsEmpty() ||
1956 !fetch_callbacks_.IsEmpty() || 1970 !push_callbacks_.IsEmpty() || !geofencing_callbacks_.IsEmpty() ||
1957 !sync_callbacks_.IsEmpty() || 1971 !service_port_connect_callbacks_.IsEmpty() ||
1958 !notification_click_callbacks_.IsEmpty() || 1972 !streaming_url_request_jobs_.empty();
1959 !push_callbacks_.IsEmpty() ||
1960 !geofencing_callbacks_.IsEmpty() ||
1961 !cross_origin_connect_callbacks_.IsEmpty() ||
1962 !streaming_url_request_jobs_.empty();
1963 } 1973 }
1964 1974
1965 void ServiceWorkerVersion::RecordStartWorkerResult( 1975 void ServiceWorkerVersion::RecordStartWorkerResult(
1966 ServiceWorkerStatusCode status) { 1976 ServiceWorkerStatusCode status) {
1967 base::TimeTicks start_time = start_time_; 1977 base::TimeTicks start_time = start_time_;
1968 ClearTick(&start_time_); 1978 ClearTick(&start_time_);
1969 1979
1970 ServiceWorkerMetrics::RecordStartWorkerStatus(status, 1980 ServiceWorkerMetrics::RecordStartWorkerStatus(status,
1971 IsInstalled(prestart_status_)); 1981 IsInstalled(prestart_status_));
1972 1982
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
2040 SERVICE_WORKER_ERROR_TIMEOUT); 2050 SERVICE_WORKER_ERROR_TIMEOUT);
2041 case REQUEST_NOTIFICATION_CLICK: 2051 case REQUEST_NOTIFICATION_CLICK:
2042 return RunIDMapCallback(&notification_click_callbacks_, info.id, 2052 return RunIDMapCallback(&notification_click_callbacks_, info.id,
2043 SERVICE_WORKER_ERROR_TIMEOUT); 2053 SERVICE_WORKER_ERROR_TIMEOUT);
2044 case REQUEST_PUSH: 2054 case REQUEST_PUSH:
2045 return RunIDMapCallback(&push_callbacks_, info.id, 2055 return RunIDMapCallback(&push_callbacks_, info.id,
2046 SERVICE_WORKER_ERROR_TIMEOUT); 2056 SERVICE_WORKER_ERROR_TIMEOUT);
2047 case REQUEST_GEOFENCING: 2057 case REQUEST_GEOFENCING:
2048 return RunIDMapCallback(&geofencing_callbacks_, info.id, 2058 return RunIDMapCallback(&geofencing_callbacks_, info.id,
2049 SERVICE_WORKER_ERROR_TIMEOUT); 2059 SERVICE_WORKER_ERROR_TIMEOUT);
2050 case REQUEST_CROSS_ORIGIN_CONNECT: 2060 case REQUEST_SERVICE_PORT_CONNECT:
2051 return RunIDMapCallback(&cross_origin_connect_callbacks_, info.id, 2061 return RunIDMapCallback(&service_port_connect_callbacks_, info.id,
2052 SERVICE_WORKER_ERROR_TIMEOUT, 2062 SERVICE_WORKER_ERROR_TIMEOUT,
2053 false /* accept_connection */); 2063 false /* accept_connection */, base::string16(),
2064 base::string16());
2054 } 2065 }
2055 NOTREACHED() << "Got unexpected request type: " << info.type; 2066 NOTREACHED() << "Got unexpected request type: " << info.type;
2056 return false; 2067 return false;
2057 } 2068 }
2058 2069
2059 void ServiceWorkerVersion::SetAllRequestTimes(const base::TimeTicks& ticks) { 2070 void ServiceWorkerVersion::SetAllRequestTimes(const base::TimeTicks& ticks) {
2060 std::queue<RequestInfo> new_requests; 2071 std::queue<RequestInfo> new_requests;
2061 while (!requests_.empty()) { 2072 while (!requests_.empty()) {
2062 RequestInfo info = requests_.front(); 2073 RequestInfo info = requests_.front();
2063 info.time = ticks; 2074 info.time = ticks;
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
2158 RunIDMapCallbacks(&install_callbacks_, 2169 RunIDMapCallbacks(&install_callbacks_,
2159 SERVICE_WORKER_ERROR_INSTALL_WORKER_FAILED); 2170 SERVICE_WORKER_ERROR_INSTALL_WORKER_FAILED);
2160 RunIDMapCallbacks(&fetch_callbacks_, SERVICE_WORKER_ERROR_FAILED, 2171 RunIDMapCallbacks(&fetch_callbacks_, SERVICE_WORKER_ERROR_FAILED,
2161 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, 2172 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK,
2162 ServiceWorkerResponse()); 2173 ServiceWorkerResponse());
2163 RunIDMapCallbacks(&sync_callbacks_, SERVICE_WORKER_ERROR_FAILED); 2174 RunIDMapCallbacks(&sync_callbacks_, SERVICE_WORKER_ERROR_FAILED);
2164 RunIDMapCallbacks(&notification_click_callbacks_, 2175 RunIDMapCallbacks(&notification_click_callbacks_,
2165 SERVICE_WORKER_ERROR_FAILED); 2176 SERVICE_WORKER_ERROR_FAILED);
2166 RunIDMapCallbacks(&push_callbacks_, SERVICE_WORKER_ERROR_FAILED); 2177 RunIDMapCallbacks(&push_callbacks_, SERVICE_WORKER_ERROR_FAILED);
2167 RunIDMapCallbacks(&geofencing_callbacks_, SERVICE_WORKER_ERROR_FAILED); 2178 RunIDMapCallbacks(&geofencing_callbacks_, SERVICE_WORKER_ERROR_FAILED);
2168 RunIDMapCallbacks(&cross_origin_connect_callbacks_, 2179 RunIDMapCallbacks(&service_port_connect_callbacks_,
2169 SERVICE_WORKER_ERROR_FAILED, false); 2180 SERVICE_WORKER_ERROR_FAILED, false, base::string16(),
2181 base::string16());
2170 2182
2171 streaming_url_request_jobs_.clear(); 2183 streaming_url_request_jobs_.clear();
2172 2184
2173 FOR_EACH_OBSERVER(Listener, listeners_, OnRunningStateChanged(this)); 2185 FOR_EACH_OBSERVER(Listener, listeners_, OnRunningStateChanged(this));
2174 2186
2175 if (should_restart) 2187 if (should_restart)
2176 StartWorkerInternal(false /* pause_after_download */); 2188 StartWorkerInternal(false /* pause_after_download */);
2177 } 2189 }
2178 2190
2191 template <typename Interface, typename Callback>
2192 void ServiceWorkerVersion::OnMojoConnectionError(
2193 mojo::InterfacePtr<Interface>* interface,
2194 IDMap<Callback, IDMapOwnPointer>* callbacks,
2195 const base::Callback<void(const Callback&, ServiceWorkerStatusCode)>
2196 callback) {
2197 typename IDMap<Callback, IDMapOwnPointer>::iterator iter(callbacks);
2198 while (!iter.IsAtEnd()) {
2199 callback.Run(*iter.GetCurrentValue(), SERVICE_WORKER_ERROR_FAILED);
2200 iter.Advance();
2201 }
2202 callbacks->Clear();
2203 }
2204
2179 } // namespace content 2205 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698