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

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

Issue 2620333003: Reland of [ServiceWorker] Mojofy PushEvent of Service Worker. (Closed)
Patch Set: Rebase Created 3 years, 11 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
« no previous file with comments | « content/renderer/service_worker/service_worker_context_client.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 web_client_info.uuid = blink::WebString::fromASCII(client_info.client_uuid); 173 web_client_info.uuid = blink::WebString::fromASCII(client_info.client_uuid);
174 web_client_info.pageVisibilityState = client_info.page_visibility_state; 174 web_client_info.pageVisibilityState = client_info.page_visibility_state;
175 web_client_info.isFocused = client_info.is_focused; 175 web_client_info.isFocused = client_info.is_focused;
176 web_client_info.url = client_info.url; 176 web_client_info.url = client_info.url;
177 web_client_info.frameType = GetBlinkFrameType(client_info.frame_type); 177 web_client_info.frameType = GetBlinkFrameType(client_info.frame_type);
178 web_client_info.clientType = client_info.client_type; 178 web_client_info.clientType = client_info.client_type;
179 179
180 return web_client_info; 180 return web_client_info;
181 } 181 }
182 182
183 // Use this template in willDestroyWorkerContext to abort all the pending
184 // events callbacks.
185 template <typename T>
186 void AbortPendingEventCallbacks(T& callbacks) {
187 for (typename T::iterator it(&callbacks); !it.IsAtEnd(); it.Advance()) {
188 it.GetCurrentValue()->Run(SERVICE_WORKER_ERROR_ABORT, base::Time::Now());
189 }
190 }
191
183 } // namespace 192 } // namespace
184 193
185 // Holding data that needs to be bound to the worker context on the 194 // Holding data that needs to be bound to the worker context on the
186 // worker thread. 195 // worker thread.
187 struct ServiceWorkerContextClient::WorkerContextData { 196 struct ServiceWorkerContextClient::WorkerContextData {
188 using ClientsCallbacksMap = 197 using ClientsCallbacksMap =
189 IDMap<std::unique_ptr<blink::WebServiceWorkerClientsCallbacks>>; 198 IDMap<std::unique_ptr<blink::WebServiceWorkerClientsCallbacks>>;
190 using ClaimClientsCallbacksMap = 199 using ClaimClientsCallbacksMap =
191 IDMap<std::unique_ptr<blink::WebServiceWorkerClientsClaimCallbacks>>; 200 IDMap<std::unique_ptr<blink::WebServiceWorkerClientsClaimCallbacks>>;
192 using ClientCallbacksMap = 201 using ClientCallbacksMap =
193 IDMap<std::unique_ptr<blink::WebServiceWorkerClientCallbacks>>; 202 IDMap<std::unique_ptr<blink::WebServiceWorkerClientCallbacks>>;
194 using SkipWaitingCallbacksMap = 203 using SkipWaitingCallbacksMap =
195 IDMap<std::unique_ptr<blink::WebServiceWorkerSkipWaitingCallbacks>>; 204 IDMap<std::unique_ptr<blink::WebServiceWorkerSkipWaitingCallbacks>>;
196 using SyncEventCallbacksMap = IDMap<std::unique_ptr<const SyncCallback>>; 205 using SyncEventCallbacksMap = IDMap<std::unique_ptr<const SyncCallback>>;
206 using PushEventCallbacksMap =
207 IDMap<std::unique_ptr<const DispatchPushEventCallback>>;
197 using FetchEventCallbacksMap = IDMap<std::unique_ptr<const FetchCallback>>; 208 using FetchEventCallbacksMap = IDMap<std::unique_ptr<const FetchCallback>>;
198 using ExtendableMessageEventCallbacksMap = 209 using ExtendableMessageEventCallbacksMap =
199 IDMap<std::unique_ptr<const DispatchExtendableMessageEventCallback>>; 210 IDMap<std::unique_ptr<const DispatchExtendableMessageEventCallback>>;
200 using NavigationPreloadRequestsMap = IDMap< 211 using NavigationPreloadRequestsMap = IDMap<
201 std::unique_ptr<ServiceWorkerContextClient::NavigationPreloadRequest>>; 212 std::unique_ptr<ServiceWorkerContextClient::NavigationPreloadRequest>>;
202 213
203 explicit WorkerContextData(ServiceWorkerContextClient* owner) 214 explicit WorkerContextData(ServiceWorkerContextClient* owner)
204 : event_dispatcher_binding(owner), 215 : event_dispatcher_binding(owner),
205 weak_factory(owner), 216 weak_factory(owner),
206 proxy_weak_factory(owner->proxy_) {} 217 proxy_weak_factory(owner->proxy_) {}
(...skipping 12 matching lines...) Expand all
219 230
220 // Pending callbacks for SkipWaiting(). 231 // Pending callbacks for SkipWaiting().
221 SkipWaitingCallbacksMap skip_waiting_callbacks; 232 SkipWaitingCallbacksMap skip_waiting_callbacks;
222 233
223 // Pending callbacks for ClaimClients(). 234 // Pending callbacks for ClaimClients().
224 ClaimClientsCallbacksMap claim_clients_callbacks; 235 ClaimClientsCallbacksMap claim_clients_callbacks;
225 236
226 // Pending callbacks for Background Sync Events. 237 // Pending callbacks for Background Sync Events.
227 SyncEventCallbacksMap sync_event_callbacks; 238 SyncEventCallbacksMap sync_event_callbacks;
228 239
240 // Pending callbacks for Push Events.
241 PushEventCallbacksMap push_event_callbacks;
242
229 // Pending callbacks for Fetch Events. 243 // Pending callbacks for Fetch Events.
230 FetchEventCallbacksMap fetch_event_callbacks; 244 FetchEventCallbacksMap fetch_event_callbacks;
231 245
232 // Pending callbacks for Extendable Message Events. 246 // Pending callbacks for Extendable Message Events.
233 ExtendableMessageEventCallbacksMap message_event_callbacks; 247 ExtendableMessageEventCallbacksMap message_event_callbacks;
234 248
235 // Pending navigation preload requests. 249 // Pending navigation preload requests.
236 NavigationPreloadRequestsMap preload_requests; 250 NavigationPreloadRequestsMap preload_requests;
237 251
238 base::ThreadChecker thread_checker; 252 base::ThreadChecker thread_checker;
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
407 const IPC::Message& message) { 421 const IPC::Message& message) {
408 CHECK_EQ(embedded_worker_id_, embedded_worker_id); 422 CHECK_EQ(embedded_worker_id_, embedded_worker_id);
409 bool handled = true; 423 bool handled = true;
410 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerContextClient, message) 424 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerContextClient, message)
411 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ActivateEvent, OnActivateEvent) 425 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ActivateEvent, OnActivateEvent)
412 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent, OnInstallEvent) 426 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent, OnInstallEvent)
413 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NotificationClickEvent, 427 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NotificationClickEvent,
414 OnNotificationClickEvent) 428 OnNotificationClickEvent)
415 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NotificationCloseEvent, 429 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NotificationCloseEvent,
416 OnNotificationCloseEvent) 430 OnNotificationCloseEvent)
417 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_PushEvent, OnPushEvent)
418 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClient, OnDidGetClient) 431 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClient, OnDidGetClient)
419 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClients, OnDidGetClients) 432 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClients, OnDidGetClients)
420 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowResponse, 433 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowResponse,
421 OnOpenWindowResponse) 434 OnOpenWindowResponse)
422 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowError, 435 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowError,
423 OnOpenWindowError) 436 OnOpenWindowError)
424 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FocusClientResponse, 437 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FocusClientResponse,
425 OnFocusClientResponse) 438 OnFocusClientResponse)
426 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NavigateClientResponse, 439 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NavigateClientResponse,
427 OnNavigateClientResponse) 440 OnNavigateClientResponse)
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
569 context, service_worker_version_id_, script_url_); 582 context, service_worker_version_id_, script_url_);
570 } 583 }
571 584
572 void ServiceWorkerContextClient::willDestroyWorkerContext( 585 void ServiceWorkerContextClient::willDestroyWorkerContext(
573 v8::Local<v8::Context> context) { 586 v8::Local<v8::Context> context) {
574 // At this point WillStopCurrentWorkerThread is already called, so 587 // At this point WillStopCurrentWorkerThread is already called, so
575 // worker_task_runner_->RunsTasksOnCurrentThread() returns false 588 // worker_task_runner_->RunsTasksOnCurrentThread() returns false
576 // (while we're still on the worker thread). 589 // (while we're still on the worker thread).
577 proxy_ = NULL; 590 proxy_ = NULL;
578 591
579 // Aborts the all pending sync event callbacks. 592 // Aborts all the pending events callbacks.
580 for (WorkerContextData::SyncEventCallbacksMap::iterator it( 593 AbortPendingEventCallbacks(context_->sync_event_callbacks);
581 &context_->sync_event_callbacks); 594 AbortPendingEventCallbacks(context_->push_event_callbacks);
582 !it.IsAtEnd(); it.Advance()) { 595 AbortPendingEventCallbacks(context_->fetch_event_callbacks);
583 it.GetCurrentValue()->Run(SERVICE_WORKER_ERROR_ABORT, base::Time::Now()); 596 AbortPendingEventCallbacks(context_->message_event_callbacks);
584 }
585 // Aborts the all pending fetch event callbacks.
586 for (WorkerContextData::FetchEventCallbacksMap::iterator it(
587 &context_->fetch_event_callbacks);
588 !it.IsAtEnd(); it.Advance()) {
589 it.GetCurrentValue()->Run(SERVICE_WORKER_ERROR_ABORT, base::Time::Now());
590 }
591 // Aborts the all pending extendable message event callbacks.
592 for (WorkerContextData::ExtendableMessageEventCallbacksMap::iterator it(
593 &context_->message_event_callbacks);
594 !it.IsAtEnd(); it.Advance()) {
595 it.GetCurrentValue()->Run(SERVICE_WORKER_ERROR_ABORT, base::Time::Now());
596 }
597 597
598 // We have to clear callbacks now, as they need to be freed on the 598 // We have to clear callbacks now, as they need to be freed on the
599 // same thread. 599 // same thread.
600 context_.reset(); 600 context_.reset();
601 601
602 // This also lets the message filter stop dispatching messages to 602 // This also lets the message filter stop dispatching messages to
603 // this client. 603 // this client.
604 g_worker_client_tls.Pointer()->Set(NULL); 604 g_worker_client_tls.Pointer()->Set(NULL);
605 605
606 GetContentClient()->renderer()->WillDestroyServiceWorkerContextOnWorkerThread( 606 GetContentClient()->renderer()->WillDestroyServiceWorkerContextOnWorkerThread(
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
753 double event_dispatch_time) { 753 double event_dispatch_time) {
754 Send(new ServiceWorkerHostMsg_NotificationCloseEventFinished( 754 Send(new ServiceWorkerHostMsg_NotificationCloseEventFinished(
755 GetRoutingID(), request_id, result, 755 GetRoutingID(), request_id, result,
756 base::Time::FromDoubleT(event_dispatch_time))); 756 base::Time::FromDoubleT(event_dispatch_time)));
757 } 757 }
758 758
759 void ServiceWorkerContextClient::didHandlePushEvent( 759 void ServiceWorkerContextClient::didHandlePushEvent(
760 int request_id, 760 int request_id,
761 blink::WebServiceWorkerEventResult result, 761 blink::WebServiceWorkerEventResult result,
762 double event_dispatch_time) { 762 double event_dispatch_time) {
763 Send(new ServiceWorkerHostMsg_PushEventFinished( 763 const DispatchPushEventCallback* callback =
764 GetRoutingID(), request_id, result, 764 context_->push_event_callbacks.Lookup(request_id);
765 base::Time::FromDoubleT(event_dispatch_time))); 765 DCHECK(callback);
766 callback->Run(result == blink::WebServiceWorkerEventResultCompleted
767 ? SERVICE_WORKER_OK
768 : SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED,
769 base::Time::FromDoubleT(event_dispatch_time));
770 context_->push_event_callbacks.Remove(request_id);
766 } 771 }
767 772
768 void ServiceWorkerContextClient::didHandleSyncEvent( 773 void ServiceWorkerContextClient::didHandleSyncEvent(
769 int request_id, 774 int request_id,
770 blink::WebServiceWorkerEventResult result, 775 blink::WebServiceWorkerEventResult result,
771 double event_dispatch_time) { 776 double event_dispatch_time) {
772 const SyncCallback* callback = 777 const SyncCallback* callback =
773 context_->sync_event_callbacks.Lookup(request_id); 778 context_->sync_event_callbacks.Lookup(request_id);
774 DCHECK(callback); 779 DCHECK(callback);
775 callback->Run(EventResultToStatus(result), 780 callback->Run(EventResultToStatus(result),
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after
1046 int request_id, 1051 int request_id,
1047 const std::string& notification_id, 1052 const std::string& notification_id,
1048 const PlatformNotificationData& notification_data) { 1053 const PlatformNotificationData& notification_data) {
1049 TRACE_EVENT0("ServiceWorker", 1054 TRACE_EVENT0("ServiceWorker",
1050 "ServiceWorkerContextClient::OnNotificationCloseEvent"); 1055 "ServiceWorkerContextClient::OnNotificationCloseEvent");
1051 proxy_->dispatchNotificationCloseEvent( 1056 proxy_->dispatchNotificationCloseEvent(
1052 request_id, blink::WebString::fromUTF8(notification_id), 1057 request_id, blink::WebString::fromUTF8(notification_id),
1053 ToWebNotificationData(notification_data)); 1058 ToWebNotificationData(notification_data));
1054 } 1059 }
1055 1060
1056 void ServiceWorkerContextClient::OnPushEvent(int request_id, 1061 void ServiceWorkerContextClient::DispatchPushEvent(
1057 const PushEventPayload& payload) { 1062 const PushEventPayload& payload,
1063 const DispatchPushEventCallback& callback) {
1058 TRACE_EVENT0("ServiceWorker", 1064 TRACE_EVENT0("ServiceWorker",
1059 "ServiceWorkerContextClient::OnPushEvent"); 1065 "ServiceWorkerContextClient::DispatchPushEvent");
1066 int request_id = context_->push_event_callbacks.Add(
1067 base::MakeUnique<DispatchPushEventCallback>(callback));
1068
1060 // Only set data to be a valid string if the payload had decrypted data. 1069 // Only set data to be a valid string if the payload had decrypted data.
1061 blink::WebString data; 1070 blink::WebString data;
1062 if (!payload.is_null) 1071 if (!payload.is_null)
1063 data.assign(blink::WebString::fromUTF8(payload.data)); 1072 data.assign(blink::WebString::fromUTF8(payload.data));
1064 proxy_->dispatchPushEvent(request_id, data); 1073 proxy_->dispatchPushEvent(request_id, data);
1065 } 1074 }
1066 1075
1067 void ServiceWorkerContextClient::OnDidGetClient( 1076 void ServiceWorkerContextClient::OnDidGetClient(
1068 int request_id, 1077 int request_id,
1069 const ServiceWorkerClientInfo& client) { 1078 const ServiceWorkerClientInfo& client) {
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
1274 } 1283 }
1275 1284
1276 base::WeakPtr<ServiceWorkerContextClient> 1285 base::WeakPtr<ServiceWorkerContextClient>
1277 ServiceWorkerContextClient::GetWeakPtr() { 1286 ServiceWorkerContextClient::GetWeakPtr() {
1278 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); 1287 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread());
1279 DCHECK(context_); 1288 DCHECK(context_);
1280 return context_->weak_factory.GetWeakPtr(); 1289 return context_->weak_factory.GetWeakPtr();
1281 } 1290 }
1282 1291
1283 } // namespace content 1292 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/service_worker/service_worker_context_client.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698