OLD | NEW |
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 13 matching lines...) Expand all Loading... |
24 #include "content/browser/service_worker/embedded_worker_instance.h" | 24 #include "content/browser/service_worker/embedded_worker_instance.h" |
25 #include "content/browser/service_worker/embedded_worker_registry.h" | 25 #include "content/browser/service_worker/embedded_worker_registry.h" |
26 #include "content/browser/service_worker/service_worker_context_core.h" | 26 #include "content/browser/service_worker/service_worker_context_core.h" |
27 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 27 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
28 #include "content/browser/service_worker/service_worker_metrics.h" | 28 #include "content/browser/service_worker/service_worker_metrics.h" |
29 #include "content/browser/service_worker/service_worker_registration.h" | 29 #include "content/browser/service_worker/service_worker_registration.h" |
30 #include "content/browser/service_worker/service_worker_utils.h" | 30 #include "content/browser/service_worker/service_worker_utils.h" |
31 #include "content/browser/service_worker/stashed_port_manager.h" | 31 #include "content/browser/service_worker/stashed_port_manager.h" |
32 #include "content/browser/storage_partition_impl.h" | 32 #include "content/browser/storage_partition_impl.h" |
33 #include "content/common/service_worker/service_worker_messages.h" | 33 #include "content/common/service_worker/service_worker_messages.h" |
| 34 #include "content/common/service_worker/service_worker_type_converters.h" |
34 #include "content/public/browser/browser_thread.h" | 35 #include "content/public/browser/browser_thread.h" |
35 #include "content/public/browser/content_browser_client.h" | 36 #include "content/public/browser/content_browser_client.h" |
36 #include "content/public/browser/page_navigator.h" | 37 #include "content/public/browser/page_navigator.h" |
37 #include "content/public/browser/render_frame_host.h" | 38 #include "content/public/browser/render_frame_host.h" |
38 #include "content/public/browser/render_process_host.h" | 39 #include "content/public/browser/render_process_host.h" |
39 #include "content/public/browser/web_contents.h" | 40 #include "content/public/browser/web_contents.h" |
40 #include "content/public/browser/web_contents_observer.h" | 41 #include "content/public/browser/web_contents_observer.h" |
41 #include "content/public/common/child_process_host.h" | 42 #include "content/public/common/child_process_host.h" |
42 #include "content/public/common/content_client.h" | 43 #include "content/public/common/content_client.h" |
43 #include "content/public/common/content_switches.h" | 44 #include "content/public/common/content_switches.h" |
44 #include "content/public/common/result_codes.h" | 45 #include "content/public/common/result_codes.h" |
| 46 #include "content/public/common/service_registry.h" |
45 #include "mojo/common/common_type_converters.h" | 47 #include "mojo/common/common_type_converters.h" |
46 #include "mojo/common/url_type_converters.h" | 48 #include "mojo/common/url_type_converters.h" |
47 #include "net/http/http_response_headers.h" | 49 #include "net/http/http_response_headers.h" |
48 #include "net/http/http_response_info.h" | 50 #include "net/http/http_response_info.h" |
49 | 51 |
50 namespace content { | 52 namespace content { |
51 | 53 |
52 using StatusCallback = ServiceWorkerVersion::StatusCallback; | 54 using StatusCallback = ServiceWorkerVersion::StatusCallback; |
53 using ServiceWorkerClients = std::vector<ServiceWorkerClientInfo>; | 55 using ServiceWorkerClients = std::vector<ServiceWorkerClientInfo>; |
54 using GetClientsCallback = | 56 using GetClientsCallback = |
(...skipping 741 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
796 // Schedule calling this method after starting the worker. | 798 // Schedule calling this method after starting the worker. |
797 StartWorker(base::Bind(&RunTaskAfterStartWorker, | 799 StartWorker(base::Bind(&RunTaskAfterStartWorker, |
798 weak_factory_.GetWeakPtr(), callback, | 800 weak_factory_.GetWeakPtr(), callback, |
799 base::Bind(&self::DispatchSyncEvent, | 801 base::Bind(&self::DispatchSyncEvent, |
800 weak_factory_.GetWeakPtr(), | 802 weak_factory_.GetWeakPtr(), |
801 callback))); | 803 callback))); |
802 return; | 804 return; |
803 } | 805 } |
804 | 806 |
805 int request_id = AddRequest(callback, &sync_requests_, REQUEST_SYNC); | 807 int request_id = AddRequest(callback, &sync_requests_, REQUEST_SYNC); |
806 ServiceWorkerStatusCode status = embedded_worker_->SendMessage( | 808 if (!background_sync_dispatcher_) { |
807 ServiceWorkerMsg_SyncEvent(request_id)); | 809 embedded_worker_->GetServiceRegistry()->ConnectToRemoteService( |
808 if (status != SERVICE_WORKER_OK) { | 810 mojo::GetProxy(&background_sync_dispatcher_)); |
809 sync_requests_.Remove(request_id); | 811 background_sync_dispatcher_.set_connection_error_handler(base::Bind( |
810 RunSoon(base::Bind(callback, status)); | 812 &ServiceWorkerVersion::OnBackgroundSyncDispatcherConnectionError, |
| 813 weak_factory_.GetWeakPtr())); |
811 } | 814 } |
| 815 |
| 816 // TODO(iclelland): Replace this with the real event registration details |
| 817 // crbug.com/482066 |
| 818 content::SyncRegistrationPtr null_event(content::SyncRegistration::New()); |
| 819 |
| 820 background_sync_dispatcher_->Sync( |
| 821 null_event.Pass(), base::Bind(&self::OnSyncEventFinished, |
| 822 weak_factory_.GetWeakPtr(), request_id)); |
812 } | 823 } |
813 | 824 |
814 void ServiceWorkerVersion::DispatchNotificationClickEvent( | 825 void ServiceWorkerVersion::DispatchNotificationClickEvent( |
815 const StatusCallback& callback, | 826 const StatusCallback& callback, |
816 int64_t persistent_notification_id, | 827 int64_t persistent_notification_id, |
817 const PlatformNotificationData& notification_data) { | 828 const PlatformNotificationData& notification_data) { |
818 DCHECK_EQ(ACTIVATED, status()) << status(); | 829 DCHECK_EQ(ACTIVATED, status()) << status(); |
819 if (running_status() != RUNNING) { | 830 if (running_status() != RUNNING) { |
820 // Schedule calling this method after starting the worker. | 831 // Schedule calling this method after starting the worker. |
821 StartWorker(base::Bind( | 832 StartWorker(base::Bind( |
(...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1200 bool handled = true; | 1211 bool handled = true; |
1201 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerVersion, message) | 1212 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerVersion, message) |
1202 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetClients, | 1213 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetClients, |
1203 OnGetClients) | 1214 OnGetClients) |
1204 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ActivateEventFinished, | 1215 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ActivateEventFinished, |
1205 OnActivateEventFinished) | 1216 OnActivateEventFinished) |
1206 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_InstallEventFinished, | 1217 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_InstallEventFinished, |
1207 OnInstallEventFinished) | 1218 OnInstallEventFinished) |
1208 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FetchEventFinished, | 1219 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FetchEventFinished, |
1209 OnFetchEventFinished) | 1220 OnFetchEventFinished) |
1210 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SyncEventFinished, | |
1211 OnSyncEventFinished) | |
1212 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_NotificationClickEventFinished, | 1221 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_NotificationClickEventFinished, |
1213 OnNotificationClickEventFinished) | 1222 OnNotificationClickEventFinished) |
1214 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PushEventFinished, | 1223 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PushEventFinished, |
1215 OnPushEventFinished) | 1224 OnPushEventFinished) |
1216 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GeofencingEventFinished, | 1225 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GeofencingEventFinished, |
1217 OnGeofencingEventFinished) | 1226 OnGeofencingEventFinished) |
1218 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_OpenWindow, | 1227 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_OpenWindow, |
1219 OnOpenWindow) | 1228 OnOpenWindow) |
1220 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SetCachedMetadata, | 1229 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SetCachedMetadata, |
1221 OnSetCachedMetadata) | 1230 OnSetCachedMetadata) |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1385 ServiceWorkerMetrics::RecordFetchEventTime( | 1394 ServiceWorkerMetrics::RecordFetchEventTime( |
1386 result, base::TimeTicks::Now() - request->start_time); | 1395 result, base::TimeTicks::Now() - request->start_time); |
1387 | 1396 |
1388 scoped_refptr<ServiceWorkerVersion> protect(this); | 1397 scoped_refptr<ServiceWorkerVersion> protect(this); |
1389 request->callback.Run(SERVICE_WORKER_OK, result, response); | 1398 request->callback.Run(SERVICE_WORKER_OK, result, response); |
1390 RemoveCallbackAndStopIfRedundant(&fetch_requests_, request_id); | 1399 RemoveCallbackAndStopIfRedundant(&fetch_requests_, request_id); |
1391 } | 1400 } |
1392 | 1401 |
1393 void ServiceWorkerVersion::OnSyncEventFinished( | 1402 void ServiceWorkerVersion::OnSyncEventFinished( |
1394 int request_id, | 1403 int request_id, |
1395 blink::WebServiceWorkerEventResult result) { | 1404 ServiceWorkerEventStatus status) { |
1396 TRACE_EVENT1("ServiceWorker", | 1405 TRACE_EVENT1("ServiceWorker", |
1397 "ServiceWorkerVersion::OnSyncEventFinished", | 1406 "ServiceWorkerVersion::OnSyncEventFinished", |
1398 "Request id", request_id); | 1407 "Request id", request_id); |
1399 PendingRequest<StatusCallback>* request = sync_requests_.Lookup(request_id); | 1408 PendingRequest<StatusCallback>* request = sync_requests_.Lookup(request_id); |
1400 if (!request) { | 1409 if (!request) { |
1401 NOTREACHED() << "Got unexpected message: " << request_id; | 1410 NOTREACHED() << "Got unexpected message: " << request_id; |
1402 return; | 1411 return; |
1403 } | 1412 } |
1404 | 1413 |
1405 ServiceWorkerStatusCode status = SERVICE_WORKER_OK; | |
1406 if (result == blink::WebServiceWorkerEventResultRejected) { | |
1407 status = SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED; | |
1408 } | |
1409 | |
1410 scoped_refptr<ServiceWorkerVersion> protect(this); | 1414 scoped_refptr<ServiceWorkerVersion> protect(this); |
1411 request->callback.Run(status); | 1415 request->callback.Run(mojo::ConvertTo<ServiceWorkerStatusCode>(status)); |
1412 RemoveCallbackAndStopIfRedundant(&sync_requests_, request_id); | 1416 RemoveCallbackAndStopIfRedundant(&sync_requests_, request_id); |
1413 } | 1417 } |
1414 | 1418 |
1415 void ServiceWorkerVersion::OnNotificationClickEventFinished( | 1419 void ServiceWorkerVersion::OnNotificationClickEventFinished( |
1416 int request_id) { | 1420 int request_id) { |
1417 TRACE_EVENT1("ServiceWorker", | 1421 TRACE_EVENT1("ServiceWorker", |
1418 "ServiceWorkerVersion::OnNotificationClickEventFinished", | 1422 "ServiceWorkerVersion::OnNotificationClickEventFinished", |
1419 "Request id", request_id); | 1423 "Request id", request_id); |
1420 PendingRequest<StatusCallback>* request = | 1424 PendingRequest<StatusCallback>* request = |
1421 notification_click_requests_.Lookup(request_id); | 1425 notification_click_requests_.Lookup(request_id); |
(...skipping 782 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2204 ServiceWorkerResponse()); | 2208 ServiceWorkerResponse()); |
2205 RunIDMapCallbacks(&sync_requests_, SERVICE_WORKER_ERROR_FAILED); | 2209 RunIDMapCallbacks(&sync_requests_, SERVICE_WORKER_ERROR_FAILED); |
2206 RunIDMapCallbacks(¬ification_click_requests_, SERVICE_WORKER_ERROR_FAILED); | 2210 RunIDMapCallbacks(¬ification_click_requests_, SERVICE_WORKER_ERROR_FAILED); |
2207 RunIDMapCallbacks(&push_requests_, SERVICE_WORKER_ERROR_FAILED); | 2211 RunIDMapCallbacks(&push_requests_, SERVICE_WORKER_ERROR_FAILED); |
2208 RunIDMapCallbacks(&geofencing_requests_, SERVICE_WORKER_ERROR_FAILED); | 2212 RunIDMapCallbacks(&geofencing_requests_, SERVICE_WORKER_ERROR_FAILED); |
2209 | 2213 |
2210 // Close all mojo services. This will also fire and clear all callbacks | 2214 // Close all mojo services. This will also fire and clear all callbacks |
2211 // for messages that are still outstanding for those services. | 2215 // for messages that are still outstanding for those services. |
2212 OnServicePortDispatcherConnectionError(); | 2216 OnServicePortDispatcherConnectionError(); |
2213 | 2217 |
| 2218 OnBackgroundSyncDispatcherConnectionError(); |
| 2219 |
2214 streaming_url_request_jobs_.clear(); | 2220 streaming_url_request_jobs_.clear(); |
2215 | 2221 |
2216 FOR_EACH_OBSERVER(Listener, listeners_, OnRunningStateChanged(this)); | 2222 FOR_EACH_OBSERVER(Listener, listeners_, OnRunningStateChanged(this)); |
2217 | 2223 |
2218 if (should_restart) | 2224 if (should_restart) |
2219 StartWorkerInternal(); | 2225 StartWorkerInternal(); |
2220 } | 2226 } |
2221 | 2227 |
2222 void ServiceWorkerVersion::OnServicePortDispatcherConnectionError() { | 2228 void ServiceWorkerVersion::OnServicePortDispatcherConnectionError() { |
2223 RunIDMapCallbacks(&service_port_connect_requests_, | 2229 RunIDMapCallbacks(&service_port_connect_requests_, |
2224 SERVICE_WORKER_ERROR_FAILED, false, base::string16(), | 2230 SERVICE_WORKER_ERROR_FAILED, false, base::string16(), |
2225 base::string16()); | 2231 base::string16()); |
2226 service_port_dispatcher_.reset(); | 2232 service_port_dispatcher_.reset(); |
2227 } | 2233 } |
2228 | 2234 |
| 2235 void ServiceWorkerVersion::OnBackgroundSyncDispatcherConnectionError() { |
| 2236 RunIDMapCallbacks(&sync_requests_, SERVICE_WORKER_ERROR_FAILED); |
| 2237 background_sync_dispatcher_.reset(); |
| 2238 } |
| 2239 |
2229 } // namespace content | 2240 } // namespace content |
OLD | NEW |