| OLD | NEW |
| 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 545 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 556 | 556 |
| 557 std::vector<std::string> names; | 557 std::vector<std::string> names; |
| 558 for (NameMap::const_iterator it = name_map_.begin(); it != name_map_.end(); | 558 for (NameMap::const_iterator it = name_map_.begin(); it != name_map_.end(); |
| 559 ++it) { | 559 ++it) { |
| 560 names.push_back(it->first); | 560 names.push_back(it->first); |
| 561 } | 561 } |
| 562 | 562 |
| 563 callback.Run(names, CACHE_STORAGE_ERROR_NO_ERROR); | 563 callback.Run(names, CACHE_STORAGE_ERROR_NO_ERROR); |
| 564 } | 564 } |
| 565 | 565 |
| 566 // static | |
| 567 void ServiceWorkerCacheStorage::DidCreateBackend( | |
| 568 base::WeakPtr<ServiceWorkerCache> cache, | |
| 569 CacheID cache_id, | |
| 570 const CacheAndErrorCallback& callback, | |
| 571 ServiceWorkerCache::ErrorType error) { | |
| 572 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 573 | |
| 574 if (error != ServiceWorkerCache::ErrorTypeOK || !cache) { | |
| 575 // TODO(jkarlin): This should delete the directory and try again in case | |
| 576 // the cache is simply corrupt. | |
| 577 callback.Run(kInvalidCacheID, CACHE_STORAGE_ERROR_STORAGE); | |
| 578 return; | |
| 579 } | |
| 580 | |
| 581 callback.Run(cache_id, CACHE_STORAGE_ERROR_NO_ERROR); | |
| 582 } | |
| 583 | |
| 584 void ServiceWorkerCacheStorage::LazyInit(const base::Closure& callback) { | 566 void ServiceWorkerCacheStorage::LazyInit(const base::Closure& callback) { |
| 585 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 567 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 586 DCHECK(!initialized_); | 568 DCHECK(!initialized_); |
| 587 | 569 |
| 588 init_callbacks_.push_back(callback); | 570 init_callbacks_.push_back(callback); |
| 589 | 571 |
| 590 // If this isn't the first call to LazyInit then return as the initialization | 572 // If this isn't the first call to LazyInit then return as the initialization |
| 591 // has already started. | 573 // has already started. |
| 592 if (init_callbacks_.size() > 1u) | 574 if (init_callbacks_.size() > 1u) |
| 593 return; | 575 return; |
| 594 | 576 |
| 595 // 1. Get the list of cache names (async call) | 577 // 1. Get the list of cache names (async call) |
| 596 // 2. For each cache name, load the cache (async call) | 578 // 2. For each cache name, load the cache (async call) |
| 597 // 3. Once each load is complete, update the map variables. | 579 // 3. Once each load is complete, update the map variables. |
| 598 // 4. Call the list of waiting callbacks. | 580 // 4. Call the list of waiting callbacks. |
| 599 | 581 |
| 600 scoped_ptr<std::vector<std::string> > indexed_cache_names( | 582 scoped_ptr<std::vector<std::string> > indexed_cache_names( |
| 601 new std::vector<std::string>()); | 583 new std::vector<std::string>()); |
| 602 | 584 |
| 603 cache_loader_->LoadIndex( | 585 cache_loader_->LoadIndex( |
| 604 indexed_cache_names.Pass(), | 586 indexed_cache_names.Pass(), |
| 605 base::Bind(&ServiceWorkerCacheStorage::LazyInitDidLoadIndex, | 587 base::Bind(&ServiceWorkerCacheStorage::LazyInitDidLoadIndex, |
| 606 callback, | 588 callback, |
| 607 weak_factory_.GetWeakPtr())); | 589 weak_factory_.GetWeakPtr())); |
| 608 } | 590 } |
| 609 | 591 |
| 592 ServiceWorkerCacheStorage::CacheContext* |
| 593 ServiceWorkerCacheStorage::GetLoadedCache(const std::string& cache_name) const { |
| 594 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 595 DCHECK(initialized_); |
| 596 |
| 597 NameMap::const_iterator name_iter = name_map_.find(cache_name); |
| 598 if (name_iter == name_map_.end()) |
| 599 return NULL; |
| 600 |
| 601 CacheMap::const_iterator map_iter = cache_map_.find(name_iter->second); |
| 602 DCHECK(map_iter != cache_map_.end()); |
| 603 return map_iter->second; |
| 604 } |
| 605 |
| 606 ServiceWorkerCacheStorage::CacheContext* |
| 607 ServiceWorkerCacheStorage::AddCacheToMaps( |
| 608 const std::string& cache_name, |
| 609 scoped_ptr<ServiceWorkerCache> cache) { |
| 610 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 611 |
| 612 CacheID id = next_cache_id_++; |
| 613 CacheContext* cache_context = new CacheContext(cache_name, id, cache.Pass()); |
| 614 cache_map_.insert(std::make_pair(id, cache_context)); // Takes ownership |
| 615 name_map_.insert(std::make_pair(cache_name, id)); |
| 616 return cache_context; |
| 617 } |
| 618 |
| 610 // static | 619 // static |
| 611 void ServiceWorkerCacheStorage::LazyInitDidLoadIndex( | 620 void ServiceWorkerCacheStorage::LazyInitDidLoadIndex( |
| 612 const base::Closure& callback, | 621 const base::Closure& callback, |
| 613 base::WeakPtr<ServiceWorkerCacheStorage> storage, | 622 base::WeakPtr<ServiceWorkerCacheStorage> storage, |
| 614 scoped_ptr<std::vector<std::string> > indexed_cache_names) { | 623 scoped_ptr<std::vector<std::string> > indexed_cache_names) { |
| 615 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 624 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 616 | 625 |
| 617 if (!storage) | 626 if (!storage) |
| 618 return; // The callbacks were already run by the destructor. | 627 return; // The callbacks were already run by the destructor. |
| 619 | 628 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 675 storage->initialized_ = true; | 684 storage->initialized_ = true; |
| 676 for (std::vector<base::Closure>::iterator it = | 685 for (std::vector<base::Closure>::iterator it = |
| 677 storage->init_callbacks_.begin(); | 686 storage->init_callbacks_.begin(); |
| 678 it != storage->init_callbacks_.end(); | 687 it != storage->init_callbacks_.end(); |
| 679 ++it) { | 688 ++it) { |
| 680 it->Run(); | 689 it->Run(); |
| 681 } | 690 } |
| 682 storage->init_callbacks_.clear(); | 691 storage->init_callbacks_.clear(); |
| 683 } | 692 } |
| 684 | 693 |
| 685 ServiceWorkerCacheStorage::CacheContext* | |
| 686 ServiceWorkerCacheStorage::AddCacheToMaps( | |
| 687 const std::string& cache_name, | |
| 688 scoped_ptr<ServiceWorkerCache> cache) { | |
| 689 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 690 | |
| 691 CacheID id = next_cache_id_++; | |
| 692 CacheContext* cache_context = new CacheContext(cache_name, id, cache.Pass()); | |
| 693 cache_map_.insert(std::make_pair(id, cache_context)); // Takes ownership | |
| 694 name_map_.insert(std::make_pair(cache_name, id)); | |
| 695 return cache_context; | |
| 696 } | |
| 697 | |
| 698 // static | 694 // static |
| 699 void ServiceWorkerCacheStorage::CreateCacheDidCreateCache( | 695 void ServiceWorkerCacheStorage::CreateCacheDidCreateCache( |
| 700 const std::string& cache_name, | 696 const std::string& cache_name, |
| 701 const CacheAndErrorCallback& callback, | 697 const CacheAndErrorCallback& callback, |
| 702 base::WeakPtr<ServiceWorkerCacheStorage> storage, | 698 base::WeakPtr<ServiceWorkerCacheStorage> storage, |
| 703 scoped_ptr<ServiceWorkerCache> cache) { | 699 scoped_ptr<ServiceWorkerCache> cache) { |
| 704 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 700 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 705 | 701 |
| 706 if (!cache || !storage) { | 702 if (!cache || !storage) { |
| 707 callback.Run(kInvalidCacheID, CACHE_STORAGE_ERROR_CLOSING); | 703 callback.Run(kInvalidCacheID, CACHE_STORAGE_ERROR_CLOSING); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 755 | 751 |
| 756 // static | 752 // static |
| 757 void ServiceWorkerCacheStorage::DeleteCacheDidCleanUp( | 753 void ServiceWorkerCacheStorage::DeleteCacheDidCleanUp( |
| 758 const BoolAndErrorCallback& callback, | 754 const BoolAndErrorCallback& callback, |
| 759 bool success) { | 755 bool success) { |
| 760 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 756 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 761 | 757 |
| 762 callback.Run(true, CACHE_STORAGE_ERROR_NO_ERROR); | 758 callback.Run(true, CACHE_STORAGE_ERROR_NO_ERROR); |
| 763 } | 759 } |
| 764 | 760 |
| 765 ServiceWorkerCacheStorage::CacheContext* | 761 // static |
| 766 ServiceWorkerCacheStorage::GetLoadedCache(const std::string& cache_name) const { | 762 void ServiceWorkerCacheStorage::DidCreateBackend( |
| 763 base::WeakPtr<ServiceWorkerCache> cache, |
| 764 CacheID cache_id, |
| 765 const CacheAndErrorCallback& callback, |
| 766 ServiceWorkerCache::ErrorType error) { |
| 767 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 767 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 768 DCHECK(initialized_); | |
| 769 | 768 |
| 770 NameMap::const_iterator name_iter = name_map_.find(cache_name); | 769 if (error != ServiceWorkerCache::ErrorTypeOK || !cache) { |
| 771 if (name_iter == name_map_.end()) | 770 // TODO(jkarlin): This should delete the directory and try again in case |
| 772 return NULL; | 771 // the cache is simply corrupt. |
| 772 callback.Run(kInvalidCacheID, CACHE_STORAGE_ERROR_STORAGE); |
| 773 return; |
| 774 } |
| 773 | 775 |
| 774 CacheMap::const_iterator map_iter = cache_map_.find(name_iter->second); | 776 callback.Run(cache_id, CACHE_STORAGE_ERROR_NO_ERROR); |
| 775 DCHECK(map_iter != cache_map_.end()); | |
| 776 return map_iter->second; | |
| 777 } | 777 } |
| 778 | 778 |
| 779 } // namespace content | 779 } // namespace content |
| OLD | NEW |