| 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 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 void SizeRetrievedFromAllCaches(scoped_ptr<int64_t> accumulator, | 57 void SizeRetrievedFromAllCaches(scoped_ptr<int64_t> accumulator, |
| 58 const CacheStorage::SizeCallback& callback) { | 58 const CacheStorage::SizeCallback& callback) { |
| 59 base::ThreadTaskRunnerHandle::Get()->PostTask( | 59 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 60 FROM_HERE, base::Bind(callback, *accumulator)); | 60 FROM_HERE, base::Bind(callback, *accumulator)); |
| 61 } | 61 } |
| 62 | 62 |
| 63 } // namespace | 63 } // namespace |
| 64 | 64 |
| 65 const char CacheStorage::kIndexFileName[] = "index.txt"; | 65 const char CacheStorage::kIndexFileName[] = "index.txt"; |
| 66 | 66 |
| 67 struct CacheStorage::CacheMatchResponse { |
| 68 CacheMatchResponse() = default; |
| 69 ~CacheMatchResponse() = default; |
| 70 |
| 71 CacheStorageError error; |
| 72 scoped_ptr<ServiceWorkerResponse> service_worker_response; |
| 73 scoped_ptr<storage::BlobDataHandle> blob_data_handle; |
| 74 }; |
| 75 |
| 67 // Handles the loading and clean up of CacheStorageCache objects. | 76 // Handles the loading and clean up of CacheStorageCache objects. |
| 68 class CacheStorage::CacheLoader { | 77 class CacheStorage::CacheLoader { |
| 69 public: | 78 public: |
| 70 typedef base::Callback<void(scoped_refptr<CacheStorageCache>)> CacheCallback; | 79 typedef base::Callback<void(scoped_refptr<CacheStorageCache>)> CacheCallback; |
| 71 typedef base::Callback<void(bool)> BoolCallback; | 80 typedef base::Callback<void(bool)> BoolCallback; |
| 72 typedef base::Callback<void(scoped_ptr<std::vector<std::string>>)> | 81 typedef base::Callback<void(scoped_ptr<std::vector<std::string>>)> |
| 73 StringVectorCallback; | 82 StringVectorCallback; |
| 74 | 83 |
| 75 CacheLoader( | 84 CacheLoader( |
| 76 base::SequencedTaskRunner* cache_task_runner, | 85 base::SequencedTaskRunner* cache_task_runner, |
| (...skipping 744 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 821 const CacheStorageCache::ResponseCallback& callback, | 830 const CacheStorageCache::ResponseCallback& callback, |
| 822 CacheStorageError error, | 831 CacheStorageError error, |
| 823 scoped_ptr<ServiceWorkerResponse> response, | 832 scoped_ptr<ServiceWorkerResponse> response, |
| 824 scoped_ptr<storage::BlobDataHandle> handle) { | 833 scoped_ptr<storage::BlobDataHandle> handle) { |
| 825 callback.Run(error, std::move(response), std::move(handle)); | 834 callback.Run(error, std::move(response), std::move(handle)); |
| 826 } | 835 } |
| 827 | 836 |
| 828 void CacheStorage::MatchAllCachesImpl( | 837 void CacheStorage::MatchAllCachesImpl( |
| 829 scoped_ptr<ServiceWorkerFetchRequest> request, | 838 scoped_ptr<ServiceWorkerFetchRequest> request, |
| 830 const CacheStorageCache::ResponseCallback& callback) { | 839 const CacheStorageCache::ResponseCallback& callback) { |
| 831 scoped_ptr<CacheStorageCache::ResponseCallback> callback_copy( | 840 std::vector<CacheMatchResponse>* match_responses = |
| 832 new CacheStorageCache::ResponseCallback(callback)); | 841 new std::vector<CacheMatchResponse>(ordered_cache_names_.size()); |
| 833 | 842 |
| 834 CacheStorageCache::ResponseCallback* callback_ptr = callback_copy.get(); | 843 base::Closure barrier_closure = base::BarrierClosure( |
| 835 base::Closure barrier_closure = | 844 ordered_cache_names_.size(), |
| 836 base::BarrierClosure(ordered_cache_names_.size(), | 845 base::Bind(&CacheStorage::MatchAllCachesDidMatchAll, |
| 837 base::Bind(&CacheStorage::MatchAllCachesDidMatchAll, | 846 weak_factory_.GetWeakPtr(), |
| 838 weak_factory_.GetWeakPtr(), | 847 base::Passed(make_scoped_ptr(match_responses)), callback)); |
| 839 base::Passed(std::move(callback_copy)))); | |
| 840 | 848 |
| 841 for (const std::string& cache_name : ordered_cache_names_) { | 849 for (size_t i = 0, max = ordered_cache_names_.size(); i < max; ++i) { |
| 842 scoped_refptr<CacheStorageCache> cache = GetLoadedCache(cache_name); | 850 scoped_refptr<CacheStorageCache> cache = |
| 851 GetLoadedCache(ordered_cache_names_[i]); |
| 843 DCHECK(cache.get()); | 852 DCHECK(cache.get()); |
| 844 | 853 |
| 845 cache->Match(make_scoped_ptr(new ServiceWorkerFetchRequest(*request)), | 854 cache->Match(make_scoped_ptr(new ServiceWorkerFetchRequest(*request)), |
| 846 base::Bind(&CacheStorage::MatchAllCachesDidMatch, | 855 base::Bind(&CacheStorage::MatchAllCachesDidMatch, |
| 847 weak_factory_.GetWeakPtr(), cache, barrier_closure, | 856 weak_factory_.GetWeakPtr(), cache, |
| 848 callback_ptr)); | 857 &match_responses->at(i), barrier_closure)); |
| 849 } | 858 } |
| 850 } | 859 } |
| 851 | 860 |
| 852 void CacheStorage::MatchAllCachesDidMatch( | 861 void CacheStorage::MatchAllCachesDidMatch( |
| 853 scoped_refptr<CacheStorageCache> cache, | 862 scoped_refptr<CacheStorageCache> cache, |
| 863 CacheMatchResponse* out_match_response, |
| 854 const base::Closure& barrier_closure, | 864 const base::Closure& barrier_closure, |
| 855 CacheStorageCache::ResponseCallback* callback, | |
| 856 CacheStorageError error, | 865 CacheStorageError error, |
| 857 scoped_ptr<ServiceWorkerResponse> response, | 866 scoped_ptr<ServiceWorkerResponse> service_worker_response, |
| 858 scoped_ptr<storage::BlobDataHandle> handle) { | 867 scoped_ptr<storage::BlobDataHandle> handle) { |
| 859 if (callback->is_null() || error == CACHE_STORAGE_ERROR_NOT_FOUND) { | 868 out_match_response->error = error; |
| 860 barrier_closure.Run(); | 869 out_match_response->service_worker_response = |
| 861 return; | 870 std::move(service_worker_response); |
| 862 } | 871 out_match_response->blob_data_handle = std::move(handle); |
| 863 callback->Run(error, std::move(response), std::move(handle)); | |
| 864 callback->Reset(); // Only call the callback once. | |
| 865 | |
| 866 barrier_closure.Run(); | 872 barrier_closure.Run(); |
| 867 } | 873 } |
| 868 | 874 |
| 869 void CacheStorage::MatchAllCachesDidMatchAll( | 875 void CacheStorage::MatchAllCachesDidMatchAll( |
| 870 scoped_ptr<CacheStorageCache::ResponseCallback> callback) { | 876 scoped_ptr<std::vector<CacheMatchResponse>> match_responses, |
| 871 if (!callback->is_null()) { | 877 const CacheStorageCache::ResponseCallback& callback) { |
| 872 callback->Run(CACHE_STORAGE_ERROR_NOT_FOUND, | 878 for (CacheMatchResponse& match_response : *match_responses) { |
| 873 scoped_ptr<ServiceWorkerResponse>(), | 879 if (match_response.error == CACHE_STORAGE_ERROR_NOT_FOUND) |
| 874 scoped_ptr<storage::BlobDataHandle>()); | 880 continue; |
| 881 callback.Run(match_response.error, |
| 882 std::move(match_response.service_worker_response), |
| 883 std::move(match_response.blob_data_handle)); |
| 884 return; |
| 875 } | 885 } |
| 886 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND, |
| 887 scoped_ptr<ServiceWorkerResponse>(), |
| 888 scoped_ptr<storage::BlobDataHandle>()); |
| 876 } | 889 } |
| 877 | 890 |
| 878 scoped_refptr<CacheStorageCache> CacheStorage::GetLoadedCache( | 891 scoped_refptr<CacheStorageCache> CacheStorage::GetLoadedCache( |
| 879 const std::string& cache_name) { | 892 const std::string& cache_name) { |
| 880 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 893 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 881 DCHECK(initialized_); | 894 DCHECK(initialized_); |
| 882 | 895 |
| 883 CacheMap::iterator map_iter = cache_map_.find(cache_name); | 896 CacheMap::iterator map_iter = cache_map_.find(cache_name); |
| 884 if (map_iter == cache_map_.end()) | 897 if (map_iter == cache_map_.end()) |
| 885 return scoped_refptr<CacheStorageCache>(); | 898 return scoped_refptr<CacheStorageCache>(); |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1019 void CacheStorage::PendingSizeCallback(const SizeCallback& callback, | 1032 void CacheStorage::PendingSizeCallback(const SizeCallback& callback, |
| 1020 int64_t size) { | 1033 int64_t size) { |
| 1021 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr(); | 1034 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr(); |
| 1022 | 1035 |
| 1023 callback.Run(size); | 1036 callback.Run(size); |
| 1024 if (cache_storage) | 1037 if (cache_storage) |
| 1025 scheduler_->CompleteOperationAndRunNext(); | 1038 scheduler_->CompleteOperationAndRunNext(); |
| 1026 } | 1039 } |
| 1027 | 1040 |
| 1028 } // namespace content | 1041 } // namespace content |
| OLD | NEW |