Chromium Code Reviews| 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 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 561 context, service_worker_version_id_, script_url_); | 565 context, service_worker_version_id_, script_url_); |
| 562 } | 566 } |
| 563 | 567 |
| 564 void ServiceWorkerContextClient::willDestroyWorkerContext( | 568 void ServiceWorkerContextClient::willDestroyWorkerContext( |
| 565 v8::Local<v8::Context> context) { | 569 v8::Local<v8::Context> context) { |
| 566 // At this point WillStopCurrentWorkerThread is already called, so | 570 // At this point WillStopCurrentWorkerThread is already called, so |
| 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 all the pending events callbacks. |
| 572 for (WorkerContextData::SyncEventCallbacksMap::iterator it( | 576 AbortPendingEventCallbacks(context_->sync_event_callbacks); |
| 573 &context_->sync_event_callbacks); | 577 AbortPendingEventCallbacks(context_->push_event_callbacks); |
| 574 !it.IsAtEnd(); it.Advance()) { | 578 AbortPendingEventCallbacks(context_->fetch_event_callbacks); |
| 575 it.GetCurrentValue()->Run(SERVICE_WORKER_ERROR_ABORT, base::Time::Now()); | 579 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 | 580 |
| 590 // We have to clear callbacks now, as they need to be freed on the | 581 // We have to clear callbacks now, as they need to be freed on the |
| 591 // same thread. | 582 // same thread. |
| 592 context_.reset(); | 583 context_.reset(); |
| 593 | 584 |
| 594 // This also lets the message filter stop dispatching messages to | 585 // This also lets the message filter stop dispatching messages to |
| 595 // this client. | 586 // this client. |
| 596 g_worker_client_tls.Pointer()->Set(NULL); | 587 g_worker_client_tls.Pointer()->Set(NULL); |
| 597 | 588 |
| 598 GetContentClient()->renderer()->WillDestroyServiceWorkerContextOnWorkerThread( | 589 GetContentClient()->renderer()->WillDestroyServiceWorkerContextOnWorkerThread( |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 754 double event_dispatch_time) { | 745 double event_dispatch_time) { |
| 755 Send(new ServiceWorkerHostMsg_NotificationCloseEventFinished( | 746 Send(new ServiceWorkerHostMsg_NotificationCloseEventFinished( |
| 756 GetRoutingID(), request_id, result, | 747 GetRoutingID(), request_id, result, |
| 757 base::Time::FromDoubleT(event_dispatch_time))); | 748 base::Time::FromDoubleT(event_dispatch_time))); |
| 758 } | 749 } |
| 759 | 750 |
| 760 void ServiceWorkerContextClient::didHandlePushEvent( | 751 void ServiceWorkerContextClient::didHandlePushEvent( |
| 761 int request_id, | 752 int request_id, |
| 762 blink::WebServiceWorkerEventResult result, | 753 blink::WebServiceWorkerEventResult result, |
| 763 double event_dispatch_time) { | 754 double event_dispatch_time) { |
| 764 Send(new ServiceWorkerHostMsg_PushEventFinished( | 755 const DispatchPushEventCallback* callback = |
| 765 GetRoutingID(), request_id, result, | 756 context_->push_event_callbacks.Lookup(request_id); |
| 766 base::Time::FromDoubleT(event_dispatch_time))); | 757 DCHECK(callback); |
|
Peter Beverloo
2016/12/15 17:35:39
nit: yay, thanks! :)
This has one downside - DCHE
Marijn Kruisselbrink
2016/12/15 17:42:36
Handling a DCHECK failure (or your NOTREACHED case
xiaofengzhang
2016/12/16 02:15:49
Acknowledged.
| |
| 758 callback->Run(result == blink::WebServiceWorkerEventResultCompleted | |
| 759 ? SERVICE_WORKER_OK | |
| 760 : SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED, | |
| 761 base::Time::FromDoubleT(event_dispatch_time)); | |
| 762 context_->push_event_callbacks.Remove(request_id); | |
| 767 } | 763 } |
| 768 | 764 |
| 769 void ServiceWorkerContextClient::didHandleSyncEvent( | 765 void ServiceWorkerContextClient::didHandleSyncEvent( |
| 770 int request_id, | 766 int request_id, |
| 771 blink::WebServiceWorkerEventResult result, | 767 blink::WebServiceWorkerEventResult result, |
| 772 double event_dispatch_time) { | 768 double event_dispatch_time) { |
| 773 const SyncCallback* callback = | 769 const SyncCallback* callback = |
| 774 context_->sync_event_callbacks.Lookup(request_id); | 770 context_->sync_event_callbacks.Lookup(request_id); |
| 775 if (!callback) | 771 if (!callback) |
| 776 return; | 772 return; |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1049 int request_id, | 1045 int request_id, |
| 1050 const std::string& notification_id, | 1046 const std::string& notification_id, |
| 1051 const PlatformNotificationData& notification_data) { | 1047 const PlatformNotificationData& notification_data) { |
| 1052 TRACE_EVENT0("ServiceWorker", | 1048 TRACE_EVENT0("ServiceWorker", |
| 1053 "ServiceWorkerContextClient::OnNotificationCloseEvent"); | 1049 "ServiceWorkerContextClient::OnNotificationCloseEvent"); |
| 1054 proxy_->dispatchNotificationCloseEvent( | 1050 proxy_->dispatchNotificationCloseEvent( |
| 1055 request_id, blink::WebString::fromUTF8(notification_id), | 1051 request_id, blink::WebString::fromUTF8(notification_id), |
| 1056 ToWebNotificationData(notification_data)); | 1052 ToWebNotificationData(notification_data)); |
| 1057 } | 1053 } |
| 1058 | 1054 |
| 1059 void ServiceWorkerContextClient::OnPushEvent(int request_id, | 1055 void ServiceWorkerContextClient::DispatchPushEvent( |
| 1060 const PushEventPayload& payload) { | 1056 const PushEventPayload& payload, |
| 1057 const DispatchPushEventCallback& callback) { | |
| 1061 TRACE_EVENT0("ServiceWorker", | 1058 TRACE_EVENT0("ServiceWorker", |
| 1062 "ServiceWorkerContextClient::OnPushEvent"); | 1059 "ServiceWorkerContextClient::DispatchPushEvent"); |
| 1060 int request_id = context_->push_event_callbacks.Add( | |
| 1061 base::MakeUnique<DispatchPushEventCallback>(callback)); | |
| 1062 | |
| 1063 // 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. |
| 1064 blink::WebString data; | 1064 blink::WebString data; |
| 1065 if (!payload.is_null) | 1065 if (!payload.is_null) |
| 1066 data.assign(blink::WebString::fromUTF8(payload.data)); | 1066 data.assign(blink::WebString::fromUTF8(payload.data)); |
| 1067 proxy_->dispatchPushEvent(request_id, data); | 1067 proxy_->dispatchPushEvent(request_id, data); |
| 1068 } | 1068 } |
| 1069 | 1069 |
| 1070 void ServiceWorkerContextClient::OnDidGetClient( | 1070 void ServiceWorkerContextClient::OnDidGetClient( |
| 1071 int request_id, | 1071 int request_id, |
| 1072 const ServiceWorkerClientInfo& client) { | 1072 const ServiceWorkerClientInfo& client) { |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1276 } | 1276 } |
| 1277 | 1277 |
| 1278 base::WeakPtr<ServiceWorkerContextClient> | 1278 base::WeakPtr<ServiceWorkerContextClient> |
| 1279 ServiceWorkerContextClient::GetWeakPtr() { | 1279 ServiceWorkerContextClient::GetWeakPtr() { |
| 1280 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); | 1280 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); |
| 1281 DCHECK(context_); | 1281 DCHECK(context_); |
| 1282 return context_->weak_factory.GetWeakPtr(); | 1282 return context_->weak_factory.GetWeakPtr(); |
| 1283 } | 1283 } |
| 1284 | 1284 |
| 1285 } // namespace content | 1285 } // namespace content |
| OLD | NEW |