| 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/cache_storage/cache_storage.h" | 5 #include "content/browser/cache_storage/cache_storage.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | |
| 9 #include <set> | 8 #include <set> |
| 10 #include <string> | 9 #include <string> |
| 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/barrier_closure.h" | 12 #include "base/barrier_closure.h" |
| 13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 14 #include "base/files/memory_mapped_file.h" | 14 #include "base/files/memory_mapped_file.h" |
| 15 #include "base/guid.h" | 15 #include "base/guid.h" |
| 16 #include "base/location.h" | 16 #include "base/location.h" |
| 17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 18 #include "base/metrics/histogram_macros.h" | 18 #include "base/metrics/histogram_macros.h" |
| 19 #include "base/numerics/safe_conversions.h" | 19 #include "base/numerics/safe_conversions.h" |
| 20 #include "base/sha1.h" | 20 #include "base/sha1.h" |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 callback.Run(true); | 149 callback.Run(true); |
| 150 } | 150 } |
| 151 | 151 |
| 152 void WriteIndex(const StringVector& cache_names, | 152 void WriteIndex(const StringVector& cache_names, |
| 153 const BoolCallback& callback) override { | 153 const BoolCallback& callback) override { |
| 154 callback.Run(false); | 154 callback.Run(false); |
| 155 } | 155 } |
| 156 | 156 |
| 157 void LoadIndex(scoped_ptr<std::vector<std::string>> cache_names, | 157 void LoadIndex(scoped_ptr<std::vector<std::string>> cache_names, |
| 158 const StringVectorCallback& callback) override { | 158 const StringVectorCallback& callback) override { |
| 159 callback.Run(cache_names.Pass()); | 159 callback.Run(std::move(cache_names)); |
| 160 } | 160 } |
| 161 | 161 |
| 162 private: | 162 private: |
| 163 typedef std::map<std::string, scoped_refptr<CacheStorageCache>> CacheRefMap; | 163 typedef std::map<std::string, scoped_refptr<CacheStorageCache>> CacheRefMap; |
| 164 ~MemoryLoader() override {} | 164 ~MemoryLoader() override {} |
| 165 | 165 |
| 166 // Keep a reference to each cache to ensure that it's not freed before the | 166 // Keep a reference to each cache to ensure that it's not freed before the |
| 167 // client calls CacheStorage::Delete or the CacheStorage is | 167 // client calls CacheStorage::Delete or the CacheStorage is |
| 168 // freed. | 168 // freed. |
| 169 CacheRefMap cache_refs_; | 169 CacheRefMap cache_refs_; |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 339 DCHECK(cache.has_cache_dir()); | 339 DCHECK(cache.has_cache_dir()); |
| 340 names->push_back(cache.name()); | 340 names->push_back(cache.name()); |
| 341 cache_name_to_cache_dir_[cache.name()] = cache.cache_dir(); | 341 cache_name_to_cache_dir_[cache.name()] = cache.cache_dir(); |
| 342 cache_dirs->insert(cache.cache_dir()); | 342 cache_dirs->insert(cache.cache_dir()); |
| 343 } | 343 } |
| 344 } | 344 } |
| 345 | 345 |
| 346 cache_task_runner_->PostTask( | 346 cache_task_runner_->PostTask( |
| 347 FROM_HERE, base::Bind(&DeleteUnreferencedCachesInPool, origin_path_, | 347 FROM_HERE, base::Bind(&DeleteUnreferencedCachesInPool, origin_path_, |
| 348 base::Passed(&cache_dirs))); | 348 base::Passed(&cache_dirs))); |
| 349 callback.Run(names.Pass()); | 349 callback.Run(std::move(names)); |
| 350 } | 350 } |
| 351 | 351 |
| 352 private: | 352 private: |
| 353 friend class MigratedLegacyCacheDirectoryNameTest; | 353 friend class MigratedLegacyCacheDirectoryNameTest; |
| 354 ~SimpleCacheLoader() override {} | 354 ~SimpleCacheLoader() override {} |
| 355 | 355 |
| 356 // Iterates over the caches and deletes any directory not found in | 356 // Iterates over the caches and deletes any directory not found in |
| 357 // |cache_dirs|. Runs on cache_task_runner_ | 357 // |cache_dirs|. Runs on cache_task_runner_ |
| 358 static void DeleteUnreferencedCachesInPool( | 358 static void DeleteUnreferencedCachesInPool( |
| 359 const base::FilePath& cache_base_dir, | 359 const base::FilePath& cache_base_dir, |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 530 scoped_ptr<ServiceWorkerFetchRequest> request, | 530 scoped_ptr<ServiceWorkerFetchRequest> request, |
| 531 const CacheStorageCache::ResponseCallback& callback) { | 531 const CacheStorageCache::ResponseCallback& callback) { |
| 532 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 532 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 533 | 533 |
| 534 if (!initialized_) | 534 if (!initialized_) |
| 535 LazyInit(); | 535 LazyInit(); |
| 536 | 536 |
| 537 CacheStorageCache::ResponseCallback pending_callback = | 537 CacheStorageCache::ResponseCallback pending_callback = |
| 538 base::Bind(&CacheStorage::PendingResponseCallback, | 538 base::Bind(&CacheStorage::PendingResponseCallback, |
| 539 weak_factory_.GetWeakPtr(), callback); | 539 weak_factory_.GetWeakPtr(), callback); |
| 540 scheduler_->ScheduleOperation( | 540 scheduler_->ScheduleOperation(base::Bind( |
| 541 base::Bind(&CacheStorage::MatchCacheImpl, weak_factory_.GetWeakPtr(), | 541 &CacheStorage::MatchCacheImpl, weak_factory_.GetWeakPtr(), cache_name, |
| 542 cache_name, base::Passed(request.Pass()), pending_callback)); | 542 base::Passed(std::move(request)), pending_callback)); |
| 543 } | 543 } |
| 544 | 544 |
| 545 void CacheStorage::MatchAllCaches( | 545 void CacheStorage::MatchAllCaches( |
| 546 scoped_ptr<ServiceWorkerFetchRequest> request, | 546 scoped_ptr<ServiceWorkerFetchRequest> request, |
| 547 const CacheStorageCache::ResponseCallback& callback) { | 547 const CacheStorageCache::ResponseCallback& callback) { |
| 548 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 548 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 549 | 549 |
| 550 if (!initialized_) | 550 if (!initialized_) |
| 551 LazyInit(); | 551 LazyInit(); |
| 552 | 552 |
| 553 CacheStorageCache::ResponseCallback pending_callback = | 553 CacheStorageCache::ResponseCallback pending_callback = |
| 554 base::Bind(&CacheStorage::PendingResponseCallback, | 554 base::Bind(&CacheStorage::PendingResponseCallback, |
| 555 weak_factory_.GetWeakPtr(), callback); | 555 weak_factory_.GetWeakPtr(), callback); |
| 556 scheduler_->ScheduleOperation( | 556 scheduler_->ScheduleOperation( |
| 557 base::Bind(&CacheStorage::MatchAllCachesImpl, weak_factory_.GetWeakPtr(), | 557 base::Bind(&CacheStorage::MatchAllCachesImpl, weak_factory_.GetWeakPtr(), |
| 558 base::Passed(request.Pass()), pending_callback)); | 558 base::Passed(std::move(request)), pending_callback)); |
| 559 } | 559 } |
| 560 | 560 |
| 561 void CacheStorage::CloseAllCaches(const base::Closure& callback) { | 561 void CacheStorage::CloseAllCaches(const base::Closure& callback) { |
| 562 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 562 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 563 | 563 |
| 564 if (!initialized_) { | 564 if (!initialized_) { |
| 565 callback.Run(); | 565 callback.Run(); |
| 566 return; | 566 return; |
| 567 } | 567 } |
| 568 | 568 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 616 DCHECK(initializing_); | 616 DCHECK(initializing_); |
| 617 | 617 |
| 618 // 1. Get the list of cache names (async call) | 618 // 1. Get the list of cache names (async call) |
| 619 // 2. For each cache name, load the cache (async call) | 619 // 2. For each cache name, load the cache (async call) |
| 620 // 3. Once each load is complete, update the map variables. | 620 // 3. Once each load is complete, update the map variables. |
| 621 // 4. Call the list of waiting callbacks. | 621 // 4. Call the list of waiting callbacks. |
| 622 | 622 |
| 623 scoped_ptr<std::vector<std::string>> indexed_cache_names( | 623 scoped_ptr<std::vector<std::string>> indexed_cache_names( |
| 624 new std::vector<std::string>()); | 624 new std::vector<std::string>()); |
| 625 | 625 |
| 626 cache_loader_->LoadIndex(indexed_cache_names.Pass(), | 626 cache_loader_->LoadIndex(std::move(indexed_cache_names), |
| 627 base::Bind(&CacheStorage::LazyInitDidLoadIndex, | 627 base::Bind(&CacheStorage::LazyInitDidLoadIndex, |
| 628 weak_factory_.GetWeakPtr())); | 628 weak_factory_.GetWeakPtr())); |
| 629 } | 629 } |
| 630 | 630 |
| 631 void CacheStorage::LazyInitDidLoadIndex( | 631 void CacheStorage::LazyInitDidLoadIndex( |
| 632 scoped_ptr<std::vector<std::string>> indexed_cache_names) { | 632 scoped_ptr<std::vector<std::string>> indexed_cache_names) { |
| 633 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 633 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 634 | 634 |
| 635 for (size_t i = 0u, max = indexed_cache_names->size(); i < max; ++i) { | 635 for (size_t i = 0u, max = indexed_cache_names->size(); i < max; ++i) { |
| 636 cache_map_.insert(std::make_pair(indexed_cache_names->at(i), | 636 cache_map_.insert(std::make_pair(indexed_cache_names->at(i), |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 773 | 773 |
| 774 if (!cache.get()) { | 774 if (!cache.get()) { |
| 775 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND, | 775 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND, |
| 776 scoped_ptr<ServiceWorkerResponse>(), | 776 scoped_ptr<ServiceWorkerResponse>(), |
| 777 scoped_ptr<storage::BlobDataHandle>()); | 777 scoped_ptr<storage::BlobDataHandle>()); |
| 778 return; | 778 return; |
| 779 } | 779 } |
| 780 | 780 |
| 781 // Pass the cache along to the callback to keep the cache open until match is | 781 // Pass the cache along to the callback to keep the cache open until match is |
| 782 // done. | 782 // done. |
| 783 cache->Match(request.Pass(), | 783 cache->Match(std::move(request), |
| 784 base::Bind(&CacheStorage::MatchCacheDidMatch, | 784 base::Bind(&CacheStorage::MatchCacheDidMatch, |
| 785 weak_factory_.GetWeakPtr(), cache, callback)); | 785 weak_factory_.GetWeakPtr(), cache, callback)); |
| 786 } | 786 } |
| 787 | 787 |
| 788 void CacheStorage::MatchCacheDidMatch( | 788 void CacheStorage::MatchCacheDidMatch( |
| 789 const scoped_refptr<CacheStorageCache>& cache, | 789 const scoped_refptr<CacheStorageCache>& cache, |
| 790 const CacheStorageCache::ResponseCallback& callback, | 790 const CacheStorageCache::ResponseCallback& callback, |
| 791 CacheStorageError error, | 791 CacheStorageError error, |
| 792 scoped_ptr<ServiceWorkerResponse> response, | 792 scoped_ptr<ServiceWorkerResponse> response, |
| 793 scoped_ptr<storage::BlobDataHandle> handle) { | 793 scoped_ptr<storage::BlobDataHandle> handle) { |
| 794 callback.Run(error, response.Pass(), handle.Pass()); | 794 callback.Run(error, std::move(response), std::move(handle)); |
| 795 } | 795 } |
| 796 | 796 |
| 797 void CacheStorage::MatchAllCachesImpl( | 797 void CacheStorage::MatchAllCachesImpl( |
| 798 scoped_ptr<ServiceWorkerFetchRequest> request, | 798 scoped_ptr<ServiceWorkerFetchRequest> request, |
| 799 const CacheStorageCache::ResponseCallback& callback) { | 799 const CacheStorageCache::ResponseCallback& callback) { |
| 800 scoped_ptr<CacheStorageCache::ResponseCallback> callback_copy( | 800 scoped_ptr<CacheStorageCache::ResponseCallback> callback_copy( |
| 801 new CacheStorageCache::ResponseCallback(callback)); | 801 new CacheStorageCache::ResponseCallback(callback)); |
| 802 | 802 |
| 803 CacheStorageCache::ResponseCallback* callback_ptr = callback_copy.get(); | 803 CacheStorageCache::ResponseCallback* callback_ptr = callback_copy.get(); |
| 804 base::Closure barrier_closure = | 804 base::Closure barrier_closure = |
| 805 base::BarrierClosure(ordered_cache_names_.size(), | 805 base::BarrierClosure(ordered_cache_names_.size(), |
| 806 base::Bind(&CacheStorage::MatchAllCachesDidMatchAll, | 806 base::Bind(&CacheStorage::MatchAllCachesDidMatchAll, |
| 807 weak_factory_.GetWeakPtr(), | 807 weak_factory_.GetWeakPtr(), |
| 808 base::Passed(callback_copy.Pass()))); | 808 base::Passed(std::move(callback_copy)))); |
| 809 | 809 |
| 810 for (const std::string& cache_name : ordered_cache_names_) { | 810 for (const std::string& cache_name : ordered_cache_names_) { |
| 811 scoped_refptr<CacheStorageCache> cache = GetLoadedCache(cache_name); | 811 scoped_refptr<CacheStorageCache> cache = GetLoadedCache(cache_name); |
| 812 DCHECK(cache.get()); | 812 DCHECK(cache.get()); |
| 813 | 813 |
| 814 cache->Match(make_scoped_ptr(new ServiceWorkerFetchRequest(*request)), | 814 cache->Match(make_scoped_ptr(new ServiceWorkerFetchRequest(*request)), |
| 815 base::Bind(&CacheStorage::MatchAllCachesDidMatch, | 815 base::Bind(&CacheStorage::MatchAllCachesDidMatch, |
| 816 weak_factory_.GetWeakPtr(), cache, barrier_closure, | 816 weak_factory_.GetWeakPtr(), cache, barrier_closure, |
| 817 callback_ptr)); | 817 callback_ptr)); |
| 818 } | 818 } |
| 819 } | 819 } |
| 820 | 820 |
| 821 void CacheStorage::MatchAllCachesDidMatch( | 821 void CacheStorage::MatchAllCachesDidMatch( |
| 822 scoped_refptr<CacheStorageCache> cache, | 822 scoped_refptr<CacheStorageCache> cache, |
| 823 const base::Closure& barrier_closure, | 823 const base::Closure& barrier_closure, |
| 824 CacheStorageCache::ResponseCallback* callback, | 824 CacheStorageCache::ResponseCallback* callback, |
| 825 CacheStorageError error, | 825 CacheStorageError error, |
| 826 scoped_ptr<ServiceWorkerResponse> response, | 826 scoped_ptr<ServiceWorkerResponse> response, |
| 827 scoped_ptr<storage::BlobDataHandle> handle) { | 827 scoped_ptr<storage::BlobDataHandle> handle) { |
| 828 if (callback->is_null() || error == CACHE_STORAGE_ERROR_NOT_FOUND) { | 828 if (callback->is_null() || error == CACHE_STORAGE_ERROR_NOT_FOUND) { |
| 829 barrier_closure.Run(); | 829 barrier_closure.Run(); |
| 830 return; | 830 return; |
| 831 } | 831 } |
| 832 callback->Run(error, response.Pass(), handle.Pass()); | 832 callback->Run(error, std::move(response), std::move(handle)); |
| 833 callback->Reset(); // Only call the callback once. | 833 callback->Reset(); // Only call the callback once. |
| 834 | 834 |
| 835 barrier_closure.Run(); | 835 barrier_closure.Run(); |
| 836 } | 836 } |
| 837 | 837 |
| 838 void CacheStorage::MatchAllCachesDidMatchAll( | 838 void CacheStorage::MatchAllCachesDidMatchAll( |
| 839 scoped_ptr<CacheStorageCache::ResponseCallback> callback) { | 839 scoped_ptr<CacheStorageCache::ResponseCallback> callback) { |
| 840 if (!callback->is_null()) { | 840 if (!callback->is_null()) { |
| 841 callback->Run(CACHE_STORAGE_ERROR_NOT_FOUND, | 841 callback->Run(CACHE_STORAGE_ERROR_NOT_FOUND, |
| 842 scoped_ptr<ServiceWorkerResponse>(), | 842 scoped_ptr<ServiceWorkerResponse>(), |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 963 scheduler_->CompleteOperationAndRunNext(); | 963 scheduler_->CompleteOperationAndRunNext(); |
| 964 } | 964 } |
| 965 | 965 |
| 966 void CacheStorage::PendingResponseCallback( | 966 void CacheStorage::PendingResponseCallback( |
| 967 const CacheStorageCache::ResponseCallback& callback, | 967 const CacheStorageCache::ResponseCallback& callback, |
| 968 CacheStorageError error, | 968 CacheStorageError error, |
| 969 scoped_ptr<ServiceWorkerResponse> response, | 969 scoped_ptr<ServiceWorkerResponse> response, |
| 970 scoped_ptr<storage::BlobDataHandle> blob_data_handle) { | 970 scoped_ptr<storage::BlobDataHandle> blob_data_handle) { |
| 971 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr(); | 971 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr(); |
| 972 | 972 |
| 973 callback.Run(error, response.Pass(), blob_data_handle.Pass()); | 973 callback.Run(error, std::move(response), std::move(blob_data_handle)); |
| 974 if (cache_storage) | 974 if (cache_storage) |
| 975 scheduler_->CompleteOperationAndRunNext(); | 975 scheduler_->CompleteOperationAndRunNext(); |
| 976 } | 976 } |
| 977 | 977 |
| 978 } // namespace content | 978 } // namespace content |
| OLD | NEW |