OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/renderer/service_worker/service_worker_context_client.h" | 5 #include "content/renderer/service_worker/service_worker_context_client.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
172 struct ServiceWorkerContextClient::WorkerContextData { | 172 struct ServiceWorkerContextClient::WorkerContextData { |
173 using ClientsCallbacksMap = | 173 using ClientsCallbacksMap = |
174 IDMap<std::unique_ptr<blink::WebServiceWorkerClientsCallbacks>>; | 174 IDMap<std::unique_ptr<blink::WebServiceWorkerClientsCallbacks>>; |
175 using ClaimClientsCallbacksMap = | 175 using ClaimClientsCallbacksMap = |
176 IDMap<std::unique_ptr<blink::WebServiceWorkerClientsClaimCallbacks>>; | 176 IDMap<std::unique_ptr<blink::WebServiceWorkerClientsClaimCallbacks>>; |
177 using ClientCallbacksMap = | 177 using ClientCallbacksMap = |
178 IDMap<std::unique_ptr<blink::WebServiceWorkerClientCallbacks>>; | 178 IDMap<std::unique_ptr<blink::WebServiceWorkerClientCallbacks>>; |
179 using SkipWaitingCallbacksMap = | 179 using SkipWaitingCallbacksMap = |
180 IDMap<std::unique_ptr<blink::WebServiceWorkerSkipWaitingCallbacks>>; | 180 IDMap<std::unique_ptr<blink::WebServiceWorkerSkipWaitingCallbacks>>; |
181 using SyncEventCallbacksMap = IDMap<std::unique_ptr<const SyncCallback>>; | 181 using SyncEventCallbacksMap = IDMap<std::unique_ptr<const SyncCallback>>; |
182 using PushEventCallbacksMap = | |
183 IDMap<std::unique_ptr<const DispatchPushEventCallback>>; | |
182 using FetchEventCallbacksMap = IDMap<std::unique_ptr<const FetchCallback>>; | 184 using FetchEventCallbacksMap = IDMap<std::unique_ptr<const FetchCallback>>; |
183 using ExtendableMessageEventCallbacksMap = | 185 using ExtendableMessageEventCallbacksMap = |
184 IDMap<std::unique_ptr<const DispatchExtendableMessageEventCallback>>; | 186 IDMap<std::unique_ptr<const DispatchExtendableMessageEventCallback>>; |
185 using NavigationPreloadRequestsMap = IDMap< | 187 using NavigationPreloadRequestsMap = IDMap< |
186 std::unique_ptr<ServiceWorkerContextClient::NavigationPreloadRequest>>; | 188 std::unique_ptr<ServiceWorkerContextClient::NavigationPreloadRequest>>; |
187 | 189 |
188 explicit WorkerContextData(ServiceWorkerContextClient* owner) | 190 explicit WorkerContextData(ServiceWorkerContextClient* owner) |
189 : event_dispatcher_binding(owner), | 191 : event_dispatcher_binding(owner), |
190 weak_factory(owner), | 192 weak_factory(owner), |
191 proxy_weak_factory(owner->proxy_) {} | 193 proxy_weak_factory(owner->proxy_) {} |
(...skipping 12 matching lines...) Expand all Loading... | |
204 | 206 |
205 // Pending callbacks for SkipWaiting(). | 207 // Pending callbacks for SkipWaiting(). |
206 SkipWaitingCallbacksMap skip_waiting_callbacks; | 208 SkipWaitingCallbacksMap skip_waiting_callbacks; |
207 | 209 |
208 // Pending callbacks for ClaimClients(). | 210 // Pending callbacks for ClaimClients(). |
209 ClaimClientsCallbacksMap claim_clients_callbacks; | 211 ClaimClientsCallbacksMap claim_clients_callbacks; |
210 | 212 |
211 // Pending callbacks for Background Sync Events. | 213 // Pending callbacks for Background Sync Events. |
212 SyncEventCallbacksMap sync_event_callbacks; | 214 SyncEventCallbacksMap sync_event_callbacks; |
213 | 215 |
216 // Pending callbacks for Push Events. | |
217 PushEventCallbacksMap push_event_callbacks; | |
218 | |
214 // Pending callbacks for Fetch Events. | 219 // Pending callbacks for Fetch Events. |
215 FetchEventCallbacksMap fetch_event_callbacks; | 220 FetchEventCallbacksMap fetch_event_callbacks; |
216 | 221 |
217 // Pending callbacks for Extendable Message Events. | 222 // Pending callbacks for Extendable Message Events. |
218 ExtendableMessageEventCallbacksMap message_event_callbacks; | 223 ExtendableMessageEventCallbacksMap message_event_callbacks; |
219 | 224 |
220 // Pending navigation preload requests. | 225 // Pending navigation preload requests. |
221 NavigationPreloadRequestsMap preload_requests; | 226 NavigationPreloadRequestsMap preload_requests; |
222 | 227 |
223 base::ThreadChecker thread_checker; | 228 base::ThreadChecker thread_checker; |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
399 const IPC::Message& message) { | 404 const IPC::Message& message) { |
400 CHECK_EQ(embedded_worker_id_, embedded_worker_id); | 405 CHECK_EQ(embedded_worker_id_, embedded_worker_id); |
401 bool handled = true; | 406 bool handled = true; |
402 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerContextClient, message) | 407 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerContextClient, message) |
403 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ActivateEvent, OnActivateEvent) | 408 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ActivateEvent, OnActivateEvent) |
404 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent, OnInstallEvent) | 409 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent, OnInstallEvent) |
405 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NotificationClickEvent, | 410 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NotificationClickEvent, |
406 OnNotificationClickEvent) | 411 OnNotificationClickEvent) |
407 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NotificationCloseEvent, | 412 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NotificationCloseEvent, |
408 OnNotificationCloseEvent) | 413 OnNotificationCloseEvent) |
409 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_PushEvent, OnPushEvent) | |
410 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClient, OnDidGetClient) | 414 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClient, OnDidGetClient) |
411 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClients, OnDidGetClients) | 415 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClients, OnDidGetClients) |
412 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowResponse, | 416 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowResponse, |
413 OnOpenWindowResponse) | 417 OnOpenWindowResponse) |
414 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowError, | 418 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowError, |
415 OnOpenWindowError) | 419 OnOpenWindowError) |
416 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FocusClientResponse, | 420 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FocusClientResponse, |
417 OnFocusClientResponse) | 421 OnFocusClientResponse) |
418 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NavigateClientResponse, | 422 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NavigateClientResponse, |
419 OnNavigateClientResponse) | 423 OnNavigateClientResponse) |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
567 // worker_task_runner_->RunsTasksOnCurrentThread() returns false | 571 // worker_task_runner_->RunsTasksOnCurrentThread() returns false |
568 // (while we're still on the worker thread). | 572 // (while we're still on the worker thread). |
569 proxy_ = NULL; | 573 proxy_ = NULL; |
570 | 574 |
571 // Aborts the all pending sync event callbacks. | 575 // Aborts the all pending sync event callbacks. |
572 for (WorkerContextData::SyncEventCallbacksMap::iterator it( | 576 for (WorkerContextData::SyncEventCallbacksMap::iterator it( |
573 &context_->sync_event_callbacks); | 577 &context_->sync_event_callbacks); |
574 !it.IsAtEnd(); it.Advance()) { | 578 !it.IsAtEnd(); it.Advance()) { |
575 it.GetCurrentValue()->Run(SERVICE_WORKER_ERROR_ABORT, base::Time::Now()); | 579 it.GetCurrentValue()->Run(SERVICE_WORKER_ERROR_ABORT, base::Time::Now()); |
576 } | 580 } |
581 // Aborts the all pending push event callbacks. | |
582 for (WorkerContextData::PushEventCallbacksMap::iterator it( | |
583 &context_->push_event_callbacks); | |
584 !it.IsAtEnd(); it.Advance()) { | |
585 it.GetCurrentValue()->Run(SERVICE_WORKER_ERROR_ABORT, base::Time::Now()); | |
586 } | |
577 // Aborts the all pending fetch event callbacks. | 587 // Aborts the all pending fetch event callbacks. |
578 for (WorkerContextData::FetchEventCallbacksMap::iterator it( | 588 for (WorkerContextData::FetchEventCallbacksMap::iterator it( |
579 &context_->fetch_event_callbacks); | 589 &context_->fetch_event_callbacks); |
580 !it.IsAtEnd(); it.Advance()) { | 590 !it.IsAtEnd(); it.Advance()) { |
581 it.GetCurrentValue()->Run(SERVICE_WORKER_ERROR_ABORT, base::Time::Now()); | 591 it.GetCurrentValue()->Run(SERVICE_WORKER_ERROR_ABORT, base::Time::Now()); |
582 } | 592 } |
583 // Aborts the all pending extendable message event callbacks. | 593 // Aborts the all pending extendable message event callbacks. |
584 for (WorkerContextData::ExtendableMessageEventCallbacksMap::iterator it( | 594 for (WorkerContextData::ExtendableMessageEventCallbacksMap::iterator it( |
585 &context_->message_event_callbacks); | 595 &context_->message_event_callbacks); |
586 !it.IsAtEnd(); it.Advance()) { | 596 !it.IsAtEnd(); it.Advance()) { |
587 it.GetCurrentValue()->Run(SERVICE_WORKER_ERROR_ABORT, base::Time::Now()); | 597 it.GetCurrentValue()->Run(SERVICE_WORKER_ERROR_ABORT, base::Time::Now()); |
588 } | 598 } |
Peter Beverloo
2016/12/13 13:31:32
fwiw, since this is now repeated four times, consi
| |
589 | 599 |
590 // We have to clear callbacks now, as they need to be freed on the | 600 // We have to clear callbacks now, as they need to be freed on the |
591 // same thread. | 601 // same thread. |
592 context_.reset(); | 602 context_.reset(); |
593 | 603 |
594 // This also lets the message filter stop dispatching messages to | 604 // This also lets the message filter stop dispatching messages to |
595 // this client. | 605 // this client. |
596 g_worker_client_tls.Pointer()->Set(NULL); | 606 g_worker_client_tls.Pointer()->Set(NULL); |
597 | 607 |
598 GetContentClient()->renderer()->WillDestroyServiceWorkerContextOnWorkerThread( | 608 GetContentClient()->renderer()->WillDestroyServiceWorkerContextOnWorkerThread( |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
754 double event_dispatch_time) { | 764 double event_dispatch_time) { |
755 Send(new ServiceWorkerHostMsg_NotificationCloseEventFinished( | 765 Send(new ServiceWorkerHostMsg_NotificationCloseEventFinished( |
756 GetRoutingID(), request_id, result, | 766 GetRoutingID(), request_id, result, |
757 base::Time::FromDoubleT(event_dispatch_time))); | 767 base::Time::FromDoubleT(event_dispatch_time))); |
758 } | 768 } |
759 | 769 |
760 void ServiceWorkerContextClient::didHandlePushEvent( | 770 void ServiceWorkerContextClient::didHandlePushEvent( |
761 int request_id, | 771 int request_id, |
762 blink::WebServiceWorkerEventResult result, | 772 blink::WebServiceWorkerEventResult result, |
763 double event_dispatch_time) { | 773 double event_dispatch_time) { |
764 Send(new ServiceWorkerHostMsg_PushEventFinished( | 774 const DispatchPushEventCallback* callback = |
765 GetRoutingID(), request_id, result, | 775 context_->push_event_callbacks.Lookup(request_id); |
766 base::Time::FromDoubleT(event_dispatch_time))); | 776 if (!callback) |
777 return; | |
Peter Beverloo
2016/12/13 13:31:32
Should this have a LOG(WARN)? Maybe even a DCHECK?
xiaofengzhang
2016/12/15 01:30:49
Done.
| |
778 if (result == blink::WebServiceWorkerEventResultCompleted) { | |
Peter Beverloo
2016/12/13 13:31:32
I notice that this reverses the logic for determin
| |
779 callback->Run(SERVICE_WORKER_OK, | |
780 base::Time::FromDoubleT(event_dispatch_time)); | |
781 } else { | |
782 callback->Run(SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED, | |
783 base::Time::FromDoubleT(event_dispatch_time)); | |
Peter Beverloo
2016/12/13 13:31:32
nit: prefer avoiding multiple calls to the same me
xiaofengzhang
2016/12/15 01:30:49
Done. Thanks :-)
| |
784 } | |
785 context_->push_event_callbacks.Remove(request_id); | |
767 } | 786 } |
768 | 787 |
769 void ServiceWorkerContextClient::didHandleSyncEvent( | 788 void ServiceWorkerContextClient::didHandleSyncEvent( |
770 int request_id, | 789 int request_id, |
771 blink::WebServiceWorkerEventResult result, | 790 blink::WebServiceWorkerEventResult result, |
772 double event_dispatch_time) { | 791 double event_dispatch_time) { |
773 const SyncCallback* callback = | 792 const SyncCallback* callback = |
774 context_->sync_event_callbacks.Lookup(request_id); | 793 context_->sync_event_callbacks.Lookup(request_id); |
775 if (!callback) | 794 if (!callback) |
776 return; | 795 return; |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1056 int request_id, | 1075 int request_id, |
1057 const std::string& notification_id, | 1076 const std::string& notification_id, |
1058 const PlatformNotificationData& notification_data) { | 1077 const PlatformNotificationData& notification_data) { |
1059 TRACE_EVENT0("ServiceWorker", | 1078 TRACE_EVENT0("ServiceWorker", |
1060 "ServiceWorkerContextClient::OnNotificationCloseEvent"); | 1079 "ServiceWorkerContextClient::OnNotificationCloseEvent"); |
1061 proxy_->dispatchNotificationCloseEvent( | 1080 proxy_->dispatchNotificationCloseEvent( |
1062 request_id, blink::WebString::fromUTF8(notification_id), | 1081 request_id, blink::WebString::fromUTF8(notification_id), |
1063 ToWebNotificationData(notification_data)); | 1082 ToWebNotificationData(notification_data)); |
1064 } | 1083 } |
1065 | 1084 |
1066 void ServiceWorkerContextClient::OnPushEvent(int request_id, | 1085 void ServiceWorkerContextClient::DispatchPushEvent( |
1067 const PushEventPayload& payload) { | 1086 const PushEventPayload& payload, |
1087 const DispatchPushEventCallback& callback) { | |
1068 TRACE_EVENT0("ServiceWorker", | 1088 TRACE_EVENT0("ServiceWorker", |
1069 "ServiceWorkerContextClient::OnPushEvent"); | 1089 "ServiceWorkerContextClient::DispatchPushEvent"); |
1090 int request_id = context_->push_event_callbacks.Add( | |
1091 base::MakeUnique<DispatchPushEventCallback>(callback)); | |
1092 | |
1070 // Only set data to be a valid string if the payload had decrypted data. | 1093 // Only set data to be a valid string if the payload had decrypted data. |
1071 blink::WebString data; | 1094 blink::WebString data; |
1072 if (!payload.is_null) | 1095 if (!payload.is_null) |
1073 data.assign(blink::WebString::fromUTF8(payload.data)); | 1096 data.assign(blink::WebString::fromUTF8(payload.data)); |
1074 proxy_->dispatchPushEvent(request_id, data); | 1097 proxy_->dispatchPushEvent(request_id, data); |
1075 } | 1098 } |
1076 | 1099 |
1077 void ServiceWorkerContextClient::OnDidGetClient( | 1100 void ServiceWorkerContextClient::OnDidGetClient( |
1078 int request_id, | 1101 int request_id, |
1079 const ServiceWorkerClientInfo& client) { | 1102 const ServiceWorkerClientInfo& client) { |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1283 } | 1306 } |
1284 | 1307 |
1285 base::WeakPtr<ServiceWorkerContextClient> | 1308 base::WeakPtr<ServiceWorkerContextClient> |
1286 ServiceWorkerContextClient::GetWeakPtr() { | 1309 ServiceWorkerContextClient::GetWeakPtr() { |
1287 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); | 1310 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); |
1288 DCHECK(context_); | 1311 DCHECK(context_); |
1289 return context_->weak_factory.GetWeakPtr(); | 1312 return context_->weak_factory.GetWeakPtr(); |
1290 } | 1313 } |
1291 | 1314 |
1292 } // namespace content | 1315 } // namespace content |
OLD | NEW |