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 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 // worker thread. | 241 // worker thread. |
242 struct ServiceWorkerContextClient::WorkerContextData { | 242 struct ServiceWorkerContextClient::WorkerContextData { |
243 using ClientsCallbacksMap = | 243 using ClientsCallbacksMap = |
244 IDMap<std::unique_ptr<blink::WebServiceWorkerClientsCallbacks>>; | 244 IDMap<std::unique_ptr<blink::WebServiceWorkerClientsCallbacks>>; |
245 using ClaimClientsCallbacksMap = | 245 using ClaimClientsCallbacksMap = |
246 IDMap<std::unique_ptr<blink::WebServiceWorkerClientsClaimCallbacks>>; | 246 IDMap<std::unique_ptr<blink::WebServiceWorkerClientsClaimCallbacks>>; |
247 using ClientCallbacksMap = | 247 using ClientCallbacksMap = |
248 IDMap<std::unique_ptr<blink::WebServiceWorkerClientCallbacks>>; | 248 IDMap<std::unique_ptr<blink::WebServiceWorkerClientCallbacks>>; |
249 using SkipWaitingCallbacksMap = | 249 using SkipWaitingCallbacksMap = |
250 IDMap<std::unique_ptr<blink::WebServiceWorkerSkipWaitingCallbacks>>; | 250 IDMap<std::unique_ptr<blink::WebServiceWorkerSkipWaitingCallbacks>>; |
| 251 using InstallEventCallbacksMap = |
| 252 IDMap<std::unique_ptr<const DispatchInstallEventCallback>>; |
251 using ActivateEventCallbacksMap = | 253 using ActivateEventCallbacksMap = |
252 IDMap<std::unique_ptr<const DispatchActivateEventCallback>>; | 254 IDMap<std::unique_ptr<const DispatchActivateEventCallback>>; |
253 using BackgroundFetchAbortEventCallbacksMap = | 255 using BackgroundFetchAbortEventCallbacksMap = |
254 IDMap<std::unique_ptr<const DispatchBackgroundFetchAbortEventCallback>>; | 256 IDMap<std::unique_ptr<const DispatchBackgroundFetchAbortEventCallback>>; |
255 using BackgroundFetchClickEventCallbacksMap = | 257 using BackgroundFetchClickEventCallbacksMap = |
256 IDMap<std::unique_ptr<const DispatchBackgroundFetchClickEventCallback>>; | 258 IDMap<std::unique_ptr<const DispatchBackgroundFetchClickEventCallback>>; |
257 using BackgroundFetchFailEventCallbacksMap = | 259 using BackgroundFetchFailEventCallbacksMap = |
258 IDMap<std::unique_ptr<const DispatchBackgroundFetchFailEventCallback>>; | 260 IDMap<std::unique_ptr<const DispatchBackgroundFetchFailEventCallback>>; |
259 using BackgroundFetchedEventCallbacksMap = | 261 using BackgroundFetchedEventCallbacksMap = |
260 IDMap<std::unique_ptr<const DispatchBackgroundFetchedEventCallback>>; | 262 IDMap<std::unique_ptr<const DispatchBackgroundFetchedEventCallback>>; |
(...skipping 26 matching lines...) Expand all Loading... |
287 | 289 |
288 // Pending callbacks for OpenWindow() and FocusClient(). | 290 // Pending callbacks for OpenWindow() and FocusClient(). |
289 ClientCallbacksMap client_callbacks; | 291 ClientCallbacksMap client_callbacks; |
290 | 292 |
291 // Pending callbacks for SkipWaiting(). | 293 // Pending callbacks for SkipWaiting(). |
292 SkipWaitingCallbacksMap skip_waiting_callbacks; | 294 SkipWaitingCallbacksMap skip_waiting_callbacks; |
293 | 295 |
294 // Pending callbacks for ClaimClients(). | 296 // Pending callbacks for ClaimClients(). |
295 ClaimClientsCallbacksMap claim_clients_callbacks; | 297 ClaimClientsCallbacksMap claim_clients_callbacks; |
296 | 298 |
| 299 // Pending callbacks for Install Events. |
| 300 InstallEventCallbacksMap install_event_callbacks; |
| 301 |
297 // Pending callbacks for Activate Events. | 302 // Pending callbacks for Activate Events. |
298 ActivateEventCallbacksMap activate_event_callbacks; | 303 ActivateEventCallbacksMap activate_event_callbacks; |
299 | 304 |
300 // Pending callbacks for Background Fetch Abort Events. | 305 // Pending callbacks for Background Fetch Abort Events. |
301 BackgroundFetchAbortEventCallbacksMap background_fetch_abort_event_callbacks; | 306 BackgroundFetchAbortEventCallbacksMap background_fetch_abort_event_callbacks; |
302 | 307 |
303 // Pending callbacks for Background Fetch Click Events. | 308 // Pending callbacks for Background Fetch Click Events. |
304 BackgroundFetchClickEventCallbacksMap background_fetch_click_event_callbacks; | 309 BackgroundFetchClickEventCallbacksMap background_fetch_click_event_callbacks; |
305 | 310 |
306 // Pending callbacks for Background Fetch Fail Events. | 311 // Pending callbacks for Background Fetch Fail Events. |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
528 | 533 |
529 ServiceWorkerContextClient::~ServiceWorkerContextClient() {} | 534 ServiceWorkerContextClient::~ServiceWorkerContextClient() {} |
530 | 535 |
531 void ServiceWorkerContextClient::OnMessageReceived( | 536 void ServiceWorkerContextClient::OnMessageReceived( |
532 int thread_id, | 537 int thread_id, |
533 int embedded_worker_id, | 538 int embedded_worker_id, |
534 const IPC::Message& message) { | 539 const IPC::Message& message) { |
535 CHECK_EQ(embedded_worker_id_, embedded_worker_id); | 540 CHECK_EQ(embedded_worker_id_, embedded_worker_id); |
536 bool handled = true; | 541 bool handled = true; |
537 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerContextClient, message) | 542 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerContextClient, message) |
538 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent, OnInstallEvent) | |
539 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClient, OnDidGetClient) | 543 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClient, OnDidGetClient) |
540 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClients, OnDidGetClients) | 544 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClients, OnDidGetClients) |
541 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowResponse, | 545 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowResponse, |
542 OnOpenWindowResponse) | 546 OnOpenWindowResponse) |
543 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowError, | 547 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowError, |
544 OnOpenWindowError) | 548 OnOpenWindowError) |
545 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FocusClientResponse, | 549 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FocusClientResponse, |
546 OnFocusClientResponse) | 550 OnFocusClientResponse) |
547 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NavigateClientResponse, | 551 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NavigateClientResponse, |
548 OnNavigateClientResponse) | 552 OnNavigateClientResponse) |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
690 context, service_worker_version_id_, script_url_); | 694 context, service_worker_version_id_, script_url_); |
691 } | 695 } |
692 | 696 |
693 void ServiceWorkerContextClient::WillDestroyWorkerContext( | 697 void ServiceWorkerContextClient::WillDestroyWorkerContext( |
694 v8::Local<v8::Context> context) { | 698 v8::Local<v8::Context> context) { |
695 // At this point WillStopCurrentWorkerThread is already called, so | 699 // At this point WillStopCurrentWorkerThread is already called, so |
696 // worker_task_runner_->RunsTasksOnCurrentThread() returns false | 700 // worker_task_runner_->RunsTasksOnCurrentThread() returns false |
697 // (while we're still on the worker thread). | 701 // (while we're still on the worker thread). |
698 proxy_ = NULL; | 702 proxy_ = NULL; |
699 | 703 |
700 // Aborts all the pending events callbacks. | 704 // Clear all install event methods. |
| 705 install_methods_map_.clear(); |
| 706 |
| 707 // Aborts the all pending install event callbacks which has three |
| 708 // parameters so that here can't use AbortPendingEventCallbacks. |
| 709 for (WorkerContextData::InstallEventCallbacksMap::iterator it( |
| 710 &context_->install_event_callbacks); |
| 711 !it.IsAtEnd(); it.Advance()) { |
| 712 it.GetCurrentValue()->Run(SERVICE_WORKER_ERROR_ABORT, false, |
| 713 base::Time::Now()); |
| 714 } |
| 715 // Aborts all other pending events callbacks. |
701 AbortPendingEventCallbacks(context_->activate_event_callbacks); | 716 AbortPendingEventCallbacks(context_->activate_event_callbacks); |
702 AbortPendingEventCallbacks(context_->background_fetch_abort_event_callbacks); | 717 AbortPendingEventCallbacks(context_->background_fetch_abort_event_callbacks); |
703 AbortPendingEventCallbacks(context_->background_fetch_click_event_callbacks); | 718 AbortPendingEventCallbacks(context_->background_fetch_click_event_callbacks); |
704 AbortPendingEventCallbacks(context_->background_fetch_fail_event_callbacks); | 719 AbortPendingEventCallbacks(context_->background_fetch_fail_event_callbacks); |
705 AbortPendingEventCallbacks(context_->background_fetched_event_callbacks); | 720 AbortPendingEventCallbacks(context_->background_fetched_event_callbacks); |
706 AbortPendingEventCallbacks(context_->sync_event_callbacks); | 721 AbortPendingEventCallbacks(context_->sync_event_callbacks); |
707 AbortPendingEventCallbacks(context_->notification_click_event_callbacks); | 722 AbortPendingEventCallbacks(context_->notification_click_event_callbacks); |
708 AbortPendingEventCallbacks(context_->notification_close_event_callbacks); | 723 AbortPendingEventCallbacks(context_->notification_close_event_callbacks); |
709 AbortPendingEventCallbacks(context_->push_event_callbacks); | 724 AbortPendingEventCallbacks(context_->push_event_callbacks); |
710 AbortPendingEventCallbacks(context_->fetch_event_callbacks); | 725 AbortPendingEventCallbacks(context_->fetch_event_callbacks); |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
860 DCHECK(callback); | 875 DCHECK(callback); |
861 callback->Run(EventResultToStatus(result), | 876 callback->Run(EventResultToStatus(result), |
862 base::Time::FromDoubleT(event_dispatch_time)); | 877 base::Time::FromDoubleT(event_dispatch_time)); |
863 context_->message_event_callbacks.Remove(request_id); | 878 context_->message_event_callbacks.Remove(request_id); |
864 } | 879 } |
865 | 880 |
866 void ServiceWorkerContextClient::DidHandleInstallEvent( | 881 void ServiceWorkerContextClient::DidHandleInstallEvent( |
867 int request_id, | 882 int request_id, |
868 blink::WebServiceWorkerEventResult result, | 883 blink::WebServiceWorkerEventResult result, |
869 double event_dispatch_time) { | 884 double event_dispatch_time) { |
870 Send(new ServiceWorkerHostMsg_InstallEventFinished( | 885 const DispatchInstallEventCallback* callback = |
871 GetRoutingID(), request_id, result, proxy_->HasFetchEventHandler(), | 886 context_->install_event_callbacks.Lookup(request_id); |
872 base::Time::FromDoubleT(event_dispatch_time))); | 887 DCHECK(callback); |
| 888 callback->Run(EventResultToStatus(result), proxy_->HasFetchEventHandler(), |
| 889 base::Time::FromDoubleT(event_dispatch_time)); |
| 890 context_->install_event_callbacks.Remove(request_id); |
| 891 install_methods_map_.erase(request_id); |
873 } | 892 } |
874 | 893 |
875 void ServiceWorkerContextClient::RespondToFetchEvent( | 894 void ServiceWorkerContextClient::RespondToFetchEvent( |
876 int fetch_event_id, | 895 int fetch_event_id, |
877 double event_dispatch_time) { | 896 double event_dispatch_time) { |
878 Send(new ServiceWorkerHostMsg_FetchEventResponse( | 897 Send(new ServiceWorkerHostMsg_FetchEventResponse( |
879 GetRoutingID(), fetch_event_id, | 898 GetRoutingID(), fetch_event_id, |
880 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, ServiceWorkerResponse(), | 899 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, ServiceWorkerResponse(), |
881 base::Time::FromDoubleT(event_dispatch_time))); | 900 base::Time::FromDoubleT(event_dispatch_time))); |
882 } | 901 } |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1047 } | 1066 } |
1048 | 1067 |
1049 void ServiceWorkerContextClient::Claim( | 1068 void ServiceWorkerContextClient::Claim( |
1050 std::unique_ptr<blink::WebServiceWorkerClientsClaimCallbacks> callbacks) { | 1069 std::unique_ptr<blink::WebServiceWorkerClientsClaimCallbacks> callbacks) { |
1051 DCHECK(callbacks); | 1070 DCHECK(callbacks); |
1052 int request_id = context_->claim_clients_callbacks.Add(std::move(callbacks)); | 1071 int request_id = context_->claim_clients_callbacks.Add(std::move(callbacks)); |
1053 Send(new ServiceWorkerHostMsg_ClaimClients(GetRoutingID(), request_id)); | 1072 Send(new ServiceWorkerHostMsg_ClaimClients(GetRoutingID(), request_id)); |
1054 } | 1073 } |
1055 | 1074 |
1056 void ServiceWorkerContextClient::RegisterForeignFetchScopes( | 1075 void ServiceWorkerContextClient::RegisterForeignFetchScopes( |
| 1076 const int event_id, |
1057 const blink::WebVector<blink::WebURL>& sub_scopes, | 1077 const blink::WebVector<blink::WebURL>& sub_scopes, |
1058 const blink::WebVector<blink::WebSecurityOrigin>& origins) { | 1078 const blink::WebVector<blink::WebSecurityOrigin>& origins) { |
1059 Send(new ServiceWorkerHostMsg_RegisterForeignFetchScopes( | 1079 std::vector<GURL> urls; |
1060 GetRoutingID(), std::vector<GURL>(sub_scopes.begin(), sub_scopes.end()), | 1080 for (const auto& url : sub_scopes) |
1061 std::vector<url::Origin>(origins.begin(), origins.end()))); | 1081 urls.push_back(url); |
| 1082 |
| 1083 std::vector<url::Origin> url_origins; |
| 1084 for (const auto& origin : origins) |
| 1085 url_origins.push_back(origin); |
| 1086 |
| 1087 install_methods_map_[event_id]->RegisterForeignFetchScopes(urls, url_origins); |
1062 } | 1088 } |
1063 | 1089 |
1064 void ServiceWorkerContextClient::DispatchSyncEvent( | 1090 void ServiceWorkerContextClient::DispatchSyncEvent( |
1065 const std::string& tag, | 1091 const std::string& tag, |
1066 blink::mojom::BackgroundSyncEventLastChance last_chance, | 1092 blink::mojom::BackgroundSyncEventLastChance last_chance, |
1067 const DispatchSyncEventCallback& callback) { | 1093 const DispatchSyncEventCallback& callback) { |
1068 TRACE_EVENT0("ServiceWorker", | 1094 TRACE_EVENT0("ServiceWorker", |
1069 "ServiceWorkerContextClient::DispatchSyncEvent"); | 1095 "ServiceWorkerContextClient::DispatchSyncEvent"); |
1070 int request_id = context_->sync_event_callbacks.Add( | 1096 int request_id = context_->sync_event_callbacks.Add( |
1071 base::MakeUnique<SyncCallback>(callback)); | 1097 base::MakeUnique<SyncCallback>(callback)); |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1199 fetches.size()); | 1225 fetches.size()); |
1200 for (size_t i = 0; i < fetches.size(); ++i) { | 1226 for (size_t i = 0; i < fetches.size(); ++i) { |
1201 ToWebServiceWorkerRequest(fetches[i].request, &web_fetches[i].request); | 1227 ToWebServiceWorkerRequest(fetches[i].request, &web_fetches[i].request); |
1202 ToWebServiceWorkerResponse(fetches[i].response, &web_fetches[i].response); | 1228 ToWebServiceWorkerResponse(fetches[i].response, &web_fetches[i].response); |
1203 } | 1229 } |
1204 | 1230 |
1205 proxy_->DispatchBackgroundFetchedEvent( | 1231 proxy_->DispatchBackgroundFetchedEvent( |
1206 request_id, blink::WebString::FromUTF8(tag), web_fetches); | 1232 request_id, blink::WebString::FromUTF8(tag), web_fetches); |
1207 } | 1233 } |
1208 | 1234 |
| 1235 void ServiceWorkerContextClient::DispatchInstallEvent( |
| 1236 mojom::ServiceWorkerInstallEventMethodsAssociatedPtrInfo client, |
| 1237 const DispatchInstallEventCallback& callback) { |
| 1238 TRACE_EVENT0("ServiceWorker", |
| 1239 "ServiceWorkerContextClient::DispatchInstallEvent"); |
| 1240 |
| 1241 int request_id = context_->install_event_callbacks.Add( |
| 1242 base::MakeUnique<DispatchInstallEventCallback>(callback)); |
| 1243 |
| 1244 auto it = install_methods_map_.find(request_id); |
| 1245 DCHECK(it == install_methods_map_.end()); |
| 1246 mojom::ServiceWorkerInstallEventMethodsAssociatedPtr install_method_client_; |
| 1247 install_method_client_.Bind(std::move(client)); |
| 1248 install_methods_map_[request_id] = std::move(install_method_client_); |
| 1249 |
| 1250 proxy_->DispatchInstallEvent(request_id); |
| 1251 } |
| 1252 |
1209 void ServiceWorkerContextClient::DispatchExtendableMessageEvent( | 1253 void ServiceWorkerContextClient::DispatchExtendableMessageEvent( |
1210 mojom::ExtendableMessageEventPtr event, | 1254 mojom::ExtendableMessageEventPtr event, |
1211 const DispatchExtendableMessageEventCallback& callback) { | 1255 const DispatchExtendableMessageEventCallback& callback) { |
1212 TRACE_EVENT0("ServiceWorker", | 1256 TRACE_EVENT0("ServiceWorker", |
1213 "ServiceWorkerContextClient::DispatchExtendableMessageEvent"); | 1257 "ServiceWorkerContextClient::DispatchExtendableMessageEvent"); |
1214 int request_id = context_->message_event_callbacks.Add( | 1258 int request_id = context_->message_event_callbacks.Add( |
1215 base::MakeUnique<DispatchExtendableMessageEventCallback>(callback)); | 1259 base::MakeUnique<DispatchExtendableMessageEventCallback>(callback)); |
1216 | 1260 |
1217 blink::WebMessagePortChannelArray ports = | 1261 blink::WebMessagePortChannelArray ports = |
1218 WebMessagePortChannelImpl::CreateFromMessagePipeHandles( | 1262 WebMessagePortChannelImpl::CreateFromMessagePipeHandles( |
(...skipping 15 matching lines...) Expand all Loading... |
1234 ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance( | 1278 ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance( |
1235 sender_.get(), main_thread_task_runner_.get()); | 1279 sender_.get(), main_thread_task_runner_.get()); |
1236 scoped_refptr<WebServiceWorkerImpl> worker = | 1280 scoped_refptr<WebServiceWorkerImpl> worker = |
1237 dispatcher->GetOrCreateServiceWorker(std::move(handle)); | 1281 dispatcher->GetOrCreateServiceWorker(std::move(handle)); |
1238 proxy_->DispatchExtendableMessageEvent( | 1282 proxy_->DispatchExtendableMessageEvent( |
1239 request_id, blink::WebString::FromUTF16(event->message), | 1283 request_id, blink::WebString::FromUTF16(event->message), |
1240 event->source_origin, std::move(ports), | 1284 event->source_origin, std::move(ports), |
1241 WebServiceWorkerImpl::CreateHandle(worker)); | 1285 WebServiceWorkerImpl::CreateHandle(worker)); |
1242 } | 1286 } |
1243 | 1287 |
1244 void ServiceWorkerContextClient::OnInstallEvent(int request_id) { | |
1245 TRACE_EVENT0("ServiceWorker", | |
1246 "ServiceWorkerContextClient::OnInstallEvent"); | |
1247 proxy_->DispatchInstallEvent(request_id); | |
1248 } | |
1249 | |
1250 void ServiceWorkerContextClient::DispatchFetchEvent( | 1288 void ServiceWorkerContextClient::DispatchFetchEvent( |
1251 int fetch_event_id, | 1289 int fetch_event_id, |
1252 const ServiceWorkerFetchRequest& request, | 1290 const ServiceWorkerFetchRequest& request, |
1253 mojom::FetchEventPreloadHandlePtr preload_handle, | 1291 mojom::FetchEventPreloadHandlePtr preload_handle, |
1254 const DispatchFetchEventCallback& callback) { | 1292 const DispatchFetchEventCallback& callback) { |
1255 std::unique_ptr<NavigationPreloadRequest> preload_request = | 1293 std::unique_ptr<NavigationPreloadRequest> preload_request = |
1256 preload_handle | 1294 preload_handle |
1257 ? base::MakeUnique<NavigationPreloadRequest>( | 1295 ? base::MakeUnique<NavigationPreloadRequest>( |
1258 fetch_event_id, request.url, std::move(preload_handle)) | 1296 fetch_event_id, request.url, std::move(preload_handle)) |
1259 : nullptr; | 1297 : nullptr; |
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1538 } | 1576 } |
1539 | 1577 |
1540 base::WeakPtr<ServiceWorkerContextClient> | 1578 base::WeakPtr<ServiceWorkerContextClient> |
1541 ServiceWorkerContextClient::GetWeakPtr() { | 1579 ServiceWorkerContextClient::GetWeakPtr() { |
1542 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); | 1580 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); |
1543 DCHECK(context_); | 1581 DCHECK(context_); |
1544 return context_->weak_factory.GetWeakPtr(); | 1582 return context_->weak_factory.GetWeakPtr(); |
1545 } | 1583 } |
1546 | 1584 |
1547 } // namespace content | 1585 } // namespace content |
OLD | NEW |