| 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 426 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 } | 437 } |
| 438 | 438 |
| 439 if (GetLoadedCache(cache_name)) { | 439 if (GetLoadedCache(cache_name)) { |
| 440 callback.Run(kInvalidCacheID, CACHE_STORAGE_ERROR_EXISTS); | 440 callback.Run(kInvalidCacheID, CACHE_STORAGE_ERROR_EXISTS); |
| 441 return; | 441 return; |
| 442 } | 442 } |
| 443 | 443 |
| 444 cache_loader_->CreateCache( | 444 cache_loader_->CreateCache( |
| 445 cache_name, | 445 cache_name, |
| 446 base::Bind(&ServiceWorkerCacheStorage::CreateCacheDidCreateCache, | 446 base::Bind(&ServiceWorkerCacheStorage::CreateCacheDidCreateCache, |
| 447 weak_factory_.GetWeakPtr(), | |
| 448 cache_name, | 447 cache_name, |
| 449 callback)); | 448 callback, |
| 449 weak_factory_.GetWeakPtr())); |
| 450 } | 450 } |
| 451 | 451 |
| 452 void ServiceWorkerCacheStorage::GetCache( | 452 void ServiceWorkerCacheStorage::GetCache( |
| 453 const std::string& cache_name, | 453 const std::string& cache_name, |
| 454 const CacheAndErrorCallback& callback) { | 454 const CacheAndErrorCallback& callback) { |
| 455 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 455 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 456 | 456 |
| 457 if (!initialized_) { | 457 if (!initialized_) { |
| 458 LazyInit(base::Bind(&ServiceWorkerCacheStorage::GetCache, | 458 LazyInit(base::Bind(&ServiceWorkerCacheStorage::GetCache, |
| 459 weak_factory_.GetWeakPtr(), | 459 weak_factory_.GetWeakPtr(), |
| (...skipping 12 matching lines...) Expand all Loading... |
| 472 callback.Run(kInvalidCacheID, CACHE_STORAGE_ERROR_NOT_FOUND); | 472 callback.Run(kInvalidCacheID, CACHE_STORAGE_ERROR_NOT_FOUND); |
| 473 return; | 473 return; |
| 474 } | 474 } |
| 475 | 475 |
| 476 ServiceWorkerCache* cache = cache_context->cache.get(); | 476 ServiceWorkerCache* cache = cache_context->cache.get(); |
| 477 | 477 |
| 478 if (cache->HasCreatedBackend()) | 478 if (cache->HasCreatedBackend()) |
| 479 return callback.Run(cache_context->id, CACHE_STORAGE_ERROR_NO_ERROR); | 479 return callback.Run(cache_context->id, CACHE_STORAGE_ERROR_NO_ERROR); |
| 480 | 480 |
| 481 cache->CreateBackend(base::Bind(&ServiceWorkerCacheStorage::DidCreateBackend, | 481 cache->CreateBackend(base::Bind(&ServiceWorkerCacheStorage::DidCreateBackend, |
| 482 weak_factory_.GetWeakPtr(), | |
| 483 cache->AsWeakPtr(), | 482 cache->AsWeakPtr(), |
| 484 cache_context->id, | 483 cache_context->id, |
| 485 callback)); | 484 callback)); |
| 486 } | 485 } |
| 487 | 486 |
| 488 void ServiceWorkerCacheStorage::HasCache(const std::string& cache_name, | 487 void ServiceWorkerCacheStorage::HasCache(const std::string& cache_name, |
| 489 const BoolAndErrorCallback& callback) { | 488 const BoolAndErrorCallback& callback) { |
| 490 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 489 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 491 | 490 |
| 492 if (!initialized_) { | 491 if (!initialized_) { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 532 } | 531 } |
| 533 | 532 |
| 534 name_map_.erase(cache_name); | 533 name_map_.erase(cache_name); |
| 535 cache_map_.erase(cache_context->id); | 534 cache_map_.erase(cache_context->id); |
| 536 cache_context.reset(); | 535 cache_context.reset(); |
| 537 | 536 |
| 538 // Update the Index | 537 // Update the Index |
| 539 cache_loader_->WriteIndex( | 538 cache_loader_->WriteIndex( |
| 540 cache_map_, | 539 cache_map_, |
| 541 base::Bind(&ServiceWorkerCacheStorage::DeleteCacheDidWriteIndex, | 540 base::Bind(&ServiceWorkerCacheStorage::DeleteCacheDidWriteIndex, |
| 542 weak_factory_.GetWeakPtr(), | |
| 543 cache_name, | 541 cache_name, |
| 544 callback)); | 542 callback, |
| 543 weak_factory_.GetWeakPtr())); |
| 545 } | 544 } |
| 546 | 545 |
| 547 void ServiceWorkerCacheStorage::EnumerateCaches( | 546 void ServiceWorkerCacheStorage::EnumerateCaches( |
| 548 const StringsAndErrorCallback& callback) { | 547 const StringsAndErrorCallback& callback) { |
| 549 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 548 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 550 | 549 |
| 551 if (!initialized_) { | 550 if (!initialized_) { |
| 552 LazyInit(base::Bind(&ServiceWorkerCacheStorage::EnumerateCaches, | 551 LazyInit(base::Bind(&ServiceWorkerCacheStorage::EnumerateCaches, |
| 553 weak_factory_.GetWeakPtr(), | 552 weak_factory_.GetWeakPtr(), |
| 554 callback)); | 553 callback)); |
| 555 return; | 554 return; |
| 556 } | 555 } |
| 557 | 556 |
| 558 std::vector<std::string> names; | 557 std::vector<std::string> names; |
| 559 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(); |
| 560 ++it) { | 559 ++it) { |
| 561 names.push_back(it->first); | 560 names.push_back(it->first); |
| 562 } | 561 } |
| 563 | 562 |
| 564 callback.Run(names, CACHE_STORAGE_ERROR_NO_ERROR); | 563 callback.Run(names, CACHE_STORAGE_ERROR_NO_ERROR); |
| 565 } | 564 } |
| 566 | 565 |
| 566 // static |
| 567 void ServiceWorkerCacheStorage::DidCreateBackend( | 567 void ServiceWorkerCacheStorage::DidCreateBackend( |
| 568 base::WeakPtr<ServiceWorkerCache> cache, | 568 base::WeakPtr<ServiceWorkerCache> cache, |
| 569 CacheID cache_id, | 569 CacheID cache_id, |
| 570 const CacheAndErrorCallback& callback, | 570 const CacheAndErrorCallback& callback, |
| 571 ServiceWorkerCache::ErrorType error) { | 571 ServiceWorkerCache::ErrorType error) { |
| 572 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 572 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 573 | 573 |
| 574 if (error != ServiceWorkerCache::ErrorTypeOK || !cache) { | 574 if (error != ServiceWorkerCache::ErrorTypeOK || !cache) { |
| 575 // 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 |
| 576 // the cache is simply corrupt. | 576 // the cache is simply corrupt. |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 688 scoped_ptr<ServiceWorkerCache> cache) { | 688 scoped_ptr<ServiceWorkerCache> cache) { |
| 689 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 689 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 690 | 690 |
| 691 CacheID id = next_cache_id_++; | 691 CacheID id = next_cache_id_++; |
| 692 CacheContext* cache_context = new CacheContext(cache_name, id, cache.Pass()); | 692 CacheContext* cache_context = new CacheContext(cache_name, id, cache.Pass()); |
| 693 cache_map_.insert(std::make_pair(id, cache_context)); // Takes ownership | 693 cache_map_.insert(std::make_pair(id, cache_context)); // Takes ownership |
| 694 name_map_.insert(std::make_pair(cache_name, id)); | 694 name_map_.insert(std::make_pair(cache_name, id)); |
| 695 return cache_context; | 695 return cache_context; |
| 696 } | 696 } |
| 697 | 697 |
| 698 // static |
| 698 void ServiceWorkerCacheStorage::CreateCacheDidCreateCache( | 699 void ServiceWorkerCacheStorage::CreateCacheDidCreateCache( |
| 699 const std::string& cache_name, | 700 const std::string& cache_name, |
| 700 const CacheAndErrorCallback& callback, | 701 const CacheAndErrorCallback& callback, |
| 702 base::WeakPtr<ServiceWorkerCacheStorage> storage, |
| 701 scoped_ptr<ServiceWorkerCache> cache) { | 703 scoped_ptr<ServiceWorkerCache> cache) { |
| 702 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 704 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 703 | 705 |
| 704 if (!cache) { | 706 if (!cache || !storage) { |
| 705 callback.Run(kInvalidCacheID, CACHE_STORAGE_ERROR_CLOSING); | 707 callback.Run(kInvalidCacheID, CACHE_STORAGE_ERROR_CLOSING); |
| 706 return; | 708 return; |
| 707 } | 709 } |
| 708 | 710 |
| 709 CacheContext* cache_context = AddCacheToMaps(cache_name, cache.Pass()); | 711 CacheContext* cache_context = |
| 712 storage->AddCacheToMaps(cache_name, cache.Pass()); |
| 710 | 713 |
| 711 cache_loader_->WriteIndex( | 714 storage->cache_loader_->WriteIndex( |
| 712 cache_map_, | 715 storage->cache_map_, |
| 713 base::Bind(&ServiceWorkerCacheStorage::CreateCacheDidWriteIndex, | 716 base::Bind(&ServiceWorkerCacheStorage::CreateCacheDidWriteIndex, |
| 714 weak_factory_.GetWeakPtr(), | |
| 715 callback, | 717 callback, |
| 716 cache_context->cache->AsWeakPtr(), | 718 cache_context->cache->AsWeakPtr(), |
| 717 cache_context->id)); | 719 cache_context->id)); |
| 718 } | 720 } |
| 719 | 721 |
| 722 // static |
| 720 void ServiceWorkerCacheStorage::CreateCacheDidWriteIndex( | 723 void ServiceWorkerCacheStorage::CreateCacheDidWriteIndex( |
| 721 const CacheAndErrorCallback& callback, | 724 const CacheAndErrorCallback& callback, |
| 722 base::WeakPtr<ServiceWorkerCache> cache, | 725 base::WeakPtr<ServiceWorkerCache> cache, |
| 723 CacheID id, | 726 CacheID id, |
| 724 bool success) { | 727 bool success) { |
| 725 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 728 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 726 if (!cache) { | 729 if (!cache) { |
| 727 callback.Run(false, CACHE_STORAGE_ERROR_CLOSING); | 730 callback.Run(false, CACHE_STORAGE_ERROR_CLOSING); |
| 728 return; | 731 return; |
| 729 } | 732 } |
| 730 | 733 |
| 731 cache->CreateBackend(base::Bind(&ServiceWorkerCacheStorage::DidCreateBackend, | 734 cache->CreateBackend(base::Bind(&ServiceWorkerCacheStorage::DidCreateBackend, |
| 732 weak_factory_.GetWeakPtr(), | |
| 733 cache, | 735 cache, |
| 734 id, | 736 id, |
| 735 callback)); | 737 callback)); |
| 736 } | 738 } |
| 737 | 739 |
| 740 // static |
| 738 void ServiceWorkerCacheStorage::DeleteCacheDidWriteIndex( | 741 void ServiceWorkerCacheStorage::DeleteCacheDidWriteIndex( |
| 739 const std::string& cache_name, | 742 const std::string& cache_name, |
| 740 const BoolAndErrorCallback& callback, | 743 const BoolAndErrorCallback& callback, |
| 744 base::WeakPtr<ServiceWorkerCacheStorage> storage, |
| 741 bool success) { | 745 bool success) { |
| 742 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 746 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 743 | 747 |
| 744 cache_loader_->CleanUpDeletedCache( | 748 if (!storage) |
| 749 return callback.Run(false, CACHE_STORAGE_ERROR_CLOSING); |
| 750 |
| 751 storage->cache_loader_->CleanUpDeletedCache( |
| 745 cache_name, | 752 cache_name, |
| 746 base::Bind(&ServiceWorkerCacheStorage::DeleteCacheDidCleanUp, | 753 base::Bind(&ServiceWorkerCacheStorage::DeleteCacheDidCleanUp, callback)); |
| 747 weak_factory_.GetWeakPtr(), | |
| 748 callback)); | |
| 749 } | 754 } |
| 750 | 755 |
| 756 // static |
| 751 void ServiceWorkerCacheStorage::DeleteCacheDidCleanUp( | 757 void ServiceWorkerCacheStorage::DeleteCacheDidCleanUp( |
| 752 const BoolAndErrorCallback& callback, | 758 const BoolAndErrorCallback& callback, |
| 753 bool success) { | 759 bool success) { |
| 754 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 760 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 755 | 761 |
| 756 callback.Run(true, CACHE_STORAGE_ERROR_NO_ERROR); | 762 callback.Run(true, CACHE_STORAGE_ERROR_NO_ERROR); |
| 757 } | 763 } |
| 758 | 764 |
| 759 ServiceWorkerCacheStorage::CacheContext* | 765 ServiceWorkerCacheStorage::CacheContext* |
| 760 ServiceWorkerCacheStorage::GetLoadedCache(const std::string& cache_name) const { | 766 ServiceWorkerCacheStorage::GetLoadedCache(const std::string& cache_name) const { |
| 761 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 767 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 762 DCHECK(initialized_); | 768 DCHECK(initialized_); |
| 763 | 769 |
| 764 NameMap::const_iterator name_iter = name_map_.find(cache_name); | 770 NameMap::const_iterator name_iter = name_map_.find(cache_name); |
| 765 if (name_iter == name_map_.end()) | 771 if (name_iter == name_map_.end()) |
| 766 return NULL; | 772 return NULL; |
| 767 | 773 |
| 768 CacheMap::const_iterator map_iter = cache_map_.find(name_iter->second); | 774 CacheMap::const_iterator map_iter = cache_map_.find(name_iter->second); |
| 769 DCHECK(map_iter != cache_map_.end()); | 775 DCHECK(map_iter != cache_map_.end()); |
| 770 return map_iter->second; | 776 return map_iter->second; |
| 771 } | 777 } |
| 772 | 778 |
| 773 } // namespace content | 779 } // namespace content |
| OLD | NEW |