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 "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" |
11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
13 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
15 #include "base/thread_task_runner_handle.h" | 15 #include "base/thread_task_runner_handle.h" |
16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
17 #include "content/browser/bad_message.h" | 17 #include "content/browser/bad_message.h" |
18 #include "content/browser/child_process_security_policy_impl.h" | 18 #include "content/browser/child_process_security_policy_impl.h" |
19 #include "content/browser/message_port_message_filter.h" | 19 #include "content/browser/message_port_message_filter.h" |
20 #include "content/browser/message_port_service.h" | 20 #include "content/browser/message_port_service.h" |
21 #include "content/browser/service_worker/embedded_worker_instance.h" | 21 #include "content/browser/service_worker/embedded_worker_instance.h" |
22 #include "content/browser/service_worker/embedded_worker_registry.h" | 22 #include "content/browser/service_worker/embedded_worker_registry.h" |
23 #include "content/browser/service_worker/service_worker_context_core.h" | 23 #include "content/browser/service_worker/service_worker_context_core.h" |
24 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 24 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
25 #include "content/browser/service_worker/service_worker_metrics.h" | 25 #include "content/browser/service_worker/service_worker_metrics.h" |
| 26 #include "content/browser/service_worker/service_worker_mojo_event_dispatcher.h" |
26 #include "content/browser/service_worker/service_worker_registration.h" | 27 #include "content/browser/service_worker/service_worker_registration.h" |
27 #include "content/browser/service_worker/service_worker_utils.h" | 28 #include "content/browser/service_worker/service_worker_utils.h" |
28 #include "content/browser/service_worker/stashed_port_manager.h" | 29 #include "content/browser/service_worker/stashed_port_manager.h" |
29 #include "content/browser/storage_partition_impl.h" | 30 #include "content/browser/storage_partition_impl.h" |
30 #include "content/common/service_worker/service_worker_messages.h" | 31 #include "content/common/service_worker/service_worker_messages.h" |
31 #include "content/public/browser/browser_thread.h" | 32 #include "content/public/browser/browser_thread.h" |
32 #include "content/public/browser/content_browser_client.h" | 33 #include "content/public/browser/content_browser_client.h" |
33 #include "content/public/browser/page_navigator.h" | 34 #include "content/public/browser/page_navigator.h" |
34 #include "content/public/browser/render_frame_host.h" | 35 #include "content/public/browser/render_frame_host.h" |
35 #include "content/public/browser/render_process_host.h" | 36 #include "content/public/browser/render_process_host.h" |
36 #include "content/public/browser/web_contents.h" | 37 #include "content/public/browser/web_contents.h" |
37 #include "content/public/browser/web_contents_observer.h" | 38 #include "content/public/browser/web_contents_observer.h" |
38 #include "content/public/common/child_process_host.h" | 39 #include "content/public/common/child_process_host.h" |
39 #include "content/public/common/content_client.h" | 40 #include "content/public/common/content_client.h" |
40 #include "content/public/common/content_switches.h" | 41 #include "content/public/common/content_switches.h" |
41 #include "content/public/common/result_codes.h" | 42 #include "content/public/common/result_codes.h" |
| 43 #include "content/public/common/service_registry.h" |
42 #include "net/http/http_response_headers.h" | 44 #include "net/http/http_response_headers.h" |
43 #include "net/http/http_response_info.h" | 45 #include "net/http/http_response_info.h" |
44 | 46 |
45 namespace content { | 47 namespace content { |
46 | 48 |
47 using StatusCallback = ServiceWorkerVersion::StatusCallback; | 49 using StatusCallback = ServiceWorkerVersion::StatusCallback; |
48 using ServiceWorkerClients = std::vector<ServiceWorkerClientInfo>; | 50 using ServiceWorkerClients = std::vector<ServiceWorkerClientInfo>; |
49 using GetClientsCallback = | 51 using GetClientsCallback = |
50 base::Callback<void(scoped_ptr<ServiceWorkerClients>)>; | 52 base::Callback<void(scoped_ptr<ServiceWorkerClients>)>; |
51 | 53 |
(...skipping 702 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
754 // Schedule calling this method after starting the worker. | 756 // Schedule calling this method after starting the worker. |
755 StartWorker(base::Bind(&RunTaskAfterStartWorker, | 757 StartWorker(base::Bind(&RunTaskAfterStartWorker, |
756 weak_factory_.GetWeakPtr(), callback, | 758 weak_factory_.GetWeakPtr(), callback, |
757 base::Bind(&self::DispatchSyncEvent, | 759 base::Bind(&self::DispatchSyncEvent, |
758 weak_factory_.GetWeakPtr(), | 760 weak_factory_.GetWeakPtr(), |
759 callback))); | 761 callback))); |
760 return; | 762 return; |
761 } | 763 } |
762 | 764 |
763 int request_id = AddRequest(callback, &sync_callbacks_, REQUEST_SYNC); | 765 int request_id = AddRequest(callback, &sync_callbacks_, REQUEST_SYNC); |
764 ServiceWorkerStatusCode status = embedded_worker_->SendMessage( | 766 ServiceWorkerMojoEventDispatcher* mojo_event_dispatcher = |
765 ServiceWorkerMsg_SyncEvent(request_id)); | 767 context_->GetMojoEventDispatcher(embedded_worker_->process_id()); |
766 if (status != SERVICE_WORKER_OK) { | 768 DCHECK(mojo_event_dispatcher); |
767 sync_callbacks_.Remove(request_id); | 769 mojo_event_dispatcher->DispatchSyncEvent( |
768 RunSoon(base::Bind(callback, status)); | 770 embedded_worker_->thread_id(), |
769 } | 771 base::Bind(&self::OnSyncEventFinished, weak_factory_.GetWeakPtr(), |
| 772 request_id)); |
770 } | 773 } |
771 | 774 |
772 void ServiceWorkerVersion::DispatchNotificationClickEvent( | 775 void ServiceWorkerVersion::DispatchNotificationClickEvent( |
773 const StatusCallback& callback, | 776 const StatusCallback& callback, |
774 int64_t persistent_notification_id, | 777 int64_t persistent_notification_id, |
775 const PlatformNotificationData& notification_data) { | 778 const PlatformNotificationData& notification_data) { |
776 DCHECK_EQ(ACTIVATED, status()) << status(); | 779 DCHECK_EQ(ACTIVATED, status()) << status(); |
777 if (running_status() != RUNNING) { | 780 if (running_status() != RUNNING) { |
778 // Schedule calling this method after starting the worker. | 781 // Schedule calling this method after starting the worker. |
779 StartWorker(base::Bind( | 782 StartWorker(base::Bind( |
(...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1174 bool handled = true; | 1177 bool handled = true; |
1175 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerVersion, message) | 1178 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerVersion, message) |
1176 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetClients, | 1179 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetClients, |
1177 OnGetClients) | 1180 OnGetClients) |
1178 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ActivateEventFinished, | 1181 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ActivateEventFinished, |
1179 OnActivateEventFinished) | 1182 OnActivateEventFinished) |
1180 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_InstallEventFinished, | 1183 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_InstallEventFinished, |
1181 OnInstallEventFinished) | 1184 OnInstallEventFinished) |
1182 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FetchEventFinished, | 1185 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FetchEventFinished, |
1183 OnFetchEventFinished) | 1186 OnFetchEventFinished) |
1184 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SyncEventFinished, | |
1185 OnSyncEventFinished) | |
1186 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_NotificationClickEventFinished, | 1187 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_NotificationClickEventFinished, |
1187 OnNotificationClickEventFinished) | 1188 OnNotificationClickEventFinished) |
1188 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PushEventFinished, | 1189 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PushEventFinished, |
1189 OnPushEventFinished) | 1190 OnPushEventFinished) |
1190 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GeofencingEventFinished, | 1191 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GeofencingEventFinished, |
1191 OnGeofencingEventFinished) | 1192 OnGeofencingEventFinished) |
1192 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CrossOriginConnectEventFinished, | 1193 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CrossOriginConnectEventFinished, |
1193 OnCrossOriginConnectEventFinished) | 1194 OnCrossOriginConnectEventFinished) |
1194 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_OpenWindow, | 1195 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_OpenWindow, |
1195 OnOpenWindow) | 1196 OnOpenWindow) |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1347 | 1348 |
1348 // TODO(kinuko): Record other event statuses too. | 1349 // TODO(kinuko): Record other event statuses too. |
1349 const bool handled = (result == SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE); | 1350 const bool handled = (result == SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE); |
1350 metrics_->RecordEventStatus(handled); | 1351 metrics_->RecordEventStatus(handled); |
1351 | 1352 |
1352 scoped_refptr<ServiceWorkerVersion> protect(this); | 1353 scoped_refptr<ServiceWorkerVersion> protect(this); |
1353 callback->Run(SERVICE_WORKER_OK, result, response); | 1354 callback->Run(SERVICE_WORKER_OK, result, response); |
1354 RemoveCallbackAndStopIfRedundant(&fetch_callbacks_, request_id); | 1355 RemoveCallbackAndStopIfRedundant(&fetch_callbacks_, request_id); |
1355 } | 1356 } |
1356 | 1357 |
1357 void ServiceWorkerVersion::OnSyncEventFinished( | 1358 void ServiceWorkerVersion::OnSyncEventFinished(int request_id, |
1358 int request_id, | 1359 ServiceWorkerStatusCode status) { |
1359 blink::WebServiceWorkerEventResult result) { | |
1360 TRACE_EVENT1("ServiceWorker", | 1360 TRACE_EVENT1("ServiceWorker", |
1361 "ServiceWorkerVersion::OnSyncEventFinished", | 1361 "ServiceWorkerVersion::OnSyncEventFinished", |
1362 "Request id", request_id); | 1362 "Request id", request_id); |
1363 StatusCallback* callback = sync_callbacks_.Lookup(request_id); | 1363 StatusCallback* callback = sync_callbacks_.Lookup(request_id); |
1364 if (!callback) { | 1364 if (!callback) { |
1365 NOTREACHED() << "Got unexpected message: " << request_id; | 1365 NOTREACHED() << "Got unexpected message: " << request_id; |
1366 return; | 1366 return; |
1367 } | 1367 } |
1368 | 1368 |
1369 ServiceWorkerStatusCode status = SERVICE_WORKER_OK; | |
1370 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( | 1369 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
1371 switches::kEnableServiceWorkerSync)) { | 1370 switches::kEnableServiceWorkerSync)) { |
1372 // Avoid potential race condition where flag is disabled after a sync event | 1371 // Avoid potential race condition where flag is disabled after a sync event |
1373 // was dispatched | 1372 // was dispatched |
1374 status = SERVICE_WORKER_ERROR_ABORT; | 1373 status = SERVICE_WORKER_ERROR_ABORT; |
1375 } else if (result == blink::WebServiceWorkerEventResultRejected) { | |
1376 status = SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED; | |
1377 } | 1374 } |
1378 | 1375 |
1379 scoped_refptr<ServiceWorkerVersion> protect(this); | 1376 scoped_refptr<ServiceWorkerVersion> protect(this); |
1380 callback->Run(status); | 1377 callback->Run(status); |
1381 RemoveCallbackAndStopIfRedundant(&sync_callbacks_, request_id); | 1378 RemoveCallbackAndStopIfRedundant(&sync_callbacks_, request_id); |
1382 } | 1379 } |
1383 | 1380 |
1384 void ServiceWorkerVersion::OnNotificationClickEventFinished( | 1381 void ServiceWorkerVersion::OnNotificationClickEventFinished( |
1385 int request_id) { | 1382 int request_id) { |
1386 TRACE_EVENT1("ServiceWorker", | 1383 TRACE_EVENT1("ServiceWorker", |
(...skipping 724 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2111 is_update_scheduled_ = false; | 2108 is_update_scheduled_ = false; |
2112 } | 2109 } |
2113 | 2110 |
2114 if (status != SERVICE_WORKER_OK || registration->active_version() != this) | 2111 if (status != SERVICE_WORKER_OK || registration->active_version() != this) |
2115 return; | 2112 return; |
2116 context_->UpdateServiceWorker(registration.get(), | 2113 context_->UpdateServiceWorker(registration.get(), |
2117 false /* force_bypass_cache */); | 2114 false /* force_bypass_cache */); |
2118 } | 2115 } |
2119 | 2116 |
2120 } // namespace content | 2117 } // namespace content |
OLD | NEW |