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

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

Issue 1210643002: Update navigator.services API to use the new services.onconnect event [2/3]. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@serviceport
Patch Set: properly run error callbacks when worker is stopped Created 5 years, 5 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 "base/lazy_instance.h" 7 #include "base/lazy_instance.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/metrics/histogram.h" 9 #include "base/metrics/histogram.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
11 #include "base/thread_task_runner_handle.h" 11 #include "base/thread_task_runner_handle.h"
12 #include "base/threading/thread_checker.h" 12 #include "base/threading/thread_checker.h"
13 #include "base/threading/thread_local.h" 13 #include "base/threading/thread_local.h"
14 #include "base/trace_event/trace_event.h" 14 #include "base/trace_event/trace_event.h"
15 #include "content/child/navigator_connect/service_port_dispatcher_impl.h"
15 #include "content/child/notifications/notification_data_conversions.h" 16 #include "content/child/notifications/notification_data_conversions.h"
16 #include "content/child/request_extra_data.h" 17 #include "content/child/request_extra_data.h"
17 #include "content/child/service_worker/service_worker_dispatcher.h" 18 #include "content/child/service_worker/service_worker_dispatcher.h"
18 #include "content/child/service_worker/service_worker_network_provider.h" 19 #include "content/child/service_worker/service_worker_network_provider.h"
19 #include "content/child/service_worker/service_worker_provider_context.h" 20 #include "content/child/service_worker/service_worker_provider_context.h"
20 #include "content/child/service_worker/service_worker_registration_handle_refere nce.h" 21 #include "content/child/service_worker/service_worker_registration_handle_refere nce.h"
21 #include "content/child/service_worker/web_service_worker_impl.h" 22 #include "content/child/service_worker/web_service_worker_impl.h"
22 #include "content/child/service_worker/web_service_worker_provider_impl.h" 23 #include "content/child/service_worker/web_service_worker_provider_impl.h"
23 #include "content/child/service_worker/web_service_worker_registration_impl.h" 24 #include "content/child/service_worker/web_service_worker_registration_impl.h"
24 #include "content/child/thread_safe_sender.h" 25 #include "content/child/thread_safe_sender.h"
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 using ClientsCallbacksMap = 175 using ClientsCallbacksMap =
175 IDMap<blink::WebServiceWorkerClientsCallbacks, IDMapOwnPointer>; 176 IDMap<blink::WebServiceWorkerClientsCallbacks, IDMapOwnPointer>;
176 using ClaimClientsCallbacksMap = 177 using ClaimClientsCallbacksMap =
177 IDMap<blink::WebServiceWorkerClientsClaimCallbacks, IDMapOwnPointer>; 178 IDMap<blink::WebServiceWorkerClientsClaimCallbacks, IDMapOwnPointer>;
178 using ClientCallbacksMap = 179 using ClientCallbacksMap =
179 IDMap<blink::WebServiceWorkerClientCallbacks, IDMapOwnPointer>; 180 IDMap<blink::WebServiceWorkerClientCallbacks, IDMapOwnPointer>;
180 using SkipWaitingCallbacksMap = 181 using SkipWaitingCallbacksMap =
181 IDMap<blink::WebServiceWorkerSkipWaitingCallbacks, IDMapOwnPointer>; 182 IDMap<blink::WebServiceWorkerSkipWaitingCallbacks, IDMapOwnPointer>;
182 183
183 explicit WorkerContextData(ServiceWorkerContextClient* owner) 184 explicit WorkerContextData(ServiceWorkerContextClient* owner)
184 : weak_factory(owner) {} 185 : weak_factory(owner), proxy_weak_factory(owner->proxy_) {}
185 186
186 ~WorkerContextData() { 187 ~WorkerContextData() {
187 DCHECK(thread_checker.CalledOnValidThread()); 188 DCHECK(thread_checker.CalledOnValidThread());
188 } 189 }
189 190
190 // Pending callbacks for GetClientDocuments(). 191 // Pending callbacks for GetClientDocuments().
191 ClientsCallbacksMap clients_callbacks; 192 ClientsCallbacksMap clients_callbacks;
192 193
193 // Pending callbacks for OpenWindow() and FocusClient(). 194 // Pending callbacks for OpenWindow() and FocusClient().
194 ClientCallbacksMap client_callbacks; 195 ClientCallbacksMap client_callbacks;
195 196
196 // Pending callbacks for SkipWaiting(). 197 // Pending callbacks for SkipWaiting().
197 SkipWaitingCallbacksMap skip_waiting_callbacks; 198 SkipWaitingCallbacksMap skip_waiting_callbacks;
198 199
199 // Pending callbacks for ClaimClients(). 200 // Pending callbacks for ClaimClients().
200 ClaimClientsCallbacksMap claim_clients_callbacks; 201 ClaimClientsCallbacksMap claim_clients_callbacks;
201 202
202 ServiceRegistryImpl service_registry; 203 ServiceRegistryImpl service_registry;
203 204
204 base::ThreadChecker thread_checker; 205 base::ThreadChecker thread_checker;
205 base::WeakPtrFactory<ServiceWorkerContextClient> weak_factory; 206 base::WeakPtrFactory<ServiceWorkerContextClient> weak_factory;
207 base::WeakPtrFactory<blink::WebServiceWorkerContextProxy> proxy_weak_factory;
206 }; 208 };
207 209
208 ServiceWorkerContextClient* 210 ServiceWorkerContextClient*
209 ServiceWorkerContextClient::ThreadSpecificInstance() { 211 ServiceWorkerContextClient::ThreadSpecificInstance() {
210 return g_worker_client_tls.Pointer()->Get(); 212 return g_worker_client_tls.Pointer()->Get();
211 } 213 }
212 214
213 ServiceWorkerContextClient::ServiceWorkerContextClient( 215 ServiceWorkerContextClient::ServiceWorkerContextClient(
214 int embedded_worker_id, 216 int embedded_worker_id,
215 int64 service_worker_version_id, 217 int64 service_worker_version_id,
(...skipping 28 matching lines...) Expand all
244 bool handled = true; 246 bool handled = true;
245 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerContextClient, message) 247 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerContextClient, message)
246 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ActivateEvent, OnActivateEvent) 248 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ActivateEvent, OnActivateEvent)
247 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FetchEvent, OnFetchEvent) 249 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FetchEvent, OnFetchEvent)
248 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent, OnInstallEvent) 250 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent, OnInstallEvent)
249 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_SyncEvent, OnSyncEvent) 251 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_SyncEvent, OnSyncEvent)
250 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NotificationClickEvent, 252 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NotificationClickEvent,
251 OnNotificationClickEvent) 253 OnNotificationClickEvent)
252 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_PushEvent, OnPushEvent) 254 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_PushEvent, OnPushEvent)
253 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_GeofencingEvent, OnGeofencingEvent) 255 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_GeofencingEvent, OnGeofencingEvent)
254 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CrossOriginConnectEvent,
255 OnCrossOriginConnectEvent)
256 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_MessageToWorker, OnPostMessage) 256 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_MessageToWorker, OnPostMessage)
257 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CrossOriginMessageToWorker, 257 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CrossOriginMessageToWorker,
258 OnCrossOriginMessageToWorker) 258 OnCrossOriginMessageToWorker)
259 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_SendStashedMessagePorts, 259 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_SendStashedMessagePorts,
260 OnSendStashedMessagePorts) 260 OnSendStashedMessagePorts)
261 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClients, OnDidGetClients) 261 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClients, OnDidGetClients)
262 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowResponse, 262 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowResponse,
263 OnOpenWindowResponse) 263 OnOpenWindowResponse)
264 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowError, 264 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowError,
265 OnOpenWindowError) 265 OnOpenWindowError)
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 DCHECK(g_worker_client_tls.Pointer()->Get() == NULL); 342 DCHECK(g_worker_client_tls.Pointer()->Get() == NULL);
343 DCHECK(!proxy_); 343 DCHECK(!proxy_);
344 g_worker_client_tls.Pointer()->Set(this); 344 g_worker_client_tls.Pointer()->Set(this);
345 proxy_ = proxy; 345 proxy_ = proxy;
346 346
347 // Initialize pending callback maps. This needs to be freed on the 347 // Initialize pending callback maps. This needs to be freed on the
348 // same thread before the worker context goes away in 348 // same thread before the worker context goes away in
349 // willDestroyWorkerContext. 349 // willDestroyWorkerContext.
350 context_.reset(new WorkerContextData(this)); 350 context_.reset(new WorkerContextData(this));
351 351
352 // Register Mojo services.
353 context_->service_registry.ServiceRegistry::AddService(
354 base::Bind(&ServicePortDispatcherImpl::Create,
355 context_->proxy_weak_factory.GetWeakPtr()));
356
352 SetRegistrationInServiceWorkerGlobalScope(); 357 SetRegistrationInServiceWorkerGlobalScope();
353 358
354 Send(new EmbeddedWorkerHostMsg_WorkerScriptLoaded( 359 Send(new EmbeddedWorkerHostMsg_WorkerScriptLoaded(
355 embedded_worker_id_, 360 embedded_worker_id_,
356 WorkerTaskRunner::Instance()->CurrentWorkerId(), 361 WorkerTaskRunner::Instance()->CurrentWorkerId(),
357 provider_context_->provider_id())); 362 provider_context_->provider_id()));
358 363
359 TRACE_EVENT_ASYNC_STEP_INTO0( 364 TRACE_EVENT_ASYNC_STEP_INTO0(
360 "ServiceWorker", 365 "ServiceWorker",
361 "ServiceWorkerContextClient::StartingWorkerContext", 366 "ServiceWorkerContextClient::StartingWorkerContext",
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
490 GetRoutingID(), request_id, result)); 495 GetRoutingID(), request_id, result));
491 } 496 }
492 497
493 void ServiceWorkerContextClient::didHandleSyncEvent( 498 void ServiceWorkerContextClient::didHandleSyncEvent(
494 int request_id, 499 int request_id,
495 blink::WebServiceWorkerEventResult result) { 500 blink::WebServiceWorkerEventResult result) {
496 Send(new ServiceWorkerHostMsg_SyncEventFinished(GetRoutingID(), request_id, 501 Send(new ServiceWorkerHostMsg_SyncEventFinished(GetRoutingID(), request_id,
497 result)); 502 result));
498 } 503 }
499 504
500 void ServiceWorkerContextClient::didHandleCrossOriginConnectEvent(
501 int request_id,
502 bool accept_connection) {
503 Send(new ServiceWorkerHostMsg_CrossOriginConnectEventFinished(
504 GetRoutingID(), request_id, accept_connection));
505 }
506
507 blink::WebServiceWorkerNetworkProvider* 505 blink::WebServiceWorkerNetworkProvider*
508 ServiceWorkerContextClient::createServiceWorkerNetworkProvider( 506 ServiceWorkerContextClient::createServiceWorkerNetworkProvider(
509 blink::WebDataSource* data_source) { 507 blink::WebDataSource* data_source) {
510 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread()); 508 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread());
511 509
512 // Create a content::ServiceWorkerNetworkProvider for this data source so 510 // Create a content::ServiceWorkerNetworkProvider for this data source so
513 // we can observe its requests. 511 // we can observe its requests.
514 scoped_ptr<ServiceWorkerNetworkProvider> provider( 512 scoped_ptr<ServiceWorkerNetworkProvider> provider(
515 new ServiceWorkerNetworkProvider( 513 new ServiceWorkerNetworkProvider(
516 MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_CONTROLLER)); 514 MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_CONTROLLER));
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
729 const std::string& region_id, 727 const std::string& region_id,
730 const blink::WebCircularGeofencingRegion& region) { 728 const blink::WebCircularGeofencingRegion& region) {
731 TRACE_EVENT0("ServiceWorker", 729 TRACE_EVENT0("ServiceWorker",
732 "ServiceWorkerContextClient::OnGeofencingEvent"); 730 "ServiceWorkerContextClient::OnGeofencingEvent");
733 proxy_->dispatchGeofencingEvent( 731 proxy_->dispatchGeofencingEvent(
734 request_id, event_type, blink::WebString::fromUTF8(region_id), region); 732 request_id, event_type, blink::WebString::fromUTF8(region_id), region);
735 Send(new ServiceWorkerHostMsg_GeofencingEventFinished(GetRoutingID(), 733 Send(new ServiceWorkerHostMsg_GeofencingEventFinished(GetRoutingID(),
736 request_id)); 734 request_id));
737 } 735 }
738 736
739 void ServiceWorkerContextClient::OnCrossOriginConnectEvent(
740 int request_id,
741 const NavigatorConnectClient& client) {
742 TRACE_EVENT0("ServiceWorker",
743 "ServiceWorkerContextClient::OnCrossOriginConnectEvent");
744 blink::WebCrossOriginServiceWorkerClient web_client;
745 web_client.origin = client.origin;
746 web_client.targetURL = client.target_url;
747 web_client.clientID = client.message_port_id;
748 proxy_->dispatchCrossOriginConnectEvent(request_id, web_client);
749 }
750
751 void ServiceWorkerContextClient::OnPostMessage( 737 void ServiceWorkerContextClient::OnPostMessage(
752 const base::string16& message, 738 const base::string16& message,
753 const std::vector<TransferredMessagePort>& sent_message_ports, 739 const std::vector<TransferredMessagePort>& sent_message_ports,
754 const std::vector<int>& new_routing_ids) { 740 const std::vector<int>& new_routing_ids) {
755 TRACE_EVENT0("ServiceWorker", 741 TRACE_EVENT0("ServiceWorker",
756 "ServiceWorkerContextClient::OnPostEvent"); 742 "ServiceWorkerContextClient::OnPostEvent");
757 blink::WebMessagePortChannelArray ports = 743 blink::WebMessagePortChannelArray ports =
758 WebMessagePortChannelImpl::CreatePorts( 744 WebMessagePortChannelImpl::CreatePorts(
759 sent_message_ports, new_routing_ids, 745 sent_message_ports, new_routing_ids,
760 main_thread_task_runner_); 746 main_thread_task_runner_);
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
938 } 924 }
939 925
940 base::WeakPtr<ServiceWorkerContextClient> 926 base::WeakPtr<ServiceWorkerContextClient>
941 ServiceWorkerContextClient::GetWeakPtr() { 927 ServiceWorkerContextClient::GetWeakPtr() {
942 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); 928 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread());
943 DCHECK(context_); 929 DCHECK(context_);
944 return context_->weak_factory.GetWeakPtr(); 930 return context_->weak_factory.GetWeakPtr();
945 } 931 }
946 932
947 } // namespace content 933 } // 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