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

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: properly run error callbacks when worker is stopped 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 <map> 7 #include <map>
8 #include <string> 8 #include <string>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 24 matching lines...) Expand all
35 #include "content/public/browser/content_browser_client.h" 35 #include "content/public/browser/content_browser_client.h"
36 #include "content/public/browser/page_navigator.h" 36 #include "content/public/browser/page_navigator.h"
37 #include "content/public/browser/render_frame_host.h" 37 #include "content/public/browser/render_frame_host.h"
38 #include "content/public/browser/render_process_host.h" 38 #include "content/public/browser/render_process_host.h"
39 #include "content/public/browser/web_contents.h" 39 #include "content/public/browser/web_contents.h"
40 #include "content/public/browser/web_contents_observer.h" 40 #include "content/public/browser/web_contents_observer.h"
41 #include "content/public/common/child_process_host.h" 41 #include "content/public/common/child_process_host.h"
42 #include "content/public/common/content_client.h" 42 #include "content/public/common/content_client.h"
43 #include "content/public/common/content_switches.h" 43 #include "content/public/common/content_switches.h"
44 #include "content/public/common/result_codes.h" 44 #include "content/public/common/result_codes.h"
45 #include "mojo/common/common_type_converters.h"
46 #include "mojo/common/url_type_converters.h"
45 #include "net/http/http_response_headers.h" 47 #include "net/http/http_response_headers.h"
46 #include "net/http/http_response_info.h" 48 #include "net/http/http_response_info.h"
47 49
48 namespace content { 50 namespace content {
49 51
50 using StatusCallback = ServiceWorkerVersion::StatusCallback; 52 using StatusCallback = ServiceWorkerVersion::StatusCallback;
51 using ServiceWorkerClients = std::vector<ServiceWorkerClientInfo>; 53 using ServiceWorkerClients = std::vector<ServiceWorkerClientInfo>;
52 using GetClientsCallback = 54 using GetClientsCallback =
53 base::Callback<void(scoped_ptr<ServiceWorkerClients>)>; 55 base::Callback<void(scoped_ptr<ServiceWorkerClients>)>;
54 56
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 const std::vector<TransferredMessagePort>& sent_message_ports, 160 const std::vector<TransferredMessagePort>& sent_message_ports,
159 const ServiceWorkerVersion::StatusCallback& callback, 161 const ServiceWorkerVersion::StatusCallback& callback,
160 ServiceWorkerStatusCode status) { 162 ServiceWorkerStatusCode status) {
161 // Transfering the message ports failed, so destroy the ports. 163 // Transfering the message ports failed, so destroy the ports.
162 for (const TransferredMessagePort& port : sent_message_ports) { 164 for (const TransferredMessagePort& port : sent_message_ports) {
163 MessagePortService::GetInstance()->ClosePort(port.id); 165 MessagePortService::GetInstance()->ClosePort(port.id);
164 } 166 }
165 callback.Run(status); 167 callback.Run(status);
166 } 168 }
167 169
168 void RunErrorCrossOriginConnectCallback( 170 void RunErrorServicePortConnectCallback(
169 const ServiceWorkerVersion::CrossOriginConnectCallback& callback, 171 const ServiceWorkerVersion::ServicePortConnectCallback& callback,
170 ServiceWorkerStatusCode status) { 172 ServiceWorkerStatusCode status) {
171 callback.Run(status, false /* accept_connection */); 173 callback.Run(status, false /* accept_connection */, base::string16(),
174 base::string16());
172 } 175 }
173 176
174 void RunErrorSendStashedPortsCallback( 177 void RunErrorSendStashedPortsCallback(
175 const ServiceWorkerVersion::SendStashedPortsCallback& callback, 178 const ServiceWorkerVersion::SendStashedPortsCallback& callback,
176 ServiceWorkerStatusCode status) { 179 ServiceWorkerStatusCode status) {
177 callback.Run(status, std::vector<int>()); 180 callback.Run(status, std::vector<int>());
178 } 181 }
179 182
180 using WindowOpenedCallback = base::Callback<void(int, int)>; 183 using WindowOpenedCallback = base::Callback<void(int, int)>;
181 184
(...skipping 705 matching lines...) Expand 10 before | Expand all | Expand 10 after
887 AddRequest(callback, &geofencing_requests_, REQUEST_GEOFENCING); 890 AddRequest(callback, &geofencing_requests_, REQUEST_GEOFENCING);
888 ServiceWorkerStatusCode status = 891 ServiceWorkerStatusCode status =
889 embedded_worker_->SendMessage(ServiceWorkerMsg_GeofencingEvent( 892 embedded_worker_->SendMessage(ServiceWorkerMsg_GeofencingEvent(
890 request_id, event_type, region_id, region)); 893 request_id, event_type, region_id, region));
891 if (status != SERVICE_WORKER_OK) { 894 if (status != SERVICE_WORKER_OK) {
892 geofencing_requests_.Remove(request_id); 895 geofencing_requests_.Remove(request_id);
893 RunSoon(base::Bind(callback, status)); 896 RunSoon(base::Bind(callback, status));
894 } 897 }
895 } 898 }
896 899
897 void ServiceWorkerVersion::DispatchCrossOriginConnectEvent( 900 void ServiceWorkerVersion::DispatchServicePortConnectEvent(
898 const CrossOriginConnectCallback& callback, 901 const ServicePortConnectCallback& callback,
899 const NavigatorConnectClient& client) { 902 const GURL& target_url,
903 const GURL& origin,
904 int port_id) {
900 DCHECK_EQ(ACTIVATED, status()) << status(); 905 DCHECK_EQ(ACTIVATED, status()) << status();
901 906
902 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( 907 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
903 switches::kEnableExperimentalWebPlatformFeatures)) { 908 switches::kEnableExperimentalWebPlatformFeatures)) {
904 callback.Run(SERVICE_WORKER_ERROR_ABORT, false); 909 callback.Run(SERVICE_WORKER_ERROR_ABORT, false, base::string16(),
910 base::string16());
905 return; 911 return;
906 } 912 }
907 913
908 if (running_status() != RUNNING) { 914 if (running_status() != RUNNING) {
909 // Schedule calling this method after starting the worker. 915 // Schedule calling this method after starting the worker.
910 StartWorker( 916 StartWorker(
911 base::Bind(&RunTaskAfterStartWorker, weak_factory_.GetWeakPtr(), 917 base::Bind(&RunTaskAfterStartWorker, weak_factory_.GetWeakPtr(),
912 base::Bind(&RunErrorCrossOriginConnectCallback, callback), 918 base::Bind(&RunErrorServicePortConnectCallback, callback),
913 base::Bind(&self::DispatchCrossOriginConnectEvent, 919 base::Bind(&self::DispatchServicePortConnectEvent,
914 weak_factory_.GetWeakPtr(), callback, client))); 920 weak_factory_.GetWeakPtr(), callback, target_url,
921 origin, port_id)));
915 return; 922 return;
916 } 923 }
917 924
918 int request_id = AddRequest(callback, &cross_origin_connect_requests_, 925 int request_id = AddRequest(callback, &service_port_connect_requests_,
919 REQUEST_CROSS_ORIGIN_CONNECT); 926 REQUEST_SERVICE_PORT_CONNECT);
920 ServiceWorkerStatusCode status = embedded_worker_->SendMessage( 927 if (!service_port_dispatcher_) {
921 ServiceWorkerMsg_CrossOriginConnectEvent(request_id, client)); 928 embedded_worker_->GetServiceRegistry()->ConnectToRemoteService(
922 if (status != SERVICE_WORKER_OK) { 929 mojo::GetProxy(&service_port_dispatcher_));
923 cross_origin_connect_requests_.Remove(request_id); 930 service_port_dispatcher_.set_connection_error_handler(base::Bind(
924 RunSoon(base::Bind(callback, status, false)); 931 &ServiceWorkerVersion::OnServicePortDispatcherConnectionError,
932 weak_factory_.GetWeakPtr()));
925 } 933 }
934 service_port_dispatcher_->Connect(
935 mojo::String::From(target_url), mojo::String::From(origin), port_id,
936 base::Bind(&ServiceWorkerVersion::OnServicePortConnectEventFinished,
937 weak_factory_.GetWeakPtr(), request_id));
926 } 938 }
927 939
928 void ServiceWorkerVersion::DispatchCrossOriginMessageEvent( 940 void ServiceWorkerVersion::DispatchCrossOriginMessageEvent(
929 const NavigatorConnectClient& client, 941 const NavigatorConnectClient& client,
930 const base::string16& message, 942 const base::string16& message,
931 const std::vector<TransferredMessagePort>& sent_message_ports, 943 const std::vector<TransferredMessagePort>& sent_message_ports,
932 const StatusCallback& callback) { 944 const StatusCallback& callback) {
933 // Unlike in the case of DispatchMessageEvent, here the caller is assumed to 945 // Unlike in the case of DispatchMessageEvent, here the caller is assumed to
934 // have already put all the sent message ports on hold. So no need to do that 946 // have already put all the sent message ports on hold. So no need to do that
935 // here again. 947 // here again.
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after
1196 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FetchEventFinished, 1208 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FetchEventFinished,
1197 OnFetchEventFinished) 1209 OnFetchEventFinished)
1198 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SyncEventFinished, 1210 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SyncEventFinished,
1199 OnSyncEventFinished) 1211 OnSyncEventFinished)
1200 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_NotificationClickEventFinished, 1212 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_NotificationClickEventFinished,
1201 OnNotificationClickEventFinished) 1213 OnNotificationClickEventFinished)
1202 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PushEventFinished, 1214 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PushEventFinished,
1203 OnPushEventFinished) 1215 OnPushEventFinished)
1204 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GeofencingEventFinished, 1216 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GeofencingEventFinished,
1205 OnGeofencingEventFinished) 1217 OnGeofencingEventFinished)
1206 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CrossOriginConnectEventFinished,
1207 OnCrossOriginConnectEventFinished)
1208 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_OpenWindow, 1218 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_OpenWindow,
1209 OnOpenWindow) 1219 OnOpenWindow)
1210 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SetCachedMetadata, 1220 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SetCachedMetadata,
1211 OnSetCachedMetadata) 1221 OnSetCachedMetadata)
1212 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ClearCachedMetadata, 1222 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ClearCachedMetadata,
1213 OnClearCachedMetadata) 1223 OnClearCachedMetadata)
1214 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToClient, 1224 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToClient,
1215 OnPostMessageToClient) 1225 OnPostMessageToClient)
1216 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FocusClient, 1226 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FocusClient,
1217 OnFocusClient) 1227 OnFocusClient)
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after
1455 if (!request) { 1465 if (!request) {
1456 NOTREACHED() << "Got unexpected message: " << request_id; 1466 NOTREACHED() << "Got unexpected message: " << request_id;
1457 return; 1467 return;
1458 } 1468 }
1459 1469
1460 scoped_refptr<ServiceWorkerVersion> protect(this); 1470 scoped_refptr<ServiceWorkerVersion> protect(this);
1461 request->callback.Run(SERVICE_WORKER_OK); 1471 request->callback.Run(SERVICE_WORKER_OK);
1462 RemoveCallbackAndStopIfRedundant(&geofencing_requests_, request_id); 1472 RemoveCallbackAndStopIfRedundant(&geofencing_requests_, request_id);
1463 } 1473 }
1464 1474
1465 void ServiceWorkerVersion::OnCrossOriginConnectEventFinished( 1475 void ServiceWorkerVersion::OnServicePortConnectEventFinished(
1466 int request_id, 1476 int request_id,
1467 bool accept_connection) { 1477 ServicePortConnectResult result,
1478 const mojo::String& name,
1479 const mojo::String& data) {
1468 TRACE_EVENT1("ServiceWorker", 1480 TRACE_EVENT1("ServiceWorker",
1469 "ServiceWorkerVersion::OnCrossOriginConnectEventFinished", 1481 "ServiceWorkerVersion::OnServicePortConnectEventFinished",
1470 "Request id", request_id); 1482 "Request id", request_id);
1471 PendingRequest<CrossOriginConnectCallback>* request = 1483 PendingRequest<ServicePortConnectCallback>* request =
1472 cross_origin_connect_requests_.Lookup(request_id); 1484 service_port_connect_requests_.Lookup(request_id);
1473 if (!request) { 1485 if (!request) {
1474 NOTREACHED() << "Got unexpected message: " << request_id; 1486 NOTREACHED() << "Got unexpected message: " << request_id;
1475 return; 1487 return;
1476 } 1488 }
1477 1489
1478 scoped_refptr<ServiceWorkerVersion> protect(this); 1490 scoped_refptr<ServiceWorkerVersion> protect(this);
1479 request->callback.Run(SERVICE_WORKER_OK, accept_connection); 1491 request->callback.Run(SERVICE_WORKER_OK,
1480 RemoveCallbackAndStopIfRedundant(&cross_origin_connect_requests_, request_id); 1492 result == SERVICE_PORT_CONNECT_RESULT_ACCEPT,
1493 name.To<base::string16>(), data.To<base::string16>());
1494 RemoveCallbackAndStopIfRedundant(&service_port_connect_requests_, request_id);
1481 } 1495 }
1482 1496
1483 void ServiceWorkerVersion::OnOpenWindow(int request_id, GURL url) { 1497 void ServiceWorkerVersion::OnOpenWindow(int request_id, GURL url) {
1484 // Just abort if we are shutting down. 1498 // Just abort if we are shutting down.
1485 if (!context_) 1499 if (!context_)
1486 return; 1500 return;
1487 1501
1488 if (!url.is_valid()) { 1502 if (!url.is_valid()) {
1489 DVLOG(1) << "Received unexpected invalid URL from renderer process."; 1503 DVLOG(1) << "Received unexpected invalid URL from renderer process.";
1490 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 1504 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
(...skipping 482 matching lines...) Expand 10 before | Expand all | Expand 10 after
1973 // forcibly fail pending callbacks so no one is stuck waiting 1987 // forcibly fail pending callbacks so no one is stuck waiting
1974 // for the worker. 1988 // for the worker.
1975 embedded_worker_->StopIfIdle(); 1989 embedded_worker_->StopIfIdle();
1976 } 1990 }
1977 1991
1978 bool ServiceWorkerVersion::HasInflightRequests() const { 1992 bool ServiceWorkerVersion::HasInflightRequests() const {
1979 return !activate_requests_.IsEmpty() || !install_requests_.IsEmpty() || 1993 return !activate_requests_.IsEmpty() || !install_requests_.IsEmpty() ||
1980 !fetch_requests_.IsEmpty() || !sync_requests_.IsEmpty() || 1994 !fetch_requests_.IsEmpty() || !sync_requests_.IsEmpty() ||
1981 !notification_click_requests_.IsEmpty() || !push_requests_.IsEmpty() || 1995 !notification_click_requests_.IsEmpty() || !push_requests_.IsEmpty() ||
1982 !geofencing_requests_.IsEmpty() || 1996 !geofencing_requests_.IsEmpty() ||
1983 !cross_origin_connect_requests_.IsEmpty() || 1997 !service_port_connect_requests_.IsEmpty() ||
1984 !streaming_url_request_jobs_.empty(); 1998 !streaming_url_request_jobs_.empty();
1985 } 1999 }
1986 2000
1987 void ServiceWorkerVersion::RecordStartWorkerResult( 2001 void ServiceWorkerVersion::RecordStartWorkerResult(
1988 ServiceWorkerStatusCode status) { 2002 ServiceWorkerStatusCode status) {
1989 base::TimeTicks start_time = start_time_; 2003 base::TimeTicks start_time = start_time_;
1990 ClearTick(&start_time_); 2004 ClearTick(&start_time_);
1991 2005
1992 ServiceWorkerMetrics::RecordStartWorkerStatus(status, 2006 ServiceWorkerMetrics::RecordStartWorkerStatus(status,
1993 IsInstalled(prestart_status_)); 2007 IsInstalled(prestart_status_));
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
2064 SERVICE_WORKER_ERROR_TIMEOUT); 2078 SERVICE_WORKER_ERROR_TIMEOUT);
2065 case REQUEST_NOTIFICATION_CLICK: 2079 case REQUEST_NOTIFICATION_CLICK:
2066 return RunIDMapCallback(&notification_click_requests_, info.id, 2080 return RunIDMapCallback(&notification_click_requests_, info.id,
2067 SERVICE_WORKER_ERROR_TIMEOUT); 2081 SERVICE_WORKER_ERROR_TIMEOUT);
2068 case REQUEST_PUSH: 2082 case REQUEST_PUSH:
2069 return RunIDMapCallback(&push_requests_, info.id, 2083 return RunIDMapCallback(&push_requests_, info.id,
2070 SERVICE_WORKER_ERROR_TIMEOUT); 2084 SERVICE_WORKER_ERROR_TIMEOUT);
2071 case REQUEST_GEOFENCING: 2085 case REQUEST_GEOFENCING:
2072 return RunIDMapCallback(&geofencing_requests_, info.id, 2086 return RunIDMapCallback(&geofencing_requests_, info.id,
2073 SERVICE_WORKER_ERROR_TIMEOUT); 2087 SERVICE_WORKER_ERROR_TIMEOUT);
2074 case REQUEST_CROSS_ORIGIN_CONNECT: 2088 case REQUEST_SERVICE_PORT_CONNECT:
2075 return RunIDMapCallback(&cross_origin_connect_requests_, info.id, 2089 return RunIDMapCallback(&service_port_connect_requests_, info.id,
2076 SERVICE_WORKER_ERROR_TIMEOUT, 2090 SERVICE_WORKER_ERROR_TIMEOUT,
2077 false /* accept_connection */); 2091 false /* accept_connection */, base::string16(),
2092 base::string16());
2078 } 2093 }
2079 NOTREACHED() << "Got unexpected request type: " << info.type; 2094 NOTREACHED() << "Got unexpected request type: " << info.type;
2080 return false; 2095 return false;
2081 } 2096 }
2082 2097
2083 void ServiceWorkerVersion::SetAllRequestTimes(const base::TimeTicks& ticks) { 2098 void ServiceWorkerVersion::SetAllRequestTimes(const base::TimeTicks& ticks) {
2084 std::queue<RequestInfo> new_requests; 2099 std::queue<RequestInfo> new_requests;
2085 while (!requests_.empty()) { 2100 while (!requests_.empty()) {
2086 RequestInfo info = requests_.front(); 2101 RequestInfo info = requests_.front();
2087 info.time = ticks; 2102 info.time = ticks;
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
2184 SERVICE_WORKER_ERROR_ACTIVATE_WORKER_FAILED); 2199 SERVICE_WORKER_ERROR_ACTIVATE_WORKER_FAILED);
2185 RunIDMapCallbacks(&install_requests_, 2200 RunIDMapCallbacks(&install_requests_,
2186 SERVICE_WORKER_ERROR_INSTALL_WORKER_FAILED); 2201 SERVICE_WORKER_ERROR_INSTALL_WORKER_FAILED);
2187 RunIDMapCallbacks(&fetch_requests_, SERVICE_WORKER_ERROR_FAILED, 2202 RunIDMapCallbacks(&fetch_requests_, SERVICE_WORKER_ERROR_FAILED,
2188 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, 2203 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK,
2189 ServiceWorkerResponse()); 2204 ServiceWorkerResponse());
2190 RunIDMapCallbacks(&sync_requests_, SERVICE_WORKER_ERROR_FAILED); 2205 RunIDMapCallbacks(&sync_requests_, SERVICE_WORKER_ERROR_FAILED);
2191 RunIDMapCallbacks(&notification_click_requests_, SERVICE_WORKER_ERROR_FAILED); 2206 RunIDMapCallbacks(&notification_click_requests_, SERVICE_WORKER_ERROR_FAILED);
2192 RunIDMapCallbacks(&push_requests_, SERVICE_WORKER_ERROR_FAILED); 2207 RunIDMapCallbacks(&push_requests_, SERVICE_WORKER_ERROR_FAILED);
2193 RunIDMapCallbacks(&geofencing_requests_, SERVICE_WORKER_ERROR_FAILED); 2208 RunIDMapCallbacks(&geofencing_requests_, SERVICE_WORKER_ERROR_FAILED);
2194 RunIDMapCallbacks(&cross_origin_connect_requests_, 2209
2195 SERVICE_WORKER_ERROR_FAILED, false); 2210 // Close all mojo services. This will also fire and clear all callbacks
2211 // for messages that are still outstanding for those services.
2212 OnServicePortDispatcherConnectionError();
2196 2213
2197 streaming_url_request_jobs_.clear(); 2214 streaming_url_request_jobs_.clear();
2198 2215
2199 FOR_EACH_OBSERVER(Listener, listeners_, OnRunningStateChanged(this)); 2216 FOR_EACH_OBSERVER(Listener, listeners_, OnRunningStateChanged(this));
2200 2217
2201 if (should_restart) 2218 if (should_restart)
2202 StartWorkerInternal(); 2219 StartWorkerInternal();
2203 } 2220 }
2204 2221
2222 void ServiceWorkerVersion::OnServicePortDispatcherConnectionError() {
2223 RunIDMapCallbacks(&service_port_connect_requests_,
2224 SERVICE_WORKER_ERROR_FAILED, false, base::string16(),
2225 base::string16());
2226 service_port_dispatcher_.reset();
2227 }
2228
2205 } // namespace content 2229 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698