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

Side by Side Diff: content/child/service_worker/service_worker_dispatcher.cc

Issue 1307133003: ServiceWorker: Make APIs that return ServiceWorkerRegistration coin a new JS object (2/3) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: split CreateRegistration() Created 5 years, 3 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 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
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 bool adopt_handle) { 303 const ServiceWorkerVersionAttributes& attrs) {
304 DCHECK(!ContainsKey(registrations_, info.handle_id)); 304 return CreateRegistrationInternal(
305 if (info.handle_id == kInvalidServiceWorkerRegistrationHandleId) 305 ServiceWorkerRegistrationHandleReference::Create(
306 return NULL; 306 info, thread_safe_sender_.get()),
307 attrs, false /* adopt_handle */);
308 }
307 309
308 scoped_ptr<ServiceWorkerRegistrationHandleReference> handle_ref = 310 scoped_ptr<WebServiceWorkerRegistrationImpl>
309 adopt_handle ? ServiceWorkerRegistrationHandleReference::Adopt( 311 ServiceWorkerDispatcher::AdoptRegistration(
310 info, thread_safe_sender_.get()) 312 const ServiceWorkerRegistrationObjectInfo& info,
311 : ServiceWorkerRegistrationHandleReference::Create( 313 const ServiceWorkerVersionAttributes& attrs) {
312 info, thread_safe_sender_.get()); 314 return CreateRegistrationInternal(
313 315 ServiceWorkerRegistrationHandleReference::Adopt(
314 // WebServiceWorkerRegistrationImpl constructor calls 316 info, thread_safe_sender_.get()),
315 // AddServiceWorkerRegistration. 317 attrs, true /* adopt_handle */);
316 return new WebServiceWorkerRegistrationImpl(handle_ref.Pass());
317 } 318 }
318 319
319 // We can assume that this message handler is called before the worker context 320 // We can assume that this message handler is called before the worker context
320 // starts because script loading happens after this association. 321 // starts because script loading happens after this association.
321 // TODO(nhiroki): This association information could be pushed into 322 // TODO(nhiroki): This association information could be pushed into
322 // EmbeddedWorkerMsg_StartWorker message and handed over to the worker thread 323 // EmbeddedWorkerMsg_StartWorker message and handed over to the worker thread
323 // without a lock in ServiceWorkerProviderContext. 324 // without a lock in ServiceWorkerProviderContext.
324 void ServiceWorkerDispatcher::OnAssociateRegistrationWithServiceWorker( 325 void ServiceWorkerDispatcher::OnAssociateRegistrationWithServiceWorker(
325 int thread_id, 326 int thread_id,
326 int provider_id, 327 int provider_id,
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
378 "OnRegistered"); 379 "OnRegistered");
379 TRACE_EVENT_ASYNC_END0("ServiceWorker", 380 TRACE_EVENT_ASYNC_END0("ServiceWorker",
380 "ServiceWorkerDispatcher::RegisterServiceWorker", 381 "ServiceWorkerDispatcher::RegisterServiceWorker",
381 request_id); 382 request_id);
382 WebServiceWorkerRegistrationCallbacks* callbacks = 383 WebServiceWorkerRegistrationCallbacks* callbacks =
383 pending_registration_callbacks_.Lookup(request_id); 384 pending_registration_callbacks_.Lookup(request_id);
384 DCHECK(callbacks); 385 DCHECK(callbacks);
385 if (!callbacks) 386 if (!callbacks)
386 return; 387 return;
387 388
388 callbacks->onSuccess(FindOrCreateRegistration(info, attrs)); 389 callbacks->onSuccess(AdoptRegistration(info, attrs).release());
389 pending_registration_callbacks_.Remove(request_id); 390 pending_registration_callbacks_.Remove(request_id);
390 } 391 }
391 392
392 void ServiceWorkerDispatcher::OnUpdated(int thread_id, int request_id) { 393 void ServiceWorkerDispatcher::OnUpdated(int thread_id, int request_id) {
393 TRACE_EVENT_ASYNC_STEP_INTO0("ServiceWorker", 394 TRACE_EVENT_ASYNC_STEP_INTO0("ServiceWorker",
394 "ServiceWorkerDispatcher::UpdateServiceWorker", 395 "ServiceWorkerDispatcher::UpdateServiceWorker",
395 request_id, "OnUpdated"); 396 request_id, "OnUpdated");
396 TRACE_EVENT_ASYNC_END0("ServiceWorker", 397 TRACE_EVENT_ASYNC_END0("ServiceWorker",
397 "ServiceWorkerDispatcher::UpdateServiceWorker", 398 "ServiceWorkerDispatcher::UpdateServiceWorker",
398 request_id); 399 request_id);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 "OnDidGetRegistration"); 439 "OnDidGetRegistration");
439 TRACE_EVENT_ASYNC_END0("ServiceWorker", 440 TRACE_EVENT_ASYNC_END0("ServiceWorker",
440 "ServiceWorkerDispatcher::GetRegistration", 441 "ServiceWorkerDispatcher::GetRegistration",
441 request_id); 442 request_id);
442 WebServiceWorkerRegistrationCallbacks* callbacks = 443 WebServiceWorkerRegistrationCallbacks* callbacks =
443 pending_get_registration_callbacks_.Lookup(request_id); 444 pending_get_registration_callbacks_.Lookup(request_id);
444 DCHECK(callbacks); 445 DCHECK(callbacks);
445 if (!callbacks) 446 if (!callbacks)
446 return; 447 return;
447 448
448 WebServiceWorkerRegistrationImpl* registration = NULL; 449 scoped_ptr<WebServiceWorkerRegistrationImpl> registration;
449 if (info.handle_id != kInvalidServiceWorkerHandleId) 450 if (info.handle_id != kInvalidServiceWorkerHandleId)
450 registration = FindOrCreateRegistration(info, attrs); 451 registration = AdoptRegistration(info, attrs);
451 452
452 callbacks->onSuccess(registration); 453 callbacks->onSuccess(registration.release());
453 pending_get_registration_callbacks_.Remove(request_id); 454 pending_get_registration_callbacks_.Remove(request_id);
454 } 455 }
455 456
456 void ServiceWorkerDispatcher::OnDidGetRegistrations( 457 void ServiceWorkerDispatcher::OnDidGetRegistrations(
457 int thread_id, 458 int thread_id,
458 int request_id, 459 int request_id,
459 const std::vector<ServiceWorkerRegistrationObjectInfo>& infos, 460 const std::vector<ServiceWorkerRegistrationObjectInfo>& infos,
460 const std::vector<ServiceWorkerVersionAttributes>& attrs) { 461 const std::vector<ServiceWorkerVersionAttributes>& attrs) {
461 TRACE_EVENT_ASYNC_STEP_INTO0( 462 TRACE_EVENT_ASYNC_STEP_INTO0(
462 "ServiceWorker", 463 "ServiceWorker",
(...skipping 11 matching lines...) Expand all
474 return; 475 return;
475 476
476 typedef blink::WebVector<blink::WebServiceWorkerRegistration*> 477 typedef blink::WebVector<blink::WebServiceWorkerRegistration*>
477 WebServiceWorkerRegistrationArray; 478 WebServiceWorkerRegistrationArray;
478 WebServiceWorkerRegistrationArray* registrations = 479 WebServiceWorkerRegistrationArray* registrations =
479 new WebServiceWorkerRegistrationArray(infos.size()); 480 new WebServiceWorkerRegistrationArray(infos.size());
480 for (size_t i = 0; i < infos.size(); ++i) { 481 for (size_t i = 0; i < infos.size(); ++i) {
481 if (infos[i].handle_id != kInvalidServiceWorkerHandleId) { 482 if (infos[i].handle_id != kInvalidServiceWorkerHandleId) {
482 ServiceWorkerRegistrationObjectInfo info(infos[i]); 483 ServiceWorkerRegistrationObjectInfo info(infos[i]);
483 ServiceWorkerVersionAttributes attr(attrs[i]); 484 ServiceWorkerVersionAttributes attr(attrs[i]);
484 (*registrations)[i] = FindOrCreateRegistration(info, attr); 485 (*registrations)[i] = AdoptRegistration(info, attr).release();
485 } 486 }
486 } 487 }
487 488
488 callbacks->onSuccess(registrations); 489 callbacks->onSuccess(registrations);
489 pending_get_registrations_callbacks_.Remove(request_id); 490 pending_get_registrations_callbacks_.Remove(request_id);
490 } 491 }
491 492
492 void ServiceWorkerDispatcher::OnDidGetRegistrationForReady( 493 void ServiceWorkerDispatcher::OnDidGetRegistrationForReady(
493 int thread_id, 494 int thread_id,
494 int request_id, 495 int request_id,
495 const ServiceWorkerRegistrationObjectInfo& info, 496 const ServiceWorkerRegistrationObjectInfo& info,
496 const ServiceWorkerVersionAttributes& attrs) { 497 const ServiceWorkerVersionAttributes& attrs) {
497 TRACE_EVENT_ASYNC_STEP_INTO0( 498 TRACE_EVENT_ASYNC_STEP_INTO0(
498 "ServiceWorker", 499 "ServiceWorker",
499 "ServiceWorkerDispatcher::GetRegistrationForReady", 500 "ServiceWorkerDispatcher::GetRegistrationForReady",
500 request_id, 501 request_id,
501 "OnDidGetRegistrationForReady"); 502 "OnDidGetRegistrationForReady");
502 TRACE_EVENT_ASYNC_END0("ServiceWorker", 503 TRACE_EVENT_ASYNC_END0("ServiceWorker",
503 "ServiceWorkerDispatcher::GetRegistrationForReady", 504 "ServiceWorkerDispatcher::GetRegistrationForReady",
504 request_id); 505 request_id);
505 WebServiceWorkerGetRegistrationForReadyCallbacks* callbacks = 506 WebServiceWorkerGetRegistrationForReadyCallbacks* callbacks =
506 get_for_ready_callbacks_.Lookup(request_id); 507 get_for_ready_callbacks_.Lookup(request_id);
507 DCHECK(callbacks); 508 DCHECK(callbacks);
508 if (!callbacks) 509 if (!callbacks)
509 return; 510 return;
510 511
511 WebServiceWorkerRegistrationImpl* registration = NULL; 512 callbacks->onSuccess(AdoptRegistration(info, attrs).release());
512 DCHECK(info.handle_id != kInvalidServiceWorkerHandleId);
513 registration = FindOrCreateRegistration(info, attrs);
514 callbacks->onSuccess(registration);
515 get_for_ready_callbacks_.Remove(request_id); 513 get_for_ready_callbacks_.Remove(request_id);
516 } 514 }
517 515
518 void ServiceWorkerDispatcher::OnRegistrationError( 516 void ServiceWorkerDispatcher::OnRegistrationError(
519 int thread_id, 517 int thread_id,
520 int request_id, 518 int request_id,
521 WebServiceWorkerError::ErrorType error_type, 519 WebServiceWorkerError::ErrorType error_type,
522 const base::string16& message) { 520 const base::string16& message) {
523 TRACE_EVENT_ASYNC_STEP_INTO0("ServiceWorker", 521 TRACE_EVENT_ASYNC_STEP_INTO0("ServiceWorker",
524 "ServiceWorkerDispatcher::RegisterServiceWorker", 522 "ServiceWorkerDispatcher::RegisterServiceWorker",
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after
769 DCHECK(!ContainsKey(registrations_, registration_handle_id)); 767 DCHECK(!ContainsKey(registrations_, registration_handle_id));
770 registrations_[registration_handle_id] = registration; 768 registrations_[registration_handle_id] = registration;
771 } 769 }
772 770
773 void ServiceWorkerDispatcher::RemoveServiceWorkerRegistration( 771 void ServiceWorkerDispatcher::RemoveServiceWorkerRegistration(
774 int registration_handle_id) { 772 int registration_handle_id) {
775 DCHECK(ContainsKey(registrations_, registration_handle_id)); 773 DCHECK(ContainsKey(registrations_, registration_handle_id));
776 registrations_.erase(registration_handle_id); 774 registrations_.erase(registration_handle_id);
777 } 775 }
778 776
779 WebServiceWorkerRegistrationImpl* 777 scoped_ptr<WebServiceWorkerRegistrationImpl>
780 ServiceWorkerDispatcher::FindOrCreateRegistration( 778 ServiceWorkerDispatcher::CreateRegistrationInternal(
781 const ServiceWorkerRegistrationObjectInfo& info, 779 scoped_ptr<ServiceWorkerRegistrationHandleReference> handle_ref,
782 const ServiceWorkerVersionAttributes& attrs) { 780 const ServiceWorkerVersionAttributes& attrs,
783 RegistrationObjectMap::iterator found = registrations_.find(info.handle_id); 781 bool adopt_handle) {
784 if (found != registrations_.end()) { 782 // WebServiceWorkerRegistrationImpl constructor calls
785 ServiceWorkerRegistrationHandleReference::Adopt(info, 783 // AddServiceWorkerRegistration.
786 thread_safe_sender_.get()); 784 scoped_ptr<WebServiceWorkerRegistrationImpl> registration(
787 ServiceWorkerHandleReference::Adopt(attrs.installing, 785 new WebServiceWorkerRegistrationImpl(handle_ref.Pass()));
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)); 786 registration->SetInstalling(GetServiceWorker(attrs.installing, adopt_handle));
800 registration->SetWaiting(GetServiceWorker(attrs.waiting, adopt_handle)); 787 registration->SetWaiting(GetServiceWorker(attrs.waiting, adopt_handle));
801 registration->SetActive(GetServiceWorker(attrs.active, adopt_handle)); 788 registration->SetActive(GetServiceWorker(attrs.active, adopt_handle));
802 return registration; 789 return registration.Pass();
803 } 790 }
804 791
805 } // namespace content 792 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698