| 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 <set> | 7 #include <set> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/barrier_closure.h" | 10 #include "base/barrier_closure.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 #include "net/base/directory_lister.h" | 28 #include "net/base/directory_lister.h" |
| 29 #include "net/base/net_errors.h" | 29 #include "net/base/net_errors.h" |
| 30 #include "net/url_request/url_request_context_getter.h" | 30 #include "net/url_request/url_request_context_getter.h" |
| 31 #include "storage/browser/blob/blob_storage_context.h" | 31 #include "storage/browser/blob/blob_storage_context.h" |
| 32 #include "storage/browser/quota/quota_manager_proxy.h" | 32 #include "storage/browser/quota/quota_manager_proxy.h" |
| 33 | 33 |
| 34 namespace content { | 34 namespace content { |
| 35 | 35 |
| 36 namespace { | 36 namespace { |
| 37 | 37 |
| 38 const int kCachePreservationInSecs = 30; |
| 39 |
| 38 std::string HexedHash(const std::string& value) { | 40 std::string HexedHash(const std::string& value) { |
| 39 std::string value_hash = base::SHA1HashString(value); | 41 std::string value_hash = base::SHA1HashString(value); |
| 40 std::string valued_hexed_hash = base::ToLowerASCII( | 42 std::string valued_hexed_hash = base::ToLowerASCII( |
| 41 base::HexEncode(value_hash.c_str(), value_hash.length())); | 43 base::HexEncode(value_hash.c_str(), value_hash.length())); |
| 42 return valued_hexed_hash; | 44 return valued_hexed_hash; |
| 43 } | 45 } |
| 44 | 46 |
| 45 void CloseAllCachesDidCloseCache(const scoped_refptr<CacheStorageCache>& cache, | 47 void CloseAllCachesDidCloseCache(const scoped_refptr<CacheStorageCache>& cache, |
| 46 const base::Closure& barrier_closure) { | 48 const base::Closure& barrier_closure) { |
| 47 barrier_closure.Run(); | 49 barrier_closure.Run(); |
| (...skipping 616 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 664 | 666 |
| 665 if (!cache.get()) { | 667 if (!cache.get()) { |
| 666 callback.Run(scoped_refptr<CacheStorageCache>(), | 668 callback.Run(scoped_refptr<CacheStorageCache>(), |
| 667 CACHE_STORAGE_ERROR_STORAGE); | 669 CACHE_STORAGE_ERROR_STORAGE); |
| 668 return; | 670 return; |
| 669 } | 671 } |
| 670 | 672 |
| 671 cache_map_.insert(std::make_pair(cache_name, cache->AsWeakPtr())); | 673 cache_map_.insert(std::make_pair(cache_name, cache->AsWeakPtr())); |
| 672 ordered_cache_names_.push_back(cache_name); | 674 ordered_cache_names_.push_back(cache_name); |
| 673 | 675 |
| 676 TemporarilyPreserveCache(cache); |
| 677 |
| 674 cache_loader_->WriteIndex( | 678 cache_loader_->WriteIndex( |
| 675 ordered_cache_names_, | 679 ordered_cache_names_, |
| 676 base::Bind(&CacheStorage::CreateCacheDidWriteIndex, | 680 base::Bind(&CacheStorage::CreateCacheDidWriteIndex, |
| 677 weak_factory_.GetWeakPtr(), callback, cache)); | 681 weak_factory_.GetWeakPtr(), callback, cache)); |
| 678 } | 682 } |
| 679 | 683 |
| 680 void CacheStorage::CreateCacheDidWriteIndex( | 684 void CacheStorage::CreateCacheDidWriteIndex( |
| 681 const CacheAndErrorCallback& callback, | 685 const CacheAndErrorCallback& callback, |
| 682 const scoped_refptr<CacheStorageCache>& cache, | 686 const scoped_refptr<CacheStorageCache>& cache, |
| 683 bool success) { | 687 bool success) { |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 846 CacheMap::iterator map_iter = cache_map_.find(cache_name); | 850 CacheMap::iterator map_iter = cache_map_.find(cache_name); |
| 847 if (map_iter == cache_map_.end()) | 851 if (map_iter == cache_map_.end()) |
| 848 return scoped_refptr<CacheStorageCache>(); | 852 return scoped_refptr<CacheStorageCache>(); |
| 849 | 853 |
| 850 base::WeakPtr<CacheStorageCache> cache = map_iter->second; | 854 base::WeakPtr<CacheStorageCache> cache = map_iter->second; |
| 851 | 855 |
| 852 if (!cache) { | 856 if (!cache) { |
| 853 scoped_refptr<CacheStorageCache> new_cache = | 857 scoped_refptr<CacheStorageCache> new_cache = |
| 854 cache_loader_->CreateCache(cache_name); | 858 cache_loader_->CreateCache(cache_name); |
| 855 map_iter->second = new_cache->AsWeakPtr(); | 859 map_iter->second = new_cache->AsWeakPtr(); |
| 860 |
| 861 TemporarilyPreserveCache(new_cache); |
| 856 return new_cache; | 862 return new_cache; |
| 857 } | 863 } |
| 858 | 864 |
| 859 return make_scoped_refptr(cache.get()); | 865 return make_scoped_refptr(cache.get()); |
| 860 } | 866 } |
| 861 | 867 |
| 868 void CacheStorage::TemporarilyPreserveCache( |
| 869 const scoped_refptr<CacheStorageCache>& cache) { |
| 870 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 871 DCHECK(!ContainsKey(preserved_caches_, cache.get())); |
| 872 |
| 873 preserved_caches_[cache.get()] = cache; |
| 874 SchedulePreservedCacheRemoval(base::Bind(&CacheStorage::RemovePreservedCache, |
| 875 weak_factory_.GetWeakPtr(), |
| 876 base::Unretained(cache.get()))); |
| 877 } |
| 878 |
| 879 void CacheStorage::SchedulePreservedCacheRemoval( |
| 880 const base::Closure& callback) { |
| 881 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 882 |
| 883 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 884 FROM_HERE, callback, |
| 885 base::TimeDelta::FromSeconds(kCachePreservationInSecs)); |
| 886 } |
| 887 |
| 888 void CacheStorage::RemovePreservedCache(const CacheStorageCache* cache) { |
| 889 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 890 DCHECK(ContainsKey(preserved_caches_, cache)); |
| 891 |
| 892 preserved_caches_.erase(cache); |
| 893 } |
| 894 |
| 862 void CacheStorage::CloseAllCachesImpl(const base::Closure& callback) { | 895 void CacheStorage::CloseAllCachesImpl(const base::Closure& callback) { |
| 863 int live_cache_count = 0; | 896 int live_cache_count = 0; |
| 864 for (const auto& key_value : cache_map_) { | 897 for (const auto& key_value : cache_map_) { |
| 865 if (key_value.second) | 898 if (key_value.second) |
| 866 live_cache_count += 1; | 899 live_cache_count += 1; |
| 867 } | 900 } |
| 868 | 901 |
| 869 if (live_cache_count == 0) { | 902 if (live_cache_count == 0) { |
| 870 callback.Run(); | 903 callback.Run(); |
| 871 return; | 904 return; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 934 scoped_ptr<ServiceWorkerResponse> response, | 967 scoped_ptr<ServiceWorkerResponse> response, |
| 935 scoped_ptr<storage::BlobDataHandle> blob_data_handle) { | 968 scoped_ptr<storage::BlobDataHandle> blob_data_handle) { |
| 936 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr(); | 969 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr(); |
| 937 | 970 |
| 938 callback.Run(error, response.Pass(), blob_data_handle.Pass()); | 971 callback.Run(error, response.Pass(), blob_data_handle.Pass()); |
| 939 if (cache_storage) | 972 if (cache_storage) |
| 940 scheduler_->CompleteOperationAndRunNext(); | 973 scheduler_->CompleteOperationAndRunNext(); |
| 941 } | 974 } |
| 942 | 975 |
| 943 } // namespace content | 976 } // namespace content |
| OLD | NEW |