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