OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/child/service_worker/service_worker_dispatcher.h" | 5 #include "content/child/service_worker/service_worker_dispatcher.h" |
6 | 6 |
| 7 #include "base/debug/trace_event.h" |
7 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
8 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
9 #include "base/threading/thread_local.h" | 10 #include "base/threading/thread_local.h" |
10 #include "content/child/child_thread.h" | 11 #include "content/child/child_thread.h" |
11 #include "content/child/service_worker/service_worker_handle_reference.h" | 12 #include "content/child/service_worker/service_worker_handle_reference.h" |
12 #include "content/child/service_worker/service_worker_provider_context.h" | 13 #include "content/child/service_worker/service_worker_provider_context.h" |
13 #include "content/child/service_worker/service_worker_registration_handle_refere
nce.h" | 14 #include "content/child/service_worker/service_worker_registration_handle_refere
nce.h" |
14 #include "content/child/service_worker/web_service_worker_impl.h" | 15 #include "content/child/service_worker/web_service_worker_impl.h" |
15 #include "content/child/service_worker/web_service_worker_registration_impl.h" | 16 #include "content/child/service_worker/web_service_worker_registration_impl.h" |
16 #include "content/child/thread_safe_sender.h" | 17 #include "content/child/thread_safe_sender.h" |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 script_url.possibly_invalid_spec().size() > GetMaxURLChars()) { | 91 script_url.possibly_invalid_spec().size() > GetMaxURLChars()) { |
91 scoped_ptr<WebServiceWorkerRegistrationCallbacks> | 92 scoped_ptr<WebServiceWorkerRegistrationCallbacks> |
92 owned_callbacks(callbacks); | 93 owned_callbacks(callbacks); |
93 scoped_ptr<WebServiceWorkerError> error(new WebServiceWorkerError( | 94 scoped_ptr<WebServiceWorkerError> error(new WebServiceWorkerError( |
94 WebServiceWorkerError::ErrorTypeSecurity, "URL too long")); | 95 WebServiceWorkerError::ErrorTypeSecurity, "URL too long")); |
95 callbacks->onError(error.release()); | 96 callbacks->onError(error.release()); |
96 return; | 97 return; |
97 } | 98 } |
98 | 99 |
99 int request_id = pending_registration_callbacks_.Add(callbacks); | 100 int request_id = pending_registration_callbacks_.Add(callbacks); |
| 101 TRACE_EVENT_ASYNC_BEGIN2("ServiceWorker", |
| 102 "ServiceWorkerDispatcher::RegisterServiceWorker", |
| 103 request_id, |
| 104 "Pettern", pattern.spec(), |
| 105 "Script URL", script_url.spec()); |
100 thread_safe_sender_->Send(new ServiceWorkerHostMsg_RegisterServiceWorker( | 106 thread_safe_sender_->Send(new ServiceWorkerHostMsg_RegisterServiceWorker( |
101 CurrentWorkerId(), request_id, provider_id, pattern, script_url)); | 107 CurrentWorkerId(), request_id, provider_id, pattern, script_url)); |
102 } | 108 } |
103 | 109 |
104 void ServiceWorkerDispatcher::UnregisterServiceWorker( | 110 void ServiceWorkerDispatcher::UnregisterServiceWorker( |
105 int provider_id, | 111 int provider_id, |
106 const GURL& pattern, | 112 const GURL& pattern, |
107 WebServiceWorkerUnregistrationCallbacks* callbacks) { | 113 WebServiceWorkerUnregistrationCallbacks* callbacks) { |
108 DCHECK(callbacks); | 114 DCHECK(callbacks); |
109 | 115 |
110 if (pattern.possibly_invalid_spec().size() > GetMaxURLChars()) { | 116 if (pattern.possibly_invalid_spec().size() > GetMaxURLChars()) { |
111 scoped_ptr<WebServiceWorkerUnregistrationCallbacks> | 117 scoped_ptr<WebServiceWorkerUnregistrationCallbacks> |
112 owned_callbacks(callbacks); | 118 owned_callbacks(callbacks); |
113 scoped_ptr<WebServiceWorkerError> error(new WebServiceWorkerError( | 119 scoped_ptr<WebServiceWorkerError> error(new WebServiceWorkerError( |
114 WebServiceWorkerError::ErrorTypeSecurity, "URL too long")); | 120 WebServiceWorkerError::ErrorTypeSecurity, "URL too long")); |
115 callbacks->onError(error.release()); | 121 callbacks->onError(error.release()); |
116 return; | 122 return; |
117 } | 123 } |
118 | 124 |
119 int request_id = pending_unregistration_callbacks_.Add(callbacks); | 125 int request_id = pending_unregistration_callbacks_.Add(callbacks); |
| 126 TRACE_EVENT_ASYNC_BEGIN1("ServiceWorker", |
| 127 "ServiceWorkerDispatcher::UnregisterServiceWorker", |
| 128 request_id, |
| 129 "Pettern", pattern.spec()); |
120 thread_safe_sender_->Send(new ServiceWorkerHostMsg_UnregisterServiceWorker( | 130 thread_safe_sender_->Send(new ServiceWorkerHostMsg_UnregisterServiceWorker( |
121 CurrentWorkerId(), request_id, provider_id, pattern)); | 131 CurrentWorkerId(), request_id, provider_id, pattern)); |
122 } | 132 } |
123 | 133 |
124 void ServiceWorkerDispatcher::AddProviderContext( | 134 void ServiceWorkerDispatcher::AddProviderContext( |
125 ServiceWorkerProviderContext* provider_context) { | 135 ServiceWorkerProviderContext* provider_context) { |
126 DCHECK(provider_context); | 136 DCHECK(provider_context); |
127 int provider_id = provider_context->provider_id(); | 137 int provider_id = provider_context->provider_id(); |
128 DCHECK(!ContainsKey(provider_contexts_, provider_id)); | 138 DCHECK(!ContainsKey(provider_contexts_, provider_id)); |
129 provider_contexts_[provider_id] = provider_context; | 139 provider_contexts_[provider_id] = provider_context; |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 // WebServiceWorkerRegistrationImpl constructor calls | 247 // WebServiceWorkerRegistrationImpl constructor calls |
238 // AddServiceWorkerRegistration. | 248 // AddServiceWorkerRegistration. |
239 return new WebServiceWorkerRegistrationImpl(handle_ref.Pass()); | 249 return new WebServiceWorkerRegistrationImpl(handle_ref.Pass()); |
240 } | 250 } |
241 | 251 |
242 void ServiceWorkerDispatcher::OnRegistered( | 252 void ServiceWorkerDispatcher::OnRegistered( |
243 int thread_id, | 253 int thread_id, |
244 int request_id, | 254 int request_id, |
245 const ServiceWorkerRegistrationObjectInfo& info, | 255 const ServiceWorkerRegistrationObjectInfo& info, |
246 const ServiceWorkerVersionAttributes& attrs) { | 256 const ServiceWorkerVersionAttributes& attrs) { |
| 257 TRACE_EVENT_ASYNC_STEP_INTO0("ServiceWorker", |
| 258 "ServiceWorkerDispatcher::RegisterServiceWorker", |
| 259 request_id, |
| 260 "OnRegistered"); |
247 WebServiceWorkerRegistrationCallbacks* callbacks = | 261 WebServiceWorkerRegistrationCallbacks* callbacks = |
248 pending_registration_callbacks_.Lookup(request_id); | 262 pending_registration_callbacks_.Lookup(request_id); |
249 DCHECK(callbacks); | 263 DCHECK(callbacks); |
250 if (!callbacks) | 264 if (!callbacks) |
251 return; | 265 return; |
252 | 266 |
253 WebServiceWorkerRegistrationImpl* registration = | 267 WebServiceWorkerRegistrationImpl* registration = |
254 GetServiceWorkerRegistration(info, true); | 268 GetServiceWorkerRegistration(info, true); |
255 registration->SetInstalling(GetServiceWorker(attrs.installing, true)); | 269 registration->SetInstalling(GetServiceWorker(attrs.installing, true)); |
256 registration->SetWaiting(GetServiceWorker(attrs.waiting, true)); | 270 registration->SetWaiting(GetServiceWorker(attrs.waiting, true)); |
257 registration->SetActive(GetServiceWorker(attrs.active, true)); | 271 registration->SetActive(GetServiceWorker(attrs.active, true)); |
258 | 272 |
259 callbacks->onSuccess(registration); | 273 callbacks->onSuccess(registration); |
260 pending_registration_callbacks_.Remove(request_id); | 274 pending_registration_callbacks_.Remove(request_id); |
| 275 TRACE_EVENT_ASYNC_END0("ServiceWorker", |
| 276 "ServiceWorkerDispatcher::RegisterServiceWorker", |
| 277 request_id); |
261 } | 278 } |
262 | 279 |
263 void ServiceWorkerDispatcher::OnUnregistered( | 280 void ServiceWorkerDispatcher::OnUnregistered( |
264 int thread_id, | 281 int thread_id, |
265 int request_id) { | 282 int request_id) { |
266 WebServiceWorkerUnregistrationCallbacks* callbacks = | 283 WebServiceWorkerUnregistrationCallbacks* callbacks = |
267 pending_unregistration_callbacks_.Lookup(request_id); | 284 pending_unregistration_callbacks_.Lookup(request_id); |
| 285 TRACE_EVENT_ASYNC_STEP_INTO0( |
| 286 "ServiceWorker", |
| 287 "ServiceWorkerDispatcher::UnregisterServiceWorker", |
| 288 request_id, |
| 289 "OnUnregistered"); |
268 DCHECK(callbacks); | 290 DCHECK(callbacks); |
269 if (!callbacks) | 291 if (!callbacks) |
270 return; | 292 return; |
271 bool is_success = true; | 293 bool is_success = true; |
272 callbacks->onSuccess(&is_success); | 294 callbacks->onSuccess(&is_success); |
273 pending_unregistration_callbacks_.Remove(request_id); | 295 pending_unregistration_callbacks_.Remove(request_id); |
| 296 TRACE_EVENT_ASYNC_END0("ServiceWorker", |
| 297 "ServiceWorkerDispatcher::UnregisterServiceWorker", |
| 298 request_id); |
274 } | 299 } |
275 | 300 |
276 void ServiceWorkerDispatcher::OnRegistrationError( | 301 void ServiceWorkerDispatcher::OnRegistrationError( |
277 int thread_id, | 302 int thread_id, |
278 int request_id, | 303 int request_id, |
279 WebServiceWorkerError::ErrorType error_type, | 304 WebServiceWorkerError::ErrorType error_type, |
280 const base::string16& message) { | 305 const base::string16& message) { |
| 306 TRACE_EVENT_ASYNC_STEP_INTO0("ServiceWorker", |
| 307 "ServiceWorkerDispatcher::RegisterServiceWorker", |
| 308 request_id, |
| 309 "OnRegistrationError"); |
281 WebServiceWorkerRegistrationCallbacks* callbacks = | 310 WebServiceWorkerRegistrationCallbacks* callbacks = |
282 pending_registration_callbacks_.Lookup(request_id); | 311 pending_registration_callbacks_.Lookup(request_id); |
283 DCHECK(callbacks); | 312 DCHECK(callbacks); |
284 if (!callbacks) | 313 if (!callbacks) |
285 return; | 314 return; |
286 | 315 |
287 scoped_ptr<WebServiceWorkerError> error( | 316 scoped_ptr<WebServiceWorkerError> error( |
288 new WebServiceWorkerError(error_type, message)); | 317 new WebServiceWorkerError(error_type, message)); |
289 callbacks->onError(error.release()); | 318 callbacks->onError(error.release()); |
290 pending_registration_callbacks_.Remove(request_id); | 319 pending_registration_callbacks_.Remove(request_id); |
| 320 TRACE_EVENT_ASYNC_END0("ServiceWorker", |
| 321 "ServiceWorkerDispatcher::RegisterServiceWorker", |
| 322 request_id); |
291 } | 323 } |
292 | 324 |
293 void ServiceWorkerDispatcher::OnUnregistrationError( | 325 void ServiceWorkerDispatcher::OnUnregistrationError( |
294 int thread_id, | 326 int thread_id, |
295 int request_id, | 327 int request_id, |
296 WebServiceWorkerError::ErrorType error_type, | 328 WebServiceWorkerError::ErrorType error_type, |
297 const base::string16& message) { | 329 const base::string16& message) { |
| 330 TRACE_EVENT_ASYNC_STEP_INTO0( |
| 331 "ServiceWorker", |
| 332 "ServiceWorkerDispatcher::UnregisterServiceWorker", |
| 333 request_id, |
| 334 "OnUnregistrationError"); |
298 WebServiceWorkerUnregistrationCallbacks* callbacks = | 335 WebServiceWorkerUnregistrationCallbacks* callbacks = |
299 pending_unregistration_callbacks_.Lookup(request_id); | 336 pending_unregistration_callbacks_.Lookup(request_id); |
300 DCHECK(callbacks); | 337 DCHECK(callbacks); |
301 if (!callbacks) | 338 if (!callbacks) |
302 return; | 339 return; |
303 | 340 |
304 scoped_ptr<WebServiceWorkerError> error( | 341 scoped_ptr<WebServiceWorkerError> error( |
305 new WebServiceWorkerError(error_type, message)); | 342 new WebServiceWorkerError(error_type, message)); |
306 callbacks->onError(error.release()); | 343 callbacks->onError(error.release()); |
307 pending_unregistration_callbacks_.Remove(request_id); | 344 pending_unregistration_callbacks_.Remove(request_id); |
| 345 TRACE_EVENT_ASYNC_END0("ServiceWorker", |
| 346 "ServiceWorkerDispatcher::UnregisterServiceWorker", |
| 347 request_id); |
308 } | 348 } |
309 | 349 |
310 void ServiceWorkerDispatcher::OnServiceWorkerStateChanged( | 350 void ServiceWorkerDispatcher::OnServiceWorkerStateChanged( |
311 int thread_id, | 351 int thread_id, |
312 int handle_id, | 352 int handle_id, |
313 blink::WebServiceWorkerState state) { | 353 blink::WebServiceWorkerState state) { |
| 354 TRACE_EVENT2("ServiceWorker", |
| 355 "ServiceWorkerDispatcher::OnServiceWorkerStateChanged", |
| 356 "Thread ID", thread_id, |
| 357 "State", state); |
314 WorkerObjectMap::iterator worker = service_workers_.find(handle_id); | 358 WorkerObjectMap::iterator worker = service_workers_.find(handle_id); |
315 if (worker != service_workers_.end()) | 359 if (worker != service_workers_.end()) |
316 worker->second->OnStateChanged(state); | 360 worker->second->OnStateChanged(state); |
317 | 361 |
318 WorkerToProviderMap::iterator provider = worker_to_provider_.find(handle_id); | 362 WorkerToProviderMap::iterator provider = worker_to_provider_.find(handle_id); |
319 if (provider != worker_to_provider_.end()) | 363 if (provider != worker_to_provider_.end()) |
320 provider->second->OnServiceWorkerStateChanged(handle_id, state); | 364 provider->second->OnServiceWorkerStateChanged(handle_id, state); |
321 } | 365 } |
322 | 366 |
323 void ServiceWorkerDispatcher::OnSetVersionAttributes( | 367 void ServiceWorkerDispatcher::OnSetVersionAttributes( |
324 int thread_id, | 368 int thread_id, |
325 int provider_id, | 369 int provider_id, |
326 int registration_handle_id, | 370 int registration_handle_id, |
327 int changed_mask, | 371 int changed_mask, |
328 const ServiceWorkerVersionAttributes& attributes) { | 372 const ServiceWorkerVersionAttributes& attributes) { |
| 373 TRACE_EVENT1("ServiceWorker", |
| 374 "ServiceWorkerDispatcher::OnSetVersionAttributes", |
| 375 "Thread ID", thread_id); |
329 ChangedVersionAttributesMask mask(changed_mask); | 376 ChangedVersionAttributesMask mask(changed_mask); |
330 if (mask.installing_changed()) { | 377 if (mask.installing_changed()) { |
331 SetInstallingServiceWorker(provider_id, | 378 SetInstallingServiceWorker(provider_id, |
332 registration_handle_id, | 379 registration_handle_id, |
333 attributes.installing); | 380 attributes.installing); |
334 } | 381 } |
335 if (mask.waiting_changed()) { | 382 if (mask.waiting_changed()) { |
336 SetWaitingServiceWorker(provider_id, | 383 SetWaitingServiceWorker(provider_id, |
337 registration_handle_id, | 384 registration_handle_id, |
338 attributes.waiting); | 385 attributes.waiting); |
339 } | 386 } |
340 if (mask.active_changed()) { | 387 if (mask.active_changed()) { |
341 SetActiveServiceWorker(provider_id, | 388 SetActiveServiceWorker(provider_id, |
342 registration_handle_id, | 389 registration_handle_id, |
343 attributes.active); | 390 attributes.active); |
344 } | 391 } |
345 } | 392 } |
346 | 393 |
347 void ServiceWorkerDispatcher::OnUpdateFound( | 394 void ServiceWorkerDispatcher::OnUpdateFound( |
348 int thread_id, | 395 int thread_id, |
349 const ServiceWorkerRegistrationObjectInfo& info) { | 396 const ServiceWorkerRegistrationObjectInfo& info) { |
| 397 TRACE_EVENT0("ServiceWorker", |
| 398 "ServiceWorkerDispatcher::OnUpdateFound"); |
350 RegistrationObjectMap::iterator found = registrations_.find(info.handle_id); | 399 RegistrationObjectMap::iterator found = registrations_.find(info.handle_id); |
351 if (found != registrations_.end()) | 400 if (found != registrations_.end()) |
352 found->second->OnUpdateFound(); | 401 found->second->OnUpdateFound(); |
353 } | 402 } |
354 | 403 |
355 void ServiceWorkerDispatcher::SetInstallingServiceWorker( | 404 void ServiceWorkerDispatcher::SetInstallingServiceWorker( |
356 int provider_id, | 405 int provider_id, |
357 int registration_handle_id, | 406 int registration_handle_id, |
358 const ServiceWorkerObjectInfo& info) { | 407 const ServiceWorkerObjectInfo& info) { |
359 ProviderContextMap::iterator provider = provider_contexts_.find(provider_id); | 408 ProviderContextMap::iterator provider = provider_contexts_.find(provider_id); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
433 if (found != registrations_.end()) { | 482 if (found != registrations_.end()) { |
434 // Populate the .active field with the new worker object. | 483 // Populate the .active field with the new worker object. |
435 found->second->SetActive(GetServiceWorker(info, false)); | 484 found->second->SetActive(GetServiceWorker(info, false)); |
436 } | 485 } |
437 } | 486 } |
438 | 487 |
439 void ServiceWorkerDispatcher::OnSetControllerServiceWorker( | 488 void ServiceWorkerDispatcher::OnSetControllerServiceWorker( |
440 int thread_id, | 489 int thread_id, |
441 int provider_id, | 490 int provider_id, |
442 const ServiceWorkerObjectInfo& info) { | 491 const ServiceWorkerObjectInfo& info) { |
| 492 TRACE_EVENT2("ServiceWorker", |
| 493 "ServiceWorkerDispatcher::OnSetControllerServiceWorker", |
| 494 "Thread ID", thread_id, |
| 495 "Provider ID", provider_id); |
443 ProviderContextMap::iterator provider = provider_contexts_.find(provider_id); | 496 ProviderContextMap::iterator provider = provider_contexts_.find(provider_id); |
444 if (provider != provider_contexts_.end()) { | 497 if (provider != provider_contexts_.end()) { |
445 provider->second->OnSetControllerServiceWorker(provider_id, info); | 498 provider->second->OnSetControllerServiceWorker(provider_id, info); |
446 worker_to_provider_[info.handle_id] = provider->second; | 499 worker_to_provider_[info.handle_id] = provider->second; |
447 } | 500 } |
448 | 501 |
449 ScriptClientMap::iterator found = script_clients_.find(provider_id); | 502 ScriptClientMap::iterator found = script_clients_.find(provider_id); |
450 if (found != script_clients_.end()) { | 503 if (found != script_clients_.end()) { |
451 // Populate the .controller field with the new worker object. | 504 // Populate the .controller field with the new worker object. |
452 found->second->setController(GetServiceWorker(info, false)); | 505 found->second->setController(GetServiceWorker(info, false)); |
453 } | 506 } |
454 } | 507 } |
455 | 508 |
456 void ServiceWorkerDispatcher::OnPostMessage( | 509 void ServiceWorkerDispatcher::OnPostMessage( |
457 int thread_id, | 510 int thread_id, |
458 int provider_id, | 511 int provider_id, |
459 const base::string16& message, | 512 const base::string16& message, |
460 const std::vector<int>& sent_message_port_ids, | 513 const std::vector<int>& sent_message_port_ids, |
461 const std::vector<int>& new_routing_ids) { | 514 const std::vector<int>& new_routing_ids) { |
462 // Make sure we're on the main document thread. (That must be the only | 515 // Make sure we're on the main document thread. (That must be the only |
463 // thread we get this message) | 516 // thread we get this message) |
464 DCHECK(ChildThread::current()); | 517 DCHECK(ChildThread::current()); |
| 518 TRACE_EVENT1("ServiceWorker", |
| 519 "ServiceWorkerDispatcher::OnPostMessage", |
| 520 "Thread ID", thread_id); |
465 | 521 |
466 ScriptClientMap::iterator found = script_clients_.find(provider_id); | 522 ScriptClientMap::iterator found = script_clients_.find(provider_id); |
467 if (found == script_clients_.end()) { | 523 if (found == script_clients_.end()) { |
468 // For now we do no queueing for messages sent to nonexistent / unattached | 524 // For now we do no queueing for messages sent to nonexistent / unattached |
469 // client. | 525 // client. |
470 return; | 526 return; |
471 } | 527 } |
472 | 528 |
473 std::vector<WebMessagePortChannelImpl*> ports; | 529 std::vector<WebMessagePortChannelImpl*> ports; |
474 if (!sent_message_port_ids.empty()) { | 530 if (!sent_message_port_ids.empty()) { |
(...skipping 26 matching lines...) Expand all Loading... |
501 registrations_[registration_handle_id] = registration; | 557 registrations_[registration_handle_id] = registration; |
502 } | 558 } |
503 | 559 |
504 void ServiceWorkerDispatcher::RemoveServiceWorkerRegistration( | 560 void ServiceWorkerDispatcher::RemoveServiceWorkerRegistration( |
505 int registration_handle_id) { | 561 int registration_handle_id) { |
506 DCHECK(ContainsKey(registrations_, registration_handle_id)); | 562 DCHECK(ContainsKey(registrations_, registration_handle_id)); |
507 registrations_.erase(registration_handle_id); | 563 registrations_.erase(registration_handle_id); |
508 } | 564 } |
509 | 565 |
510 } // namespace content | 566 } // namespace content |
OLD | NEW |