Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(170)

Side by Side Diff: content/renderer/service_worker/service_worker_context_client.cc

Issue 2746783002: [ServiceWorker] Mojofy InstallEvent of Service Worker (Closed)
Patch Set: Rebase Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <map> 7 #include <map>
8 #include <memory> 8 #include <memory>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 using SimpleEventCallback = 265 using SimpleEventCallback =
266 base::Callback<void(ServiceWorkerStatusCode, base::Time)>; 266 base::Callback<void(ServiceWorkerStatusCode, base::Time)>;
267 using ClientsCallbacksMap = 267 using ClientsCallbacksMap =
268 IDMap<std::unique_ptr<blink::WebServiceWorkerClientsCallbacks>>; 268 IDMap<std::unique_ptr<blink::WebServiceWorkerClientsCallbacks>>;
269 using ClaimClientsCallbacksMap = 269 using ClaimClientsCallbacksMap =
270 IDMap<std::unique_ptr<blink::WebServiceWorkerClientsClaimCallbacks>>; 270 IDMap<std::unique_ptr<blink::WebServiceWorkerClientsClaimCallbacks>>;
271 using ClientCallbacksMap = 271 using ClientCallbacksMap =
272 IDMap<std::unique_ptr<blink::WebServiceWorkerClientCallbacks>>; 272 IDMap<std::unique_ptr<blink::WebServiceWorkerClientCallbacks>>;
273 using SkipWaitingCallbacksMap = 273 using SkipWaitingCallbacksMap =
274 IDMap<std::unique_ptr<blink::WebServiceWorkerSkipWaitingCallbacks>>; 274 IDMap<std::unique_ptr<blink::WebServiceWorkerSkipWaitingCallbacks>>;
275 using InstallEventCallbacksMap =
276 IDMap<std::unique_ptr<const DispatchInstallEventCallback>>;
275 using ActivateEventCallbacksMap = 277 using ActivateEventCallbacksMap =
276 IDMap<std::unique_ptr<const DispatchActivateEventCallback>>; 278 IDMap<std::unique_ptr<const DispatchActivateEventCallback>>;
277 using BackgroundFetchAbortEventCallbacksMap = 279 using BackgroundFetchAbortEventCallbacksMap =
278 IDMap<std::unique_ptr<const DispatchBackgroundFetchAbortEventCallback>>; 280 IDMap<std::unique_ptr<const DispatchBackgroundFetchAbortEventCallback>>;
279 using BackgroundFetchClickEventCallbacksMap = 281 using BackgroundFetchClickEventCallbacksMap =
280 IDMap<std::unique_ptr<const DispatchBackgroundFetchClickEventCallback>>; 282 IDMap<std::unique_ptr<const DispatchBackgroundFetchClickEventCallback>>;
281 using BackgroundFetchFailEventCallbacksMap = 283 using BackgroundFetchFailEventCallbacksMap =
282 IDMap<std::unique_ptr<const DispatchBackgroundFetchFailEventCallback>>; 284 IDMap<std::unique_ptr<const DispatchBackgroundFetchFailEventCallback>>;
283 using BackgroundFetchedEventCallbacksMap = 285 using BackgroundFetchedEventCallbacksMap =
284 IDMap<std::unique_ptr<const DispatchBackgroundFetchedEventCallback>>; 286 IDMap<std::unique_ptr<const DispatchBackgroundFetchedEventCallback>>;
(...skipping 25 matching lines...) Expand all
310 312
311 // Pending callbacks for OpenWindow() and FocusClient(). 313 // Pending callbacks for OpenWindow() and FocusClient().
312 ClientCallbacksMap client_callbacks; 314 ClientCallbacksMap client_callbacks;
313 315
314 // Pending callbacks for SkipWaiting(). 316 // Pending callbacks for SkipWaiting().
315 SkipWaitingCallbacksMap skip_waiting_callbacks; 317 SkipWaitingCallbacksMap skip_waiting_callbacks;
316 318
317 // Pending callbacks for ClaimClients(). 319 // Pending callbacks for ClaimClients().
318 ClaimClientsCallbacksMap claim_clients_callbacks; 320 ClaimClientsCallbacksMap claim_clients_callbacks;
319 321
322 // Pending callbacks for Install Events.
323 InstallEventCallbacksMap install_event_callbacks;
324
320 // Pending callbacks for Activate Events. 325 // Pending callbacks for Activate Events.
321 ActivateEventCallbacksMap activate_event_callbacks; 326 ActivateEventCallbacksMap activate_event_callbacks;
322 327
323 // Pending callbacks for Background Fetch Abort Events. 328 // Pending callbacks for Background Fetch Abort Events.
324 BackgroundFetchAbortEventCallbacksMap background_fetch_abort_event_callbacks; 329 BackgroundFetchAbortEventCallbacksMap background_fetch_abort_event_callbacks;
325 330
326 // Pending callbacks for Background Fetch Click Events. 331 // Pending callbacks for Background Fetch Click Events.
327 BackgroundFetchClickEventCallbacksMap background_fetch_click_event_callbacks; 332 BackgroundFetchClickEventCallbacksMap background_fetch_click_event_callbacks;
328 333
329 // Pending callbacks for Background Fetch Fail Events. 334 // Pending callbacks for Background Fetch Fail Events.
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
556 561
557 ServiceWorkerContextClient::~ServiceWorkerContextClient() {} 562 ServiceWorkerContextClient::~ServiceWorkerContextClient() {}
558 563
559 void ServiceWorkerContextClient::OnMessageReceived( 564 void ServiceWorkerContextClient::OnMessageReceived(
560 int thread_id, 565 int thread_id,
561 int embedded_worker_id, 566 int embedded_worker_id,
562 const IPC::Message& message) { 567 const IPC::Message& message) {
563 CHECK_EQ(embedded_worker_id_, embedded_worker_id); 568 CHECK_EQ(embedded_worker_id_, embedded_worker_id);
564 bool handled = true; 569 bool handled = true;
565 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerContextClient, message) 570 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerContextClient, message)
566 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent, OnInstallEvent)
567 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClient, OnDidGetClient) 571 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClient, OnDidGetClient)
568 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClients, OnDidGetClients) 572 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClients, OnDidGetClients)
569 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowResponse, 573 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowResponse,
570 OnOpenWindowResponse) 574 OnOpenWindowResponse)
571 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowError, 575 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowError,
572 OnOpenWindowError) 576 OnOpenWindowError)
573 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FocusClientResponse, 577 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FocusClientResponse,
574 OnFocusClientResponse) 578 OnFocusClientResponse)
575 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NavigateClientResponse, 579 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NavigateClientResponse,
576 OnNavigateClientResponse) 580 OnNavigateClientResponse)
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
718 context, service_worker_version_id_, script_url_); 722 context, service_worker_version_id_, script_url_);
719 } 723 }
720 724
721 void ServiceWorkerContextClient::WillDestroyWorkerContext( 725 void ServiceWorkerContextClient::WillDestroyWorkerContext(
722 v8::Local<v8::Context> context) { 726 v8::Local<v8::Context> context) {
723 // At this point WillStopCurrentWorkerThread is already called, so 727 // At this point WillStopCurrentWorkerThread is already called, so
724 // worker_task_runner_->RunsTasksOnCurrentThread() returns false 728 // worker_task_runner_->RunsTasksOnCurrentThread() returns false
725 // (while we're still on the worker thread). 729 // (while we're still on the worker thread).
726 proxy_ = NULL; 730 proxy_ = NULL;
727 731
728 // Aborts all the pending events callbacks. 732 // Clear all install event methods.
733 install_methods_map_.clear();
734
735 // Aborts the all pending install event callbacks which has three
736 // parameters so that here can't use AbortPendingEventCallbacks.
737 for (WorkerContextData::InstallEventCallbacksMap::iterator it(
738 &context_->install_event_callbacks);
739 !it.IsAtEnd(); it.Advance()) {
740 it.GetCurrentValue()->Run(SERVICE_WORKER_ERROR_ABORT, false,
741 base::Time::Now());
742 }
743 // Aborts all other pending events callbacks.
729 AbortPendingEventCallbacks(context_->activate_event_callbacks); 744 AbortPendingEventCallbacks(context_->activate_event_callbacks);
730 AbortPendingEventCallbacks(context_->background_fetch_abort_event_callbacks); 745 AbortPendingEventCallbacks(context_->background_fetch_abort_event_callbacks);
731 AbortPendingEventCallbacks(context_->background_fetch_click_event_callbacks); 746 AbortPendingEventCallbacks(context_->background_fetch_click_event_callbacks);
732 AbortPendingEventCallbacks(context_->background_fetch_fail_event_callbacks); 747 AbortPendingEventCallbacks(context_->background_fetch_fail_event_callbacks);
733 AbortPendingEventCallbacks(context_->background_fetched_event_callbacks); 748 AbortPendingEventCallbacks(context_->background_fetched_event_callbacks);
734 AbortPendingEventCallbacks(context_->sync_event_callbacks); 749 AbortPendingEventCallbacks(context_->sync_event_callbacks);
735 AbortPendingEventCallbacks(context_->notification_click_event_callbacks); 750 AbortPendingEventCallbacks(context_->notification_click_event_callbacks);
736 AbortPendingEventCallbacks(context_->notification_close_event_callbacks); 751 AbortPendingEventCallbacks(context_->notification_close_event_callbacks);
737 AbortPendingEventCallbacks(context_->push_event_callbacks); 752 AbortPendingEventCallbacks(context_->push_event_callbacks);
738 AbortPendingEventCallbacks(context_->fetch_event_callbacks); 753 AbortPendingEventCallbacks(context_->fetch_event_callbacks);
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
888 DCHECK(callback); 903 DCHECK(callback);
889 callback->Run(EventResultToStatus(result), 904 callback->Run(EventResultToStatus(result),
890 base::Time::FromDoubleT(event_dispatch_time)); 905 base::Time::FromDoubleT(event_dispatch_time));
891 context_->message_event_callbacks.Remove(request_id); 906 context_->message_event_callbacks.Remove(request_id);
892 } 907 }
893 908
894 void ServiceWorkerContextClient::DidHandleInstallEvent( 909 void ServiceWorkerContextClient::DidHandleInstallEvent(
895 int request_id, 910 int request_id,
896 blink::WebServiceWorkerEventResult result, 911 blink::WebServiceWorkerEventResult result,
897 double event_dispatch_time) { 912 double event_dispatch_time) {
898 Send(new ServiceWorkerHostMsg_InstallEventFinished( 913 const DispatchInstallEventCallback* callback =
899 GetRoutingID(), request_id, result, proxy_->HasFetchEventHandler(), 914 context_->install_event_callbacks.Lookup(request_id);
900 base::Time::FromDoubleT(event_dispatch_time))); 915 DCHECK(callback);
916 callback->Run(EventResultToStatus(result), proxy_->HasFetchEventHandler(),
917 base::Time::FromDoubleT(event_dispatch_time));
918 context_->install_event_callbacks.Remove(request_id);
919 install_methods_map_.erase(request_id);
901 } 920 }
902 921
903 void ServiceWorkerContextClient::RespondToFetchEventWithNoResponse( 922 void ServiceWorkerContextClient::RespondToFetchEventWithNoResponse(
904 int fetch_event_id, 923 int fetch_event_id,
905 double event_dispatch_time) { 924 double event_dispatch_time) {
906 const mojom::ServiceWorkerFetchResponseCallbackPtr& response_callback = 925 const mojom::ServiceWorkerFetchResponseCallbackPtr& response_callback =
907 context_->fetch_response_callbacks[fetch_event_id]; 926 context_->fetch_response_callbacks[fetch_event_id];
908 DCHECK(response_callback.is_bound()); 927 DCHECK(response_callback.is_bound());
909 response_callback->OnFallback(base::Time::FromDoubleT(event_dispatch_time)); 928 response_callback->OnFallback(base::Time::FromDoubleT(event_dispatch_time));
910 context_->fetch_response_callbacks.erase(fetch_event_id); 929 context_->fetch_response_callbacks.erase(fetch_event_id);
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
1112 } 1131 }
1113 1132
1114 void ServiceWorkerContextClient::Claim( 1133 void ServiceWorkerContextClient::Claim(
1115 std::unique_ptr<blink::WebServiceWorkerClientsClaimCallbacks> callbacks) { 1134 std::unique_ptr<blink::WebServiceWorkerClientsClaimCallbacks> callbacks) {
1116 DCHECK(callbacks); 1135 DCHECK(callbacks);
1117 int request_id = context_->claim_clients_callbacks.Add(std::move(callbacks)); 1136 int request_id = context_->claim_clients_callbacks.Add(std::move(callbacks));
1118 Send(new ServiceWorkerHostMsg_ClaimClients(GetRoutingID(), request_id)); 1137 Send(new ServiceWorkerHostMsg_ClaimClients(GetRoutingID(), request_id));
1119 } 1138 }
1120 1139
1121 void ServiceWorkerContextClient::RegisterForeignFetchScopes( 1140 void ServiceWorkerContextClient::RegisterForeignFetchScopes(
1141 const int event_id,
leonhsl(Using Gerrit) 2017/04/23 03:33:10 No need to use 'const'.
xiaofengzhang 2017/04/24 05:02:41 Done.
1122 const blink::WebVector<blink::WebURL>& sub_scopes, 1142 const blink::WebVector<blink::WebURL>& sub_scopes,
1123 const blink::WebVector<blink::WebSecurityOrigin>& origins) { 1143 const blink::WebVector<blink::WebSecurityOrigin>& origins) {
1124 Send(new ServiceWorkerHostMsg_RegisterForeignFetchScopes( 1144 std::vector<GURL> urls;
1125 GetRoutingID(), std::vector<GURL>(sub_scopes.begin(), sub_scopes.end()), 1145 for (const auto& url : sub_scopes)
1126 std::vector<url::Origin>(origins.begin(), origins.end()))); 1146 urls.push_back(url);
1147
1148 std::vector<url::Origin> url_origins;
1149 for (const auto& origin : origins)
1150 url_origins.push_back(origin);
1151
1152 install_methods_map_[event_id]->RegisterForeignFetchScopes(urls, url_origins);
leonhsl(Using Gerrit) 2017/04/23 03:33:10 Need to DCHECK that install_methods_map_[event_id]
xiaofengzhang 2017/04/24 05:02:41 Done.
1127 } 1153 }
1128 1154
1129 void ServiceWorkerContextClient::DispatchSyncEvent( 1155 void ServiceWorkerContextClient::DispatchSyncEvent(
1130 const std::string& tag, 1156 const std::string& tag,
1131 blink::mojom::BackgroundSyncEventLastChance last_chance, 1157 blink::mojom::BackgroundSyncEventLastChance last_chance,
1132 const DispatchSyncEventCallback& callback) { 1158 const DispatchSyncEventCallback& callback) {
1133 TRACE_EVENT0("ServiceWorker", 1159 TRACE_EVENT0("ServiceWorker",
1134 "ServiceWorkerContextClient::DispatchSyncEvent"); 1160 "ServiceWorkerContextClient::DispatchSyncEvent");
1135 int request_id = context_->sync_event_callbacks.Add( 1161 int request_id = context_->sync_event_callbacks.Add(
1136 base::MakeUnique<SyncCallback>(callback)); 1162 base::MakeUnique<SyncCallback>(callback));
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
1264 fetches.size()); 1290 fetches.size());
1265 for (size_t i = 0; i < fetches.size(); ++i) { 1291 for (size_t i = 0; i < fetches.size(); ++i) {
1266 ToWebServiceWorkerRequest(fetches[i].request, &web_fetches[i].request); 1292 ToWebServiceWorkerRequest(fetches[i].request, &web_fetches[i].request);
1267 ToWebServiceWorkerResponse(fetches[i].response, &web_fetches[i].response); 1293 ToWebServiceWorkerResponse(fetches[i].response, &web_fetches[i].response);
1268 } 1294 }
1269 1295
1270 proxy_->DispatchBackgroundFetchedEvent( 1296 proxy_->DispatchBackgroundFetchedEvent(
1271 request_id, blink::WebString::FromUTF8(tag), web_fetches); 1297 request_id, blink::WebString::FromUTF8(tag), web_fetches);
1272 } 1298 }
1273 1299
1300 void ServiceWorkerContextClient::DispatchInstallEvent(
1301 mojom::ServiceWorkerInstallEventMethodsAssociatedPtrInfo client,
1302 const DispatchInstallEventCallback& callback) {
1303 TRACE_EVENT0("ServiceWorker",
1304 "ServiceWorkerContextClient::DispatchInstallEvent");
1305
1306 int request_id = context_->install_event_callbacks.Add(
leonhsl(Using Gerrit) 2017/04/23 03:33:10 Better to uniform the naming as 'event_id' with Li
xiaofengzhang 2017/04/24 05:02:41 Done.
1307 base::MakeUnique<DispatchInstallEventCallback>(callback));
1308
1309 auto it = install_methods_map_.find(request_id);
shimazu 2017/04/24 05:00:13 DCHECK(install_methods_map_.find(request_id) == in
1310 DCHECK(it == install_methods_map_.end());
1311 mojom::ServiceWorkerInstallEventMethodsAssociatedPtr install_method_client_;
leonhsl(Using Gerrit) 2017/04/23 03:33:10 install_method_client_ --> install_methods ?
xiaofengzhang 2017/04/24 05:02:41 Done.
1312 install_method_client_.Bind(std::move(client));
1313 install_methods_map_[request_id] = std::move(install_method_client_);
1314
1315 proxy_->DispatchInstallEvent(request_id);
1316 }
1317
1274 void ServiceWorkerContextClient::DispatchExtendableMessageEvent( 1318 void ServiceWorkerContextClient::DispatchExtendableMessageEvent(
1275 mojom::ExtendableMessageEventPtr event, 1319 mojom::ExtendableMessageEventPtr event,
1276 const DispatchExtendableMessageEventCallback& callback) { 1320 const DispatchExtendableMessageEventCallback& callback) {
1277 TRACE_EVENT0("ServiceWorker", 1321 TRACE_EVENT0("ServiceWorker",
1278 "ServiceWorkerContextClient::DispatchExtendableMessageEvent"); 1322 "ServiceWorkerContextClient::DispatchExtendableMessageEvent");
1279 int request_id = context_->message_event_callbacks.Add( 1323 int request_id = context_->message_event_callbacks.Add(
1280 base::MakeUnique<DispatchExtendableMessageEventCallback>(callback)); 1324 base::MakeUnique<DispatchExtendableMessageEventCallback>(callback));
1281 1325
1282 blink::WebMessagePortChannelArray ports = 1326 blink::WebMessagePortChannelArray ports =
1283 WebMessagePortChannelImpl::CreateFromMessagePipeHandles( 1327 WebMessagePortChannelImpl::CreateFromMessagePipeHandles(
(...skipping 15 matching lines...) Expand all
1299 ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance( 1343 ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance(
1300 sender_.get(), main_thread_task_runner_.get()); 1344 sender_.get(), main_thread_task_runner_.get());
1301 scoped_refptr<WebServiceWorkerImpl> worker = 1345 scoped_refptr<WebServiceWorkerImpl> worker =
1302 dispatcher->GetOrCreateServiceWorker(std::move(handle)); 1346 dispatcher->GetOrCreateServiceWorker(std::move(handle));
1303 proxy_->DispatchExtendableMessageEvent( 1347 proxy_->DispatchExtendableMessageEvent(
1304 request_id, blink::WebString::FromUTF16(event->message), 1348 request_id, blink::WebString::FromUTF16(event->message),
1305 event->source_origin, std::move(ports), 1349 event->source_origin, std::move(ports),
1306 WebServiceWorkerImpl::CreateHandle(worker)); 1350 WebServiceWorkerImpl::CreateHandle(worker));
1307 } 1351 }
1308 1352
1309 void ServiceWorkerContextClient::OnInstallEvent(int request_id) {
1310 TRACE_EVENT0("ServiceWorker",
1311 "ServiceWorkerContextClient::OnInstallEvent");
1312 proxy_->DispatchInstallEvent(request_id);
1313 }
1314
1315 void ServiceWorkerContextClient::DispatchFetchEvent( 1353 void ServiceWorkerContextClient::DispatchFetchEvent(
1316 int fetch_event_id, 1354 int fetch_event_id,
1317 const ServiceWorkerFetchRequest& request, 1355 const ServiceWorkerFetchRequest& request,
1318 mojom::FetchEventPreloadHandlePtr preload_handle, 1356 mojom::FetchEventPreloadHandlePtr preload_handle,
1319 mojom::ServiceWorkerFetchResponseCallbackPtr response_callback, 1357 mojom::ServiceWorkerFetchResponseCallbackPtr response_callback,
1320 const DispatchFetchEventCallback& callback) { 1358 const DispatchFetchEventCallback& callback) {
1321 std::unique_ptr<NavigationPreloadRequest> preload_request = 1359 std::unique_ptr<NavigationPreloadRequest> preload_request =
1322 preload_handle 1360 preload_handle
1323 ? base::MakeUnique<NavigationPreloadRequest>( 1361 ? base::MakeUnique<NavigationPreloadRequest>(
1324 fetch_event_id, request.url, std::move(preload_handle)) 1362 fetch_event_id, request.url, std::move(preload_handle))
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
1606 } 1644 }
1607 1645
1608 base::WeakPtr<ServiceWorkerContextClient> 1646 base::WeakPtr<ServiceWorkerContextClient>
1609 ServiceWorkerContextClient::GetWeakPtr() { 1647 ServiceWorkerContextClient::GetWeakPtr() {
1610 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); 1648 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread());
1611 DCHECK(context_); 1649 DCHECK(context_);
1612 return context_->weak_factory.GetWeakPtr(); 1650 return context_->weak_factory.GetWeakPtr();
1613 } 1651 }
1614 1652
1615 } // namespace content 1653 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698