| 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/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "base/single_thread_task_runner.h" | 8 #include "base/single_thread_task_runner.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
| (...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 | 290 |
| 291 scoped_ptr<ServiceWorkerHandleReference> handle_ref = | 291 scoped_ptr<ServiceWorkerHandleReference> handle_ref = |
| 292 adopt_handle | 292 adopt_handle |
| 293 ? ServiceWorkerHandleReference::Adopt(info, thread_safe_sender_.get()) | 293 ? ServiceWorkerHandleReference::Adopt(info, thread_safe_sender_.get()) |
| 294 : ServiceWorkerHandleReference::Create(info, | 294 : ServiceWorkerHandleReference::Create(info, |
| 295 thread_safe_sender_.get()); | 295 thread_safe_sender_.get()); |
| 296 // WebServiceWorkerImpl constructor calls AddServiceWorker. | 296 // WebServiceWorkerImpl constructor calls AddServiceWorker. |
| 297 return new WebServiceWorkerImpl(handle_ref.Pass(), thread_safe_sender_.get()); | 297 return new WebServiceWorkerImpl(handle_ref.Pass(), thread_safe_sender_.get()); |
| 298 } | 298 } |
| 299 | 299 |
| 300 WebServiceWorkerRegistrationImpl* | 300 scoped_ptr<WebServiceWorkerRegistrationImpl> |
| 301 ServiceWorkerDispatcher::CreateServiceWorkerRegistration( | 301 ServiceWorkerDispatcher::CreateRegistration( |
| 302 const ServiceWorkerRegistrationObjectInfo& info, | 302 const ServiceWorkerRegistrationObjectInfo& info, |
| 303 const ServiceWorkerVersionAttributes& attrs, |
| 303 bool adopt_handle) { | 304 bool adopt_handle) { |
| 304 DCHECK(!ContainsKey(registrations_, info.handle_id)); | 305 DCHECK(!ContainsKey(registrations_, info.handle_id)); |
| 305 if (info.handle_id == kInvalidServiceWorkerRegistrationHandleId) | 306 DCHECK_NE(kInvalidServiceWorkerRegistrationHandleId, info.handle_id); |
| 306 return NULL; | |
| 307 | 307 |
| 308 scoped_ptr<ServiceWorkerRegistrationHandleReference> handle_ref = | 308 scoped_ptr<ServiceWorkerRegistrationHandleReference> handle_ref = |
| 309 adopt_handle ? ServiceWorkerRegistrationHandleReference::Adopt( | 309 adopt_handle ? ServiceWorkerRegistrationHandleReference::Adopt( |
| 310 info, thread_safe_sender_.get()) | 310 info, thread_safe_sender_.get()) |
| 311 : ServiceWorkerRegistrationHandleReference::Create( | 311 : ServiceWorkerRegistrationHandleReference::Create( |
| 312 info, thread_safe_sender_.get()); | 312 info, thread_safe_sender_.get()); |
| 313 | 313 |
| 314 // WebServiceWorkerRegistrationImpl constructor calls | 314 // WebServiceWorkerRegistrationImpl constructor calls |
| 315 // AddServiceWorkerRegistration. | 315 // AddServiceWorkerRegistration. |
| 316 return new WebServiceWorkerRegistrationImpl(handle_ref.Pass()); | 316 scoped_ptr<WebServiceWorkerRegistrationImpl> registration( |
| 317 new WebServiceWorkerRegistrationImpl(handle_ref.Pass())); |
| 318 |
| 319 registration->SetInstalling(GetServiceWorker(attrs.installing, adopt_handle)); |
| 320 registration->SetWaiting(GetServiceWorker(attrs.waiting, adopt_handle)); |
| 321 registration->SetActive(GetServiceWorker(attrs.active, adopt_handle)); |
| 322 return registration.Pass(); |
| 317 } | 323 } |
| 318 | 324 |
| 319 // We can assume that this message handler is called before the worker context | 325 // We can assume that this message handler is called before the worker context |
| 320 // starts because script loading happens after this association. | 326 // starts because script loading happens after this association. |
| 321 // TODO(nhiroki): This association information could be pushed into | 327 // TODO(nhiroki): This association information could be pushed into |
| 322 // EmbeddedWorkerMsg_StartWorker message and handed over to the worker thread | 328 // EmbeddedWorkerMsg_StartWorker message and handed over to the worker thread |
| 323 // without a lock in ServiceWorkerProviderContext. | 329 // without a lock in ServiceWorkerProviderContext. |
| 324 void ServiceWorkerDispatcher::OnAssociateRegistrationWithServiceWorker( | 330 void ServiceWorkerDispatcher::OnAssociateRegistrationWithServiceWorker( |
| 325 int thread_id, | 331 int thread_id, |
| 326 int provider_id, | 332 int provider_id, |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 "OnRegistered"); | 384 "OnRegistered"); |
| 379 TRACE_EVENT_ASYNC_END0("ServiceWorker", | 385 TRACE_EVENT_ASYNC_END0("ServiceWorker", |
| 380 "ServiceWorkerDispatcher::RegisterServiceWorker", | 386 "ServiceWorkerDispatcher::RegisterServiceWorker", |
| 381 request_id); | 387 request_id); |
| 382 WebServiceWorkerRegistrationCallbacks* callbacks = | 388 WebServiceWorkerRegistrationCallbacks* callbacks = |
| 383 pending_registration_callbacks_.Lookup(request_id); | 389 pending_registration_callbacks_.Lookup(request_id); |
| 384 DCHECK(callbacks); | 390 DCHECK(callbacks); |
| 385 if (!callbacks) | 391 if (!callbacks) |
| 386 return; | 392 return; |
| 387 | 393 |
| 388 callbacks->onSuccess(FindOrCreateRegistration(info, attrs)); | 394 callbacks->onSuccess(CreateRegistration(info, attrs, true).release()); |
| 389 pending_registration_callbacks_.Remove(request_id); | 395 pending_registration_callbacks_.Remove(request_id); |
| 390 } | 396 } |
| 391 | 397 |
| 392 void ServiceWorkerDispatcher::OnUpdated(int thread_id, int request_id) { | 398 void ServiceWorkerDispatcher::OnUpdated(int thread_id, int request_id) { |
| 393 TRACE_EVENT_ASYNC_STEP_INTO0("ServiceWorker", | 399 TRACE_EVENT_ASYNC_STEP_INTO0("ServiceWorker", |
| 394 "ServiceWorkerDispatcher::UpdateServiceWorker", | 400 "ServiceWorkerDispatcher::UpdateServiceWorker", |
| 395 request_id, "OnUpdated"); | 401 request_id, "OnUpdated"); |
| 396 TRACE_EVENT_ASYNC_END0("ServiceWorker", | 402 TRACE_EVENT_ASYNC_END0("ServiceWorker", |
| 397 "ServiceWorkerDispatcher::UpdateServiceWorker", | 403 "ServiceWorkerDispatcher::UpdateServiceWorker", |
| 398 request_id); | 404 request_id); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 438 "OnDidGetRegistration"); | 444 "OnDidGetRegistration"); |
| 439 TRACE_EVENT_ASYNC_END0("ServiceWorker", | 445 TRACE_EVENT_ASYNC_END0("ServiceWorker", |
| 440 "ServiceWorkerDispatcher::GetRegistration", | 446 "ServiceWorkerDispatcher::GetRegistration", |
| 441 request_id); | 447 request_id); |
| 442 WebServiceWorkerRegistrationCallbacks* callbacks = | 448 WebServiceWorkerRegistrationCallbacks* callbacks = |
| 443 pending_get_registration_callbacks_.Lookup(request_id); | 449 pending_get_registration_callbacks_.Lookup(request_id); |
| 444 DCHECK(callbacks); | 450 DCHECK(callbacks); |
| 445 if (!callbacks) | 451 if (!callbacks) |
| 446 return; | 452 return; |
| 447 | 453 |
| 448 WebServiceWorkerRegistrationImpl* registration = NULL; | 454 scoped_ptr<WebServiceWorkerRegistrationImpl> registration; |
| 449 if (info.handle_id != kInvalidServiceWorkerHandleId) | 455 if (info.handle_id != kInvalidServiceWorkerHandleId) |
| 450 registration = FindOrCreateRegistration(info, attrs); | 456 registration = CreateRegistration(info, attrs, true); |
| 451 | 457 |
| 452 callbacks->onSuccess(registration); | 458 callbacks->onSuccess(registration.release()); |
| 453 pending_get_registration_callbacks_.Remove(request_id); | 459 pending_get_registration_callbacks_.Remove(request_id); |
| 454 } | 460 } |
| 455 | 461 |
| 456 void ServiceWorkerDispatcher::OnDidGetRegistrations( | 462 void ServiceWorkerDispatcher::OnDidGetRegistrations( |
| 457 int thread_id, | 463 int thread_id, |
| 458 int request_id, | 464 int request_id, |
| 459 const std::vector<ServiceWorkerRegistrationObjectInfo>& infos, | 465 const std::vector<ServiceWorkerRegistrationObjectInfo>& infos, |
| 460 const std::vector<ServiceWorkerVersionAttributes>& attrs) { | 466 const std::vector<ServiceWorkerVersionAttributes>& attrs) { |
| 461 TRACE_EVENT_ASYNC_STEP_INTO0( | 467 TRACE_EVENT_ASYNC_STEP_INTO0( |
| 462 "ServiceWorker", | 468 "ServiceWorker", |
| (...skipping 11 matching lines...) Expand all Loading... |
| 474 return; | 480 return; |
| 475 | 481 |
| 476 typedef blink::WebVector<blink::WebServiceWorkerRegistration*> | 482 typedef blink::WebVector<blink::WebServiceWorkerRegistration*> |
| 477 WebServiceWorkerRegistrationArray; | 483 WebServiceWorkerRegistrationArray; |
| 478 WebServiceWorkerRegistrationArray* registrations = | 484 WebServiceWorkerRegistrationArray* registrations = |
| 479 new WebServiceWorkerRegistrationArray(infos.size()); | 485 new WebServiceWorkerRegistrationArray(infos.size()); |
| 480 for (size_t i = 0; i < infos.size(); ++i) { | 486 for (size_t i = 0; i < infos.size(); ++i) { |
| 481 if (infos[i].handle_id != kInvalidServiceWorkerHandleId) { | 487 if (infos[i].handle_id != kInvalidServiceWorkerHandleId) { |
| 482 ServiceWorkerRegistrationObjectInfo info(infos[i]); | 488 ServiceWorkerRegistrationObjectInfo info(infos[i]); |
| 483 ServiceWorkerVersionAttributes attr(attrs[i]); | 489 ServiceWorkerVersionAttributes attr(attrs[i]); |
| 484 (*registrations)[i] = FindOrCreateRegistration(info, attr); | 490 (*registrations)[i] = CreateRegistration(info, attr, true).release(); |
| 485 } | 491 } |
| 486 } | 492 } |
| 487 | 493 |
| 488 callbacks->onSuccess(registrations); | 494 callbacks->onSuccess(registrations); |
| 489 pending_get_registrations_callbacks_.Remove(request_id); | 495 pending_get_registrations_callbacks_.Remove(request_id); |
| 490 } | 496 } |
| 491 | 497 |
| 492 void ServiceWorkerDispatcher::OnDidGetRegistrationForReady( | 498 void ServiceWorkerDispatcher::OnDidGetRegistrationForReady( |
| 493 int thread_id, | 499 int thread_id, |
| 494 int request_id, | 500 int request_id, |
| 495 const ServiceWorkerRegistrationObjectInfo& info, | 501 const ServiceWorkerRegistrationObjectInfo& info, |
| 496 const ServiceWorkerVersionAttributes& attrs) { | 502 const ServiceWorkerVersionAttributes& attrs) { |
| 497 TRACE_EVENT_ASYNC_STEP_INTO0( | 503 TRACE_EVENT_ASYNC_STEP_INTO0( |
| 498 "ServiceWorker", | 504 "ServiceWorker", |
| 499 "ServiceWorkerDispatcher::GetRegistrationForReady", | 505 "ServiceWorkerDispatcher::GetRegistrationForReady", |
| 500 request_id, | 506 request_id, |
| 501 "OnDidGetRegistrationForReady"); | 507 "OnDidGetRegistrationForReady"); |
| 502 TRACE_EVENT_ASYNC_END0("ServiceWorker", | 508 TRACE_EVENT_ASYNC_END0("ServiceWorker", |
| 503 "ServiceWorkerDispatcher::GetRegistrationForReady", | 509 "ServiceWorkerDispatcher::GetRegistrationForReady", |
| 504 request_id); | 510 request_id); |
| 505 WebServiceWorkerGetRegistrationForReadyCallbacks* callbacks = | 511 WebServiceWorkerGetRegistrationForReadyCallbacks* callbacks = |
| 506 get_for_ready_callbacks_.Lookup(request_id); | 512 get_for_ready_callbacks_.Lookup(request_id); |
| 507 DCHECK(callbacks); | 513 DCHECK(callbacks); |
| 508 if (!callbacks) | 514 if (!callbacks) |
| 509 return; | 515 return; |
| 510 | 516 |
| 511 WebServiceWorkerRegistrationImpl* registration = NULL; | 517 callbacks->onSuccess(CreateRegistration(info, attrs, true).release()); |
| 512 DCHECK(info.handle_id != kInvalidServiceWorkerHandleId); | |
| 513 registration = FindOrCreateRegistration(info, attrs); | |
| 514 callbacks->onSuccess(registration); | |
| 515 get_for_ready_callbacks_.Remove(request_id); | 518 get_for_ready_callbacks_.Remove(request_id); |
| 516 } | 519 } |
| 517 | 520 |
| 518 void ServiceWorkerDispatcher::OnRegistrationError( | 521 void ServiceWorkerDispatcher::OnRegistrationError( |
| 519 int thread_id, | 522 int thread_id, |
| 520 int request_id, | 523 int request_id, |
| 521 WebServiceWorkerError::ErrorType error_type, | 524 WebServiceWorkerError::ErrorType error_type, |
| 522 const base::string16& message) { | 525 const base::string16& message) { |
| 523 TRACE_EVENT_ASYNC_STEP_INTO0("ServiceWorker", | 526 TRACE_EVENT_ASYNC_STEP_INTO0("ServiceWorker", |
| 524 "ServiceWorkerDispatcher::RegisterServiceWorker", | 527 "ServiceWorkerDispatcher::RegisterServiceWorker", |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 769 DCHECK(!ContainsKey(registrations_, registration_handle_id)); | 772 DCHECK(!ContainsKey(registrations_, registration_handle_id)); |
| 770 registrations_[registration_handle_id] = registration; | 773 registrations_[registration_handle_id] = registration; |
| 771 } | 774 } |
| 772 | 775 |
| 773 void ServiceWorkerDispatcher::RemoveServiceWorkerRegistration( | 776 void ServiceWorkerDispatcher::RemoveServiceWorkerRegistration( |
| 774 int registration_handle_id) { | 777 int registration_handle_id) { |
| 775 DCHECK(ContainsKey(registrations_, registration_handle_id)); | 778 DCHECK(ContainsKey(registrations_, registration_handle_id)); |
| 776 registrations_.erase(registration_handle_id); | 779 registrations_.erase(registration_handle_id); |
| 777 } | 780 } |
| 778 | 781 |
| 779 WebServiceWorkerRegistrationImpl* | |
| 780 ServiceWorkerDispatcher::FindOrCreateRegistration( | |
| 781 const ServiceWorkerRegistrationObjectInfo& info, | |
| 782 const ServiceWorkerVersionAttributes& attrs) { | |
| 783 RegistrationObjectMap::iterator found = registrations_.find(info.handle_id); | |
| 784 if (found != registrations_.end()) { | |
| 785 ServiceWorkerRegistrationHandleReference::Adopt(info, | |
| 786 thread_safe_sender_.get()); | |
| 787 ServiceWorkerHandleReference::Adopt(attrs.installing, | |
| 788 thread_safe_sender_.get()); | |
| 789 ServiceWorkerHandleReference::Adopt(attrs.waiting, | |
| 790 thread_safe_sender_.get()); | |
| 791 ServiceWorkerHandleReference::Adopt(attrs.active, | |
| 792 thread_safe_sender_.get()); | |
| 793 return found->second; | |
| 794 } | |
| 795 | |
| 796 bool adopt_handle = true; | |
| 797 WebServiceWorkerRegistrationImpl* registration = | |
| 798 CreateServiceWorkerRegistration(info, adopt_handle); | |
| 799 registration->SetInstalling(GetServiceWorker(attrs.installing, adopt_handle)); | |
| 800 registration->SetWaiting(GetServiceWorker(attrs.waiting, adopt_handle)); | |
| 801 registration->SetActive(GetServiceWorker(attrs.active, adopt_handle)); | |
| 802 return registration; | |
| 803 } | |
| 804 | |
| 805 } // namespace content | 782 } // namespace content |
| OLD | NEW |