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

Side by Side Diff: content/browser/service_worker/service_worker_cache_storage.cc

Issue 512743002: Guarantee ServiceWorkerCacheStorage::LazyInit calls its callback (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@loader_weak
Patch Set: Rebase Created 6 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
« no previous file with comments | « content/browser/service_worker/service_worker_cache_storage.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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/browser/service_worker/service_worker_cache_storage.h" 5 #include "content/browser/service_worker/service_worker_cache_storage.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/files/memory_mapped_file.h" 10 #include "base/files/memory_mapped_file.h"
(...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after
400 weak_factory_(this) { 400 weak_factory_(this) {
401 if (memory_only) 401 if (memory_only)
402 cache_loader_ = new MemoryLoader( 402 cache_loader_ = new MemoryLoader(
403 cache_task_runner_.get(), request_context, blob_context); 403 cache_task_runner_.get(), request_context, blob_context);
404 else 404 else
405 cache_loader_ = new SimpleCacheLoader( 405 cache_loader_ = new SimpleCacheLoader(
406 origin_path_, cache_task_runner_.get(), request_context, blob_context); 406 origin_path_, cache_task_runner_.get(), request_context, blob_context);
407 } 407 }
408 408
409 ServiceWorkerCacheStorage::~ServiceWorkerCacheStorage() { 409 ServiceWorkerCacheStorage::~ServiceWorkerCacheStorage() {
410 DCHECK_CURRENTLY_ON(BrowserThread::IO);
411
412 // If init is running call any pending callbacks. Init will cease (due to
413 // checking the WeakPtr) once this object is deleted.
414 for (std::vector<base::Closure>::iterator it = init_callbacks_.begin();
415 it != init_callbacks_.end();
416 ++it) {
417 it->Run();
michaeln 2014/08/27 22:00:54 This doesn't look quite right. The set of possible
418 }
419
410 STLDeleteContainerPairSecondPointers(cache_map_.begin(), cache_map_.end()); 420 STLDeleteContainerPairSecondPointers(cache_map_.begin(), cache_map_.end());
411 } 421 }
412 422
413 void ServiceWorkerCacheStorage::CreateCache( 423 void ServiceWorkerCacheStorage::CreateCache(
414 const std::string& cache_name, 424 const std::string& cache_name,
415 const CacheAndErrorCallback& callback) { 425 const CacheAndErrorCallback& callback) {
416 if (!initialized_) { 426 if (!initialized_) {
417 LazyInit(base::Bind(&ServiceWorkerCacheStorage::CreateCache, 427 LazyInit(base::Bind(&ServiceWorkerCacheStorage::CreateCache,
418 weak_factory_.GetWeakPtr(), 428 weak_factory_.GetWeakPtr(),
419 cache_name, 429 cache_name,
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
564 if (error != ServiceWorkerCache::ErrorTypeOK || !cache) { 574 if (error != ServiceWorkerCache::ErrorTypeOK || !cache) {
565 // TODO(jkarlin): This should delete the directory and try again in case 575 // TODO(jkarlin): This should delete the directory and try again in case
566 // the cache is simply corrupt. 576 // the cache is simply corrupt.
567 callback.Run(kInvalidCacheID, CACHE_STORAGE_ERROR_STORAGE); 577 callback.Run(kInvalidCacheID, CACHE_STORAGE_ERROR_STORAGE);
568 return; 578 return;
569 } 579 }
570 580
571 callback.Run(cache_id, CACHE_STORAGE_ERROR_NO_ERROR); 581 callback.Run(cache_id, CACHE_STORAGE_ERROR_NO_ERROR);
572 } 582 }
573 583
574 // Init is run lazily so that it is called on the proper MessageLoop.
575 void ServiceWorkerCacheStorage::LazyInit(const base::Closure& callback) { 584 void ServiceWorkerCacheStorage::LazyInit(const base::Closure& callback) {
576 DCHECK_CURRENTLY_ON(BrowserThread::IO); 585 DCHECK_CURRENTLY_ON(BrowserThread::IO);
577 DCHECK(!initialized_); 586 DCHECK(!initialized_);
578 587
579 init_callbacks_.push_back(callback); 588 init_callbacks_.push_back(callback);
580 589
581 // If this isn't the first call to LazyInit then return as the initialization 590 // If this isn't the first call to LazyInit then return as the initialization
582 // has already started. 591 // has already started.
583 if (init_callbacks_.size() > 1u) 592 if (init_callbacks_.size() > 1u)
584 return; 593 return;
585 594
586 // 1. Get the list of cache names (async call) 595 // 1. Get the list of cache names (async call)
587 // 2. For each cache name, load the cache (async call) 596 // 2. For each cache name, load the cache (async call)
588 // 3. Once each load is complete, update the map variables. 597 // 3. Once each load is complete, update the map variables.
589 // 4. Call the list of waiting callbacks. 598 // 4. Call the list of waiting callbacks.
590 599
591 scoped_ptr<std::vector<std::string> > indexed_cache_names( 600 scoped_ptr<std::vector<std::string> > indexed_cache_names(
592 new std::vector<std::string>()); 601 new std::vector<std::string>());
593 602
594 cache_loader_->LoadIndex( 603 cache_loader_->LoadIndex(
595 indexed_cache_names.Pass(), 604 indexed_cache_names.Pass(),
596 base::Bind(&ServiceWorkerCacheStorage::LazyInitDidLoadIndex, 605 base::Bind(&ServiceWorkerCacheStorage::LazyInitDidLoadIndex,
597 weak_factory_.GetWeakPtr(), 606 callback,
598 callback)); 607 weak_factory_.GetWeakPtr()));
599 } 608 }
600 609
610 // static
601 void ServiceWorkerCacheStorage::LazyInitDidLoadIndex( 611 void ServiceWorkerCacheStorage::LazyInitDidLoadIndex(
602 const base::Closure& callback, 612 const base::Closure& callback,
613 base::WeakPtr<ServiceWorkerCacheStorage> storage,
603 scoped_ptr<std::vector<std::string> > indexed_cache_names) { 614 scoped_ptr<std::vector<std::string> > indexed_cache_names) {
604 DCHECK_CURRENTLY_ON(BrowserThread::IO); 615 DCHECK_CURRENTLY_ON(BrowserThread::IO);
605 616
606 if (indexed_cache_names->empty()) { 617 if (!storage)
607 LazyInitDone(); 618 return; // The callbacks were already run by the destructor.
608 return; 619
609 } 620 if (indexed_cache_names->empty())
621 return LazyInitDone(storage);
610 622
611 std::vector<std::string>::const_iterator iter = indexed_cache_names->begin(); 623 std::vector<std::string>::const_iterator iter = indexed_cache_names->begin();
612 std::vector<std::string>::const_iterator iter_next = iter + 1; 624 std::vector<std::string>::const_iterator iter_next = iter + 1;
613 625
614 cache_loader_->LoadCache( 626 storage->cache_loader_->LoadCache(
615 *iter, 627 *iter,
616 base::Bind(&ServiceWorkerCacheStorage::LazyInitIterateAndLoadCacheName, 628 base::Bind(&ServiceWorkerCacheStorage::LazyInitIterateAndLoadCacheName,
617 weak_factory_.GetWeakPtr(),
618 callback, 629 callback,
630 storage,
619 base::Passed(indexed_cache_names.Pass()), 631 base::Passed(indexed_cache_names.Pass()),
620 iter_next, 632 iter_next,
621 *iter)); 633 *iter));
622 } 634 }
623 635
636 // static
624 void ServiceWorkerCacheStorage::LazyInitIterateAndLoadCacheName( 637 void ServiceWorkerCacheStorage::LazyInitIterateAndLoadCacheName(
625 const base::Closure& callback, 638 const base::Closure& callback,
639 base::WeakPtr<ServiceWorkerCacheStorage> storage,
626 scoped_ptr<std::vector<std::string> > indexed_cache_names, 640 scoped_ptr<std::vector<std::string> > indexed_cache_names,
627 const std::vector<std::string>::const_iterator& iter, 641 const std::vector<std::string>::const_iterator& iter,
628 const std::string& cache_name, 642 const std::string& cache_name,
629 scoped_ptr<ServiceWorkerCache> cache) { 643 scoped_ptr<ServiceWorkerCache> cache) {
630 DCHECK_CURRENTLY_ON(BrowserThread::IO); 644 DCHECK_CURRENTLY_ON(BrowserThread::IO);
631 645
646 if (!storage)
647 return; // The callbacks were already run by the destructor.
648
632 if (cache) 649 if (cache)
633 AddCacheToMaps(cache_name, cache.Pass()); 650 storage->AddCacheToMaps(cache_name, cache.Pass());
634 651
635 if (iter == indexed_cache_names->end()) { 652 if (iter == indexed_cache_names->end()) {
636 LazyInitDone(); 653 LazyInitDone(storage);
637 return; 654 return;
638 } 655 }
639 656
640 std::vector<std::string>::const_iterator iter_next = iter + 1; 657 std::vector<std::string>::const_iterator iter_next = iter + 1;
641 cache_loader_->LoadCache( 658 storage->cache_loader_->LoadCache(
642 *iter, 659 *iter,
643 base::Bind(&ServiceWorkerCacheStorage::LazyInitIterateAndLoadCacheName, 660 base::Bind(&ServiceWorkerCacheStorage::LazyInitIterateAndLoadCacheName,
644 weak_factory_.GetWeakPtr(),
645 callback, 661 callback,
662 storage,
646 base::Passed(indexed_cache_names.Pass()), 663 base::Passed(indexed_cache_names.Pass()),
647 iter_next, 664 iter_next,
648 *iter)); 665 *iter));
649 } 666 }
650 667
651 void ServiceWorkerCacheStorage::LazyInitDone() { 668 // static
652 initialized_ = true; 669 void ServiceWorkerCacheStorage::LazyInitDone(
653 for (std::vector<base::Closure>::iterator it = init_callbacks_.begin(); 670 base::WeakPtr<ServiceWorkerCacheStorage> storage) {
654 it != init_callbacks_.end(); 671 DCHECK_CURRENTLY_ON(BrowserThread::IO);
672 if (!storage)
673 return; // The callbacks were already run by the destructor.
674
675 storage->initialized_ = true;
676 for (std::vector<base::Closure>::iterator it =
677 storage->init_callbacks_.begin();
678 it != storage->init_callbacks_.end();
655 ++it) { 679 ++it) {
656 it->Run(); 680 it->Run();
657 } 681 }
658 init_callbacks_.clear(); 682 storage->init_callbacks_.clear();
659 } 683 }
660 684
661 ServiceWorkerCacheStorage::CacheContext* 685 ServiceWorkerCacheStorage::CacheContext*
662 ServiceWorkerCacheStorage::AddCacheToMaps( 686 ServiceWorkerCacheStorage::AddCacheToMaps(
663 const std::string& cache_name, 687 const std::string& cache_name,
664 scoped_ptr<ServiceWorkerCache> cache) { 688 scoped_ptr<ServiceWorkerCache> cache) {
665 DCHECK_CURRENTLY_ON(BrowserThread::IO); 689 DCHECK_CURRENTLY_ON(BrowserThread::IO);
666 690
667 CacheID id = next_cache_id_++; 691 CacheID id = next_cache_id_++;
668 CacheContext* cache_context = new CacheContext(cache_name, id, cache.Pass()); 692 CacheContext* cache_context = new CacheContext(cache_name, id, cache.Pass());
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
740 NameMap::const_iterator name_iter = name_map_.find(cache_name); 764 NameMap::const_iterator name_iter = name_map_.find(cache_name);
741 if (name_iter == name_map_.end()) 765 if (name_iter == name_map_.end())
742 return NULL; 766 return NULL;
743 767
744 CacheMap::const_iterator map_iter = cache_map_.find(name_iter->second); 768 CacheMap::const_iterator map_iter = cache_map_.find(name_iter->second);
745 DCHECK(map_iter != cache_map_.end()); 769 DCHECK(map_iter != cache_map_.end());
746 return map_iter->second; 770 return map_iter->second;
747 } 771 }
748 772
749 } // namespace content 773 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/service_worker/service_worker_cache_storage.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698