Chromium Code Reviews| 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 | 8 |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 117 const BoolCallback& callback) = 0; | 117 const BoolCallback& callback) = 0; |
| 118 | 118 |
| 119 // Writes the cache names (and sizes) to disk if applicable. | 119 // Writes the cache names (and sizes) to disk if applicable. |
| 120 virtual void WriteIndex(const StringVector& cache_names, | 120 virtual void WriteIndex(const StringVector& cache_names, |
| 121 const BoolCallback& callback) = 0; | 121 const BoolCallback& callback) = 0; |
| 122 | 122 |
| 123 // Loads the cache names from disk if applicable. | 123 // Loads the cache names from disk if applicable. |
| 124 virtual void LoadIndex(std::unique_ptr<std::vector<std::string>> cache_names, | 124 virtual void LoadIndex(std::unique_ptr<std::vector<std::string>> cache_names, |
| 125 const StringVectorCallback& callback) = 0; | 125 const StringVectorCallback& callback) = 0; |
| 126 | 126 |
| 127 // Called when CacheStorage has created a cache. Used to hold onto a handle to | |
| 128 // the cache if necessary. | |
| 129 virtual void NotifyCacheCreated( | |
| 130 const std::string& cache_name, | |
| 131 std::unique_ptr<CacheStorageCacheHandle> cache_handle){}; | |
| 132 | |
| 133 // Notification that a cache has been doomed and will be deleted once the last | |
| 134 // cache handle has been dropped. If the loader is holding a handle to the | |
| 135 // cache, it should drop it now. | |
| 136 virtual void NotifyCacheDoomed(const std::string& cache_name){}; | |
| 137 | |
| 127 protected: | 138 protected: |
| 128 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; | 139 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; |
| 129 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 140 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 130 | 141 |
| 131 // Owned by CacheStorage which owns this. | 142 // Owned by CacheStorage which owns this. |
| 132 storage::QuotaManagerProxy* quota_manager_proxy_; | 143 storage::QuotaManagerProxy* quota_manager_proxy_; |
| 133 | 144 |
| 134 base::WeakPtr<storage::BlobStorageContext> blob_context_; | 145 base::WeakPtr<storage::BlobStorageContext> blob_context_; |
| 135 | 146 |
| 136 // Raw pointer is safe because this object is owned by cache_storage_. | 147 // Raw pointer is safe because this object is owned by cache_storage_. |
| 137 CacheStorage* cache_storage_; | 148 CacheStorage* cache_storage_; |
| 138 | 149 |
| 139 GURL origin_; | 150 GURL origin_; |
| 140 }; | 151 }; |
| 141 | 152 |
| 142 // Creates memory-only ServiceWorkerCaches. Because these caches have no | 153 // Creates memory-only ServiceWorkerCaches. Because these caches have no |
| 143 // persistent storage it is not safe to free them from memory if they might be | 154 // persistent storage it is not safe to free them from memory if they might be |
| 144 // used again. Therefore this class holds a reference to each cache until the | 155 // used again. Therefore this class holds a reference to each cache until the |
| 145 // cache is deleted. | 156 // cache is doomed. |
| 146 class CacheStorage::MemoryLoader : public CacheStorage::CacheLoader { | 157 class CacheStorage::MemoryLoader : public CacheStorage::CacheLoader { |
| 147 public: | 158 public: |
| 148 MemoryLoader(base::SequencedTaskRunner* cache_task_runner, | 159 MemoryLoader(base::SequencedTaskRunner* cache_task_runner, |
| 149 scoped_refptr<net::URLRequestContextGetter> request_context, | 160 scoped_refptr<net::URLRequestContextGetter> request_context, |
| 150 storage::QuotaManagerProxy* quota_manager_proxy, | 161 storage::QuotaManagerProxy* quota_manager_proxy, |
| 151 base::WeakPtr<storage::BlobStorageContext> blob_context, | 162 base::WeakPtr<storage::BlobStorageContext> blob_context, |
| 152 CacheStorage* cache_storage, | 163 CacheStorage* cache_storage, |
| 153 const GURL& origin) | 164 const GURL& origin) |
| 154 : CacheLoader(cache_task_runner, | 165 : CacheLoader(cache_task_runner, |
| 155 request_context, | 166 request_context, |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 166 } | 177 } |
| 167 | 178 |
| 168 void PrepareNewCacheDestination(const std::string& cache_name, | 179 void PrepareNewCacheDestination(const std::string& cache_name, |
| 169 const CacheCallback& callback) override { | 180 const CacheCallback& callback) override { |
| 170 std::unique_ptr<CacheStorageCache> cache = CreateCache(cache_name); | 181 std::unique_ptr<CacheStorageCache> cache = CreateCache(cache_name); |
| 171 callback.Run(std::move(cache)); | 182 callback.Run(std::move(cache)); |
| 172 } | 183 } |
| 173 | 184 |
| 174 void CleanUpDeletedCache(const std::string& cache_name, | 185 void CleanUpDeletedCache(const std::string& cache_name, |
| 175 const BoolCallback& callback) override { | 186 const BoolCallback& callback) override { |
| 176 CacheHandles::iterator it = cache_handles_.find(cache_name); | 187 DCHECK(!ContainsKey(cache_handles_, cache_name)); |
| 177 DCHECK(it != cache_handles_.end()); | |
| 178 cache_handles_.erase(it); | |
| 179 callback.Run(true); | 188 callback.Run(true); |
| 180 } | 189 } |
| 181 | 190 |
| 182 void WriteIndex(const StringVector& cache_names, | 191 void WriteIndex(const StringVector& cache_names, |
| 183 const BoolCallback& callback) override { | 192 const BoolCallback& callback) override { |
| 184 callback.Run(false); | 193 callback.Run(true); |
| 185 } | 194 } |
| 186 | 195 |
| 187 void LoadIndex(std::unique_ptr<std::vector<std::string>> cache_names, | 196 void LoadIndex(std::unique_ptr<std::vector<std::string>> cache_names, |
| 188 const StringVectorCallback& callback) override { | 197 const StringVectorCallback& callback) override { |
| 189 callback.Run(std::move(cache_names)); | 198 callback.Run(std::move(cache_names)); |
| 190 } | 199 } |
| 191 | 200 |
| 201 void NotifyCacheCreated( | |
| 202 const std::string& cache_name, | |
| 203 std::unique_ptr<CacheStorageCacheHandle> cache_handle) override { | |
| 204 DCHECK(!ContainsKey(cache_handles_, cache_name)); | |
| 205 cache_handles_.insert(std::make_pair(cache_name, std::move(cache_handle))); | |
| 206 }; | |
| 207 | |
| 208 void NotifyCacheDoomed(const std::string& cache_name) override { | |
| 209 DCHECK(ContainsKey(cache_handles_, cache_name)); | |
| 210 cache_handles_.erase(cache_name); | |
| 211 }; | |
| 212 | |
| 192 void StoreCacheHandle(const std::string& cache_name, | 213 void StoreCacheHandle(const std::string& cache_name, |
|
nhiroki
2016/07/04 04:19:33
It looks like no one calls this function.
jkarlin
2016/07/06 00:35:33
Thanks. I meant to delete that earlier. Done.
| |
| 193 std::unique_ptr<CacheStorageCacheHandle> cache_handle) { | 214 std::unique_ptr<CacheStorageCacheHandle> cache_handle) { |
| 194 DCHECK(!ContainsKey(cache_handles_, cache_name)); | 215 DCHECK(!ContainsKey(cache_handles_, cache_name)); |
| 195 cache_handles_.insert(std::make_pair(cache_name, std::move(cache_handle))); | 216 cache_handles_.insert(std::make_pair(cache_name, std::move(cache_handle))); |
| 196 } | 217 } |
| 197 | 218 |
| 198 private: | 219 private: |
| 199 typedef std::map<std::string, std::unique_ptr<CacheStorageCacheHandle>> | 220 typedef std::map<std::string, std::unique_ptr<CacheStorageCacheHandle>> |
| 200 CacheHandles; | 221 CacheHandles; |
| 201 ~MemoryLoader() override {} | 222 ~MemoryLoader() override {} |
| 202 | 223 |
| (...skipping 528 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 731 callback.Run(std::unique_ptr<CacheStorageCacheHandle>(), | 752 callback.Run(std::unique_ptr<CacheStorageCacheHandle>(), |
| 732 CACHE_STORAGE_ERROR_STORAGE); | 753 CACHE_STORAGE_ERROR_STORAGE); |
| 733 return; | 754 return; |
| 734 } | 755 } |
| 735 | 756 |
| 736 CacheStorageCache* cache_ptr = cache.get(); | 757 CacheStorageCache* cache_ptr = cache.get(); |
| 737 | 758 |
| 738 cache_map_.insert(std::make_pair(cache_name, std::move(cache))); | 759 cache_map_.insert(std::make_pair(cache_name, std::move(cache))); |
| 739 ordered_cache_names_.push_back(cache_name); | 760 ordered_cache_names_.push_back(cache_name); |
| 740 | 761 |
| 741 if (memory_only_) { | |
| 742 static_cast<MemoryLoader*>(cache_loader_.get()) | |
| 743 ->StoreCacheHandle(cache_name, CreateCacheHandle(cache_ptr)); | |
| 744 } | |
| 745 | |
| 746 cache_loader_->WriteIndex( | 762 cache_loader_->WriteIndex( |
| 747 ordered_cache_names_, | 763 ordered_cache_names_, |
| 748 base::Bind(&CacheStorage::CreateCacheDidWriteIndex, | 764 base::Bind(&CacheStorage::CreateCacheDidWriteIndex, |
| 749 weak_factory_.GetWeakPtr(), callback, | 765 weak_factory_.GetWeakPtr(), callback, |
| 750 base::Passed(CreateCacheHandle(cache_ptr)))); | 766 base::Passed(CreateCacheHandle(cache_ptr)))); |
| 767 | |
| 768 cache_loader_->NotifyCacheCreated(cache_name, CreateCacheHandle(cache_ptr)); | |
| 751 } | 769 } |
| 752 | 770 |
| 753 void CacheStorage::CreateCacheDidWriteIndex( | 771 void CacheStorage::CreateCacheDidWriteIndex( |
| 754 const CacheAndErrorCallback& callback, | 772 const CacheAndErrorCallback& callback, |
| 755 std::unique_ptr<CacheStorageCacheHandle> cache_handle, | 773 std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 756 bool success) { | 774 bool success) { |
| 757 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 775 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 758 DCHECK(cache_handle); | 776 DCHECK(cache_handle); |
| 759 | 777 |
| 760 // TODO(jkarlin): Handle !success. | 778 // TODO(jkarlin): Handle !success. |
| 761 | 779 |
| 762 callback.Run(std::move(cache_handle), CACHE_STORAGE_OK); | 780 callback.Run(std::move(cache_handle), CACHE_STORAGE_OK); |
| 763 } | 781 } |
| 764 | 782 |
| 765 void CacheStorage::HasCacheImpl(const std::string& cache_name, | 783 void CacheStorage::HasCacheImpl(const std::string& cache_name, |
| 766 const BoolAndErrorCallback& callback) { | 784 const BoolAndErrorCallback& callback) { |
| 767 bool has_cache = ContainsKey(cache_map_, cache_name); | 785 bool has_cache = ContainsKey(cache_map_, cache_name); |
| 768 callback.Run(has_cache, CACHE_STORAGE_OK); | 786 callback.Run(has_cache, CACHE_STORAGE_OK); |
| 769 } | 787 } |
| 770 | 788 |
| 771 void CacheStorage::DeleteCacheImpl(const std::string& cache_name, | 789 void CacheStorage::DeleteCacheImpl(const std::string& cache_name, |
| 772 const BoolAndErrorCallback& callback) { | 790 const BoolAndErrorCallback& callback) { |
| 773 std::unique_ptr<CacheStorageCacheHandle> cache_handle = | 791 if (!GetLoadedCache(cache_name)) { |
| 774 GetLoadedCache(cache_name); | |
| 775 if (!cache_handle) { | |
| 776 base::ThreadTaskRunnerHandle::Get()->PostTask( | 792 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 777 FROM_HERE, base::Bind(callback, false, CACHE_STORAGE_ERROR_NOT_FOUND)); | 793 FROM_HERE, base::Bind(callback, false, CACHE_STORAGE_ERROR_NOT_FOUND)); |
| 778 return; | 794 return; |
| 779 } | 795 } |
| 780 | 796 |
| 781 CacheMap::iterator map_iter = cache_map_.find(cache_name); | |
| 782 deleted_caches_.insert( | |
| 783 std::make_pair(cache_handle->value(), std::move(map_iter->second))); | |
| 784 cache_map_.erase(map_iter); | |
| 785 | |
| 786 // Delete the name from ordered_cache_names_. | 797 // Delete the name from ordered_cache_names_. |
| 798 StringVector original_ordered_cache_names = ordered_cache_names_; | |
| 787 StringVector::iterator iter = std::find( | 799 StringVector::iterator iter = std::find( |
| 788 ordered_cache_names_.begin(), ordered_cache_names_.end(), cache_name); | 800 ordered_cache_names_.begin(), ordered_cache_names_.end(), cache_name); |
| 789 DCHECK(iter != ordered_cache_names_.end()); | 801 DCHECK(iter != ordered_cache_names_.end()); |
| 790 ordered_cache_names_.erase(iter); | 802 ordered_cache_names_.erase(iter); |
| 791 | 803 |
| 792 CacheStorageCache* cache_ptr = cache_handle->value(); | 804 cache_loader_->WriteIndex(ordered_cache_names_, |
| 793 cache_ptr->GetSizeThenClose( | 805 base::Bind(&CacheStorage::DeleteCacheDidWriteIndex, |
| 794 base::Bind(&CacheStorage::DeleteCacheDidClose, weak_factory_.GetWeakPtr(), | 806 weak_factory_.GetWeakPtr(), cache_name, |
| 795 cache_name, callback, ordered_cache_names_, | 807 original_ordered_cache_names, callback)); |
| 796 base::Passed(std::move(cache_handle)))); | |
| 797 } | |
| 798 | |
| 799 void CacheStorage::DeleteCacheDidClose( | |
| 800 const std::string& cache_name, | |
| 801 const BoolAndErrorCallback& callback, | |
| 802 const StringVector& ordered_cache_names, | |
| 803 std::unique_ptr<CacheStorageCacheHandle> cache_handle, | |
| 804 int64_t cache_size) { | |
| 805 cache_loader_->WriteIndex( | |
| 806 ordered_cache_names, | |
| 807 base::Bind(&CacheStorage::DeleteCacheDidWriteIndex, | |
| 808 weak_factory_.GetWeakPtr(), cache_name, callback, cache_size)); | |
| 809 } | 808 } |
| 810 | 809 |
| 811 void CacheStorage::DeleteCacheDidWriteIndex( | 810 void CacheStorage::DeleteCacheDidWriteIndex( |
| 812 const std::string& cache_name, | 811 const std::string& cache_name, |
| 812 const StringVector& original_ordered_cache_names, | |
| 813 const BoolAndErrorCallback& callback, | 813 const BoolAndErrorCallback& callback, |
| 814 int cache_size, | |
| 815 bool success) { | 814 bool success) { |
| 816 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 815 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 817 | 816 |
| 817 if (!success) { | |
| 818 // Undo any changes if the change couldn't be written to disk. | |
| 819 ordered_cache_names_ = original_ordered_cache_names; | |
| 820 callback.Run(false, CACHE_STORAGE_ERROR_STORAGE); | |
| 821 return; | |
| 822 } | |
| 823 | |
| 824 CacheMap::iterator map_iter = cache_map_.find(cache_name); | |
| 825 doomed_caches_.insert( | |
| 826 std::make_pair(map_iter->second.get(), std::move(map_iter->second))); | |
| 827 cache_map_.erase(map_iter); | |
| 828 | |
| 829 cache_loader_->NotifyCacheDoomed(cache_name); | |
| 830 | |
| 831 callback.Run(true, CACHE_STORAGE_OK); | |
| 832 } | |
| 833 | |
| 834 // Call this once the last handle to a doomed cache is gone. It's okay if this | |
| 835 // doesn't get to complete before shutdown, the cache will be removed from disk | |
| 836 // on next startup in that case. | |
| 837 void CacheStorage::DeleteCacheFinalize( | |
| 838 std::unique_ptr<CacheStorageCache> doomed_cache) { | |
| 839 CacheStorageCache* cache = doomed_cache.get(); | |
| 840 cache->Size(base::Bind(&CacheStorage::DeleteCacheDidGetSize, | |
| 841 weak_factory_.GetWeakPtr(), | |
| 842 base::Passed(std::move(doomed_cache)))); | |
| 843 } | |
| 844 | |
| 845 void CacheStorage::DeleteCacheDidGetSize( | |
| 846 std::unique_ptr<CacheStorageCache> cache, | |
| 847 int64_t cache_size) { | |
| 818 quota_manager_proxy_->NotifyStorageModified( | 848 quota_manager_proxy_->NotifyStorageModified( |
| 819 storage::QuotaClient::kServiceWorkerCache, origin_, | 849 storage::QuotaClient::kServiceWorkerCache, origin_, |
| 820 storage::kStorageTypeTemporary, -1 * cache_size); | 850 storage::kStorageTypeTemporary, -1 * cache_size); |
| 821 | 851 |
| 822 cache_loader_->CleanUpDeletedCache( | 852 cache_loader_->CleanUpDeletedCache( |
| 823 cache_name, base::Bind(&CacheStorage::DeleteCacheDidCleanUp, | 853 cache->cache_name(), base::Bind(&CacheStorage::DeleteCacheDidCleanUp, |
| 824 weak_factory_.GetWeakPtr(), callback)); | 854 weak_factory_.GetWeakPtr())); |
|
nhiroki
2016/07/04 04:19:33
OPTIONAL: Are we able to remove a callback paramet
jkarlin
2016/07/06 00:35:33
Good point. Done.
| |
| 825 } | 855 } |
| 826 | 856 |
| 827 void CacheStorage::DeleteCacheDidCleanUp(const BoolAndErrorCallback& callback, | 857 void CacheStorage::DeleteCacheDidCleanUp(bool success) { |
| 828 bool success) { | |
| 829 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 858 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 830 | |
| 831 callback.Run(true, CACHE_STORAGE_OK); | |
| 832 } | 859 } |
| 833 | 860 |
| 834 void CacheStorage::EnumerateCachesImpl( | 861 void CacheStorage::EnumerateCachesImpl( |
| 835 const StringsAndErrorCallback& callback) { | 862 const StringsAndErrorCallback& callback) { |
| 836 callback.Run(ordered_cache_names_, CACHE_STORAGE_OK); | 863 callback.Run(ordered_cache_names_, CACHE_STORAGE_OK); |
| 837 } | 864 } |
| 838 | 865 |
| 839 void CacheStorage::MatchCacheImpl( | 866 void CacheStorage::MatchCacheImpl( |
| 840 const std::string& cache_name, | 867 const std::string& cache_name, |
| 841 std::unique_ptr<ServiceWorkerFetchRequest> request, | 868 std::unique_ptr<ServiceWorkerFetchRequest> request, |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 938 void CacheStorage::DropCacheHandleRef(CacheStorageCache* cache) { | 965 void CacheStorage::DropCacheHandleRef(CacheStorageCache* cache) { |
| 939 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 966 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 940 auto iter = cache_handle_counts_.find(cache); | 967 auto iter = cache_handle_counts_.find(cache); |
| 941 DCHECK(iter != cache_handle_counts_.end()); | 968 DCHECK(iter != cache_handle_counts_.end()); |
| 942 DCHECK(iter->second >= 1); | 969 DCHECK(iter->second >= 1); |
| 943 | 970 |
| 944 iter->second -= 1; | 971 iter->second -= 1; |
| 945 if (iter->second == 0) { | 972 if (iter->second == 0) { |
| 946 // Delete the CacheStorageCache object. It's either in the main cache map or | 973 // Delete the CacheStorageCache object. It's either in the main cache map or |
| 947 // the CacheStorage::Delete operation has run on the cache, in which case | 974 // the CacheStorage::Delete operation has run on the cache, in which case |
| 948 // it's in the deleted caches map. | 975 // it's in the doomed caches map. |
| 949 auto cache_map_iter = cache_map_.find(cache->cache_name()); | 976 auto cache_map_iter = cache_map_.find(cache->cache_name()); |
| 950 | 977 |
| 951 if (cache_map_iter == cache_map_.end()) { | 978 if (cache_map_iter == cache_map_.end()) { |
| 952 auto deleted_caches_iter = deleted_caches_.find(cache); | 979 auto doomed_caches_iter = doomed_caches_.find(cache); |
| 953 DCHECK(deleted_caches_iter != deleted_caches_.end()); | 980 DCHECK(doomed_caches_iter != doomed_caches_.end()); |
| 954 deleted_caches_.erase(deleted_caches_iter); | 981 |
| 982 // The last reference to a doomed cache is gone, perform clean up. | |
| 983 DeleteCacheFinalize(std::move(doomed_caches_iter->second)); | |
| 984 doomed_caches_.erase(doomed_caches_iter); | |
| 955 return; | 985 return; |
| 956 } | 986 } |
| 957 | 987 |
| 958 cache_map_iter->second.reset(); | 988 cache_map_iter->second.reset(); |
| 959 cache_handle_counts_.erase(iter); | 989 cache_handle_counts_.erase(iter); |
| 960 } | 990 } |
| 961 } | 991 } |
| 962 | 992 |
| 963 std::unique_ptr<CacheStorageCacheHandle> CacheStorage::CreateCacheHandle( | 993 std::unique_ptr<CacheStorageCacheHandle> CacheStorage::CreateCacheHandle( |
| 964 CacheStorageCache* cache) { | 994 CacheStorageCache* cache) { |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1090 void CacheStorage::PendingSizeCallback(const SizeCallback& callback, | 1120 void CacheStorage::PendingSizeCallback(const SizeCallback& callback, |
| 1091 int64_t size) { | 1121 int64_t size) { |
| 1092 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr(); | 1122 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr(); |
| 1093 | 1123 |
| 1094 callback.Run(size); | 1124 callback.Run(size); |
| 1095 if (cache_storage) | 1125 if (cache_storage) |
| 1096 scheduler_->CompleteOperationAndRunNext(); | 1126 scheduler_->CompleteOperationAndRunNext(); |
| 1097 } | 1127 } |
| 1098 | 1128 |
| 1099 } // namespace content | 1129 } // namespace content |
| OLD | NEW |