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

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 and address falken, leon and mek's comments 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 <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 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 // worker thread. 249 // worker thread.
250 struct ServiceWorkerContextClient::WorkerContextData { 250 struct ServiceWorkerContextClient::WorkerContextData {
251 using ClientsCallbacksMap = 251 using ClientsCallbacksMap =
252 IDMap<std::unique_ptr<blink::WebServiceWorkerClientsCallbacks>>; 252 IDMap<std::unique_ptr<blink::WebServiceWorkerClientsCallbacks>>;
253 using ClaimClientsCallbacksMap = 253 using ClaimClientsCallbacksMap =
254 IDMap<std::unique_ptr<blink::WebServiceWorkerClientsClaimCallbacks>>; 254 IDMap<std::unique_ptr<blink::WebServiceWorkerClientsClaimCallbacks>>;
255 using ClientCallbacksMap = 255 using ClientCallbacksMap =
256 IDMap<std::unique_ptr<blink::WebServiceWorkerClientCallbacks>>; 256 IDMap<std::unique_ptr<blink::WebServiceWorkerClientCallbacks>>;
257 using SkipWaitingCallbacksMap = 257 using SkipWaitingCallbacksMap =
258 IDMap<std::unique_ptr<blink::WebServiceWorkerSkipWaitingCallbacks>>; 258 IDMap<std::unique_ptr<blink::WebServiceWorkerSkipWaitingCallbacks>>;
259 using InstallEventCallbacksMap =
260 IDMap<std::unique_ptr<const DispatchInstallEventCallback>>;
259 using ActivateEventCallbacksMap = 261 using ActivateEventCallbacksMap =
260 IDMap<std::unique_ptr<const DispatchActivateEventCallback>>; 262 IDMap<std::unique_ptr<const DispatchActivateEventCallback>>;
261 using BackgroundFetchAbortEventCallbacksMap = 263 using BackgroundFetchAbortEventCallbacksMap =
262 IDMap<std::unique_ptr<const DispatchBackgroundFetchAbortEventCallback>>; 264 IDMap<std::unique_ptr<const DispatchBackgroundFetchAbortEventCallback>>;
263 using BackgroundFetchClickEventCallbacksMap = 265 using BackgroundFetchClickEventCallbacksMap =
264 IDMap<std::unique_ptr<const DispatchBackgroundFetchClickEventCallback>>; 266 IDMap<std::unique_ptr<const DispatchBackgroundFetchClickEventCallback>>;
265 using BackgroundFetchFailEventCallbacksMap = 267 using BackgroundFetchFailEventCallbacksMap =
266 IDMap<std::unique_ptr<const DispatchBackgroundFetchFailEventCallback>>; 268 IDMap<std::unique_ptr<const DispatchBackgroundFetchFailEventCallback>>;
267 using BackgroundFetchedEventCallbacksMap = 269 using BackgroundFetchedEventCallbacksMap =
268 IDMap<std::unique_ptr<const DispatchBackgroundFetchedEventCallback>>; 270 IDMap<std::unique_ptr<const DispatchBackgroundFetchedEventCallback>>;
(...skipping 26 matching lines...) Expand all
295 297
296 // Pending callbacks for OpenWindow() and FocusClient(). 298 // Pending callbacks for OpenWindow() and FocusClient().
297 ClientCallbacksMap client_callbacks; 299 ClientCallbacksMap client_callbacks;
298 300
299 // Pending callbacks for SkipWaiting(). 301 // Pending callbacks for SkipWaiting().
300 SkipWaitingCallbacksMap skip_waiting_callbacks; 302 SkipWaitingCallbacksMap skip_waiting_callbacks;
301 303
302 // Pending callbacks for ClaimClients(). 304 // Pending callbacks for ClaimClients().
303 ClaimClientsCallbacksMap claim_clients_callbacks; 305 ClaimClientsCallbacksMap claim_clients_callbacks;
304 306
307 // Pending callbacks for Install Events.
308 InstallEventCallbacksMap install_event_callbacks;
309
305 // Pending callbacks for Activate Events. 310 // Pending callbacks for Activate Events.
306 ActivateEventCallbacksMap activate_event_callbacks; 311 ActivateEventCallbacksMap activate_event_callbacks;
307 312
308 // Pending callbacks for Background Fetch Abort Events. 313 // Pending callbacks for Background Fetch Abort Events.
309 BackgroundFetchAbortEventCallbacksMap background_fetch_abort_event_callbacks; 314 BackgroundFetchAbortEventCallbacksMap background_fetch_abort_event_callbacks;
310 315
311 // Pending callbacks for Background Fetch Click Events. 316 // Pending callbacks for Background Fetch Click Events.
312 BackgroundFetchClickEventCallbacksMap background_fetch_click_event_callbacks; 317 BackgroundFetchClickEventCallbacksMap background_fetch_click_event_callbacks;
313 318
314 // Pending callbacks for Background Fetch Fail Events. 319 // Pending callbacks for Background Fetch Fail Events.
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
493 498
494 ServiceWorkerContextClient::~ServiceWorkerContextClient() {} 499 ServiceWorkerContextClient::~ServiceWorkerContextClient() {}
495 500
496 void ServiceWorkerContextClient::OnMessageReceived( 501 void ServiceWorkerContextClient::OnMessageReceived(
497 int thread_id, 502 int thread_id,
498 int embedded_worker_id, 503 int embedded_worker_id,
499 const IPC::Message& message) { 504 const IPC::Message& message) {
500 CHECK_EQ(embedded_worker_id_, embedded_worker_id); 505 CHECK_EQ(embedded_worker_id_, embedded_worker_id);
501 bool handled = true; 506 bool handled = true;
502 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerContextClient, message) 507 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerContextClient, message)
503 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent, OnInstallEvent)
504 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClient, OnDidGetClient) 508 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClient, OnDidGetClient)
505 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClients, OnDidGetClients) 509 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClients, OnDidGetClients)
506 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowResponse, 510 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowResponse,
507 OnOpenWindowResponse) 511 OnOpenWindowResponse)
508 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowError, 512 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowError,
509 OnOpenWindowError) 513 OnOpenWindowError)
510 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FocusClientResponse, 514 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FocusClientResponse,
511 OnFocusClientResponse) 515 OnFocusClientResponse)
512 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NavigateClientResponse, 516 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NavigateClientResponse,
513 OnNavigateClientResponse) 517 OnNavigateClientResponse)
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
654 context, service_worker_version_id_, script_url_); 658 context, service_worker_version_id_, script_url_);
655 } 659 }
656 660
657 void ServiceWorkerContextClient::willDestroyWorkerContext( 661 void ServiceWorkerContextClient::willDestroyWorkerContext(
658 v8::Local<v8::Context> context) { 662 v8::Local<v8::Context> context) {
659 // At this point WillStopCurrentWorkerThread is already called, so 663 // At this point WillStopCurrentWorkerThread is already called, so
660 // worker_task_runner_->RunsTasksOnCurrentThread() returns false 664 // worker_task_runner_->RunsTasksOnCurrentThread() returns false
661 // (while we're still on the worker thread). 665 // (while we're still on the worker thread).
662 proxy_ = NULL; 666 proxy_ = NULL;
663 667
664 // Aborts all the pending events callbacks. 668 // Aborts the all pending install event callbacks.
669 for (WorkerContextData::InstallEventCallbacksMap::iterator it(
670 &context_->install_event_callbacks);
671 !it.IsAtEnd(); it.Advance()) {
672 it.GetCurrentValue()->Run(SERVICE_WORKER_ERROR_ABORT, false,
shimazu 2017/04/05 05:01:27 I think you can use AbortPendingEventCallbacks for
673 base::Time::Now());
674 }
675 // Aborts all other pending events callbacks.
665 AbortPendingEventCallbacks(context_->activate_event_callbacks); 676 AbortPendingEventCallbacks(context_->activate_event_callbacks);
666 AbortPendingEventCallbacks(context_->background_fetch_abort_event_callbacks); 677 AbortPendingEventCallbacks(context_->background_fetch_abort_event_callbacks);
667 AbortPendingEventCallbacks(context_->background_fetch_click_event_callbacks); 678 AbortPendingEventCallbacks(context_->background_fetch_click_event_callbacks);
668 AbortPendingEventCallbacks(context_->background_fetch_fail_event_callbacks); 679 AbortPendingEventCallbacks(context_->background_fetch_fail_event_callbacks);
669 AbortPendingEventCallbacks(context_->background_fetched_event_callbacks); 680 AbortPendingEventCallbacks(context_->background_fetched_event_callbacks);
670 AbortPendingEventCallbacks(context_->sync_event_callbacks); 681 AbortPendingEventCallbacks(context_->sync_event_callbacks);
671 AbortPendingEventCallbacks(context_->notification_click_event_callbacks); 682 AbortPendingEventCallbacks(context_->notification_click_event_callbacks);
672 AbortPendingEventCallbacks(context_->notification_close_event_callbacks); 683 AbortPendingEventCallbacks(context_->notification_close_event_callbacks);
673 AbortPendingEventCallbacks(context_->push_event_callbacks); 684 AbortPendingEventCallbacks(context_->push_event_callbacks);
674 AbortPendingEventCallbacks(context_->fetch_event_callbacks); 685 AbortPendingEventCallbacks(context_->fetch_event_callbacks);
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
825 DCHECK(callback); 836 DCHECK(callback);
826 callback->Run(EventResultToStatus(result), 837 callback->Run(EventResultToStatus(result),
827 base::Time::FromDoubleT(event_dispatch_time)); 838 base::Time::FromDoubleT(event_dispatch_time));
828 context_->message_event_callbacks.Remove(request_id); 839 context_->message_event_callbacks.Remove(request_id);
829 } 840 }
830 841
831 void ServiceWorkerContextClient::didHandleInstallEvent( 842 void ServiceWorkerContextClient::didHandleInstallEvent(
832 int request_id, 843 int request_id,
833 blink::WebServiceWorkerEventResult result, 844 blink::WebServiceWorkerEventResult result,
834 double event_dispatch_time) { 845 double event_dispatch_time) {
835 Send(new ServiceWorkerHostMsg_InstallEventFinished( 846 const DispatchInstallEventCallback* callback =
836 GetRoutingID(), request_id, result, proxy_->hasFetchEventHandler(), 847 context_->install_event_callbacks.Lookup(request_id);
837 base::Time::FromDoubleT(event_dispatch_time))); 848 DCHECK(callback);
849 callback->Run(EventResultToStatus(result), proxy_->hasFetchEventHandler(),
850 base::Time::FromDoubleT(event_dispatch_time));
851 context_->install_event_callbacks.Remove(request_id);
838 } 852 }
839 853
840 void ServiceWorkerContextClient::respondToFetchEvent( 854 void ServiceWorkerContextClient::respondToFetchEvent(
841 int fetch_event_id, 855 int fetch_event_id,
842 double event_dispatch_time) { 856 double event_dispatch_time) {
843 Send(new ServiceWorkerHostMsg_FetchEventResponse( 857 Send(new ServiceWorkerHostMsg_FetchEventResponse(
844 GetRoutingID(), fetch_event_id, 858 GetRoutingID(), fetch_event_id,
845 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, ServiceWorkerResponse(), 859 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, ServiceWorkerResponse(),
846 base::Time::FromDoubleT(event_dispatch_time))); 860 base::Time::FromDoubleT(event_dispatch_time)));
847 } 861 }
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
1014 void ServiceWorkerContextClient::claim( 1028 void ServiceWorkerContextClient::claim(
1015 std::unique_ptr<blink::WebServiceWorkerClientsClaimCallbacks> callbacks) { 1029 std::unique_ptr<blink::WebServiceWorkerClientsClaimCallbacks> callbacks) {
1016 DCHECK(callbacks); 1030 DCHECK(callbacks);
1017 int request_id = context_->claim_clients_callbacks.Add(std::move(callbacks)); 1031 int request_id = context_->claim_clients_callbacks.Add(std::move(callbacks));
1018 Send(new ServiceWorkerHostMsg_ClaimClients(GetRoutingID(), request_id)); 1032 Send(new ServiceWorkerHostMsg_ClaimClients(GetRoutingID(), request_id));
1019 } 1033 }
1020 1034
1021 void ServiceWorkerContextClient::registerForeignFetchScopes( 1035 void ServiceWorkerContextClient::registerForeignFetchScopes(
1022 const blink::WebVector<blink::WebURL>& sub_scopes, 1036 const blink::WebVector<blink::WebURL>& sub_scopes,
1023 const blink::WebVector<blink::WebSecurityOrigin>& origins) { 1037 const blink::WebVector<blink::WebSecurityOrigin>& origins) {
1024 Send(new ServiceWorkerHostMsg_RegisterForeignFetchScopes( 1038 std::vector<GURL> urls;
1025 GetRoutingID(), std::vector<GURL>(sub_scopes.begin(), sub_scopes.end()), 1039 for (const auto& url : sub_scopes)
1026 std::vector<url::Origin>(origins.begin(), origins.end()))); 1040 urls.push_back(url);
1041
1042 std::vector<url::Origin> url_origins;
1043 for (const auto& origin : origins)
1044 url_origins.push_back(origin);
1045
1046 install_method_client_->Register(urls, url_origins);
1027 } 1047 }
1028 1048
1029 void ServiceWorkerContextClient::DispatchSyncEvent( 1049 void ServiceWorkerContextClient::DispatchSyncEvent(
1030 const std::string& tag, 1050 const std::string& tag,
1031 blink::mojom::BackgroundSyncEventLastChance last_chance, 1051 blink::mojom::BackgroundSyncEventLastChance last_chance,
1032 const DispatchSyncEventCallback& callback) { 1052 const DispatchSyncEventCallback& callback) {
1033 TRACE_EVENT0("ServiceWorker", 1053 TRACE_EVENT0("ServiceWorker",
1034 "ServiceWorkerContextClient::DispatchSyncEvent"); 1054 "ServiceWorkerContextClient::DispatchSyncEvent");
1035 int request_id = context_->sync_event_callbacks.Add( 1055 int request_id = context_->sync_event_callbacks.Add(
1036 base::MakeUnique<SyncCallback>(callback)); 1056 base::MakeUnique<SyncCallback>(callback));
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
1164 fetches.size()); 1184 fetches.size());
1165 for (size_t i = 0; i < fetches.size(); ++i) { 1185 for (size_t i = 0; i < fetches.size(); ++i) {
1166 ToWebServiceWorkerRequest(fetches[i].request, &web_fetches[i].request); 1186 ToWebServiceWorkerRequest(fetches[i].request, &web_fetches[i].request);
1167 ToWebServiceWorkerResponse(fetches[i].response, &web_fetches[i].response); 1187 ToWebServiceWorkerResponse(fetches[i].response, &web_fetches[i].response);
1168 } 1188 }
1169 1189
1170 proxy_->dispatchBackgroundFetchedEvent( 1190 proxy_->dispatchBackgroundFetchedEvent(
1171 request_id, blink::WebString::fromUTF8(tag), web_fetches); 1191 request_id, blink::WebString::fromUTF8(tag), web_fetches);
1172 } 1192 }
1173 1193
1194 void ServiceWorkerContextClient::DispatchInstallEvent(
1195 mojom::InstallEventMethodAssociatedPtrInfo client,
1196 const DispatchInstallEventCallback& callback) {
1197 TRACE_EVENT0("ServiceWorker",
1198 "ServiceWorkerContextClient::DispatchInstallEvent");
1199
1200 install_method_client_.Bind(std::move(client));
1201
1202 int request_id = context_->install_event_callbacks.Add(
1203 base::MakeUnique<DispatchInstallEventCallback>(callback));
1204 proxy_->dispatchInstallEvent(request_id);
1205 }
1206
1174 void ServiceWorkerContextClient::DispatchExtendableMessageEvent( 1207 void ServiceWorkerContextClient::DispatchExtendableMessageEvent(
1175 mojom::ExtendableMessageEventPtr event, 1208 mojom::ExtendableMessageEventPtr event,
1176 const DispatchExtendableMessageEventCallback& callback) { 1209 const DispatchExtendableMessageEventCallback& callback) {
1177 TRACE_EVENT0("ServiceWorker", 1210 TRACE_EVENT0("ServiceWorker",
1178 "ServiceWorkerContextClient::DispatchExtendableMessageEvent"); 1211 "ServiceWorkerContextClient::DispatchExtendableMessageEvent");
1179 int request_id = context_->message_event_callbacks.Add( 1212 int request_id = context_->message_event_callbacks.Add(
1180 base::MakeUnique<DispatchExtendableMessageEventCallback>(callback)); 1213 base::MakeUnique<DispatchExtendableMessageEventCallback>(callback));
1181 1214
1182 blink::WebMessagePortChannelArray ports = 1215 blink::WebMessagePortChannelArray ports =
1183 WebMessagePortChannelImpl::CreateFromMessagePipeHandles( 1216 WebMessagePortChannelImpl::CreateFromMessagePipeHandles(
(...skipping 15 matching lines...) Expand all
1199 ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance( 1232 ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance(
1200 sender_.get(), main_thread_task_runner_.get()); 1233 sender_.get(), main_thread_task_runner_.get());
1201 scoped_refptr<WebServiceWorkerImpl> worker = 1234 scoped_refptr<WebServiceWorkerImpl> worker =
1202 dispatcher->GetOrCreateServiceWorker(std::move(handle)); 1235 dispatcher->GetOrCreateServiceWorker(std::move(handle));
1203 proxy_->dispatchExtendableMessageEvent( 1236 proxy_->dispatchExtendableMessageEvent(
1204 request_id, blink::WebString::fromUTF16(event->message), 1237 request_id, blink::WebString::fromUTF16(event->message),
1205 event->source_origin, std::move(ports), 1238 event->source_origin, std::move(ports),
1206 WebServiceWorkerImpl::CreateHandle(worker)); 1239 WebServiceWorkerImpl::CreateHandle(worker));
1207 } 1240 }
1208 1241
1209 void ServiceWorkerContextClient::OnInstallEvent(int request_id) {
1210 TRACE_EVENT0("ServiceWorker",
1211 "ServiceWorkerContextClient::OnInstallEvent");
1212 proxy_->dispatchInstallEvent(request_id);
1213 }
1214
1215 void ServiceWorkerContextClient::DispatchFetchEvent( 1242 void ServiceWorkerContextClient::DispatchFetchEvent(
1216 int fetch_event_id, 1243 int fetch_event_id,
1217 const ServiceWorkerFetchRequest& request, 1244 const ServiceWorkerFetchRequest& request,
1218 mojom::FetchEventPreloadHandlePtr preload_handle, 1245 mojom::FetchEventPreloadHandlePtr preload_handle,
1219 const DispatchFetchEventCallback& callback) { 1246 const DispatchFetchEventCallback& callback) {
1220 std::unique_ptr<NavigationPreloadRequest> preload_request = 1247 std::unique_ptr<NavigationPreloadRequest> preload_request =
1221 preload_handle 1248 preload_handle
1222 ? base::MakeUnique<NavigationPreloadRequest>( 1249 ? base::MakeUnique<NavigationPreloadRequest>(
1223 fetch_event_id, request.url, std::move(preload_handle)) 1250 fetch_event_id, request.url, std::move(preload_handle))
1224 : nullptr; 1251 : nullptr;
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after
1503 } 1530 }
1504 1531
1505 base::WeakPtr<ServiceWorkerContextClient> 1532 base::WeakPtr<ServiceWorkerContextClient>
1506 ServiceWorkerContextClient::GetWeakPtr() { 1533 ServiceWorkerContextClient::GetWeakPtr() {
1507 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); 1534 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread());
1508 DCHECK(context_); 1535 DCHECK(context_);
1509 return context_->weak_factory.GetWeakPtr(); 1536 return context_->weak_factory.GetWeakPtr();
1510 } 1537 }
1511 1538
1512 } // namespace content 1539 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698