| 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 #ifndef CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_H_ | 5 #ifndef CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_H_ |
| 6 #define CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_H_ | 6 #define CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
| 18 #include "base/strings/string16.h" |
| 18 #include "content/browser/cache_storage/cache_storage_cache.h" | 19 #include "content/browser/cache_storage/cache_storage_cache.h" |
| 19 | 20 |
| 20 namespace base { | 21 namespace base { |
| 21 class SequencedTaskRunner; | 22 class SequencedTaskRunner; |
| 22 } | 23 } |
| 23 | 24 |
| 24 namespace net { | 25 namespace net { |
| 25 class URLRequestContextGetter; | 26 class URLRequestContextGetter; |
| 26 } | 27 } |
| 27 | 28 |
| 28 namespace storage { | 29 namespace storage { |
| 29 class BlobStorageContext; | 30 class BlobStorageContext; |
| 30 } | 31 } |
| 31 | 32 |
| 32 namespace content { | 33 namespace content { |
| 33 class CacheStorageScheduler; | 34 class CacheStorageScheduler; |
| 34 | 35 |
| 35 // TODO(jkarlin): Constrain the total bytes used per origin. | 36 // TODO(jkarlin): Constrain the total bytes used per origin. |
| 36 | 37 |
| 37 // CacheStorage holds the set of caches for a given origin. It is | 38 // CacheStorage holds the set of caches for a given origin. It is |
| 38 // owned by the CacheStorageManager. This class expects to be run | 39 // owned by the CacheStorageManager. This class expects to be run |
| 39 // on the IO thread. The asynchronous methods are executed serially. | 40 // on the IO thread. The asynchronous methods are executed serially. |
| 40 class CONTENT_EXPORT CacheStorage { | 41 class CONTENT_EXPORT CacheStorage { |
| 41 public: | 42 public: |
| 42 typedef std::vector<std::string> StringVector; | 43 typedef std::vector<base::string16> StringVector; |
| 43 typedef base::Callback<void(bool, CacheStorageError)> BoolAndErrorCallback; | 44 typedef base::Callback<void(bool, CacheStorageError)> BoolAndErrorCallback; |
| 44 typedef base::Callback<void(const scoped_refptr<CacheStorageCache>&, | 45 typedef base::Callback<void(const scoped_refptr<CacheStorageCache>&, |
| 45 CacheStorageError)> CacheAndErrorCallback; | 46 CacheStorageError)> CacheAndErrorCallback; |
| 46 typedef base::Callback<void(const StringVector&, CacheStorageError)> | 47 typedef base::Callback<void(const StringVector&, CacheStorageError)> |
| 47 StringsAndErrorCallback; | 48 StringsAndErrorCallback; |
| 48 using SizeCallback = base::Callback<void(int64_t)>; | 49 using SizeCallback = base::Callback<void(int64_t)>; |
| 49 | 50 |
| 50 static const char kIndexFileName[]; | 51 static const char kIndexFileName[]; |
| 51 | 52 |
| 52 CacheStorage( | 53 CacheStorage( |
| 53 const base::FilePath& origin_path, | 54 const base::FilePath& origin_path, |
| 54 bool memory_only, | 55 bool memory_only, |
| 55 base::SequencedTaskRunner* cache_task_runner, | 56 base::SequencedTaskRunner* cache_task_runner, |
| 56 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, | 57 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, |
| 57 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 58 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
| 58 base::WeakPtr<storage::BlobStorageContext> blob_context, | 59 base::WeakPtr<storage::BlobStorageContext> blob_context, |
| 59 const GURL& origin); | 60 const GURL& origin); |
| 60 | 61 |
| 61 // Any unfinished asynchronous operations may not complete or call their | 62 // Any unfinished asynchronous operations may not complete or call their |
| 62 // callbacks. | 63 // callbacks. |
| 63 virtual ~CacheStorage(); | 64 virtual ~CacheStorage(); |
| 64 | 65 |
| 65 // Get the cache for the given key. If the cache is not found it is | 66 // Get the cache for the given key. If the cache is not found it is |
| 66 // created. | 67 // created. |
| 67 void OpenCache(const std::string& cache_name, | 68 void OpenCache(const base::string16& cache_name, |
| 68 const CacheAndErrorCallback& callback); | 69 const CacheAndErrorCallback& callback); |
| 69 | 70 |
| 70 // Calls the callback with whether or not the cache exists. | 71 // Calls the callback with whether or not the cache exists. |
| 71 void HasCache(const std::string& cache_name, | 72 void HasCache(const base::string16& cache_name, |
| 72 const BoolAndErrorCallback& callback); | 73 const BoolAndErrorCallback& callback); |
| 73 | 74 |
| 74 // Deletes the cache if it exists. If it doesn't exist, | 75 // Deletes the cache if it exists. If it doesn't exist, |
| 75 // CACHE_STORAGE_ERROR_NOT_FOUND is returned. | 76 // CACHE_STORAGE_ERROR_NOT_FOUND is returned. |
| 76 void DeleteCache(const std::string& cache_name, | 77 void DeleteCache(const base::string16& cache_name, |
| 77 const BoolAndErrorCallback& callback); | 78 const BoolAndErrorCallback& callback); |
| 78 | 79 |
| 79 // Calls the callback with a vector of cache names (keys) available. | 80 // Calls the callback with a vector of cache names (keys) available. |
| 80 void EnumerateCaches(const StringsAndErrorCallback& callback); | 81 void EnumerateCaches(const StringsAndErrorCallback& callback); |
| 81 | 82 |
| 82 // Calls match on the cache with the given |cache_name|. | 83 // Calls match on the cache with the given |cache_name|. |
| 83 void MatchCache(const std::string& cache_name, | 84 void MatchCache(const base::string16& cache_name, |
| 84 scoped_ptr<ServiceWorkerFetchRequest> request, | 85 scoped_ptr<ServiceWorkerFetchRequest> request, |
| 85 const CacheStorageCache::ResponseCallback& callback); | 86 const CacheStorageCache::ResponseCallback& callback); |
| 86 | 87 |
| 87 // Calls match on all of the caches in parallel, calling |callback| with the | 88 // Calls match on all of the caches in parallel, calling |callback| with the |
| 88 // first response found. Note that if multiple caches have the same | 89 // first response found. Note that if multiple caches have the same |
| 89 // request/response then it is not defined which cache's response will be | 90 // request/response then it is not defined which cache's response will be |
| 90 // returned. If no response is found then |callback| is called with | 91 // returned. If no response is found then |callback| is called with |
| 91 // CACHE_STORAGE_ERROR_NOT_FOUND. | 92 // CACHE_STORAGE_ERROR_NOT_FOUND. |
| 92 void MatchAllCaches(scoped_ptr<ServiceWorkerFetchRequest> request, | 93 void MatchAllCaches(scoped_ptr<ServiceWorkerFetchRequest> request, |
| 93 const CacheStorageCache::ResponseCallback& callback); | 94 const CacheStorageCache::ResponseCallback& callback); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 105 void StartAsyncOperationForTesting(); | 106 void StartAsyncOperationForTesting(); |
| 106 void CompleteAsyncOperationForTesting(); | 107 void CompleteAsyncOperationForTesting(); |
| 107 | 108 |
| 108 private: | 109 private: |
| 109 friend class TestCacheStorage; | 110 friend class TestCacheStorage; |
| 110 | 111 |
| 111 class MemoryLoader; | 112 class MemoryLoader; |
| 112 class SimpleCacheLoader; | 113 class SimpleCacheLoader; |
| 113 class CacheLoader; | 114 class CacheLoader; |
| 114 | 115 |
| 115 typedef std::map<std::string, base::WeakPtr<CacheStorageCache>> CacheMap; | 116 typedef std::map<base::string16, base::WeakPtr<CacheStorageCache>> CacheMap; |
| 116 | 117 |
| 117 // Return a CacheStorageCache for the given name if the name is known. If the | 118 // Return a CacheStorageCache for the given name if the name is known. If the |
| 118 // CacheStorageCache has been deleted, creates a new one. | 119 // CacheStorageCache has been deleted, creates a new one. |
| 119 scoped_refptr<CacheStorageCache> GetLoadedCache( | 120 scoped_refptr<CacheStorageCache> GetLoadedCache( |
| 120 const std::string& cache_name); | 121 const base::string16& cache_name); |
| 121 | 122 |
| 122 // Holds a reference to a cache for thirty seconds. | 123 // Holds a reference to a cache for thirty seconds. |
| 123 void TemporarilyPreserveCache(const scoped_refptr<CacheStorageCache>& cache); | 124 void TemporarilyPreserveCache(const scoped_refptr<CacheStorageCache>& cache); |
| 124 virtual void SchedulePreservedCacheRemoval( | 125 virtual void SchedulePreservedCacheRemoval( |
| 125 const base::Closure& callback); // Virtual for testing. | 126 const base::Closure& callback); // Virtual for testing. |
| 126 void RemovePreservedCache(const CacheStorageCache* cache); | 127 void RemovePreservedCache(const CacheStorageCache* cache); |
| 127 | 128 |
| 128 // Initializer and its callback are below. | 129 // Initializer and its callback are below. |
| 129 void LazyInit(); | 130 void LazyInit(); |
| 130 void LazyInitImpl(); | 131 void LazyInitImpl(); |
| 131 void LazyInitDidLoadIndex( | 132 void LazyInitDidLoadIndex( |
| 132 scoped_ptr<std::vector<std::string>> indexed_cache_names); | 133 scoped_ptr<std::vector<base::string16>> indexed_cache_names); |
| 133 | 134 |
| 134 // The Open and CreateCache callbacks are below. | 135 // The Open and CreateCache callbacks are below. |
| 135 void OpenCacheImpl(const std::string& cache_name, | 136 void OpenCacheImpl(const base::string16& cache_name, |
| 136 const CacheAndErrorCallback& callback); | 137 const CacheAndErrorCallback& callback); |
| 137 void CreateCacheDidCreateCache(const std::string& cache_name, | 138 void CreateCacheDidCreateCache(const base::string16& cache_name, |
| 138 const CacheAndErrorCallback& callback, | 139 const CacheAndErrorCallback& callback, |
| 139 const scoped_refptr<CacheStorageCache>& cache); | 140 const scoped_refptr<CacheStorageCache>& cache); |
| 140 void CreateCacheDidWriteIndex(const CacheAndErrorCallback& callback, | 141 void CreateCacheDidWriteIndex(const CacheAndErrorCallback& callback, |
| 141 const scoped_refptr<CacheStorageCache>& cache, | 142 const scoped_refptr<CacheStorageCache>& cache, |
| 142 bool success); | 143 bool success); |
| 143 | 144 |
| 144 // The HasCache callbacks are below. | 145 // The HasCache callbacks are below. |
| 145 void HasCacheImpl(const std::string& cache_name, | 146 void HasCacheImpl(const base::string16& cache_name, |
| 146 const BoolAndErrorCallback& callback); | 147 const BoolAndErrorCallback& callback); |
| 147 | 148 |
| 148 // The DeleteCache callbacks are below. | 149 // The DeleteCache callbacks are below. |
| 149 void DeleteCacheImpl(const std::string& cache_name, | 150 void DeleteCacheImpl(const base::string16& cache_name, |
| 150 const BoolAndErrorCallback& callback); | 151 const BoolAndErrorCallback& callback); |
| 151 | 152 |
| 152 void DeleteCacheDidClose(const std::string& cache_name, | 153 void DeleteCacheDidClose(const base::string16& cache_name, |
| 153 const BoolAndErrorCallback& callback, | 154 const BoolAndErrorCallback& callback, |
| 154 const StringVector& ordered_cache_names, | 155 const StringVector& ordered_cache_names, |
| 155 const scoped_refptr<CacheStorageCache>& cache, | 156 const scoped_refptr<CacheStorageCache>& cache, |
| 156 int64_t cache_size); | 157 int64_t cache_size); |
| 157 void DeleteCacheDidWriteIndex(const std::string& cache_name, | 158 void DeleteCacheDidWriteIndex(const base::string16& cache_name, |
| 158 const BoolAndErrorCallback& callback, | 159 const BoolAndErrorCallback& callback, |
| 159 int cache_size, | 160 int cache_size, |
| 160 bool success); | 161 bool success); |
| 161 void DeleteCacheDidCleanUp(const BoolAndErrorCallback& callback, | 162 void DeleteCacheDidCleanUp(const BoolAndErrorCallback& callback, |
| 162 bool success); | 163 bool success); |
| 163 | 164 |
| 164 // The EnumerateCache callbacks are below. | 165 // The EnumerateCache callbacks are below. |
| 165 void EnumerateCachesImpl(const StringsAndErrorCallback& callback); | 166 void EnumerateCachesImpl(const StringsAndErrorCallback& callback); |
| 166 | 167 |
| 167 // The MatchCache callbacks are below. | 168 // The MatchCache callbacks are below. |
| 168 void MatchCacheImpl(const std::string& cache_name, | 169 void MatchCacheImpl(const base::string16& cache_name, |
| 169 scoped_ptr<ServiceWorkerFetchRequest> request, | 170 scoped_ptr<ServiceWorkerFetchRequest> request, |
| 170 const CacheStorageCache::ResponseCallback& callback); | 171 const CacheStorageCache::ResponseCallback& callback); |
| 171 void MatchCacheDidMatch(const scoped_refptr<CacheStorageCache>& cache, | 172 void MatchCacheDidMatch(const scoped_refptr<CacheStorageCache>& cache, |
| 172 const CacheStorageCache::ResponseCallback& callback, | 173 const CacheStorageCache::ResponseCallback& callback, |
| 173 CacheStorageError error, | 174 CacheStorageError error, |
| 174 scoped_ptr<ServiceWorkerResponse> response, | 175 scoped_ptr<ServiceWorkerResponse> response, |
| 175 scoped_ptr<storage::BlobDataHandle> handle); | 176 scoped_ptr<storage::BlobDataHandle> handle); |
| 176 | 177 |
| 177 // The MatchAllCaches callbacks are below. | 178 // The MatchAllCaches callbacks are below. |
| 178 void MatchAllCachesImpl(scoped_ptr<ServiceWorkerFetchRequest> request, | 179 void MatchAllCachesImpl(scoped_ptr<ServiceWorkerFetchRequest> request, |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 GURL origin_; | 247 GURL origin_; |
| 247 | 248 |
| 248 base::WeakPtrFactory<CacheStorage> weak_factory_; | 249 base::WeakPtrFactory<CacheStorage> weak_factory_; |
| 249 | 250 |
| 250 DISALLOW_COPY_AND_ASSIGN(CacheStorage); | 251 DISALLOW_COPY_AND_ASSIGN(CacheStorage); |
| 251 }; | 252 }; |
| 252 | 253 |
| 253 } // namespace content | 254 } // namespace content |
| 254 | 255 |
| 255 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_H_ | 256 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_H_ |
| OLD | NEW |