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 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 web_client_info.uuid = base::UTF8ToUTF16(client_info.client_uuid); | 158 web_client_info.uuid = base::UTF8ToUTF16(client_info.client_uuid); |
159 web_client_info.pageVisibilityState = client_info.page_visibility_state; | 159 web_client_info.pageVisibilityState = client_info.page_visibility_state; |
160 web_client_info.isFocused = client_info.is_focused; | 160 web_client_info.isFocused = client_info.is_focused; |
161 web_client_info.url = client_info.url; | 161 web_client_info.url = client_info.url; |
162 web_client_info.frameType = GetBlinkFrameType(client_info.frame_type); | 162 web_client_info.frameType = GetBlinkFrameType(client_info.frame_type); |
163 web_client_info.clientType = client_info.client_type; | 163 web_client_info.clientType = client_info.client_type; |
164 | 164 |
165 return web_client_info; | 165 return web_client_info; |
166 } | 166 } |
167 | 167 |
| 168 // Use this template in willDestroyWorkerContext to abort all the pending |
| 169 // events callbacks. |
| 170 template <typename T> |
| 171 void AbortPendingEventCallbacks(T& callbacks) { |
| 172 for (typename T::iterator it(&callbacks); !it.IsAtEnd(); it.Advance()) { |
| 173 it.GetCurrentValue()->Run(SERVICE_WORKER_ERROR_ABORT, base::Time::Now()); |
| 174 } |
| 175 } |
| 176 |
168 } // namespace | 177 } // namespace |
169 | 178 |
170 // Holding data that needs to be bound to the worker context on the | 179 // Holding data that needs to be bound to the worker context on the |
171 // worker thread. | 180 // worker thread. |
172 struct ServiceWorkerContextClient::WorkerContextData { | 181 struct ServiceWorkerContextClient::WorkerContextData { |
173 using ClientsCallbacksMap = | 182 using ClientsCallbacksMap = |
174 IDMap<std::unique_ptr<blink::WebServiceWorkerClientsCallbacks>>; | 183 IDMap<std::unique_ptr<blink::WebServiceWorkerClientsCallbacks>>; |
175 using ClaimClientsCallbacksMap = | 184 using ClaimClientsCallbacksMap = |
176 IDMap<std::unique_ptr<blink::WebServiceWorkerClientsClaimCallbacks>>; | 185 IDMap<std::unique_ptr<blink::WebServiceWorkerClientsClaimCallbacks>>; |
177 using ClientCallbacksMap = | 186 using ClientCallbacksMap = |
178 IDMap<std::unique_ptr<blink::WebServiceWorkerClientCallbacks>>; | 187 IDMap<std::unique_ptr<blink::WebServiceWorkerClientCallbacks>>; |
179 using SkipWaitingCallbacksMap = | 188 using SkipWaitingCallbacksMap = |
180 IDMap<std::unique_ptr<blink::WebServiceWorkerSkipWaitingCallbacks>>; | 189 IDMap<std::unique_ptr<blink::WebServiceWorkerSkipWaitingCallbacks>>; |
181 using SyncEventCallbacksMap = IDMap<std::unique_ptr<const SyncCallback>>; | 190 using SyncEventCallbacksMap = IDMap<std::unique_ptr<const SyncCallback>>; |
| 191 using PushEventCallbacksMap = |
| 192 IDMap<std::unique_ptr<const DispatchPushEventCallback>>; |
182 using FetchEventCallbacksMap = IDMap<std::unique_ptr<const FetchCallback>>; | 193 using FetchEventCallbacksMap = IDMap<std::unique_ptr<const FetchCallback>>; |
183 using ExtendableMessageEventCallbacksMap = | 194 using ExtendableMessageEventCallbacksMap = |
184 IDMap<std::unique_ptr<const DispatchExtendableMessageEventCallback>>; | 195 IDMap<std::unique_ptr<const DispatchExtendableMessageEventCallback>>; |
185 using NavigationPreloadRequestsMap = IDMap< | 196 using NavigationPreloadRequestsMap = IDMap< |
186 std::unique_ptr<ServiceWorkerContextClient::NavigationPreloadRequest>>; | 197 std::unique_ptr<ServiceWorkerContextClient::NavigationPreloadRequest>>; |
187 | 198 |
188 explicit WorkerContextData(ServiceWorkerContextClient* owner) | 199 explicit WorkerContextData(ServiceWorkerContextClient* owner) |
189 : event_dispatcher_binding(owner), | 200 : event_dispatcher_binding(owner), |
190 weak_factory(owner), | 201 weak_factory(owner), |
191 proxy_weak_factory(owner->proxy_) {} | 202 proxy_weak_factory(owner->proxy_) {} |
(...skipping 12 matching lines...) Expand all Loading... |
204 | 215 |
205 // Pending callbacks for SkipWaiting(). | 216 // Pending callbacks for SkipWaiting(). |
206 SkipWaitingCallbacksMap skip_waiting_callbacks; | 217 SkipWaitingCallbacksMap skip_waiting_callbacks; |
207 | 218 |
208 // Pending callbacks for ClaimClients(). | 219 // Pending callbacks for ClaimClients(). |
209 ClaimClientsCallbacksMap claim_clients_callbacks; | 220 ClaimClientsCallbacksMap claim_clients_callbacks; |
210 | 221 |
211 // Pending callbacks for Background Sync Events. | 222 // Pending callbacks for Background Sync Events. |
212 SyncEventCallbacksMap sync_event_callbacks; | 223 SyncEventCallbacksMap sync_event_callbacks; |
213 | 224 |
| 225 // Pending callbacks for Push Events. |
| 226 PushEventCallbacksMap push_event_callbacks; |
| 227 |
214 // Pending callbacks for Fetch Events. | 228 // Pending callbacks for Fetch Events. |
215 FetchEventCallbacksMap fetch_event_callbacks; | 229 FetchEventCallbacksMap fetch_event_callbacks; |
216 | 230 |
217 // Pending callbacks for Extendable Message Events. | 231 // Pending callbacks for Extendable Message Events. |
218 ExtendableMessageEventCallbacksMap message_event_callbacks; | 232 ExtendableMessageEventCallbacksMap message_event_callbacks; |
219 | 233 |
220 // Pending navigation preload requests. | 234 // Pending navigation preload requests. |
221 NavigationPreloadRequestsMap preload_requests; | 235 NavigationPreloadRequestsMap preload_requests; |
222 | 236 |
223 base::ThreadChecker thread_checker; | 237 base::ThreadChecker thread_checker; |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 const IPC::Message& message) { | 413 const IPC::Message& message) { |
400 CHECK_EQ(embedded_worker_id_, embedded_worker_id); | 414 CHECK_EQ(embedded_worker_id_, embedded_worker_id); |
401 bool handled = true; | 415 bool handled = true; |
402 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerContextClient, message) | 416 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerContextClient, message) |
403 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ActivateEvent, OnActivateEvent) | 417 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ActivateEvent, OnActivateEvent) |
404 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent, OnInstallEvent) | 418 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent, OnInstallEvent) |
405 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NotificationClickEvent, | 419 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NotificationClickEvent, |
406 OnNotificationClickEvent) | 420 OnNotificationClickEvent) |
407 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NotificationCloseEvent, | 421 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NotificationCloseEvent, |
408 OnNotificationCloseEvent) | 422 OnNotificationCloseEvent) |
409 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_PushEvent, OnPushEvent) | |
410 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClient, OnDidGetClient) | 423 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClient, OnDidGetClient) |
411 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClients, OnDidGetClients) | 424 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClients, OnDidGetClients) |
412 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowResponse, | 425 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowResponse, |
413 OnOpenWindowResponse) | 426 OnOpenWindowResponse) |
414 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowError, | 427 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowError, |
415 OnOpenWindowError) | 428 OnOpenWindowError) |
416 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FocusClientResponse, | 429 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FocusClientResponse, |
417 OnFocusClientResponse) | 430 OnFocusClientResponse) |
418 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NavigateClientResponse, | 431 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NavigateClientResponse, |
419 OnNavigateClientResponse) | 432 OnNavigateClientResponse) |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
561 context, service_worker_version_id_, script_url_); | 574 context, service_worker_version_id_, script_url_); |
562 } | 575 } |
563 | 576 |
564 void ServiceWorkerContextClient::willDestroyWorkerContext( | 577 void ServiceWorkerContextClient::willDestroyWorkerContext( |
565 v8::Local<v8::Context> context) { | 578 v8::Local<v8::Context> context) { |
566 // At this point WillStopCurrentWorkerThread is already called, so | 579 // At this point WillStopCurrentWorkerThread is already called, so |
567 // worker_task_runner_->RunsTasksOnCurrentThread() returns false | 580 // worker_task_runner_->RunsTasksOnCurrentThread() returns false |
568 // (while we're still on the worker thread). | 581 // (while we're still on the worker thread). |
569 proxy_ = NULL; | 582 proxy_ = NULL; |
570 | 583 |
571 // Aborts the all pending sync event callbacks. | 584 // Aborts all the pending events callbacks. |
572 for (WorkerContextData::SyncEventCallbacksMap::iterator it( | 585 AbortPendingEventCallbacks(context_->sync_event_callbacks); |
573 &context_->sync_event_callbacks); | 586 AbortPendingEventCallbacks(context_->push_event_callbacks); |
574 !it.IsAtEnd(); it.Advance()) { | 587 AbortPendingEventCallbacks(context_->fetch_event_callbacks); |
575 it.GetCurrentValue()->Run(SERVICE_WORKER_ERROR_ABORT, base::Time::Now()); | 588 AbortPendingEventCallbacks(context_->message_event_callbacks); |
576 } | |
577 // Aborts the all pending fetch event callbacks. | |
578 for (WorkerContextData::FetchEventCallbacksMap::iterator it( | |
579 &context_->fetch_event_callbacks); | |
580 !it.IsAtEnd(); it.Advance()) { | |
581 it.GetCurrentValue()->Run(SERVICE_WORKER_ERROR_ABORT, base::Time::Now()); | |
582 } | |
583 // Aborts the all pending extendable message event callbacks. | |
584 for (WorkerContextData::ExtendableMessageEventCallbacksMap::iterator it( | |
585 &context_->message_event_callbacks); | |
586 !it.IsAtEnd(); it.Advance()) { | |
587 it.GetCurrentValue()->Run(SERVICE_WORKER_ERROR_ABORT, base::Time::Now()); | |
588 } | |
589 | 589 |
590 // We have to clear callbacks now, as they need to be freed on the | 590 // We have to clear callbacks now, as they need to be freed on the |
591 // same thread. | 591 // same thread. |
592 context_.reset(); | 592 context_.reset(); |
593 | 593 |
594 // This also lets the message filter stop dispatching messages to | 594 // This also lets the message filter stop dispatching messages to |
595 // this client. | 595 // this client. |
596 g_worker_client_tls.Pointer()->Set(NULL); | 596 g_worker_client_tls.Pointer()->Set(NULL); |
597 | 597 |
598 GetContentClient()->renderer()->WillDestroyServiceWorkerContextOnWorkerThread( | 598 GetContentClient()->renderer()->WillDestroyServiceWorkerContextOnWorkerThread( |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
754 double event_dispatch_time) { | 754 double event_dispatch_time) { |
755 Send(new ServiceWorkerHostMsg_NotificationCloseEventFinished( | 755 Send(new ServiceWorkerHostMsg_NotificationCloseEventFinished( |
756 GetRoutingID(), request_id, result, | 756 GetRoutingID(), request_id, result, |
757 base::Time::FromDoubleT(event_dispatch_time))); | 757 base::Time::FromDoubleT(event_dispatch_time))); |
758 } | 758 } |
759 | 759 |
760 void ServiceWorkerContextClient::didHandlePushEvent( | 760 void ServiceWorkerContextClient::didHandlePushEvent( |
761 int request_id, | 761 int request_id, |
762 blink::WebServiceWorkerEventResult result, | 762 blink::WebServiceWorkerEventResult result, |
763 double event_dispatch_time) { | 763 double event_dispatch_time) { |
764 Send(new ServiceWorkerHostMsg_PushEventFinished( | 764 const DispatchPushEventCallback* callback = |
765 GetRoutingID(), request_id, result, | 765 context_->push_event_callbacks.Lookup(request_id); |
766 base::Time::FromDoubleT(event_dispatch_time))); | 766 DCHECK(callback); |
| 767 callback->Run(result == blink::WebServiceWorkerEventResultCompleted |
| 768 ? SERVICE_WORKER_OK |
| 769 : SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED, |
| 770 base::Time::FromDoubleT(event_dispatch_time)); |
| 771 context_->push_event_callbacks.Remove(request_id); |
767 } | 772 } |
768 | 773 |
769 void ServiceWorkerContextClient::didHandleSyncEvent( | 774 void ServiceWorkerContextClient::didHandleSyncEvent( |
770 int request_id, | 775 int request_id, |
771 blink::WebServiceWorkerEventResult result, | 776 blink::WebServiceWorkerEventResult result, |
772 double event_dispatch_time) { | 777 double event_dispatch_time) { |
773 const SyncCallback* callback = | 778 const SyncCallback* callback = |
774 context_->sync_event_callbacks.Lookup(request_id); | 779 context_->sync_event_callbacks.Lookup(request_id); |
775 if (!callback) | 780 if (!callback) |
776 return; | 781 return; |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1049 int request_id, | 1054 int request_id, |
1050 const std::string& notification_id, | 1055 const std::string& notification_id, |
1051 const PlatformNotificationData& notification_data) { | 1056 const PlatformNotificationData& notification_data) { |
1052 TRACE_EVENT0("ServiceWorker", | 1057 TRACE_EVENT0("ServiceWorker", |
1053 "ServiceWorkerContextClient::OnNotificationCloseEvent"); | 1058 "ServiceWorkerContextClient::OnNotificationCloseEvent"); |
1054 proxy_->dispatchNotificationCloseEvent( | 1059 proxy_->dispatchNotificationCloseEvent( |
1055 request_id, blink::WebString::fromUTF8(notification_id), | 1060 request_id, blink::WebString::fromUTF8(notification_id), |
1056 ToWebNotificationData(notification_data)); | 1061 ToWebNotificationData(notification_data)); |
1057 } | 1062 } |
1058 | 1063 |
1059 void ServiceWorkerContextClient::OnPushEvent(int request_id, | 1064 void ServiceWorkerContextClient::DispatchPushEvent( |
1060 const PushEventPayload& payload) { | 1065 const PushEventPayload& payload, |
| 1066 const DispatchPushEventCallback& callback) { |
1061 TRACE_EVENT0("ServiceWorker", | 1067 TRACE_EVENT0("ServiceWorker", |
1062 "ServiceWorkerContextClient::OnPushEvent"); | 1068 "ServiceWorkerContextClient::DispatchPushEvent"); |
| 1069 int request_id = context_->push_event_callbacks.Add( |
| 1070 base::MakeUnique<DispatchPushEventCallback>(callback)); |
| 1071 |
1063 // Only set data to be a valid string if the payload had decrypted data. | 1072 // Only set data to be a valid string if the payload had decrypted data. |
1064 blink::WebString data; | 1073 blink::WebString data; |
1065 if (!payload.is_null) | 1074 if (!payload.is_null) |
1066 data.assign(blink::WebString::fromUTF8(payload.data)); | 1075 data.assign(blink::WebString::fromUTF8(payload.data)); |
1067 proxy_->dispatchPushEvent(request_id, data); | 1076 proxy_->dispatchPushEvent(request_id, data); |
1068 } | 1077 } |
1069 | 1078 |
1070 void ServiceWorkerContextClient::OnDidGetClient( | 1079 void ServiceWorkerContextClient::OnDidGetClient( |
1071 int request_id, | 1080 int request_id, |
1072 const ServiceWorkerClientInfo& client) { | 1081 const ServiceWorkerClientInfo& client) { |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1276 } | 1285 } |
1277 | 1286 |
1278 base::WeakPtr<ServiceWorkerContextClient> | 1287 base::WeakPtr<ServiceWorkerContextClient> |
1279 ServiceWorkerContextClient::GetWeakPtr() { | 1288 ServiceWorkerContextClient::GetWeakPtr() { |
1280 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); | 1289 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); |
1281 DCHECK(context_); | 1290 DCHECK(context_); |
1282 return context_->weak_factory.GetWeakPtr(); | 1291 return context_->weak_factory.GetWeakPtr(); |
1283 } | 1292 } |
1284 | 1293 |
1285 } // namespace content | 1294 } // namespace content |
OLD | NEW |